]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
wx.FRAME_FLOAT_ON_PARENT notes
[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__)
ea7fb468
VZ
35 #include "../sample.xpm"
36 #include "chart.xpm"
716b7364
RR
37#endif
38
d04d32c4
CE
39#include "bitmaps/new.xpm"
40#include "bitmaps/open.xpm"
41#include "bitmaps/save.xpm"
42#include "bitmaps/copy.xpm"
43#include "bitmaps/cut.xpm"
44#include "bitmaps/paste.xpm"
45#include "bitmaps/print.xpm"
46#include "bitmaps/help.xpm"
47
48
c801d85f
KB
49#include "mdi.h"
50
c52d95b4
VZ
51IMPLEMENT_APP(MyApp)
52
53// ---------------------------------------------------------------------------
54// global variables
55// ---------------------------------------------------------------------------
56
c67daf87 57MyFrame *frame = (MyFrame *) NULL;
c801d85f
KB
58wxList my_children;
59
c801d85f 60// For drawing lines in a canvas
c52d95b4
VZ
61static long xpos = -1;
62static long ypos = -1;
63
64static int gs_nFrames = 0;
65
66// ---------------------------------------------------------------------------
67// event tables
68// ---------------------------------------------------------------------------
69
70BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
71 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
72 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
73 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
74
75 EVT_CLOSE(MyFrame::OnClose)
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
c9f856af
VZ
89#if wxUSE_CLIPBOARD
90 EVT_MENU(wxID_PASTE, MyChild::OnPaste)
91 EVT_UPDATE_UI(wxID_PASTE, MyChild::OnUpdatePaste)
92#endif // wxUSE_CLIPBOARD
93
3d8dea7e
VZ
94 EVT_SIZE(MyChild::OnSize)
95 EVT_MOVE(MyChild::OnMove)
c52d95b4
VZ
96
97 EVT_CLOSE(MyChild::OnClose)
98END_EVENT_TABLE()
99
100BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
101 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
102END_EVENT_TABLE()
103
104// ===========================================================================
105// implementation
106// ===========================================================================
c801d85f 107
c52d95b4
VZ
108// ---------------------------------------------------------------------------
109// MyApp
110// ---------------------------------------------------------------------------
c801d85f
KB
111
112// Initialise this in OnInit, not statically
c52d95b4 113bool MyApp::OnInit()
c801d85f 114{
c52d95b4 115 // Create the main frame window
c801d85f 116
eb839c84
WS
117 frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("MDI Demo"),
118 wxDefaultPosition, wxSize(500, 400),
c52d95b4 119 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
df61c009
JS
120#if 0
121 // Experimental: change the window menu
122 wxMenu* windowMenu = new wxMenu;
42ed7532 123 windowMenu->Append(5000, _T("My menu item!"));
df61c009 124 frame->SetWindowMenu(windowMenu);
df61c009 125#endif
c801d85f 126
c52d95b4 127 // Give it an icon
e21d8614 128 frame->SetIcon(wxICON(sample));
c801d85f 129
c52d95b4
VZ
130 // Make a menubar
131 wxMenu *file_menu = new wxMenu;
c801d85f 132
42ed7532
MB
133 file_menu->Append(MDI_NEW_WINDOW, _T("&New window\tCtrl-N"), _T("Create a new child window"));
134 file_menu->Append(MDI_QUIT, _T("&Exit\tAlt-X"), _T("Quit the program"));
c801d85f 135
c52d95b4 136 wxMenu *help_menu = new wxMenu;
42ed7532 137 help_menu->Append(MDI_ABOUT, _T("&About\tF1"));
c801d85f 138
c52d95b4 139 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 140
42ed7532
MB
141 menu_bar->Append(file_menu, _T("&File"));
142 menu_bar->Append(help_menu, _T("&Help"));
c801d85f 143
c52d95b4
VZ
144 // Associate the menu bar with the frame
145 frame->SetMenuBar(menu_bar);
c801d85f 146
8520f137 147#if wxUSE_STATUSBAR
c52d95b4 148 frame->CreateStatusBar();
8520f137 149#endif // wxUSE_STATUSBAR
c801d85f 150
eb839c84 151 frame->Show(true);
c801d85f 152
c52d95b4 153 SetTopWindow(frame);
c801d85f 154
eb839c84 155 return true;
c801d85f
KB
156}
157
c52d95b4
VZ
158// ---------------------------------------------------------------------------
159// MyFrame
160// ---------------------------------------------------------------------------
c801d85f
KB
161
162// Define my frame constructor
c52d95b4
VZ
163MyFrame::MyFrame(wxWindow *parent,
164 const wxWindowID id,
165 const wxString& title,
166 const wxPoint& pos,
167 const wxSize& size,
168 const long style)
e21d8614 169 : wxMDIParentFrame(parent, id, title, pos, size, style)
c801d85f 170{
eb839c84 171 textWindow = new wxTextCtrl(this, wxID_ANY, _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
4219b5e7 180#if wxUSE_ACCEL
57a7b7c1
JS
181 // Accelerators
182 wxAcceleratorEntry entries[3];
183 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
184 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
185 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
186 wxAcceleratorTable accel(3, entries);
187 SetAcceleratorTable(accel);
4219b5e7 188#endif // wxUSE_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
e21d8614 232 subframe->SetIcon(wxICON(chart));
c801d85f 233
290c74fb 234#if wxUSE_MENUS
c52d95b4
VZ
235 // Make a menubar
236 wxMenu *file_menu = new wxMenu;
237
42ed7532
MB
238 file_menu->Append(MDI_NEW_WINDOW, _T("&New window"));
239 file_menu->Append(MDI_CHILD_QUIT, _T("&Close child"), _T("Close this window"));
240 file_menu->Append(MDI_QUIT, _T("&Exit"));
c801d85f 241
c52d95b4 242 wxMenu *option_menu = new wxMenu;
c801d85f 243
42ed7532
MB
244 option_menu->Append(MDI_REFRESH, _T("&Refresh picture"));
245 option_menu->Append(MDI_CHANGE_TITLE, _T("Change &title...\tCtrl-T"));
fa762db4 246 option_menu->AppendSeparator();
42ed7532
MB
247 option_menu->Append(MDI_CHANGE_POSITION, _T("Move frame\tCtrl-M"));
248 option_menu->Append(MDI_CHANGE_SIZE, _T("Resize frame\tCtrl-S"));
c9f856af
VZ
249#if wxUSE_CLIPBOARD
250 option_menu->AppendSeparator();
251 option_menu->Append(wxID_PASTE, _T("Copy text from clipboard\tCtrl-V"));
252#endif // wxUSE_CLIPBOARD
c801d85f 253
c52d95b4 254 wxMenu *help_menu = new wxMenu;
42ed7532 255 help_menu->Append(MDI_ABOUT, _T("&About"));
c801d85f 256
c52d95b4 257 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 258
42ed7532
MB
259 menu_bar->Append(file_menu, _T("&File"));
260 menu_bar->Append(option_menu, _T("&Child"));
261 menu_bar->Append(help_menu, _T("&Help"));
c801d85f 262
c52d95b4
VZ
263 // Associate the menu bar with the frame
264 subframe->SetMenuBar(menu_bar);
290c74fb 265#endif // wxUSE_MENUS
c801d85f 266
8520f137 267#if wxUSE_STATUSBAR
c41ea66a
VZ
268 subframe->CreateStatusBar();
269 subframe->SetStatusText(title);
8520f137 270#endif // wxUSE_STATUSBAR
c41ea66a 271
c52d95b4
VZ
272 int width, height;
273 subframe->GetClientSize(&width, &height);
274 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
275 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
276 subframe->canvas = canvas;
c801d85f 277
c52d95b4
VZ
278 // Give it scrollbars
279 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 280
eb839c84 281 subframe->Show(true);
c801d85f
KB
282}
283
4219b5e7 284void MyFrame::OnSize(wxSizeEvent&
5f58391f
JS
285 #ifdef __WXUNIVERSAL__
286 event
287 #else
288 WXUNUSED(event)
289 #endif
290 )
c52d95b4
VZ
291{
292 int w, h;
293 GetClientSize(&w, &h);
294
295 textWindow->SetSize(0, 0, 200, h);
296 GetClientWindow()->SetSize(200, 0, w - 200, h);
a9c824e9
JS
297
298 // FIXME: On wxX11, we need the MDI frame to process this
299 // event, but on other platforms this should not
300 // be done.
4219b5e7 301#ifdef __WXUNIVERSAL__
b9f933ab 302 event.Skip();
a9c824e9 303#endif
c52d95b4
VZ
304}
305
6ceba174 306#if wxUSE_TOOLBAR
c52d95b4
VZ
307void MyFrame::InitToolBar(wxToolBar* toolBar)
308{
e21d8614
VZ
309 wxBitmap bitmaps[8];
310
311 bitmaps[0] = wxBitmap( new_xpm );
312 bitmaps[1] = wxBitmap( open_xpm );
313 bitmaps[2] = wxBitmap( save_xpm );
314 bitmaps[3] = wxBitmap( copy_xpm );
315 bitmaps[4] = wxBitmap( cut_xpm );
316 bitmaps[5] = wxBitmap( paste_xpm );
317 bitmaps[6] = wxBitmap( print_xpm );
318 bitmaps[7] = wxBitmap( help_xpm );
319
320 toolBar->AddTool(MDI_NEW_WINDOW, _T("New"), bitmaps[0], _T("New file"));
321 toolBar->AddTool(1, _T("Open"), bitmaps[1], _T("Open file"));
322 toolBar->AddTool(2, _T("Save"), bitmaps[2], _T("Save file"));
c52d95b4 323 toolBar->AddSeparator();
e21d8614
VZ
324 toolBar->AddTool(3, _T("Copy"), bitmaps[3], _T("Copy"));
325 toolBar->AddTool(4, _T("Cut"), bitmaps[4], _T("Cut"));
326 toolBar->AddTool(5, _T("Paste"), bitmaps[5], _T("Paste"));
c52d95b4 327 toolBar->AddSeparator();
e21d8614 328 toolBar->AddTool(6, _T("Print"), bitmaps[6], _T("Print"));
c52d95b4 329 toolBar->AddSeparator();
e21d8614 330 toolBar->AddTool(MDI_ABOUT, _T("About"), bitmaps[7], _T("Help"));
c52d95b4
VZ
331
332 toolBar->Realize();
c52d95b4 333}
6ceba174 334#endif // wxUSE_TOOLBAR
c52d95b4
VZ
335
336// ---------------------------------------------------------------------------
337// MyCanvas
338// ---------------------------------------------------------------------------
c801d85f
KB
339
340// Define a constructor for my canvas
c52d95b4 341MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
eb839c84 342 : wxScrolledWindow(parent, wxID_ANY, pos, size,
3ebcfb76
VZ
343 wxSUNKEN_BORDER |
344 wxNO_FULL_REPAINT_ON_RESIZE |
345 wxVSCROLL | wxHSCROLL)
c801d85f 346{
42ed7532 347 SetBackgroundColour(wxColour(_T("WHITE")));
c52d95b4 348
eb839c84 349 m_dirty = false;
c801d85f
KB
350}
351
352// Define the repainting behaviour
353void MyCanvas::OnDraw(wxDC& dc)
354{
c9f856af
VZ
355 if ( !m_text.empty() )
356 dc.DrawText(m_text, 10, 10);
357
c52d95b4
VZ
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);
c41ea66a 369#if wxUSE_SPLINES
c52d95b4 370 dc.DrawSpline(50, 200, 50, 100, 200, 10);
c41ea66a 371#endif // wxUSE_SPLINES
c52d95b4 372 dc.DrawLine(50, 230, 200, 230);
42ed7532 373 dc.DrawText(_T("This is a test string"), 50, 230);
c52d95b4
VZ
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);
c801d85f
KB
381}
382
c52d95b4
VZ
383// This implements a tiny doodling program! Drag the mouse using the left
384// button.
c801d85f
KB
385void MyCanvas::OnEvent(wxMouseEvent& event)
386{
c52d95b4
VZ
387 wxClientDC dc(this);
388 PrepareDC(dc);
c801d85f 389
c52d95b4 390 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 391
c52d95b4
VZ
392 if (xpos > -1 && ypos > -1 && event.Dragging())
393 {
394 dc.SetPen(*wxBLACK_PEN);
395 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 396
eb839c84 397 m_dirty = true;
c52d95b4 398 }
c801d85f 399
c52d95b4
VZ
400 xpos = pt.x;
401 ypos = pt.y;
402}
403
404// ---------------------------------------------------------------------------
405// MyChild
406// ---------------------------------------------------------------------------
407
eb839c84
WS
408MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title)
409 : wxMDIChildFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
410 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 411{
c52d95b4
VZ
412 canvas = (MyCanvas *) NULL;
413 my_children.Append(this);
3ebcfb76
VZ
414
415 // this should work for MDI frames as well as for normal ones
416 SetSizeHints(100, 100);
c801d85f
KB
417}
418
c52d95b4 419MyChild::~MyChild()
c801d85f 420{
c52d95b4 421 my_children.DeleteObject(this);
c801d85f
KB
422}
423
33d0b396 424void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 425{
eb839c84 426 Close(true);
c52d95b4
VZ
427}
428
f6bcfd97
BP
429void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
430{
431 if ( canvas )
432 canvas->Refresh();
433}
434
fa762db4
VZ
435void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
436{
437 Move(10, 10);
438}
439
440void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
441{
442 SetClientSize(100, 100);
443}
444
f6bcfd97 445void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 446{
e21d8614 447#if wxUSE_TEXTDLG
f6bcfd97
BP
448 static wxString s_title = _T("Canvas Frame");
449
450 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
451 _T("MDI sample question"),
452 s_title,
fa762db4 453 GetParent()->GetParent());
f6bcfd97
BP
454 if ( !title )
455 return;
456
457 s_title = title;
458 SetTitle(s_title);
e21d8614 459#endif // wxUSE_TEXTDLG
c801d85f
KB
460}
461
462void MyChild::OnActivate(wxActivateEvent& event)
463{
c52d95b4
VZ
464 if ( event.GetActive() && canvas )
465 canvas->SetFocus();
c801d85f
KB
466}
467
fa762db4
VZ
468void MyChild::OnMove(wxMoveEvent& event)
469{
470 // VZ: here everything is totally wrong under MSW, the positions are
471 // different and both wrong (pos2 is off by 2 pixels for me which seems
472 // to be the width of the MDI canvas border)
473 wxPoint pos1 = event.GetPosition(),
474 pos2 = GetPosition();
4693b20c 475 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
fa762db4
VZ
476 pos1.x, pos1.y, pos2.x, pos2.y);
477
478 event.Skip();
479}
480
481void MyChild::OnSize(wxSizeEvent& event)
482{
483 // VZ: under MSW the size event carries the client size (quite
484 // unexpectedly) *except* for the very first one which has the full
485 // size... what should it really be? TODO: check under wxGTK
486 wxSize size1 = event.GetSize(),
487 size2 = GetSize(),
488 size3 = GetClientSize();
4693b20c 489 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
fa762db4
VZ
490 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
491
492 event.Skip();
493}
494
c52d95b4 495void MyChild::OnClose(wxCloseEvent& event)
c801d85f 496{
c52d95b4
VZ
497 if ( canvas && canvas->IsDirty() )
498 {
42ed7532 499 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
c52d95b4
VZ
500 wxICON_QUESTION | wxYES_NO) != wxYES )
501 {
502 event.Veto();
c801d85f 503
c52d95b4
VZ
504 return;
505 }
506 }
c801d85f 507
c52d95b4
VZ
508 gs_nFrames--;
509
510 event.Skip();
c801d85f 511}
c9f856af
VZ
512
513#if wxUSE_CLIPBOARD
514
515#include "wx/clipbrd.h"
516
517void MyChild::OnPaste(wxCommandEvent& WXUNUSED(event))
518{
519 wxClipboardLocker lock;
520 wxTextDataObject data;
37f66b04 521 canvas->SetText(wxTheClipboard->GetData(data) ? data.GetText().c_str()
c9f856af
VZ
522 : _T("No text on clipboard"));
523}
524
525void MyChild::OnUpdatePaste(wxUpdateUIEvent& event)
526{
527 wxClipboardLocker lock;
528 event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
529}
530
531#endif // wxUSE_CLIPBOARD