7 * Copyright: (c) 1995, AIAI, University of Edinburgh
10 static const char sccsid
[] = "%W% %G%";
13 #pragma implementation
17 // For compilers that support precompilation, includes "wx/wx.h".
18 #include "wx/wxprec.h"
29 #include "wx/colordlg.h"
31 // Define a new application type
32 class MyApp
: public wxApp
37 // Define a new frame type
38 class MyFrame
: public wxFrame
41 MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
);
43 bool OnClose(void) { return TRUE
; }
45 void ToggleEditable(wxCommandEvent
& event
);
46 void ToggleRowLabel(wxCommandEvent
& event
);
47 void ToggleColLabel(wxCommandEvent
& event
);
48 void ToggleDividers(wxCommandEvent
& event
);
49 void LeftCell(wxCommandEvent
& event
);
50 void CentreCell(wxCommandEvent
& event
);
51 void RightCell(wxCommandEvent
& event
);
52 void ColourLabelBackground(wxCommandEvent
& event
);
53 void ColourLabelText(wxCommandEvent
& event
);
54 void NormalLabelColouring(wxCommandEvent
& event
);
55 void ColourCellBackground(wxCommandEvent
& event
);
56 void ColourCellText(wxCommandEvent
& event
);
57 void NormalCellColouring(wxCommandEvent
& event
);
58 void Quit(wxCommandEvent
& event
);
60 void OnActivate(wxActivateEvent
& event
);
65 wxBitmap
*cellBitmap1
= NULL
;
66 wxBitmap
*cellBitmap2
= NULL
;
68 // ID for the menu quit command
70 #define GRID_TOGGLE_EDITABLE 2
71 #define GRID_LEFT_CELL 3
72 #define GRID_CENTRE_CELL 4
73 #define GRID_RIGHT_CELL 5
74 #define GRID_TOGGLE_ROW_LABEL 6
75 #define GRID_TOGGLE_COL_LABEL 7
76 #define GRID_COLOUR_LABEL_BACKGROUND 8
77 #define GRID_COLOUR_LABEL_TEXT 9
78 #define GRID_NORMAL_LABEL_COLOURING 10
79 #define GRID_COLOUR_CELL_BACKGROUND 11
80 #define GRID_COLOUR_CELL_TEXT 12
81 #define GRID_NORMAL_CELL_COLOURING 13
82 #define GRID_TOGGLE_DIVIDERS 14
88 // `Main program' equivalent, creating windows and returning main app frame
89 bool MyApp::OnInit(void)
92 cellBitmap1
= new wxBitmap("bitmap1");
93 cellBitmap2
= new wxBitmap("bitmap2");
96 // Create the main frame window
97 MyFrame
*frame
= new MyFrame(NULL
, "wxGrid Sample", wxPoint(50, 50), wxSize(450, 300));
101 frame
->SetIcon(wxIcon("mondrian"));
104 frame
->SetIcon(wxIcon("aiai.xbm"));
108 wxMenu
*file_menu
= new wxMenu
;
109 file_menu
->Append(GRID_QUIT
, "E&xit");
111 wxMenu
*settings_menu
= new wxMenu
;
112 settings_menu
->Append(GRID_TOGGLE_EDITABLE
, "&Toggle editable");
113 settings_menu
->Append(GRID_TOGGLE_ROW_LABEL
, "Toggle ro&w label");
114 settings_menu
->Append(GRID_TOGGLE_COL_LABEL
, "Toggle co&l label");
115 settings_menu
->Append(GRID_TOGGLE_DIVIDERS
, "Toggle ÷rs");
116 settings_menu
->AppendSeparator();
117 settings_menu
->Append(GRID_LEFT_CELL
, "&Left cell alignment ");
118 settings_menu
->Append(GRID_CENTRE_CELL
, "&Centre cell alignment ");
119 settings_menu
->Append(GRID_RIGHT_CELL
, "&Right cell alignment ");
120 settings_menu
->AppendSeparator();
121 settings_menu
->Append(GRID_COLOUR_LABEL_BACKGROUND
, "Choose a label &background colour");
122 settings_menu
->Append(GRID_COLOUR_LABEL_TEXT
, "Choose a label fore&ground colour");
123 settings_menu
->Append(GRID_NORMAL_LABEL_COLOURING
, "&Normal label colouring");
124 settings_menu
->AppendSeparator();
125 settings_menu
->Append(GRID_COLOUR_CELL_BACKGROUND
, "Choo&se a cell &background colour");
126 settings_menu
->Append(GRID_COLOUR_CELL_TEXT
, "Choose &a cell foreground colour");
127 settings_menu
->Append(GRID_NORMAL_CELL_COLOURING
, "N&ormal cell colouring");
129 wxMenuBar
*menu_bar
= new wxMenuBar
;
130 menu_bar
->Append(file_menu
, "&File");
131 menu_bar
->Append(settings_menu
, "&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("First cell", 0, 0);
141 frame
->grid
->SetCellValue("Another cell", 1, 1);
142 frame
->grid
->SetCellValue("Yet another cell", 2, 2);
143 frame
->grid
->SetCellTextFont(wxTheFontList
->FindOrCreateFont(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();
163 // My frame constructor
164 MyFrame::MyFrame(wxFrame
*frame
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
):
165 wxFrame(frame
, -1, title
, pos
, size
)
170 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
171 EVT_MENU(GRID_TOGGLE_EDITABLE
, MyFrame::ToggleEditable
)
172 EVT_MENU(GRID_TOGGLE_ROW_LABEL
, MyFrame::ToggleRowLabel
)
173 EVT_MENU(GRID_TOGGLE_COL_LABEL
, MyFrame::ToggleColLabel
)
174 EVT_MENU(GRID_TOGGLE_DIVIDERS
, MyFrame::ToggleDividers
)
175 EVT_MENU(GRID_LEFT_CELL
, MyFrame::LeftCell
)
176 EVT_MENU(GRID_CENTRE_CELL
, MyFrame::CentreCell
)
177 EVT_MENU(GRID_RIGHT_CELL
, MyFrame::RightCell
)
178 EVT_MENU(GRID_COLOUR_LABEL_BACKGROUND
, MyFrame::ColourLabelBackground
)
179 EVT_MENU(GRID_COLOUR_LABEL_TEXT
, MyFrame::ColourLabelText
)
180 EVT_MENU(GRID_NORMAL_LABEL_COLOURING
, MyFrame::NormalLabelColouring
)
181 EVT_MENU(GRID_COLOUR_CELL_BACKGROUND
, MyFrame::ColourCellBackground
)
182 EVT_MENU(GRID_COLOUR_CELL_TEXT
, MyFrame::ColourCellText
)
183 EVT_MENU(GRID_NORMAL_CELL_COLOURING
, MyFrame::NormalCellColouring
)
184 EVT_MENU(GRID_QUIT
, MyFrame::Quit
)
187 void MyFrame::ToggleEditable(wxCommandEvent
& event
)
189 grid
->SetEditable(!grid
->GetEditable());
193 void MyFrame::ToggleRowLabel(wxCommandEvent
& event
)
195 if (grid
->GetLabelSize(wxVERTICAL
) > 0)
196 grid
->SetLabelSize(wxVERTICAL
, 0);
198 grid
->SetLabelSize(wxVERTICAL
, 40);
202 void MyFrame::ToggleColLabel(wxCommandEvent
& event
)
204 if (grid
->GetLabelSize(wxHORIZONTAL
) > 0)
205 grid
->SetLabelSize(wxHORIZONTAL
, 0);
207 grid
->SetLabelSize(wxHORIZONTAL
, 20);
211 void MyFrame::ToggleDividers(wxCommandEvent
& event
)
213 if (!grid
->GetDividerPen())
214 grid
->SetDividerPen(wxThePenList
->FindOrCreatePen("LIGHT GREY", 1, wxSOLID
));
216 grid
->SetDividerPen(NULL
);
220 void MyFrame::LeftCell(wxCommandEvent
& event
)
222 grid
->SetCellAlignment(wxLEFT
);
226 void MyFrame::CentreCell(wxCommandEvent
& event
)
228 grid
->SetCellAlignment(wxCENTRE
);
232 void MyFrame::RightCell(wxCommandEvent
& event
)
234 grid
->SetCellAlignment(wxRIGHT
);
238 void MyFrame::ColourLabelBackground(wxCommandEvent
& event
)
241 data
.SetChooseFull(TRUE
);
242 wxColourDialog
dialog(this, &data
);
243 if (dialog
.ShowModal() != wxID_CANCEL
)
245 wxColourData retData
= dialog
.GetColourData();
246 wxColour col
= retData
.GetColour();
247 grid
->SetLabelBackgroundColour(col
);
252 void MyFrame::ColourLabelText(wxCommandEvent
& event
)
255 data
.SetChooseFull(TRUE
);
256 wxColourDialog
dialog(this, &data
);
257 if (dialog
.ShowModal() != wxID_CANCEL
)
259 wxColourData retData
= dialog
.GetColourData();
260 wxColour col
= retData
.GetColour();
261 grid
->SetLabelTextColour(col
);
266 void MyFrame::NormalLabelColouring(wxCommandEvent
& event
)
268 grid
->SetLabelBackgroundColour(*wxLIGHT_GREY
);
269 grid
->SetLabelTextColour(*wxBLACK
);
273 void MyFrame::ColourCellBackground(wxCommandEvent
& event
)
276 data
.SetChooseFull(TRUE
);
277 wxColourDialog
dialog(this, &data
);
278 if (dialog
.ShowModal() != wxID_CANCEL
)
280 wxColourData retData
= dialog
.GetColourData();
281 wxColour col
= retData
.GetColour();
282 grid
->SetCellBackgroundColour(col
);
287 void MyFrame::ColourCellText(wxCommandEvent
& event
)
290 data
.SetChooseFull(TRUE
);
291 wxColourDialog
dialog(this, &data
);
292 if (dialog
.ShowModal() != wxID_CANCEL
)
294 wxColourData retData
= dialog
.GetColourData();
295 wxColour col
= retData
.GetColour();
296 grid
->SetCellTextColour(col
);
301 void MyFrame::NormalCellColouring(wxCommandEvent
& event
)
303 grid
->SetCellBackgroundColour(*wxWHITE
);
304 grid
->SetCellTextColour(*wxBLACK
);
308 void MyFrame::Quit(wxCommandEvent
& event
)
313 // Ensure that the grid's edit control always has the focus.
314 void MyFrame::OnActivate(wxActivateEvent
& event
)
316 if (grid
) grid
->OnActivate(event
.GetActive());