]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
more type-safety
[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
46dc76ba 32#include <wx/toolbar.h>
c801d85f 33
8704bf74 34#if defined(__WXGTK__) || defined(__WXMOTIF__)
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)
83
84 EVT_CLOSE(MyChild::OnClose)
85END_EVENT_TABLE()
86
87BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
88 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
89END_EVENT_TABLE()
90
91// ===========================================================================
92// implementation
93// ===========================================================================
c801d85f 94
c52d95b4
VZ
95// ---------------------------------------------------------------------------
96// MyApp
97// ---------------------------------------------------------------------------
c801d85f
KB
98
99// Initialise this in OnInit, not statically
c52d95b4 100bool MyApp::OnInit()
c801d85f 101{
c52d95b4 102 // Create the main frame window
c801d85f 103
c52d95b4
VZ
104 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
105 wxPoint(-1, -1), wxSize(500, 400),
106 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
c801d85f 107
c52d95b4 108 // Give it an icon
2049ba38 109#ifdef __WXMSW__
c52d95b4 110 frame->SetIcon(wxIcon("mdi_icn"));
52cbfcf0 111#else
c52d95b4 112 frame->SetIcon(wxIcon( mondrian_xpm ));
c801d85f 113#endif
c801d85f 114
c52d95b4
VZ
115 // Make a menubar
116 wxMenu *file_menu = new wxMenu;
c801d85f 117
c52d95b4
VZ
118 file_menu->Append(MDI_NEW_WINDOW, "&New window", "Create a new child window");
119 file_menu->Append(MDI_QUIT, "&Exit", "Quit the program");
c801d85f 120
c52d95b4
VZ
121 wxMenu *help_menu = new wxMenu;
122 help_menu->Append(MDI_ABOUT, "&About");
c801d85f 123
c52d95b4 124 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 125
c52d95b4
VZ
126 menu_bar->Append(file_menu, "&File");
127 menu_bar->Append(help_menu, "&Help");
c801d85f 128
c52d95b4
VZ
129 // Associate the menu bar with the frame
130 frame->SetMenuBar(menu_bar);
c801d85f 131
c52d95b4 132 frame->CreateStatusBar();
c801d85f 133
c52d95b4 134 frame->Show(TRUE);
c801d85f 135
c52d95b4 136 SetTopWindow(frame);
c801d85f 137
c52d95b4 138 return TRUE;
c801d85f
KB
139}
140
c52d95b4
VZ
141// ---------------------------------------------------------------------------
142// MyFrame
143// ---------------------------------------------------------------------------
c801d85f
KB
144
145// Define my frame constructor
c52d95b4
VZ
146MyFrame::MyFrame(wxWindow *parent,
147 const wxWindowID id,
148 const wxString& title,
149 const wxPoint& pos,
150 const wxSize& size,
151 const long style)
152 : wxMDIParentFrame(parent, id, title, pos, size, style)
c801d85f 153{
c52d95b4
VZ
154 textWindow = new wxTextCtrl(this, -1, "A help window",
155 wxDefaultPosition, wxDefaultSize,
156 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 157
c52d95b4 158 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 159 InitToolBar(GetToolBar());
57a7b7c1 160
57a7b7c1
JS
161 // Accelerators
162 wxAcceleratorEntry entries[3];
163 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
164 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
165 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
166 wxAcceleratorTable accel(3, entries);
167 SetAcceleratorTable(accel);
c801d85f
KB
168}
169
c52d95b4
VZ
170void MyFrame::OnClose(wxCloseEvent& event)
171{
172 if ( event.CanVeto() && (gs_nFrames > 0) )
173 {
174 wxString msg;
ddb6bc71 175 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
c52d95b4
VZ
176 if ( wxMessageBox(msg, "Please confirm",
177 wxICON_QUESTION | wxYES_NO) != wxYES )
178 {
179 event.Veto();
180
181 return;
182 }
183 }
184
185 event.Skip();
186}
187
188void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 189{
c52d95b4 190 Close();
c801d85f
KB
191}
192
e3e65dac 193void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 194{
c52d95b4
VZ
195 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
196 "Author: Julian Smart (c) 1997\n"
197 "Usage: mdi.exe", "About MDI Demo");
c801d85f
KB
198}
199
e3e65dac 200void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 201{
c52d95b4
VZ
202 // Make another frame, containing a canvas
203 MyChild *subframe = new MyChild(frame, "Canvas Frame",
204 wxPoint(-1, -1), wxSize(-1, -1),
205 wxDEFAULT_FRAME_STYLE);
c801d85f 206
c52d95b4 207 wxString title;
ddb6bc71 208 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
c801d85f 209
c52d95b4
VZ
210 subframe->SetTitle(title);
211
212 // Give it an icon
2049ba38 213#ifdef __WXMSW__
c52d95b4 214 subframe->SetIcon(wxIcon("chrt_icn"));
8704bf74 215#else
c52d95b4 216 subframe->SetIcon(wxIcon( mondrian_xpm ));
c801d85f
KB
217#endif
218
c52d95b4
VZ
219 // Make a menubar
220 wxMenu *file_menu = new wxMenu;
221
222 file_menu->Append(MDI_NEW_WINDOW, "&New window");
223 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
224 file_menu->Append(MDI_QUIT, "&Exit");
c801d85f 225
c52d95b4 226 wxMenu *option_menu = new wxMenu;
c801d85f 227
c52d95b4
VZ
228 // Dummy option
229 option_menu->Append(MDI_REFRESH, "&Refresh picture");
c801d85f 230
c52d95b4
VZ
231 wxMenu *help_menu = new wxMenu;
232 help_menu->Append(MDI_ABOUT, "&About");
c801d85f 233
c52d95b4 234 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 235
c52d95b4
VZ
236 menu_bar->Append(file_menu, "&File");
237 menu_bar->Append(option_menu, "&Options");
238 menu_bar->Append(help_menu, "&Help");
c801d85f 239
c52d95b4
VZ
240 // Associate the menu bar with the frame
241 subframe->SetMenuBar(menu_bar);
c801d85f 242
c52d95b4
VZ
243 int width, height;
244 subframe->GetClientSize(&width, &height);
245 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
246 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
247 subframe->canvas = canvas;
c801d85f 248
c52d95b4
VZ
249 // Give it scrollbars
250 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 251
c52d95b4
VZ
252 subframe->CreateStatusBar();
253 subframe->SetStatusText(title);
c801d85f 254
c52d95b4 255 subframe->Show(TRUE);
c801d85f
KB
256}
257
c52d95b4
VZ
258void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
259{
260 int w, h;
261 GetClientSize(&w, &h);
262
263 textWindow->SetSize(0, 0, 200, h);
264 GetClientWindow()->SetSize(200, 0, w - 200, h);
265}
266
267void MyFrame::InitToolBar(wxToolBar* toolBar)
268{
269 wxBitmap* bitmaps[8];
270
271#ifdef __WXMSW__
272 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
273 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
274 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
275 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
276 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
277 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
278 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
279 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
280#else
281 bitmaps[0] = new wxBitmap( new_xpm );
282 bitmaps[1] = new wxBitmap( open_xpm );
283 bitmaps[2] = new wxBitmap( save_xpm );
284 bitmaps[3] = new wxBitmap( copy_xpm );
285 bitmaps[4] = new wxBitmap( cut_xpm );
286 bitmaps[5] = new wxBitmap( paste_xpm );
287 bitmaps[6] = new wxBitmap( print_xpm );
288 bitmaps[7] = new wxBitmap( help_xpm );
289#endif
290
291#ifdef __WXMSW__
292 int width = 24;
293#else
294 int width = 16;
295#endif
296 int currentX = 5;
297
298 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
299 currentX += width + 5;
300 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
301 currentX += width + 5;
302 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
303 currentX += width + 5;
304 toolBar->AddSeparator();
305 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
306 currentX += width + 5;
307 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
308 currentX += width + 5;
309 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
310 currentX += width + 5;
311 toolBar->AddSeparator();
312 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
313 currentX += width + 5;
314 toolBar->AddSeparator();
315 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
316
317 toolBar->Realize();
318
319 int i;
320 for (i = 0; i < 8; i++)
321 delete bitmaps[i];
322}
323
324// ---------------------------------------------------------------------------
325// MyCanvas
326// ---------------------------------------------------------------------------
c801d85f
KB
327
328// Define a constructor for my canvas
c52d95b4
VZ
329MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
330 : wxScrolledWindow(parent, -1, pos, size,
331 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
c801d85f 332{
02800301 333 SetBackgroundColour(wxColour("WHITE"));
c52d95b4
VZ
334
335 m_dirty = FALSE;
c801d85f
KB
336}
337
338// Define the repainting behaviour
339void MyCanvas::OnDraw(wxDC& dc)
340{
c52d95b4
VZ
341 dc.SetFont(*wxSWISS_FONT);
342 dc.SetPen(*wxGREEN_PEN);
343 dc.DrawLine(0, 0, 200, 200);
344 dc.DrawLine(200, 0, 0, 200);
345
346 dc.SetBrush(*wxCYAN_BRUSH);
347 dc.SetPen(*wxRED_PEN);
348 dc.DrawRectangle(100, 100, 100, 50);
349 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
350
351 dc.DrawEllipse(250, 250, 100, 50);
352 dc.DrawSpline(50, 200, 50, 100, 200, 10);
353 dc.DrawLine(50, 230, 200, 230);
354 dc.DrawText("This is a test string", 50, 230);
355
356 wxPoint points[3];
357 points[0].x = 200; points[0].y = 300;
358 points[1].x = 100; points[1].y = 400;
359 points[2].x = 300; points[2].y = 400;
360
361 dc.DrawPolygon(3, points);
c801d85f
KB
362}
363
c52d95b4
VZ
364// This implements a tiny doodling program! Drag the mouse using the left
365// button.
c801d85f
KB
366void MyCanvas::OnEvent(wxMouseEvent& event)
367{
c52d95b4
VZ
368 wxClientDC dc(this);
369 PrepareDC(dc);
c801d85f 370
c52d95b4 371 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 372
c52d95b4
VZ
373 if (xpos > -1 && ypos > -1 && event.Dragging())
374 {
375 dc.SetPen(*wxBLACK_PEN);
376 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 377
c52d95b4
VZ
378 m_dirty = TRUE;
379 }
c801d85f 380
c52d95b4
VZ
381 xpos = pt.x;
382 ypos = pt.y;
383}
384
385// ---------------------------------------------------------------------------
386// MyChild
387// ---------------------------------------------------------------------------
388
389MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
390 const wxPoint& pos, const wxSize& size,
391 const long style)
392 : wxMDIChildFrame(parent, -1, title, pos, size, style)
c801d85f 393{
c52d95b4
VZ
394 canvas = (MyCanvas *) NULL;
395 my_children.Append(this);
c801d85f
KB
396}
397
c52d95b4 398MyChild::~MyChild()
c801d85f 399{
c52d95b4 400 my_children.DeleteObject(this);
c801d85f
KB
401}
402
33d0b396 403void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 404{
c52d95b4
VZ
405 Close(TRUE);
406}
407
408void MyChild::OnRefresh(wxCommandEvent& event)
409{
410 Refresh();
c801d85f
KB
411}
412
413void MyChild::OnActivate(wxActivateEvent& event)
414{
c52d95b4
VZ
415 if ( event.GetActive() && canvas )
416 canvas->SetFocus();
c801d85f
KB
417}
418
c52d95b4 419void MyChild::OnClose(wxCloseEvent& event)
c801d85f 420{
c52d95b4
VZ
421 if ( canvas && canvas->IsDirty() )
422 {
423 if ( wxMessageBox("Really close?", "Please confirm",
424 wxICON_QUESTION | wxYES_NO) != wxYES )
425 {
426 event.Veto();
c801d85f 427
c52d95b4
VZ
428 return;
429 }
430 }
c801d85f 431
c52d95b4
VZ
432 gs_nFrames--;
433
434 event.Skip();
c801d85f
KB
435}
436
c52d95b4 437