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