]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
22538962f95f7270ca67582fd6cfbfb413d5b9c3
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: DataVewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by:
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #ifndef __WXMSW__
24 #include "mondrian.xpm"
25 #endif
26
27 // -------------------- wxDataViewControl --------------------
28
29 // wxDataViewStore
30
31 class wxDataViewStore
32 {
33 public:
34 wxDataViewStore() { }
35 virtual ~wxDataViewStore() { }
36
37 protected:
38 DECLARE_NO_COPY_CLASS(wxDataViewStore)
39 };
40
41
42 // wxDataViewListStoreBase
43
44 class wxDataViewListStoreBase: public wxDataViewStore
45 {
46 public:
47 wxDataViewListStoreBase() { }
48
49 virtual bool AppendRow() = 0;
50
51 protected:
52 DECLARE_NO_COPY_CLASS(wxDataViewListStoreBase)
53 };
54
55
56 // wxDataViewCtrlBase
57
58 class wxDataViewCtrlBase: public wxControl
59 {
60 public:
61 wxDataViewCtrlBase();
62
63 // Define public API here
64
65 virtual bool AppendStringColumn( const wxString &label, int index ) = 0;
66
67 virtual bool AssociateStore( wxDataViewStore *store );
68 wxDataViewStore* GetStore();
69
70 private:
71 wxDataViewStore *m_store;
72
73 protected:
74 DECLARE_NO_COPY_CLASS(wxDataViewCtrlBase)
75 };
76
77
78 // -------------------- GTK2 header --------------------
79
80 #ifdef __WXGTK20__
81
82 #include "wx/gtk/private.h"
83
84 class wxDataViewListStore: public wxDataViewListStoreBase
85 {
86 public:
87 wxDataViewListStore();
88
89 // interface
90 virtual bool AppendRow();
91
92 // implementation
93 GtkListStore* GetGtkListStore() { return m_store; }
94
95 private:
96 GtkListStore *m_store;
97
98 protected:
99 DECLARE_NO_COPY_CLASS(wxDataViewListStore)
100 };
101
102 class wxDataViewCtrl: public wxDataViewCtrlBase
103 {
104 public:
105 wxDataViewCtrl()
106 {
107 Init();
108 }
109
110 wxDataViewCtrl( wxWindow *parent, wxWindowID id,
111 const wxPoint& pos = wxDefaultPosition,
112 const wxSize& size = wxDefaultSize, long style = 0,
113 const wxValidator& validator = wxDefaultValidator )
114 {
115 Create(parent, id, pos, size, style, validator );
116 }
117
118 virtual ~wxDataViewCtrl();
119
120 void Init();
121
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 );
126
127 virtual bool AppendStringColumn( const wxString &label, int index );
128
129 virtual bool AssociateStore( wxDataViewStore *store );
130
131
132 private:
133 DECLARE_DYNAMIC_CLASS(wxDataViewCtrl)
134 DECLARE_NO_COPY_CLASS(wxDataViewCtrl)
135 };
136
137 #endif
138
139 // -------------------- wxDataViewControl --------------------
140
141 wxDataViewCtrlBase::wxDataViewCtrlBase()
142 {
143 m_store = NULL;
144 }
145
146 bool wxDataViewCtrlBase::AssociateStore( wxDataViewStore *store )
147 {
148 m_store = store;
149
150 return true;
151 }
152
153 wxDataViewStore* wxDataViewCtrlBase::GetStore()
154 {
155 return m_store;
156 }
157
158 IMPLEMENT_DYNAMIC_CLASS(wxDataViewCtrl,wxControl)
159
160 // -------------------- GTK2 implementaion --------------------
161
162 #ifdef __WXGTK20__
163
164 // wxDataViewListStore
165
166 wxDataViewListStore::wxDataViewListStore()
167 {
168 m_store = gtk_list_store_new( 3, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING );
169 }
170
171 bool wxDataViewListStore::AppendRow()
172 {
173 GtkTreeIter iter;
174 gtk_list_store_append( m_store, &iter );
175
176 return true;
177 }
178
179
180 // wxDataViewCtrl
181
182 wxDataViewCtrl::~wxDataViewCtrl()
183 {
184 }
185
186 void wxDataViewCtrl::Init()
187 {
188 }
189
190 bool wxDataViewCtrl::Create(wxWindow *parent, wxWindowID id,
191 const wxPoint& pos, const wxSize& size,
192 long style, const wxValidator& validator )
193 {
194 Init();
195
196 m_needParent = TRUE;
197 m_acceptsFocus = TRUE;
198
199 if (!PreCreation( parent, pos, size ) ||
200 !CreateBase( parent, id, pos, size, style, validator ))
201 {
202 wxFAIL_MSG( wxT("wxDataViewCtrl creation failed") );
203 return FALSE;
204 }
205
206 m_widget = gtk_tree_view_new();
207
208 m_parent->DoAddChild( this );
209
210 PostCreation(size);
211
212 return true;
213 }
214
215 bool wxDataViewCtrl::AppendStringColumn( const wxString &label, int index )
216 {
217 GtkCellRenderer *renderer
218 = gtk_cell_renderer_text_new();
219
220 GtkTreeViewColumn *column
221 = gtk_tree_view_column_new_with_attributes( wxGTK_CONV(label), renderer, "text", index, NULL );
222
223 gtk_tree_view_append_column( GTK_TREE_VIEW(m_widget), column );
224
225 return true;
226 }
227
228 bool wxDataViewCtrl::AssociateStore( wxDataViewStore *store )
229 {
230 wxDataViewCtrlBase::AssociateStore( store );
231
232 // Right now we only have the GTK+ port's
233 // list store variant, so cast to that...
234
235 wxDataViewListStore *liststore = (wxDataViewListStore*) store;
236
237 gtk_tree_view_set_model( GTK_TREE_VIEW(m_widget), GTK_TREE_MODEL(liststore->GetGtkListStore()) );
238
239 return true;
240 }
241
242 #endif
243
244 // -------------------- wxDataViewControl --------------------
245
246 class MyApp: public wxApp
247 {
248 public:
249 bool OnInit(void);
250 };
251
252 class MyFrame: public wxFrame
253 {
254 public:
255 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
256
257 public:
258 void OnQuit(wxCommandEvent& event);
259 void OnAbout(wxCommandEvent& event);
260
261 private:
262 wxDataViewCtrl* dataview;
263 };
264
265 // ID for the menu commands
266 #define DYNAMIC_QUIT wxID_EXIT
267 #define DYNAMIC_ABOUT wxID_ABOUT
268
269 // Create a new application object
270 IMPLEMENT_APP (MyApp)
271
272 // `Main program' equivalent, creating windows and returning main app frame
273 bool MyApp::OnInit(void)
274 {
275 // Create the main frame window
276 MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 450, 340);
277
278 // Show the frame
279 frame->Show(true);
280
281 SetTopWindow(frame);
282
283 return true;
284 }
285
286 // -------------------------------------
287 // MyFrame
288 // -------------------------------------
289
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))
293 {
294 // Give it an icon
295 #ifdef __WXMSW__
296 SetIcon(wxIcon(_T("mondrian")));
297 #else
298 SetIcon(wxIcon(mondrian_xpm));
299 #endif
300
301 // Make a menubar
302 wxMenu *file_menu = new wxMenu;
303
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);
309
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) );
315
316 CreateStatusBar();
317
318
319 dataview = new wxDataViewCtrl( this, -1 );
320 dataview->AppendStringColumn( wxT("first"), 0 );
321 dataview->AppendStringColumn( wxT("second"), 1 );
322 dataview->AppendStringColumn( wxT("third"), 2 );
323
324 wxDataViewListStore *store = new wxDataViewListStore;
325 store->AppendRow();
326 store->AppendRow();
327 store->AppendRow();
328 store->AppendRow();
329
330 dataview->AssociateStore( store );
331 }
332
333 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
334 {
335 Close(true);
336 }
337
338 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
339 {
340 wxMessageDialog dialog(this, _T("This demonstrates the dataview control handling"),
341 _T("About DataView"), wxOK);
342
343 dialog.ShowModal();
344 }
345
346