]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * File: grid.cpp | |
3 | * Purpose: wxGrid test | |
4 | * Author: Julian Smart | |
5 | * Created: 1995 | |
6 | * Updated: | |
7 | * Copyright: (c) 1995, AIAI, University of Edinburgh | |
8 | */ | |
9 | ||
10 | static const char sccsid[] = "%W% %G%"; | |
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 | #include "wx/grid.h" | |
24 | #include "wx/colordlg.h" | |
25 | ||
26 | // Define a new application type | |
27 | class MyApp: public wxApp | |
28 | { | |
29 | public: | |
30 | virtual bool OnInit(void); | |
31 | virtual int OnExit(); | |
32 | }; | |
33 | ||
34 | ||
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 | ||
41 | void ToggleEditable(wxCommandEvent& event); | |
42 | void ToggleEditInPlace(wxCommandEvent& event); | |
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 | ||
62 | wxBitmap *cellBitmap1 = (wxBitmap *) NULL; | |
63 | wxBitmap *cellBitmap2 = (wxBitmap *) NULL; | |
64 | ||
65 | // ID for the menu quit command | |
66 | #define GRID_QUIT 1 | |
67 | #define GRID_TOGGLE_EDITABLE 2 | |
68 | #define GRID_TOGGLE_EDITINPLACE 22 | |
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 | { | |
89 | #ifdef __WXMSW__ | |
90 | cellBitmap1 = new wxBitmap("bitmap1"); | |
91 | cellBitmap2 = new wxBitmap("bitmap2"); | |
92 | #endif | |
93 | ||
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 | |
98 | #ifdef __WXMSW__ | |
99 | frame->SetIcon(wxIcon("mondrian")); | |
100 | #endif | |
101 | ||
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; | |
175 | } | |
176 | ||
177 | ||
178 | // My frame constructor | |
179 | MyFrame::MyFrame(wxFrame *frame, const wxString& title, | |
180 | const wxPoint& pos, const wxSize& size): | |
181 | wxFrame(frame, -1, title, pos, size) | |
182 | { | |
183 | grid = (wxGrid*) NULL; | |
184 | } | |
185 | ||
186 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
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) | |
202 | END_EVENT_TABLE() | |
203 | ||
204 | void MyFrame::ToggleEditable(wxCommandEvent& WXUNUSED(event)) | |
205 | { | |
206 | grid->SetEditable(!grid->GetEditable()); | |
207 | grid->Refresh(); | |
208 | } | |
209 | ||
210 | void MyFrame::ToggleEditInPlace(wxCommandEvent& WXUNUSED(event)) | |
211 | { | |
212 | grid->SetEditInPlace(!grid->GetEditInPlace()); | |
213 | grid->Refresh(); | |
214 | } | |
215 | ||
216 | void MyFrame::ToggleRowLabel(wxCommandEvent& WXUNUSED(event)) | |
217 | { | |
218 | if (grid->GetLabelSize(wxVERTICAL) > 0) | |
219 | grid->SetLabelSize(wxVERTICAL, 0); | |
220 | else | |
221 | grid->SetLabelSize(wxVERTICAL, 40); | |
222 | ||
223 | grid->Refresh(); | |
224 | } | |
225 | ||
226 | void MyFrame::ToggleColLabel(wxCommandEvent& WXUNUSED(event)) | |
227 | { | |
228 | if (grid->GetLabelSize(wxHORIZONTAL) > 0) | |
229 | grid->SetLabelSize(wxHORIZONTAL, 0); | |
230 | else | |
231 | grid->SetLabelSize(wxHORIZONTAL, 20); | |
232 | ||
233 | grid->Refresh(); | |
234 | } | |
235 | ||
236 | void MyFrame::ToggleDividers(wxCommandEvent& WXUNUSED(event)) | |
237 | { | |
238 | if (!grid->GetDividerPen().Ok()) | |
239 | grid->SetDividerPen(wxPen(wxT("LIGHT GREY"), 1, wxSOLID)); | |
240 | else | |
241 | grid->SetDividerPen(wxNullPen); | |
242 | ||
243 | grid->Refresh(); | |
244 | } | |
245 | ||
246 | void MyFrame::LeftCell(wxCommandEvent& WXUNUSED(event)) | |
247 | { | |
248 | grid->SetCellAlignment(wxLEFT); | |
249 | grid->Refresh(); | |
250 | } | |
251 | ||
252 | void MyFrame::CentreCell(wxCommandEvent& WXUNUSED(event)) | |
253 | { | |
254 | grid->SetCellAlignment(wxCENTRE); | |
255 | grid->Refresh(); | |
256 | } | |
257 | ||
258 | void MyFrame::RightCell(wxCommandEvent& WXUNUSED(event)) | |
259 | { | |
260 | grid->SetCellAlignment(wxRIGHT); | |
261 | grid->Refresh(); | |
262 | } | |
263 | ||
264 | void MyFrame::ColourLabelBackground(wxCommandEvent& WXUNUSED(event)) | |
265 | { | |
266 | wxColourData data; | |
267 | data.SetChooseFull(TRUE); | |
268 | wxColourDialog dialog(this, &data); | |
269 | if (dialog.ShowModal() != wxID_CANCEL) | |
270 | { | |
271 | wxColourData retData = dialog.GetColourData(); | |
272 | wxColour col = retData.GetColour(); | |
273 | grid->SetLabelBackgroundColour(col); | |
274 | grid->Refresh(); | |
275 | } | |
276 | } | |
277 | ||
278 | void MyFrame::ColourLabelText(wxCommandEvent& WXUNUSED(event)) | |
279 | { | |
280 | wxColourData data; | |
281 | data.SetChooseFull(TRUE); | |
282 | wxColourDialog dialog(this, &data); | |
283 | if (dialog.ShowModal() != wxID_CANCEL) | |
284 | { | |
285 | wxColourData retData = dialog.GetColourData(); | |
286 | wxColour col = retData.GetColour(); | |
287 | grid->SetLabelTextColour(col); | |
288 | grid->Refresh(); | |
289 | } | |
290 | } | |
291 | ||
292 | void MyFrame::NormalLabelColouring(wxCommandEvent& WXUNUSED(event)) | |
293 | { | |
294 | grid->SetLabelBackgroundColour(*wxLIGHT_GREY); | |
295 | grid->SetLabelTextColour(*wxBLACK); | |
296 | grid->Refresh(); | |
297 | } | |
298 | ||
299 | void MyFrame::ColourCellBackground(wxCommandEvent& WXUNUSED(event)) | |
300 | { | |
301 | wxColourData data; | |
302 | data.SetChooseFull(TRUE); | |
303 | wxColourDialog dialog(this, &data); | |
304 | if (dialog.ShowModal() != wxID_CANCEL) | |
305 | { | |
306 | wxColourData retData = dialog.GetColourData(); | |
307 | wxColour col = retData.GetColour(); | |
308 | grid->SetCellBackgroundColour(col); | |
309 | grid->Refresh(); | |
310 | } | |
311 | } | |
312 | ||
313 | void MyFrame::ColourCellText(wxCommandEvent& WXUNUSED(event)) | |
314 | { | |
315 | wxColourData data; | |
316 | data.SetChooseFull(TRUE); | |
317 | wxColourDialog dialog(this, &data); | |
318 | if (dialog.ShowModal() != wxID_CANCEL) | |
319 | { | |
320 | wxColourData retData = dialog.GetColourData(); | |
321 | wxColour col = retData.GetColour(); | |
322 | grid->SetCellTextColour(col); | |
323 | grid->Refresh(); | |
324 | } | |
325 | } | |
326 | ||
327 | void MyFrame::NormalCellColouring(wxCommandEvent& WXUNUSED(event)) | |
328 | { | |
329 | grid->SetCellBackgroundColour(*wxWHITE); | |
330 | grid->SetCellTextColour(*wxBLACK); | |
331 | grid->Refresh(); | |
332 | } | |
333 | ||
334 | void MyFrame::Quit(wxCommandEvent& WXUNUSED(event)) | |
335 | { | |
336 | this->Close(TRUE); | |
337 | } | |
338 | ||
339 | // Ensure that the grid's edit control always has the focus. | |
340 | void MyFrame::OnActivate(wxActivateEvent& event) | |
341 | { | |
342 | if (grid) grid->OnActivate(event.GetActive()); | |
343 | } | |
344 |