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