]> git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
Misc release-related mods
[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__) || defined(__WXMAC__)
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 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
84 EVT_MENU(MDI_CHANGE_POSITION, MyChild::OnChangePosition)
85 EVT_MENU(MDI_CHANGE_SIZE, MyChild::OnChangeSize)
86
87 EVT_SIZE(MyChild::OnSize)
88 EVT_MOVE(MyChild::OnMove)
89
90 EVT_CLOSE(MyChild::OnClose)
91 END_EVENT_TABLE()
92
93 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
94 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
95 END_EVENT_TABLE()
96
97 // ===========================================================================
98 // implementation
99 // ===========================================================================
100
101 // ---------------------------------------------------------------------------
102 // MyApp
103 // ---------------------------------------------------------------------------
104
105 // Initialise this in OnInit, not statically
106 bool MyApp::OnInit()
107 {
108 // Create the main frame window
109
110 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
111 wxPoint(-1, -1), wxSize(500, 400),
112 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
113 #ifdef __WXMSW__
114 #if 0
115 // Experimental: change the window menu
116 wxMenu* windowMenu = new wxMenu;
117 windowMenu->Append(5000, "My menu item!");
118 frame->SetWindowMenu(windowMenu);
119 #endif
120 #endif
121
122 // Give it an icon
123 #ifdef __WXMSW__
124 frame->SetIcon(wxIcon("mdi_icn"));
125 #else
126 frame->SetIcon(wxIcon( mondrian_xpm ));
127 #endif
128
129 // Make a menubar
130 wxMenu *file_menu = new wxMenu;
131
132 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N", "Create a new child window");
133 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X", "Quit the program");
134
135 wxMenu *help_menu = new wxMenu;
136 help_menu->Append(MDI_ABOUT, "&About\tF1");
137
138 wxMenuBar *menu_bar = new wxMenuBar;
139
140 menu_bar->Append(file_menu, "&File");
141 menu_bar->Append(help_menu, "&Help");
142
143 // Associate the menu bar with the frame
144 frame->SetMenuBar(menu_bar);
145
146 frame->CreateStatusBar();
147
148 frame->Show(TRUE);
149
150 SetTopWindow(frame);
151
152 return TRUE;
153 }
154
155 // ---------------------------------------------------------------------------
156 // MyFrame
157 // ---------------------------------------------------------------------------
158
159 // Define my frame constructor
160 MyFrame::MyFrame(wxWindow *parent,
161 const wxWindowID id,
162 const wxString& title,
163 const wxPoint& pos,
164 const wxSize& size,
165 const long style)
166 : wxMDIParentFrame(parent, id, title, pos, size, style)
167 {
168 textWindow = new wxTextCtrl(this, -1, "A help window",
169 wxDefaultPosition, wxDefaultSize,
170 wxTE_MULTILINE | wxSUNKEN_BORDER);
171
172 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
173 InitToolBar(GetToolBar());
174
175 // Accelerators
176 wxAcceleratorEntry entries[3];
177 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
178 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
179 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
180 wxAcceleratorTable accel(3, entries);
181 SetAcceleratorTable(accel);
182 }
183
184 void MyFrame::OnClose(wxCloseEvent& event)
185 {
186 if ( event.CanVeto() && (gs_nFrames > 0) )
187 {
188 wxString msg;
189 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
190 if ( wxMessageBox(msg, "Please confirm",
191 wxICON_QUESTION | wxYES_NO) != wxYES )
192 {
193 event.Veto();
194
195 return;
196 }
197 }
198
199 event.Skip();
200 }
201
202 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
203 {
204 Close();
205 }
206
207 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
208 {
209 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
210 "Author: Julian Smart (c) 1997\n"
211 "Usage: mdi.exe", "About MDI Demo");
212 }
213
214 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
215 {
216 // Make another frame, containing a canvas
217 MyChild *subframe = new MyChild(frame, "Canvas Frame",
218 wxPoint(-1, -1), wxSize(-1, -1),
219 wxDEFAULT_FRAME_STYLE);
220
221 wxString title;
222 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
223
224 subframe->SetTitle(title);
225
226 // Give it an icon
227 #ifdef __WXMSW__
228 subframe->SetIcon(wxIcon("chrt_icn"));
229 #else
230 subframe->SetIcon(wxIcon( mondrian_xpm ));
231 #endif
232
233 // Make a menubar
234 wxMenu *file_menu = new wxMenu;
235
236 file_menu->Append(MDI_NEW_WINDOW, "&New window");
237 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
238 file_menu->Append(MDI_QUIT, "&Exit");
239
240 wxMenu *option_menu = new wxMenu;
241
242 option_menu->Append(MDI_REFRESH, "&Refresh picture");
243 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
244 option_menu->AppendSeparator();
245 option_menu->Append(MDI_CHANGE_POSITION, "Move frame\tCtrl-M");
246 option_menu->Append(MDI_CHANGE_SIZE, "Resize frame\tCtrl-S");
247
248 wxMenu *help_menu = new wxMenu;
249 help_menu->Append(MDI_ABOUT, "&About");
250
251 wxMenuBar *menu_bar = new wxMenuBar;
252
253 menu_bar->Append(file_menu, "&File");
254 menu_bar->Append(option_menu, "&Child");
255 menu_bar->Append(help_menu, "&Help");
256
257 // Associate the menu bar with the frame
258 subframe->SetMenuBar(menu_bar);
259
260 subframe->CreateStatusBar();
261 subframe->SetStatusText(title);
262
263 int width, height;
264 subframe->GetClientSize(&width, &height);
265 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
266 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
267 subframe->canvas = canvas;
268
269 // Give it scrollbars
270 canvas->SetScrollbars(20, 20, 50, 50);
271
272 subframe->Show(TRUE);
273 }
274
275 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
276 {
277 int w, h;
278 GetClientSize(&w, &h);
279
280 textWindow->SetSize(0, 0, 200, h);
281 GetClientWindow()->SetSize(200, 0, w - 200, h);
282 }
283
284 void MyFrame::InitToolBar(wxToolBar* toolBar)
285 {
286 wxBitmap* bitmaps[8];
287
288 #ifdef __WXMSW__
289 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
290 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
291 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
292 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
293 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
294 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
295 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
296 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
297 #else
298 bitmaps[0] = new wxBitmap( new_xpm );
299 bitmaps[1] = new wxBitmap( open_xpm );
300 bitmaps[2] = new wxBitmap( save_xpm );
301 bitmaps[3] = new wxBitmap( copy_xpm );
302 bitmaps[4] = new wxBitmap( cut_xpm );
303 bitmaps[5] = new wxBitmap( paste_xpm );
304 bitmaps[6] = new wxBitmap( print_xpm );
305 bitmaps[7] = new wxBitmap( help_xpm );
306 #endif
307
308 #ifdef __WXMSW__
309 int width = 24;
310 #else
311 int width = 16;
312 #endif
313 int currentX = 5;
314
315 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
316 currentX += width + 5;
317 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
318 currentX += width + 5;
319 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
320 currentX += width + 5;
321 toolBar->AddSeparator();
322 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
323 currentX += width + 5;
324 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
325 currentX += width + 5;
326 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
327 currentX += width + 5;
328 toolBar->AddSeparator();
329 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
330 currentX += width + 5;
331 toolBar->AddSeparator();
332 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
333
334 toolBar->Realize();
335
336 int i;
337 for (i = 0; i < 8; i++)
338 delete bitmaps[i];
339 }
340
341 // ---------------------------------------------------------------------------
342 // MyCanvas
343 // ---------------------------------------------------------------------------
344
345 // Define a constructor for my canvas
346 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
347 : wxScrolledWindow(parent, -1, pos, size,
348 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
349 {
350 SetBackgroundColour(wxColour("WHITE"));
351
352 m_dirty = FALSE;
353 }
354
355 // Define the repainting behaviour
356 void MyCanvas::OnDraw(wxDC& dc)
357 {
358 dc.SetFont(*wxSWISS_FONT);
359 dc.SetPen(*wxGREEN_PEN);
360 dc.DrawLine(0, 0, 200, 200);
361 dc.DrawLine(200, 0, 0, 200);
362
363 dc.SetBrush(*wxCYAN_BRUSH);
364 dc.SetPen(*wxRED_PEN);
365 dc.DrawRectangle(100, 100, 100, 50);
366 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
367
368 dc.DrawEllipse(250, 250, 100, 50);
369 #if wxUSE_SPLINES
370 dc.DrawSpline(50, 200, 50, 100, 200, 10);
371 #endif // wxUSE_SPLINES
372 dc.DrawLine(50, 230, 200, 230);
373 dc.DrawText("This is a test string", 50, 230);
374
375 wxPoint points[3];
376 points[0].x = 200; points[0].y = 300;
377 points[1].x = 100; points[1].y = 400;
378 points[2].x = 300; points[2].y = 400;
379
380 dc.DrawPolygon(3, points);
381 }
382
383 // This implements a tiny doodling program! Drag the mouse using the left
384 // button.
385 void MyCanvas::OnEvent(wxMouseEvent& event)
386 {
387 wxClientDC dc(this);
388 PrepareDC(dc);
389
390 wxPoint pt(event.GetLogicalPosition(dc));
391
392 if (xpos > -1 && ypos > -1 && event.Dragging())
393 {
394 dc.SetPen(*wxBLACK_PEN);
395 dc.DrawLine(xpos, ypos, pt.x, pt.y);
396
397 m_dirty = TRUE;
398 }
399
400 xpos = pt.x;
401 ypos = pt.y;
402 }
403
404 // ---------------------------------------------------------------------------
405 // MyChild
406 // ---------------------------------------------------------------------------
407
408 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
409 const wxPoint& pos, const wxSize& size,
410 const long style)
411 : wxMDIChildFrame(parent, -1, title, pos, size, style)
412 {
413 canvas = (MyCanvas *) NULL;
414 my_children.Append(this);
415 }
416
417 MyChild::~MyChild()
418 {
419 my_children.DeleteObject(this);
420 }
421
422 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
423 {
424 Close(TRUE);
425 }
426
427 void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
428 {
429 if ( canvas )
430 canvas->Refresh();
431 }
432
433 void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
434 {
435 Move(10, 10);
436 }
437
438 void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
439 {
440 SetClientSize(100, 100);
441 }
442
443 void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
444 {
445 static wxString s_title = _T("Canvas Frame");
446
447 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
448 _T("MDI sample question"),
449 s_title,
450 GetParent()->GetParent());
451 if ( !title )
452 return;
453
454 s_title = title;
455 SetTitle(s_title);
456 }
457
458 void MyChild::OnActivate(wxActivateEvent& event)
459 {
460 if ( event.GetActive() && canvas )
461 canvas->SetFocus();
462 }
463
464 void MyChild::OnMove(wxMoveEvent& event)
465 {
466 // VZ: here everything is totally wrong under MSW, the positions are
467 // different and both wrong (pos2 is off by 2 pixels for me which seems
468 // to be the width of the MDI canvas border)
469 wxPoint pos1 = event.GetPosition(),
470 pos2 = GetPosition();
471 wxLogStatus("position from event: (%d, %d), from frame (%d, %d)",
472 pos1.x, pos1.y, pos2.x, pos2.y);
473
474 event.Skip();
475 }
476
477 void MyChild::OnSize(wxSizeEvent& event)
478 {
479 // VZ: under MSW the size event carries the client size (quite
480 // unexpectedly) *except* for the very first one which has the full
481 // size... what should it really be? TODO: check under wxGTK
482 wxSize size1 = event.GetSize(),
483 size2 = GetSize(),
484 size3 = GetClientSize();
485 wxLogStatus("size from event: %dx%d, from frame %dx%d, client %dx%d",
486 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
487
488 event.Skip();
489 }
490
491 void MyChild::OnClose(wxCloseEvent& event)
492 {
493 if ( canvas && canvas->IsDirty() )
494 {
495 if ( wxMessageBox("Really close?", "Please confirm",
496 wxICON_QUESTION | wxYES_NO) != wxYES )
497 {
498 event.Veto();
499
500 return;
501 }
502 }
503
504 gs_nFrames--;
505
506 event.Skip();
507 }
508
509