1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
5 // Modified by: 2008-10-31 Vadim Zeitlin: big clean up
8 // Copyright: (c) 1997 Julian Smart
9 // (c) 2008 Vadim Zeitlin
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
15 // ===========================================================================
17 // ---------------------------------------------------------------------------
19 // ---------------------------------------------------------------------------
21 // For compilers that support precompilation, includes "wx/wx.h".
22 #include "wx/wxprec.h"
33 #include "wx/toolbar.h"
35 #if !defined(__WXMSW__)
36 #include "../sample.xpm"
40 #include "bitmaps/new.xpm"
41 #include "bitmaps/open.xpm"
42 #include "bitmaps/save.xpm"
43 #include "bitmaps/copy.xpm"
44 #include "bitmaps/cut.xpm"
45 #include "bitmaps/paste.xpm"
46 #include "bitmaps/print.xpm"
47 #include "bitmaps/help.xpm"
49 // replace this 0 with 1 to build the sample using the generic MDI classes (you
50 // may also need to add src/generic/mdig.cpp to the build)
52 #include "wx/generic/mdig.h"
53 #define wxMDIParentFrame wxGenericMDIParentFrame
54 #define wxMDIChildFrame wxGenericMDIChildFrame
55 #define wxMDIClientWindow wxGenericMDIClientWindow
62 // ---------------------------------------------------------------------------
64 // ---------------------------------------------------------------------------
66 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
67 EVT_MENU(wxID_ABOUT
, MyFrame::OnAbout
)
68 EVT_MENU(wxID_NEW
, MyFrame::OnNewWindow
)
69 EVT_MENU(MDI_FULLSCREEN
, MyFrame::OnFullScreen
)
70 EVT_MENU(wxID_EXIT
, MyFrame::OnQuit
)
72 EVT_MENU(wxID_CLOSE_ALL
, MyFrame::OnCloseAll
)
74 EVT_CLOSE(MyFrame::OnClose
)
77 // Note that wxID_NEW and wxID_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(wxID_CLOSE
, MyChild::OnClose
)
82 EVT_MENU(MDI_REFRESH
, MyChild::OnRefresh
)
83 EVT_MENU(MDI_CHANGE_TITLE
, MyChild::OnChangeTitle
)
84 EVT_MENU(MDI_CHANGE_POSITION
, MyChild::OnChangePosition
)
85 EVT_MENU(MDI_CHANGE_SIZE
, MyChild::OnChangeSize
)
88 EVT_MENU(wxID_PASTE
, MyChild::OnPaste
)
89 EVT_UPDATE_UI(wxID_PASTE
, MyChild::OnUpdatePaste
)
90 #endif // wxUSE_CLIPBOARD
92 EVT_SIZE(MyChild::OnSize
)
93 EVT_MOVE(MyChild::OnMove
)
95 EVT_CLOSE(MyChild::OnCloseWindow
)
98 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
99 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
102 // ===========================================================================
104 // ===========================================================================
106 // ---------------------------------------------------------------------------
108 // ---------------------------------------------------------------------------
110 // Initialise this in OnInit, not statically
113 if ( !wxApp::OnInit() )
116 // Create the main frame window
118 MyFrame
*frame
= new MyFrame
;
125 // ---------------------------------------------------------------------------
127 // ---------------------------------------------------------------------------
129 // Define my frame constructor
131 : wxMDIParentFrame(NULL
, wxID_ANY
, "wxWidgets MDI Sample",
132 wxDefaultPosition
, wxSize(500, 400))
134 SetIcon(wxICON(sample
));
138 // Associate the menu bar with the frame
139 SetMenuBar(CreateMainMenubar());
142 // This shows that the standard window menu may be customized:
143 wxMenu
* const windowMenu
= GetWindowMenu();
146 // we can change the labels of standard items (which also means we can
147 // set up accelerators for them as they're part of the label)
148 windowMenu
->SetLabel(wxID_MDI_WINDOW_TILE_HORZ
,
149 "&Tile horizontally\tCtrl-Shift-H");
150 windowMenu
->SetLabel(wxID_MDI_WINDOW_TILE_VERT
,
151 "&Tile vertically\tCtrl-Shift-V");
153 // we can also change the help string
154 windowMenu
->SetHelpString(wxID_MDI_WINDOW_CASCADE
,
155 "Arrange windows in cascade");
157 // we can remove some items
158 windowMenu
->Delete(wxID_MDI_WINDOW_ARRANGE_ICONS
);
160 // and we can add completely custom commands -- but then we must handle
161 // them ourselves, see OnCloseAll()
162 windowMenu
->AppendSeparator();
163 windowMenu
->Append(wxID_CLOSE_ALL
, "&Close all windows\tCtrl-Shift-C",
164 "Close all open windows");
166 SetWindowMenu(windowMenu
);
168 #endif // wxUSE_MENUS
172 #endif // wxUSE_STATUSBAR
175 m_textWindow
= new wxTextCtrl(this, wxID_ANY
, "A help window",
176 wxDefaultPosition
, wxDefaultSize
,
177 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
180 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
181 InitToolBar(GetToolBar());
182 #endif // wxUSE_TOOLBAR
186 wxAcceleratorEntry entries
[3];
187 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', wxID_NEW
);
188 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_EXIT
);
189 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', wxID_ABOUT
);
190 wxAcceleratorTable
accel(3, entries
);
191 SetAcceleratorTable(accel
);
192 #endif // wxUSE_ACCEL
194 // connect it only now, after creating m_textWindow
195 Connect(wxEVT_SIZE
, wxSizeEventHandler(MyFrame::OnSize
));
200 // and disconnect it to prevent accessing already deleted m_textWindow in
201 // the size event handler if it's called during destruction
202 Disconnect(wxEVT_SIZE
, wxSizeEventHandler(MyFrame::OnSize
));
207 wxMenuBar
*MyFrame::CreateMainMenubar()
209 wxMenu
*menuFile
= new wxMenu
;
211 menuFile
->Append(wxID_NEW
, "&New window\tCtrl-N", "Create a new child window");
212 menuFile
->AppendCheckItem(MDI_FULLSCREEN
, "Show &full screen\tCtrl-F");
213 menuFile
->Append(wxID_EXIT
, "&Exit\tAlt-X", "Quit the program");
215 wxMenu
*menuHelp
= new wxMenu
;
216 menuHelp
->Append(wxID_ABOUT
, "&About\tF1");
218 wxMenuBar
*mbar
= new wxMenuBar
;
219 mbar
->Append(menuFile
, "&File");
220 mbar
->Append(menuHelp
, "&Help");
224 #endif // wxUSE_MENUS
226 void MyFrame::OnClose(wxCloseEvent
& event
)
228 unsigned numChildren
= MyChild::GetChildrenCount();
229 if ( event
.CanVeto() && (numChildren
> 0) )
232 msg
.Printf("%d windows still open, close anyhow?", numChildren
);
233 if ( wxMessageBox(msg
, "Please confirm",
234 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
245 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
250 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
252 (void)wxMessageBox("wxWidgets 2.0 MDI Demo\n"
253 "Author: Julian Smart (c) 1997\n"
254 "Usage: mdi.exe", "About MDI Demo");
257 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
259 // create and show another child frame
260 MyChild
*subframe
= new MyChild(this);
261 subframe
->Show(true);
264 void MyFrame::OnFullScreen(wxCommandEvent
& event
)
266 ShowFullScreen(event
.IsChecked());
269 void MyFrame::OnCloseAll(wxCommandEvent
& WXUNUSED(event
))
271 for ( wxWindowList::const_iterator i
= GetChildren().begin();
272 i
!= GetChildren().end();
275 if ( wxDynamicCast(*i
, wxMDIChildFrame
) )
280 void MyFrame::OnSize(wxSizeEvent
& event
)
283 GetClientSize(&w
, &h
);
285 m_textWindow
->SetSize(0, 0, 200, h
);
286 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
288 // FIXME: On wxX11, we need the MDI frame to process this
289 // event, but on other platforms this should not
291 #ifdef __WXUNIVERSAL__
299 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
303 bitmaps
[0] = wxBitmap( new_xpm
);
304 bitmaps
[1] = wxBitmap( open_xpm
);
305 bitmaps
[2] = wxBitmap( save_xpm
);
306 bitmaps
[3] = wxBitmap( copy_xpm
);
307 bitmaps
[4] = wxBitmap( cut_xpm
);
308 bitmaps
[5] = wxBitmap( paste_xpm
);
309 bitmaps
[6] = wxBitmap( print_xpm
);
310 bitmaps
[7] = wxBitmap( help_xpm
);
312 toolBar
->AddTool(wxID_NEW
, "New", bitmaps
[0], "New file");
313 toolBar
->AddTool(1, "Open", bitmaps
[1], "Open file");
314 toolBar
->AddTool(2, "Save", bitmaps
[2], "Save file");
315 toolBar
->AddSeparator();
316 toolBar
->AddTool(3, "Copy", bitmaps
[3], "Copy");
317 toolBar
->AddTool(4, "Cut", bitmaps
[4], "Cut");
318 toolBar
->AddTool(5, "Paste", bitmaps
[5], "Paste");
319 toolBar
->AddSeparator();
320 toolBar
->AddTool(6, "Print", bitmaps
[6], "Print");
321 toolBar
->AddSeparator();
322 toolBar
->AddTool(wxID_ABOUT
, "About", bitmaps
[7], "Help");
326 #endif // wxUSE_TOOLBAR
328 // ---------------------------------------------------------------------------
330 // ---------------------------------------------------------------------------
332 // Define a constructor for my canvas
333 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
334 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
336 wxNO_FULL_REPAINT_ON_RESIZE
|
337 wxVSCROLL
| wxHSCROLL
)
339 SetBackgroundColour(wxColour("WHITE"));
340 SetCursor(wxCursor(wxCURSOR_PENCIL
));
342 SetScrollbars(20, 20, 50, 50);
347 // Define the repainting behaviour
348 void MyCanvas::OnDraw(wxDC
& dc
)
350 if ( !m_text
.empty() )
351 dc
.DrawText(m_text
, 10, 10);
353 dc
.SetFont(*wxSWISS_FONT
);
354 dc
.SetPen(*wxGREEN_PEN
);
355 dc
.DrawLine(0, 0, 200, 200);
356 dc
.DrawLine(200, 0, 0, 200);
358 dc
.SetBrush(*wxCYAN_BRUSH
);
359 dc
.SetPen(*wxRED_PEN
);
360 dc
.DrawRectangle(100, 100, 100, 50);
361 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
363 dc
.DrawEllipse(250, 250, 100, 50);
365 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
366 #endif // wxUSE_SPLINES
367 dc
.DrawLine(50, 230, 200, 230);
368 dc
.DrawText("This is a test string", 50, 230);
371 points
[0].x
= 200; points
[0].y
= 300;
372 points
[1].x
= 100; points
[1].y
= 400;
373 points
[2].x
= 300; points
[2].y
= 400;
375 dc
.DrawPolygon(3, points
);
378 // This implements a tiny doodling program! Drag the mouse using the left
380 void MyCanvas::OnEvent(wxMouseEvent
& event
)
385 wxPoint
pt(event
.GetLogicalPosition(dc
));
387 static long xpos
= -1;
388 static long ypos
= -1;
390 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
392 dc
.SetPen(*wxBLACK_PEN
);
393 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
402 // ---------------------------------------------------------------------------
404 // ---------------------------------------------------------------------------
406 unsigned MyChild::ms_numChildren
= 0;
408 MyChild::MyChild(wxMDIParentFrame
*parent
)
413 wxString::Format("Child %u", ++ms_numChildren
)
416 m_canvas
= new MyCanvas(this, wxPoint(0, 0), GetClientSize());
418 SetIcon(wxICON(chart
));
420 const bool canBeResized
= !IsAlwaysMaximized();
422 // create our menu bar: it will be shown instead of the main frame one when
425 wxMenuBar
*mbar
= MyFrame::CreateMainMenubar();
426 mbar
->GetMenu(0)->Insert(1, wxID_CLOSE
, "&Close child\tCtrl-W",
427 "Close this window");
429 wxMenu
*menuChild
= new wxMenu
;
431 menuChild
->Append(MDI_REFRESH
, "&Refresh picture");
432 menuChild
->Append(MDI_CHANGE_TITLE
, "Change &title...\tCtrl-T");
435 menuChild
->AppendSeparator();
436 menuChild
->Append(MDI_CHANGE_POSITION
, "Move frame\tCtrl-M");
437 menuChild
->Append(MDI_CHANGE_SIZE
, "Resize frame\tCtrl-S");
440 menuChild
->AppendSeparator();
441 menuChild
->Append(wxID_PASTE
, "Copy text from clipboard\tCtrl-V");
442 #endif // wxUSE_CLIPBOARD
444 mbar
->Insert(1, menuChild
, "&Child");
446 // Associate the menu bar with the frame
448 #endif // wxUSE_MENUS
450 // this should work for MDI frames as well as for normal ones, provided
451 // they can be resized at all
453 SetSizeHints(100, 100);
461 void MyChild::OnClose(wxCommandEvent
& WXUNUSED(event
))
466 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
472 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
477 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
479 SetClientSize(100, 100);
482 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
485 static wxString s_title
= "Canvas Frame";
487 wxString title
= wxGetTextFromUser("Enter the new title for MDI child",
488 "MDI sample question",
490 GetParent()->GetParent());
496 #endif // wxUSE_TEXTDLG
499 void MyChild::OnActivate(wxActivateEvent
& event
)
501 if ( event
.GetActive() && m_canvas
)
502 m_canvas
->SetFocus();
505 void MyChild::OnMove(wxMoveEvent
& event
)
507 // VZ: here everything is totally wrong under MSW, the positions are
508 // different and both wrong (pos2 is off by 2 pixels for me which seems
509 // to be the width of the MDI canvas border)
510 wxPoint pos1
= event
.GetPosition(),
511 pos2
= GetPosition();
512 wxLogStatus("position from event: (%d, %d), from frame (%d, %d)",
513 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
518 void MyChild::OnSize(wxSizeEvent
& event
)
520 // VZ: under MSW the size event carries the client size (quite
521 // unexpectedly) *except* for the very first one which has the full
522 // size... what should it really be? TODO: check under wxGTK
523 wxSize size1
= event
.GetSize(),
525 size3
= GetClientSize();
526 wxLogStatus("size from event: %dx%d, from frame %dx%d, client %dx%d",
527 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
532 void MyChild::OnCloseWindow(wxCloseEvent
& event
)
534 if ( m_canvas
&& m_canvas
->IsDirty() )
536 if ( wxMessageBox("Really close?", "Please confirm",
537 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
550 #include "wx/clipbrd.h"
552 void MyChild::OnPaste(wxCommandEvent
& WXUNUSED(event
))
554 wxClipboardLocker lock
;
555 wxTextDataObject data
;
556 m_canvas
->SetText(wxTheClipboard
->GetData(data
)
558 : wxString("No text on clipboard"));
561 void MyChild::OnUpdatePaste(wxUpdateUIEvent
& event
)
563 wxClipboardLocker lock
;
564 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
567 #endif // wxUSE_CLIPBOARD