1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: DataVewCtrl wxWidgets sample
4 // Author: Robert Roebling
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"
24 #include "mondrian.xpm"
27 #include "wx/dataview.h"
29 // -------------------------------------
31 // -------------------------------------
33 class MyTextModel
: public wxDataViewListModel
39 for (i
= 0; i
< 1000; i
++)
40 m_list
.Add( wxT("Test") );
41 for (i
= 0; i
< 500; i
++)
42 { m_bools
.Add( 0 ); m_bools
.Add( 1 ); }
43 for (i
= 0; i
< 500; i
++)
44 { m_colours
.Add( wxT("red") ); m_colours
.Add( wxT("green") ); }
47 virtual size_t GetNumberOfRows()
49 virtual size_t GetNumberOfCols()
52 // as reported by wxVariant
53 virtual wxString
GetColType( size_t col
)
61 virtual wxVariant
GetValue( size_t col
, size_t row
)
65 return (bool) m_bools
[row
];
69 return m_colours
[row
];
78 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
82 virtual bool SetValue( wxVariant
&value
, size_t col
, size_t row
)
86 m_bools
[row
] = (int) value
.GetBool();
90 m_list
[row
] = value
.GetString();
97 wxArrayString m_colours
;
100 // -------------------------------------
102 // -------------------------------------
104 class MyCustomCell
: public wxDataViewCustomCell
108 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_INERT
)
110 m_colour
= wxT("black");
112 bool SetValue( const wxVariant
&value
)
114 m_colour
= value
.GetString();
117 bool Render( wxRect rect
, wxDC
*dc
, int state
)
119 dc
->SetPen( *wxBLACK_PEN
);
120 if (m_colour
== wxT("red"))
121 dc
->SetBrush( *wxRED_BRUSH
);
122 else if (m_colour
== wxT("green"))
123 dc
->SetBrush( *wxGREEN_BRUSH
);
125 dc
->SetBrush( *wxBLACK_BRUSH
);
126 dc
->DrawRectangle( rect
);
138 // -------------------------------------
140 // -------------------------------------
142 class MyApp
: public wxApp
148 // -------------------------------------
150 // -------------------------------------
152 class MyFrame
: public wxFrame
155 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
158 void OnQuit(wxCommandEvent
& event
);
159 void OnAbout(wxCommandEvent
& event
);
162 wxDataViewCtrl
* dataview_left
;
163 wxDataViewCtrl
* dataview_right
;
166 // -------------------------------------
168 // -------------------------------------
170 #define DYNAMIC_QUIT wxID_EXIT
171 #define DYNAMIC_ABOUT wxID_ABOUT
173 IMPLEMENT_APP (MyApp
)
175 bool MyApp::OnInit(void)
177 MyFrame
*frame
= new MyFrame(NULL
, _T("Dynamic wxWidgets App"), 50, 50, 600, 340);
186 // -------------------------------------
188 // -------------------------------------
190 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
191 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
194 SetIcon(wxIcon(_T("mondrian")));
196 SetIcon(wxIcon(mondrian_xpm
));
199 wxMenu
*file_menu
= new wxMenu
;
201 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
202 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
203 wxMenuBar
*menu_bar
= new wxMenuBar
;
204 menu_bar
->Append(file_menu
, _T("&File"));
205 SetMenuBar(menu_bar
);
207 // You used to have to do some casting for param 4, but now there are type-safe handlers
208 Connect( DYNAMIC_QUIT
, wxID_ANY
,
209 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnQuit
) );
210 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
211 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnAbout
) );
216 // Left wxDataViewCtrl
217 dataview_left
= new wxDataViewCtrl( this, -1 );
219 MyTextModel
*model
= new MyTextModel
;
220 dataview_left
->AssociateModel( model
);
222 dataview_left
->AppendTextColumn( wxT("first"), 0 );
223 dataview_left
->AppendTextColumn( wxT("second"), 1 );
225 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
226 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
227 dataview_left
->AppendColumn( column
);
229 dataview_left
->AppendToggleColumn( wxT("fourth"), 3 );
231 MyCustomCell
*custom_cell
= new MyCustomCell
;
232 column
= new wxDataViewColumn( wxT("custom"), custom_cell
, 4 );
233 dataview_left
->AppendColumn( column
);
235 // Right wxDataViewCtrl using the same model
236 dataview_right
= new wxDataViewCtrl( this, -1 );
237 dataview_right
->AssociateModel( model
);
239 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
240 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
241 dataview_right
->AppendColumn( column
);
242 dataview_right
->AppendTextColumn( wxT("first"), 0 );
243 dataview_right
->AppendTextColumn( wxT("second"), 1 );
244 wxDataViewToggleCell
*toggle_cell
= new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE
);
245 column
= new wxDataViewColumn( wxT("bool"), toggle_cell
, 3 );
246 dataview_right
->AppendColumn( column
);
248 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
249 sizer
->Add( dataview_left
, 1, wxGROW
);
251 sizer
->Add( dataview_right
, 1, wxGROW
);
255 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
260 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
262 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
263 _T("About DataView"), wxOK
);