]> git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
a6259d29b1b5ec7d6785cd4f5428b6b346fc81e7
[wxWidgets.git] / samples / dataview / dataview.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dataview.cpp
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
6 // Created: 06/01/06
7 // RCS-ID: $Id$
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #endif
22
23 #include "wx/datetime.h"
24 #include "wx/splitter.h"
25 #include "wx/aboutdlg.h"
26 #include "wx/choicdlg.h"
27 #include "wx/numdlg.h"
28 #include "wx/dataview.h"
29 #include "wx/spinctrl.h"
30
31 #ifndef __WXMSW__
32 #include "../sample.xpm"
33 #endif
34
35 #include "null.xpm"
36
37
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
40
41
42
43 // -------------------------------------
44 // MyMusicModel
45 // -------------------------------------
46
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 */
59
60
61 class MyMusicModel: public wxDataViewModel
62 {
63 public:
64 MyMusicModel() {}
65
66 virtual unsigned int GetColumnCount() const
67 {
68 return 3;
69 }
70
71 virtual wxString GetColumnType( unsigned int col ) const
72 {
73 return "string";
74 }
75
76 virtual void GetValue( wxVariant &variant,
77 const wxDataViewItem &item, unsigned int col ) const
78 {
79 variant = wxString("");
80 int ID = item.GetID();
81 switch (ID)
82 {
83 case 1: if (col == 0) variant = wxString("My Music"); break;
84 case 2: if (col == 0) variant = wxString("Pop music"); break;
85 case 5: if (col == 0) variant = wxString("Classical music"); break;
86 case 3:
87 {
88 switch (col)
89 {
90 case 0: variant = wxString("You are not alone"); break;
91 case 1: variant = wxString("Michael Jackson"); break;
92 case 2: variant = wxString("1995");
93 }
94 }
95 break;
96 case 4:
97 {
98 switch (col)
99 {
100 case 0: variant = wxString("Take a bow"); break;
101 case 1: variant = wxString("Madonna"); break;
102 case 2: variant = wxString("1994");
103 }
104 }
105 break;
106 case 6:
107 {
108 switch (col)
109 {
110 case 0: variant = wxString("Ninth symphony"); break;
111 case 1: variant = wxString("Ludwig v. Beethoven"); break;
112 case 2: variant = wxString("1824");
113 }
114 }
115 break;
116 case 7:
117 {
118 switch (col)
119 {
120 case 0: variant = wxString("German requiem"); break;
121 case 1: variant = wxString("Johannes Brahms"); break;
122 case 2: variant = wxString("1868");
123 }
124 }
125 break;
126 }
127
128 }
129
130 virtual bool SetValue( const wxVariant &variant,
131 const wxDataViewItem &item, unsigned int col )
132 {
133 // readonly
134 return true;
135 }
136
137 /*****************************************************************
138 If wxDataViewItem is not valid in the two methods I quote above
139 then it means "return the child item from the invisible root".
140 ******************************************************************/
141
142 virtual bool HasChildren( const wxDataViewItem &item ) const
143 {
144 int ID = item.GetID();
145 return ((ID == 1) || (ID == 2) || (ID == 5) || (ID == 0));
146 }
147
148 virtual wxDataViewItem GetFirstChild( const wxDataViewItem &parent ) const
149 {
150 int ID = parent.GetID();
151 switch (ID)
152 {
153 case 0: return wxDataViewItem( 1 );
154 case 1: return wxDataViewItem( 2 );
155 case 2: return wxDataViewItem( 3 );
156 case 5: return wxDataViewItem( 6 );
157 }
158
159 return wxDataViewItem(0);
160 }
161 virtual wxDataViewItem GetNextSibling( const wxDataViewItem &item ) const
162 {
163 int ID = item.GetID();
164 switch (ID)
165 {
166 case 2: return wxDataViewItem( 5 );
167 case 3: return wxDataViewItem( 4 );
168 case 6: return wxDataViewItem( 7 );
169 }
170
171 return wxDataViewItem(0);
172 }
173 };
174
175 // -------------------------------------
176 // MyApp
177 // -------------------------------------
178
179 class MyApp: public wxApp
180 {
181 public:
182 bool OnInit(void);
183 int OnExit();
184 };
185
186 // -------------------------------------
187 // MyFrame
188 // -------------------------------------
189
190 class MyFrame : public wxFrame
191 {
192 public:
193 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
194
195 public:
196 void OnQuit(wxCommandEvent& event);
197 void OnAbout(wxCommandEvent& event);
198
199 private:
200 wxDataViewCtrl* m_dataview;
201
202 private:
203 DECLARE_EVENT_TABLE()
204 };
205
206 // -------------------------------------
207 // MyApp
208 // -------------------------------------
209
210 IMPLEMENT_APP(MyApp)
211
212 bool MyApp::OnInit(void)
213 {
214 if ( !wxApp::OnInit() )
215 return false;
216
217 // build the first frame
218 MyFrame *frame =
219 new MyFrame(NULL, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
220 frame->Show(true);
221
222 SetTopWindow(frame);
223 return true;
224 }
225
226 int MyApp::OnExit()
227 {
228 return 0;
229 }
230
231
232 // -------------------------------------
233 // MyFrame
234 // -------------------------------------
235
236 enum
237 {
238 // file menu
239 ID_ABOUT = wxID_ABOUT,
240 ID_EXIT = wxID_EXIT,
241 };
242
243 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
244 EVT_MENU( ID_ABOUT, MyFrame::OnAbout )
245 EVT_MENU( ID_EXIT, MyFrame::OnQuit )
246 END_EVENT_TABLE()
247
248 MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
249 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
250 {
251 SetIcon(wxICON(sample));
252
253 // build the menus:
254
255 wxMenu *file_menu = new wxMenu;
256 file_menu->Append(ID_ABOUT, "&About");
257 file_menu->AppendSeparator();
258 file_menu->Append(ID_EXIT, "E&xit");
259
260 wxMenuBar *menu_bar = new wxMenuBar;
261 menu_bar->Append(file_menu, "&File");
262
263 SetMenuBar(menu_bar);
264 CreateStatusBar();
265
266 m_dataview = new wxDataViewCtrl( this, wxID_ANY, wxDefaultPosition,
267 wxDefaultSize );
268
269 wxObjectDataPtr<MyMusicModel> model(new MyMusicModel);
270 m_dataview->AssociateModel( model.get() );
271
272 m_dataview->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT, 200,
273 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
274 m_dataview->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT, 200,
275 DEFAULT_ALIGN, wxDATAVIEW_COL_SORTABLE );
276 m_dataview->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT, 50,
277 DEFAULT_ALIGN );
278 }
279
280 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
281 {
282 Close(true);
283 }
284
285 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
286 {
287 wxAboutDialogInfo info;
288 info.SetName(_("DataView sample"));
289 info.SetDescription(_("This sample demonstrates the dataview control handling"));
290 info.SetCopyright(_T("(C) 2007 Robert Roebling"));
291
292 wxAboutBox(info);
293 }
294