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