]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
Applied patch [ 598016 ] remove flicker during combo box creation
[wxWidgets.git] / samples / mdi / mdi.cpp
CommitLineData
c801d85f
KB
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
c52d95b4 9// Licence: wxWindows license
c801d85f
KB
10/////////////////////////////////////////////////////////////////////////////
11
c52d95b4
VZ
12// ===========================================================================
13// declarations
14// ===========================================================================
15
16// ---------------------------------------------------------------------------
17// headers
18// ---------------------------------------------------------------------------
19
c801d85f
KB
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
c52d95b4 24 #pragma hdrstop
c801d85f
KB
25#endif
26
27#ifndef WX_PRECOMP
c52d95b4
VZ
28 #include "wx/wx.h"
29 #include "wx/mdi.h"
c801d85f
KB
30#endif
31
012f2cb2 32#include "wx/toolbar.h"
c801d85f 33
618f2efa 34#if defined(__WXGTK__) || defined(__WXX11__) || defined(__WXMOTIF__) || defined(__WXMAC__)
c52d95b4 35 #include "mondrian.xpm"
716b7364
RR
36#endif
37
d04d32c4
CE
38#include "bitmaps/new.xpm"
39#include "bitmaps/open.xpm"
40#include "bitmaps/save.xpm"
41#include "bitmaps/copy.xpm"
42#include "bitmaps/cut.xpm"
43#include "bitmaps/paste.xpm"
44#include "bitmaps/print.xpm"
45#include "bitmaps/help.xpm"
46
47
c801d85f
KB
48#include "mdi.h"
49
c52d95b4
VZ
50IMPLEMENT_APP(MyApp)
51
52// ---------------------------------------------------------------------------
53// global variables
54// ---------------------------------------------------------------------------
55
c67daf87 56MyFrame *frame = (MyFrame *) NULL;
c801d85f
KB
57wxList my_children;
58
c801d85f 59// For drawing lines in a canvas
c52d95b4
VZ
60static long xpos = -1;
61static long ypos = -1;
62
63static int gs_nFrames = 0;
64
65// ---------------------------------------------------------------------------
66// event tables
67// ---------------------------------------------------------------------------
68
69BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
70 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
71 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
72 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
73
74 EVT_CLOSE(MyFrame::OnClose)
75
76 EVT_SIZE(MyFrame::OnSize)
77END_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.
82BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
83 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
84 EVT_MENU(MDI_REFRESH, MyChild::OnRefresh)
f6bcfd97 85 EVT_MENU(MDI_CHANGE_TITLE, MyChild::OnChangeTitle)
3d8dea7e
VZ
86 EVT_MENU(MDI_CHANGE_POSITION, MyChild::OnChangePosition)
87 EVT_MENU(MDI_CHANGE_SIZE, MyChild::OnChangeSize)
88
3d8dea7e
VZ
89 EVT_SIZE(MyChild::OnSize)
90 EVT_MOVE(MyChild::OnMove)
c52d95b4
VZ
91
92 EVT_CLOSE(MyChild::OnClose)
93END_EVENT_TABLE()
94
95BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
96 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
97END_EVENT_TABLE()
98
99// ===========================================================================
100// implementation
101// ===========================================================================
c801d85f 102
c52d95b4
VZ
103// ---------------------------------------------------------------------------
104// MyApp
105// ---------------------------------------------------------------------------
c801d85f
KB
106
107// Initialise this in OnInit, not statically
c52d95b4 108bool MyApp::OnInit()
c801d85f 109{
c52d95b4 110 // Create the main frame window
c801d85f 111
c52d95b4
VZ
112 frame = new MyFrame((wxFrame *)NULL, -1, "MDI Demo",
113 wxPoint(-1, -1), wxSize(500, 400),
114 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
df61c009
JS
115#ifdef __WXMSW__
116#if 0
117 // Experimental: change the window menu
118 wxMenu* windowMenu = new wxMenu;
119 windowMenu->Append(5000, "My menu item!");
120 frame->SetWindowMenu(windowMenu);
121#endif
122#endif
c801d85f 123
c52d95b4 124 // Give it an icon
2049ba38 125#ifdef __WXMSW__
c52d95b4 126 frame->SetIcon(wxIcon("mdi_icn"));
52cbfcf0 127#else
c52d95b4 128 frame->SetIcon(wxIcon( mondrian_xpm ));
c801d85f 129#endif
c801d85f 130
c52d95b4
VZ
131 // Make a menubar
132 wxMenu *file_menu = new wxMenu;
c801d85f 133
f6bcfd97
BP
134 file_menu->Append(MDI_NEW_WINDOW, "&New window\tCtrl-N", "Create a new child window");
135 file_menu->Append(MDI_QUIT, "&Exit\tAlt-X", "Quit the program");
c801d85f 136
c52d95b4 137 wxMenu *help_menu = new wxMenu;
f6bcfd97 138 help_menu->Append(MDI_ABOUT, "&About\tF1");
c801d85f 139
c52d95b4 140 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 141
c52d95b4
VZ
142 menu_bar->Append(file_menu, "&File");
143 menu_bar->Append(help_menu, "&Help");
c801d85f 144
c52d95b4
VZ
145 // Associate the menu bar with the frame
146 frame->SetMenuBar(menu_bar);
c801d85f 147
c52d95b4 148 frame->CreateStatusBar();
c801d85f 149
c52d95b4 150 frame->Show(TRUE);
c801d85f 151
c52d95b4 152 SetTopWindow(frame);
c801d85f 153
c52d95b4 154 return TRUE;
c801d85f
KB
155}
156
c52d95b4
VZ
157// ---------------------------------------------------------------------------
158// MyFrame
159// ---------------------------------------------------------------------------
c801d85f
KB
160
161// Define my frame constructor
c52d95b4
VZ
162MyFrame::MyFrame(wxWindow *parent,
163 const wxWindowID id,
164 const wxString& title,
165 const wxPoint& pos,
166 const wxSize& size,
167 const long style)
3ebcfb76
VZ
168 : wxMDIParentFrame(parent, id, title, pos, size,
169 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 170{
c52d95b4
VZ
171 textWindow = new wxTextCtrl(this, -1, "A help window",
172 wxDefaultPosition, wxDefaultSize,
173 wxTE_MULTILINE | wxSUNKEN_BORDER);
c801d85f 174
fa762db4 175 CreateToolBar(wxNO_BORDER | wxTB_FLAT | wxTB_HORIZONTAL);
81d66cf3 176 InitToolBar(GetToolBar());
57a7b7c1 177
57a7b7c1
JS
178 // Accelerators
179 wxAcceleratorEntry entries[3];
180 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
181 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
182 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
183 wxAcceleratorTable accel(3, entries);
184 SetAcceleratorTable(accel);
c801d85f
KB
185}
186
c52d95b4
VZ
187void MyFrame::OnClose(wxCloseEvent& event)
188{
189 if ( event.CanVeto() && (gs_nFrames > 0) )
190 {
191 wxString msg;
ddb6bc71 192 msg.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames);
c52d95b4
VZ
193 if ( wxMessageBox(msg, "Please confirm",
194 wxICON_QUESTION | wxYES_NO) != wxYES )
195 {
196 event.Veto();
197
198 return;
199 }
200 }
201
202 event.Skip();
203}
204
205void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 206{
c52d95b4 207 Close();
c801d85f
KB
208}
209
e3e65dac 210void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
c801d85f 211{
c52d95b4
VZ
212 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
213 "Author: Julian Smart (c) 1997\n"
214 "Usage: mdi.exe", "About MDI Demo");
c801d85f
KB
215}
216
e3e65dac 217void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
c801d85f 218{
c52d95b4
VZ
219 // Make another frame, containing a canvas
220 MyChild *subframe = new MyChild(frame, "Canvas Frame",
221 wxPoint(-1, -1), wxSize(-1, -1),
222 wxDEFAULT_FRAME_STYLE);
c801d85f 223
c52d95b4 224 wxString title;
ddb6bc71 225 title.Printf(_T("Canvas Frame %d"), ++gs_nFrames);
c801d85f 226
c52d95b4
VZ
227 subframe->SetTitle(title);
228
229 // Give it an icon
2049ba38 230#ifdef __WXMSW__
c52d95b4 231 subframe->SetIcon(wxIcon("chrt_icn"));
8704bf74 232#else
c52d95b4 233 subframe->SetIcon(wxIcon( mondrian_xpm ));
c801d85f
KB
234#endif
235
c52d95b4
VZ
236 // Make a menubar
237 wxMenu *file_menu = new wxMenu;
238
fa762db4 239 file_menu->Append(MDI_NEW_WINDOW, "&New window");
c52d95b4 240 file_menu->Append(MDI_CHILD_QUIT, "&Close child", "Close this window");
fa762db4 241 file_menu->Append(MDI_QUIT, "&Exit");
c801d85f 242
c52d95b4 243 wxMenu *option_menu = new wxMenu;
c801d85f 244
fa762db4 245 option_menu->Append(MDI_REFRESH, "&Refresh picture");
f6bcfd97 246 option_menu->Append(MDI_CHANGE_TITLE, "Change &title...\tCtrl-T");
fa762db4
VZ
247 option_menu->AppendSeparator();
248 option_menu->Append(MDI_CHANGE_POSITION, "Move frame\tCtrl-M");
249 option_menu->Append(MDI_CHANGE_SIZE, "Resize frame\tCtrl-S");
c801d85f 250
c52d95b4 251 wxMenu *help_menu = new wxMenu;
fa762db4 252 help_menu->Append(MDI_ABOUT, "&About");
c801d85f 253
c52d95b4 254 wxMenuBar *menu_bar = new wxMenuBar;
c801d85f 255
c52d95b4 256 menu_bar->Append(file_menu, "&File");
fa762db4 257 menu_bar->Append(option_menu, "&Child");
c52d95b4 258 menu_bar->Append(help_menu, "&Help");
c801d85f 259
c52d95b4
VZ
260 // Associate the menu bar with the frame
261 subframe->SetMenuBar(menu_bar);
c801d85f 262
c41ea66a
VZ
263 subframe->CreateStatusBar();
264 subframe->SetStatusText(title);
265
c52d95b4
VZ
266 int width, height;
267 subframe->GetClientSize(&width, &height);
268 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
269 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
270 subframe->canvas = canvas;
c801d85f 271
c52d95b4
VZ
272 // Give it scrollbars
273 canvas->SetScrollbars(20, 20, 50, 50);
c801d85f 274
c52d95b4 275 subframe->Show(TRUE);
c801d85f
KB
276}
277
b9f933ab 278void MyFrame::OnSize(wxSizeEvent& event)
c52d95b4
VZ
279{
280 int w, h;
281 GetClientSize(&w, &h);
282
283 textWindow->SetSize(0, 0, 200, h);
284 GetClientWindow()->SetSize(200, 0, w - 200, h);
b9f933ab
JS
285
286 event.Skip();
c52d95b4
VZ
287}
288
289void MyFrame::InitToolBar(wxToolBar* toolBar)
290{
291 wxBitmap* bitmaps[8];
292
c52d95b4
VZ
293 bitmaps[0] = new wxBitmap( new_xpm );
294 bitmaps[1] = new wxBitmap( open_xpm );
295 bitmaps[2] = new wxBitmap( save_xpm );
296 bitmaps[3] = new wxBitmap( copy_xpm );
297 bitmaps[4] = new wxBitmap( cut_xpm );
298 bitmaps[5] = new wxBitmap( paste_xpm );
299 bitmaps[6] = new wxBitmap( print_xpm );
300 bitmaps[7] = new wxBitmap( help_xpm );
c52d95b4 301
c52d95b4 302 int width = 24;
c52d95b4
VZ
303 int currentX = 5;
304
305 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
306 currentX += width + 5;
307 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
308 currentX += width + 5;
309 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
310 currentX += width + 5;
311 toolBar->AddSeparator();
312 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
313 currentX += width + 5;
314 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
315 currentX += width + 5;
316 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
317 currentX += width + 5;
318 toolBar->AddSeparator();
319 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
320 currentX += width + 5;
321 toolBar->AddSeparator();
322 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
323
324 toolBar->Realize();
325
326 int i;
327 for (i = 0; i < 8; i++)
328 delete bitmaps[i];
329}
330
331// ---------------------------------------------------------------------------
332// MyCanvas
333// ---------------------------------------------------------------------------
c801d85f
KB
334
335// Define a constructor for my canvas
c52d95b4
VZ
336MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
337 : wxScrolledWindow(parent, -1, pos, size,
3ebcfb76
VZ
338 wxSUNKEN_BORDER |
339 wxNO_FULL_REPAINT_ON_RESIZE |
340 wxVSCROLL | wxHSCROLL)
c801d85f 341{
02800301 342 SetBackgroundColour(wxColour("WHITE"));
c52d95b4
VZ
343
344 m_dirty = FALSE;
c801d85f
KB
345}
346
347// Define the repainting behaviour
348void MyCanvas::OnDraw(wxDC& dc)
349{
c52d95b4
VZ
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);
c41ea66a 361#if wxUSE_SPLINES
c52d95b4 362 dc.DrawSpline(50, 200, 50, 100, 200, 10);
c41ea66a 363#endif // wxUSE_SPLINES
c52d95b4
VZ
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);
c801d85f
KB
373}
374
c52d95b4
VZ
375// This implements a tiny doodling program! Drag the mouse using the left
376// button.
c801d85f
KB
377void MyCanvas::OnEvent(wxMouseEvent& event)
378{
c52d95b4
VZ
379 wxClientDC dc(this);
380 PrepareDC(dc);
c801d85f 381
c52d95b4 382 wxPoint pt(event.GetLogicalPosition(dc));
c801d85f 383
c52d95b4
VZ
384 if (xpos > -1 && ypos > -1 && event.Dragging())
385 {
386 dc.SetPen(*wxBLACK_PEN);
387 dc.DrawLine(xpos, ypos, pt.x, pt.y);
c801d85f 388
c52d95b4
VZ
389 m_dirty = TRUE;
390 }
c801d85f 391
c52d95b4
VZ
392 xpos = pt.x;
393 ypos = pt.y;
394}
395
396// ---------------------------------------------------------------------------
397// MyChild
398// ---------------------------------------------------------------------------
399
400MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title,
401 const wxPoint& pos, const wxSize& size,
402 const long style)
3ebcfb76
VZ
403 : wxMDIChildFrame(parent, -1, title, pos, size,
404 style | wxNO_FULL_REPAINT_ON_RESIZE)
c801d85f 405{
c52d95b4
VZ
406 canvas = (MyCanvas *) NULL;
407 my_children.Append(this);
3ebcfb76
VZ
408
409 // this should work for MDI frames as well as for normal ones
410 SetSizeHints(100, 100);
c801d85f
KB
411}
412
c52d95b4 413MyChild::~MyChild()
c801d85f 414{
c52d95b4 415 my_children.DeleteObject(this);
c801d85f
KB
416}
417
33d0b396 418void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f 419{
c52d95b4
VZ
420 Close(TRUE);
421}
422
f6bcfd97
BP
423void MyChild::OnRefresh(wxCommandEvent& WXUNUSED(event))
424{
425 if ( canvas )
426 canvas->Refresh();
427}
428
fa762db4
VZ
429void MyChild::OnChangePosition(wxCommandEvent& WXUNUSED(event))
430{
431 Move(10, 10);
432}
433
434void MyChild::OnChangeSize(wxCommandEvent& WXUNUSED(event))
435{
436 SetClientSize(100, 100);
437}
438
f6bcfd97 439void MyChild::OnChangeTitle(wxCommandEvent& WXUNUSED(event))
c52d95b4 440{
f6bcfd97
BP
441 static wxString s_title = _T("Canvas Frame");
442
443 wxString title = wxGetTextFromUser(_T("Enter the new title for MDI child"),
444 _T("MDI sample question"),
445 s_title,
fa762db4 446 GetParent()->GetParent());
f6bcfd97
BP
447 if ( !title )
448 return;
449
450 s_title = title;
451 SetTitle(s_title);
c801d85f
KB
452}
453
454void MyChild::OnActivate(wxActivateEvent& event)
455{
c52d95b4
VZ
456 if ( event.GetActive() && canvas )
457 canvas->SetFocus();
c801d85f
KB
458}
459
fa762db4
VZ
460void MyChild::OnMove(wxMoveEvent& event)
461{
462 // VZ: here everything is totally wrong under MSW, the positions are
463 // different and both wrong (pos2 is off by 2 pixels for me which seems
464 // to be the width of the MDI canvas border)
465 wxPoint pos1 = event.GetPosition(),
466 pos2 = GetPosition();
4693b20c 467 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
fa762db4
VZ
468 pos1.x, pos1.y, pos2.x, pos2.y);
469
470 event.Skip();
471}
472
473void MyChild::OnSize(wxSizeEvent& event)
474{
475 // VZ: under MSW the size event carries the client size (quite
476 // unexpectedly) *except* for the very first one which has the full
477 // size... what should it really be? TODO: check under wxGTK
478 wxSize size1 = event.GetSize(),
479 size2 = GetSize(),
480 size3 = GetClientSize();
4693b20c 481 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
fa762db4
VZ
482 size1.x, size1.y, size2.x, size2.y, size3.x, size3.y);
483
484 event.Skip();
485}
486
c52d95b4 487void MyChild::OnClose(wxCloseEvent& event)
c801d85f 488{
c52d95b4
VZ
489 if ( canvas && canvas->IsDirty() )
490 {
491 if ( wxMessageBox("Really close?", "Please confirm",
492 wxICON_QUESTION | wxYES_NO) != wxYES )
493 {
494 event.Veto();
c801d85f 495
c52d95b4
VZ
496 return;
497 }
498 }
c801d85f 499
c52d95b4
VZ
500 gs_nFrames--;
501
502 event.Skip();
c801d85f
KB
503}
504
c52d95b4 505