]>
git.saurik.com Git - wxWidgets.git/blob - samples/dataview/dataview.cpp
457c81f1d9ed493552dbc2c114c3babaf1fd0402
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 int ID
= item
.GetID();
82 case 1: variant
= "My Music"; break;
83 case 2: variant
= "Pop music"; break;
84 case 5: variant
= "Classical music"; break;
89 case 0: variant
= "You are not alone"; break;
90 case 1: variant
= "Michael Jackson"; break;
91 case 2: variant
= "1995";
99 case 0: variant
= "Take a bow"; break;
100 case 1: variant
= "Madonna"; break;
101 case 2: variant
= "1994";
109 case 0: variant
= "Ninth symphony"; break;
110 case 1: variant
= "Ludwig v. Beethoven"; break;
111 case 2: variant
= "1824";
119 case 0: variant
= "German requiem"; break;
120 case 1: variant
= "Johannes Brahms"; break;
121 case 2: variant
= "1868";
129 virtual bool SetValue( const wxVariant
&variant
,
130 const wxDataViewItem
&item
, unsigned int col
)
136 virtual bool HasChildren( const wxDataViewItem
&item
) const
138 int ID
= item
.GetID();
139 return ((ID
== 1) || (ID
== 2) || (ID
== 5));
142 virtual int GetChildCount( const wxDataViewItem
&item
) const
144 int ID
= item
.GetID();
153 virtual wxDataViewItem
GetParent( const wxDataViewItem
&child
) const
155 int ID
= child
.GetID();
159 case 5: return wxDataViewItem( 1 );
161 case 4: return wxDataViewItem( 2 );
163 case 7: return wxDataViewItem( 5 );
166 return wxDataViewItem(0);
168 virtual wxDataViewItem
GetFirstChild( const wxDataViewItem
&parent
) const
170 int ID
= parent
.GetID();
173 case 1: return wxDataViewItem( 2 );
174 case 2: return wxDataViewItem( 3 );
175 case 5: return wxDataViewItem( 6 );
178 return wxDataViewItem(0);
180 virtual wxDataViewItem
GetNextSibling( const wxDataViewItem
&item
) const
182 int ID
= item
.GetID();
185 case 2: return wxDataViewItem( 5 );
186 case 3: return wxDataViewItem( 4 );
187 case 6: return wxDataViewItem( 7 );
190 return wxDataViewItem(0);
192 virtual wxDataViewItem
GetNthChild( const wxDataViewItem
&parent
, unsigned int n
) const
194 int ID
= parent
.GetID();
199 if (n
== 0) return wxDataViewItem( 2 );
200 else if (n
== 1) return wxDataViewItem( 5 );
205 if (n
== 0) return wxDataViewItem( 3 );
206 else if (n
== 1) return wxDataViewItem( 4 );
211 if (n
== 0) return wxDataViewItem( 6 );
212 else if (n
== 1) return wxDataViewItem( 7 );
217 return wxDataViewItem(0);
221 // -------------------------------------
223 // -------------------------------------
225 class MyApp
: public wxApp
232 // -------------------------------------
234 // -------------------------------------
236 class MyFrame
: public wxFrame
239 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
242 void OnQuit(wxCommandEvent
& event
);
243 void OnAbout(wxCommandEvent
& event
);
246 wxDataViewCtrl
* m_dataview
;
249 DECLARE_EVENT_TABLE()
252 // -------------------------------------
254 // -------------------------------------
258 bool MyApp::OnInit(void)
260 if ( !wxApp::OnInit() )
263 // build the first frame
265 new MyFrame(NULL
, wxT("wxDataViewCtrl feature test"), 10, 10, 800, 340);
278 // -------------------------------------
280 // -------------------------------------
285 ID_ABOUT
= wxID_ABOUT
,
289 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
290 EVT_MENU( ID_ABOUT
, MyFrame::OnAbout
)
291 EVT_MENU( ID_EXIT
, MyFrame::OnQuit
)
294 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
295 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
297 SetIcon(wxICON(sample
));
301 wxMenu
*file_menu
= new wxMenu
;
302 file_menu
->Append(ID_ABOUT
, "&About");
303 file_menu
->AppendSeparator();
304 file_menu
->Append(ID_EXIT
, "E&xit");
306 wxMenuBar
*menu_bar
= new wxMenuBar
;
307 menu_bar
->Append(file_menu
, "&File");
309 SetMenuBar(menu_bar
);
312 m_dataview
= new wxDataViewCtrl( this, wxID_ANY
, wxDefaultPosition
,
315 wxObjectDataPtr
<MyMusicModel
> model(new MyMusicModel
);
316 m_dataview
->AssociateModel( model
.get() );
318 m_dataview
->AppendTextColumn( "Title", 0, wxDATAVIEW_CELL_INERT
, -1,
320 m_dataview
->AppendTextColumn( "Artist", 1, wxDATAVIEW_CELL_INERT
, -1,
322 m_dataview
->AppendTextColumn( "Year", 1, wxDATAVIEW_CELL_INERT
, -1,
326 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
331 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
333 wxAboutDialogInfo info
;
334 info
.SetName(_("DataView sample"));
335 info
.SetDescription(_("This sample demonstrates the dataview control handling"));
336 info
.SetCopyright(_T("(C) 2007 Robert Roebling"));