]>
git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
9a80b8196a7dbdfcff89b2db92112d3e69e6ae2a
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxDataViewCtrl wxWidgets sample
4 // Author: Robert Roebling
5 // Modified by: Francesco Montorsi
8 // Copyright: (c) Robert Roebling
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
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"
32 #include "../sample.xpm"
38 #define DEFAULT_ALIGN wxALIGN_LEFT
39 #define DATAVIEW_DEFAULT_STYLE (wxDV_MULTIPLE|wxDV_HORIZ_RULES|wxDV_VERT_RULES)
43 // -------------------------------------
45 // -------------------------------------
48 Implement this data model
50 -------------------------------------------------------------
53 3: You are not alone Michael Jackson 1995
54 4: Take a bow Madonna 1994
56 6: Ninth Symphony Ludwig v. Beethoven 1824
57 7: German Requiem Johannes Brahms 1868
61 class MyMusicModel
: public wxDataViewModel
66 virtual unsigned int GetColumnCount() const
71 virtual wxString
GetColumnType( unsigned int col
) const
76 virtual void GetValue( wxVariant
&variant
,
77 const wxDataViewItem
&item
, unsigned int col
) const
79 variant
= wxString("");
80 int ID
= item
.GetID();
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;
90 case 0: variant
= wxString("You are not alone"); break;
91 case 1: variant
= wxString("Michael Jackson"); break;
92 case 2: variant
= wxString("1995");
100 case 0: variant
= wxString("Take a bow"); break;
101 case 1: variant
= wxString("Madonna"); break;
102 case 2: variant
= wxString("1994");
110 case 0: variant
= wxString("Ninth symphony"); break;
111 case 1: variant
= wxString("Ludwig v. Beethoven"); break;
112 case 2: variant
= wxString("1824");
120 case 0: variant
= wxString("German requiem"); break;
121 case 1: variant
= wxString("Johannes Brahms"); break;
122 case 2: variant
= wxString("1868");
130 virtual bool SetValue( const wxVariant
&variant
,
131 const wxDataViewItem
&item
, unsigned int col
)
137 virtual bool HasChildren( const wxDataViewItem
&item
) const
139 int ID
= item
.GetID();
140 return ((ID
== 1) || (ID
== 2) || (ID
== 5));
143 virtual int GetChildCount( const wxDataViewItem
&item
) const
145 int ID
= item
.GetID();
154 virtual wxDataViewItem
GetParent( const wxDataViewItem
&child
) const
156 int ID
= child
.GetID();
160 case 5: return wxDataViewItem( 1 );
162 case 4: return wxDataViewItem( 2 );
164 case 7: return wxDataViewItem( 5 );
167 return wxDataViewItem(0);
169 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
171 int ID
= parent
.GetID();
174 case 1: return wxDataViewItem( 2 );
175 case 2: return wxDataViewItem( 3 );
176 case 5: return wxDataViewItem( 6 );
179 return wxDataViewItem(0);
181 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
183 int ID
= item
.GetID();
186 case 2: return wxDataViewItem( 5 );
187 case 3: return wxDataViewItem( 4 );
188 case 6: return wxDataViewItem( 7 );
191 return wxDataViewItem(0);
193 virtual wxDataViewItem
GetNthChild( const wxDataViewItem
&parent
, unsigned int n
) const
199 return wxDataViewItem( 1 );
201 return wxDataViewItem( 0 );
204 int ID
= parent
.GetID();
209 if (n
== 0) return wxDataViewItem( 2 );
210 else if (n
== 1) return wxDataViewItem( 5 );
215 if (n
== 0) return wxDataViewItem( 3 );
216 else if (n
== 1) return wxDataViewItem( 4 );
221 if (n
== 0) return wxDataViewItem( 6 );
222 else if (n
== 1) return wxDataViewItem( 7 );
227 return wxDataViewItem(0);
231 // -------------------------------------
233 // -------------------------------------
235 class MyApp
: public wxApp
242 // -------------------------------------
244 // -------------------------------------
246 class MyFrame
: public wxFrame
249 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
252 void OnQuit(wxCommandEvent
& event
);
253 void OnAbout(wxCommandEvent
& event
);
256 wxDataViewCtrl
* m_dataview
;
259 DECLARE_EVENT_TABLE()
262 // -------------------------------------
264 // -------------------------------------
268 bool MyApp::OnInit(void)
270 if ( !wxApp::OnInit() )
273 // build the first frame
275 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
288 // -------------------------------------
290 // -------------------------------------
295 ID_ABOUT
= wxID_ABOUT
,
299 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
300 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
301 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
304 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
305 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
307 SetIcon(wxICON(sample
));
311 wxMenu
*file_menu
= new wxMenu
;
312 file_menu
->Append(ID_ABOUT
, "&About");
313 file_menu
->AppendSeparator();
314 file_menu
->Append(ID_EXIT
, "E&xit");
316 wxMenuBar
*menu_bar
= new wxMenuBar
;
317 menu_bar
->Append(file_menu
, "&File");
319 SetMenuBar(menu_bar
);
322 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
325 wxObjectDataPtr
<MyMusicModel
> model(new MyMusicModel
);
326 m_dataview
->AssociateModel( model
.get() );
328 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
330 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, 200,
332 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
336 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
341 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
343 wxAboutDialogInfo info
;
344 info
.SetName(_("DataView sample"));
345 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
346 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));