]> git.saurik.com Git - wxWidgets.git/blame - samples/dataview/dataview.cpp
Workaround the lack of a wx.EVT_FIND_CLOSE on Windows when doing a
[wxWidgets.git] / samples / dataview / dataview.cpp
CommitLineData
bd6169a6
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: dataview.cpp
3// Purpose: DataVewCtrl wxWidgets sample
4// Author: Robert Roebling
5// Modified by:
6// Created: 06/01/06
7// RCS-ID: $Id$
8// Copyright: (c) Robert Roebling
9// Licence: wxWindows license
10/////////////////////////////////////////////////////////////////////////////
11
12// For compilers that support precompilation, includes "wx/wx.h".
13#include "wx/wxprec.h"
14
15#ifdef __BORLANDC__
16#pragma hdrstop
17#endif
18
19#ifndef WX_PRECOMP
20#include "wx/wx.h"
21#endif
22
23#ifndef __WXMSW__
24#include "mondrian.xpm"
25#endif
26
362d7fb9 27#include "wx/dataview.h"
bd6169a6 28
362d7fb9
RR
29// -------------------------------------
30// MyTextModel
31// -------------------------------------
bd6169a6 32
362d7fb9 33class MyTextModel: public wxDataViewListModel
bd6169a6
RR
34{
35public:
a7f61f76
RR
36 MyTextModel()
37 {
38 size_t i;
39 for (i = 0; i < 1000; i++)
40 m_list.Add( wxT("Test") );
605c2c4a
RR
41 for (i = 0; i < 500; i++)
42 { m_bools.Add( 0 ); m_bools.Add( 1 ); }
a7f61f76 43 }
362d7fb9
RR
44
45 virtual size_t GetNumberOfRows()
46 { return 1000; }
47 virtual size_t GetNumberOfCols()
605c2c4a
RR
48 { return 4; }
49
362d7fb9
RR
50 // as reported by wxVariant
51 virtual wxString GetColType( size_t col )
605c2c4a
RR
52 {
53 if (col == 3)
54 return wxT("bool");
55
56 return wxT("string");
57 }
a7f61f76 58
362d7fb9 59 virtual wxVariant GetValue( size_t col, size_t row )
605c2c4a
RR
60 {
61 if (col == 3)
62 {
63 return (bool) m_bools[row];
64 } else
a7f61f76
RR
65 if (col == 2)
66 {
67 return m_list[row];
68 }
69 else
70 {
71 wxString tmp;
72 tmp.Printf( wxT("item(%d;%d)"), (int)row, (int)col );
73 return tmp;
74 }
362d7fb9 75 }
a7f61f76
RR
76 virtual bool SetValue( wxVariant &value, size_t col, size_t row )
77 {
605c2c4a
RR
78 if (col == 3)
79 {
80 m_bools[row] = (int) value.GetBool();
81 } else
a7f61f76
RR
82 if (col == 2)
83 {
84 m_list[row] = value.GetString();
85 }
86 return true;
87 }
88
89 wxArrayString m_list;
605c2c4a 90 wxArrayInt m_bools;
0313c114 91};
bd6169a6 92
362d7fb9
RR
93// -------------------------------------
94// MyApp
95// -------------------------------------
bd6169a6
RR
96
97class MyApp: public wxApp
98{
99public:
100 bool OnInit(void);
101};
102
362d7fb9
RR
103// -------------------------------------
104// MyFrame
105// -------------------------------------
106
bd6169a6
RR
107class MyFrame: public wxFrame
108{
109public:
110 MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h);
111
112public:
113 void OnQuit(wxCommandEvent& event);
114 void OnAbout(wxCommandEvent& event);
115
116private:
a7f61f76
RR
117 wxDataViewCtrl* dataview_left;
118 wxDataViewCtrl* dataview_right;
bd6169a6
RR
119};
120
362d7fb9
RR
121// -------------------------------------
122// MyApp
123// -------------------------------------
124
bd6169a6
RR
125#define DYNAMIC_QUIT wxID_EXIT
126#define DYNAMIC_ABOUT wxID_ABOUT
127
bd6169a6
RR
128IMPLEMENT_APP (MyApp)
129
bd6169a6
RR
130bool MyApp::OnInit(void)
131{
605c2c4a 132 MyFrame *frame = new MyFrame(NULL, _T("Dynamic wxWidgets App"), 50, 50, 600, 340);
bd6169a6 133
bd6169a6
RR
134 frame->Show(true);
135
136 SetTopWindow(frame);
137
138 return true;
139}
140
141// -------------------------------------
142// MyFrame
143// -------------------------------------
144
bd6169a6
RR
145MyFrame::MyFrame(wxFrame *frame, wxChar *title, int x, int y, int w, int h):
146 wxFrame(frame, wxID_ANY, title, wxPoint(x, y), wxSize(w, h))
147{
bd6169a6
RR
148#ifdef __WXMSW__
149 SetIcon(wxIcon(_T("mondrian")));
150#else
151 SetIcon(wxIcon(mondrian_xpm));
152#endif
153
bd6169a6
RR
154 wxMenu *file_menu = new wxMenu;
155
156 file_menu->Append(DYNAMIC_ABOUT, _T("&About"));
157 file_menu->Append(DYNAMIC_QUIT, _T("E&xit"));
158 wxMenuBar *menu_bar = new wxMenuBar;
159 menu_bar->Append(file_menu, _T("&File"));
160 SetMenuBar(menu_bar);
161
162 // You used to have to do some casting for param 4, but now there are type-safe handlers
163 Connect( DYNAMIC_QUIT, wxID_ANY,
164 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnQuit) );
165 Connect( DYNAMIC_ABOUT, wxID_ANY,
166 wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(MyFrame::OnAbout) );
167
168 CreateStatusBar();
169
170
a7f61f76
RR
171 // Left wxDataViewCtrl
172 dataview_left = new wxDataViewCtrl( this, -1 );
bd6169a6 173
362d7fb9 174 MyTextModel *model = new MyTextModel;
a7f61f76
RR
175 dataview_left->AssociateModel( model );
176
605c2c4a
RR
177 dataview_left->AppendTextColumn( wxT("first"), 0 );
178 dataview_left->AppendTextColumn( wxT("second"), 1 );
179 wxDataViewTextCell *text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
180 wxDataViewColumn *column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
a7f61f76 181 dataview_left->AppendColumn( column );
605c2c4a 182 dataview_left->AppendToggleColumn( wxT("fourth"), 3 );
a7f61f76
RR
183
184 // Right wxDataViewCtrl using the same model
185 dataview_right = new wxDataViewCtrl( this, -1 );
186 dataview_right->AssociateModel( model );
0313c114 187
605c2c4a
RR
188 text_cell = new wxDataViewTextCell( wxT("string"), wxDATAVIEW_CELL_EDITABLE );
189 column = new wxDataViewColumn( wxT("editable"), text_cell, 2 );
190 dataview_right->AppendColumn( column );
191 dataview_right->AppendTextColumn( wxT("first"), 0 );
192 dataview_right->AppendTextColumn( wxT("second"), 1 );
193 wxDataViewToggleCell *toggle_cell = new wxDataViewToggleCell( wxT("bool"), wxDATAVIEW_CELL_EDITABLE );
194 column = new wxDataViewColumn( wxT("bool"), toggle_cell, 3 );
a7f61f76 195 dataview_right->AppendColumn( column );
0313c114 196
a7f61f76
RR
197 wxBoxSizer *sizer = new wxBoxSizer( wxHORIZONTAL );
198 sizer->Add( dataview_left, 1, wxGROW );
199 sizer->Add(10,10);
200 sizer->Add( dataview_right, 1, wxGROW );
201 SetSizer( sizer );
bd6169a6
RR
202}
203
204void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
205{
206 Close(true);
207}
208
209void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
210{
211 wxMessageDialog dialog(this, _T("This demonstrates the dataview control handling"),
212 _T("About DataView"), wxOK);
213
214 dialog.ShowModal();
215}
216
217