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