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