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