Updated wxDataViewCtrl sample
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@37654 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
@@ -24,224 +24,34 @@
|
|||||||
#include "mondrian.xpm"
|
#include "mondrian.xpm"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// -------------------- wxDataViewControl --------------------
|
#include "wx/dataview.h"
|
||||||
|
|
||||||
// wxDataViewStore
|
// -------------------------------------
|
||||||
|
// MyTextModel
|
||||||
|
// -------------------------------------
|
||||||
|
|
||||||
class wxDataViewStore
|
class MyTextModel: public wxDataViewListModel
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
wxDataViewStore() { }
|
MyTextModel() {}
|
||||||
virtual ~wxDataViewStore() { }
|
|
||||||
|
|
||||||
protected:
|
virtual size_t GetNumberOfRows()
|
||||||
DECLARE_NO_COPY_CLASS(wxDataViewStore)
|
{ return 1000; }
|
||||||
};
|
virtual size_t GetNumberOfCols()
|
||||||
|
{ return 3; }
|
||||||
|
// as reported by wxVariant
|
||||||
// wxDataViewListStoreBase
|
virtual wxString GetColType( size_t col )
|
||||||
|
{ return wxT("string"); }
|
||||||
class wxDataViewListStoreBase: public wxDataViewStore
|
virtual wxVariant GetValue( size_t col, size_t row )
|
||||||
{
|
{ wxString tmp;
|
||||||
public:
|
tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col );
|
||||||
wxDataViewListStoreBase() { }
|
return tmp;
|
||||||
|
}
|
||||||
virtual bool AppendRow() = 0;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
DECLARE_NO_COPY_CLASS(wxDataViewListStoreBase)
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// wxDataViewCtrlBase
|
|
||||||
|
|
||||||
class wxDataViewCtrlBase: public wxControl
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
wxDataViewCtrlBase();
|
|
||||||
|
|
||||||
// Define public API here
|
|
||||||
|
|
||||||
virtual bool AppendStringColumn( const wxString &label, int index ) = 0;
|
|
||||||
|
|
||||||
virtual bool AssociateStore( wxDataViewStore *store );
|
|
||||||
wxDataViewStore* GetStore();
|
|
||||||
|
|
||||||
private:
|
|
||||||
wxDataViewStore *m_store;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
DECLARE_NO_COPY_CLASS(wxDataViewCtrlBase)
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
// -------------------- GTK2 header --------------------
|
|
||||||
|
|
||||||
#ifdef __WXGTK20__
|
|
||||||
|
|
||||||
#include "wx/gtk/private.h"
|
|
||||||
|
|
||||||
class wxDataViewListStore: public wxDataViewListStoreBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
wxDataViewListStore();
|
|
||||||
|
|
||||||
// interface
|
|
||||||
virtual bool AppendRow();
|
|
||||||
|
|
||||||
// implementation
|
|
||||||
GtkListStore* GetGtkListStore() { return m_store; }
|
|
||||||
|
|
||||||
private:
|
|
||||||
GtkListStore *m_store;
|
|
||||||
|
|
||||||
protected:
|
|
||||||
DECLARE_NO_COPY_CLASS(wxDataViewListStore)
|
|
||||||
};
|
|
||||||
|
|
||||||
class wxDataViewCtrl: public wxDataViewCtrlBase
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
wxDataViewCtrl()
|
|
||||||
{
|
|
||||||
Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
wxDataViewCtrl( wxWindow *parent, wxWindowID id,
|
|
||||||
const wxPoint& pos = wxDefaultPosition,
|
|
||||||
const wxSize& size = wxDefaultSize, long style = 0,
|
|
||||||
const wxValidator& validator = wxDefaultValidator )
|
|
||||||
{
|
|
||||||
Create(parent, id, pos, size, style, validator );
|
|
||||||
}
|
|
||||||
|
|
||||||
virtual ~wxDataViewCtrl();
|
|
||||||
|
|
||||||
void Init();
|
|
||||||
|
|
||||||
bool Create(wxWindow *parent, wxWindowID id,
|
|
||||||
const wxPoint& pos = wxDefaultPosition,
|
|
||||||
const wxSize& size = wxDefaultSize, long style = 0,
|
|
||||||
const wxValidator& validator = wxDefaultValidator );
|
|
||||||
|
|
||||||
virtual bool AppendStringColumn( const wxString &label, int index );
|
|
||||||
|
|
||||||
virtual bool AssociateStore( wxDataViewStore *store );
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
|
||||||
DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
|
|
||||||
DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// -------------------- wxDataViewControl --------------------
|
|
||||||
|
|
||||||
wxDataViewCtrlBase::wxDataViewCtrlBase()
|
|
||||||
{
|
|
||||||
m_store = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool wxDataViewCtrlBase::AssociateStore( wxDataViewStore *store )
|
// -------------------------------------
|
||||||
{
|
// MyApp
|
||||||
m_store = store;
|
// -------------------------------------
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
wxDataViewStore* wxDataViewCtrlBase::GetStore()
|
|
||||||
{
|
|
||||||
return m_store;
|
|
||||||
}
|
|
||||||
|
|
||||||
IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl,wxControl)
|
|
||||||
|
|
||||||
// -------------------- GTK2 implementaion --------------------
|
|
||||||
|
|
||||||
#ifdef __WXGTK20__
|
|
||||||
|
|
||||||
// wxDataViewListStore
|
|
||||||
|
|
||||||
wxDataViewListStore::wxDataViewListStore()
|
|
||||||
{
|
|
||||||
m_store = gtk_list_store_new( 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING );
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxDataViewListStore::AppendRow()
|
|
||||||
{
|
|
||||||
GtkTreeIter iter;
|
|
||||||
gtk_list_store_append( m_store, &iter );
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// wxDataViewCtrl
|
|
||||||
|
|
||||||
wxDataViewCtrl::~wxDataViewCtrl()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
void wxDataViewCtrl::Init()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
|
|
||||||
const wxPoint& pos, const wxSize& size,
|
|
||||||
long style, const wxValidator& validator )
|
|
||||||
{
|
|
||||||
Init();
|
|
||||||
|
|
||||||
m_needParent = TRUE;
|
|
||||||
m_acceptsFocus = TRUE;
|
|
||||||
|
|
||||||
if (!PreCreation( parent, pos, size ) ||
|
|
||||||
!CreateBase( parent, id, pos, size, style, validator ))
|
|
||||||
{
|
|
||||||
wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_widget = gtk_tree_view_new();
|
|
||||||
|
|
||||||
m_parent->DoAddChild( this );
|
|
||||||
|
|
||||||
PostCreation(size);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxDataViewCtrl::AppendStringColumn( const wxString &label, int index )
|
|
||||||
{
|
|
||||||
GtkCellRenderer *renderer
|
|
||||||
= gtk_cell_renderer_text_new();
|
|
||||||
|
|
||||||
GtkTreeViewColumn *column
|
|
||||||
= gtk_tree_view_column_new_with_attributes( wxGTK_CONV(label), renderer, "text", index, NULL );
|
|
||||||
|
|
||||||
gtk_tree_view_append_column( GTK_TREE_VIEW(m_widget), column );
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool wxDataViewCtrl::AssociateStore( wxDataViewStore *store )
|
|
||||||
{
|
|
||||||
wxDataViewCtrlBase::AssociateStore( store );
|
|
||||||
|
|
||||||
// Right now we only have the GTK+ port's
|
|
||||||
// list store variant, so cast to that...
|
|
||||||
|
|
||||||
wxDataViewListStore *liststore = (wxDataViewListStore*) store;
|
|
||||||
|
|
||||||
gtk_tree_view_set_model( GTK_TREE_VIEW(m_widget), GTK_TREE_MODEL(liststore->GetGtkListStore()) );
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// -------------------- wxDataViewControl --------------------
|
|
||||||
|
|
||||||
class MyApp: public wxApp
|
class MyApp: public wxApp
|
||||||
{
|
{
|
||||||
@@ -249,6 +59,10 @@ public:
|
|||||||
bool OnInit(void);
|
bool OnInit(void);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// -------------------------------------
|
||||||
|
// MyFrame
|
||||||
|
// -------------------------------------
|
||||||
|
|
||||||
class MyFrame: public wxFrame
|
class MyFrame: public wxFrame
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
@@ -262,20 +76,19 @@ private:
|
|||||||
wxDataViewCtrl* dataview;
|
wxDataViewCtrl* dataview;
|
||||||
};
|
};
|
||||||
|
|
||||||
// ID for the menu commands
|
// -------------------------------------
|
||||||
|
// MyApp
|
||||||
|
// -------------------------------------
|
||||||
|
|
||||||
#define DYNAMIC_QUIT wxID_EXIT
|
#define DYNAMIC_QUIT wxID_EXIT
|
||||||
#define DYNAMIC_ABOUT wxID_ABOUT
|
#define DYNAMIC_ABOUT wxID_ABOUT
|
||||||
|
|
||||||
// Create a new application object
|
|
||||||
IMPLEMENT_APP (MyApp)
|
IMPLEMENT_APP (MyApp)
|
||||||
|
|
||||||
// `Main program' equivalent, creating windows and returning main app frame
|
|
||||||
bool MyApp::OnInit(void)
|
bool MyApp::OnInit(void)
|
||||||
{
|
{
|
||||||
// Create the main frame window
|
|
||||||
MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 450, 340);
|
MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 450, 340);
|
||||||
|
|
||||||
// Show the frame
|
|
||||||
frame->Show(true);
|
frame->Show(true);
|
||||||
|
|
||||||
SetTopWindow(frame);
|
SetTopWindow(frame);
|
||||||
@@ -287,18 +100,15 @@ bool MyApp::OnInit(void)
|
|||||||
// MyFrame
|
// MyFrame
|
||||||
// -------------------------------------
|
// -------------------------------------
|
||||||
|
|
||||||
// My frame constructor
|
|
||||||
MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
|
MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
|
||||||
wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
|
wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
|
||||||
{
|
{
|
||||||
// Give it an icon
|
|
||||||
#ifdef __WXMSW__
|
#ifdef __WXMSW__
|
||||||
SetIcon(wxIcon(_T("mondrian")));
|
SetIcon(wxIcon(_T("mondrian")));
|
||||||
#else
|
#else
|
||||||
SetIcon(wxIcon(mondrian_xpm));
|
SetIcon(wxIcon(mondrian_xpm));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// Make a menubar
|
|
||||||
wxMenu *file_menu = new wxMenu;
|
wxMenu *file_menu = new wxMenu;
|
||||||
|
|
||||||
file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
|
file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
|
||||||
@@ -317,17 +127,12 @@ MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
|
|||||||
|
|
||||||
|
|
||||||
dataview = new wxDataViewCtrl( this, -1 );
|
dataview = new wxDataViewCtrl( this, -1 );
|
||||||
dataview->AppendStringColumn( wxT("first"), 0 );
|
dataview->AppendStringColumn( wxT("first") );
|
||||||
dataview->AppendStringColumn( wxT("second"), 1 );
|
dataview->AppendStringColumn( wxT("second") );
|
||||||
dataview->AppendStringColumn( wxT("third"), 2 );
|
dataview->AppendStringColumn( wxT("third") );
|
||||||
|
|
||||||
wxDataViewListStore *store = new wxDataViewListStore;
|
MyTextModel *model = new MyTextModel;
|
||||||
store->AppendRow();
|
dataview->AssociateModel( Model );
|
||||||
store->AppendRow();
|
|
||||||
store->AppendRow();
|
|
||||||
store->AppendRow();
|
|
||||||
|
|
||||||
dataview->AssociateStore( store );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
|
||||||
|
Reference in New Issue
Block a user