Set/GetWindowMenu added to MDI parent frame under MSW
[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 // ===========================================================================
13 // declarations
14 // ===========================================================================
15
16 // ---------------------------------------------------------------------------
17 // headers
18 // ---------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #ifndef WX_PRECOMP
28 #include "wx/wx.h"
29 #include "wx/mdi.h"
30 #endif
31
32 #include <wx/toolbar.h>
33
34 #if defined(__WXGTK__) || defined(__WXMOTIF__)
35 #include "mondrian.xpm"
36 #include "bitmaps/new.xpm"
37 #include "bitmaps/open.xpm"
38 #include "bitmaps/save.xpm"
39 #include "bitmaps/copy.xpm"
40 #include "bitmaps/cut.xpm"
41 #include "bitmaps/paste.xpm"
42 #include "bitmaps/print.xpm"
43 #include "bitmaps/help.xpm"
44 #endif
45
46 #include "mdi.h"
47
48 IMPLEMENT_APP(MyApp)
49
50 // ---------------------------------------------------------------------------
51 // global variables
52 // ---------------------------------------------------------------------------
53
54 MyFrame *frame = (MyFrame *) NULL;
55 wxList my_children;
56
57 // For drawing lines in a canvas
58 static long xpos = -1;
59 static long ypos = -1;
60
61 static int gs_nFrames = 0;
62
63 // ---------------------------------------------------------------------------
64 // event tables
65 // ---------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
68 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
69 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
70 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
71
72 EVT_CLOSE(MyFrame::OnClose)
73
74 EVT_SIZE(MyFrame::OnSize)
75 END_EVENT_TABLE()
76
77 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
78 // to the parent window for processing, so no need to
79 // duplicate event handlers here.
80 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
81 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
82 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
83
84 EVT_CLOSE(MyChild::OnClose)
85 END_EVENT_TABLE()
86
87 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
88 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
89 END_EVENT_TABLE()
90
91 // ===========================================================================
92 // implementation
93 // ===========================================================================
94
95 // ---------------------------------------------------------------------------
96 // MyApp
97 // ---------------------------------------------------------------------------
98
99 // Initialise this in OnInit, not statically
100 bool MyApp::OnInit()
101 {
102 // Create the main frame window
103
104 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
105 wxPoint(-1, -1), wxSize(500, 400),
106 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
107 #ifdef __WXMSW__
108 #if 0
109 // Experimental: change the window menu
110 wxMenu* windowMenu = new wxMenu;
111 windowMenu->Append(5000, "My menu item!");
112 frame->SetWindowMenu(windowMenu);
113 #endif
114 #endif
115
116 // Give it an icon
117 #ifdef __WXMSW__
118 frame->SetIcon(wxIcon("mdi_icn"));
119 #else
120 frame->SetIcon(wxIcon( mondrian_xpm ));
121 #endif
122
123 // Make a menubar
124 wxMenu *file_menu = new wxMenu;
125
126 file_menu->Append(MDI_NEW_WINDOW, "&New window", "Create a new child window");
127 file_menu->Append(MDI_QUIT, "&Exit", "Quit the program");
128
129 wxMenu *help_menu = new wxMenu;
130 help_menu->Append(MDI_ABOUT, "&About");
131
132 wxMenuBar *menu_bar = new wxMenuBar;
133
134 menu_bar->Append(file_menu, "&File");
135 menu_bar->Append(help_menu, "&Help");
136
137 // Associate the menu bar with the frame
138 frame->SetMenuBar(menu_bar);
139
140 frame->CreateStatusBar();
141
142 frame->Show(TRUE);
143
144 SetTopWindow(frame);
145
146 return TRUE;
147 }
148
149 // ---------------------------------------------------------------------------
150 // MyFrame
151 // ---------------------------------------------------------------------------
152
153 // Define my frame constructor
154 MyFrame::MyFrame(wxWindow *parent,
155 const wxWindowID id,
156 const wxString& title,
157 const wxPoint& pos,
158 const wxSize& size,
159 const long style)
160 : wxMDIParentFrame(parent, id, title, pos, size, style)
161 {
162 textWindow = new wxTextCtrl(this, -1, "A help window",
163 wxDefaultPosition, wxDefaultSize,
164 wxTE_MULTILINE | wxSUNKEN_BORDER);
165
166 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
167 InitToolBar(GetToolBar());
168
169 // Accelerators
170 wxAcceleratorEntry entries[3];
171 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
172 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
173 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
174 wxAcceleratorTable accel(3, entries);
175 SetAcceleratorTable(accel);
176 }
177
178 void MyFrame::OnClose(wxCloseEvent& event)
179 {
180 if ( event.CanVeto() && (gs_nFrames > 0) )
181 {
182 wxString msg;
183 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
184 if ( wxMessageBox(msg, "Please confirm",
185 wxICON_QUESTION | wxYES_NO) != wxYES )
186 {
187 event.Veto();
188
189 return;
190 }
191 }
192
193 event.Skip();
194 }
195
196 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
197 {
198 Close();
199 }
200
201 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
202 {
203 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
204 "Author: Julian Smart (c) 1997\n"
205 "Usage: mdi.exe", "About MDI Demo");
206 }
207
208 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
209 {
210 // Make another frame, containing a canvas
211 MyChild *subframe = new MyChild(frame, "Canvas Frame",
212 wxPoint(-1, -1), wxSize(-1, -1),
213 wxDEFAULT_FRAME_STYLE);
214
215 wxString title;
216 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
217
218 subframe->SetTitle(title);
219
220 // Give it an icon
221 #ifdef __WXMSW__
222 subframe->SetIcon(wxIcon("chrt_icn"));
223 #else
224 subframe->SetIcon(wxIcon( mondrian_xpm ));
225 #endif
226
227 // Make a menubar
228 wxMenu *file_menu = new wxMenu;
229
230 file_menu->Append(MDI_NEW_WINDOW, "&New window");
231 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
232 file_menu->Append(MDI_QUIT, "&Exit");
233
234 wxMenu *option_menu = new wxMenu;
235
236 // Dummy option
237 option_menu->Append(MDI_REFRESH, "&Refresh picture");
238
239 wxMenu *help_menu = new wxMenu;
240 help_menu->Append(MDI_ABOUT, "&About");
241
242 wxMenuBar *menu_bar = new wxMenuBar;
243
244 menu_bar->Append(file_menu, "&File");
245 menu_bar->Append(option_menu, "&Options");
246 menu_bar->Append(help_menu, "&Help");
247
248 // Associate the menu bar with the frame
249 subframe->SetMenuBar(menu_bar);
250
251 int width, height;
252 subframe->GetClientSize(&width, &height);
253 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
254 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
255 subframe->canvas = canvas;
256
257 // Give it scrollbars
258 canvas->SetScrollbars(20, 20, 50, 50);
259
260 subframe->CreateStatusBar();
261 subframe->SetStatusText(title);
262
263 subframe->Show(TRUE);
264 }
265
266 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
267 {
268 int w, h;
269 GetClientSize(&w, &h);
270
271 textWindow->SetSize(0, 0, 200, h);
272 GetClientWindow()->SetSize(200, 0, w - 200, h);
273 }
274
275 void MyFrame::InitToolBar(wxToolBar* toolBar)
276 {
277 wxBitmap* bitmaps[8];
278
279 #ifdef __WXMSW__
280 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
281 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
282 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
283 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
284 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
285 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
286 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
287 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
288 #else
289 bitmaps[0] = new wxBitmap( new_xpm );
290 bitmaps[1] = new wxBitmap( open_xpm );
291 bitmaps[2] = new wxBitmap( save_xpm );
292 bitmaps[3] = new wxBitmap( copy_xpm );
293 bitmaps[4] = new wxBitmap( cut_xpm );
294 bitmaps[5] = new wxBitmap( paste_xpm );
295 bitmaps[6] = new wxBitmap( print_xpm );
296 bitmaps[7] = new wxBitmap( help_xpm );
297 #endif
298
299 #ifdef __WXMSW__
300 int width = 24;
301 #else
302 int width = 16;
303 #endif
304 int currentX = 5;
305
306 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
307 currentX += width + 5;
308 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
309 currentX += width + 5;
310 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
311 currentX += width + 5;
312 toolBar->AddSeparator();
313 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
314 currentX += width + 5;
315 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
316 currentX += width + 5;
317 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
318 currentX += width + 5;
319 toolBar->AddSeparator();
320 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
321 currentX += width + 5;
322 toolBar->AddSeparator();
323 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
324
325 toolBar->Realize();
326
327 int i;
328 for (i = 0; i < 8; i++)
329 delete bitmaps[i];
330 }
331
332 // ---------------------------------------------------------------------------
333 // MyCanvas
334 // ---------------------------------------------------------------------------
335
336 // Define a constructor for my canvas
337 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
338 : wxScrolledWindow(parent, -1, pos, size,
339 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
340 {
341 SetBackgroundColour(wxColour("WHITE"));
342
343 m_dirty = FALSE;
344 }
345
346 // Define the repainting behaviour
347 void MyCanvas::OnDraw(wxDC& dc)
348 {
349 dc.SetFont(*wxSWISS_FONT);
350 dc.SetPen(*wxGREEN_PEN);
351 dc.DrawLine(0, 0, 200, 200);
352 dc.DrawLine(200, 0, 0, 200);
353
354 dc.SetBrush(*wxCYAN_BRUSH);
355 dc.SetPen(*wxRED_PEN);
356 dc.DrawRectangle(100, 100, 100, 50);
357 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
358
359 dc.DrawEllipse(250, 250, 100, 50);
360 dc.DrawSpline(50, 200, 50, 100, 200, 10);
361 dc.DrawLine(50, 230, 200, 230);
362 dc.DrawText("This is a test string", 50, 230);
363
364 wxPoint points[3];
365 points[0].x = 200; points[0].y = 300;
366 points[1].x = 100; points[1].y = 400;
367 points[2].x = 300; points[2].y = 400;
368
369 dc.DrawPolygon(3, points);
370 }
371
372 // This implements a tiny doodling program! Drag the mouse using the left
373 // button.
374 void MyCanvas::OnEvent(wxMouseEvent& event)
375 {
376 wxClientDC dc(this);
377 PrepareDC(dc);
378
379 wxPoint pt(event.GetLogicalPosition(dc));
380
381 if (xpos > -1 && ypos > -1 && event.Dragging())
382 {
383 dc.SetPen(*wxBLACK_PEN);
384 dc.DrawLine(xpos, ypos, pt.x, pt.y);
385
386 m_dirty = TRUE;
387 }
388
389 xpos = pt.x;
390 ypos = pt.y;
391 }
392
393 // ---------------------------------------------------------------------------
394 // MyChild
395 // ---------------------------------------------------------------------------
396
397 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
398 const wxPoint& pos, const wxSize& size,
399 const long style)
400 : wxMDIChildFrame(parent, -1, title, pos, size, style)
401 {
402 canvas = (MyCanvas *) NULL;
403 my_children.Append(this);
404 }
405
406 MyChild::~MyChild()
407 {
408 my_children.DeleteObject(this);
409 }
410
411 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
412 {
413 Close(TRUE);
414 }
415
416 void MyChild::OnRefresh(wxCommandEvent& event)
417 {
418 Refresh();
419 }
420
421 void MyChild::OnActivate(wxActivateEvent& event)
422 {
423 if ( event.GetActive() && canvas )
424 canvas->SetFocus();
425 }
426
427 void MyChild::OnClose(wxCloseEvent& event)
428 {
429 if ( canvas && canvas->IsDirty() )
430 {
431 if ( wxMessageBox("Really close?", "Please confirm",
432 wxICON_QUESTION | wxYES_NO) != wxYES )
433 {
434 event.Veto();
435
436 return;
437 }
438 }
439
440 gs_nFrames--;
441
442 event.Skip();
443 }
444
445