]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
Corrected bug in cmdlpars.tex
[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
2f20fe22
JJ
20#ifdef __VMS
21#define XtDisplay XTDISPLAY
22#define XtWindow XTWINDOW
23#endif
24
c801d85f
KB
25// For compilers that support precompilation, includes "wx/wx.h".
26#include "wx/wxprec.h"
27
28#ifdef __BORLANDC__
c52d95b4 29 #pragma hdrstop
c801d85f
KB
30#endif
31
32#ifndef WX_PRECOMP
c52d95b4
VZ
33 #include "wx/wx.h"
34 #include "wx/mdi.h"
c801d85f
KB
35#endif
36
46dc76ba 37#include <wx/toolbar.h>
c801d85f 38
8704bf74 39#if defined(__WXGTK__) || defined(__WXMOTIF__)
c52d95b4
VZ
40 #include "mondrian.xpm"
41 #include "bitmaps/new.xpm"
42 #include "bitmaps/open.xpm"
43 #include "bitmaps/save.xpm"
44 #include "bitmaps/copy.xpm"
45 #include "bitmaps/cut.xpm"
46 #include "bitmaps/paste.xpm"
47 #include "bitmaps/print.xpm"
48 #include "bitmaps/help.xpm"
716b7364
RR
49#endif
50
c801d85f
KB
51#include "mdi.h"
52
c52d95b4
VZ
53IMPLEMENT_APP(MyApp)
54
55// ---------------------------------------------------------------------------
56// global variables
57// ---------------------------------------------------------------------------
58
c67daf87 59MyFrame *frame = (MyFrame *) NULL;
c801d85f
KB
60wxList my_children;
61
c801d85f 62// For drawing lines in a canvas
c52d95b4
VZ
63static long xpos = -1;
64static long ypos = -1;
65
66static int gs_nFrames = 0;
67
68// ---------------------------------------------------------------------------
69// event tables
70// ---------------------------------------------------------------------------
71
72BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
73 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
74 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
75 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
76
77 EVT_CLOSE(MyFrame::OnClose)
78
79 EVT_SIZE(MyFrame::OnSize)
80END_EVENT_TABLE()
81
82// Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
83// to the parent window for processing, so no need to
84// duplicate event handlers here.
85BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
86 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
87 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
f6bcfd97 88 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
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)
166 : wxMDIParentFrame(parent, id, title, pos, size, style)
c801d85f 167{
c52d95b4
VZ
168 textWindow = new wxTextCtrl(this, -1, "A help window",
169 wxDefaultPosition, wxDefaultSize,
170 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 171
c52d95b4 172 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 173 InitToolBar(GetToolBar());
57a7b7c1 174
57a7b7c1
JS
175 // Accelerators
176 wxAcceleratorEntry entries[3];
177 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
178 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
179 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
180 wxAcceleratorTable accel(3, entries);
181 SetAcceleratorTable(accel);
c801d85f
KB
182}
183
c52d95b4
VZ
184void MyFrame::OnClose(wxCloseEvent& event)
185{
186 if ( event.CanVeto() && (gs_nFrames > 0) )
187 {
188 wxString msg;
ddb6bc71 189 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
c52d95b4
VZ
190 if ( wxMessageBox(msg, "Please confirm",
191 wxICON_QUESTION | wxYES_NO) != wxYES )
192 {
193 event.Veto();
194
195 return;
196 }
197 }
198
199 event.Skip();
200}
201
202void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 203{
c52d95b4 204 Close();
c801d85f
KB
205}
206
e3e65dac 207void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 208{
c52d95b4
VZ
209 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
210 "Author: Julian Smart (c) 1997\n"
211 "Usage: mdi.exe", "About MDI Demo");
c801d85f
KB
212}
213
e3e65dac 214void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 215{
c52d95b4
VZ
216 // Make another frame, containing a canvas
217 MyChild *subframe = new MyChild(frame, "Canvas Frame",
218 wxPoint(-1, -1), wxSize(-1, -1),
219 wxDEFAULT_FRAME_STYLE);
c801d85f 220
c52d95b4 221 wxString title;
ddb6bc71 222 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
c801d85f 223
c52d95b4
VZ
224 subframe->SetTitle(title);
225
226 // Give it an icon
2049ba38 227#ifdef __WXMSW__
c52d95b4 228 subframe->SetIcon(wxIcon("chrt_icn"));
8704bf74 229#else
c52d95b4 230 subframe->SetIcon(wxIcon( mondrian_xpm ));
c801d85f
KB
231#endif
232
c52d95b4
VZ
233 // Make a menubar
234 wxMenu *file_menu = new wxMenu;
235
5a2762f0 236 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N");
c52d95b4 237 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
5a2762f0 238 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X");
c801d85f 239
c52d95b4 240 wxMenu *option_menu = new wxMenu;
c801d85f 241
5a2762f0 242 option_menu->Append(MDI_REFRESH, "&Refresh picture\tF5");
f6bcfd97 243 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
c801d85f 244
c52d95b4 245 wxMenu *help_menu = new wxMenu;
5a2762f0 246 help_menu->Append(MDI_ABOUT, "&About\tF1");
c801d85f 247
c52d95b4 248 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 249
c52d95b4
VZ
250 menu_bar->Append(file_menu, "&File");
251 menu_bar->Append(option_menu, "&Options");
252 menu_bar->Append(help_menu, "&Help");
c801d85f 253
c52d95b4
VZ
254 // Associate the menu bar with the frame
255 subframe->SetMenuBar(menu_bar);
c801d85f 256
c41ea66a
VZ
257 subframe->CreateStatusBar();
258 subframe->SetStatusText(title);
259
c52d95b4
VZ
260 int width, height;
261 subframe->GetClientSize(&width, &height);
262 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
263 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
264 subframe->canvas = canvas;
c801d85f 265
c52d95b4
VZ
266 // Give it scrollbars
267 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 268
c52d95b4 269 subframe->Show(TRUE);
c801d85f
KB
270}
271
74b31181 272void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
c52d95b4
VZ
273{
274 int w, h;
275 GetClientSize(&w, &h);
276
277 textWindow->SetSize(0, 0, 200, h);
278 GetClientWindow()->SetSize(200, 0, w - 200, h);
279}
280
281void MyFrame::InitToolBar(wxToolBar* toolBar)
282{
283 wxBitmap* bitmaps[8];
284
285#ifdef __WXMSW__
286 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
287 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
288 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
289 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
290 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
291 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
292 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
293 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
294#else
295 bitmaps[0] = new wxBitmap( new_xpm );
296 bitmaps[1] = new wxBitmap( open_xpm );
297 bitmaps[2] = new wxBitmap( save_xpm );
298 bitmaps[3] = new wxBitmap( copy_xpm );
299 bitmaps[4] = new wxBitmap( cut_xpm );
300 bitmaps[5] = new wxBitmap( paste_xpm );
301 bitmaps[6] = new wxBitmap( print_xpm );
302 bitmaps[7] = new wxBitmap( help_xpm );
303#endif
304
305#ifdef __WXMSW__
306 int width = 24;
307#else
308 int width = 16;
309#endif
310 int currentX = 5;
311
312 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
313 currentX += width + 5;
314 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
315 currentX += width + 5;
316 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
317 currentX += width + 5;
318 toolBar->AddSeparator();
319 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
320 currentX += width + 5;
321 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
322 currentX += width + 5;
323 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
324 currentX += width + 5;
325 toolBar->AddSeparator();
326 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
327 currentX += width + 5;
328 toolBar->AddSeparator();
329 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
330
331 toolBar->Realize();
332
333 int i;
334 for (i = 0; i < 8; i++)
335 delete bitmaps[i];
336}
337
338// ---------------------------------------------------------------------------
339// MyCanvas
340// ---------------------------------------------------------------------------
c801d85f
KB
341
342// Define a constructor for my canvas
c52d95b4
VZ
343MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
344 : wxScrolledWindow(parent, -1, pos, size,
345 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
c801d85f 346{
02800301 347 SetBackgroundColour(wxColour("WHITE"));
c52d95b4
VZ
348
349 m_dirty = FALSE;
c801d85f
KB
350}
351
352// Define the repainting behaviour
353void MyCanvas::OnDraw(wxDC& dc)
354{
c52d95b4
VZ
355 dc.SetFont(*wxSWISS_FONT);
356 dc.SetPen(*wxGREEN_PEN);
357 dc.DrawLine(0, 0, 200, 200);
358 dc.DrawLine(200, 0, 0, 200);
359
360 dc.SetBrush(*wxCYAN_BRUSH);
361 dc.SetPen(*wxRED_PEN);
362 dc.DrawRectangle(100, 100, 100, 50);
363 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
364
365 dc.DrawEllipse(250, 250, 100, 50);
c41ea66a 366#if wxUSE_SPLINES
c52d95b4 367 dc.DrawSpline(50, 200, 50, 100, 200, 10);
c41ea66a 368#endif // wxUSE_SPLINES
c52d95b4
VZ
369 dc.DrawLine(50, 230, 200, 230);
370 dc.DrawText("This is a test string", 50, 230);
371
372 wxPoint points[3];
373 points[0].x = 200; points[0].y = 300;
374 points[1].x = 100; points[1].y = 400;
375 points[2].x = 300; points[2].y = 400;
376
377 dc.DrawPolygon(3, points);
c801d85f
KB
378}
379
c52d95b4
VZ
380// This implements a tiny doodling program! Drag the mouse using the left
381// button.
c801d85f
KB
382void MyCanvas::OnEvent(wxMouseEvent& event)
383{
c52d95b4
VZ
384 wxClientDC dc(this);
385 PrepareDC(dc);
c801d85f 386
c52d95b4 387 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 388
c52d95b4
VZ
389 if (xpos > -1 && ypos > -1 && event.Dragging())
390 {
391 dc.SetPen(*wxBLACK_PEN);
392 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 393
c52d95b4
VZ
394 m_dirty = TRUE;
395 }
c801d85f 396
c52d95b4
VZ
397 xpos = pt.x;
398 ypos = pt.y;
399}
400
401// ---------------------------------------------------------------------------
402// MyChild
403// ---------------------------------------------------------------------------
404
405MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
406 const wxPoint& pos, const wxSize& size,
407 const long style)
408 : wxMDIChildFrame(parent, -1, title, pos, size, style)
c801d85f 409{
c52d95b4
VZ
410 canvas = (MyCanvas *) NULL;
411 my_children.Append(this);
c801d85f
KB
412}
413
c52d95b4 414MyChild::~MyChild()
c801d85f 415{
c52d95b4 416 my_children.DeleteObject(this);
c801d85f
KB
417}
418
33d0b396 419void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 420{
c52d95b4
VZ
421 Close(TRUE);
422}
423
f6bcfd97
BP
424void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
425{
426 if ( canvas )
427 canvas->Refresh();
428}
429
430void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 431{
f6bcfd97
BP
432 static wxString s_title = _T("Canvas Frame");
433
434 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
435 _T("MDI sample question"),
436 s_title,
2fb40b2c 437 GetParent());
f6bcfd97
BP
438 if ( !title )
439 return;
440
441 s_title = title;
442 SetTitle(s_title);
c801d85f
KB
443}
444
445void MyChild::OnActivate(wxActivateEvent& event)
446{
c52d95b4
VZ
447 if ( event.GetActive() && canvas )
448 canvas->SetFocus();
c801d85f
KB
449}
450
c52d95b4 451void MyChild::OnClose(wxCloseEvent& event)
c801d85f 452{
c52d95b4
VZ
453 if ( canvas && canvas->IsDirty() )
454 {
455 if ( wxMessageBox("Really close?", "Please confirm",
456 wxICON_QUESTION | wxYES_NO) != wxYES )
457 {
458 event.Veto();
c801d85f 459
c52d95b4
VZ
460 return;
461 }
462 }
c801d85f 463
c52d95b4
VZ
464 gs_nFrames--;
465
466 event.Skip();
c801d85f
KB
467}
468
c52d95b4 469