include correct XPMs (Unix compilation fix after last commit)
[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
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(__WXMSW__)
35 #include "../sample.xpm"
36 #include "chart.xpm"
37 #endif
38
39 #include "bitmaps/new.xpm"
40 #include "bitmaps/open.xpm"
41 #include "bitmaps/save.xpm"
42 #include "bitmaps/copy.xpm"
43 #include "bitmaps/cut.xpm"
44 #include "bitmaps/paste.xpm"
45 #include "bitmaps/print.xpm"
46 #include "bitmaps/help.xpm"
47
48
49 #include "mdi.h"
50
51 IMPLEMENT_APP(MyApp)
52
53 // ---------------------------------------------------------------------------
54 // global variables
55 // ---------------------------------------------------------------------------
56
57 MyFrame *frame = (MyFrame *) NULL;
58 wxList my_children;
59
60 // For drawing lines in a canvas
61 static long xpos = -1;
62 static long ypos = -1;
63
64 static int gs_nFrames = 0;
65
66 // ---------------------------------------------------------------------------
67 // event tables
68 // ---------------------------------------------------------------------------
69
70 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
71 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
72 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
73 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
74
75 EVT_CLOSE(MyFrame::OnClose)
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 #if wxUSE_CLIPBOARD
90 EVT_MENU(wxID_PASTE, MyChild::OnPaste)
91 EVT_UPDATE_UI(wxID_PASTE, MyChild::OnUpdatePaste)
92 #endif // wxUSE_CLIPBOARD
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, wxID_ANY, _T("MDI Demo"),
118 wxDefaultPosition, wxSize(500, 400),
119 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
120 #if 0
121 // Experimental: change the window menu
122 wxMenu* windowMenu = new wxMenu;
123 windowMenu->Append(5000, _T("My menu item!"));
124 frame->SetWindowMenu(windowMenu);
125 #endif
126
127 // Give it an icon
128 frame->SetIcon(wxICON(sample));
129
130 // Make a menubar
131 wxMenu *file_menu = new wxMenu;
132
133 file_menu->Append(MDI_NEW_WINDOW, _T("&New window\tCtrl-N"), _T("Create a new child window"));
134 file_menu->Append(MDI_QUIT, _T("&Exit\tAlt-X"), _T("Quit the program"));
135
136 wxMenu *help_menu = new wxMenu;
137 help_menu->Append(MDI_ABOUT, _T("&About\tF1"));
138
139 wxMenuBar *menu_bar = new wxMenuBar;
140
141 menu_bar->Append(file_menu, _T("&File"));
142 menu_bar->Append(help_menu, _T("&Help"));
143
144 // Associate the menu bar with the frame
145 frame->SetMenuBar(menu_bar);
146
147 #if wxUSE_STATUSBAR
148 frame->CreateStatusBar();
149 #endif // wxUSE_STATUSBAR
150
151 frame->Show(true);
152
153 SetTopWindow(frame);
154
155 return true;
156 }
157
158 // ---------------------------------------------------------------------------
159 // MyFrame
160 // ---------------------------------------------------------------------------
161
162 // Define my frame constructor
163 MyFrame::MyFrame(wxWindow *parent,
164 const wxWindowID id,
165 const wxString& title,
166 const wxPoint& pos,
167 const wxSize& size,
168 const long style)
169 : wxMDIParentFrame(parent, id, title, pos, size, style)
170 {
171 textWindow = new wxTextCtrl(this, wxID_ANY, _T("A help window"),
172 wxDefaultPosition, wxDefaultSize,
173 wxTE_MULTILINE | wxSUNKEN_BORDER);
174
175 #if wxUSE_TOOLBAR
176 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
177 InitToolBar(GetToolBar());
178 #endif // wxUSE_TOOLBAR
179
180 #if wxUSE_ACCEL
181 // Accelerators
182 wxAcceleratorEntry entries[3];
183 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
184 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
185 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
186 wxAcceleratorTable accel(3, entries);
187 SetAcceleratorTable(accel);
188 #endif // wxUSE_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, _T("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(_T("wxWidgets 2.0 MDI Demo\n")
217 _T("Author: Julian Smart (c) 1997\n")
218 _T("Usage: mdi.exe"), _T("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, _T("Canvas Frame"));
225
226 wxString title;
227 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
228
229 subframe->SetTitle(title);
230
231 // Give it an icon
232 subframe->SetIcon(wxICON(chart));
233
234 #if wxUSE_MENUS
235 // Make a menubar
236 wxMenu *file_menu = new wxMenu;
237
238 file_menu->Append(MDI_NEW_WINDOW, _T("&New window"));
239 file_menu->Append(MDI_CHILD_QUIT, _T("&Close child"), _T("Close this window"));
240 file_menu->Append(MDI_QUIT, _T("&Exit"));
241
242 wxMenu *option_menu = new wxMenu;
243
244 option_menu->Append(MDI_REFRESH, _T("&Refresh picture"));
245 option_menu->Append(MDI_CHANGE_TITLE, _T("Change &title...\tCtrl-T"));
246 option_menu->AppendSeparator();
247 option_menu->Append(MDI_CHANGE_POSITION, _T("Move frame\tCtrl-M"));
248 option_menu->Append(MDI_CHANGE_SIZE, _T("Resize frame\tCtrl-S"));
249 #if wxUSE_CLIPBOARD
250 option_menu->AppendSeparator();
251 option_menu->Append(wxID_PASTE, _T("Copy text from clipboard\tCtrl-V"));
252 #endif // wxUSE_CLIPBOARD
253
254 wxMenu *help_menu = new wxMenu;
255 help_menu->Append(MDI_ABOUT, _T("&About"));
256
257 wxMenuBar *menu_bar = new wxMenuBar;
258
259 menu_bar->Append(file_menu, _T("&File"));
260 menu_bar->Append(option_menu, _T("&Child"));
261 menu_bar->Append(help_menu, _T("&Help"));
262
263 // Associate the menu bar with the frame
264 subframe->SetMenuBar(menu_bar);
265 #endif // wxUSE_MENUS
266
267 #if wxUSE_STATUSBAR
268 subframe->CreateStatusBar();
269 subframe->SetStatusText(title);
270 #endif // wxUSE_STATUSBAR
271
272 int width, height;
273 subframe->GetClientSize(&width, &height);
274 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
275 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
276 subframe->canvas = canvas;
277
278 // Give it scrollbars
279 canvas->SetScrollbars(20, 20, 50, 50);
280
281 subframe->Show(true);
282 }
283
284 void MyFrame::OnSize(wxSizeEvent&
285 #ifdef __WXUNIVERSAL__
286 event
287 #else
288 WXUNUSED(event)
289 #endif
290 )
291 {
292 int w, h;
293 GetClientSize(&w, &h);
294
295 textWindow->SetSize(0, 0, 200, h);
296 GetClientWindow()->SetSize(200, 0, w - 200, h);
297
298 // FIXME: On wxX11, we need the MDI frame to process this
299 // event, but on other platforms this should not
300 // be done.
301 #ifdef __WXUNIVERSAL__
302 event.Skip();
303 #endif
304 }
305
306 #if wxUSE_TOOLBAR
307 void MyFrame::InitToolBar(wxToolBar* toolBar)
308 {
309 wxBitmap bitmaps[8];
310
311 bitmaps[0] = wxBitmap( new_xpm );
312 bitmaps[1] = wxBitmap( open_xpm );
313 bitmaps[2] = wxBitmap( save_xpm );
314 bitmaps[3] = wxBitmap( copy_xpm );
315 bitmaps[4] = wxBitmap( cut_xpm );
316 bitmaps[5] = wxBitmap( paste_xpm );
317 bitmaps[6] = wxBitmap( print_xpm );
318 bitmaps[7] = wxBitmap( help_xpm );
319
320 toolBar->AddTool(MDI_NEW_WINDOW, _T("New"), bitmaps[0], _T("New file"));
321 toolBar->AddTool(1, _T("Open"), bitmaps[1], _T("Open file"));
322 toolBar->AddTool(2, _T("Save"), bitmaps[2], _T("Save file"));
323 toolBar->AddSeparator();
324 toolBar->AddTool(3, _T("Copy"), bitmaps[3], _T("Copy"));
325 toolBar->AddTool(4, _T("Cut"), bitmaps[4], _T("Cut"));
326 toolBar->AddTool(5, _T("Paste"), bitmaps[5], _T("Paste"));
327 toolBar->AddSeparator();
328 toolBar->AddTool(6, _T("Print"), bitmaps[6], _T("Print"));
329 toolBar->AddSeparator();
330 toolBar->AddTool(MDI_ABOUT, _T("About"), bitmaps[7], _T("Help"));
331
332 toolBar->Realize();
333 }
334 #endif // wxUSE_TOOLBAR
335
336 // ---------------------------------------------------------------------------
337 // MyCanvas
338 // ---------------------------------------------------------------------------
339
340 // Define a constructor for my canvas
341 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
342 : wxScrolledWindow(parent, wxID_ANY, pos, size,
343 wxSUNKEN_BORDER |
344 wxNO_FULL_REPAINT_ON_RESIZE |
345 wxVSCROLL | wxHSCROLL)
346 {
347 SetBackgroundColour(wxColour(_T("WHITE")));
348
349 m_dirty = false;
350 }
351
352 // Define the repainting behaviour
353 void MyCanvas::OnDraw(wxDC& dc)
354 {
355 if ( !m_text.empty() )
356 dc.DrawText(m_text, 10, 10);
357
358 dc.SetFont(*wxSWISS_FONT);
359 dc.SetPen(*wxGREEN_PEN);
360 dc.DrawLine(0, 0, 200, 200);
361 dc.DrawLine(200, 0, 0, 200);
362
363 dc.SetBrush(*wxCYAN_BRUSH);
364 dc.SetPen(*wxRED_PEN);
365 dc.DrawRectangle(100, 100, 100, 50);
366 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
367
368 dc.DrawEllipse(250, 250, 100, 50);
369 #if wxUSE_SPLINES
370 dc.DrawSpline(50, 200, 50, 100, 200, 10);
371 #endif // wxUSE_SPLINES
372 dc.DrawLine(50, 230, 200, 230);
373 dc.DrawText(_T("This is a test string"), 50, 230);
374
375 wxPoint points[3];
376 points[0].x = 200; points[0].y = 300;
377 points[1].x = 100; points[1].y = 400;
378 points[2].x = 300; points[2].y = 400;
379
380 dc.DrawPolygon(3, points);
381 }
382
383 // This implements a tiny doodling program! Drag the mouse using the left
384 // button.
385 void MyCanvas::OnEvent(wxMouseEvent& event)
386 {
387 wxClientDC dc(this);
388 PrepareDC(dc);
389
390 wxPoint pt(event.GetLogicalPosition(dc));
391
392 if (xpos > -1 && ypos > -1 && event.Dragging())
393 {
394 dc.SetPen(*wxBLACK_PEN);
395 dc.DrawLine(xpos, ypos, pt.x, pt.y);
396
397 m_dirty = true;
398 }
399
400 xpos = pt.x;
401 ypos = pt.y;
402 }
403
404 // ---------------------------------------------------------------------------
405 // MyChild
406 // ---------------------------------------------------------------------------
407
408 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title)
409 : wxMDIChildFrame(parent, wxID_ANY, title, wxDefaultPosition, wxDefaultSize,
410 wxDEFAULT_FRAME_STYLE | wxNO_FULL_REPAINT_ON_RESIZE)
411 {
412 canvas = (MyCanvas *) NULL;
413 my_children.Append(this);
414
415 // this should work for MDI frames as well as for normal ones
416 SetSizeHints(100, 100);
417 }
418
419 MyChild::~MyChild()
420 {
421 my_children.DeleteObject(this);
422 }
423
424 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
425 {
426 Close(true);
427 }
428
429 void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
430 {
431 if ( canvas )
432 canvas->Refresh();
433 }
434
435 void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
436 {
437 Move(10, 10);
438 }
439
440 void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
441 {
442 SetClientSize(100, 100);
443 }
444
445 void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
446 {
447 #if wxUSE_TEXTDLG
448 static wxString s_title = _T("Canvas Frame");
449
450 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
451 _T("MDI sample question"),
452 s_title,
453 GetParent()->GetParent());
454 if ( !title )
455 return;
456
457 s_title = title;
458 SetTitle(s_title);
459 #endif // wxUSE_TEXTDLG
460 }
461
462 void MyChild::OnActivate(wxActivateEvent& event)
463 {
464 if ( event.GetActive() && canvas )
465 canvas->SetFocus();
466 }
467
468 void MyChild::OnMove(wxMoveEvent& event)
469 {
470 // VZ: here everything is totally wrong under MSW, the positions are
471 // different and both wrong (pos2 is off by 2 pixels for me which seems
472 // to be the width of the MDI canvas border)
473 wxPoint pos1 = event.GetPosition(),
474 pos2 = GetPosition();
475 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
476 pos1.x, pos1.y, pos2.x, pos2.y);
477
478 event.Skip();
479 }
480
481 void MyChild::OnSize(wxSizeEvent& event)
482 {
483 // VZ: under MSW the size event carries the client size (quite
484 // unexpectedly) *except* for the very first one which has the full
485 // size... what should it really be? TODO: check under wxGTK
486 wxSize size1 = event.GetSize(),
487 size2 = GetSize(),
488 size3 = GetClientSize();
489 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
490 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
491
492 event.Skip();
493 }
494
495 void MyChild::OnClose(wxCloseEvent& event)
496 {
497 if ( canvas && canvas->IsDirty() )
498 {
499 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
500 wxICON_QUESTION | wxYES_NO) != wxYES )
501 {
502 event.Veto();
503
504 return;
505 }
506 }
507
508 gs_nFrames--;
509
510 event.Skip();
511 }
512
513 #if wxUSE_CLIPBOARD
514
515 #include "wx/clipbrd.h"
516
517 void MyChild::OnPaste(wxCommandEvent& WXUNUSED(event))
518 {
519 wxClipboardLocker lock;
520 wxTextDataObject data;
521 canvas->SetText(wxTheClipboard->GetData(data) ? data.GetText().c_str()
522 : _T("No text on clipboard"));
523 }
524
525 void MyChild::OnUpdatePaste(wxUpdateUIEvent& event)
526 {
527 wxClipboardLocker lock;
528 event.Enable( wxTheClipboard->IsSupported(wxDF_TEXT) );
529 }
530
531 #endif // wxUSE_CLIPBOARD