7 * Copyright: (c) 1995, AIAI, University of Edinburgh
10 static const char sccsid
[] = "%W% %G%";
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/colordlg.h"
26 // Define a new application type
27 class MyApp
: public wxApp
30 virtual bool OnInit(void);
35 // Define a new frame type
36 class MyFrame
: public wxFrame
39 MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
41 void ToggleEditable(wxCommandEvent
& event
);
42 void ToggleEditInPlace(wxCommandEvent
& event
);
43 void ToggleRowLabel(wxCommandEvent
& event
);
44 void ToggleColLabel(wxCommandEvent
& event
);
45 void ToggleDividers(wxCommandEvent
& event
);
46 void LeftCell(wxCommandEvent
& event
);
47 void CentreCell(wxCommandEvent
& event
);
48 void RightCell(wxCommandEvent
& event
);
49 void ColourLabelBackground(wxCommandEvent
& event
);
50 void ColourLabelText(wxCommandEvent
& event
);
51 void NormalLabelColouring(wxCommandEvent
& event
);
52 void ColourCellBackground(wxCommandEvent
& event
);
53 void ColourCellText(wxCommandEvent
& event
);
54 void NormalCellColouring(wxCommandEvent
& event
);
55 void Quit(wxCommandEvent
& event
);
57 void OnActivate(wxActivateEvent
& event
);
62 wxBitmap
*cellBitmap1
= (wxBitmap
*) NULL
;
63 wxBitmap
*cellBitmap2
= (wxBitmap
*) NULL
;
65 // ID for the menu quit command
67 #define GRID_TOGGLE_EDITABLE 2
68 #define GRID_TOGGLE_EDITINPLACE 22
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
86 // `Main program' equivalent, creating windows and returning main app frame
87 bool MyApp::OnInit(void)
90 cellBitmap1
= new wxBitmap(_T("bitmap1"));
91 cellBitmap2
= new wxBitmap(_T("bitmap2"));
94 // Create the main frame window
95 MyFrame
*frame
= new MyFrame(NULL
, _T("wxGrid Sample"), wxPoint(50, 50), wxSize(450, 300));
99 frame
->SetIcon(wxIcon(_T("mondrian")));
103 wxMenu
*file_menu
= new wxMenu
;
104 file_menu
->Append(GRID_QUIT
, _T("E&xit"));
106 wxMenu
*settings_menu
= new wxMenu
;
107 settings_menu
->Append(GRID_TOGGLE_EDITABLE
, _T("&Toggle editable"));
108 settings_menu
->Append(GRID_TOGGLE_EDITINPLACE
, _T("&Toggle edit in place"));
109 settings_menu
->Append(GRID_TOGGLE_ROW_LABEL
, _T("Toggle ro&w label"));
110 settings_menu
->Append(GRID_TOGGLE_COL_LABEL
, _T("Toggle co&l label"));
111 settings_menu
->Append(GRID_TOGGLE_DIVIDERS
, _T("Toggle ÷rs"));
112 settings_menu
->AppendSeparator();
113 settings_menu
->Append(GRID_LEFT_CELL
, _T("&Left cell alignment "));
114 settings_menu
->Append(GRID_CENTRE_CELL
, _T("&Centre cell alignment "));
115 settings_menu
->Append(GRID_RIGHT_CELL
, _T("&Right cell alignment "));
116 settings_menu
->AppendSeparator();
117 settings_menu
->Append(GRID_COLOUR_LABEL_BACKGROUND
, _T("Choose a label &background colour"));
118 settings_menu
->Append(GRID_COLOUR_LABEL_TEXT
, _T("Choose a label fore&ground colour"));
119 settings_menu
->Append(GRID_NORMAL_LABEL_COLOURING
, _T("&Normal label colouring"));
120 settings_menu
->AppendSeparator();
121 settings_menu
->Append(GRID_COLOUR_CELL_BACKGROUND
, _T("Choo&se a cell &background colour"));
122 settings_menu
->Append(GRID_COLOUR_CELL_TEXT
, _T("Choose &a cell foreground colour"));
123 settings_menu
->Append(GRID_NORMAL_CELL_COLOURING
, _T("N&ormal cell colouring"));
125 wxMenuBar
*menu_bar
= new wxMenuBar
;
126 menu_bar
->Append(file_menu
, _T("&File"));
127 menu_bar
->Append(settings_menu
, _T("&Settings"));
128 frame
->SetMenuBar(menu_bar
);
131 frame
->grid
= new wxGrid(frame
, 0, 0, 400, 400);
133 frame
->grid
->CreateGrid(10, 8);
134 frame
->grid
->SetColumnWidth(3, 200);
135 frame
->grid
->SetRowHeight(4, 45);
136 frame
->grid
->SetCellValue(_T("First cell"), 0, 0);
137 frame
->grid
->SetCellValue(_T("Another cell"), 1, 1);
138 frame
->grid
->SetCellValue(_T("Yet another cell"), 2, 2);
139 frame
->grid
->SetCellTextFont(wxFont(10, wxROMAN
, wxITALIC
, wxNORMAL
), 0, 0);
140 frame
->grid
->SetCellTextColour(*wxRED
, 1, 1);
141 frame
->grid
->SetCellBackgroundColour(*wxCYAN
, 2, 2);
142 if (cellBitmap1
&& cellBitmap2
)
144 frame
->grid
->SetCellAlignment(wxCENTRE
, 5, 0);
145 frame
->grid
->SetCellAlignment(wxCENTRE
, 6, 0);
146 frame
->grid
->SetCellBitmap(cellBitmap1
, 5, 0);
147 frame
->grid
->SetCellBitmap(cellBitmap2
, 6, 0);
150 frame
->grid
->UpdateDimensions();
164 cellBitmap1
= (wxBitmap
*) NULL
;
170 cellBitmap1
= (wxBitmap
*) NULL
;
173 // exit code is 0, everything is ok
178 // My frame constructor
179 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
,
180 const wxPoint
& pos
, const wxSize
& size
):
181 wxFrame(frame
, -1, title
, pos
, size
)
183 grid
= (wxGrid
*) NULL
;
186 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
187 EVT_MENU(GRID_TOGGLE_EDITABLE
, MyFrame::ToggleEditable
)
188 EVT_MENU(GRID_TOGGLE_EDITINPLACE
, MyFrame::ToggleEditInPlace
)
189 EVT_MENU(GRID_TOGGLE_ROW_LABEL
, MyFrame::ToggleRowLabel
)
190 EVT_MENU(GRID_TOGGLE_COL_LABEL
, MyFrame::ToggleColLabel
)
191 EVT_MENU(GRID_TOGGLE_DIVIDERS
, MyFrame::ToggleDividers
)
192 EVT_MENU(GRID_LEFT_CELL
, MyFrame::LeftCell
)
193 EVT_MENU(GRID_CENTRE_CELL
, MyFrame::CentreCell
)
194 EVT_MENU(GRID_RIGHT_CELL
, MyFrame::RightCell
)
195 EVT_MENU(GRID_COLOUR_LABEL_BACKGROUND
, MyFrame::ColourLabelBackground
)
196 EVT_MENU(GRID_COLOUR_LABEL_TEXT
, MyFrame::ColourLabelText
)
197 EVT_MENU(GRID_NORMAL_LABEL_COLOURING
, MyFrame::NormalLabelColouring
)
198 EVT_MENU(GRID_COLOUR_CELL_BACKGROUND
, MyFrame::ColourCellBackground
)
199 EVT_MENU(GRID_COLOUR_CELL_TEXT
, MyFrame::ColourCellText
)
200 EVT_MENU(GRID_NORMAL_CELL_COLOURING
, MyFrame::NormalCellColouring
)
201 EVT_MENU(GRID_QUIT
, MyFrame::Quit
)
204 void MyFrame::ToggleEditable(wxCommandEvent
& WXUNUSED(event
))
206 grid
->SetEditable(!grid
->GetEditable());
210 void MyFrame::ToggleEditInPlace(wxCommandEvent
& WXUNUSED(event
))
212 grid
->SetEditInPlace(!grid
->GetEditInPlace());
216 void MyFrame::ToggleRowLabel(wxCommandEvent
& WXUNUSED(event
))
218 if (grid
->GetLabelSize(wxVERTICAL
) > 0)
219 grid
->SetLabelSize(wxVERTICAL
, 0);
221 grid
->SetLabelSize(wxVERTICAL
, 40);
226 void MyFrame::ToggleColLabel(wxCommandEvent
& WXUNUSED(event
))
228 if (grid
->GetLabelSize(wxHORIZONTAL
) > 0)
229 grid
->SetLabelSize(wxHORIZONTAL
, 0);
231 grid
->SetLabelSize(wxHORIZONTAL
, 20);
236 void MyFrame::ToggleDividers(wxCommandEvent
& WXUNUSED(event
))
238 if (!grid
->GetDividerPen().Ok())
239 grid
->SetDividerPen(wxPen(wxT("LIGHT GREY"), 1, wxSOLID
));
241 grid
->SetDividerPen(wxNullPen
);
246 void MyFrame::LeftCell(wxCommandEvent
& WXUNUSED(event
))
248 grid
->SetCellAlignment(wxLEFT
);
252 void MyFrame::CentreCell(wxCommandEvent
& WXUNUSED(event
))
254 grid
->SetCellAlignment(wxCENTRE
);
258 void MyFrame::RightCell(wxCommandEvent
& WXUNUSED(event
))
260 grid
->SetCellAlignment(wxRIGHT
);
264 void MyFrame::ColourLabelBackground(wxCommandEvent
& WXUNUSED(event
))
267 data
.SetChooseFull(TRUE
);
268 wxColourDialog
dialog(this, &data
);
269 if (dialog
.ShowModal() != wxID_CANCEL
)
271 wxColourData retData
= dialog
.GetColourData();
272 wxColour col
= retData
.GetColour();
273 grid
->SetLabelBackgroundColour(col
);
278 void MyFrame::ColourLabelText(wxCommandEvent
& WXUNUSED(event
))
281 data
.SetChooseFull(TRUE
);
282 wxColourDialog
dialog(this, &data
);
283 if (dialog
.ShowModal() != wxID_CANCEL
)
285 wxColourData retData
= dialog
.GetColourData();
286 wxColour col
= retData
.GetColour();
287 grid
->SetLabelTextColour(col
);
292 void MyFrame::NormalLabelColouring(wxCommandEvent
& WXUNUSED(event
))
294 grid
->SetLabelBackgroundColour(*wxLIGHT_GREY
);
295 grid
->SetLabelTextColour(*wxBLACK
);
299 void MyFrame::ColourCellBackground(wxCommandEvent
& WXUNUSED(event
))
302 data
.SetChooseFull(TRUE
);
303 wxColourDialog
dialog(this, &data
);
304 if (dialog
.ShowModal() != wxID_CANCEL
)
306 wxColourData retData
= dialog
.GetColourData();
307 wxColour col
= retData
.GetColour();
308 grid
->SetCellBackgroundColour(col
);
313 void MyFrame::ColourCellText(wxCommandEvent
& WXUNUSED(event
))
316 data
.SetChooseFull(TRUE
);
317 wxColourDialog
dialog(this, &data
);
318 if (dialog
.ShowModal() != wxID_CANCEL
)
320 wxColourData retData
= dialog
.GetColourData();
321 wxColour col
= retData
.GetColour();
322 grid
->SetCellTextColour(col
);
327 void MyFrame::NormalCellColouring(wxCommandEvent
& WXUNUSED(event
))
329 grid
->SetCellBackgroundColour(*wxWHITE
);
330 grid
->SetCellTextColour(*wxBLACK
);
334 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
))
339 // Ensure that the grid's edit control always has the focus.
340 void MyFrame::OnActivate(wxActivateEvent
& event
)
342 if (grid
) grid
->OnActivate(event
.GetActive());