5 PLEASE NOTE: this sample is deprecated. See
6 newgrid for a sample based on the newer wxGrid API.
11 * Copyright: (c) 1995, AIAI, University of Edinburgh
14 static const char sccsid
[] = "%W% %G%";
16 // For compilers that support precompilation, includes "wx/wx.h".
17 #include "wx/wxprec.h"
28 #include "wx/colordlg.h"
30 // Define a new application type
31 class MyApp
: public wxApp
34 virtual bool OnInit(void);
39 // Define a new frame type
40 class MyFrame
: public wxFrame
43 MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
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
);
61 void OnActivate(wxActivateEvent
& event
);
66 wxBitmap
*cellBitmap1
= (wxBitmap
*) NULL
;
67 wxBitmap
*cellBitmap2
= (wxBitmap
*) NULL
;
69 // ID for the menu quit command
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
90 // `Main program' equivalent, creating windows and returning main app frame
91 bool MyApp::OnInit(void)
94 cellBitmap1
= new wxBitmap(_T("bitmap1"));
95 cellBitmap2
= new wxBitmap(_T("bitmap2"));
98 // Create the main frame window
99 MyFrame
*frame
= new MyFrame(NULL
, _T("wxGrid Sample"), wxPoint(50, 50), wxSize(450, 300));
103 frame
->SetIcon(wxIcon(_T("mondrian")));
107 wxMenu
*file_menu
= new wxMenu
;
108 file_menu
->Append(GRID_QUIT
, _T("E&xit"));
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 ÷rs"));
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"));
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
);
135 frame
->grid
= new wxGrid(frame
, 0, 0, 400, 400);
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
)
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);
154 frame
->grid
->UpdateDimensions();
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
);
170 cellBitmap1
= (wxBitmap
*) NULL
;
176 cellBitmap1
= (wxBitmap
*) NULL
;
179 // exit code is 0, everything is ok
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
)
189 grid
= (wxGrid
*) NULL
;
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
)
210 void MyFrame::ToggleEditable(wxCommandEvent
& WXUNUSED(event
))
212 grid
->SetEditable(!grid
->GetEditable());
216 void MyFrame::ToggleEditInPlace(wxCommandEvent
& WXUNUSED(event
))
218 grid
->SetEditInPlace(!grid
->GetEditInPlace());
222 void MyFrame::ToggleRowLabel(wxCommandEvent
& WXUNUSED(event
))
224 if (grid
->GetLabelSize(wxVERTICAL
) > 0)
225 grid
->SetLabelSize(wxVERTICAL
, 0);
227 grid
->SetLabelSize(wxVERTICAL
, 40);
232 void MyFrame::ToggleColLabel(wxCommandEvent
& WXUNUSED(event
))
234 if (grid
->GetLabelSize(wxHORIZONTAL
) > 0)
235 grid
->SetLabelSize(wxHORIZONTAL
, 0);
237 grid
->SetLabelSize(wxHORIZONTAL
, 20);
242 void MyFrame::ToggleDividers(wxCommandEvent
& WXUNUSED(event
))
244 if (!grid
->GetDividerPen().Ok())
245 grid
->SetDividerPen(wxPen(wxT("LIGHT GREY"), 1, wxSOLID
));
247 grid
->SetDividerPen(wxNullPen
);
252 void MyFrame::LeftCell(wxCommandEvent
& WXUNUSED(event
))
254 grid
->SetCellAlignment(wxLEFT
);
258 void MyFrame::CentreCell(wxCommandEvent
& WXUNUSED(event
))
260 grid
->SetCellAlignment(wxCENTRE
);
264 void MyFrame::RightCell(wxCommandEvent
& WXUNUSED(event
))
266 grid
->SetCellAlignment(wxRIGHT
);
270 void MyFrame::ColourLabelBackground(wxCommandEvent
& WXUNUSED(event
))
273 data
.SetChooseFull(TRUE
);
274 wxColourDialog
dialog(this, &data
);
275 if (dialog
.ShowModal() != wxID_CANCEL
)
277 wxColourData retData
= dialog
.GetColourData();
278 wxColour col
= retData
.GetColour();
279 grid
->SetLabelBackgroundColour(col
);
284 void MyFrame::ColourLabelText(wxCommandEvent
& WXUNUSED(event
))
287 data
.SetChooseFull(TRUE
);
288 wxColourDialog
dialog(this, &data
);
289 if (dialog
.ShowModal() != wxID_CANCEL
)
291 wxColourData retData
= dialog
.GetColourData();
292 wxColour col
= retData
.GetColour();
293 grid
->SetLabelTextColour(col
);
298 void MyFrame::NormalLabelColouring(wxCommandEvent
& WXUNUSED(event
))
300 grid
->SetLabelBackgroundColour(*wxLIGHT_GREY
);
301 grid
->SetLabelTextColour(*wxBLACK
);
305 void MyFrame::ColourCellBackground(wxCommandEvent
& WXUNUSED(event
))
308 data
.SetChooseFull(TRUE
);
309 wxColourDialog
dialog(this, &data
);
310 if (dialog
.ShowModal() != wxID_CANCEL
)
312 wxColourData retData
= dialog
.GetColourData();
313 wxColour col
= retData
.GetColour();
314 grid
->SetCellBackgroundColour(col
);
319 void MyFrame::ColourCellText(wxCommandEvent
& WXUNUSED(event
))
322 data
.SetChooseFull(TRUE
);
323 wxColourDialog
dialog(this, &data
);
324 if (dialog
.ShowModal() != wxID_CANCEL
)
326 wxColourData retData
= dialog
.GetColourData();
327 wxColour col
= retData
.GetColour();
328 grid
->SetCellTextColour(col
);
333 void MyFrame::NormalCellColouring(wxCommandEvent
& WXUNUSED(event
))
335 grid
->SetCellBackgroundColour(*wxWHITE
);
336 grid
->SetCellTextColour(*wxBLACK
);
340 void MyFrame::Quit(wxCommandEvent
& WXUNUSED(event
))
345 // Ensure that the grid's edit control always has the focus.
346 void MyFrame::OnActivate(wxActivateEvent
& event
)
348 if (grid
) grid
->OnActivate(event
.GetActive());