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