]>
Commit | Line | Data |
---|---|---|
bd6169a6 RR |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: dataview.cpp | |
9861f022 | 3 | // Purpose: wxDataViewCtrl wxWidgets sample |
bd6169a6 | 4 | // Author: Robert Roebling |
b7e9f8b1 | 5 | // Modified by: Francesco Montorsi, Bo Yang |
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 | ||
405a351f RR |
42 | // ------------------------------------- |
43 | // MySpinCtrlInPlaceRenderer | |
44 | // ------------------------------------- | |
45 | ||
46 | class MySpinCtrlInPlaceRenderer: public wxDataViewCustomRenderer | |
47 | { | |
48 | public: | |
49 | MySpinCtrlInPlaceRenderer() : | |
50 | wxDataViewCustomRenderer( wxT("long"), wxDATAVIEW_CELL_EDITABLE ) { } | |
51 | ||
52 | ||
53 | virtual bool HasEditorCtrl() | |
54 | { | |
55 | return true; | |
56 | } | |
57 | virtual wxControl* CreateEditorCtrl( wxWindow *parent, wxRect labelRect, const wxVariant &value ) | |
58 | { | |
59 | long l = value; | |
60 | return new wxSpinCtrl( parent, wxID_ANY, wxEmptyString, | |
61 | labelRect.GetTopLeft(), labelRect.GetSize(), -0, -1, 2010, l ); | |
62 | } | |
63 | virtual bool GetValueFromEditorCtrl( wxControl* editor, wxVariant &value ) | |
64 | { | |
65 | wxSpinCtrl *sc = (wxSpinCtrl*) editor; | |
66 | long l = sc->GetValue(); | |
67 | value = l; | |
68 | return true; | |
69 | } | |
70 | ||
71 | bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) ) | |
72 | { | |
73 | wxString str; | |
74 | str.Printf( wxT("%d"), (int) m_data ); | |
75 | dc->SetTextForeground( *wxBLACK ); | |
76 | dc->DrawText( str, rect.x, rect.y ); | |
77 | return true; | |
78 | } | |
79 | wxSize GetSize() const | |
80 | { | |
81 | return wxSize(80,16); | |
82 | } | |
83 | bool SetValue( const wxVariant &value ) | |
84 | { | |
85 | m_data = value.GetLong(); | |
86 | return true; | |
87 | } | |
88 | bool GetValue( wxVariant &value ) const | |
89 | { | |
90 | value = m_data; | |
91 | return true; | |
92 | } | |
93 | ||
94 | private: | |
95 | long m_data; | |
96 | }; | |
97 | ||
98 | ||
bd6169a6 | 99 | |
362d7fb9 | 100 | // ------------------------------------- |
5debbdcf | 101 | // MyMusicModel |
362d7fb9 | 102 | // ------------------------------------- |
bd6169a6 | 103 | |
5debbdcf RR |
104 | /* |
105 | Implement this data model | |
106 | Title Artist Year | |
107 | ------------------------------------------------------------- | |
108 | 1: My Music: | |
109 | 2: Pop music | |
110 | 3: You are not alone Michael Jackson 1995 | |
111 | 4: Take a bow Madonna 1994 | |
112 | 5: Classical music | |
113 | 6: Ninth Symphony Ludwig v. Beethoven 1824 | |
114 | 7: German Requiem Johannes Brahms 1868 | |
115 | */ | |
4d496ecb | 116 | |
1e08ad10 RR |
117 | |
118 | ||
119 | class MyMusicModelNode; | |
120 | WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes ); | |
121 | ||
122 | class MyMusicModelNode | |
123 | { | |
124 | public: | |
b5fce9e2 | 125 | MyMusicModelNode( MyMusicModelNode* parent, |
405a351f | 126 | const wxString &title, const wxString &artist, int year ) |
1e08ad10 RR |
127 | { |
128 | m_parent = parent; | |
1e08ad10 RR |
129 | m_title = title; |
130 | m_artist = artist; | |
131 | m_year = year; | |
132 | m_isContainer = false; | |
133 | } | |
134 | ||
b5fce9e2 | 135 | MyMusicModelNode( MyMusicModelNode* parent, |
1e08ad10 RR |
136 | const wxString &branch ) |
137 | { | |
138 | m_parent = parent; | |
1e08ad10 | 139 | m_title = branch; |
405a351f | 140 | m_year = -1; |
1e08ad10 RR |
141 | m_isContainer = true; |
142 | } | |
143 | ||
144 | ~MyMusicModelNode() | |
145 | { | |
146 | size_t count = m_children.GetCount(); | |
147 | size_t i; | |
148 | for (i = 0; i < count; i++) | |
149 | { | |
150 | MyMusicModelNode *child = m_children[i]; | |
151 | delete child; | |
152 | } | |
153 | } | |
154 | ||
1e08ad10 RR |
155 | bool IsContainer() { return m_isContainer; } |
156 | ||
157 | MyMusicModelNode* GetParent() { return m_parent; } | |
158 | MyMusicModelNodes &GetChildren() { return m_children; } | |
159 | MyMusicModelNode* GetNthChild( unsigned int n ) { return m_children.Item( n ); } | |
160 | void Insert( MyMusicModelNode* child, unsigned int n) { m_children.Insert( child, n); } | |
161 | void Append( MyMusicModelNode* child ) { m_children.Add( child ); } | |
162 | unsigned int GetChildCount() { return m_children.GetCount(); } | |
163 | ||
164 | public: | |
165 | wxString m_title; | |
166 | wxString m_artist; | |
405a351f | 167 | int m_year; |
1e08ad10 RR |
168 | |
169 | private: | |
170 | MyMusicModelNode *m_parent; | |
171 | MyMusicModelNodes m_children; | |
1e08ad10 RR |
172 | bool m_isContainer; |
173 | }; | |
174 | ||
aba9bfd0 | 175 | |
5debbdcf | 176 | class MyMusicModel: public wxDataViewModel |
bd6169a6 RR |
177 | { |
178 | public: | |
e63807a8 RR |
179 | |
180 | // constructor | |
181 | ||
1e08ad10 RR |
182 | MyMusicModel() |
183 | { | |
b5fce9e2 RR |
184 | m_root = new MyMusicModelNode( NULL, "My Music" ); |
185 | m_pop = new MyMusicModelNode( m_root, "Pop music" ); | |
1e08ad10 | 186 | m_root->Append( m_pop ); |
b5fce9e2 | 187 | m_pop->Append( new MyMusicModelNode( m_pop, |
405a351f | 188 | "You are not alone", "Michael Jackson", 1995 ) ); |
b5fce9e2 | 189 | m_pop->Append( new MyMusicModelNode( m_pop, |
405a351f | 190 | "Take a bow", "Madonna", 1994 ) ); |
b5fce9e2 | 191 | m_classical = new MyMusicModelNode( m_root, "Classical music" ); |
1e08ad10 | 192 | m_root->Append( m_classical ); |
b5fce9e2 | 193 | m_classical->Append( new MyMusicModelNode( m_classical, |
405a351f | 194 | "Ninth symphony", "Ludwig van Beethoven", 1824 ) ); |
b5fce9e2 | 195 | m_classical->Append( new MyMusicModelNode( m_classical, |
405a351f | 196 | "German Requiem", "Johannes Brahms", 1868 ) ); |
befa9b61 | 197 | m_classicalMusicIsKnownToControl = false; |
1e08ad10 RR |
198 | } |
199 | ||
e63807a8 RR |
200 | // helper methods to change the model |
201 | ||
405a351f | 202 | void AddToClassical( const wxString &title, const wxString &artist, int year ) |
1e08ad10 RR |
203 | { |
204 | // add to data | |
205 | MyMusicModelNode *child_node = | |
b5fce9e2 | 206 | new MyMusicModelNode( m_classical, title, artist, year ); |
b7e9f8b1 | 207 | |
1e08ad10 RR |
208 | m_classical->Append( child_node ); |
209 | ||
befa9b61 RR |
210 | if (m_classicalMusicIsKnownToControl) |
211 | { | |
212 | // notify control | |
b5fce9e2 RR |
213 | wxDataViewItem child( (void*) child_node ); |
214 | wxDataViewItem parent( (void*) m_classical ); | |
befa9b61 RR |
215 | ItemAdded( parent, child ); |
216 | } | |
1e08ad10 | 217 | } |
e63807a8 RR |
218 | |
219 | void Delete( const wxDataViewItem &item ) | |
220 | { | |
b5fce9e2 | 221 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
9d8fe14a | 222 | wxDataViewItem parent( node->GetParent() ); |
e63807a8 | 223 | |
d3f00f59 VZ |
224 | node->GetParent()->GetChildren().Remove( node ); |
225 | delete node; | |
405a351f RR |
226 | |
227 | // notify control | |
228 | ItemDeleted( parent, item ); | |
e63807a8 RR |
229 | } |
230 | ||
cf283a47 RR |
231 | // override sorting to always sort branches ascendingly |
232 | ||
7ee7191c RR |
233 | int Compare( const wxDataViewItem &item1, const wxDataViewItem &item2, |
234 | unsigned int column, bool ascending ) | |
cf283a47 | 235 | { |
ed903e42 | 236 | if (IsContainer(item1) && IsContainer(item2)) |
cf283a47 RR |
237 | { |
238 | wxVariant value1,value2; | |
239 | GetValue( value1, item1, 0 ); | |
240 | GetValue( value2, item2, 0 ); | |
241 | ||
242 | wxString str1 = value1.GetString(); | |
243 | wxString str2 = value2.GetString(); | |
244 | int res = str1.Cmp( str2 ); | |
245 | if (res) return res; | |
246 | ||
247 | // items must be different | |
248 | unsigned long litem1 = (unsigned long) item1.GetID(); | |
249 | unsigned long litem2 = (unsigned long) item2.GetID(); | |
250 | ||
251 | return litem1-litem2; | |
252 | } | |
253 | ||
7ee7191c | 254 | return wxDataViewModel::Compare( item1, item2, column, ascending ); |
cf283a47 RR |
255 | } |
256 | ||
e63807a8 | 257 | // implementation of base class virtuals to define model |
5debbdcf RR |
258 | |
259 | virtual unsigned int GetColumnCount() const | |
a7f61f76 | 260 | { |
fc5eacdb | 261 | return 3; |
a7f61f76 | 262 | } |
b910a8ad | 263 | |
9861f022 | 264 | virtual wxString GetColumnType( unsigned int col ) const |
5debbdcf | 265 | { |
405a351f RR |
266 | if (col == 2) |
267 | return "long"; | |
268 | ||
5debbdcf RR |
269 | return "string"; |
270 | } | |
b910a8ad | 271 | |
5debbdcf RR |
272 | virtual void GetValue( wxVariant &variant, |
273 | const wxDataViewItem &item, unsigned int col ) const | |
274 | { | |
b5fce9e2 | 275 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 | 276 | switch (col) |
605c2c4a | 277 | { |
1e08ad10 RR |
278 | case 0: variant = node->m_title; break; |
279 | case 1: variant = node->m_artist; break; | |
405a351f | 280 | case 2: variant = (long) node->m_year; break; |
0bd26819 RR |
281 | default: |
282 | { | |
283 | wxLogError( "MyMusicModel::GetValue: wrong column" ); | |
405a351f RR |
284 | |
285 | // provoke a crash when mouse button down | |
0bd26819 RR |
286 | wxMouseState state = wxGetMouseState(); |
287 | if (state.ShiftDown()) | |
288 | { | |
289 | char *crash = 0; | |
290 | *crash = 0; | |
291 | } | |
292 | } | |
a7f61f76 | 293 | } |
e152afc3 | 294 | } |
9861f022 | 295 | |
5debbdcf RR |
296 | virtual bool SetValue( const wxVariant &variant, |
297 | const wxDataViewItem &item, unsigned int col ) | |
9861f022 | 298 | { |
b5fce9e2 | 299 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 RR |
300 | switch (col) |
301 | { | |
c2a92c80 RR |
302 | case 0: node->m_title = variant.GetString(); return true; |
303 | case 1: node->m_artist = variant.GetString(); return true; | |
304 | case 2: node->m_year = variant.GetLong(); return true; | |
1e08ad10 RR |
305 | default: wxLogError( "MyMusicModel::SetValue: wrong column" ); |
306 | } | |
c2a92c80 | 307 | return false; |
9861f022 RR |
308 | } |
309 | ||
ed903e42 | 310 | virtual wxDataViewItem GetParent( const wxDataViewItem &item ) const |
b910a8ad | 311 | { |
ed903e42 RR |
312 | // the invisble root node has no parent |
313 | if (!item.IsOk()) | |
314 | return wxDataViewItem(0); | |
315 | ||
b5fce9e2 | 316 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
ed903e42 RR |
317 | |
318 | // "MyMusic" also has no parent | |
319 | if (node == m_root) | |
320 | return wxDataViewItem(0); | |
321 | ||
322 | return wxDataViewItem( (void*) node->GetParent() ); | |
323 | } | |
324 | ||
325 | virtual bool IsContainer( const wxDataViewItem &item ) const | |
326 | { | |
327 | // the invisble root node can have children (in | |
328 | // our model always "MyMusic") | |
329 | if (!item.IsOk()) | |
1e08ad10 RR |
330 | return true; |
331 | ||
ed903e42 | 332 | MyMusicModelNode *node = (MyMusicModelNode*) item.GetID(); |
1e08ad10 | 333 | return node->IsContainer(); |
e152afc3 | 334 | } |
5debbdcf | 335 | |
74fe973b | 336 | virtual unsigned int GetChildren( const wxDataViewItem &parent, wxDataViewItemArray &array ) const |
1e510b1e | 337 | { |
b5fce9e2 RR |
338 | MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID(); |
339 | if (!node) | |
74fe973b RR |
340 | { |
341 | array.Add( wxDataViewItem( (void*) m_root ) ); | |
342 | return 1; | |
343 | } | |
1e08ad10 | 344 | |
befa9b61 RR |
345 | if (node == m_classical) |
346 | { | |
347 | MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this; | |
348 | model->m_classicalMusicIsKnownToControl = true; | |
349 | } | |
1e510b1e | 350 | |
74fe973b RR |
351 | if (node->GetChildCount() == 0) |
352 | { | |
353 | return 0; | |
354 | } | |
355 | ||
356 | unsigned int count = node->GetChildren().GetCount(); | |
357 | unsigned int pos; | |
358 | for (pos = 0; pos < count; pos++) | |
359 | { | |
360 | MyMusicModelNode *child = node->GetChildren().Item( pos ); | |
361 | array.Add( wxDataViewItem( (void*) child ) ); | |
362 | } | |
363 | return count; | |
9861f022 | 364 | } |
1e08ad10 | 365 | |
1e08ad10 | 366 | private: |
1e08ad10 RR |
367 | MyMusicModelNode* m_root; |
368 | MyMusicModelNode* m_pop; | |
369 | MyMusicModelNode* m_classical; | |
befa9b61 | 370 | bool m_classicalMusicIsKnownToControl; |
241cb04b RR |
371 | }; |
372 | ||
c534e696 RR |
373 | class MyListModel: public wxDataViewIndexListModel |
374 | { | |
375 | public: | |
376 | MyListModel() : | |
377 | wxDataViewIndexListModel( 100 ) | |
378 | { | |
379 | unsigned int i; | |
380 | for (i = 0; i < 100; i++) | |
381 | { | |
382 | wxString str; | |
383 | str.Printf( "row number %d", i ); | |
384 | m_array.Add( str ); | |
385 | } | |
c9c13e70 RR |
386 | |
387 | m_icon = wxIcon( null_xpm ); | |
c534e696 RR |
388 | } |
389 | ||
390 | // helper methods to change the model | |
391 | ||
392 | void Prepend( const wxString &text ) | |
393 | { | |
394 | m_array.Insert( text, 0 ); | |
395 | RowPrepended(); | |
396 | } | |
397 | ||
398 | void DeleteItem( const wxDataViewItem &item ) | |
399 | { | |
400 | unsigned int row = GetRow( item ); | |
401 | m_array.RemoveAt( row ); | |
402 | RowDeleted( row ); | |
403 | } | |
404 | ||
405 | // implementation of base class virtuals to define model | |
406 | ||
407 | virtual unsigned int GetColumnCount() const | |
408 | { | |
c9c13e70 | 409 | return 3; |
c534e696 RR |
410 | } |
411 | ||
412 | virtual wxString GetColumnType( unsigned int col ) const | |
413 | { | |
c9c13e70 RR |
414 | if (col == 1) |
415 | return "wxDataViewIconText"; | |
416 | ||
c534e696 RR |
417 | return "string"; |
418 | } | |
419 | ||
420 | virtual unsigned int GetRowCount() | |
421 | { | |
422 | return m_array.GetCount(); | |
423 | } | |
424 | ||
425 | virtual void GetValue( wxVariant &variant, | |
426 | unsigned int row, unsigned int col ) const | |
427 | { | |
428 | if (col==0) | |
429 | { | |
430 | variant = m_array[ row ]; | |
c9c13e70 RR |
431 | } else |
432 | if (col==1) | |
433 | { | |
434 | wxDataViewIconText data( "test", m_icon ); | |
435 | variant << data; | |
c534e696 RR |
436 | } |
437 | else | |
438 | { | |
439 | wxString str; | |
440 | str.Printf( "row %d col %d", row, col ); | |
441 | variant = str; | |
442 | } | |
443 | } | |
444 | ||
445 | virtual bool SetValue( const wxVariant &variant, | |
446 | unsigned int row, unsigned int col ) | |
447 | { | |
448 | if (col == 0) | |
449 | { | |
450 | m_array[row] = variant.GetString(); | |
451 | return true; | |
452 | } | |
453 | ||
454 | return false; | |
455 | } | |
456 | ||
457 | wxArrayString m_array; | |
c9c13e70 | 458 | wxIcon m_icon; |
c534e696 RR |
459 | }; |
460 | ||
362d7fb9 RR |
461 | // ------------------------------------- |
462 | // MyApp | |
463 | // ------------------------------------- | |
bd6169a6 RR |
464 | |
465 | class MyApp: public wxApp | |
466 | { | |
467 | public: | |
468 | bool OnInit(void); | |
87f0efe2 | 469 | int OnExit(); |
bd6169a6 RR |
470 | }; |
471 | ||
362d7fb9 RR |
472 | // ------------------------------------- |
473 | // MyFrame | |
474 | // ------------------------------------- | |
475 | ||
87f0efe2 | 476 | class MyFrame : public wxFrame |
bd6169a6 RR |
477 | { |
478 | public: | |
479 | MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); | |
480 | ||
481 | public: | |
482 | void OnQuit(wxCommandEvent& event); | |
483 | void OnAbout(wxCommandEvent& event); | |
c534e696 RR |
484 | |
485 | void OnAddMozart(wxCommandEvent& event); | |
486 | void OnDeleteMusic(wxCommandEvent& event); | |
487 | ||
488 | void OnPrependList(wxCommandEvent& event); | |
489 | void OnDeleteList(wxCommandEvent& event); | |
b910a8ad | 490 | |
d8331a01 RR |
491 | void OnValueChanged( wxDataViewEvent &event ); |
492 | void OnItemAdded( wxDataViewEvent &event ); | |
493 | void OnItemDeleted( wxDataViewEvent &event ); | |
718fd180 | 494 | |
b7e9f8b1 | 495 | void OnActivated( wxDataViewEvent &event ); |
718fd180 RR |
496 | void OnExpanding( wxDataViewEvent &event ); |
497 | void OnExpanded( wxDataViewEvent &event ); | |
498 | void OnCollapsing( wxDataViewEvent &event ); | |
499 | void OnCollapsed( wxDataViewEvent &event ); | |
6848478c | 500 | void OnSelected( wxDataViewEvent &event ); |
718fd180 | 501 | |
d14e1c3a RR |
502 | void OnEditingStarted( wxDataViewEvent &event ); |
503 | void OnEditingDone( wxDataViewEvent &event ); | |
504 | ||
b7e9f8b1 RR |
505 | void OnHeaderClick( wxDataViewEvent &event ); |
506 | void OnHeaderRightClick( wxDataViewEvent &event ); | |
507 | void OnSorted( wxDataViewEvent &event ); | |
508 | ||
509 | void OnRightClick( wxMouseEvent &event ); | |
510 | void OnGoto( wxCommandEvent &event); | |
d8331a01 | 511 | |
bd6169a6 | 512 | private: |
c534e696 RR |
513 | wxDataViewCtrl* m_musicCtrl; |
514 | wxObjectDataPtr<MyMusicModel> m_music_model; | |
515 | ||
516 | wxDataViewCtrl* m_listCtrl; | |
517 | wxObjectDataPtr<MyListModel> m_list_model; | |
fbda518c RR |
518 | |
519 | wxDataViewColumn * m_col; | |
c534e696 | 520 | |
1e08ad10 | 521 | wxTextCtrl * m_log; |
b7e9f8b1 | 522 | wxLog *m_logOld; |
31fb32e1 | 523 | |
241cb04b | 524 | private: |
abd6692c | 525 | DECLARE_EVENT_TABLE() |
241cb04b RR |
526 | }; |
527 | ||
362d7fb9 RR |
528 | // ------------------------------------- |
529 | // MyApp | |
530 | // ------------------------------------- | |
531 | ||
87f0efe2 | 532 | IMPLEMENT_APP(MyApp) |
bd6169a6 | 533 | |
bd6169a6 RR |
534 | bool MyApp::OnInit(void) |
535 | { | |
45e6e6f8 VZ |
536 | if ( !wxApp::OnInit() ) |
537 | return false; | |
538 | ||
87f0efe2 RR |
539 | // build the first frame |
540 | MyFrame *frame = | |
405a351f | 541 | new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 40, 40, 800, 440); |
bd6169a6 RR |
542 | frame->Show(true); |
543 | ||
544 | SetTopWindow(frame); | |
bd6169a6 RR |
545 | return true; |
546 | } | |
547 | ||
87f0efe2 RR |
548 | int MyApp::OnExit() |
549 | { | |
550 | return 0; | |
551 | } | |
552 | ||
553 | ||
bd6169a6 RR |
554 | // ------------------------------------- |
555 | // MyFrame | |
556 | // ------------------------------------- | |
557 | ||
87f0efe2 RR |
558 | enum |
559 | { | |
560 | // file menu | |
561 | ID_ABOUT = wxID_ABOUT, | |
87f0efe2 | 562 | ID_EXIT = wxID_EXIT, |
1e08ad10 | 563 | |
d8331a01 RR |
564 | ID_MUSIC_CTRL = 50, |
565 | ||
c534e696 RR |
566 | ID_ADD_MOZART = 100, |
567 | ID_DELETE_MUSIC = 101, | |
568 | ||
569 | ID_PREPEND_LIST = 200, | |
b7e9f8b1 | 570 | ID_DELETE_LIST = 201, |
405a351f | 571 | ID_GOTO = 202 |
87f0efe2 RR |
572 | }; |
573 | ||
574 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
87f0efe2 | 575 | EVT_MENU( ID_ABOUT, MyFrame::OnAbout ) |
87f0efe2 | 576 | EVT_MENU( ID_EXIT, MyFrame::OnQuit ) |
c534e696 RR |
577 | EVT_BUTTON( ID_ADD_MOZART, MyFrame::OnAddMozart ) |
578 | EVT_BUTTON( ID_DELETE_MUSIC, MyFrame::OnDeleteMusic ) | |
579 | EVT_BUTTON( ID_PREPEND_LIST, MyFrame::OnPrependList ) | |
580 | EVT_BUTTON( ID_DELETE_LIST, MyFrame::OnDeleteList ) | |
b7e9f8b1 | 581 | EVT_BUTTON( ID_GOTO, MyFrame::OnGoto) |
718fd180 | 582 | |
d8331a01 RR |
583 | EVT_DATAVIEW_MODEL_ITEM_ADDED( ID_MUSIC_CTRL, MyFrame::OnItemAdded ) |
584 | EVT_DATAVIEW_MODEL_ITEM_DELETED( ID_MUSIC_CTRL, MyFrame::OnItemDeleted ) | |
585 | EVT_DATAVIEW_MODEL_VALUE_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) | |
b7e9f8b1 | 586 | EVT_DATAVIEW_MODEL_ITEM_CHANGED( ID_MUSIC_CTRL, MyFrame::OnValueChanged ) |
718fd180 | 587 | |
b7e9f8b1 | 588 | EVT_DATAVIEW_ITEM_ACTIVATED(ID_MUSIC_CTRL, MyFrame::OnActivated ) |
718fd180 RR |
589 | EVT_DATAVIEW_ITEM_EXPANDING(ID_MUSIC_CTRL, MyFrame::OnExpanding) |
590 | EVT_DATAVIEW_ITEM_EXPANDED(ID_MUSIC_CTRL, MyFrame::OnExpanded) | |
591 | EVT_DATAVIEW_ITEM_COLLAPSING(ID_MUSIC_CTRL, MyFrame::OnCollapsing) | |
592 | EVT_DATAVIEW_ITEM_COLLAPSED(ID_MUSIC_CTRL, MyFrame::OnCollapsed) | |
6848478c | 593 | EVT_DATAVIEW_ITEM_SELECTED(ID_MUSIC_CTRL, MyFrame::OnSelected) |
718fd180 | 594 | |
d14e1c3a RR |
595 | EVT_DATAVIEW_ITEM_EDITING_STARTED(ID_MUSIC_CTRL, MyFrame::OnEditingStarted) |
596 | EVT_DATAVIEW_ITEM_EDITING_DONE(ID_MUSIC_CTRL, MyFrame::OnEditingDone) | |
597 | ||
598 | ||
b7e9f8b1 RR |
599 | EVT_DATAVIEW_COLUMN_HEADER_CLICK(ID_MUSIC_CTRL, MyFrame::OnHeaderClick) |
600 | EVT_DATAVIEW_COLUMN_HEADER_RIGHT_CLICKED(ID_MUSIC_CTRL, MyFrame::OnHeaderRightClick) | |
601 | EVT_DATAVIEW_COLUMN_SORTED(ID_MUSIC_CTRL, MyFrame::OnSorted) | |
718fd180 | 602 | |
b7e9f8b1 | 603 | EVT_RIGHT_UP(MyFrame::OnRightClick) |
87f0efe2 RR |
604 | END_EVENT_TABLE() |
605 | ||
bd6169a6 RR |
606 | MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): |
607 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
608 | { | |
d8331a01 | 609 | m_log = NULL; |
fbda518c | 610 | m_col = NULL; |
d8331a01 | 611 | |
0bcd4039 | 612 | SetIcon(wxICON(sample)); |
bd6169a6 | 613 | |
87f0efe2 RR |
614 | // build the menus: |
615 | ||
bd6169a6 | 616 | wxMenu *file_menu = new wxMenu; |
5debbdcf | 617 | file_menu->Append(ID_ABOUT, "&About"); |
87f0efe2 | 618 | file_menu->AppendSeparator(); |
5debbdcf | 619 | file_menu->Append(ID_EXIT, "E&xit"); |
bd6169a6 | 620 | |
bd6169a6 | 621 | wxMenuBar *menu_bar = new wxMenuBar; |
5debbdcf | 622 | menu_bar->Append(file_menu, "&File"); |
87f0efe2 | 623 | |
bd6169a6 | 624 | SetMenuBar(menu_bar); |
87f0efe2 | 625 | CreateStatusBar(); |
bd6169a6 | 626 | |
1e08ad10 RR |
627 | wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); |
628 | ||
c534e696 | 629 | wxBoxSizer *data_sizer = new wxBoxSizer( wxHORIZONTAL ); |
bd6169a6 | 630 | |
c534e696 | 631 | // MyMusic |
b910a8ad | 632 | |
d8331a01 | 633 | m_musicCtrl = new wxDataViewCtrl( this, ID_MUSIC_CTRL, wxDefaultPosition, |
b7e9f8b1 | 634 | wxDefaultSize, wxDV_MULTIPLE ); |
c534e696 RR |
635 | |
636 | m_music_model = new MyMusicModel; | |
637 | m_musicCtrl->AssociateModel( m_music_model.get() ); | |
638 | ||
0bd26819 | 639 | wxDataViewColumn *col = m_musicCtrl->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200, |
94b1f7bc | 640 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE ); |
0bd26819 RR |
641 | #if 0 |
642 | // Call this and sorting is enabled | |
643 | // immediatly upon start up. | |
644 | col->SetSortOrder( true ); | |
645 | #endif | |
646 | ||
405a351f | 647 | m_musicCtrl->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 150, |
94b1f7bc | 648 | DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE ); |
405a351f RR |
649 | |
650 | MySpinCtrlInPlaceRenderer *sr = new MySpinCtrlInPlaceRenderer; | |
651 | wxDataViewColumn *column = new wxDataViewColumn( "year", sr, 2, -1, wxALIGN_CENTRE, wxDATAVIEW_COL_SORTABLE ); | |
652 | m_musicCtrl->AppendColumn( column ); | |
1e08ad10 | 653 | |
c534e696 RR |
654 | data_sizer->Add( m_musicCtrl, 3, wxGROW ); |
655 | ||
656 | #if 1 | |
657 | ||
658 | // MyList | |
659 | ||
660 | m_listCtrl = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition, | |
b7e9f8b1 | 661 | wxDefaultSize, wxDV_MULTIPLE ); |
c534e696 RR |
662 | |
663 | m_list_model = new MyListModel; | |
664 | m_listCtrl->AssociateModel( m_list_model.get() ); | |
665 | ||
666 | m_listCtrl->AppendTextColumn( "editable string", 0, wxDATAVIEW_CELL_EDITABLE, 120 ); | |
b04fcede RR |
667 | m_listCtrl->AppendIconTextColumn( "icon", 1, wxDATAVIEW_CELL_INERT, 60 ); |
668 | m_listCtrl->AppendTextColumn( "index", 2, wxDATAVIEW_CELL_INERT, 120 ); | |
c534e696 RR |
669 | |
670 | data_sizer->Add( m_listCtrl, 2, wxGROW ); | |
671 | ||
672 | #endif | |
673 | ||
674 | main_sizer->Add( data_sizer, 2, wxGROW ); | |
1e08ad10 RR |
675 | |
676 | wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); | |
677 | ||
c534e696 RR |
678 | button_sizer->Add( new wxButton( this, ID_ADD_MOZART, "Add Mozart"), 0, wxALL, 10 ); |
679 | button_sizer->Add( new wxButton( this, ID_DELETE_MUSIC, "Delete selected"), 0, wxALL, 10 ); | |
680 | button_sizer->Add( 10, 10, 1 ); | |
681 | button_sizer->Add( new wxButton( this, ID_PREPEND_LIST, "Prepend"), 0, wxALL, 10 ); | |
682 | button_sizer->Add( new wxButton( this, ID_DELETE_LIST, "Delete selected"), 0, wxALL, 10 ); | |
b7e9f8b1 | 683 | button_sizer->Add( new wxButton( this, ID_GOTO, "Goto 50"), 0, wxALL, 10 ); |
1e08ad10 | 684 | |
c534e696 | 685 | main_sizer->Add( button_sizer, 0, wxGROW, 0 ); |
1e08ad10 RR |
686 | |
687 | m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE ); | |
b7e9f8b1 RR |
688 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_log)); |
689 | wxLogMessage("This is the log window"); | |
690 | ||
1e08ad10 RR |
691 | main_sizer->Add( m_log, 1, wxGROW ); |
692 | ||
693 | SetSizer( main_sizer ); | |
bd6169a6 RR |
694 | } |
695 | ||
696 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
697 | { | |
698 | Close(true); | |
699 | } | |
700 | ||
c534e696 RR |
701 | void MyFrame::OnAddMozart(wxCommandEvent& WXUNUSED(event) ) |
702 | { | |
405a351f | 703 | m_music_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", 1787 ); |
c534e696 RR |
704 | } |
705 | ||
706 | void MyFrame::OnDeleteMusic(wxCommandEvent& WXUNUSED(event) ) | |
1e08ad10 | 707 | { |
b7e9f8b1 RR |
708 | wxDataViewItemArray items; |
709 | int len = m_musicCtrl->GetSelections( items ); | |
710 | for( int i = 0; i < len; i ++ ) | |
711 | if (items[i].IsOk()) | |
712 | m_music_model->Delete( items[i] ); | |
e63807a8 RR |
713 | } |
714 | ||
c534e696 | 715 | void MyFrame::OnPrependList( wxCommandEvent& WXUNUSED(event) ) |
e63807a8 | 716 | { |
c534e696 RR |
717 | m_list_model->Prepend( "Test" ); |
718 | } | |
719 | ||
720 | void MyFrame::OnDeleteList( wxCommandEvent& WXUNUSED(event) ) | |
721 | { | |
b7e9f8b1 RR |
722 | wxDataViewItemArray items; |
723 | int len = m_listCtrl->GetSelections( items ); | |
724 | for( int i = 0; i < len; i ++ ) | |
725 | if (items[i].IsOk()) | |
726 | m_list_model->DeleteItem( items[i] ); | |
1e08ad10 RR |
727 | } |
728 | ||
d8331a01 RR |
729 | void MyFrame::OnItemAdded( wxDataViewEvent &event ) |
730 | { | |
731 | if (!m_log) | |
732 | return; | |
733 | ||
b7e9f8b1 | 734 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_MODEL_ITEM_ADDED, Item Id: %d",event.GetItem().GetID()); |
d8331a01 RR |
735 | } |
736 | ||
737 | void MyFrame::OnItemDeleted( wxDataViewEvent &event ) | |
738 | { | |
739 | if (!m_log) | |
740 | return; | |
741 | ||
b7e9f8b1 | 742 | wxLogMessage( "EVT_DATAVIEW_MODEL_ITEM_DELETED, Item Id: %d", event.GetItem().GetID() ); |
d8331a01 RR |
743 | } |
744 | ||
745 | void MyFrame::OnValueChanged( wxDataViewEvent &event ) | |
746 | { | |
747 | if (!m_log) | |
748 | return; | |
749 | ||
b7e9f8b1 RR |
750 | wxLogMessage( "EVT_DATAVIEW_MODEL_VALUE_CHANGED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn() ); |
751 | } | |
752 | ||
753 | void MyFrame::OnActivated( wxDataViewEvent &event ) | |
754 | { | |
755 | if(!m_log) | |
756 | return; | |
757 | ||
758 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_ACTIVATED, Item Id: %d; Column: %d", event.GetItem().GetID(), event.GetColumn()); | |
759 | } | |
760 | ||
6848478c RR |
761 | void MyFrame::OnSelected( wxDataViewEvent &event ) |
762 | { | |
763 | if(!m_log) | |
764 | return; | |
765 | ||
766 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_SELECTED, Item Id: %d", event.GetItem().GetID() ); | |
767 | } | |
768 | ||
718fd180 RR |
769 | void MyFrame::OnExpanding( wxDataViewEvent &event ) |
770 | { | |
771 | if (!m_log) | |
772 | return; | |
773 | ||
774 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDING, Item Id: %d", event.GetItem().GetID() ); | |
775 | } | |
776 | ||
d14e1c3a RR |
777 | |
778 | void MyFrame::OnEditingStarted( wxDataViewEvent &event ) | |
779 | { | |
780 | if (!m_log) | |
781 | return; | |
782 | ||
783 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_STARTED, Item Id: %d", event.GetItem().GetID() ); | |
784 | } | |
785 | ||
786 | void MyFrame::OnEditingDone( wxDataViewEvent &event ) | |
787 | { | |
788 | if (!m_log) | |
789 | return; | |
790 | ||
791 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EDITING_DONE, Item Id: %d", event.GetItem().GetID() ); | |
792 | } | |
793 | ||
718fd180 RR |
794 | void MyFrame::OnExpanded( wxDataViewEvent &event ) |
795 | { | |
796 | if (!m_log) | |
797 | return; | |
798 | ||
799 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_EXPANDED, Item Id: %d", event.GetItem().GetID() ); | |
800 | } | |
801 | ||
802 | void MyFrame::OnCollapsing( wxDataViewEvent &event ) | |
803 | { | |
804 | if (!m_log) | |
805 | return; | |
806 | ||
807 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSING, Item Id: %d", event.GetItem().GetID() ); | |
808 | } | |
809 | ||
810 | void MyFrame::OnCollapsed( wxDataViewEvent &event ) | |
811 | { | |
812 | if (!m_log) | |
813 | return; | |
814 | ||
815 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_ITEM_COLLAPSED, Item Id: %d", event.GetItem().GetID() ); | |
816 | } | |
817 | ||
b7e9f8b1 RR |
818 | void MyFrame::OnHeaderClick( wxDataViewEvent &event ) |
819 | { | |
820 | if(!m_log) | |
821 | return; | |
822 | ||
823 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK, Column: %d", event.GetColumn()); | |
824 | } | |
825 | ||
826 | void MyFrame::OnHeaderRightClick( wxDataViewEvent &event ) | |
827 | { | |
828 | if(!m_log) | |
829 | return; | |
830 | ||
831 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_RIGHT_CLICK, Column: %d", event.GetColumn()); | |
832 | } | |
833 | ||
834 | void MyFrame::OnSorted( wxDataViewEvent &event ) | |
835 | { | |
836 | if(!m_log) | |
837 | return; | |
838 | ||
839 | wxLogMessage("wxEVT_COMMAND_DATAVIEW_COLUMN_SORTED, Column: %d", event.GetColumn()); | |
840 | } | |
841 | ||
842 | void MyFrame::OnRightClick( wxMouseEvent &event ) | |
843 | { | |
844 | if(!m_log) | |
845 | return; | |
846 | ||
847 | wxLogMessage("wxEVT_MOUSE_RIGHT_UP, Click Point is X: %d, Y: %d", event.GetX(), event.GetY()); | |
848 | } | |
849 | ||
850 | void MyFrame::OnGoto( wxCommandEvent &event) | |
851 | { | |
718fd180 | 852 | wxDataViewItem item = m_list_model->GetItem( 50 ); |
fbda518c | 853 | m_listCtrl->EnsureVisible(item,m_col); |
d8331a01 RR |
854 | } |
855 | ||
bd6169a6 RR |
856 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) |
857 | { | |
87f0efe2 RR |
858 | wxAboutDialogInfo info; |
859 | info.SetName(_("DataView sample")); | |
860 | info.SetDescription(_("This sample demonstrates the dataview control handling")); | |
861 | info.SetCopyright(_T("(C) 2007 Robert Roebling")); | |
bd6169a6 | 862 | |
87f0efe2 | 863 | wxAboutBox(info); |
bd6169a6 RR |
864 | } |
865 |