Regenerated file lists so features.h would be installed
[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$
8// Copyright: (c) Julian Smart and Markus Holzem
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
e70d4ce8 34#if defined(__WXGTK__) || defined(__WXMOTIF__) || defined(__WXMAC__)
c52d95b4
VZ
35 #include "mondrian.xpm"
36 #include "bitmaps/new.xpm"
37 #include "bitmaps/open.xpm"
38 #include "bitmaps/save.xpm"
39 #include "bitmaps/copy.xpm"
40 #include "bitmaps/cut.xpm"
41 #include "bitmaps/paste.xpm"
42 #include "bitmaps/print.xpm"
43 #include "bitmaps/help.xpm"
716b7364
RR
44#endif
45
c801d85f
KB
46#include "mdi.h"
47
c52d95b4
VZ
48IMPLEMENT_APP(MyApp)
49
50// ---------------------------------------------------------------------------
51// global variables
52// ---------------------------------------------------------------------------
53
c67daf87 54MyFrame *frame = (MyFrame *) NULL;
c801d85f
KB
55wxList my_children;
56
c801d85f 57// For drawing lines in a canvas
c52d95b4
VZ
58static long xpos = -1;
59static long ypos = -1;
60
61static int gs_nFrames = 0;
62
63// ---------------------------------------------------------------------------
64// event tables
65// ---------------------------------------------------------------------------
66
67BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
68 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
69 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
70 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
71
72 EVT_CLOSE(MyFrame::OnClose)
73
74 EVT_SIZE(MyFrame::OnSize)
75END_EVENT_TABLE()
76
77// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
78// to the parent window for processing, so no need to
79// duplicate event handlers here.
80BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
81 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
82 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
f6bcfd97 83 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
3d8dea7e
VZ
84 EVT_MENU(MDI_CHANGE_POSITION, MyChild::OnChangePosition)
85 EVT_MENU(MDI_CHANGE_SIZE, MyChild::OnChangeSize)
86
3d8dea7e
VZ
87 EVT_SIZE(MyChild::OnSize)
88 EVT_MOVE(MyChild::OnMove)
c52d95b4
VZ
89
90 EVT_CLOSE(MyChild::OnClose)
91END_EVENT_TABLE()
92
93BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
94 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
95END_EVENT_TABLE()
96
97// ===========================================================================
98// implementation
99// ===========================================================================
c801d85f 100
c52d95b4
VZ
101// ---------------------------------------------------------------------------
102// MyApp
103// ---------------------------------------------------------------------------
c801d85f
KB
104
105// Initialise this in OnInit, not statically
c52d95b4 106bool MyApp::OnInit()
c801d85f 107{
c52d95b4 108 // Create the main frame window
c801d85f 109
c52d95b4
VZ
110 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
111 wxPoint(-1, -1), wxSize(500, 400),
112 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
df61c009
JS
113#ifdef __WXMSW__
114#if 0
115 // Experimental: change the window menu
116 wxMenu* windowMenu = new wxMenu;
117 windowMenu->Append(5000, "My menu item!");
118 frame->SetWindowMenu(windowMenu);
119#endif
120#endif
c801d85f 121
c52d95b4 122 // Give it an icon
2049ba38 123#ifdef __WXMSW__
c52d95b4 124 frame->SetIcon(wxIcon("mdi_icn"));
52cbfcf0 125#else
c52d95b4 126 frame->SetIcon(wxIcon( mondrian_xpm ));
c801d85f 127#endif
c801d85f 128
c52d95b4
VZ
129 // Make a menubar
130 wxMenu *file_menu = new wxMenu;
c801d85f 131
f6bcfd97
BP
132 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N", "Create a new child window");
133 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X", "Quit the program");
c801d85f 134
c52d95b4 135 wxMenu *help_menu = new wxMenu;
f6bcfd97 136 help_menu->Append(MDI_ABOUT, "&About\tF1");
c801d85f 137
c52d95b4 138 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 139
c52d95b4
VZ
140 menu_bar->Append(file_menu, "&File");
141 menu_bar->Append(help_menu, "&Help");
c801d85f 142
c52d95b4
VZ
143 // Associate the menu bar with the frame
144 frame->SetMenuBar(menu_bar);
c801d85f 145
c52d95b4 146 frame->CreateStatusBar();
c801d85f 147
c52d95b4 148 frame->Show(TRUE);
c801d85f 149
c52d95b4 150 SetTopWindow(frame);
c801d85f 151
c52d95b4 152 return TRUE;
c801d85f
KB
153}
154
c52d95b4
VZ
155// ---------------------------------------------------------------------------
156// MyFrame
157// ---------------------------------------------------------------------------
c801d85f
KB
158
159// Define my frame constructor
c52d95b4
VZ
160MyFrame::MyFrame(wxWindow *parent,
161 const wxWindowID id,
162 const wxString& title,
163 const wxPoint& pos,
164 const wxSize& size,
165 const long style)
3ebcfb76
VZ
166 : wxMDIParentFrame(parent, id, title, pos, size,
167 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 168{
c52d95b4
VZ
169 textWindow = new wxTextCtrl(this, -1, "A help window",
170 wxDefaultPosition, wxDefaultSize,
171 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 172
fa762db4 173 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 174 InitToolBar(GetToolBar());
57a7b7c1 175
57a7b7c1
JS
176 // Accelerators
177 wxAcceleratorEntry entries[3];
178 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
179 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
180 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
181 wxAcceleratorTable accel(3, entries);
182 SetAcceleratorTable(accel);
c801d85f
KB
183}
184
c52d95b4
VZ
185void MyFrame::OnClose(wxCloseEvent& event)
186{
187 if ( event.CanVeto() && (gs_nFrames > 0) )
188 {
189 wxString msg;
ddb6bc71 190 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
c52d95b4
VZ
191 if ( wxMessageBox(msg, "Please confirm",
192 wxICON_QUESTION | wxYES_NO) != wxYES )
193 {
194 event.Veto();
195
196 return;
197 }
198 }
199
200 event.Skip();
201}
202
203void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 204{
c52d95b4 205 Close();
c801d85f
KB
206}
207
e3e65dac 208void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 209{
c52d95b4
VZ
210 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
211 "Author: Julian Smart (c) 1997\n"
212 "Usage: mdi.exe", "About MDI Demo");
c801d85f
KB
213}
214
e3e65dac 215void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 216{
c52d95b4
VZ
217 // Make another frame, containing a canvas
218 MyChild *subframe = new MyChild(frame, "Canvas Frame",
219 wxPoint(-1, -1), wxSize(-1, -1),
220 wxDEFAULT_FRAME_STYLE);
c801d85f 221
c52d95b4 222 wxString title;
ddb6bc71 223 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
c801d85f 224
c52d95b4
VZ
225 subframe->SetTitle(title);
226
227 // Give it an icon
2049ba38 228#ifdef __WXMSW__
c52d95b4 229 subframe->SetIcon(wxIcon("chrt_icn"));
8704bf74 230#else
c52d95b4 231 subframe->SetIcon(wxIcon( mondrian_xpm ));
c801d85f
KB
232#endif
233
c52d95b4
VZ
234 // Make a menubar
235 wxMenu *file_menu = new wxMenu;
236
fa762db4 237 file_menu->Append(MDI_NEW_WINDOW, "&New window");
c52d95b4 238 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
fa762db4 239 file_menu->Append(MDI_QUIT, "&Exit");
c801d85f 240
c52d95b4 241 wxMenu *option_menu = new wxMenu;
c801d85f 242
fa762db4 243 option_menu->Append(MDI_REFRESH, "&Refresh picture");
f6bcfd97 244 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
fa762db4
VZ
245 option_menu->AppendSeparator();
246 option_menu->Append(MDI_CHANGE_POSITION, "Move frame\tCtrl-M");
247 option_menu->Append(MDI_CHANGE_SIZE, "Resize frame\tCtrl-S");
c801d85f 248
c52d95b4 249 wxMenu *help_menu = new wxMenu;
fa762db4 250 help_menu->Append(MDI_ABOUT, "&About");
c801d85f 251
c52d95b4 252 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 253
c52d95b4 254 menu_bar->Append(file_menu, "&File");
fa762db4 255 menu_bar->Append(option_menu, "&Child");
c52d95b4 256 menu_bar->Append(help_menu, "&Help");
c801d85f 257
c52d95b4
VZ
258 // Associate the menu bar with the frame
259 subframe->SetMenuBar(menu_bar);
c801d85f 260
c41ea66a
VZ
261 subframe->CreateStatusBar();
262 subframe->SetStatusText(title);
263
c52d95b4
VZ
264 int width, height;
265 subframe->GetClientSize(&width, &height);
266 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
267 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
268 subframe->canvas = canvas;
c801d85f 269
c52d95b4
VZ
270 // Give it scrollbars
271 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 272
c52d95b4 273 subframe->Show(TRUE);
c801d85f
KB
274}
275
74b31181 276void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
c52d95b4
VZ
277{
278 int w, h;
279 GetClientSize(&w, &h);
280
281 textWindow->SetSize(0, 0, 200, h);
282 GetClientWindow()->SetSize(200, 0, w - 200, h);
283}
284
285void MyFrame::InitToolBar(wxToolBar* toolBar)
286{
287 wxBitmap* bitmaps[8];
288
289#ifdef __WXMSW__
290 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
291 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
292 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
293 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
294 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
295 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
296 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
297 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
298#else
299 bitmaps[0] = new wxBitmap( new_xpm );
300 bitmaps[1] = new wxBitmap( open_xpm );
301 bitmaps[2] = new wxBitmap( save_xpm );
302 bitmaps[3] = new wxBitmap( copy_xpm );
303 bitmaps[4] = new wxBitmap( cut_xpm );
304 bitmaps[5] = new wxBitmap( paste_xpm );
305 bitmaps[6] = new wxBitmap( print_xpm );
306 bitmaps[7] = new wxBitmap( help_xpm );
307#endif
308
309#ifdef __WXMSW__
310 int width = 24;
311#else
312 int width = 16;
313#endif
314 int currentX = 5;
315
316 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
317 currentX += width + 5;
318 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
319 currentX += width + 5;
320 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
321 currentX += width + 5;
322 toolBar->AddSeparator();
323 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
324 currentX += width + 5;
325 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
326 currentX += width + 5;
327 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
328 currentX += width + 5;
329 toolBar->AddSeparator();
330 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
331 currentX += width + 5;
332 toolBar->AddSeparator();
333 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
334
335 toolBar->Realize();
336
337 int i;
338 for (i = 0; i < 8; i++)
339 delete bitmaps[i];
340}
341
342// ---------------------------------------------------------------------------
343// MyCanvas
344// ---------------------------------------------------------------------------
c801d85f
KB
345
346// Define a constructor for my canvas
c52d95b4
VZ
347MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
348 : wxScrolledWindow(parent, -1, pos, size,
3ebcfb76
VZ
349 wxSUNKEN_BORDER |
350 wxNO_FULL_REPAINT_ON_RESIZE |
351 wxVSCROLL | wxHSCROLL)
c801d85f 352{
02800301 353 SetBackgroundColour(wxColour("WHITE"));
c52d95b4
VZ
354
355 m_dirty = FALSE;
c801d85f
KB
356}
357
358// Define the repainting behaviour
359void MyCanvas::OnDraw(wxDC& dc)
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
VZ
375 dc.DrawLine(50, 230, 200, 230);
376 dc.DrawText("This is a test string", 50, 230);
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
c52d95b4
VZ
400 m_dirty = TRUE;
401 }
c801d85f 402
c52d95b4
VZ
403 xpos = pt.x;
404 ypos = pt.y;
405}
406
407// ---------------------------------------------------------------------------
408// MyChild
409// ---------------------------------------------------------------------------
410
411MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
412 const wxPoint& pos, const wxSize& size,
413 const long style)
3ebcfb76
VZ
414 : wxMDIChildFrame(parent, -1, title, pos, size,
415 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 416{
c52d95b4
VZ
417 canvas = (MyCanvas *) NULL;
418 my_children.Append(this);
3ebcfb76
VZ
419
420 // this should work for MDI frames as well as for normal ones
421 SetSizeHints(100, 100);
c801d85f
KB
422}
423
c52d95b4 424MyChild::~MyChild()
c801d85f 425{
c52d95b4 426 my_children.DeleteObject(this);
c801d85f
KB
427}
428
33d0b396 429void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 430{
c52d95b4
VZ
431 Close(TRUE);
432}
433
f6bcfd97
BP
434void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
435{
436 if ( canvas )
437 canvas->Refresh();
438}
439
fa762db4
VZ
440void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
441{
442 Move(10, 10);
443}
444
445void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
446{
447 SetClientSize(100, 100);
448}
449
f6bcfd97 450void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 451{
f6bcfd97
BP
452 static wxString s_title = _T("Canvas Frame");
453
454 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
455 _T("MDI sample question"),
456 s_title,
fa762db4 457 GetParent()->GetParent());
f6bcfd97
BP
458 if ( !title )
459 return;
460
461 s_title = title;
462 SetTitle(s_title);
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 {
502 if ( wxMessageBox("Really close?", "Please confirm",
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
KB
514}
515
c52d95b4 516