merged in the small changes from the 2.2 branch
[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 #ifdef __VMS
21 #define XtDisplay XTDISPLAY
22 #define XtWindow XTWINDOW
23 #endif
24
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
27
28 #ifdef __BORLANDC__
29 #pragma hdrstop
30 #endif
31
32 #ifndef WX_PRECOMP
33 #include "wx/wx.h"
34 #include "wx/mdi.h"
35 #endif
36
37 #include <wx/toolbar.h>
38
39 #if defined(__WXGTK__) || defined(__WXMOTIF__)
40 #include "mondrian.xpm"
41 #include "bitmaps/new.xpm"
42 #include "bitmaps/open.xpm"
43 #include "bitmaps/save.xpm"
44 #include "bitmaps/copy.xpm"
45 #include "bitmaps/cut.xpm"
46 #include "bitmaps/paste.xpm"
47 #include "bitmaps/print.xpm"
48 #include "bitmaps/help.xpm"
49 #endif
50
51 #include "mdi.h"
52
53 IMPLEMENT_APP(MyApp)
54
55 // ---------------------------------------------------------------------------
56 // global variables
57 // ---------------------------------------------------------------------------
58
59 MyFrame *frame = (MyFrame *) NULL;
60 wxList my_children;
61
62 // For drawing lines in a canvas
63 static long xpos = -1;
64 static long ypos = -1;
65
66 static int gs_nFrames = 0;
67
68 // ---------------------------------------------------------------------------
69 // event tables
70 // ---------------------------------------------------------------------------
71
72 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
73 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
74 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
75 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
76
77 EVT_CLOSE(MyFrame::OnClose)
78
79 EVT_SIZE(MyFrame::OnSize)
80 END_EVENT_TABLE()
81
82 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
83 // to the parent window for processing, so no need to
84 // duplicate event handlers here.
85 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
86 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
87 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
88 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
89 EVT_MENU(MDI_CHANGE_POSITION, MyChild::OnChangePosition)
90 EVT_MENU(MDI_CHANGE_SIZE, MyChild::OnChangeSize)
91
92 EVT_UPDATE_UI(MDI_REFRESH, MyChild::OnUpdateRefresh)
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 // Create the main frame window
116
117 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
118 wxPoint(-1, -1), wxSize(500, 400),
119 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
120 #ifdef __WXMSW__
121 #if 0
122 // Experimental: change the window menu
123 wxMenu* windowMenu = new wxMenu;
124 windowMenu->Append(5000, "My menu item!");
125 frame->SetWindowMenu(windowMenu);
126 #endif
127 #endif
128
129 // Give it an icon
130 #ifdef __WXMSW__
131 frame->SetIcon(wxIcon("mdi_icn"));
132 #else
133 frame->SetIcon(wxIcon( mondrian_xpm ));
134 #endif
135
136 // Make a menubar
137 wxMenu *file_menu = new wxMenu;
138
139 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N", "Create a new child window");
140 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X", "Quit the program");
141
142 wxMenu *help_menu = new wxMenu;
143 help_menu->Append(MDI_ABOUT, "&About\tF1");
144
145 wxMenuBar *menu_bar = new wxMenuBar;
146
147 menu_bar->Append(file_menu, "&File");
148 menu_bar->Append(help_menu, "&Help");
149
150 // Associate the menu bar with the frame
151 frame->SetMenuBar(menu_bar);
152
153 frame->CreateStatusBar();
154
155 frame->Show(TRUE);
156
157 SetTopWindow(frame);
158
159 return TRUE;
160 }
161
162 // ---------------------------------------------------------------------------
163 // MyFrame
164 // ---------------------------------------------------------------------------
165
166 // Define my frame constructor
167 MyFrame::MyFrame(wxWindow *parent,
168 const wxWindowID id,
169 const wxString& title,
170 const wxPoint& pos,
171 const wxSize& size,
172 const long style)
173 : wxMDIParentFrame(parent, id, title, pos, size, style)
174 {
175 textWindow = new wxTextCtrl(this, -1, "A help window",
176 wxDefaultPosition, wxDefaultSize,
177 wxTE_MULTILINE | wxSUNKEN_BORDER);
178
179 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
180 InitToolBar(GetToolBar());
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, "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("wxWindows 2.0 MDI Demo\n"
217 "Author: Julian Smart (c) 1997\n"
218 "Usage: mdi.exe", "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, "Canvas Frame",
225 wxPoint(-1, -1), wxSize(-1, -1),
226 wxDEFAULT_FRAME_STYLE);
227
228 wxString title;
229 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
230
231 subframe->SetTitle(title);
232
233 // Give it an icon
234 #ifdef __WXMSW__
235 subframe->SetIcon(wxIcon("chrt_icn"));
236 #else
237 subframe->SetIcon(wxIcon( mondrian_xpm ));
238 #endif
239
240 // Make a menubar
241 wxMenu *file_menu = new wxMenu;
242
243 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N");
244 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
245 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X");
246
247 wxMenu *option_menu = new wxMenu;
248
249 option_menu->Append(MDI_REFRESH, "&Refresh picture\tF5");
250 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
251
252 wxMenu *help_menu = new wxMenu;
253 help_menu->Append(MDI_ABOUT, "&About\tF1");
254
255 wxMenuBar *menu_bar = new wxMenuBar;
256
257 menu_bar->Append(file_menu, "&File");
258 menu_bar->Append(option_menu, "&Options");
259 menu_bar->Append(help_menu, "&Help");
260
261 // Associate the menu bar with the frame
262 subframe->SetMenuBar(menu_bar);
263
264 subframe->CreateStatusBar();
265 subframe->SetStatusText(title);
266
267 int width, height;
268 subframe->GetClientSize(&width, &height);
269 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
270 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
271 subframe->canvas = canvas;
272
273 // Give it scrollbars
274 canvas->SetScrollbars(20, 20, 50, 50);
275
276 subframe->Show(TRUE);
277 }
278
279 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
280 {
281 int w, h;
282 GetClientSize(&w, &h);
283
284 textWindow->SetSize(0, 0, 200, h);
285 GetClientWindow()->SetSize(200, 0, w - 200, h);
286 }
287
288 void MyFrame::InitToolBar(wxToolBar* toolBar)
289 {
290 wxBitmap* bitmaps[8];
291
292 #ifdef __WXMSW__
293 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
294 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
295 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
296 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
297 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
298 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
299 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
300 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
301 #else
302 bitmaps[0] = new wxBitmap( new_xpm );
303 bitmaps[1] = new wxBitmap( open_xpm );
304 bitmaps[2] = new wxBitmap( save_xpm );
305 bitmaps[3] = new wxBitmap( copy_xpm );
306 bitmaps[4] = new wxBitmap( cut_xpm );
307 bitmaps[5] = new wxBitmap( paste_xpm );
308 bitmaps[6] = new wxBitmap( print_xpm );
309 bitmaps[7] = new wxBitmap( help_xpm );
310 #endif
311
312 #ifdef __WXMSW__
313 int width = 24;
314 #else
315 int width = 16;
316 #endif
317 int currentX = 5;
318
319 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
320 currentX += width + 5;
321 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
322 currentX += width + 5;
323 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
324 currentX += width + 5;
325 toolBar->AddSeparator();
326 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
327 currentX += width + 5;
328 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
329 currentX += width + 5;
330 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
331 currentX += width + 5;
332 toolBar->AddSeparator();
333 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
334 currentX += width + 5;
335 toolBar->AddSeparator();
336 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
337
338 toolBar->Realize();
339
340 int i;
341 for (i = 0; i < 8; i++)
342 delete bitmaps[i];
343 }
344
345 // ---------------------------------------------------------------------------
346 // MyCanvas
347 // ---------------------------------------------------------------------------
348
349 // Define a constructor for my canvas
350 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
351 : wxScrolledWindow(parent, -1, pos, size,
352 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
353 {
354 SetBackgroundColour(wxColour("WHITE"));
355
356 m_dirty = FALSE;
357 }
358
359 // Define the repainting behaviour
360 void MyCanvas::OnDraw(wxDC& dc)
361 {
362 dc.SetFont(*wxSWISS_FONT);
363 dc.SetPen(*wxGREEN_PEN);
364 dc.DrawLine(0, 0, 200, 200);
365 dc.DrawLine(200, 0, 0, 200);
366
367 dc.SetBrush(*wxCYAN_BRUSH);
368 dc.SetPen(*wxRED_PEN);
369 dc.DrawRectangle(100, 100, 100, 50);
370 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
371
372 dc.DrawEllipse(250, 250, 100, 50);
373 #if wxUSE_SPLINES
374 dc.DrawSpline(50, 200, 50, 100, 200, 10);
375 #endif // wxUSE_SPLINES
376 dc.DrawLine(50, 230, 200, 230);
377 dc.DrawText("This is a test string", 50, 230);
378
379 wxPoint points[3];
380 points[0].x = 200; points[0].y = 300;
381 points[1].x = 100; points[1].y = 400;
382 points[2].x = 300; points[2].y = 400;
383
384 dc.DrawPolygon(3, points);
385 }
386
387 // This implements a tiny doodling program! Drag the mouse using the left
388 // button.
389 void MyCanvas::OnEvent(wxMouseEvent& event)
390 {
391 wxClientDC dc(this);
392 PrepareDC(dc);
393
394 wxPoint pt(event.GetLogicalPosition(dc));
395
396 if (xpos > -1 && ypos > -1 && event.Dragging())
397 {
398 dc.SetPen(*wxBLACK_PEN);
399 dc.DrawLine(xpos, ypos, pt.x, pt.y);
400
401 m_dirty = TRUE;
402 }
403
404 xpos = pt.x;
405 ypos = pt.y;
406 }
407
408 // ---------------------------------------------------------------------------
409 // MyChild
410 // ---------------------------------------------------------------------------
411
412 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
413 const wxPoint& pos, const wxSize& size,
414 const long style)
415 : wxMDIChildFrame(parent, -1, title, pos, size, style)
416 {
417 canvas = (MyCanvas *) NULL;
418 my_children.Append(this);
419 }
420
421 MyChild::~MyChild()
422 {
423 my_children.DeleteObject(this);
424 }
425
426 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
427 {
428 Close(TRUE);
429 }
430
431 void MyChild::OnUpdateRefresh(wxUpdateUIEvent& event)
432 {
433 event.Enable( canvas && canvas->IsDirty() );
434 }
435
436 void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
437 {
438 if ( canvas )
439 canvas->Refresh();
440 }
441
442 void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
443 {
444 static wxString s_title = _T("Canvas Frame");
445
446 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
447 _T("MDI sample question"),
448 s_title,
449 GetParent());
450 if ( !title )
451 return;
452
453 s_title = title;
454 SetTitle(s_title);
455 }
456
457 void MyChild::OnActivate(wxActivateEvent& event)
458 {
459 if ( event.GetActive() && canvas )
460 canvas->SetFocus();
461 }
462
463 void MyChild::OnClose(wxCloseEvent& event)
464 {
465 if ( canvas && canvas->IsDirty() )
466 {
467 if ( wxMessageBox("Really close?", "Please confirm",
468 wxICON_QUESTION | wxYES_NO) != wxYES )
469 {
470 event.Veto();
471
472 return;
473 }
474 }
475
476 gs_nFrames--;
477
478 event.Skip();
479 }
480
481