1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/toolbar.h"
34 #if !defined(__WXMSW__)
35 #include "mondrian.xpm"
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"
52 // ---------------------------------------------------------------------------
54 // ---------------------------------------------------------------------------
56 MyFrame
*frame
= (MyFrame
*) NULL
;
59 // For drawing lines in a canvas
60 static long xpos
= -1;
61 static long ypos
= -1;
63 static int gs_nFrames
= 0;
65 // ---------------------------------------------------------------------------
67 // ---------------------------------------------------------------------------
69 BEGIN_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
)
74 EVT_CLOSE(MyFrame::OnClose
)
76 EVT_SIZE(MyFrame::OnSize
)
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
)
89 EVT_SIZE(MyChild::OnSize
)
90 EVT_MOVE(MyChild::OnMove
)
92 EVT_CLOSE(MyChild::OnClose
)
95 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
96 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
99 // ===========================================================================
101 // ===========================================================================
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 // Initialise this in OnInit, not statically
110 // Create the main frame window
112 frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("MDI Demo"),
113 wxDefaultPosition
, wxSize(500, 400),
114 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
117 // Experimental: change the window menu
118 wxMenu
* windowMenu
= new wxMenu
;
119 windowMenu
->Append(5000, _T("My menu item!"));
120 frame
->SetWindowMenu(windowMenu
);
126 frame
->SetIcon(wxIcon(_T("mdi_icn")));
128 frame
->SetIcon(wxIcon( mondrian_xpm
));
132 wxMenu
*file_menu
= new wxMenu
;
134 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window\tCtrl-N"), _T("Create a new child window"));
135 file_menu
->Append(MDI_QUIT
, _T("&Exit\tAlt-X"), _T("Quit the program"));
137 wxMenu
*help_menu
= new wxMenu
;
138 help_menu
->Append(MDI_ABOUT
, _T("&About\tF1"));
140 wxMenuBar
*menu_bar
= new wxMenuBar
;
142 menu_bar
->Append(file_menu
, _T("&File"));
143 menu_bar
->Append(help_menu
, _T("&Help"));
145 // Associate the menu bar with the frame
146 frame
->SetMenuBar(menu_bar
);
148 frame
->CreateStatusBar();
157 // ---------------------------------------------------------------------------
159 // ---------------------------------------------------------------------------
161 // Define my frame constructor
162 MyFrame::MyFrame(wxWindow
*parent
,
164 const wxString
& title
,
168 : wxMDIParentFrame(parent
, id
, title
, pos
, size
,
169 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
171 textWindow
= new wxTextCtrl(this, wxID_ANY
, _T("A help window"),
172 wxDefaultPosition
, wxDefaultSize
,
173 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
176 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
177 InitToolBar(GetToolBar());
178 #endif // wxUSE_TOOLBAR
181 wxAcceleratorEntry entries
[3];
182 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
183 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
184 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
185 wxAcceleratorTable
accel(3, entries
);
186 SetAcceleratorTable(accel
);
189 void MyFrame::OnClose(wxCloseEvent
& event
)
191 if ( event
.CanVeto() && (gs_nFrames
> 0) )
194 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
195 if ( wxMessageBox(msg
, _T("Please confirm"),
196 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
207 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
212 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
214 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
215 _T("Author: Julian Smart (c) 1997\n")
216 _T("Usage: mdi.exe"), _T("About MDI Demo"));
219 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
221 // Make another frame, containing a canvas
222 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"));
225 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
227 subframe
->SetTitle(title
);
231 subframe
->SetIcon(wxIcon(_T("chrt_icn")));
233 subframe
->SetIcon(wxIcon( mondrian_xpm
));
237 wxMenu
*file_menu
= new wxMenu
;
239 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window"));
240 file_menu
->Append(MDI_CHILD_QUIT
, _T("&Close child"), _T("Close this window"));
241 file_menu
->Append(MDI_QUIT
, _T("&Exit"));
243 wxMenu
*option_menu
= new wxMenu
;
245 option_menu
->Append(MDI_REFRESH
, _T("&Refresh picture"));
246 option_menu
->Append(MDI_CHANGE_TITLE
, _T("Change &title...\tCtrl-T"));
247 option_menu
->AppendSeparator();
248 option_menu
->Append(MDI_CHANGE_POSITION
, _T("Move frame\tCtrl-M"));
249 option_menu
->Append(MDI_CHANGE_SIZE
, _T("Resize frame\tCtrl-S"));
251 wxMenu
*help_menu
= new wxMenu
;
252 help_menu
->Append(MDI_ABOUT
, _T("&About"));
254 wxMenuBar
*menu_bar
= new wxMenuBar
;
256 menu_bar
->Append(file_menu
, _T("&File"));
257 menu_bar
->Append(option_menu
, _T("&Child"));
258 menu_bar
->Append(help_menu
, _T("&Help"));
260 // Associate the menu bar with the frame
261 subframe
->SetMenuBar(menu_bar
);
263 subframe
->CreateStatusBar();
264 subframe
->SetStatusText(title
);
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
;
272 // Give it scrollbars
273 canvas
->SetScrollbars(20, 20, 50, 50);
275 subframe
->Show(true);
278 void MyFrame::OnSize(wxSizeEvent
&
279 #ifdef __WXUNIVERSAL__
287 GetClientSize(&w
, &h
);
289 textWindow
->SetSize(0, 0, 200, h
);
290 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
292 // FIXME: On wxX11, we need the MDI frame to process this
293 // event, but on other platforms this should not
295 #ifdef __WXUNIVERSAL__
301 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
303 wxBitmap
* bitmaps
[8];
305 bitmaps
[0] = new wxBitmap( new_xpm
);
306 bitmaps
[1] = new wxBitmap( open_xpm
);
307 bitmaps
[2] = new wxBitmap( save_xpm
);
308 bitmaps
[3] = new wxBitmap( copy_xpm
);
309 bitmaps
[4] = new wxBitmap( cut_xpm
);
310 bitmaps
[5] = new wxBitmap( paste_xpm
);
311 bitmaps
[6] = new wxBitmap( print_xpm
);
312 bitmaps
[7] = new wxBitmap( help_xpm
);
317 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("New file"));
318 currentX
+= width
+ 5;
319 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Open file"));
320 currentX
+= width
+ 5;
321 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Save file"));
322 currentX
+= width
+ 5;
323 toolBar
->AddSeparator();
324 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Copy"));
325 currentX
+= width
+ 5;
326 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Cut"));
327 currentX
+= width
+ 5;
328 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Paste"));
329 currentX
+= width
+ 5;
330 toolBar
->AddSeparator();
331 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, false, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Print"));
332 currentX
+= width
+ 5;
333 toolBar
->AddSeparator();
334 toolBar
->AddTool( MDI_ABOUT
, *bitmaps
[7], wxNullBitmap
, true, currentX
, wxDefaultPosition
.y
, (wxObject
*) NULL
, _T("Help"));
339 for (i
= 0; i
< 8; i
++)
342 #endif // wxUSE_TOOLBAR
344 // ---------------------------------------------------------------------------
346 // ---------------------------------------------------------------------------
348 // Define a constructor for my canvas
349 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
350 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
352 wxNO_FULL_REPAINT_ON_RESIZE
|
353 wxVSCROLL
| wxHSCROLL
)
355 SetBackgroundColour(wxColour(_T("WHITE")));
360 // Define the repainting behaviour
361 void MyCanvas::OnDraw(wxDC
& dc
)
363 dc
.SetFont(*wxSWISS_FONT
);
364 dc
.SetPen(*wxGREEN_PEN
);
365 dc
.DrawLine(0, 0, 200, 200);
366 dc
.DrawLine(200, 0, 0, 200);
368 dc
.SetBrush(*wxCYAN_BRUSH
);
369 dc
.SetPen(*wxRED_PEN
);
370 dc
.DrawRectangle(100, 100, 100, 50);
371 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
373 dc
.DrawEllipse(250, 250, 100, 50);
375 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
376 #endif // wxUSE_SPLINES
377 dc
.DrawLine(50, 230, 200, 230);
378 dc
.DrawText(_T("This is a test string"), 50, 230);
381 points
[0].x
= 200; points
[0].y
= 300;
382 points
[1].x
= 100; points
[1].y
= 400;
383 points
[2].x
= 300; points
[2].y
= 400;
385 dc
.DrawPolygon(3, points
);
388 // This implements a tiny doodling program! Drag the mouse using the left
390 void MyCanvas::OnEvent(wxMouseEvent
& event
)
395 wxPoint
pt(event
.GetLogicalPosition(dc
));
397 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
399 dc
.SetPen(*wxBLACK_PEN
);
400 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
409 // ---------------------------------------------------------------------------
411 // ---------------------------------------------------------------------------
413 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
)
414 : wxMDIChildFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxDefaultSize
,
415 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
417 canvas
= (MyCanvas
*) NULL
;
418 my_children
.Append(this);
420 // this should work for MDI frames as well as for normal ones
421 SetSizeHints(100, 100);
426 my_children
.DeleteObject(this);
429 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
434 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
440 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
445 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
447 SetClientSize(100, 100);
450 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
453 static wxString s_title
= _T("Canvas Frame");
455 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
456 _T("MDI sample question"),
458 GetParent()->GetParent());
467 void MyChild::OnActivate(wxActivateEvent
& event
)
469 if ( event
.GetActive() && canvas
)
473 void MyChild::OnMove(wxMoveEvent
& event
)
475 // VZ: here everything is totally wrong under MSW, the positions are
476 // different and both wrong (pos2 is off by 2 pixels for me which seems
477 // to be the width of the MDI canvas border)
478 wxPoint pos1
= event
.GetPosition(),
479 pos2
= GetPosition();
480 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
481 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
486 void MyChild::OnSize(wxSizeEvent
& event
)
488 // VZ: under MSW the size event carries the client size (quite
489 // unexpectedly) *except* for the very first one which has the full
490 // size... what should it really be? TODO: check under wxGTK
491 wxSize size1
= event
.GetSize(),
493 size3
= GetClientSize();
494 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
495 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
500 void MyChild::OnClose(wxCloseEvent
& event
)
502 if ( canvas
&& canvas
->IsDirty() )
504 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
505 wxICON_QUESTION
| wxYES_NO
) != wxYES
)