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
)
75 EVT_SIZE(MyFrame::OnSize
)
78 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
79 // to the parent window for processing, so no need to
80 // duplicate event handlers here.
81 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
82 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
83 EVT_MENU(MDI_REFRESH
, MyChild::OnRefresh
)
84 EVT_MENU(MDI_CHANGE_TITLE
, MyChild::OnChangeTitle
)
85 EVT_MENU(MDI_CHANGE_POSITION
, MyChild::OnChangePosition
)
86 EVT_MENU(MDI_CHANGE_SIZE
, MyChild::OnChangeSize
)
88 EVT_SIZE(MyChild::OnSize
)
89 EVT_MOVE(MyChild::OnMove
)
91 EVT_CLOSE(MyChild::OnClose
)
94 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
95 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
98 // ===========================================================================
100 // ===========================================================================
102 // ---------------------------------------------------------------------------
104 // ---------------------------------------------------------------------------
106 // Initialise this in OnInit, not statically
109 // Create the main frame window
111 frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("MDI Demo"),
112 wxDefaultPosition
, wxSize(500, 400),
113 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
116 // Experimental: change the window menu
117 wxMenu
* windowMenu
= new wxMenu
;
118 windowMenu
->Append(5000, _T("My menu item!"));
119 frame
->SetWindowMenu(windowMenu
);
125 frame
->SetIcon(wxIcon(_T("mdi_icn")));
127 frame
->SetIcon(wxIcon( mondrian_xpm
));
131 wxMenu
*file_menu
= new wxMenu
;
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"));
136 wxMenu
*help_menu
= new wxMenu
;
137 help_menu
->Append(MDI_ABOUT
, _T("&About\tF1"));
139 wxMenuBar
*menu_bar
= new wxMenuBar
;
141 menu_bar
->Append(file_menu
, _T("&File"));
142 menu_bar
->Append(help_menu
, _T("&Help"));
144 // Associate the menu bar with the frame
145 frame
->SetMenuBar(menu_bar
);
148 frame
->CreateStatusBar();
149 #endif // wxUSE_STATUSBAR
158 // ---------------------------------------------------------------------------
160 // ---------------------------------------------------------------------------
162 // Define my frame constructor
163 MyFrame::MyFrame(wxWindow
*parent
,
165 const wxString
& title
,
169 : wxMDIParentFrame(parent
, id
, title
, pos
, size
,
170 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
172 textWindow
= new wxTextCtrl(this, wxID_ANY
, _T("A help window"),
173 wxDefaultPosition
, wxDefaultSize
,
174 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
177 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
178 InitToolBar(GetToolBar());
179 #endif // wxUSE_TOOLBAR
183 wxAcceleratorEntry entries
[3];
184 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
185 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
186 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
187 wxAcceleratorTable
accel(3, entries
);
188 SetAcceleratorTable(accel
);
189 #endif // wxUSE_ACCEL
192 void MyFrame::OnClose(wxCloseEvent
& event
)
194 if ( event
.CanVeto() && (gs_nFrames
> 0) )
197 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
198 if ( wxMessageBox(msg
, _T("Please confirm"),
199 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
210 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
215 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
217 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
218 _T("Author: Julian Smart (c) 1997\n")
219 _T("Usage: mdi.exe"), _T("About MDI Demo"));
222 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
224 // Make another frame, containing a canvas
225 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"));
228 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
230 subframe
->SetTitle(title
);
234 subframe
->SetIcon(wxIcon(_T("chrt_icn")));
236 subframe
->SetIcon(wxIcon( mondrian_xpm
));
241 wxMenu
*file_menu
= new wxMenu
;
243 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window"));
244 file_menu
->Append(MDI_CHILD_QUIT
, _T("&Close child"), _T("Close this window"));
245 file_menu
->Append(MDI_QUIT
, _T("&Exit"));
247 wxMenu
*option_menu
= new wxMenu
;
249 option_menu
->Append(MDI_REFRESH
, _T("&Refresh picture"));
250 option_menu
->Append(MDI_CHANGE_TITLE
, _T("Change &title...\tCtrl-T"));
251 option_menu
->AppendSeparator();
252 option_menu
->Append(MDI_CHANGE_POSITION
, _T("Move frame\tCtrl-M"));
253 option_menu
->Append(MDI_CHANGE_SIZE
, _T("Resize frame\tCtrl-S"));
255 wxMenu
*help_menu
= new wxMenu
;
256 help_menu
->Append(MDI_ABOUT
, _T("&About"));
258 wxMenuBar
*menu_bar
= new wxMenuBar
;
260 menu_bar
->Append(file_menu
, _T("&File"));
261 menu_bar
->Append(option_menu
, _T("&Child"));
262 menu_bar
->Append(help_menu
, _T("&Help"));
264 // Associate the menu bar with the frame
265 subframe
->SetMenuBar(menu_bar
);
266 #endif // wxUSE_MENUS
269 subframe
->CreateStatusBar();
270 subframe
->SetStatusText(title
);
271 #endif // wxUSE_STATUSBAR
274 subframe
->GetClientSize(&width
, &height
);
275 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
276 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
277 subframe
->canvas
= canvas
;
279 // Give it scrollbars
280 canvas
->SetScrollbars(20, 20, 50, 50);
282 subframe
->Show(true);
285 void MyFrame::OnSize(wxSizeEvent
&
286 #ifdef __WXUNIVERSAL__
294 GetClientSize(&w
, &h
);
296 textWindow
->SetSize(0, 0, 200, h
);
297 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
299 // FIXME: On wxX11, we need the MDI frame to process this
300 // event, but on other platforms this should not
302 #ifdef __WXUNIVERSAL__
308 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
310 wxBitmap
* bitmaps
[8];
312 bitmaps
[0] = new wxBitmap( new_xpm
);
313 bitmaps
[1] = new wxBitmap( open_xpm
);
314 bitmaps
[2] = new wxBitmap( save_xpm
);
315 bitmaps
[3] = new wxBitmap( copy_xpm
);
316 bitmaps
[4] = new wxBitmap( cut_xpm
);
317 bitmaps
[5] = new wxBitmap( paste_xpm
);
318 bitmaps
[6] = new wxBitmap( print_xpm
);
319 bitmaps
[7] = new wxBitmap( help_xpm
);
324 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("New file"));
325 currentX
+= width
+ 5;
326 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Open file"));
327 currentX
+= width
+ 5;
328 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Save file"));
329 currentX
+= width
+ 5;
330 toolBar
->AddSeparator();
331 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Copy"));
332 currentX
+= width
+ 5;
333 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Cut"));
334 currentX
+= width
+ 5;
335 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Paste"));
336 currentX
+= width
+ 5;
337 toolBar
->AddSeparator();
338 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Print"));
339 currentX
+= width
+ 5;
340 toolBar
->AddSeparator();
341 toolBar
->AddTool( MDI_ABOUT
, *bitmaps
[7], wxNullBitmap
, true, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Help"));
346 for (i
= 0; i
< 8; i
++)
349 #endif // wxUSE_TOOLBAR
351 // ---------------------------------------------------------------------------
353 // ---------------------------------------------------------------------------
355 // Define a constructor for my canvas
356 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
357 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
359 wxNO_FULL_REPAINT_ON_RESIZE
|
360 wxVSCROLL
| wxHSCROLL
)
362 SetBackgroundColour(wxColour(_T("WHITE")));
367 // Define the repainting behaviour
368 void MyCanvas::OnDraw(wxDC
& dc
)
370 dc
.SetFont(*wxSWISS_FONT
);
371 dc
.SetPen(*wxGREEN_PEN
);
372 dc
.DrawLine(0, 0, 200, 200);
373 dc
.DrawLine(200, 0, 0, 200);
375 dc
.SetBrush(*wxCYAN_BRUSH
);
376 dc
.SetPen(*wxRED_PEN
);
377 dc
.DrawRectangle(100, 100, 100, 50);
378 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
380 dc
.DrawEllipse(250, 250, 100, 50);
382 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
383 #endif // wxUSE_SPLINES
384 dc
.DrawLine(50, 230, 200, 230);
385 dc
.DrawText(_T("This is a test string"), 50, 230);
388 points
[0].x
= 200; points
[0].y
= 300;
389 points
[1].x
= 100; points
[1].y
= 400;
390 points
[2].x
= 300; points
[2].y
= 400;
392 dc
.DrawPolygon(3, points
);
395 // This implements a tiny doodling program! Drag the mouse using the left
397 void MyCanvas::OnEvent(wxMouseEvent
& event
)
402 wxPoint
pt(event
.GetLogicalPosition(dc
));
404 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
406 dc
.SetPen(*wxBLACK_PEN
);
407 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
416 // ---------------------------------------------------------------------------
418 // ---------------------------------------------------------------------------
420 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
)
421 : wxMDIChildFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxDefaultSize
,
422 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
424 canvas
= (MyCanvas
*) NULL
;
425 my_children
.Append(this);
427 // this should work for MDI frames as well as for normal ones
428 SetSizeHints(100, 100);
433 my_children
.DeleteObject(this);
436 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
441 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
447 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
452 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
454 SetClientSize(100, 100);
457 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
460 static wxString s_title
= _T("Canvas Frame");
462 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
463 _T("MDI sample question"),
465 GetParent()->GetParent());
474 void MyChild::OnActivate(wxActivateEvent
& event
)
476 if ( event
.GetActive() && canvas
)
480 void MyChild::OnMove(wxMoveEvent
& event
)
482 // VZ: here everything is totally wrong under MSW, the positions are
483 // different and both wrong (pos2 is off by 2 pixels for me which seems
484 // to be the width of the MDI canvas border)
485 wxPoint pos1
= event
.GetPosition(),
486 pos2
= GetPosition();
487 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
488 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
493 void MyChild::OnSize(wxSizeEvent
& event
)
495 // VZ: under MSW the size event carries the client size (quite
496 // unexpectedly) *except* for the very first one which has the full
497 // size... what should it really be? TODO: check under wxGTK
498 wxSize size1
= event
.GetSize(),
500 size3
= GetClientSize();
501 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
502 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
507 void MyChild::OnClose(wxCloseEvent
& event
)
509 if ( canvas
&& canvas
->IsDirty() )
511 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
512 wxICON_QUESTION
| wxYES_NO
) != wxYES
)