]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
added wxUSE_TASKBARICON; corrected condition for generating the taskbar sample makefi...
[wxWidgets.git] / samples / dataview / dataview.cpp
CommitLineData
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/*
48Implement this data model
49 Title Artist Year
50-------------------------------------------------------------
511: 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
62class MyMusicModelNode;
63WX_DEFINE_ARRAY_PTR( MyMusicModelNode*, MyMusicModelNodes );
64
65class MyMusicModelNode
66{
67public:
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
106public:
107 wxString m_title;
108 wxString m_artist;
109 wxString m_year;
110
111private:
112 MyMusicModelNode *m_parent;
113 MyMusicModelNodes m_children;
1e08ad10
RR
114 bool m_isContainer;
115};
116
aba9bfd0 117
5debbdcf 118class MyMusicModel: public wxDataViewModel
bd6169a6
RR
119{
120public:
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();
e63807a8
RR
163 node->GetParent()->GetChildren().Remove( node );
164 delete node;
165
166 // notify control
167 ItemDeleted( item );
168 }
169
170 // implementation of base class virtuals to define model
5debbdcf
RR
171
172 virtual unsigned int GetColumnCount() const
a7f61f76 173 {
fc5eacdb 174 return 3;
a7f61f76 175 }
b910a8ad 176
9861f022 177 virtual wxString GetColumnType( unsigned int col ) const
5debbdcf
RR
178 {
179 return "string";
180 }
b910a8ad 181
5debbdcf
RR
182 virtual void GetValue( wxVariant &variant,
183 const wxDataViewItem &item, unsigned int col ) const
184 {
b5fce9e2 185 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10 186 switch (col)
605c2c4a 187 {
1e08ad10
RR
188 case 0: variant = node->m_title; break;
189 case 1: variant = node->m_artist; break;
190 case 2: variant = node->m_year; break;
191 default: wxLogError( "MyMusicModel::GetValue: wrong column" );
a7f61f76 192 }
e152afc3 193 }
9861f022 194
5debbdcf
RR
195 virtual bool SetValue( const wxVariant &variant,
196 const wxDataViewItem &item, unsigned int col )
9861f022 197 {
b5fce9e2 198 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10
RR
199 switch (col)
200 {
201 case 0: node->m_title = variant.GetString(); break;
202 case 1: node->m_artist = variant.GetString(); break;
203 case 2: node->m_year = variant.GetString(); break;
204 default: wxLogError( "MyMusicModel::SetValue: wrong column" );
205 }
9861f022
RR
206 }
207
5debbdcf 208 virtual bool HasChildren( const wxDataViewItem &item ) const
b910a8ad 209 {
b5fce9e2
RR
210 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
211 if (!node)
1e08ad10
RR
212 return true;
213
1e08ad10 214 return node->IsContainer();
e152afc3 215 }
5debbdcf 216
5debbdcf 217 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
1e510b1e 218 {
b5fce9e2
RR
219 MyMusicModelNode *node = (MyMusicModelNode*) parent.GetID();
220 if (!node)
221 return wxDataViewItem( (void*) m_root );
1e08ad10 222
1e08ad10
RR
223 if (node->GetChildCount() == 0)
224 return wxDataViewItem( 0 );
befa9b61
RR
225
226 if (node == m_classical)
227 {
228 MyMusicModel *model = (MyMusicModel*)(const MyMusicModel*) this;
229 model->m_classicalMusicIsKnownToControl = true;
230 }
1e510b1e 231
1e08ad10 232 MyMusicModelNode *first_child = node->GetChildren().Item( 0 );
b5fce9e2 233 return wxDataViewItem( (void*) first_child );
9861f022 234 }
1e08ad10 235
5debbdcf 236 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
241cb04b 237 {
b5fce9e2 238 MyMusicModelNode *node = (MyMusicModelNode*) item.GetID();
1e08ad10
RR
239 MyMusicModelNode *parent = node->GetParent();
240 if (!parent)
241 return wxDataViewItem(0);
242
243 int pos = parent->GetChildren().Index( node );
244 if (pos == wxNOT_FOUND)
245 return wxDataViewItem(0);
246
247 if (pos == parent->GetChildCount()-1)
248 return wxDataViewItem(0);
249
250 node = parent->GetChildren().Item( pos+1 );
b5fce9e2 251 return wxDataViewItem( (void*) node );
1e08ad10
RR
252 }
253
254private:
1e08ad10
RR
255 MyMusicModelNode* m_root;
256 MyMusicModelNode* m_pop;
257 MyMusicModelNode* m_classical;
befa9b61 258 bool m_classicalMusicIsKnownToControl;
241cb04b
RR
259};
260
362d7fb9
RR
261// -------------------------------------
262// MyApp
263// -------------------------------------
bd6169a6
RR
264
265class MyApp: public wxApp
266{
267public:
268 bool OnInit(void);
87f0efe2 269 int OnExit();
bd6169a6
RR
270};
271
362d7fb9
RR
272// -------------------------------------
273// MyFrame
274// -------------------------------------
275
87f0efe2 276class MyFrame : public wxFrame
bd6169a6
RR
277{
278public:
279 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
280
281public:
282 void OnQuit(wxCommandEvent& event);
283 void OnAbout(wxCommandEvent& event);
1e08ad10 284 void OnAdd(wxCommandEvent& event);
e63807a8 285 void OnDelete(wxCommandEvent& event);
b910a8ad 286
bd6169a6 287private:
5debbdcf 288 wxDataViewCtrl* m_dataview;
1e08ad10
RR
289 wxTextCtrl * m_log;
290 wxObjectDataPtr<MyMusicModel> m_model;
31fb32e1 291
241cb04b 292private:
abd6692c 293 DECLARE_EVENT_TABLE()
241cb04b
RR
294};
295
362d7fb9
RR
296// -------------------------------------
297// MyApp
298// -------------------------------------
299
87f0efe2 300IMPLEMENT_APP(MyApp)
bd6169a6 301
bd6169a6
RR
302bool MyApp::OnInit(void)
303{
45e6e6f8
VZ
304 if ( !wxApp::OnInit() )
305 return false;
306
87f0efe2
RR
307 // build the first frame
308 MyFrame *frame =
1e08ad10 309 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 700, 440);
bd6169a6
RR
310 frame->Show(true);
311
312 SetTopWindow(frame);
bd6169a6
RR
313 return true;
314}
315
87f0efe2
RR
316int MyApp::OnExit()
317{
318 return 0;
319}
320
321
bd6169a6
RR
322// -------------------------------------
323// MyFrame
324// -------------------------------------
325
87f0efe2
RR
326enum
327{
328 // file menu
329 ID_ABOUT = wxID_ABOUT,
87f0efe2 330 ID_EXIT = wxID_EXIT,
1e08ad10
RR
331
332 ID_ADD = 100,
e63807a8 333 ID_DELETE = 101,
87f0efe2
RR
334};
335
336BEGIN_EVENT_TABLE(MyFrame, wxFrame)
87f0efe2 337 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
87f0efe2 338 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
1e08ad10 339 EVT_BUTTON( ID_ADD, MyFrame::OnAdd )
e63807a8 340 EVT_BUTTON( ID_DELETE, MyFrame::OnDelete )
87f0efe2
RR
341END_EVENT_TABLE()
342
bd6169a6
RR
343MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
344 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
345{
0bcd4039 346 SetIcon(wxICON(sample));
bd6169a6 347
87f0efe2
RR
348 // build the menus:
349
bd6169a6 350 wxMenu *file_menu = new wxMenu;
5debbdcf 351 file_menu->Append(ID_ABOUT, "&About");
87f0efe2 352 file_menu->AppendSeparator();
5debbdcf 353 file_menu->Append(ID_EXIT, "E&xit");
bd6169a6 354
bd6169a6 355 wxMenuBar *menu_bar = new wxMenuBar;
5debbdcf 356 menu_bar->Append(file_menu, "&File");
87f0efe2 357
bd6169a6 358 SetMenuBar(menu_bar);
87f0efe2 359 CreateStatusBar();
bd6169a6 360
1e08ad10
RR
361 wxBoxSizer *main_sizer = new wxBoxSizer( wxVERTICAL );
362
5debbdcf
RR
363 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
364 wxDefaultSize );
bd6169a6 365
1e08ad10
RR
366 m_model = new MyMusicModel;
367 m_dataview->AssociateModel( m_model.get() );
b910a8ad 368
fc5eacdb 369 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
94b1f7bc 370 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
55fbde12 371 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_EDITABLE, 200,
94b1f7bc 372 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
fc5eacdb 373 m_dataview->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
9861f022 374 DEFAULT_ALIGN );
1e08ad10
RR
375
376 main_sizer->Add( m_dataview, 2, wxGROW );
377
378 wxBoxSizer *button_sizer = new wxBoxSizer( wxHORIZONTAL );
379
380 button_sizer->Add( new wxButton( this, ID_ADD, "Add Mozart"), 0, wxALL, 10 );
e63807a8 381 button_sizer->Add( new wxButton( this, ID_DELETE, "Delete selected"), 0, wxALL, 10 );
1e08ad10
RR
382
383 main_sizer->Add( button_sizer, 0, 0, 0 );
384
385 m_log = new wxTextCtrl( this, -1, "", wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
386
387 main_sizer->Add( m_log, 1, wxGROW );
388
389 SetSizer( main_sizer );
bd6169a6
RR
390}
391
392void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
393{
394 Close(true);
395}
396
e63807a8 397void MyFrame::OnDelete(wxCommandEvent& WXUNUSED(event) )
1e08ad10 398{
1e08ad10
RR
399 wxDataViewItem item = m_dataview->GetSelection();
400 if (item.IsOk())
e63807a8
RR
401 m_model->Delete( item );
402}
403
404void MyFrame::OnAdd(wxCommandEvent& WXUNUSED(event) )
405{
1e08ad10
RR
406 m_model->AddToClassical( "Kleine Nachtmusik", "Wolfgang Mozart", "1787" );
407}
408
bd6169a6
RR
409void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
410{
87f0efe2
RR
411 wxAboutDialogInfo info;
412 info.SetName(_("DataView sample"));
413 info.SetDescription(_("This sample demonstrates the dataview control handling"));
414 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
bd6169a6 415
87f0efe2 416 wxAboutBox(info);
bd6169a6
RR
417}
418