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