]>
Commit | Line | Data |
---|---|---|
bd6169a6 RR |
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 | |
0bcd4039 | 20 | #include "wx/wx.h" |
bd6169a6 RR |
21 | #endif |
22 | ||
b910a8ad JS |
23 | #include "wx/datetime.h" |
24 | ||
bd6169a6 | 25 | #ifndef __WXMSW__ |
0bcd4039 | 26 | #include "../sample.xpm" |
bd6169a6 RR |
27 | #endif |
28 | ||
cbc9145c RR |
29 | #include "null.xpm" |
30 | ||
362d7fb9 | 31 | #include "wx/dataview.h" |
bd6169a6 | 32 | |
362d7fb9 RR |
33 | // ------------------------------------- |
34 | // MyTextModel | |
35 | // ------------------------------------- | |
bd6169a6 | 36 | |
4d496ecb RR |
37 | WX_DECLARE_LIST(wxDateTime,wxArrayDate); |
38 | #include <wx/listimpl.cpp> | |
004f4002 | 39 | WX_DEFINE_LIST(wxArrayDate) |
4d496ecb | 40 | |
362d7fb9 | 41 | class MyTextModel: public wxDataViewListModel |
bd6169a6 RR |
42 | { |
43 | public: | |
b910a8ad | 44 | MyTextModel() |
a7f61f76 | 45 | { |
0a71f9e9 | 46 | unsigned int i; |
a7f61f76 RR |
47 | for (i = 0; i < 1000; i++) |
48 | m_list.Add( wxT("Test") ); | |
605c2c4a RR |
49 | for (i = 0; i < 500; i++) |
50 | { m_bools.Add( 0 ); m_bools.Add( 1 ); } | |
e152afc3 RR |
51 | for (i = 0; i < 500; i++) |
52 | { m_colours.Add( wxT("red") ); m_colours.Add( wxT("green") ); } | |
ad63bf41 RR |
53 | for (i = 0; i < 1000; i++) |
54 | { m_progress.Add( i/10 ); } | |
4d496ecb | 55 | for (i = 0; i < 1000; i++) |
b910a8ad | 56 | { |
4d496ecb | 57 | wxDateTime *date = new wxDateTime( wxDateTime::Now() ); |
b910a8ad | 58 | m_dates.Append( date ); |
4d496ecb | 59 | } |
a7f61f76 | 60 | } |
b910a8ad | 61 | |
0a71f9e9 | 62 | virtual unsigned int GetNumberOfRows() |
362d7fb9 | 63 | { return 1000; } |
0a71f9e9 | 64 | virtual unsigned int GetNumberOfCols() |
4d496ecb | 65 | { return 7; } |
b910a8ad | 66 | |
362d7fb9 | 67 | // as reported by wxVariant |
0a71f9e9 | 68 | virtual wxString GetColType( unsigned int col ) |
605c2c4a | 69 | { |
4d496ecb RR |
70 | if (col == 6) |
71 | return wxT("datetime"); | |
b910a8ad | 72 | |
ad63bf41 RR |
73 | if (col == 5) |
74 | return wxT("long"); | |
b910a8ad | 75 | |
605c2c4a RR |
76 | if (col == 3) |
77 | return wxT("bool"); | |
b910a8ad JS |
78 | |
79 | return wxT("string"); | |
605c2c4a | 80 | } |
b910a8ad | 81 | |
0a71f9e9 | 82 | virtual void GetValue( wxVariant &variant, unsigned int col, unsigned int row ) |
605c2c4a | 83 | { |
4d496ecb RR |
84 | if (col == 6) |
85 | { | |
3f3af7e7 | 86 | variant = (wxDateTime) *m_dates[row]; |
4d496ecb | 87 | } else |
ad63bf41 | 88 | if (col == 5) |
605c2c4a | 89 | { |
3f3af7e7 | 90 | variant = (long) m_progress[row]; |
ad63bf41 | 91 | } else |
e152afc3 RR |
92 | if (col == 4) |
93 | { | |
3f3af7e7 | 94 | variant = m_colours[row]; |
ad63bf41 RR |
95 | } else |
96 | if (col == 3) | |
97 | { | |
3f3af7e7 | 98 | variant = (bool) m_bools[row]; |
b910a8ad | 99 | } else |
a7f61f76 RR |
100 | if (col == 2) |
101 | { | |
3f3af7e7 | 102 | variant = m_list[row]; |
a7f61f76 RR |
103 | } |
104 | else | |
105 | { | |
b910a8ad JS |
106 | wxString tmp; |
107 | tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col ); | |
3f3af7e7 | 108 | variant = tmp; |
a7f61f76 | 109 | } |
362d7fb9 | 110 | } |
0a71f9e9 | 111 | virtual bool SetValue( wxVariant &value, unsigned int col, unsigned int row ) |
a7f61f76 | 112 | { |
4d496ecb RR |
113 | if (col == 6) |
114 | { | |
115 | *m_dates[row] = value.GetDateTime(); | |
116 | } else | |
605c2c4a RR |
117 | if (col == 3) |
118 | { | |
119 | m_bools[row] = (int) value.GetBool(); | |
120 | } else | |
a7f61f76 RR |
121 | if (col == 2) |
122 | { | |
123 | m_list[row] = value.GetString(); | |
124 | } | |
125 | return true; | |
126 | } | |
b910a8ad | 127 | |
a7f61f76 | 128 | wxArrayString m_list; |
605c2c4a | 129 | wxArrayInt m_bools; |
e152afc3 | 130 | wxArrayString m_colours; |
ad63bf41 | 131 | wxArrayInt m_progress; |
4d496ecb | 132 | wxArrayDate m_dates; |
e152afc3 RR |
133 | }; |
134 | ||
135 | // ------------------------------------- | |
baa9ebc4 | 136 | // MyCustomRenderer |
e152afc3 RR |
137 | // ------------------------------------- |
138 | ||
baa9ebc4 | 139 | class MyCustomRenderer: public wxDataViewCustomRenderer |
e152afc3 RR |
140 | { |
141 | public: | |
baa9ebc4 RR |
142 | MyCustomRenderer() : |
143 | wxDataViewCustomRenderer( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE ) | |
b910a8ad JS |
144 | { |
145 | m_colour = wxT("black"); | |
e152afc3 RR |
146 | } |
147 | bool SetValue( const wxVariant &value ) | |
148 | { | |
149 | m_colour = value.GetString(); | |
150 | return true; | |
151 | } | |
f554a14b | 152 | bool Render( wxRect rect, wxDC *dc, int WXUNUSED(state) ) |
b910a8ad | 153 | { |
e152afc3 RR |
154 | dc->SetPen( *wxBLACK_PEN ); |
155 | if (m_colour == wxT("red")) | |
156 | dc->SetBrush( *wxRED_BRUSH ); | |
157 | else if (m_colour == wxT("green")) | |
158 | dc->SetBrush( *wxGREEN_BRUSH ); | |
b910a8ad | 159 | else |
e152afc3 RR |
160 | dc->SetBrush( *wxBLACK_BRUSH ); |
161 | dc->DrawRectangle( rect ); | |
162 | return true; | |
163 | } | |
164 | wxSize GetSize() | |
165 | { | |
166 | return wxSize(20,8); | |
167 | } | |
f554a14b WS |
168 | bool Activate( wxRect WXUNUSED(rect), |
169 | wxDataViewListModel *WXUNUSED(model), | |
0a71f9e9 RR |
170 | unsigned int WXUNUSED(col), |
171 | unsigned int WXUNUSED(row) ) | |
553f7d8f | 172 | { |
553f7d8f RR |
173 | return false; |
174 | } | |
e152afc3 RR |
175 | |
176 | private: | |
b910a8ad | 177 | wxString m_colour; |
0313c114 | 178 | }; |
bd6169a6 | 179 | |
241cb04b RR |
180 | // ------------------------------------- |
181 | // MyUnsortedTextModel | |
182 | // ------------------------------------- | |
183 | ||
184 | class MyUnsortedTextModel: public wxDataViewListModel | |
185 | { | |
186 | public: | |
187 | MyUnsortedTextModel() | |
188 | { | |
189 | m_list.Add( wxT("This") ); | |
190 | m_list.Add( wxT("is") ); | |
191 | m_list.Add( wxT("an") ); | |
192 | m_list.Add( wxT("unsorted") ); | |
193 | m_list.Add( wxT("list") ); | |
194 | m_list.Add( wxT("of") ); | |
195 | m_list.Add( wxT("words.") ); | |
cbc9145c RR |
196 | |
197 | m_bitmap = wxBitmap( null_xpm ); | |
241cb04b RR |
198 | } |
199 | ||
0a71f9e9 RR |
200 | virtual unsigned int GetNumberOfRows() { return m_list.GetCount(); } |
201 | virtual unsigned int GetNumberOfCols() { return 2; } | |
202 | virtual wxString GetColType( unsigned int WXUNUSED(col) ) { return wxT("string"); } | |
203 | virtual void GetValue( wxVariant &variant, unsigned int col, unsigned int row ) | |
241cb04b RR |
204 | { |
205 | if (col == 0) | |
3f3af7e7 RR |
206 | { |
207 | variant = m_list[row]; | |
208 | return; | |
209 | } | |
979f71a3 | 210 | if ((col == 2) || (col == 3)) |
cbc9145c | 211 | { |
979f71a3 | 212 | variant << m_bitmap; |
cbc9145c RR |
213 | return; |
214 | } | |
241cb04b | 215 | wxString tmp; |
b910a8ad | 216 | tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col ); |
3f3af7e7 | 217 | variant = tmp; |
241cb04b | 218 | } |
0a71f9e9 | 219 | virtual bool SetValue( wxVariant &variant, unsigned int col, unsigned int row ) |
241cb04b RR |
220 | { |
221 | if (col == 0) | |
222 | { | |
223 | m_list[row] = variant.GetString(); | |
224 | return true; | |
225 | } | |
226 | return false; | |
b910a8ad | 227 | |
241cb04b RR |
228 | } |
229 | ||
4627af27 RR |
230 | void AppendRow( const wxString &text ) |
231 | { | |
232 | m_list.Add( text ); | |
233 | RowAppended(); | |
234 | } | |
235 | ||
236 | void PrependRow( const wxString &text ) | |
237 | { | |
238 | m_list.Insert( text, 0 ); | |
239 | RowPrepended(); | |
240 | } | |
241 | ||
242 | void InsertRowAt1( const wxString &text ) | |
243 | { | |
244 | m_list.Insert( text, 1 ); | |
245 | RowInserted( 1 ); | |
246 | } | |
247 | ||
0a71f9e9 | 248 | void DeleteRow( unsigned int index ) |
4627af27 RR |
249 | { |
250 | m_list.RemoveAt( index ); | |
251 | RowDeleted( index ); | |
252 | } | |
253 | ||
241cb04b | 254 | wxArrayString m_list; |
cbc9145c | 255 | wxBitmap m_bitmap; |
241cb04b RR |
256 | }; |
257 | ||
362d7fb9 RR |
258 | // ------------------------------------- |
259 | // MyApp | |
260 | // ------------------------------------- | |
bd6169a6 RR |
261 | |
262 | class MyApp: public wxApp | |
263 | { | |
264 | public: | |
265 | bool OnInit(void); | |
266 | }; | |
267 | ||
362d7fb9 RR |
268 | // ------------------------------------- |
269 | // MyFrame | |
270 | // ------------------------------------- | |
271 | ||
bd6169a6 RR |
272 | class MyFrame: public wxFrame |
273 | { | |
274 | public: | |
275 | MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); | |
276 | ||
277 | public: | |
278 | void OnQuit(wxCommandEvent& event); | |
279 | void OnAbout(wxCommandEvent& event); | |
b910a8ad | 280 | |
bd6169a6 | 281 | private: |
a7f61f76 RR |
282 | wxDataViewCtrl* dataview_left; |
283 | wxDataViewCtrl* dataview_right; | |
bd6169a6 RR |
284 | }; |
285 | ||
241cb04b RR |
286 | // ------------------------------------- |
287 | // MySortingFrame | |
288 | // ------------------------------------- | |
289 | ||
abd6692c RR |
290 | enum my_events |
291 | { | |
292 | ID_APPEND_ROW_LEFT = 1000, | |
293 | ID_PREPEND_ROW_LEFT, | |
294 | ID_INSERT_ROW_LEFT, | |
295 | ID_DELETE_ROW_LEFT, | |
296 | ID_EDIT_ROW_LEFT, | |
fc211fe5 RR |
297 | |
298 | ID_SELECT, | |
299 | ID_UNSELECT_ALL, | |
b910a8ad | 300 | |
abd6692c RR |
301 | ID_APPEND_ROW_RIGHT, |
302 | ID_PREPEND_ROW_RIGHT, | |
303 | ID_INSERT_ROW_RIGHT, | |
304 | ID_DELETE_ROW_RIGHT, | |
eb7f97f8 RR |
305 | ID_EDIT_ROW_RIGHT, |
306 | ||
307 | ID_SORTED, | |
308 | ID_UNSORTED | |
abd6692c RR |
309 | }; |
310 | ||
241cb04b RR |
311 | class MySortingFrame: public wxFrame |
312 | { | |
313 | public: | |
314 | MySortingFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h); | |
eb7f97f8 | 315 | ~MySortingFrame(); |
241cb04b RR |
316 | |
317 | public: | |
318 | void OnQuit(wxCommandEvent& event); | |
319 | void OnAbout(wxCommandEvent& event); | |
b910a8ad | 320 | |
abd6692c RR |
321 | void OnAppendRowLeft(wxCommandEvent& event); |
322 | void OnPrependRowLeft(wxCommandEvent& event); | |
323 | void OnInsertRowLeft(wxCommandEvent& event); | |
324 | void OnDeleteRowLeft(wxCommandEvent& event); | |
325 | void OnEditRowLeft(wxCommandEvent& event); | |
b910a8ad | 326 | |
abd6692c RR |
327 | void OnAppendRowRight(wxCommandEvent& event); |
328 | void OnPrependRowRight(wxCommandEvent& event); | |
329 | void OnInsertRowRight(wxCommandEvent& event); | |
330 | void OnDeleteRowRight(wxCommandEvent& event); | |
331 | void OnEditRowRight(wxCommandEvent& event); | |
b910a8ad | 332 | |
fc211fe5 RR |
333 | void OnSelect(wxCommandEvent& event); |
334 | void OnUnselectAll(wxCommandEvent& event); | |
335 | ||
eb7f97f8 RR |
336 | void OnSelectedUnsorted(wxDataViewEvent &event); |
337 | void OnSelectedSorted(wxDataViewEvent &event); | |
338 | ||
241cb04b RR |
339 | private: |
340 | wxDataViewCtrl* dataview_left; | |
341 | wxDataViewCtrl* dataview_right; | |
eb7f97f8 RR |
342 | |
343 | wxLog *m_logOld; | |
344 | wxTextCtrl *m_logWindow; | |
b910a8ad | 345 | |
4627af27 RR |
346 | MyUnsortedTextModel *m_unsorted_model; |
347 | ||
abd6692c | 348 | DECLARE_EVENT_TABLE() |
241cb04b RR |
349 | }; |
350 | ||
362d7fb9 RR |
351 | // ------------------------------------- |
352 | // MyApp | |
353 | // ------------------------------------- | |
354 | ||
bd6169a6 RR |
355 | #define DYNAMIC_QUIT wxID_EXIT |
356 | #define DYNAMIC_ABOUT wxID_ABOUT | |
357 | ||
bd6169a6 RR |
358 | IMPLEMENT_APP (MyApp) |
359 | ||
bd6169a6 RR |
360 | bool MyApp::OnInit(void) |
361 | { | |
abd6692c | 362 | MyFrame *frame = new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340); |
bd6169a6 RR |
363 | frame->Show(true); |
364 | ||
eb7f97f8 | 365 | MySortingFrame *frame2 = new MySortingFrame(NULL, wxT("wxDataViewCtrl sorting test"), 10, 150, 600, 500); |
241cb04b RR |
366 | frame2->Show(true); |
367 | ||
bd6169a6 | 368 | SetTopWindow(frame); |
979f71a3 | 369 | |
bd6169a6 RR |
370 | return true; |
371 | } | |
372 | ||
373 | // ------------------------------------- | |
374 | // MyFrame | |
375 | // ------------------------------------- | |
376 | ||
bd6169a6 RR |
377 | MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): |
378 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
379 | { | |
0bcd4039 | 380 | SetIcon(wxICON(sample)); |
bd6169a6 | 381 | |
bd6169a6 RR |
382 | wxMenu *file_menu = new wxMenu; |
383 | ||
384 | file_menu->Append(DYNAMIC_ABOUT, _T("&About")); | |
385 | file_menu->Append(DYNAMIC_QUIT, _T("E&xit")); | |
386 | wxMenuBar *menu_bar = new wxMenuBar; | |
387 | menu_bar->Append(file_menu, _T("&File")); | |
388 | SetMenuBar(menu_bar); | |
389 | ||
390 | // You used to have to do some casting for param 4, but now there are type-safe handlers | |
391 | Connect( DYNAMIC_QUIT, wxID_ANY, | |
392 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) ); | |
393 | Connect( DYNAMIC_ABOUT, wxID_ANY, | |
394 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) ); | |
395 | ||
396 | CreateStatusBar(); | |
b910a8ad | 397 | |
0bcd4039 | 398 | wxPanel *panel = new wxPanel( this, wxID_ANY ); |
b6339e0b | 399 | |
b910a8ad | 400 | |
a7f61f76 | 401 | // Left wxDataViewCtrl |
b6339e0b | 402 | dataview_left = new wxDataViewCtrl( panel, wxID_ANY ); |
b910a8ad | 403 | |
362d7fb9 | 404 | MyTextModel *model = new MyTextModel; |
a7f61f76 | 405 | dataview_left->AssociateModel( model ); |
b910a8ad | 406 | |
605c2c4a RR |
407 | dataview_left->AppendTextColumn( wxT("first"), 0 ); |
408 | dataview_left->AppendTextColumn( wxT("second"), 1 ); | |
553f7d8f | 409 | |
baa9ebc4 RR |
410 | wxDataViewTextRenderer *text_renderer = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); |
411 | wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_renderer, 2 ); | |
a7f61f76 | 412 | dataview_left->AppendColumn( column ); |
b910a8ad | 413 | |
605c2c4a | 414 | dataview_left->AppendToggleColumn( wxT("fourth"), 3 ); |
b910a8ad | 415 | |
baa9ebc4 RR |
416 | MyCustomRenderer *custom_renderer = new MyCustomRenderer; |
417 | column = new wxDataViewColumn( wxT("custom"), custom_renderer, 4 ); | |
e152afc3 | 418 | dataview_left->AppendColumn( column ); |
b910a8ad | 419 | |
ad63bf41 | 420 | dataview_left->AppendProgressColumn( wxT("progress"), 5 ); |
b910a8ad | 421 | |
4d496ecb | 422 | dataview_left->AppendDateColumn( wxT("date"), 6 ); |
b910a8ad | 423 | |
a7f61f76 | 424 | // Right wxDataViewCtrl using the same model |
b6339e0b | 425 | dataview_right = new wxDataViewCtrl( panel, wxID_ANY ); |
a7f61f76 | 426 | dataview_right->AssociateModel( model ); |
553f7d8f | 427 | |
baa9ebc4 RR |
428 | text_renderer = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); |
429 | column = new wxDataViewColumn( wxT("editable"), text_renderer, 2 ); | |
605c2c4a RR |
430 | dataview_right->AppendColumn( column ); |
431 | dataview_right->AppendTextColumn( wxT("first"), 0 ); | |
432 | dataview_right->AppendTextColumn( wxT("second"), 1 ); | |
baa9ebc4 RR |
433 | wxDataViewToggleRenderer *toggle_renderer = new wxDataViewToggleRenderer( wxT("bool"), wxDATAVIEW_CELL_ACTIVATABLE ); |
434 | column = new wxDataViewColumn( wxT("bool"), toggle_renderer, 3, 30 ); | |
a7f61f76 | 435 | dataview_right->AppendColumn( column ); |
553f7d8f | 436 | |
7ea3a0de | 437 | dataview_right->AppendDateColumn( wxT("date"), 6 ); |
b910a8ad | 438 | |
7ea3a0de | 439 | // layout dataview controls. |
b910a8ad | 440 | |
a7f61f76 | 441 | wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL ); |
4d496ecb | 442 | sizer->Add( dataview_left, 3, wxGROW ); |
a7f61f76 | 443 | sizer->Add(10,10); |
4d496ecb | 444 | sizer->Add( dataview_right, 2, wxGROW ); |
b6339e0b | 445 | panel->SetSizer( sizer ); |
bd6169a6 RR |
446 | } |
447 | ||
448 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) | |
449 | { | |
450 | Close(true); | |
451 | } | |
452 | ||
453 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
454 | { | |
455 | wxMessageDialog dialog(this, _T("This demonstrates the dataview control handling"), | |
456 | _T("About DataView"), wxOK); | |
457 | ||
458 | dialog.ShowModal(); | |
459 | } | |
460 | ||
241cb04b RR |
461 | // ------------------------------------- |
462 | // MySortingFrame | |
463 | // ------------------------------------- | |
464 | ||
abd6692c RR |
465 | BEGIN_EVENT_TABLE(MySortingFrame,wxFrame) |
466 | EVT_BUTTON( ID_APPEND_ROW_LEFT, MySortingFrame::OnAppendRowLeft ) | |
4627af27 RR |
467 | EVT_BUTTON( ID_PREPEND_ROW_LEFT, MySortingFrame::OnPrependRowLeft ) |
468 | EVT_BUTTON( ID_INSERT_ROW_LEFT, MySortingFrame::OnInsertRowLeft ) | |
469 | EVT_BUTTON( ID_DELETE_ROW_LEFT, MySortingFrame::OnDeleteRowLeft ) | |
fc211fe5 RR |
470 | EVT_BUTTON( ID_SELECT, MySortingFrame::OnSelect ) |
471 | EVT_BUTTON( ID_UNSELECT_ALL, MySortingFrame::OnUnselectAll ) | |
eb7f97f8 RR |
472 | EVT_DATAVIEW_ROW_SELECTED( ID_SORTED, MySortingFrame::OnSelectedSorted ) |
473 | EVT_DATAVIEW_ROW_SELECTED( ID_UNSORTED, MySortingFrame::OnSelectedUnsorted ) | |
abd6692c RR |
474 | END_EVENT_TABLE() |
475 | ||
241cb04b RR |
476 | MySortingFrame::MySortingFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h): |
477 | wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h)) | |
478 | { | |
eb7f97f8 RR |
479 | m_logOld = NULL; |
480 | ||
0bcd4039 | 481 | SetIcon(wxICON(sample)); |
241cb04b RR |
482 | |
483 | wxMenu *file_menu = new wxMenu; | |
484 | ||
485 | file_menu->Append(DYNAMIC_ABOUT, _T("&About")); | |
486 | file_menu->Append(DYNAMIC_QUIT, _T("E&xit")); | |
487 | wxMenuBar *menu_bar = new wxMenuBar; | |
488 | menu_bar->Append(file_menu, _T("&File")); | |
489 | SetMenuBar(menu_bar); | |
490 | ||
491 | // You used to have to do some casting for param 4, but now there are type-safe handlers | |
492 | Connect( DYNAMIC_QUIT, wxID_ANY, | |
493 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MySortingFrame::OnQuit) ); | |
494 | Connect( DYNAMIC_ABOUT, wxID_ANY, | |
495 | wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MySortingFrame::OnAbout) ); | |
496 | ||
497 | CreateStatusBar(); | |
b910a8ad JS |
498 | |
499 | ||
241cb04b | 500 | // Left wxDataViewCtrl |
eb7f97f8 | 501 | dataview_left = new wxDataViewCtrl( this, ID_UNSORTED, wxDefaultPosition, wxDefaultSize, wxDV_MULTIPLE ); |
241cb04b | 502 | |
4627af27 RR |
503 | m_unsorted_model = new MyUnsortedTextModel; |
504 | dataview_left->AssociateModel( m_unsorted_model ); | |
baa9ebc4 RR |
505 | wxDataViewTextRenderer *text_renderer = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); |
506 | wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_renderer, 0 ); | |
241cb04b RR |
507 | dataview_left->AppendColumn( column ); |
508 | dataview_left->AppendTextColumn( wxT("second"), 1 ); | |
baa9ebc4 RR |
509 | dataview_left->AppendColumn( new wxDataViewColumn( wxT("icon"), new wxDataViewBitmapRenderer, 2, 25 ) ); |
510 | dataview_left->AppendColumn( new wxDataViewColumn( wxT("icon"), new wxDataViewBitmapRenderer, 3, 25 ) ); | |
b910a8ad | 511 | |
241cb04b | 512 | // Right wxDataViewCtrl using the sorting model |
eb7f97f8 | 513 | dataview_right = new wxDataViewCtrl( this, ID_SORTED ); |
979f71a3 | 514 | |
b910a8ad | 515 | wxDataViewSortedListModel *sorted_model = |
4627af27 | 516 | new wxDataViewSortedListModel( m_unsorted_model ); |
241cb04b | 517 | dataview_right->AssociateModel( sorted_model ); |
baa9ebc4 RR |
518 | text_renderer = new wxDataViewTextRenderer( wxT("string"), wxDATAVIEW_CELL_EDITABLE ); |
519 | column = new wxDataViewColumn( wxT("editable"), text_renderer, 0, -1, wxDATAVIEW_COL_SORTABLE|wxDATAVIEW_COL_RESIZABLE ); | |
241cb04b | 520 | dataview_right->AppendColumn( column ); |
979f71a3 | 521 | |
241cb04b RR |
522 | dataview_right->AppendTextColumn( wxT("second"), 1 ); |
523 | ||
524 | // layout dataview controls. | |
b910a8ad | 525 | |
abd6692c RR |
526 | wxBoxSizer *top_sizer = new wxBoxSizer( wxHORIZONTAL ); |
527 | top_sizer->Add( dataview_left, 1, wxGROW ); | |
528 | top_sizer->Add(10,10); | |
529 | top_sizer->Add( dataview_right, 1, wxGROW ); | |
b910a8ad | 530 | |
abd6692c RR |
531 | wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL ); |
532 | button_sizer->Add( 10, 10, 1 ); | |
533 | wxFlexGridSizer *left_sizer = new wxFlexGridSizer( 2 ); | |
534 | left_sizer->Add( new wxButton( this, ID_APPEND_ROW_LEFT, wxT("Append") ), 0, wxALL, 5 ); | |
535 | left_sizer->Add( new wxButton( this, ID_PREPEND_ROW_LEFT, wxT("Prepend") ), 0, wxALL, 5 ); | |
536 | left_sizer->Add( new wxButton( this, ID_INSERT_ROW_LEFT, wxT("Insert") ), 0, wxALL, 5 ); | |
cbc9145c | 537 | left_sizer->Add( new wxButton( this, ID_DELETE_ROW_LEFT, wxT("Delete second") ), 0, wxALL, 5 ); |
abd6692c | 538 | left_sizer->Add( new wxButton( this, ID_EDIT_ROW_LEFT, wxT("Edit") ), 0, wxALL, 5 ); |
fc211fe5 RR |
539 | left_sizer->Add( 5,5 ); |
540 | left_sizer->Add( new wxButton( this, ID_SELECT, wxT("Select third") ), 0, wxALL, 5 ); | |
541 | left_sizer->Add( new wxButton( this, ID_UNSELECT_ALL, wxT("Unselect all") ), 0, wxALL, 5 ); | |
abd6692c RR |
542 | button_sizer->Add( left_sizer ); |
543 | button_sizer->Add( 10, 10, 2 ); | |
544 | wxFlexGridSizer *right_sizer = new wxFlexGridSizer( 2 ); | |
545 | right_sizer->Add( new wxButton( this, ID_APPEND_ROW_RIGHT, wxT("Append") ), 0, wxALL, 5 ); | |
546 | right_sizer->Add( new wxButton( this, ID_PREPEND_ROW_RIGHT, wxT("Prepend") ), 0, wxALL, 5 ); | |
547 | right_sizer->Add( new wxButton( this, ID_INSERT_ROW_RIGHT, wxT("Insert") ), 0, wxALL, 5 ); | |
cbc9145c | 548 | right_sizer->Add( new wxButton( this, ID_DELETE_ROW_RIGHT, wxT("Delete second") ), 0, wxALL, 5 ); |
abd6692c RR |
549 | right_sizer->Add( new wxButton( this, ID_EDIT_ROW_RIGHT, wxT("Edit") ), 0, wxALL, 5 ); |
550 | button_sizer->Add( right_sizer ); | |
551 | button_sizer->Add( 10, 10, 1 ); | |
b910a8ad | 552 | |
abd6692c RR |
553 | wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL ); |
554 | main_sizer->Add( top_sizer, 1, wxGROW ); | |
555 | main_sizer->Add( button_sizer, 0, wxGROW ); | |
eb7f97f8 RR |
556 | |
557 | m_logWindow = new wxTextCtrl(this, wxID_ANY, wxEmptyString, | |
558 | wxDefaultPosition, wxDefaultSize, | |
559 | wxTE_MULTILINE | wxSUNKEN_BORDER); | |
560 | main_sizer->Add( 20,20 ); | |
561 | main_sizer->Add( m_logWindow, 1, wxGROW ); | |
562 | ||
563 | m_logOld = wxLog::SetActiveTarget(new wxLogTextCtrl(m_logWindow)); | |
b910a8ad | 564 | |
abd6692c | 565 | SetSizer( main_sizer ); |
241cb04b RR |
566 | } |
567 | ||
eb7f97f8 RR |
568 | MySortingFrame::~MySortingFrame() |
569 | { | |
570 | delete wxLog::SetActiveTarget(m_logOld); | |
571 | } | |
572 | ||
573 | void MySortingFrame::OnSelectedUnsorted(wxDataViewEvent &event) | |
574 | { | |
fc211fe5 RR |
575 | int row = event.GetRow(); |
576 | wxLogMessage( wxT("OnSelected from unsorted list, selected %d"), row ); | |
577 | if (row >= 0) | |
578 | wxLogMessage( wxT("wxDataViewCtrl::IsSelected( %d ): %d (as int)"), | |
579 | row, (int) dataview_right->IsSelected( row ) ); | |
eb7f97f8 RR |
580 | } |
581 | ||
582 | void MySortingFrame::OnSelectedSorted(wxDataViewEvent &event) | |
583 | { | |
584 | wxLogMessage( wxT("OnSelected from sorted list, selected %d"), (int) event.GetRow() ); | |
585 | } | |
586 | ||
241cb04b RR |
587 | void MySortingFrame::OnQuit(wxCommandEvent& WXUNUSED(event) ) |
588 | { | |
589 | Close(true); | |
590 | } | |
591 | ||
592 | void MySortingFrame::OnAbout(wxCommandEvent& WXUNUSED(event) ) | |
593 | { | |
594 | wxMessageDialog dialog(this, _T("This demonstrates the dataview control sorting"), | |
595 | _T("About DataView"), wxOK); | |
596 | ||
597 | dialog.ShowModal(); | |
598 | } | |
bd6169a6 | 599 | |
fc211fe5 RR |
600 | void MySortingFrame::OnSelect(wxCommandEvent& WXUNUSED(event)) |
601 | { | |
602 | dataview_left->SetSelection( 2 ); | |
603 | } | |
604 | ||
605 | void MySortingFrame::OnUnselectAll(wxCommandEvent& WXUNUSED(event)) | |
606 | { | |
607 | dataview_left->ClearSelection(); | |
608 | } | |
609 | ||
f554a14b | 610 | void MySortingFrame::OnAppendRowLeft(wxCommandEvent& WXUNUSED(event)) |
abd6692c | 611 | { |
4627af27 RR |
612 | wxTextEntryDialog dialog( this, wxT("Enter text to append") ); |
613 | if (dialog.ShowModal() == wxID_OK) | |
614 | { | |
615 | wxString value = dialog.GetValue(); | |
616 | if (!value.empty()) | |
617 | m_unsorted_model->AppendRow( value ); | |
618 | } | |
abd6692c RR |
619 | } |
620 | ||
f554a14b | 621 | void MySortingFrame::OnPrependRowLeft(wxCommandEvent& WXUNUSED(event)) |
abd6692c | 622 | { |
4627af27 RR |
623 | wxTextEntryDialog dialog( this, wxT("Enter text to prepend") ); |
624 | if (dialog.ShowModal() == wxID_OK) | |
625 | { | |
626 | wxString value = dialog.GetValue(); | |
627 | if (!value.empty()) | |
628 | m_unsorted_model->PrependRow( value ); | |
629 | } | |
abd6692c RR |
630 | } |
631 | ||
f554a14b | 632 | void MySortingFrame::OnInsertRowLeft(wxCommandEvent& WXUNUSED(event)) |
abd6692c | 633 | { |
cbc9145c | 634 | wxTextEntryDialog dialog( this, wxT("Enter text to insert before second") ); |
4627af27 RR |
635 | if (dialog.ShowModal() == wxID_OK) |
636 | { | |
637 | wxString value = dialog.GetValue(); | |
638 | if (!value.empty()) | |
639 | m_unsorted_model->InsertRowAt1( value ); | |
640 | } | |
abd6692c RR |
641 | } |
642 | ||
f554a14b | 643 | void MySortingFrame::OnDeleteRowLeft(wxCommandEvent& WXUNUSED(event)) |
abd6692c | 644 | { |
4627af27 | 645 | m_unsorted_model->DeleteRow( 1 ); |
abd6692c RR |
646 | } |
647 | ||
f554a14b | 648 | void MySortingFrame::OnEditRowLeft(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
649 | { |
650 | } | |
651 | ||
f554a14b | 652 | void MySortingFrame::OnAppendRowRight(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
653 | { |
654 | } | |
655 | ||
f554a14b | 656 | void MySortingFrame::OnPrependRowRight(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
657 | { |
658 | } | |
659 | ||
f554a14b | 660 | void MySortingFrame::OnInsertRowRight(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
661 | { |
662 | } | |
663 | ||
f554a14b | 664 | void MySortingFrame::OnDeleteRowRight(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
665 | { |
666 | } | |
667 | ||
f554a14b | 668 | void MySortingFrame::OnEditRowRight(wxCommandEvent& WXUNUSED(event)) |
abd6692c RR |
669 | { |
670 | } | |
671 |