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