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") ); }
45 for (i
= 0; i
< 1000; i
++)
46 { m_progress
.Add( i
/10 ); }
49 virtual size_t GetNumberOfRows()
51 virtual size_t GetNumberOfCols()
54 // as reported by wxVariant
55 virtual wxString
GetColType( size_t col
)
66 virtual wxVariant
GetValue( size_t col
, size_t row
)
70 return (long) m_progress
[row
];
74 return m_colours
[row
];
78 return (bool) m_bools
[row
];
87 tmp
.Printf( wxT("item(%d;%d)"), (int)row
, (int)col
);
91 virtual bool SetValue( wxVariant
&value
, size_t col
, size_t row
)
95 m_bools
[row
] = (int) value
.GetBool();
99 m_list
[row
] = value
.GetString();
104 wxArrayString m_list
;
106 wxArrayString m_colours
;
107 wxArrayInt m_progress
;
110 // -------------------------------------
112 // -------------------------------------
114 class MyCustomCell
: public wxDataViewCustomCell
118 wxDataViewCustomCell( wxT("string"), wxDATAVIEW_CELL_ACTIVATABLE
)
120 m_colour
= wxT("black");
122 bool SetValue( const wxVariant
&value
)
124 m_colour
= value
.GetString();
127 bool Render( wxRect rect
, wxDC
*dc
, int state
)
129 dc
->SetPen( *wxBLACK_PEN
);
130 if (m_colour
== wxT("red"))
131 dc
->SetBrush( *wxRED_BRUSH
);
132 else if (m_colour
== wxT("green"))
133 dc
->SetBrush( *wxGREEN_BRUSH
);
135 dc
->SetBrush( *wxBLACK_BRUSH
);
136 dc
->DrawRectangle( rect
);
143 bool Activate( wxRect rect
)
145 wxPrintf( wxT("activate\n") );
153 // -------------------------------------
155 // -------------------------------------
157 class MyApp
: public wxApp
163 // -------------------------------------
165 // -------------------------------------
167 class MyFrame
: public wxFrame
170 MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
);
173 void OnQuit(wxCommandEvent
& event
);
174 void OnAbout(wxCommandEvent
& event
);
177 wxDataViewCtrl
* dataview_left
;
178 wxDataViewCtrl
* dataview_right
;
181 // -------------------------------------
183 // -------------------------------------
185 #define DYNAMIC_QUIT wxID_EXIT
186 #define DYNAMIC_ABOUT wxID_ABOUT
188 IMPLEMENT_APP (MyApp
)
190 bool MyApp::OnInit(void)
192 MyFrame
*frame
= new MyFrame(NULL
, _T("Dynamic wxWidgets App"), 50, 50, 600, 340);
201 // -------------------------------------
203 // -------------------------------------
205 MyFrame::MyFrame(wxFrame
*frame
, wxChar
*title
, int x
, int y
, int w
, int h
):
206 wxFrame(frame
, wxID_ANY
, title
, wxPoint(x
, y
), wxSize(w
, h
))
209 SetIcon(wxIcon(_T("mondrian")));
211 SetIcon(wxIcon(mondrian_xpm
));
214 wxMenu
*file_menu
= new wxMenu
;
216 file_menu
->Append(DYNAMIC_ABOUT
, _T("&About"));
217 file_menu
->Append(DYNAMIC_QUIT
, _T("E&xit"));
218 wxMenuBar
*menu_bar
= new wxMenuBar
;
219 menu_bar
->Append(file_menu
, _T("&File"));
220 SetMenuBar(menu_bar
);
222 // You used to have to do some casting for param 4, but now there are type-safe handlers
223 Connect( DYNAMIC_QUIT
, wxID_ANY
,
224 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnQuit
) );
225 Connect( DYNAMIC_ABOUT
, wxID_ANY
,
226 wxEVT_COMMAND_MENU_SELECTED
, wxCommandEventHandler(MyFrame::OnAbout
) );
231 // Left wxDataViewCtrl
232 dataview_left
= new wxDataViewCtrl( this, -1 );
234 MyTextModel
*model
= new MyTextModel
;
235 dataview_left
->AssociateModel( model
);
237 dataview_left
->AppendTextColumn( wxT("first"), 0 );
238 dataview_left
->AppendTextColumn( wxT("second"), 1 );
240 wxDataViewTextCell
*text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
241 wxDataViewColumn
*column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
242 dataview_left
->AppendColumn( column
);
244 dataview_left
->AppendToggleColumn( wxT("fourth"), 3 );
246 MyCustomCell
*custom_cell
= new MyCustomCell
;
247 column
= new wxDataViewColumn( wxT("custom"), custom_cell
, 4 );
248 dataview_left
->AppendColumn( column
);
250 dataview_left
->AppendProgressColumn( wxT("progress"), 5 );
252 // Right wxDataViewCtrl using the same model
253 dataview_right
= new wxDataViewCtrl( this, -1 );
254 dataview_right
->AssociateModel( model
);
256 text_cell
= new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE
);
257 column
= new wxDataViewColumn( wxT("editable"), text_cell
, 2 );
258 dataview_right
->AppendColumn( column
);
259 dataview_right
->AppendTextColumn( wxT("first"), 0 );
260 dataview_right
->AppendTextColumn( wxT("second"), 1 );
261 wxDataViewToggleCell
*toggle_cell
= new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE
);
262 column
= new wxDataViewColumn( wxT("bool"), toggle_cell
, 3 );
263 dataview_right
->AppendColumn( column
);
265 wxBoxSizer
*sizer
= new wxBoxSizer( wxHORIZONTAL
);
266 sizer
->Add( dataview_left
, 1, wxGROW
);
268 sizer
->Add( dataview_right
, 1, wxGROW
);
272 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
277 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
279 wxMessageDialog
dialog(this, _T("This demonstrates the dataview control handling"),
280 _T("About DataView"), wxOK
);