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