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