]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
-1->wxID_ANY, TRUE->true and FALSE->false replacements.
[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)
75
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
42ed7532 112 frame = new MyFrame((wxFrame *)NULL, -1, _T("MDI Demo"),
c52d95b4
VZ
113 wxPoint(-1, -1), wxSize(500, 400),
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
c52d95b4 148 frame->CreateStatusBar();
c801d85f 149
c52d95b4 150 frame->Show(TRUE);
c801d85f 151
c52d95b4 152 SetTopWindow(frame);
c801d85f 153
c52d95b4 154 return TRUE;
c801d85f
KB
155}
156
c52d95b4
VZ
157// ---------------------------------------------------------------------------
158// MyFrame
159// ---------------------------------------------------------------------------
c801d85f
KB
160
161// Define my frame constructor
c52d95b4
VZ
162MyFrame::MyFrame(wxWindow *parent,
163 const wxWindowID id,
164 const wxString& title,
165 const wxPoint& pos,
166 const wxSize& size,
167 const long style)
3ebcfb76
VZ
168 : wxMDIParentFrame(parent, id, title, pos, size,
169 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 170{
42ed7532 171 textWindow = new wxTextCtrl(this, -1, _T("A help window"),
c52d95b4
VZ
172 wxDefaultPosition, wxDefaultSize,
173 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 174
6ceba174 175#if wxUSE_TOOLBAR
fa762db4 176 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 177 InitToolBar(GetToolBar());
6ceba174 178#endif // wxUSE_TOOLBAR
57a7b7c1 179
57a7b7c1
JS
180 // Accelerators
181 wxAcceleratorEntry entries[3];
182 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
183 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
184 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
185 wxAcceleratorTable accel(3, entries);
186 SetAcceleratorTable(accel);
c801d85f
KB
187}
188
c52d95b4
VZ
189void MyFrame::OnClose(wxCloseEvent& event)
190{
191 if ( event.CanVeto() && (gs_nFrames > 0) )
192 {
193 wxString msg;
ddb6bc71 194 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
42ed7532 195 if ( wxMessageBox(msg, _T("Please confirm"),
c52d95b4
VZ
196 wxICON_QUESTION | wxYES_NO) != wxYES )
197 {
198 event.Veto();
199
200 return;
201 }
202 }
203
204 event.Skip();
205}
206
207void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 208{
c52d95b4 209 Close();
c801d85f
KB
210}
211
e3e65dac 212void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 213{
42ed7532
MB
214 (void)wxMessageBox(_T("wxWindows 2.0 MDI Demo\n")
215 _T("Author: Julian Smart (c) 1997\n")
216 _T("Usage: mdi.exe"), _T("About MDI Demo"));
c801d85f
KB
217}
218
e3e65dac 219void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 220{
c52d95b4 221 // Make another frame, containing a canvas
42ed7532 222 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
c52d95b4
VZ
223 wxPoint(-1, -1), wxSize(-1, -1),
224 wxDEFAULT_FRAME_STYLE);
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
c41ea66a
VZ
265 subframe->CreateStatusBar();
266 subframe->SetStatusText(title);
267
c52d95b4
VZ
268 int width, height;
269 subframe->GetClientSize(&width, &height);
270 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
271 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
272 subframe->canvas = canvas;
c801d85f 273
c52d95b4
VZ
274 // Give it scrollbars
275 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 276
c52d95b4 277 subframe->Show(TRUE);
c801d85f
KB
278}
279
5f58391f
JS
280void MyFrame::OnSize(wxSizeEvent&
281 #ifdef __WXUNIVERSAL__
282 event
283 #else
284 WXUNUSED(event)
285 #endif
286 )
c52d95b4
VZ
287{
288 int w, h;
289 GetClientSize(&w, &h);
290
291 textWindow->SetSize(0, 0, 200, h);
292 GetClientWindow()->SetSize(200, 0, w - 200, h);
a9c824e9
JS
293
294 // FIXME: On wxX11, we need the MDI frame to process this
295 // event, but on other platforms this should not
296 // be done.
2b5f62a0 297#ifdef __WXUNIVERSAL__
b9f933ab 298 event.Skip();
a9c824e9 299#endif
c52d95b4
VZ
300}
301
6ceba174 302#if wxUSE_TOOLBAR
c52d95b4
VZ
303void MyFrame::InitToolBar(wxToolBar* toolBar)
304{
305 wxBitmap* bitmaps[8];
306
c52d95b4
VZ
307 bitmaps[0] = new wxBitmap( new_xpm );
308 bitmaps[1] = new wxBitmap( open_xpm );
309 bitmaps[2] = new wxBitmap( save_xpm );
310 bitmaps[3] = new wxBitmap( copy_xpm );
311 bitmaps[4] = new wxBitmap( cut_xpm );
312 bitmaps[5] = new wxBitmap( paste_xpm );
313 bitmaps[6] = new wxBitmap( print_xpm );
314 bitmaps[7] = new wxBitmap( help_xpm );
c52d95b4 315
c52d95b4 316 int width = 24;
c52d95b4
VZ
317 int currentX = 5;
318
42ed7532 319 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("New file"));
c52d95b4 320 currentX += width + 5;
42ed7532 321 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Open file"));
c52d95b4 322 currentX += width + 5;
42ed7532 323 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Save file"));
c52d95b4
VZ
324 currentX += width + 5;
325 toolBar->AddSeparator();
42ed7532 326 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Copy"));
c52d95b4 327 currentX += width + 5;
42ed7532 328 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Cut"));
c52d95b4 329 currentX += width + 5;
42ed7532 330 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Paste"));
c52d95b4
VZ
331 currentX += width + 5;
332 toolBar->AddSeparator();
42ed7532 333 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, _T("Print"));
c52d95b4
VZ
334 currentX += width + 5;
335 toolBar->AddSeparator();
42ed7532 336 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, _T("Help"));
c52d95b4
VZ
337
338 toolBar->Realize();
339
340 int i;
341 for (i = 0; i < 8; i++)
342 delete bitmaps[i];
343}
6ceba174 344#endif // wxUSE_TOOLBAR
c52d95b4
VZ
345
346// ---------------------------------------------------------------------------
347// MyCanvas
348// ---------------------------------------------------------------------------
c801d85f
KB
349
350// Define a constructor for my canvas
c52d95b4
VZ
351MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
352 : wxScrolledWindow(parent, -1, pos, size,
3ebcfb76
VZ
353 wxSUNKEN_BORDER |
354 wxNO_FULL_REPAINT_ON_RESIZE |
355 wxVSCROLL | wxHSCROLL)
c801d85f 356{
42ed7532 357 SetBackgroundColour(wxColour(_T("WHITE")));
c52d95b4
VZ
358
359 m_dirty = FALSE;
c801d85f
KB
360}
361
362// Define the repainting behaviour
363void MyCanvas::OnDraw(wxDC& dc)
364{
c52d95b4
VZ
365 dc.SetFont(*wxSWISS_FONT);
366 dc.SetPen(*wxGREEN_PEN);
367 dc.DrawLine(0, 0, 200, 200);
368 dc.DrawLine(200, 0, 0, 200);
369
370 dc.SetBrush(*wxCYAN_BRUSH);
371 dc.SetPen(*wxRED_PEN);
372 dc.DrawRectangle(100, 100, 100, 50);
373 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
374
375 dc.DrawEllipse(250, 250, 100, 50);
c41ea66a 376#if wxUSE_SPLINES
c52d95b4 377 dc.DrawSpline(50, 200, 50, 100, 200, 10);
c41ea66a 378#endif // wxUSE_SPLINES
c52d95b4 379 dc.DrawLine(50, 230, 200, 230);
42ed7532 380 dc.DrawText(_T("This is a test string"), 50, 230);
c52d95b4
VZ
381
382 wxPoint points[3];
383 points[0].x = 200; points[0].y = 300;
384 points[1].x = 100; points[1].y = 400;
385 points[2].x = 300; points[2].y = 400;
386
387 dc.DrawPolygon(3, points);
c801d85f
KB
388}
389
c52d95b4
VZ
390// This implements a tiny doodling program! Drag the mouse using the left
391// button.
c801d85f
KB
392void MyCanvas::OnEvent(wxMouseEvent& event)
393{
c52d95b4
VZ
394 wxClientDC dc(this);
395 PrepareDC(dc);
c801d85f 396
c52d95b4 397 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 398
c52d95b4
VZ
399 if (xpos > -1 && ypos > -1 && event.Dragging())
400 {
401 dc.SetPen(*wxBLACK_PEN);
402 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 403
c52d95b4
VZ
404 m_dirty = TRUE;
405 }
c801d85f 406
c52d95b4
VZ
407 xpos = pt.x;
408 ypos = pt.y;
409}
410
411// ---------------------------------------------------------------------------
412// MyChild
413// ---------------------------------------------------------------------------
414
415MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
416 const wxPoint& pos, const wxSize& size,
417 const long style)
3ebcfb76
VZ
418 : wxMDIChildFrame(parent, -1, title, pos, size,
419 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 420{
c52d95b4
VZ
421 canvas = (MyCanvas *) NULL;
422 my_children.Append(this);
3ebcfb76
VZ
423
424 // this should work for MDI frames as well as for normal ones
425 SetSizeHints(100, 100);
c801d85f
KB
426}
427
c52d95b4 428MyChild::~MyChild()
c801d85f 429{
c52d95b4 430 my_children.DeleteObject(this);
c801d85f
KB
431}
432
33d0b396 433void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 434{
c52d95b4
VZ
435 Close(TRUE);
436}
437
f6bcfd97
BP
438void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
439{
440 if ( canvas )
441 canvas->Refresh();
442}
443
fa762db4
VZ
444void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
445{
446 Move(10, 10);
447}
448
449void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
450{
451 SetClientSize(100, 100);
452}
453
f6bcfd97 454void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 455{
2b5f62a0 456//#if wxUSE_TEXTDLG
f6bcfd97
BP
457 static wxString s_title = _T("Canvas Frame");
458
459 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
460 _T("MDI sample question"),
461 s_title,
fa762db4 462 GetParent()->GetParent());
f6bcfd97
BP
463 if ( !title )
464 return;
465
466 s_title = title;
467 SetTitle(s_title);
2b5f62a0 468//#endif
c801d85f
KB
469}
470
471void MyChild::OnActivate(wxActivateEvent& event)
472{
c52d95b4
VZ
473 if ( event.GetActive() && canvas )
474 canvas->SetFocus();
c801d85f
KB
475}
476
fa762db4
VZ
477void MyChild::OnMove(wxMoveEvent& event)
478{
479 // VZ: here everything is totally wrong under MSW, the positions are
480 // different and both wrong (pos2 is off by 2 pixels for me which seems
481 // to be the width of the MDI canvas border)
482 wxPoint pos1 = event.GetPosition(),
483 pos2 = GetPosition();
4693b20c 484 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
fa762db4
VZ
485 pos1.x, pos1.y, pos2.x, pos2.y);
486
487 event.Skip();
488}
489
490void MyChild::OnSize(wxSizeEvent& event)
491{
492 // VZ: under MSW the size event carries the client size (quite
493 // unexpectedly) *except* for the very first one which has the full
494 // size... what should it really be? TODO: check under wxGTK
495 wxSize size1 = event.GetSize(),
496 size2 = GetSize(),
497 size3 = GetClientSize();
4693b20c 498 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
fa762db4
VZ
499 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
500
501 event.Skip();
502}
503
c52d95b4 504void MyChild::OnClose(wxCloseEvent& event)
c801d85f 505{
c52d95b4
VZ
506 if ( canvas && canvas->IsDirty() )
507 {
42ed7532 508 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
c52d95b4
VZ
509 wxICON_QUESTION | wxYES_NO) != wxYES )
510 {
511 event.Veto();
c801d85f 512
c52d95b4
VZ
513 return;
514 }
515 }
c801d85f 516
c52d95b4
VZ
517 gs_nFrames--;
518
519 event.Skip();
c801d85f
KB
520}
521
c52d95b4 522