]>
Commit | Line | Data |
---|---|---|
bd6169a6 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dataview.cpp | |
9861f022 | 3 | // Purpose: wxDataViewCtrl wxWidgets sample |
bd6169a6 | 4 | // Author: Robert Roebling |
9861f022 | 5 | // Modified by: Francesco Montorsi |
bd6169a6 RR |
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 | |
0bcd4039 | 20 | #include "wx/wx.h" |
bd6169a6 RR |
21 | #endif |
22 | ||
b910a8ad | 23 | #include "wx/datetime.h" |
87f0efe2 RR |
24 | #include "wx/splitter.h" |
25 | #include "wx/aboutdlg.h" | |
9861f022 RR |
26 | #include "wx/choicdlg.h" |
27 | #include "wx/numdlg.h" | |
28 | #include "wx/dataview.h" | |
1e510b1e | 29 | #include "wx/spinctrl.h" |
b910a8ad | 30 | |
bd6169a6 | 31 | #ifndef __WXMSW__ |
0bcd4039 | 32 | #include "../sample.xpm" |
bd6169a6 RR |
33 | #endif |
34 | ||
cbc9145c RR |
35 | #include "null.xpm" |
36 | ||
9861f022 RR |
37 | |
38 | #define DEFAULT_ALIGN wxALIGN_LEFT | |
39 | #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES) | |
40 | ||
41 | ||
bd6169a6 | 42 | |
362d7fb9 | 43 | // ------------------------------------- |
5debbdcf | 44 | // MyMusicModel |
362d7fb9 | 45 | // ------------------------------------- |
bd6169a6 | 46 | |
5debbdcf RR |
47 | /* |
48 | Implement this data model | |
49 | Title Artist Year | |
50 | ------------------------------------------------------------- | |
51 | 1: My Music: | |
52 | 2: Pop music | |
53 | 3: You are not alone Michael Jackson 1995 | |
54 | 4: Take a bow Madonna 1994 | |
55 | 5: Classical music | |
56 | 6: Ninth Symphony Ludwig v. Beethoven 1824 | |
57 | 7: German Requiem Johannes Brahms 1868 | |
58 | */ | |
4d496ecb | 59 | |
1e08ad10 RR |
60 | |
61 | ||
62 | class MyMusicModelNode; | |
63 | WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes ); | |
64 | ||
65 | class MyMusicModelNode | |
66 | { | |
67 | public: | |
b5fce9e2 | 68 | MyMusicModelNode( MyMusicModelNode* parent, |
1e08ad10 RR |
69 | const wxString &title, const wxString &artist, const wxString &year ) |
70 | { | |
71 | m_parent = parent; | |
1e08ad10 RR |
72 | m_title = title; |
73 | m_artist = artist; | |
74 | m_year = year; | |
75 | m_isContainer = false; | |
76 | } | |
77 | ||
b5fce9e2 | 78 | MyMusicModelNode( MyMusicModelNode* parent, |
1e08ad10 RR |
79 | const wxString &branch ) |
80 | { | |
81 | m_parent = parent; | |
1e08ad10 RR |
82 | m_title = branch; |
83 | m_isContainer = true; | |
84 | } | |
85 | ||
86 | ~MyMusicModelNode() | |
87 | { | |
88 | size_t count = m_children.GetCount(); | |
89 | size_t i; | |
90 | for (i = 0; i < count; i++) | |
91 | { | |
92 | MyMusicModelNode *child = m_children[i]; | |
93 | delete child; | |
94 | } | |
95 | } | |
96 | ||
1e08ad10 RR |
97 | bool IsContainer() { return m_isContainer; } |
98 | ||
99 | MyMusicModelNode* GetParent() { return m_parent; } | |
100 | MyMusicModelNodes &GetChildren() { return m_children; } | |
101 | MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); } | |
102 | void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); } | |
103 | void Append( MyMusicModelNode* child ) { m_children.Add( child ); } | |
104 | unsigned int GetChildCount() { return m_children.GetCount(); } | |
105 | ||
106 | public: | |
107 | wxString m_title; | |
108 | wxString m_artist; | |
109 | wxString m_year; | |
110 | ||
111 | private: | |
112 | MyMusicModelNode *m_parent; | |
113 | MyMusicModelNodes m_children; | |
1e08ad10 RR |
114 | bool m_isContainer; |
115 | }; | |
116 | ||
aba9bfd0 | 117 | |
5debbdcf | 118 | class MyMusicModel: public wxDataViewModel |
bd6169a6 RR |
119 | { |
120 | public: | |
e63807a8 RR |
121 | |
122 | // constructor | |
123 | ||
1e08ad10 RR |
124 | MyMusicModel() |
125 | { | |
b5fce9e2 RR |
126 | m_root = new MyMusicModelNode( NULL, "My Music" ); |
127 | m_pop = new MyMusicModelNode( m_root, "Pop music" ); | |
1e08ad10 | 128 | m_root->Append( m_pop ); |
b5fce9e2 | 129 | m_pop->Append( new MyMusicModelNode( m_pop, |
1e08ad10 | 130 | "You are not alone", "Michael Jackson", "1995" ) ); |
b5fce9e2 | 131 | m_pop->Append( new MyMusicModelNode( m_pop, |
1e08ad10 | 132 | "Take a bow", "Madonna", "1994" ) ); |
b5fce9e2 | 133 | m_classical = new MyMusicModelNode( m_root, "Classical music" ); |
1e08ad10 | 134 | m_root->Append( m_classical ); |
b5fce9e2 | 135 | m_classical->Append( new MyMusicModelNode( m_classical, |
1e08ad10 | 136 | "Ninth symphony", "Ludwig van Beethoven", "1824" ) ); |
b5fce9e2 | 137 | m_classical->Append( new MyMusicModelNode( m_classical, |
1e08ad10 | 138 | "German Requiem", "Johannes Brahms", "1868" ) ); |
befa9b61 | 139 | m_classicalMusicIsKnownToControl = false; |
1e08ad10 RR |
140 | } |
141 | ||
e63807a8 RR |
142 | // helper methods to change the model |
143 | ||
1e08ad10 RR |
144 | void AddToClassical( const wxString &title, const wxString &artist, const wxString &year ) |
145 | { | |
146 | // add to data | |
147 | MyMusicModelNode *child_node = | |
b5fce9e2 | 148 | new MyMusicModelNode( m_classical, title, artist, year ); |
1e08ad10 RR |
149 | m_classical->Append( child_node ); |
150 | ||
befa9b61 RR |
151 | if (m_classicalMusicIsKnownToControl) |
152 | { | |
153 | // notify control | |
b5fce9e2 RR |
154 | wxDataViewItem child( (void*) child_node ); |
155 | wxDataViewItem parent( (void*) m_classical ); | |
befa9b61 RR |
156 | ItemAdded( parent, child ); |
157 | } | |
1e08ad10 | 158 | } |
e63807a8 RR |
159 | |
160 | void Delete( const wxDataViewItem &item ) | |
161 | { | |
b5fce9e2 | 162 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
9d8fe14a | 163 | wxDataViewItem parent( node->GetParent() ); |
e63807a8 RR |
164 | node->GetParent()->GetChildren().Remove( node ); |
165 | delete node; | |
166 | ||
167 | // notify control | |
9d8fe14a | 168 | ItemDeleted( parent, item ); |
e63807a8 RR |
169 | } |
170 | ||
cf283a47 RR |
171 | // override sorting to always sort branches ascendingly |
172 | ||
173 | int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2 ) | |
174 | { | |
ed903e42 | 175 | if (IsContainer(item1) && IsContainer(item2)) |
cf283a47 RR |
176 | { |
177 | wxVariant value1,value2; | |
178 | GetValue( value1, item1, 0 ); | |
179 | GetValue( value2, item2, 0 ); | |
180 | ||
181 | wxString str1 = value1.GetString(); | |
182 | wxString str2 = value2.GetString(); | |
183 | int res = str1.Cmp( str2 ); | |
184 | if (res) return res; | |
185 | ||
186 | // items must be different | |
187 | unsigned long litem1 = (unsigned long) item1.GetID(); | |
188 | unsigned long litem2 = (unsigned long) item2.GetID(); | |
189 | ||
190 | return litem1-litem2; | |
191 | } | |
192 | ||
193 | return wxDataViewModel::Compare( item1, item2 ); | |
194 | } | |
195 | ||
e63807a8 | 196 | // implementation of base class virtuals to define model |
5debbdcf RR |
197 | |
198 | virtual unsigned int GetColumnCount() const | |
a7f61f76 | 199 | { |
fc5eacdb | 200 | return 3; |
a7f61f76 | 201 | } |
b910a8ad | 202 | |
9861f022 | 203 | virtual wxString GetColumnType( unsigned int col ) const |
5debbdcf RR |
204 | { |
205 | return "string"; | |
206 | } | |
b910a8ad | 207 | |
5debbdcf RR |
208 | virtual void GetValue( wxVariant &variant, |
209 | const wxDataViewItem &item, unsigned int col ) const | |
210 | { | |
b5fce9e2 | 211 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 | 212 | switch (col) |
605c2c4a | 213 | { |
1e08ad10 RR |
214 | case 0: variant = node->m_title; break; |
215 | case 1: variant = node->m_artist; break; | |
216 | case 2: variant = node->m_year; break; | |
217 | default: wxLogError( "MyMusicModel::GetValue: wrong column" ); | |
a7f61f76 | 218 | } |
e152afc3 | 219 | } |
9861f022 | 220 | |
5debbdcf RR |
221 | virtual bool SetValue( const wxVariant &variant, |
222 | const wxDataViewItem &item, unsigned int col ) | |
9861f022 | 223 | { |
b5fce9e2 | 224 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 RR |
225 | switch (col) |
226 | { | |
227 | case 0: node->m_title = variant.GetString(); break; | |
228 | case 1: node->m_artist = variant.GetString(); break; | |
229 | case 2: node->m_year = variant.GetString(); break; | |
230 | default: wxLogError( "MyMusicModel::SetValue: wrong column" ); | |
231 | } | |
9861f022 RR |
232 | } |
233 | ||
ed903e42 | 234 | virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const |
b910a8ad | 235 | { |
ed903e42 RR |
236 | // the invisble root node has no parent |
237 | if (!item.IsOk()) | |
238 | return wxDataViewItem(0); | |
239 | ||
b5fce9e2 | 240 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
ed903e42 RR |
241 | |
242 | // "MyMusic" also has no parent | |
243 | if (node == m_root) | |
244 | return wxDataViewItem(0); | |
245 | ||
246 | return wxDataViewItem( (void*) node->GetParent() ); | |
247 | } | |
248 | ||
249 | virtual bool IsContainer( const wxDataViewItem &item ) const | |
250 | { | |
251 | // the invisble root node can have children (in | |
252 | // our model always "MyMusic") | |
253 | if (!item.IsOk()) | |
1e08ad10 RR |
254 | return true; |
255 | ||
ed903e42 | 256 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 | 257 | return node->IsContainer(); |
e152afc3 | 258 | } |
5debbdcf | 259 | |
5debbdcf | 260 | virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const |
1e510b1e | 261 | { |
b5fce9e2 RR |
262 | MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID(); |
263 | if (!node) | |
264 | return wxDataViewItem( (void*) m_root ); | |
1e08ad10 | 265 | |
1e08ad10 RR |
266 | if (node->GetChildCount() == 0) |
267 | return wxDataViewItem( 0 ); | |
befa9b61 RR |
268 | |
269 | if (node == m_classical) | |
270 | { | |
271 | MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this; | |
272 | model->m_classicalMusicIsKnownToControl = true; | |
273 | } | |
1e510b1e | 274 | |
1e08ad10 | 275 | MyMusicModelNode *first_child = node->GetChildren().Item( 0 ); |
b5fce9e2 | 276 | return wxDataViewItem( (void*) first_child ); |
9861f022 | 277 | } |
1e08ad10 | 278 | |
5debbdcf | 279 | virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const |
241cb04b | 280 | { |
b5fce9e2 | 281 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
ed903e42 RR |
282 | |
283 | // "MyMusic" has no siblings in our model | |
284 | if (node == m_root) | |
1e08ad10 RR |
285 | return wxDataViewItem(0); |
286 | ||
ed903e42 | 287 | MyMusicModelNode *parent = node->GetParent(); |
1e08ad10 | 288 | int pos = parent->GetChildren().Index( node ); |
ed903e42 RR |
289 | |
290 | // Something went wrong | |
1e08ad10 RR |
291 | if (pos == wxNOT_FOUND) |
292 | return wxDataViewItem(0); | |
293 | ||
ed903e42 | 294 | // No more children |
1e08ad10 RR |
295 | if (pos == parent->GetChildCount()-1) |
296 | return wxDataViewItem(0); | |
297 | ||
298 | node = parent->GetChildren().Item( pos+1 ); | |
b5fce9e2 | 299 | return wxDataViewItem( (void*) node ); |
1e08ad10 RR |
300 | } |
301 | ||
302 | private: | |
1e08ad10 RR |
303 | MyMusicModelNode* m_root; |
304 | MyMusicModelNode* m_pop; | |
305 | MyMusicModelNode* m_classical; | |
befa9b61 | 306 | bool m_classicalMusicIsKnownToControl; |
241cb04b RR |
307 | }; |
308 | ||
c534e696 RR |
309 | class MyListModel: public wxDataViewIndexListModel |
310 | { | |
311 | public: | |
312 | MyListModel() : | |
313 | wxDataViewIndexListModel( 100 ) | |
314 | { | |
315 | unsigned int i; | |
316 | for (i = 0; i < 100; i++) | |
317 | { | |
318 | wxString str; | |
319 | str.Printf( "row number %d", i ); | |
320 | m_array.Add( str ); | |
321 | } | |
322 | } | |
323 | ||
324 | // helper methods to change the model | |
325 | ||
326 | void Prepend( const wxString &text ) | |
327 | { | |
328 | m_array.Insert( text, 0 ); | |
329 | RowPrepended(); | |
330 | } | |
331 | ||
332 | void DeleteItem( const wxDataViewItem &item ) | |
333 | { | |
334 | unsigned int row = GetRow( item ); | |
335 | m_array.RemoveAt( row ); | |
336 | RowDeleted( row ); | |
337 | } | |
338 | ||
339 | // implementation of base class virtuals to define model | |
340 | ||
341 | virtual unsigned int GetColumnCount() const | |
342 | { | |
343 | return 2; | |
344 | } | |
345 | ||
346 | virtual wxString GetColumnType( unsigned int col ) const | |
347 | { | |
348 | return "string"; | |
349 | } | |
350 | ||
351 | virtual unsigned int GetRowCount() | |
352 | { | |
353 | return m_array.GetCount(); | |
354 | } | |
355 | ||
356 | virtual void GetValue( wxVariant &variant, | |
357 | unsigned int row, unsigned int col ) const | |
358 | { | |
359 | if (col==0) | |
360 | { | |
361 | variant = m_array[ row ]; | |
362 | } | |
363 | else | |
364 | { | |
365 | wxString str; | |
366 | str.Printf( "row %d col %d", row, col ); | |
367 | variant = str; | |
368 | } | |
369 | } | |
370 | ||
371 | virtual bool SetValue( const wxVariant &variant, | |
372 | unsigned int row, unsigned int col ) | |
373 | { | |
374 | if (col == 0) | |
375 | { | |
376 | m_array[row] = variant.GetString(); | |
377 | return true; | |
378 | } | |
379 | ||
380 | return false; | |
381 | } | |
382 | ||
383 | wxArrayString m_array; | |
384 | }; | |
385 | ||
362d7fb9 RR |
386 | // ------------------------------------- |
387 | // MyApp | |
388 | // ------------------------------------- | |
bd6169a6 RR |
389 | |
390 | class MyApp: public wxApp | |
391 | { | |
392 | public: | |
393 | bool OnInit(void); | |
87f0efe2 | 394 | int OnExit(); |
bd6169a6 RR |
395 | }; |
396 | ||
362d7fb9 RR |
397 | // ------------------------------------- |
398 | // MyFrame | |
399 | // ------------------------------------- | |
400 | ||
87f0efe2 | 401 | class MyFrame : public wxFrame |
bd6169a6 RR |
402 | { |
403 | public: | |
404 | MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); | |
405 | ||
406 | public: | |
407 | void OnQuit(wxCommandEvent& event); | |
408 | void OnAbout(wxCommandEvent& event); | |
c534e696 RR |
409 | |
410 | void OnAddMozart(wxCommandEvent& event); | |
411 | void OnDeleteMusic(wxCommandEvent& event); | |
412 | ||
413 | void OnPrependList(wxCommandEvent& event); | |
414 | void OnDeleteList(wxCommandEvent& event); | |
b910a8ad | 415 | |
d8331a01 RR |
416 | void OnValueChanged( wxDataViewEvent &event ); |
417 | void OnItemAdded( wxDataViewEvent &event ); | |
418 | void OnItemDeleted( wxDataViewEvent &event ); | |
419 | ||
bd6169a6 | 420 | private: |
c534e696 RR |
421 | wxDataViewCtrl* m_musicCtrl; |
422 | wxObjectDataPtr<MyMusicModel> m_music_model; | |
423 | ||
424 | wxDataViewCtrl* m_listCtrl; | |
425 | wxObjectDataPtr<MyListModel> m_list_model; | |
426 | ||
1e08ad10 | 427 | wxTextCtrl * m_log; |
31fb32e1 | 428 | |
241cb04b | 429 | private: |
abd6692c | 430 | DECLARE_EVENT_TABLE() |
241cb04b RR |
431 | }; |
432 | ||
362d7fb9 RR |
433 | // ------------------------------------- |
434 | // MyApp | |
435 | // ------------------------------------- | |
436 | ||
87f0efe2 | 437 | IMPLEMENT_APP(MyApp) |
bd6169a6 | 438 | |
bd6169a6 RR |
439 | bool MyApp::OnInit(void) |
440 | { | |
45e6e6f8 VZ |
441 | if ( !wxApp::OnInit() ) |
442 | return false; | |
443 | ||
87f0efe2 RR |
444 | // build the first frame |
445 | MyFrame *frame = | |
1e08ad10 | 446 | new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440); |
bd6169a6 RR |
447 | frame->Show(true); |
448 | ||
449 | SetTopWindow(frame); | |
bd6169a6 RR |
450 | return true; |
451 | } | |
452 | ||
87f0efe2 RR |
453 | int MyApp::OnExit() |
454 | { | |
455 | return 0; | |
456 | } | |
457 | ||
458 | ||
bd6169a6 RR |
459 | // ------------------------------------- |
460 | // MyFrame | |
461 | // ------------------------------------- | |
462 | ||
87f0efe2 RR |
463 | enum |
464 | { | |
465 | // file menu | |
466 | ID_ABOUT = wxID_ABOUT, | |
87f0efe2 | 467 | ID_EXIT = wxID_EXIT, |
1e08ad10 | 468 | |
d8331a01 RR |
469 | ID_MUSIC_CTRL = 50, |
470 | ||
c534e696 RR |
471 | ID_ADD_MOZART = 100, |
472 | ID_DELETE_MUSIC = 101, | |
473 | ||
474 | ID_PREPEND_LIST = 200, | |
475 | ID_DELETE_LIST = 201 | |
87f0efe2 RR |
476 | }; |
477 | ||
478 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
87f0efe2 | 479 | EVT_MENU( ID_ABOUT, MyFrame::OnAbout ) |
87f0efe2 | 480 | EVT_MENU( ID_EXIT, MyFrame::OnQuit ) |
c534e696 RR |
481 | EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart ) |
482 | EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic ) | |
483 | EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList ) | |
484 | EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList ) | |
d8331a01 RR |
485 | EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded ) |
486 | EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted ) | |
487 | EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) | |
87f0efe2 RR |
488 | END_EVENT_TABLE() |
489 | ||
bd6169a6 RR |
490 | MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): |
491 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
492 | { | |
d8331a01 RR |
493 | m_log = NULL; |
494 | ||
0bcd4039 | 495 | SetIcon(wxICON(sample)); |
bd6169a6 | 496 | |
87f0efe2 RR |
497 | // build the menus: |
498 | ||
bd6169a6 | 499 | wxMenu *file_menu = new wxMenu; |
5debbdcf | 500 | file_menu->Append(ID_ABOUT, "&About"); |
87f0efe2 | 501 | file_menu->AppendSeparator(); |
5debbdcf | 502 | file_menu->Append(ID_EXIT, "E&xit"); |
bd6169a6 | 503 | |
bd6169a6 | 504 | wxMenuBar *menu_bar = new wxMenuBar; |
5debbdcf | 505 | menu_bar->Append(file_menu, "&File"); |
87f0efe2 | 506 | |
bd6169a6 | 507 | SetMenuBar(menu_bar); |
87f0efe2 | 508 | CreateStatusBar(); |
bd6169a6 | 509 | |
1e08ad10 RR |
510 | wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); |
511 | ||
c534e696 | 512 | wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL ); |
bd6169a6 | 513 | |
c534e696 | 514 | // MyMusic |
b910a8ad | 515 | |
d8331a01 | 516 | m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition, |
c534e696 RR |
517 | wxDefaultSize ); |
518 | ||
519 | m_music_model = new MyMusicModel; | |
520 | m_musicCtrl->AssociateModel( m_music_model.get() ); | |
521 | ||
522 | m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, | |
94b1f7bc | 523 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE ); |
c534e696 | 524 | m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200, |
94b1f7bc | 525 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE ); |
c534e696 | 526 | m_musicCtrl->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50, |
9861f022 | 527 | DEFAULT_ALIGN ); |
1e08ad10 | 528 | |
c534e696 RR |
529 | data_sizer->Add( m_musicCtrl, 3, wxGROW ); |
530 | ||
531 | #if 1 | |
532 | ||
533 | // MyList | |
534 | ||
535 | m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, | |
536 | wxDefaultSize ); | |
537 | ||
538 | m_list_model = new MyListModel; | |
539 | m_listCtrl->AssociateModel( m_list_model.get() ); | |
540 | ||
541 | m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 ); | |
542 | m_listCtrl->AppendTextColumn( "index", 1, wxDATAVIEW_CELL_INERT, 120 ); | |
543 | ||
544 | data_sizer->Add( m_listCtrl, 2, wxGROW ); | |
545 | ||
546 | #endif | |
547 | ||
548 | main_sizer->Add( data_sizer, 2, wxGROW ); | |
1e08ad10 RR |
549 | |
550 | wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); | |
551 | ||
c534e696 RR |
552 | button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 ); |
553 | button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 ); | |
554 | button_sizer->Add( 10, 10, 1 ); | |
555 | button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 ); | |
556 | button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 ); | |
1e08ad10 | 557 | |
c534e696 | 558 | main_sizer->Add( button_sizer, 0, wxGROW, 0 ); |
1e08ad10 RR |
559 | |
560 | m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); | |
561 | ||
562 | main_sizer->Add( m_log, 1, wxGROW ); | |
563 | ||
564 | SetSizer( main_sizer ); | |
bd6169a6 RR |
565 | } |
566 | ||
567 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
568 | { | |
569 | Close(true); | |
570 | } | |
571 | ||
c534e696 RR |
572 | void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) ) |
573 | { | |
574 | m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" ); | |
575 | } | |
576 | ||
577 | void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) ) | |
1e08ad10 | 578 | { |
c534e696 | 579 | wxDataViewItem item = m_musicCtrl->GetSelection(); |
1e08ad10 | 580 | if (item.IsOk()) |
9d8fe14a | 581 | m_music_model->Delete( item ); |
e63807a8 RR |
582 | } |
583 | ||
c534e696 | 584 | void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) ) |
e63807a8 | 585 | { |
c534e696 RR |
586 | m_list_model->Prepend( "Test" ); |
587 | } | |
588 | ||
589 | void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) ) | |
590 | { | |
591 | wxDataViewItem item = m_listCtrl->GetSelection(); | |
592 | if (item.IsOk()) | |
9d8fe14a | 593 | m_list_model->DeleteItem( item ); |
1e08ad10 RR |
594 | } |
595 | ||
d8331a01 RR |
596 | void MyFrame::OnItemAdded( wxDataViewEvent &event ) |
597 | { | |
598 | if (!m_log) | |
599 | return; | |
600 | ||
601 | m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_ADDED\n" ); | |
602 | } | |
603 | ||
604 | void MyFrame::OnItemDeleted( wxDataViewEvent &event ) | |
605 | { | |
606 | if (!m_log) | |
607 | return; | |
608 | ||
609 | m_log->AppendText( "EVT_DATAVIEW_MODEL_ITEM_DELETED\n" ); | |
610 | } | |
611 | ||
612 | void MyFrame::OnValueChanged( wxDataViewEvent &event ) | |
613 | { | |
614 | if (!m_log) | |
615 | return; | |
616 | ||
617 | m_log->AppendText( "EVT_DATAVIEW_MODEL_VALUE_CHANGED\n" ); | |
618 | } | |
619 | ||
bd6169a6 RR |
620 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) |
621 | { | |
87f0efe2 RR |
622 | wxAboutDialogInfo info; |
623 | info.SetName(_("DataView sample")); | |
624 | info.SetDescription(_("This sample demonstrates the dataview control handling")); | |
625 | info.SetCopyright(_T("(C) 2007 Robert Roebling")); | |
bd6169a6 | 626 | |
87f0efe2 | 627 | wxAboutBox(info); |
bd6169a6 RR |
628 | } |
629 |