]>
git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
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 /*****************************************************************
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 ******************************************************************/
142 virtual bool HasChildren( const wxDataViewItem
&item
) const
144 int ID
= item
.GetID();
145 return ((ID
== 1) || (ID
== 2) || (ID
== 5) || (ID
== 0));
148 virtual int GetChildCount( const wxDataViewItem
&item
) const
150 int ID
= item
.GetID();
160 virtual wxDataViewItem
GetParent( const wxDataViewItem
&child
) const
162 int ID
= child
.GetID();
166 case 5: return wxDataViewItem( 1 );
168 case 4: return wxDataViewItem( 2 );
170 case 7: return wxDataViewItem( 5 );
173 return wxDataViewItem(0);
175 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
177 int ID
= parent
.GetID();
180 case 0: return wxDataViewItem( 1 );
181 case 1: return wxDataViewItem( 2 );
182 case 2: return wxDataViewItem( 3 );
183 case 5: return wxDataViewItem( 6 );
186 return wxDataViewItem(0);
188 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
190 int ID
= item
.GetID();
193 case 2: return wxDataViewItem( 5 );
194 case 3: return wxDataViewItem( 4 );
195 case 6: return wxDataViewItem( 7 );
198 return wxDataViewItem(0);
200 virtual wxDataViewItem
GetNthChild( const wxDataViewItem
&parent
, unsigned int n
) const
206 return wxDataViewItem( 1 );
208 return wxDataViewItem( 0 );
211 int ID
= parent
.GetID();
216 if (n
== 0) return wxDataViewItem( 2 );
217 else if (n
== 1) return wxDataViewItem( 5 );
222 if (n
== 0) return wxDataViewItem( 3 );
223 else if (n
== 1) return wxDataViewItem( 4 );
228 if (n
== 0) return wxDataViewItem( 6 );
229 else if (n
== 1) return wxDataViewItem( 7 );
234 return wxDataViewItem(0);
238 // -------------------------------------
240 // -------------------------------------
242 class MyApp
: public wxApp
249 // -------------------------------------
251 // -------------------------------------
253 class MyFrame
: public wxFrame
256 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
259 void OnQuit(wxCommandEvent
& event
);
260 void OnAbout(wxCommandEvent
& event
);
263 wxDataViewCtrl
* m_dataview
;
266 DECLARE_EVENT_TABLE()
269 // -------------------------------------
271 // -------------------------------------
275 bool MyApp::OnInit(void)
277 if ( !wxApp::OnInit() )
280 // build the first frame
282 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
295 // -------------------------------------
297 // -------------------------------------
302 ID_ABOUT
= wxID_ABOUT
,
306 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
307 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
308 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
311 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
312 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
314 SetIcon(wxICON(sample
));
318 wxMenu
*file_menu
= new wxMenu
;
319 file_menu
->Append(ID_ABOUT
, "&About");
320 file_menu
->AppendSeparator();
321 file_menu
->Append(ID_EXIT
, "E&xit");
323 wxMenuBar
*menu_bar
= new wxMenuBar
;
324 menu_bar
->Append(file_menu
, "&File");
326 SetMenuBar(menu_bar
);
329 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
332 wxObjectDataPtr
<MyMusicModel
> model(new MyMusicModel
);
333 m_dataview
->AssociateModel( model
.get() );
335 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, 200,
336 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
337 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, 200,
338 DEFAULT_ALIGN
, wxDATAVIEW_COL_SORTABLE
);
339 m_dataview
->AppendTextColumn( "Year", 2, wxDATAVIEW_CELL_INERT
, 50,
343 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
348 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
350 wxAboutDialogInfo info
;
351 info
.SetName(_("DataView sample"));
352 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
353 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));