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