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