]> git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
moving to gcc 4.0 exclusively, 10.4 minimum system
[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 "../sample.xpm"
36 #include "chart.xpm"
37 #endif
38
39 #include "bitmaps/new.xpm"
40 #include "bitmaps/open.xpm"
41 #include "bitmaps/save.xpm"
42 #include "bitmaps/copy.xpm"
43 #include "bitmaps/cut.xpm"
44 #include "bitmaps/paste.xpm"
45 #include "bitmaps/print.xpm"
46 #include "bitmaps/help.xpm"
47
48
49 #include "mdi.h"
50
51 IMPLEMENT_APP(MyApp)
52
53 // ---------------------------------------------------------------------------
54 // global variables
55 // ---------------------------------------------------------------------------
56
57 MyFrame *frame = (MyFrame *) NULL;
58 wxList my_children;
59
60 // For drawing lines in a canvas
61 static long xpos = -1;
62 static long ypos = -1;
63
64 static int gs_nFrames = 0;
65
66 // ---------------------------------------------------------------------------
67 // event tables
68 // ---------------------------------------------------------------------------
69
70 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
71 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
72 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
73 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
74
75 EVT_CLOSE(MyFrame::OnClose)
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 #if wxUSE_CLIPBOARD
90 EVT_MENU(wxID_PASTE, MyChild::OnPaste)
91 EVT_UPDATE_UI(wxID_PASTE, MyChild::OnUpdatePaste)
92 #endif // wxUSE_CLIPBOARD
93
94 EVT_SIZE(MyChild::OnSize)
95 EVT_MOVE(MyChild::OnMove)
96
97 EVT_CLOSE(MyChild::OnClose)
98 END_EVENT_TABLE()
99
100 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
101 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
102 END_EVENT_TABLE()
103
104 // ===========================================================================
105 // implementation
106 // ===========================================================================
107
108 // ---------------------------------------------------------------------------
109 // MyApp
110 // ---------------------------------------------------------------------------
111
112 // Initialise this in OnInit, not statically
113 bool MyApp::OnInit()
114 {
115 if ( !wxApp::OnInit() )
116 return false;
117
118 // Create the main frame window
119
120 frame = new MyFrame((wxFrame *)NULL, wxID_ANY, _T("MDI Demo"),
121 wxDefaultPosition, wxSize(500, 400),
122 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
123 #if 0
124 // Experimental: change the window menu
125 wxMenu* windowMenu = new wxMenu;
126 windowMenu->Append(5000, _T("My menu item!"));
127 frame->SetWindowMenu(windowMenu);
128 #endif
129
130 // Give it an icon
131 frame->SetIcon(wxICON(sample));
132
133 // Make a menubar
134 wxMenu *file_menu = new wxMenu;
135
136 file_menu->Append(MDI_NEW_WINDOW, _T("&New window\tCtrl-N"), _T("Create a new child window"));
137 file_menu->Append(MDI_QUIT, _T("&Exit\tAlt-X"), _T("Quit the program"));
138
139 wxMenu *help_menu = new wxMenu;
140 help_menu->Append(MDI_ABOUT, _T("&About\tF1"));
141
142 wxMenuBar *menu_bar = new wxMenuBar;
143
144 menu_bar->Append(file_menu, _T("&File"));
145 menu_bar->Append(help_menu, _T("&Help"));
146
147 // Associate the menu bar with the frame
148 frame->SetMenuBar(menu_bar);
149
150 #if wxUSE_STATUSBAR
151 frame->CreateStatusBar();
152 #endif // wxUSE_STATUSBAR
153
154 frame->Show(true);
155
156 SetTopWindow(frame);
157
158 return true;
159 }
160
161 // ---------------------------------------------------------------------------
162 // MyFrame
163 // ---------------------------------------------------------------------------
164
165 // Define my frame constructor
166 MyFrame::MyFrame(wxWindow *parent,
167 const wxWindowID id,
168 const wxString& title,
169 const wxPoint& pos,
170 const wxSize& size,
171 const long style)
172 : wxMDIParentFrame(parent, id, title, pos, size, style)
173 {
174 textWindow = new wxTextCtrl(this, wxID_ANY, _T("A help window"),
175 wxDefaultPosition, wxDefaultSize,
176 wxTE_MULTILINE | wxSUNKEN_BORDER);
177
178 #if wxUSE_TOOLBAR
179 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
180 InitToolBar(GetToolBar());
181 #endif // wxUSE_TOOLBAR
182
183 #if wxUSE_ACCEL
184 // Accelerators
185 wxAcceleratorEntry entries[3];
186 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
187 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
188 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
189 wxAcceleratorTable accel(3, entries);
190 SetAcceleratorTable(accel);
191 #endif // wxUSE_ACCEL
192 }
193
194 void MyFrame::OnClose(wxCloseEvent& event)
195 {
196 if ( event.CanVeto() && (gs_nFrames > 0) )
197 {
198 wxString msg;
199 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
200 if ( wxMessageBox(msg, _T("Please confirm"),
201 wxICON_QUESTION | wxYES_NO) != wxYES )
202 {
203 event.Veto();
204
205 return;
206 }
207 }
208
209 event.Skip();
210 }
211
212 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
213 {
214 Close();
215 }
216
217 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
218 {
219 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
220 _T("Author: Julian Smart (c) 1997\n")
221 _T("Usage: mdi.exe"), _T("About MDI Demo"));
222 }
223
224 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
225 {
226 // Make another frame, containing a canvas
227 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"));
228
229 wxString title;
230 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
231
232 subframe->SetTitle(title);
233
234 // Give it an icon
235 subframe->SetIcon(wxICON(chart));
236
237 #if wxUSE_MENUS
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 #if wxUSE_CLIPBOARD
253 option_menu->AppendSeparator();
254 option_menu->Append(wxID_PASTE, _T("Copy text from clipboard\tCtrl-V"));
255 #endif // wxUSE_CLIPBOARD
256
257 wxMenu *help_menu = new wxMenu;
258 help_menu->Append(MDI_ABOUT, _T("&About"));
259
260 wxMenuBar *menu_bar = new wxMenuBar;
261
262 menu_bar->Append(file_menu, _T("&File"));
263 menu_bar->Append(option_menu, _T("&Child"));
264 menu_bar->Append(help_menu, _T("&Help"));
265
266 // Associate the menu bar with the frame
267 subframe->SetMenuBar(menu_bar);
268 #endif // wxUSE_MENUS
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] = wxBitmap( new_xpm );
310 bitmaps[1] = wxBitmap( open_xpm );
311 bitmaps[2] = wxBitmap( save_xpm );
312 bitmaps[3] = wxBitmap( copy_xpm );
313 bitmaps[4] = wxBitmap( cut_xpm );
314 bitmaps[5] = wxBitmap( paste_xpm );
315 bitmaps[6] = wxBitmap( print_xpm );
316 bitmaps[7] = wxBitmap( help_xpm );
317
318 toolBar->AddTool(MDI_NEW_WINDOW, _T("New"), bitmaps[0], _T("New file"));
319 toolBar->AddTool(1, _T("Open"), bitmaps[1], _T("Open file"));
320 toolBar->AddTool(2, _T("Save"), bitmaps[2], _T("Save file"));
321 toolBar->AddSeparator();
322 toolBar->AddTool(3, _T("Copy"), bitmaps[3], _T("Copy"));
323 toolBar->AddTool(4, _T("Cut"), bitmaps[4], _T("Cut"));
324 toolBar->AddTool(5, _T("Paste"), bitmaps[5], _T("Paste"));
325 toolBar->AddSeparator();
326 toolBar->AddTool(6, _T("Print"), bitmaps[6], _T("Print"));
327 toolBar->AddSeparator();
328 toolBar->AddTool(MDI_ABOUT, _T("About"), bitmaps[7], _T("Help"));
329
330 toolBar->Realize();
331 }
332 #endif // wxUSE_TOOLBAR
333
334 // ---------------------------------------------------------------------------
335 // MyCanvas
336 // ---------------------------------------------------------------------------
337
338 // Define a constructor for my canvas
339 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
340 : wxScrolledWindow(parent, wxID_ANY, pos, size,
341 wxSUNKEN_BORDER |
342 wxNO_FULL_REPAINT_ON_RESIZE |
343 wxVSCROLL | wxHSCROLL)
344 {
345 SetBackgroundColour(wxColour(_T("WHITE")));
346
347 m_dirty = false;
348 }
349
350 // Define the repainting behaviour
351 void MyCanvas::OnDraw(wxDC& dc)
352 {
353 if ( !m_text.empty() )
354 dc.DrawText(m_text, 10, 10);
355
356 dc.SetFont(*wxSWISS_FONT);
357 dc.SetPen(*wxGREEN_PEN);
358 dc.DrawLine(0, 0, 200, 200);
359 dc.DrawLine(200, 0, 0, 200);
360
361 dc.SetBrush(*wxCYAN_BRUSH);
362 dc.SetPen(*wxRED_PEN);
363 dc.DrawRectangle(100, 100, 100, 50);
364 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
365
366 dc.DrawEllipse(250, 250, 100, 50);
367 #if wxUSE_SPLINES
368 dc.DrawSpline(50, 200, 50, 100, 200, 10);
369 #endif // wxUSE_SPLINES
370 dc.DrawLine(50, 230, 200, 230);
371 dc.DrawText(_T("This is a test string"), 50, 230);
372
373 wxPoint points[3];
374 points[0].x = 200; points[0].y = 300;
375 points[1].x = 100; points[1].y = 400;
376 points[2].x = 300; points[2].y = 400;
377
378 dc.DrawPolygon(3, points);
379 }
380
381 // This implements a tiny doodling program! Drag the mouse using the left
382 // button.
383 void MyCanvas::OnEvent(wxMouseEvent& event)
384 {
385 wxClientDC dc(this);
386 PrepareDC(dc);
387
388 wxPoint pt(event.GetLogicalPosition(dc));
389
390 if (xpos > -1 && ypos > -1 && event.Dragging())
391 {
392 dc.SetPen(*wxBLACK_PEN);
393 dc.DrawLine(xpos, ypos, pt.x, pt.y);
394
395 m_dirty = true;
396 }
397
398 xpos = pt.x;
399 ypos = pt.y;
400 }
401
402 // ---------------------------------------------------------------------------
403 // MyChild
404 // ---------------------------------------------------------------------------
405
406 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title)
407 : wxMDIChildFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
408 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
409 {
410 canvas = (MyCanvas *) NULL;
411 my_children.Append(this);
412
413 // this should work for MDI frames as well as for normal ones
414 SetSizeHints(100, 100);
415 }
416
417 MyChild::~MyChild()
418 {
419 my_children.DeleteObject(this);
420 }
421
422 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
423 {
424 Close(true);
425 }
426
427 void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
428 {
429 if ( canvas )
430 canvas->Refresh();
431 }
432
433 void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
434 {
435 Move(10, 10);
436 }
437
438 void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
439 {
440 SetClientSize(100, 100);
441 }
442
443 void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
444 {
445 #if wxUSE_TEXTDLG
446 static wxString s_title = _T("Canvas Frame");
447
448 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
449 _T("MDI sample question"),
450 s_title,
451 GetParent()->GetParent());
452 if ( !title )
453 return;
454
455 s_title = title;
456 SetTitle(s_title);
457 #endif // wxUSE_TEXTDLG
458 }
459
460 void MyChild::OnActivate(wxActivateEvent& event)
461 {
462 if ( event.GetActive() && canvas )
463 canvas->SetFocus();
464 }
465
466 void MyChild::OnMove(wxMoveEvent& event)
467 {
468 // VZ: here everything is totally wrong under MSW, the positions are
469 // different and both wrong (pos2 is off by 2 pixels for me which seems
470 // to be the width of the MDI canvas border)
471 wxPoint pos1 = event.GetPosition(),
472 pos2 = GetPosition();
473 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
474 pos1.x, pos1.y, pos2.x, pos2.y);
475
476 event.Skip();
477 }
478
479 void MyChild::OnSize(wxSizeEvent& event)
480 {
481 // VZ: under MSW the size event carries the client size (quite
482 // unexpectedly) *except* for the very first one which has the full
483 // size... what should it really be? TODO: check under wxGTK
484 wxSize size1 = event.GetSize(),
485 size2 = GetSize(),
486 size3 = GetClientSize();
487 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
488 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
489
490 event.Skip();
491 }
492
493 void MyChild::OnClose(wxCloseEvent& event)
494 {
495 if ( canvas && canvas->IsDirty() )
496 {
497 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
498 wxICON_QUESTION | wxYES_NO) != wxYES )
499 {
500 event.Veto();
501
502 return;
503 }
504 }
505
506 gs_nFrames--;
507
508 event.Skip();
509 }
510
511 #if wxUSE_CLIPBOARD
512
513 #include "wx/clipbrd.h"
514
515 void MyChild::OnPaste(wxCommandEvent& WXUNUSED(event))
516 {
517 wxClipboardLocker lock;
518 wxTextDataObject data;
519 canvas->SetText(
520 (
521 wxTheClipboard->GetData(data) ?
522 data.GetText() :
523 wxString("No text on clipboard")
524 ).c_str()
525 );
526 }
527
528 void MyChild::OnUpdatePaste(wxUpdateUIEvent& event)
529 {
530 wxClipboardLocker lock;
531 event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
532 }
533
534 #endif // wxUSE_CLIPBOARD