]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | /* |
2f6c54eb VZ |
2 | * File: grid.cpp |
3 | * Purpose: wxGrid test | |
17e74315 JS |
4 | |
5 | PLEASE NOTE: this sample is deprecated. See | |
6 | newgrid for a sample based on the newer wxGrid API. | |
7 | ||
2f6c54eb VZ |
8 | * Author: Julian Smart |
9 | * Created: 1995 | |
10 | * Updated: | |
c801d85f KB |
11 | * Copyright: (c) 1995, AIAI, University of Edinburgh |
12 | */ | |
13 | ||
14 | static const char sccsid[] = "%W% %G%"; | |
15 | ||
c801d85f KB |
16 | // For compilers that support precompilation, includes "wx/wx.h". |
17 | #include "wx/wxprec.h" | |
18 | ||
19 | #ifdef __BORLANDC__ | |
20 | #pragma hdrstop | |
21 | #endif | |
22 | ||
23 | #ifndef WX_PRECOMP | |
24 | #include "wx/wx.h" | |
25 | #endif | |
26 | ||
27 | #include "wx/grid.h" | |
28 | #include "wx/colordlg.h" | |
29 | ||
30 | // Define a new application type | |
31 | class MyApp: public wxApp | |
2f6c54eb VZ |
32 | { |
33 | public: | |
34 | virtual bool OnInit(void); | |
35 | virtual int OnExit(); | |
c801d85f KB |
36 | }; |
37 | ||
c0b042fc | 38 | |
c801d85f KB |
39 | // Define a new frame type |
40 | class MyFrame: public wxFrame | |
41 | { public: | |
42 | wxGrid *grid; | |
43 | MyFrame(wxFrame *frame, const wxString& title, const wxPoint& pos, const wxSize& size); | |
44 | ||
c801d85f | 45 | void ToggleEditable(wxCommandEvent& event); |
c0b042fc | 46 | void ToggleEditInPlace(wxCommandEvent& event); |
c801d85f KB |
47 | void ToggleRowLabel(wxCommandEvent& event); |
48 | void ToggleColLabel(wxCommandEvent& event); | |
49 | void ToggleDividers(wxCommandEvent& event); | |
50 | void LeftCell(wxCommandEvent& event); | |
51 | void CentreCell(wxCommandEvent& event); | |
52 | void RightCell(wxCommandEvent& event); | |
53 | void ColourLabelBackground(wxCommandEvent& event); | |
54 | void ColourLabelText(wxCommandEvent& event); | |
55 | void NormalLabelColouring(wxCommandEvent& event); | |
56 | void ColourCellBackground(wxCommandEvent& event); | |
57 | void ColourCellText(wxCommandEvent& event); | |
58 | void NormalCellColouring(wxCommandEvent& event); | |
59 | void Quit(wxCommandEvent& event); | |
60 | ||
61 | void OnActivate(wxActivateEvent& event); | |
62 | ||
63 | DECLARE_EVENT_TABLE() | |
64 | }; | |
65 | ||
c67daf87 UR |
66 | wxBitmap *cellBitmap1 = (wxBitmap *) NULL; |
67 | wxBitmap *cellBitmap2 = (wxBitmap *) NULL; | |
c801d85f KB |
68 | |
69 | // ID for the menu quit command | |
70 | #define GRID_QUIT 1 | |
71 | #define GRID_TOGGLE_EDITABLE 2 | |
c0b042fc | 72 | #define GRID_TOGGLE_EDITINPLACE 22 |
c801d85f KB |
73 | #define GRID_LEFT_CELL 3 |
74 | #define GRID_CENTRE_CELL 4 | |
75 | #define GRID_RIGHT_CELL 5 | |
76 | #define GRID_TOGGLE_ROW_LABEL 6 | |
77 | #define GRID_TOGGLE_COL_LABEL 7 | |
78 | #define GRID_COLOUR_LABEL_BACKGROUND 8 | |
79 | #define GRID_COLOUR_LABEL_TEXT 9 | |
80 | #define GRID_NORMAL_LABEL_COLOURING 10 | |
81 | #define GRID_COLOUR_CELL_BACKGROUND 11 | |
82 | #define GRID_COLOUR_CELL_TEXT 12 | |
83 | #define GRID_NORMAL_CELL_COLOURING 13 | |
84 | #define GRID_TOGGLE_DIVIDERS 14 | |
85 | ||
86 | // Main proc | |
87 | ||
88 | IMPLEMENT_APP(MyApp) | |
89 | ||
90 | // `Main program' equivalent, creating windows and returning main app frame | |
91 | bool MyApp::OnInit(void) | |
92 | { | |
2049ba38 | 93 | #ifdef __WXMSW__ |
ab1ca7b3 MB |
94 | cellBitmap1 = new wxBitmap(_T("bitmap1")); |
95 | cellBitmap2 = new wxBitmap(_T("bitmap2")); | |
c801d85f KB |
96 | #endif |
97 | ||
2f6c54eb | 98 | // Create the main frame window |
ab1ca7b3 | 99 | MyFrame *frame = new MyFrame(NULL, _T("wxGrid Sample"), wxPoint(50, 50), wxSize(450, 300)); |
2f6c54eb VZ |
100 | |
101 | // Give it an icon | |
2049ba38 | 102 | #ifdef __WXMSW__ |
ab1ca7b3 | 103 | frame->SetIcon(wxIcon(_T("mondrian"))); |
c801d85f | 104 | #endif |
c801d85f | 105 | |
2f6c54eb VZ |
106 | // Make a menubar |
107 | wxMenu *file_menu = new wxMenu; | |
ab1ca7b3 | 108 | file_menu->Append(GRID_QUIT, _T("E&xit")); |
2f6c54eb VZ |
109 | |
110 | wxMenu *settings_menu = new wxMenu; | |
ab1ca7b3 MB |
111 | settings_menu->Append(GRID_TOGGLE_EDITABLE, _T("&Toggle editable")); |
112 | settings_menu->Append(GRID_TOGGLE_EDITINPLACE, _T("&Toggle edit in place")); | |
113 | settings_menu->Append(GRID_TOGGLE_ROW_LABEL, _T("Toggle ro&w label")); | |
114 | settings_menu->Append(GRID_TOGGLE_COL_LABEL, _T("Toggle co&l label")); | |
115 | settings_menu->Append(GRID_TOGGLE_DIVIDERS, _T("Toggle ÷rs")); | |
2f6c54eb | 116 | settings_menu->AppendSeparator(); |
ab1ca7b3 MB |
117 | settings_menu->Append(GRID_LEFT_CELL, _T("&Left cell alignment ")); |
118 | settings_menu->Append(GRID_CENTRE_CELL, _T("&Centre cell alignment ")); | |
119 | settings_menu->Append(GRID_RIGHT_CELL, _T("&Right cell alignment ")); | |
2f6c54eb | 120 | settings_menu->AppendSeparator(); |
ab1ca7b3 MB |
121 | settings_menu->Append(GRID_COLOUR_LABEL_BACKGROUND, _T("Choose a label &background colour")); |
122 | settings_menu->Append(GRID_COLOUR_LABEL_TEXT, _T("Choose a label fore&ground colour")); | |
123 | settings_menu->Append(GRID_NORMAL_LABEL_COLOURING, _T("&Normal label colouring")); | |
2f6c54eb | 124 | settings_menu->AppendSeparator(); |
ab1ca7b3 MB |
125 | settings_menu->Append(GRID_COLOUR_CELL_BACKGROUND, _T("Choo&se a cell &background colour")); |
126 | settings_menu->Append(GRID_COLOUR_CELL_TEXT, _T("Choose &a cell foreground colour")); | |
127 | settings_menu->Append(GRID_NORMAL_CELL_COLOURING, _T("N&ormal cell colouring")); | |
2f6c54eb VZ |
128 | |
129 | wxMenuBar *menu_bar = new wxMenuBar; | |
ab1ca7b3 MB |
130 | menu_bar->Append(file_menu, _T("&File")); |
131 | menu_bar->Append(settings_menu, _T("&Settings")); | |
2f6c54eb VZ |
132 | frame->SetMenuBar(menu_bar); |
133 | ||
134 | // Make a grid | |
135 | frame->grid = new wxGrid(frame, 0, 0, 400, 400); | |
136 | ||
137 | frame->grid->CreateGrid(10, 8); | |
138 | frame->grid->SetColumnWidth(3, 200); | |
139 | frame->grid->SetRowHeight(4, 45); | |
ab1ca7b3 MB |
140 | frame->grid->SetCellValue(_T("First cell"), 0, 0); |
141 | frame->grid->SetCellValue(_T("Another cell"), 1, 1); | |
142 | frame->grid->SetCellValue(_T("Yet another cell"), 2, 2); | |
2f6c54eb VZ |
143 | frame->grid->SetCellTextFont(wxFont(10, wxROMAN, wxITALIC, wxNORMAL), 0, 0); |
144 | frame->grid->SetCellTextColour(*wxRED, 1, 1); | |
145 | frame->grid->SetCellBackgroundColour(*wxCYAN, 2, 2); | |
146 | if (cellBitmap1 && cellBitmap2) | |
147 | { | |
148 | frame->grid->SetCellAlignment(wxCENTRE, 5, 0); | |
149 | frame->grid->SetCellAlignment(wxCENTRE, 6, 0); | |
150 | frame->grid->SetCellBitmap(cellBitmap1, 5, 0); | |
151 | frame->grid->SetCellBitmap(cellBitmap2, 6, 0); | |
152 | } | |
153 | ||
154 | frame->grid->UpdateDimensions(); | |
155 | ||
156 | // Show the frame | |
157 | frame->Show(TRUE); | |
158 | ||
17e74315 JS |
159 | wxMessageBox(wxT("Please note: this is an obsolete sample using the old wxGrid API.\nPlease compile newgrid instead."), wxT("wxGrid"), wxICON_INFORMATION|wxOK, frame); |
160 | ||
2f6c54eb VZ |
161 | SetTopWindow(frame); |
162 | return TRUE; | |
163 | } | |
164 | ||
165 | int MyApp::OnExit() | |
166 | { | |
167 | if (cellBitmap1) | |
168 | { | |
169 | delete cellBitmap1; | |
170 | cellBitmap1 = (wxBitmap *) NULL; | |
171 | } | |
172 | ||
173 | if (cellBitmap2) | |
174 | { | |
175 | delete cellBitmap2; | |
176 | cellBitmap1 = (wxBitmap *) NULL; | |
177 | } | |
178 | ||
179 | // exit code is 0, everything is ok | |
180 | return 0; | |
c801d85f KB |
181 | } |
182 | ||
2f6c54eb | 183 | |
c801d85f | 184 | // My frame constructor |
2f6c54eb VZ |
185 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, |
186 | const wxPoint& pos, const wxSize& size): | |
187 | wxFrame(frame, -1, title, pos, size) | |
c801d85f | 188 | { |
2f6c54eb | 189 | grid = (wxGrid*) NULL; |
c801d85f KB |
190 | } |
191 | ||
192 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
2f6c54eb VZ |
193 | EVT_MENU(GRID_TOGGLE_EDITABLE, MyFrame::ToggleEditable) |
194 | EVT_MENU(GRID_TOGGLE_EDITINPLACE, MyFrame::ToggleEditInPlace) | |
195 | EVT_MENU(GRID_TOGGLE_ROW_LABEL, MyFrame::ToggleRowLabel) | |
196 | EVT_MENU(GRID_TOGGLE_COL_LABEL, MyFrame::ToggleColLabel) | |
197 | EVT_MENU(GRID_TOGGLE_DIVIDERS, MyFrame::ToggleDividers) | |
198 | EVT_MENU(GRID_LEFT_CELL, MyFrame::LeftCell) | |
199 | EVT_MENU(GRID_CENTRE_CELL, MyFrame::CentreCell) | |
200 | EVT_MENU(GRID_RIGHT_CELL, MyFrame::RightCell) | |
201 | EVT_MENU(GRID_COLOUR_LABEL_BACKGROUND, MyFrame::ColourLabelBackground) | |
202 | EVT_MENU(GRID_COLOUR_LABEL_TEXT, MyFrame::ColourLabelText) | |
203 | EVT_MENU(GRID_NORMAL_LABEL_COLOURING, MyFrame::NormalLabelColouring) | |
204 | EVT_MENU(GRID_COLOUR_CELL_BACKGROUND, MyFrame::ColourCellBackground) | |
205 | EVT_MENU(GRID_COLOUR_CELL_TEXT, MyFrame::ColourCellText) | |
206 | EVT_MENU(GRID_NORMAL_CELL_COLOURING, MyFrame::NormalCellColouring) | |
207 | EVT_MENU(GRID_QUIT, MyFrame::Quit) | |
c801d85f KB |
208 | END_EVENT_TABLE() |
209 | ||
bd7d06f2 | 210 | void MyFrame::ToggleEditable(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 211 | { |
2f6c54eb VZ |
212 | grid->SetEditable(!grid->GetEditable()); |
213 | grid->Refresh(); | |
c801d85f KB |
214 | } |
215 | ||
c0b042fc VZ |
216 | void MyFrame::ToggleEditInPlace(wxCommandEvent& WXUNUSED(event)) |
217 | { | |
2f6c54eb VZ |
218 | grid->SetEditInPlace(!grid->GetEditInPlace()); |
219 | grid->Refresh(); | |
c0b042fc VZ |
220 | } |
221 | ||
bd7d06f2 | 222 | void MyFrame::ToggleRowLabel(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 223 | { |
2f6c54eb | 224 | if (grid->GetLabelSize(wxVERTICAL) > 0) |
c801d85f | 225 | grid->SetLabelSize(wxVERTICAL, 0); |
2f6c54eb | 226 | else |
c801d85f | 227 | grid->SetLabelSize(wxVERTICAL, 40); |
2f6c54eb VZ |
228 | |
229 | grid->Refresh(); | |
c801d85f KB |
230 | } |
231 | ||
bd7d06f2 | 232 | void MyFrame::ToggleColLabel(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 233 | { |
2f6c54eb | 234 | if (grid->GetLabelSize(wxHORIZONTAL) > 0) |
c801d85f | 235 | grid->SetLabelSize(wxHORIZONTAL, 0); |
2f6c54eb | 236 | else |
c801d85f | 237 | grid->SetLabelSize(wxHORIZONTAL, 20); |
2f6c54eb | 238 | |
c801d85f KB |
239 | grid->Refresh(); |
240 | } | |
241 | ||
bd7d06f2 | 242 | void MyFrame::ToggleDividers(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 243 | { |
2f6c54eb | 244 | if (!grid->GetDividerPen().Ok()) |
a60b1f5d | 245 | grid->SetDividerPen(wxPen(wxT("LIGHT GREY"), 1, wxSOLID)); |
2f6c54eb | 246 | else |
c0ed460c | 247 | grid->SetDividerPen(wxNullPen); |
2f6c54eb | 248 | |
17e74315 | 249 | grid->Refresh(); |
c801d85f KB |
250 | } |
251 | ||
bd7d06f2 | 252 | void MyFrame::LeftCell(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 253 | { |
2f6c54eb VZ |
254 | grid->SetCellAlignment(wxLEFT); |
255 | grid->Refresh(); | |
c801d85f KB |
256 | } |
257 | ||
bd7d06f2 | 258 | void MyFrame::CentreCell(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 259 | { |
2f6c54eb VZ |
260 | grid->SetCellAlignment(wxCENTRE); |
261 | grid->Refresh(); | |
c801d85f KB |
262 | } |
263 | ||
bd7d06f2 | 264 | void MyFrame::RightCell(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 265 | { |
2f6c54eb VZ |
266 | grid->SetCellAlignment(wxRIGHT); |
267 | grid->Refresh(); | |
c801d85f KB |
268 | } |
269 | ||
bd7d06f2 | 270 | void MyFrame::ColourLabelBackground(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 271 | { |
2f6c54eb VZ |
272 | wxColourData data; |
273 | data.SetChooseFull(TRUE); | |
274 | wxColourDialog dialog(this, &data); | |
275 | if (dialog.ShowModal() != wxID_CANCEL) | |
276 | { | |
c801d85f KB |
277 | wxColourData retData = dialog.GetColourData(); |
278 | wxColour col = retData.GetColour(); | |
279 | grid->SetLabelBackgroundColour(col); | |
280 | grid->Refresh(); | |
2f6c54eb | 281 | } |
c801d85f KB |
282 | } |
283 | ||
bd7d06f2 | 284 | void MyFrame::ColourLabelText(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 285 | { |
2f6c54eb VZ |
286 | wxColourData data; |
287 | data.SetChooseFull(TRUE); | |
288 | wxColourDialog dialog(this, &data); | |
289 | if (dialog.ShowModal() != wxID_CANCEL) | |
290 | { | |
c801d85f KB |
291 | wxColourData retData = dialog.GetColourData(); |
292 | wxColour col = retData.GetColour(); | |
293 | grid->SetLabelTextColour(col); | |
294 | grid->Refresh(); | |
2f6c54eb | 295 | } |
c801d85f KB |
296 | } |
297 | ||
bd7d06f2 | 298 | void MyFrame::NormalLabelColouring(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 299 | { |
2f6c54eb VZ |
300 | grid->SetLabelBackgroundColour(*wxLIGHT_GREY); |
301 | grid->SetLabelTextColour(*wxBLACK); | |
302 | grid->Refresh(); | |
c801d85f KB |
303 | } |
304 | ||
bd7d06f2 | 305 | void MyFrame::ColourCellBackground(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 306 | { |
2f6c54eb VZ |
307 | wxColourData data; |
308 | data.SetChooseFull(TRUE); | |
309 | wxColourDialog dialog(this, &data); | |
310 | if (dialog.ShowModal() != wxID_CANCEL) | |
311 | { | |
c801d85f KB |
312 | wxColourData retData = dialog.GetColourData(); |
313 | wxColour col = retData.GetColour(); | |
314 | grid->SetCellBackgroundColour(col); | |
315 | grid->Refresh(); | |
2f6c54eb | 316 | } |
c801d85f KB |
317 | } |
318 | ||
bd7d06f2 | 319 | void MyFrame::ColourCellText(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 320 | { |
2f6c54eb VZ |
321 | wxColourData data; |
322 | data.SetChooseFull(TRUE); | |
323 | wxColourDialog dialog(this, &data); | |
324 | if (dialog.ShowModal() != wxID_CANCEL) | |
325 | { | |
c801d85f KB |
326 | wxColourData retData = dialog.GetColourData(); |
327 | wxColour col = retData.GetColour(); | |
328 | grid->SetCellTextColour(col); | |
329 | grid->Refresh(); | |
2f6c54eb | 330 | } |
c801d85f KB |
331 | } |
332 | ||
bd7d06f2 | 333 | void MyFrame::NormalCellColouring(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 334 | { |
2f6c54eb VZ |
335 | grid->SetCellBackgroundColour(*wxWHITE); |
336 | grid->SetCellTextColour(*wxBLACK); | |
337 | grid->Refresh(); | |
c801d85f KB |
338 | } |
339 | ||
bd7d06f2 | 340 | void MyFrame::Quit(wxCommandEvent& WXUNUSED(event)) |
c801d85f | 341 | { |
2f6c54eb | 342 | this->Close(TRUE); |
c801d85f KB |
343 | } |
344 | ||
345 | // Ensure that the grid's edit control always has the focus. | |
346 | void MyFrame::OnActivate(wxActivateEvent& event) | |
347 | { | |
2f6c54eb | 348 | if (grid) grid->OnActivate(event.GetActive()); |
c801d85f KB |
349 | } |
350 |