]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
Rebake after recguard.h added to bakefiles
[wxWidgets.git] / samples / mdi / mdi.cpp
CommitLineData
c801d85f
KB
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$
6aa89a22 8// Copyright: (c) Julian Smart
c52d95b4 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c52d95b4
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
c52d95b4 24 #pragma hdrstop
c801d85f
KB
25#endif
26
27#ifndef WX_PRECOMP
c52d95b4
VZ
28 #include "wx/wx.h"
29 #include "wx/mdi.h"
c801d85f
KB
30#endif
31
012f2cb2 32#include "wx/toolbar.h"
c801d85f 33
9fa453ec 34#if !defined(__WXMSW__)
c52d95b4 35 #include "mondrian.xpm"
716b7364
RR
36#endif
37
d04d32c4
CE
38#include "bitmaps/new.xpm"
39#include "bitmaps/open.xpm"
40#include "bitmaps/save.xpm"
41#include "bitmaps/copy.xpm"
42#include "bitmaps/cut.xpm"
43#include "bitmaps/paste.xpm"
44#include "bitmaps/print.xpm"
45#include "bitmaps/help.xpm"
46
47
c801d85f
KB
48#include "mdi.h"
49
c52d95b4
VZ
50IMPLEMENT_APP(MyApp)
51
52// ---------------------------------------------------------------------------
53// global variables
54// ---------------------------------------------------------------------------
55
c67daf87 56MyFrame *frame = (MyFrame *) NULL;
c801d85f
KB
57wxList my_children;
58
c801d85f 59// For drawing lines in a canvas
c52d95b4
VZ
60static long xpos = -1;
61static long ypos = -1;
62
63static int gs_nFrames = 0;
64
65// ---------------------------------------------------------------------------
66// event tables
67// ---------------------------------------------------------------------------
68
69BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
70 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
71 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
72 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
73
74 EVT_CLOSE(MyFrame::OnClose)
77380b5c 75 EVT_ICONIZE(MyFrame::OnIconize)
c52d95b4
VZ
76 EVT_SIZE(MyFrame::OnSize)
77END_EVENT_TABLE()
78
79// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
80// to the parent window for processing, so no need to
81// duplicate event handlers here.
82BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
83 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
84 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
f6bcfd97 85 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
3d8dea7e
VZ
86 EVT_MENU(MDI_CHANGE_POSITION, MyChild::OnChangePosition)
87 EVT_MENU(MDI_CHANGE_SIZE, MyChild::OnChangeSize)
88
3d8dea7e
VZ
89 EVT_SIZE(MyChild::OnSize)
90 EVT_MOVE(MyChild::OnMove)
c52d95b4
VZ
91
92 EVT_CLOSE(MyChild::OnClose)
93END_EVENT_TABLE()
94
95BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
96 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
97END_EVENT_TABLE()
98
99// ===========================================================================
100// implementation
101// ===========================================================================
c801d85f 102
c52d95b4
VZ
103// ---------------------------------------------------------------------------
104// MyApp
105// ---------------------------------------------------------------------------
c801d85f
KB
106
107// Initialise this in OnInit, not statically
c52d95b4 108bool MyApp::OnInit()
c801d85f 109{
c52d95b4 110 // Create the main frame window
c801d85f 111
eb839c84
WS
112 frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("MDI Demo"),
113 wxDefaultPosition, wxSize(500, 400),
c52d95b4 114 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
df61c009
JS
115#ifdef __WXMSW__
116#if 0
117 // Experimental: change the window menu
118 wxMenu* windowMenu = new wxMenu;
42ed7532 119 windowMenu->Append(5000, _T("My menu item!"));
df61c009
JS
120 frame->SetWindowMenu(windowMenu);
121#endif
122#endif
c801d85f 123
c52d95b4 124 // Give it an icon
2049ba38 125#ifdef __WXMSW__
42ed7532 126 frame->SetIcon(wxIcon(_T("mdi_icn")));
52cbfcf0 127#else
c52d95b4 128 frame->SetIcon(wxIcon( mondrian_xpm ));
c801d85f 129#endif
c801d85f 130
c52d95b4
VZ
131 // Make a menubar
132 wxMenu *file_menu = new wxMenu;
c801d85f 133
42ed7532
MB
134 file_menu->Append(MDI_NEW_WINDOW, _T("&New window\tCtrl-N"), _T("Create a new child window"));
135 file_menu->Append(MDI_QUIT, _T("&Exit\tAlt-X"), _T("Quit the program"));
c801d85f 136
c52d95b4 137 wxMenu *help_menu = new wxMenu;
42ed7532 138 help_menu->Append(MDI_ABOUT, _T("&About\tF1"));
c801d85f 139
c52d95b4 140 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 141
42ed7532
MB
142 menu_bar->Append(file_menu, _T("&File"));
143 menu_bar->Append(help_menu, _T("&Help"));
c801d85f 144
c52d95b4
VZ
145 // Associate the menu bar with the frame
146 frame->SetMenuBar(menu_bar);
c801d85f 147
8520f137 148#if wxUSE_STATUSBAR
c52d95b4 149 frame->CreateStatusBar();
8520f137 150#endif // wxUSE_STATUSBAR
c801d85f 151
eb839c84 152 frame->Show(true);
c801d85f 153
c52d95b4 154 SetTopWindow(frame);
c801d85f 155
eb839c84 156 return true;
c801d85f
KB
157}
158
c52d95b4
VZ
159// ---------------------------------------------------------------------------
160// MyFrame
161// ---------------------------------------------------------------------------
c801d85f
KB
162
163// Define my frame constructor
c52d95b4
VZ
164MyFrame::MyFrame(wxWindow *parent,
165 const wxWindowID id,
166 const wxString& title,
167 const wxPoint& pos,
168 const wxSize& size,
169 const long style)
3ebcfb76
VZ
170 : wxMDIParentFrame(parent, id, title, pos, size,
171 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 172{
eb839c84 173 textWindow = new wxTextCtrl(this, wxID_ANY, _T("A help window"),
c52d95b4
VZ
174 wxDefaultPosition, wxDefaultSize,
175 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 176
6ceba174 177#if wxUSE_TOOLBAR
fa762db4 178 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 179 InitToolBar(GetToolBar());
6ceba174 180#endif // wxUSE_TOOLBAR
57a7b7c1 181
57a7b7c1
JS
182 // Accelerators
183 wxAcceleratorEntry entries[3];
184 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
185 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
186 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
187 wxAcceleratorTable accel(3, entries);
188 SetAcceleratorTable(accel);
c801d85f
KB
189}
190
c52d95b4
VZ
191void MyFrame::OnClose(wxCloseEvent& event)
192{
193 if ( event.CanVeto() && (gs_nFrames > 0) )
194 {
195 wxString msg;
ddb6bc71 196 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
42ed7532 197 if ( wxMessageBox(msg, _T("Please confirm"),
c52d95b4
VZ
198 wxICON_QUESTION | wxYES_NO) != wxYES )
199 {
200 event.Veto();
201
202 return;
203 }
204 }
205
206 event.Skip();
207}
208
209void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 210{
c52d95b4 211 Close();
c801d85f
KB
212}
213
e3e65dac 214void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 215{
be5a51fb 216 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
42ed7532
MB
217 _T("Author: Julian Smart (c) 1997\n")
218 _T("Usage: mdi.exe"), _T("About MDI Demo"));
c801d85f
KB
219}
220
e3e65dac 221void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 222{
c52d95b4 223 // Make another frame, containing a canvas
eb839c84 224 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"));
c801d85f 225
c52d95b4 226 wxString title;
ddb6bc71 227 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
c801d85f 228
c52d95b4
VZ
229 subframe->SetTitle(title);
230
231 // Give it an icon
2049ba38 232#ifdef __WXMSW__
42ed7532 233 subframe->SetIcon(wxIcon(_T("chrt_icn")));
8704bf74 234#else
c52d95b4 235 subframe->SetIcon(wxIcon( mondrian_xpm ));
c801d85f
KB
236#endif
237
c52d95b4
VZ
238 // Make a menubar
239 wxMenu *file_menu = new wxMenu;
240
42ed7532
MB
241 file_menu->Append(MDI_NEW_WINDOW, _T("&New window"));
242 file_menu->Append(MDI_CHILD_QUIT, _T("&Close child"), _T("Close this window"));
243 file_menu->Append(MDI_QUIT, _T("&Exit"));
c801d85f 244
c52d95b4 245 wxMenu *option_menu = new wxMenu;
c801d85f 246
42ed7532
MB
247 option_menu->Append(MDI_REFRESH, _T("&Refresh picture"));
248 option_menu->Append(MDI_CHANGE_TITLE, _T("Change &title...\tCtrl-T"));
fa762db4 249 option_menu->AppendSeparator();
42ed7532
MB
250 option_menu->Append(MDI_CHANGE_POSITION, _T("Move frame\tCtrl-M"));
251 option_menu->Append(MDI_CHANGE_SIZE, _T("Resize frame\tCtrl-S"));
c801d85f 252
c52d95b4 253 wxMenu *help_menu = new wxMenu;
42ed7532 254 help_menu->Append(MDI_ABOUT, _T("&About"));
c801d85f 255
c52d95b4 256 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 257
42ed7532
MB
258 menu_bar->Append(file_menu, _T("&File"));
259 menu_bar->Append(option_menu, _T("&Child"));
260 menu_bar->Append(help_menu, _T("&Help"));
c801d85f 261
c52d95b4
VZ
262 // Associate the menu bar with the frame
263 subframe->SetMenuBar(menu_bar);
c801d85f 264
8520f137 265#if wxUSE_STATUSBAR
c41ea66a
VZ
266 subframe->CreateStatusBar();
267 subframe->SetStatusText(title);
8520f137 268#endif // wxUSE_STATUSBAR
c41ea66a 269
c52d95b4
VZ
270 int width, height;
271 subframe->GetClientSize(&width, &height);
272 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
273 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
274 subframe->canvas = canvas;
c801d85f 275
c52d95b4
VZ
276 // Give it scrollbars
277 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 278
eb839c84 279 subframe->Show(true);
c801d85f
KB
280}
281
5f58391f
JS
282void MyFrame::OnSize(wxSizeEvent&
283 #ifdef __WXUNIVERSAL__
284 event
285 #else
286 WXUNUSED(event)
287 #endif
288 )
c52d95b4
VZ
289{
290 int w, h;
291 GetClientSize(&w, &h);
292
293 textWindow->SetSize(0, 0, 200, h);
294 GetClientWindow()->SetSize(200, 0, w - 200, h);
a9c824e9
JS
295
296 // FIXME: On wxX11, we need the MDI frame to process this
297 // event, but on other platforms this should not
298 // be done.
2b5f62a0 299#ifdef __WXUNIVERSAL__
b9f933ab 300 event.Skip();
a9c824e9 301#endif
c52d95b4
VZ
302}
303
77380b5c
CE
304void MyFrame::OnIconize(wxIconizeEvent& event)
305{
306 wxSizeEvent e;
307 OnSize (e) ;
308}
309
6ceba174 310#if wxUSE_TOOLBAR
c52d95b4
VZ
311void MyFrame::InitToolBar(wxToolBar* toolBar)
312{
313 wxBitmap* bitmaps[8];
314
c52d95b4
VZ
315 bitmaps[0] = new wxBitmap( new_xpm );
316 bitmaps[1] = new wxBitmap( open_xpm );
317 bitmaps[2] = new wxBitmap( save_xpm );
318 bitmaps[3] = new wxBitmap( copy_xpm );
319 bitmaps[4] = new wxBitmap( cut_xpm );
320 bitmaps[5] = new wxBitmap( paste_xpm );
321 bitmaps[6] = new wxBitmap( print_xpm );
322 bitmaps[7] = new wxBitmap( help_xpm );
c52d95b4 323
c52d95b4 324 int width = 24;
c52d95b4
VZ
325 int currentX = 5;
326
422d0ff0 327 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("New file"));
c52d95b4 328 currentX += width + 5;
422d0ff0 329 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Open file"));
c52d95b4 330 currentX += width + 5;
422d0ff0 331 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Save file"));
c52d95b4
VZ
332 currentX += width + 5;
333 toolBar->AddSeparator();
422d0ff0 334 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Copy"));
c52d95b4 335 currentX += width + 5;
422d0ff0 336 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Cut"));
c52d95b4 337 currentX += width + 5;
422d0ff0 338 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Paste"));
c52d95b4
VZ
339 currentX += width + 5;
340 toolBar->AddSeparator();
422d0ff0 341 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, false, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Print"));
c52d95b4
VZ
342 currentX += width + 5;
343 toolBar->AddSeparator();
422d0ff0 344 toolBar->AddTool( MDI_ABOUT, *bitmaps[7], wxNullBitmap, true, currentX, wxDefaultCoord, (wxObject *) NULL, _T("Help"));
c52d95b4
VZ
345
346 toolBar->Realize();
347
348 int i;
349 for (i = 0; i < 8; i++)
350 delete bitmaps[i];
351}
6ceba174 352#endif // wxUSE_TOOLBAR
c52d95b4
VZ
353
354// ---------------------------------------------------------------------------
355// MyCanvas
356// ---------------------------------------------------------------------------
c801d85f
KB
357
358// Define a constructor for my canvas
c52d95b4 359MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
eb839c84 360 : wxScrolledWindow(parent, wxID_ANY, pos, size,
3ebcfb76
VZ
361 wxSUNKEN_BORDER |
362 wxNO_FULL_REPAINT_ON_RESIZE |
363 wxVSCROLL | wxHSCROLL)
c801d85f 364{
42ed7532 365 SetBackgroundColour(wxColour(_T("WHITE")));
c52d95b4 366
eb839c84 367 m_dirty = false;
c801d85f
KB
368}
369
370// Define the repainting behaviour
371void MyCanvas::OnDraw(wxDC& dc)
372{
c52d95b4
VZ
373 dc.SetFont(*wxSWISS_FONT);
374 dc.SetPen(*wxGREEN_PEN);
375 dc.DrawLine(0, 0, 200, 200);
376 dc.DrawLine(200, 0, 0, 200);
377
378 dc.SetBrush(*wxCYAN_BRUSH);
379 dc.SetPen(*wxRED_PEN);
380 dc.DrawRectangle(100, 100, 100, 50);
381 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
382
383 dc.DrawEllipse(250, 250, 100, 50);
c41ea66a 384#if wxUSE_SPLINES
c52d95b4 385 dc.DrawSpline(50, 200, 50, 100, 200, 10);
c41ea66a 386#endif // wxUSE_SPLINES
c52d95b4 387 dc.DrawLine(50, 230, 200, 230);
42ed7532 388 dc.DrawText(_T("This is a test string"), 50, 230);
c52d95b4
VZ
389
390 wxPoint points[3];
391 points[0].x = 200; points[0].y = 300;
392 points[1].x = 100; points[1].y = 400;
393 points[2].x = 300; points[2].y = 400;
394
395 dc.DrawPolygon(3, points);
c801d85f
KB
396}
397
c52d95b4
VZ
398// This implements a tiny doodling program! Drag the mouse using the left
399// button.
c801d85f
KB
400void MyCanvas::OnEvent(wxMouseEvent& event)
401{
c52d95b4
VZ
402 wxClientDC dc(this);
403 PrepareDC(dc);
c801d85f 404
c52d95b4 405 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 406
c52d95b4
VZ
407 if (xpos > -1 && ypos > -1 && event.Dragging())
408 {
409 dc.SetPen(*wxBLACK_PEN);
410 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 411
eb839c84 412 m_dirty = true;
c52d95b4 413 }
c801d85f 414
c52d95b4
VZ
415 xpos = pt.x;
416 ypos = pt.y;
417}
418
419// ---------------------------------------------------------------------------
420// MyChild
421// ---------------------------------------------------------------------------
422
eb839c84
WS
423MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title)
424 : wxMDIChildFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
425 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 426{
c52d95b4
VZ
427 canvas = (MyCanvas *) NULL;
428 my_children.Append(this);
3ebcfb76
VZ
429
430 // this should work for MDI frames as well as for normal ones
431 SetSizeHints(100, 100);
c801d85f
KB
432}
433
c52d95b4 434MyChild::~MyChild()
c801d85f 435{
c52d95b4 436 my_children.DeleteObject(this);
c801d85f
KB
437}
438
33d0b396 439void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 440{
eb839c84 441 Close(true);
c52d95b4
VZ
442}
443
f6bcfd97
BP
444void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
445{
446 if ( canvas )
447 canvas->Refresh();
448}
449
fa762db4
VZ
450void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
451{
452 Move(10, 10);
453}
454
455void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
456{
457 SetClientSize(100, 100);
458}
459
f6bcfd97 460void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 461{
2b5f62a0 462//#if wxUSE_TEXTDLG
f6bcfd97
BP
463 static wxString s_title = _T("Canvas Frame");
464
465 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
466 _T("MDI sample question"),
467 s_title,
fa762db4 468 GetParent()->GetParent());
f6bcfd97
BP
469 if ( !title )
470 return;
471
472 s_title = title;
473 SetTitle(s_title);
2b5f62a0 474//#endif
c801d85f
KB
475}
476
477void MyChild::OnActivate(wxActivateEvent& event)
478{
c52d95b4
VZ
479 if ( event.GetActive() && canvas )
480 canvas->SetFocus();
c801d85f
KB
481}
482
fa762db4
VZ
483void MyChild::OnMove(wxMoveEvent& event)
484{
485 // VZ: here everything is totally wrong under MSW, the positions are
486 // different and both wrong (pos2 is off by 2 pixels for me which seems
487 // to be the width of the MDI canvas border)
488 wxPoint pos1 = event.GetPosition(),
489 pos2 = GetPosition();
4693b20c 490 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
fa762db4
VZ
491 pos1.x, pos1.y, pos2.x, pos2.y);
492
493 event.Skip();
494}
495
496void MyChild::OnSize(wxSizeEvent& event)
497{
498 // VZ: under MSW the size event carries the client size (quite
499 // unexpectedly) *except* for the very first one which has the full
500 // size... what should it really be? TODO: check under wxGTK
501 wxSize size1 = event.GetSize(),
502 size2 = GetSize(),
503 size3 = GetClientSize();
4693b20c 504 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
fa762db4
VZ
505 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
506
507 event.Skip();
508}
509
c52d95b4 510void MyChild::OnClose(wxCloseEvent& event)
c801d85f 511{
c52d95b4
VZ
512 if ( canvas && canvas->IsDirty() )
513 {
42ed7532 514 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
c52d95b4
VZ
515 wxICON_QUESTION | wxYES_NO) != wxYES )
516 {
517 event.Veto();
c801d85f 518
c52d95b4
VZ
519 return;
520 }
521 }
c801d85f 522
c52d95b4
VZ
523 gs_nFrames--;
524
525 event.Skip();
c801d85f
KB
526}
527
c52d95b4 528