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(wxID_EXIT
, MyFrame::OnQuit
)
71 EVT_CLOSE(MyFrame::OnClose
)
74 // Note that wxID_NEW and wxID_ABOUT commands get passed
75 // to the parent window for processing, so no need to
76 // duplicate event handlers here.
77 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
78 EVT_MENU(wxID_CLOSE
, MyChild::OnClose
)
79 EVT_MENU(MDI_REFRESH
, MyChild::OnRefresh
)
80 EVT_MENU(MDI_CHANGE_TITLE
, MyChild::OnChangeTitle
)
81 EVT_MENU(MDI_CHANGE_POSITION
, MyChild::OnChangePosition
)
82 EVT_MENU(MDI_CHANGE_SIZE
, MyChild::OnChangeSize
)
85 EVT_MENU(wxID_PASTE
, MyChild::OnPaste
)
86 EVT_UPDATE_UI(wxID_PASTE
, MyChild::OnUpdatePaste
)
87 #endif // wxUSE_CLIPBOARD
89 EVT_SIZE(MyChild::OnSize
)
90 EVT_MOVE(MyChild::OnMove
)
92 EVT_CLOSE(MyChild::OnCloseWindow
)
95 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
96 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
99 // ===========================================================================
101 // ===========================================================================
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 // Initialise this in OnInit, not statically
110 if ( !wxApp::OnInit() )
113 // Create the main frame window
115 MyFrame
*frame
= new MyFrame
;
122 // ---------------------------------------------------------------------------
124 // ---------------------------------------------------------------------------
126 // Define my frame constructor
128 : wxMDIParentFrame(NULL
, wxID_ANY
, "wxWidgets MDI Sample",
129 wxDefaultPosition
, wxSize(500, 400))
131 SetIcon(wxICON(sample
));
135 wxMenu
*file_menu
= new wxMenu
;
137 file_menu
->Append(wxID_NEW
, "&New window\tCtrl-N", "Create a new child window");
138 file_menu
->Append(wxID_EXIT
, "&Exit\tAlt-X", "Quit the program");
140 wxMenu
*help_menu
= new wxMenu
;
141 help_menu
->Append(wxID_ABOUT
, "&About\tF1");
143 wxMenuBar
*menu_bar
= new wxMenuBar
;
145 menu_bar
->Append(file_menu
, "&File");
146 menu_bar
->Append(help_menu
, "&Help");
148 // Associate the menu bar with the frame
149 SetMenuBar(menu_bar
);
152 // Experimental: change the window menu
153 wxMenu
* windowMenu
= new wxMenu
;
154 windowMenu
->Append(5000, "My menu item!");
155 frame
->SetWindowMenu(windowMenu
);
157 #endif // wxUSE_MENUS
161 #endif // wxUSE_STATUSBAR
164 m_textWindow
= new wxTextCtrl(this, wxID_ANY
, "A help window",
165 wxDefaultPosition
, wxDefaultSize
,
166 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
169 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
170 InitToolBar(GetToolBar());
171 #endif // wxUSE_TOOLBAR
175 wxAcceleratorEntry entries
[3];
176 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', wxID_NEW
);
177 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', wxID_EXIT
);
178 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', wxID_ABOUT
);
179 wxAcceleratorTable
accel(3, entries
);
180 SetAcceleratorTable(accel
);
181 #endif // wxUSE_ACCEL
183 // connect it only now, after creating m_textWindow
184 Connect(wxEVT_SIZE
, wxSizeEventHandler(MyFrame::OnSize
));
189 // and disconnect it to prevent accessing already deleted m_textWindow in
190 // the size event handler if it's called during destruction
191 Disconnect(wxEVT_SIZE
, wxSizeEventHandler(MyFrame::OnSize
));
194 void MyFrame::OnClose(wxCloseEvent
& event
)
196 unsigned numChildren
= MyChild::GetChildrenCount();
197 if ( event
.CanVeto() && (numChildren
> 0) )
200 msg
.Printf("%d windows still open, close anyhow?", numChildren
);
201 if ( wxMessageBox(msg
, "Please confirm",
202 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
213 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
218 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
220 (void)wxMessageBox("wxWidgets 2.0 MDI Demo\n"
221 "Author: Julian Smart (c) 1997\n"
222 "Usage: mdi.exe", "About MDI Demo");
225 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
227 // create and show another child frame
228 MyChild
*subframe
= new MyChild(this);
229 subframe
->Show(true);
232 void MyFrame::OnSize(wxSizeEvent
& event
)
235 GetClientSize(&w
, &h
);
237 m_textWindow
->SetSize(0, 0, 200, h
);
238 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
240 // FIXME: On wxX11, we need the MDI frame to process this
241 // event, but on other platforms this should not
243 #ifdef __WXUNIVERSAL__
251 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
255 bitmaps
[0] = wxBitmap( new_xpm
);
256 bitmaps
[1] = wxBitmap( open_xpm
);
257 bitmaps
[2] = wxBitmap( save_xpm
);
258 bitmaps
[3] = wxBitmap( copy_xpm
);
259 bitmaps
[4] = wxBitmap( cut_xpm
);
260 bitmaps
[5] = wxBitmap( paste_xpm
);
261 bitmaps
[6] = wxBitmap( print_xpm
);
262 bitmaps
[7] = wxBitmap( help_xpm
);
264 toolBar
->AddTool(wxID_NEW
, "New", bitmaps
[0], "New file");
265 toolBar
->AddTool(1, "Open", bitmaps
[1], "Open file");
266 toolBar
->AddTool(2, "Save", bitmaps
[2], "Save file");
267 toolBar
->AddSeparator();
268 toolBar
->AddTool(3, "Copy", bitmaps
[3], "Copy");
269 toolBar
->AddTool(4, "Cut", bitmaps
[4], "Cut");
270 toolBar
->AddTool(5, "Paste", bitmaps
[5], "Paste");
271 toolBar
->AddSeparator();
272 toolBar
->AddTool(6, "Print", bitmaps
[6], "Print");
273 toolBar
->AddSeparator();
274 toolBar
->AddTool(wxID_ABOUT
, "About", bitmaps
[7], "Help");
278 #endif // wxUSE_TOOLBAR
280 // ---------------------------------------------------------------------------
282 // ---------------------------------------------------------------------------
284 // Define a constructor for my canvas
285 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
286 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
288 wxNO_FULL_REPAINT_ON_RESIZE
|
289 wxVSCROLL
| wxHSCROLL
)
291 SetBackgroundColour(wxColour("WHITE"));
292 SetCursor(wxCursor(wxCURSOR_PENCIL
));
294 SetScrollbars(20, 20, 50, 50);
299 // Define the repainting behaviour
300 void MyCanvas::OnDraw(wxDC
& dc
)
302 if ( !m_text
.empty() )
303 dc
.DrawText(m_text
, 10, 10);
305 dc
.SetFont(*wxSWISS_FONT
);
306 dc
.SetPen(*wxGREEN_PEN
);
307 dc
.DrawLine(0, 0, 200, 200);
308 dc
.DrawLine(200, 0, 0, 200);
310 dc
.SetBrush(*wxCYAN_BRUSH
);
311 dc
.SetPen(*wxRED_PEN
);
312 dc
.DrawRectangle(100, 100, 100, 50);
313 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
315 dc
.DrawEllipse(250, 250, 100, 50);
317 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
318 #endif // wxUSE_SPLINES
319 dc
.DrawLine(50, 230, 200, 230);
320 dc
.DrawText("This is a test string", 50, 230);
323 points
[0].x
= 200; points
[0].y
= 300;
324 points
[1].x
= 100; points
[1].y
= 400;
325 points
[2].x
= 300; points
[2].y
= 400;
327 dc
.DrawPolygon(3, points
);
330 // This implements a tiny doodling program! Drag the mouse using the left
332 void MyCanvas::OnEvent(wxMouseEvent
& event
)
337 wxPoint
pt(event
.GetLogicalPosition(dc
));
339 static long xpos
= -1;
340 static long ypos
= -1;
342 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
344 dc
.SetPen(*wxBLACK_PEN
);
345 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
354 // ---------------------------------------------------------------------------
356 // ---------------------------------------------------------------------------
358 unsigned MyChild::ms_numChildren
= 0;
360 MyChild::MyChild(wxMDIParentFrame
*parent
)
365 wxString::Format("Child %u", ++ms_numChildren
)
368 m_canvas
= new MyCanvas(this, wxPoint(0, 0), GetClientSize());
370 SetIcon(wxICON(chart
));
372 const bool canBeResized
= !IsAlwaysMaximized();
374 // create our menubar: it will be shown instead of the main frame one when
378 wxMenu
*file_menu
= new wxMenu
;
380 file_menu
->Append(wxID_NEW
, "&New window\tCtrl-N");
381 file_menu
->Append(wxID_CLOSE
, "&Close child\tCtrl-W", "Close this window");
382 file_menu
->Append(wxID_EXIT
, "&Exit\tAlt-X", "Quit the program");
384 wxMenu
*option_menu
= new wxMenu
;
386 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
387 option_menu
->Append(MDI_CHANGE_TITLE
, "Change &title...\tCtrl-T");
390 option_menu
->AppendSeparator();
391 option_menu
->Append(MDI_CHANGE_POSITION
, "Move frame\tCtrl-M");
392 option_menu
->Append(MDI_CHANGE_SIZE
, "Resize frame\tCtrl-S");
395 option_menu
->AppendSeparator();
396 option_menu
->Append(wxID_PASTE
, "Copy text from clipboard\tCtrl-V");
397 #endif // wxUSE_CLIPBOARD
399 wxMenu
*help_menu
= new wxMenu
;
400 help_menu
->Append(wxID_ABOUT
, "&About");
402 wxMenuBar
*menu_bar
= new wxMenuBar
;
404 menu_bar
->Append(file_menu
, "&File");
405 menu_bar
->Append(option_menu
, "&Child");
406 menu_bar
->Append(help_menu
, "&Help");
408 // Associate the menu bar with the frame
409 SetMenuBar(menu_bar
);
410 #endif // wxUSE_MENUS
412 // this should work for MDI frames as well as for normal ones, provided
413 // they can be resized at all
415 SetSizeHints(100, 100);
423 void MyChild::OnClose(wxCommandEvent
& WXUNUSED(event
))
428 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
434 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
439 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
441 SetClientSize(100, 100);
444 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
447 static wxString s_title
= "Canvas Frame";
449 wxString title
= wxGetTextFromUser("Enter the new title for MDI child",
450 "MDI sample question",
452 GetParent()->GetParent());
458 #endif // wxUSE_TEXTDLG
461 void MyChild::OnActivate(wxActivateEvent
& event
)
463 if ( event
.GetActive() && m_canvas
)
464 m_canvas
->SetFocus();
467 void MyChild::OnMove(wxMoveEvent
& event
)
469 // VZ: here everything is totally wrong under MSW, the positions are
470 // different and both wrong (pos2 is off by 2 pixels for me which seems
471 // to be the width of the MDI canvas border)
472 wxPoint pos1
= event
.GetPosition(),
473 pos2
= GetPosition();
474 wxLogStatus("position from event: (%d, %d), from frame (%d, %d)",
475 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
480 void MyChild::OnSize(wxSizeEvent
& event
)
482 // VZ: under MSW the size event carries the client size (quite
483 // unexpectedly) *except* for the very first one which has the full
484 // size... what should it really be? TODO: check under wxGTK
485 wxSize size1
= event
.GetSize(),
487 size3
= GetClientSize();
488 wxLogStatus("size from event: %dx%d, from frame %dx%d, client %dx%d",
489 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
494 void MyChild::OnCloseWindow(wxCloseEvent
& event
)
496 if ( m_canvas
&& m_canvas
->IsDirty() )
498 if ( wxMessageBox("Really close?", "Please confirm",
499 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
512 #include "wx/clipbrd.h"
514 void MyChild::OnPaste(wxCommandEvent
& WXUNUSED(event
))
516 wxClipboardLocker lock
;
517 wxTextDataObject data
;
518 m_canvas
->SetText(wxTheClipboard
->GetData(data
)
520 : wxString("No text on clipboard"));
523 void MyChild::OnUpdatePaste(wxUpdateUIEvent
& event
)
525 wxClipboardLocker lock
;
526 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
529 #endif // wxUSE_CLIPBOARD