1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DataVewCtrl wxWidgets sample
4 // Author: Robert Roebling
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "mondrian.xpm"
27 // -------------------- wxDataViewControl --------------------
35 virtual ~wxDataViewStore() { }
38 DECLARE_NO_COPY_CLASS(wxDataViewStore
)
42 // wxDataViewListStoreBase
44 class wxDataViewListStoreBase
: public wxDataViewStore
47 wxDataViewListStoreBase() { }
49 virtual bool AppendRow() = 0;
52 DECLARE_NO_COPY_CLASS(wxDataViewListStoreBase
)
58 class wxDataViewCtrlBase
: public wxControl
63 // Define public API here
65 virtual bool AppendStringColumn( const wxString
&label
, int index
) = 0;
67 virtual bool AssociateStore( wxDataViewStore
*store
);
68 wxDataViewStore
* GetStore();
71 wxDataViewStore
*m_store
;
74 DECLARE_NO_COPY_CLASS(wxDataViewCtrlBase
)
78 // -------------------- GTK2 header --------------------
82 #include "wx/gtk/private.h"
84 class wxDataViewListStore
: public wxDataViewListStoreBase
87 wxDataViewListStore();
90 virtual bool AppendRow();
93 GtkListStore
* GetGtkListStore() { return m_store
; }
96 GtkListStore
*m_store
;
99 DECLARE_NO_COPY_CLASS(wxDataViewListStore
)
102 class wxDataViewCtrl
: public wxDataViewCtrlBase
110 wxDataViewCtrl( wxWindow
*parent
, wxWindowID id
,
111 const wxPoint
& pos
= wxDefaultPosition
,
112 const wxSize
& size
= wxDefaultSize
, long style
= 0,
113 const wxValidator
& validator
= wxDefaultValidator
)
115 Create(parent
, id
, pos
, size
, style
, validator
);
118 virtual ~wxDataViewCtrl();
122 bool Create(wxWindow
*parent
, wxWindowID id
,
123 const wxPoint
& pos
= wxDefaultPosition
,
124 const wxSize
& size
= wxDefaultSize
, long style
= 0,
125 const wxValidator
& validator
= wxDefaultValidator
);
127 virtual bool AppendStringColumn( const wxString
&label
, int index
);
129 virtual bool AssociateStore( wxDataViewStore
*store
);
133 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl
)
134 DECLARE_NO_COPY_CLASS(wxDataViewCtrl
)
139 // -------------------- wxDataViewControl --------------------
141 wxDataViewCtrlBase::wxDataViewCtrlBase()
146 bool wxDataViewCtrlBase::AssociateStore( wxDataViewStore
*store
)
153 wxDataViewStore
* wxDataViewCtrlBase::GetStore()
158 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl
,wxControl
)
160 // -------------------- GTK2 implementaion --------------------
164 // wxDataViewListStore
166 wxDataViewListStore::wxDataViewListStore()
168 m_store
= gtk_list_store_new( 3, G_TYPE_STRING
, G_TYPE_STRING
, G_TYPE_STRING
);
171 bool wxDataViewListStore::AppendRow()
174 gtk_list_store_append( m_store
, &iter
);
182 wxDataViewCtrl::~wxDataViewCtrl()
186 void wxDataViewCtrl::Init()
190 bool wxDataViewCtrl::Create(wxWindow
*parent
, wxWindowID id
,
191 const wxPoint
& pos
, const wxSize
& size
,
192 long style
, const wxValidator
& validator
)
197 m_acceptsFocus
= TRUE
;
199 if (!PreCreation( parent
, pos
, size
) ||
200 !CreateBase( parent
, id
, pos
, size
, style
, validator
))
202 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
206 m_widget
= gtk_tree_view_new();
208 m_parent
->DoAddChild( this );
215 bool wxDataViewCtrl::AppendStringColumn( const wxString
&label
, int index
)
217 GtkCellRenderer
*renderer
218 = gtk_cell_renderer_text_new();
220 GtkTreeViewColumn
*column
221 = gtk_tree_view_column_new_with_attributes( wxGTK_CONV(label
), renderer
, "text", index
, NULL
);
223 gtk_tree_view_append_column( GTK_TREE_VIEW(m_widget
), column
);
228 bool wxDataViewCtrl::AssociateStore( wxDataViewStore
*store
)
230 wxDataViewCtrlBase::AssociateStore( store
);
232 // Right now we only have the GTK+ port's
233 // list store variant, so cast to that...
235 wxDataViewListStore
*liststore
= (wxDataViewListStore
*) store
;
237 gtk_tree_view_set_model( GTK_TREE_VIEW(m_widget
), GTK_TREE_MODEL(liststore
->GetGtkListStore()) );
244 // -------------------- wxDataViewControl --------------------
246 class MyApp
: public wxApp
252 class MyFrame
: public wxFrame
255 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
258 void OnQuit(wxCommandEvent
& event
);
259 void OnAbout(wxCommandEvent
& event
);
262 wxDataViewCtrl
* dataview
;
265 // ID for the menu commands
266 #define DYNAMIC_QUIT wxID_EXIT
267 #define DYNAMIC_ABOUT wxID_ABOUT
269 // Create a new application object
270 IMPLEMENT_APP (MyApp
)
272 // `Main program' equivalent, creating windows and returning main app frame
273 bool MyApp::OnInit(void)
275 // Create the main frame window
276 MyFrame
*frame
= new MyFrame(NULL
, _T("Dynamic wxWidgets App"), 50, 50, 450, 340);
286 // -------------------------------------
288 // -------------------------------------
290 // My frame constructor
291 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
292 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
296 SetIcon(wxIcon(_T("mondrian")));
298 SetIcon(wxIcon(mondrian_xpm
));
302 wxMenu
*file_menu
= new wxMenu
;
304 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
305 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
306 wxMenuBar
*menu_bar
= new wxMenuBar
;
307 menu_bar
->Append(file_menu
, _T("&File"));
308 SetMenuBar(menu_bar
);
310 // You used to have to do some casting for param 4, but now there are type-safe handlers
311 Connect( DYNAMIC_QUIT
, wxID_ANY
,
312 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnQuit
) );
313 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
314 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnAbout
) );
319 dataview
= new wxDataViewCtrl( this, -1 );
320 dataview
->AppendStringColumn( wxT("first"), 0 );
321 dataview
->AppendStringColumn( wxT("second"), 1 );
322 dataview
->AppendStringColumn( wxT("third"), 2 );
324 wxDataViewListStore
*store
= new wxDataViewListStore
;
330 dataview
->AssociateStore( store
);
333 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
338 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
340 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
341 _T("About DataView"), wxOK
);