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