]> git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
added test of wxFileSelector() def ext handling
[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(__WXMOTIF__)
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"
44 #endif
45
46 #include "mdi.h"
47
48 IMPLEMENT_APP(MyApp)
49
50 // ---------------------------------------------------------------------------
51 // global variables
52 // ---------------------------------------------------------------------------
53
54 MyFrame *frame = (MyFrame *) NULL;
55 wxList my_children;
56
57 // For drawing lines in a canvas
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)
83 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
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 // ===========================================================================
95
96 // ---------------------------------------------------------------------------
97 // MyApp
98 // ---------------------------------------------------------------------------
99
100 // Initialise this in OnInit, not statically
101 bool MyApp::OnInit()
102 {
103 // Create the main frame window
104
105 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
106 wxPoint(-1, -1), wxSize(500, 400),
107 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
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
116
117 // Give it an icon
118 #ifdef __WXMSW__
119 frame->SetIcon(wxIcon("mdi_icn"));
120 #else
121 frame->SetIcon(wxIcon( mondrian_xpm ));
122 #endif
123
124 // Make a menubar
125 wxMenu *file_menu = new wxMenu;
126
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");
129
130 wxMenu *help_menu = new wxMenu;
131 help_menu->Append(MDI_ABOUT, "&About\tF1");
132
133 wxMenuBar *menu_bar = new wxMenuBar;
134
135 menu_bar->Append(file_menu, "&File");
136 menu_bar->Append(help_menu, "&Help");
137
138 // Associate the menu bar with the frame
139 frame->SetMenuBar(menu_bar);
140
141 frame->CreateStatusBar();
142
143 frame->Show(TRUE);
144
145 SetTopWindow(frame);
146
147 return TRUE;
148 }
149
150 // ---------------------------------------------------------------------------
151 // MyFrame
152 // ---------------------------------------------------------------------------
153
154 // Define my frame constructor
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)
162 {
163 textWindow = new wxTextCtrl(this, -1, "A help window",
164 wxDefaultPosition, wxDefaultSize,
165 wxTE_MULTILINE | wxSUNKEN_BORDER);
166
167 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
168 InitToolBar(GetToolBar());
169
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);
177 }
178
179 void MyFrame::OnClose(wxCloseEvent& event)
180 {
181 if ( event.CanVeto() && (gs_nFrames > 0) )
182 {
183 wxString msg;
184 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
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))
198 {
199 Close();
200 }
201
202 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
203 {
204 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
205 "Author: Julian Smart (c) 1997\n"
206 "Usage: mdi.exe", "About MDI Demo");
207 }
208
209 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
210 {
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);
215
216 wxString title;
217 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
218
219 subframe->SetTitle(title);
220
221 // Give it an icon
222 #ifdef __WXMSW__
223 subframe->SetIcon(wxIcon("chrt_icn"));
224 #else
225 subframe->SetIcon(wxIcon( mondrian_xpm ));
226 #endif
227
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");
234
235 wxMenu *option_menu = new wxMenu;
236
237 option_menu->Append(MDI_REFRESH, "&Refresh picture");
238 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
239
240 wxMenu *help_menu = new wxMenu;
241 help_menu->Append(MDI_ABOUT, "&About");
242
243 wxMenuBar *menu_bar = new wxMenuBar;
244
245 menu_bar->Append(file_menu, "&File");
246 menu_bar->Append(option_menu, "&Options");
247 menu_bar->Append(help_menu, "&Help");
248
249 // Associate the menu bar with the frame
250 subframe->SetMenuBar(menu_bar);
251
252 subframe->CreateStatusBar();
253 subframe->SetStatusText(title);
254
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;
260
261 // Give it scrollbars
262 canvas->SetScrollbars(20, 20, 50, 50);
263
264 subframe->Show(TRUE);
265 }
266
267 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
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 // ---------------------------------------------------------------------------
336
337 // Define a constructor for my canvas
338 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
339 : wxScrolledWindow(parent, -1, pos, size,
340 wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
341 {
342 SetBackgroundColour(wxColour("WHITE"));
343
344 m_dirty = FALSE;
345 }
346
347 // Define the repainting behaviour
348 void MyCanvas::OnDraw(wxDC& dc)
349 {
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);
361 #if wxUSE_SPLINES
362 dc.DrawSpline(50, 200, 50, 100, 200, 10);
363 #endif // wxUSE_SPLINES
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);
373 }
374
375 // This implements a tiny doodling program! Drag the mouse using the left
376 // button.
377 void MyCanvas::OnEvent(wxMouseEvent& event)
378 {
379 wxClientDC dc(this);
380 PrepareDC(dc);
381
382 wxPoint pt(event.GetLogicalPosition(dc));
383
384 if (xpos > -1 && ypos > -1 && event.Dragging())
385 {
386 dc.SetPen(*wxBLACK_PEN);
387 dc.DrawLine(xpos, ypos, pt.x, pt.y);
388
389 m_dirty = TRUE;
390 }
391
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)
404 {
405 canvas = (MyCanvas *) NULL;
406 my_children.Append(this);
407 }
408
409 MyChild::~MyChild()
410 {
411 my_children.DeleteObject(this);
412 }
413
414 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
415 {
416 Close(TRUE);
417 }
418
419 void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
420 {
421 if ( canvas )
422 canvas->Refresh();
423 }
424
425 void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
426 {
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);
438 }
439
440 void MyChild::OnActivate(wxActivateEvent& event)
441 {
442 if ( event.GetActive() && canvas )
443 canvas->SetFocus();
444 }
445
446 void MyChild::OnClose(wxCloseEvent& event)
447 {
448 if ( canvas && canvas->IsDirty() )
449 {
450 if ( wxMessageBox("Really close?", "Please confirm",
451 wxICON_QUESTION | wxYES_NO) != wxYES )
452 {
453 event.Veto();
454
455 return;
456 }
457 }
458
459 gs_nFrames--;
460
461 event.Skip();
462 }
463
464