Added wxAcceleratorTable, wxFrame::SetAcceleratorTable and additions to process it...
[wxWidgets.git] / samples / mdi / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mdi.cpp
3 // Purpose: MDI sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
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 #include "wx/mdi.h"
22 #endif
23
24 #include <wx/toolbar.h>
25
26 #ifdef __WXGTK__
27 #include "folder.xpm"
28 #endif
29
30 #include "mdi.h"
31
32 MyFrame *frame = NULL;
33 wxList my_children;
34
35 IMPLEMENT_APP(MyApp)
36
37 // For drawing lines in a canvas
38 long xpos = -1;
39 long ypos = -1;
40
41 int winNumber = 1;
42
43 // Initialise this in OnInit, not statically
44 bool MyApp::OnInit(void)
45 {
46 // Create the main frame window
47
48 frame = new MyFrame(NULL, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
49 wxDEFAULT_FRAME | wxHSCROLL | wxVSCROLL);
50
51 // Give it an icon (this is ignored in MDI mode: uses resources)
52 #ifdef __WXMSW__
53 frame->SetIcon(wxIcon("mdi_icn"));
54 #endif
55 #ifdef __X__
56 frame->SetIcon(wxIcon("aiai.xbm"));
57 #endif
58
59 // Make a menubar
60 wxMenu *file_menu = new wxMenu;
61
62 file_menu->Append(MDI_NEW_WINDOW, "&New window");
63 file_menu->Append(MDI_QUIT, "&Exit");
64
65 wxMenu *help_menu = new wxMenu;
66 help_menu->Append(MDI_ABOUT, "&About");
67
68 wxMenuBar *menu_bar = new wxMenuBar;
69
70 menu_bar->Append(file_menu, "&File");
71 menu_bar->Append(help_menu, "&Help");
72
73 // Associate the menu bar with the frame
74 frame->SetMenuBar(menu_bar);
75
76 frame->CreateStatusBar();
77
78 frame->Show(TRUE);
79
80 SetTopWindow(frame);
81
82 return TRUE;
83 }
84
85 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
86 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
87 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
88 EVT_SIZE(MyFrame::OnSize)
89 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
90 END_EVENT_TABLE()
91
92 // Define my frame constructor
93 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
94 const long style):
95 wxMDIParentFrame(parent, id, title, pos, size, style)
96 {
97 textWindow = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize,
98 wxTE_MULTILINE|wxSUNKEN_BORDER);
99 textWindow->SetValue("A help window");
100
101 CreateToolBar(wxNO_BORDER|wxTB_FLAT|wxTB_HORIZONTAL);
102 InitToolBar(GetToolBar());
103
104 #ifdef __WXMSW__
105 // Accelerators
106 wxAcceleratorEntry entries[3];
107 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
108 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
109 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
110 wxAcceleratorTable accel(3, entries);
111 SetAcceleratorTable(accel);
112 #endif
113 }
114
115 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
116 {
117 Close(TRUE);
118 }
119
120 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
121 {
122 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
123 }
124
125 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
126 {
127 // Make another frame, containing a canvas
128 MyChild *subframe = new MyChild(frame, "Canvas Frame", wxPoint(-1, -1), wxSize(-1, -1),
129 wxDEFAULT_FRAME);
130
131 wxString title;
132 title.Printf("Canvas Frame %d", winNumber);
133 subframe->SetTitle(title);
134 winNumber ++;
135
136 // Give it an icon (this is ignored in MDI mode: uses resources)
137 #ifdef __WXMSW__
138 subframe->SetIcon(wxIcon("chrt_icn"));
139 #endif
140 #ifdef __X__
141 subframe->SetIcon(wxIcon("aiai.xbm"));
142 #endif
143
144 // Give it a status line
145 subframe->CreateStatusBar();
146
147 // Make a menubar
148 wxMenu *file_menu = new wxMenu;
149
150 file_menu->Append(MDI_NEW_WINDOW, "&New window");
151 file_menu->Append(MDI_CHILD_QUIT, "&Close child");
152 file_menu->Append(MDI_QUIT, "&Exit");
153
154 wxMenu *option_menu = new wxMenu;
155
156 // Dummy option
157 option_menu->Append(MDI_REFRESH, "&Refresh picture");
158
159 wxMenu *help_menu = new wxMenu;
160 help_menu->Append(MDI_ABOUT, "&About");
161
162 wxMenuBar *menu_bar = new wxMenuBar;
163
164 menu_bar->Append(file_menu, "&File");
165 menu_bar->Append(option_menu, "&Options");
166 menu_bar->Append(help_menu, "&Help");
167
168 // Associate the menu bar with the frame
169 subframe->SetMenuBar(menu_bar);
170
171 int width, height;
172 subframe->GetClientSize(&width, &height);
173 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
174 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
175 subframe->canvas = canvas;
176
177 // Give it scrollbars
178 canvas->SetScrollbars(20, 20, 50, 50);
179
180 subframe->Show(TRUE);
181 }
182
183 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
184 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
185 END_EVENT_TABLE()
186
187 // Define a constructor for my canvas
188 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
189 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
190 {
191 }
192
193 // Define the repainting behaviour
194 void MyCanvas::OnDraw(wxDC& dc)
195 {
196 dc.SetFont(*wxSWISS_FONT);
197 dc.SetPen(*wxGREEN_PEN);
198 dc.DrawLine(0, 0, 200, 200);
199 dc.DrawLine(200, 0, 0, 200);
200
201 dc.SetBrush(*wxCYAN_BRUSH);
202 dc.SetPen(*wxRED_PEN);
203 dc.DrawRectangle(100, 100, 100, 50);
204 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
205
206 dc.DrawEllipse(250, 250, 100, 50);
207 dc.DrawSpline(50, 200, 50, 100, 200, 10);
208 dc.DrawLine(50, 230, 200, 230);
209 dc.DrawText("This is a test string", 50, 230);
210
211 wxPoint points[3];
212 points[0].x = 200; points[0].y = 300;
213 points[1].x = 100; points[1].y = 400;
214 points[2].x = 300; points[2].y = 400;
215
216 dc.DrawPolygon(3, points);
217 }
218
219 // This implements a tiny doodling program! Drag the mouse using
220 // the left button.
221 void MyCanvas::OnEvent(wxMouseEvent& event)
222 {
223 wxClientDC dc(this);
224 PrepareDC(dc);
225
226 wxPoint pt(event.GetLogicalPosition(dc));
227
228 if (xpos > -1 && ypos > -1 && event.Dragging())
229 {
230 dc.SetPen(*wxBLACK_PEN);
231 dc.DrawLine(xpos, ypos, pt.x, pt.y);
232 }
233 xpos = pt.x;
234 ypos = pt.y;
235 }
236
237 // Define the behaviour for the frame closing
238 // - must delete all frames except for the main one.
239 bool MyFrame::OnClose(void)
240 {
241 // Must delete children
242 wxNode *node = my_children.First();
243 while (node)
244 {
245 MyChild *child = (MyChild *)node->Data();
246 wxNode *next = node->Next();
247 child->OnClose();
248 delete child;
249 node = next;
250 }
251 return TRUE;
252 }
253
254 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
255 {
256 int w, h;
257 GetClientSize(&w, &h);
258
259 textWindow->SetSize(0, 0, 200, h);
260 GetClientWindow()->SetSize(200, 0, w - 200, h);
261 }
262
263 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
264 // to the parent window for processing, so no need to
265 // duplicate event handlers here.
266
267 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
268 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
269 END_EVENT_TABLE()
270
271 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
272 const long style):
273 wxMDIChildFrame(parent, -1, title, pos, size, style)
274 {
275 canvas = NULL;
276 my_children.Append(this);
277 }
278
279 MyChild::~MyChild(void)
280 {
281 my_children.DeleteObject(this);
282 }
283
284 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
285 {
286 Close(TRUE);
287 }
288
289 void MyChild::OnActivate(wxActivateEvent& event)
290 {
291 if (event.GetActive() && canvas)
292 canvas->SetFocus();
293 }
294
295 bool MyChild::OnClose(void)
296 {
297 return TRUE;
298 }
299
300 void MyFrame::InitToolBar(wxToolBar* toolBar)
301 {
302 wxBitmap* bitmaps[8];
303
304 #ifdef __WXMSW__
305 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
306 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
307 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
308 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
309 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
310 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
311 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
312 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
313 #else
314 bitmaps[0] = new wxBitmap( folder_xpm );
315 bitmaps[1] = new wxBitmap( folder_xpm );
316 bitmaps[2] = new wxBitmap( folder_xpm );
317 bitmaps[3] = new wxBitmap( folder_xpm );
318 bitmaps[4] = new wxBitmap( folder_xpm );
319 bitmaps[5] = new wxBitmap( folder_xpm );
320 bitmaps[6] = new wxBitmap( folder_xpm );
321 bitmaps[7] = new wxBitmap( folder_xpm );
322 #endif
323
324 #ifdef __WXMSW__
325 int width = 24;
326 #else
327 int width = 16;
328 #endif
329 int currentX = 5;
330
331 toolBar->AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
332 currentX += width + 5;
333 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
334 currentX += width + 5;
335 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
336 currentX += width + 5;
337 toolBar->AddSeparator();
338 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
339 currentX += width + 5;
340 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
341 currentX += width + 5;
342 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
343 currentX += width + 5;
344 toolBar->AddSeparator();
345 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
346 currentX += width + 5;
347 toolBar->AddSeparator();
348 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
349
350 toolBar->Realize();
351
352 int i;
353 for (i = 0; i < 8; i++)
354 delete bitmaps[i];
355 }
356