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