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
)
89 EVT_MENU(wxID_PASTE
, MyChild::OnPaste
)
90 EVT_UPDATE_UI(wxID_PASTE
, MyChild::OnUpdatePaste
)
91 #endif // wxUSE_CLIPBOARD
93 EVT_SIZE(MyChild::OnSize
)
94 EVT_MOVE(MyChild::OnMove
)
96 EVT_CLOSE(MyChild::OnClose
)
99 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
100 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
103 // ===========================================================================
105 // ===========================================================================
107 // ---------------------------------------------------------------------------
109 // ---------------------------------------------------------------------------
111 // Initialise this in OnInit, not statically
114 // Create the main frame window
116 frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("MDI Demo"),
117 wxDefaultPosition
, wxSize(500, 400),
118 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
121 // Experimental: change the window menu
122 wxMenu
* windowMenu
= new wxMenu
;
123 windowMenu
->Append(5000, _T("My menu item!"));
124 frame
->SetWindowMenu(windowMenu
);
130 frame
->SetIcon(wxIcon(_T("mdi_icn")));
132 frame
->SetIcon(wxIcon( mondrian_xpm
));
136 wxMenu
*file_menu
= new wxMenu
;
138 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window\tCtrl-N"), _T("Create a new child window"));
139 file_menu
->Append(MDI_QUIT
, _T("&Exit\tAlt-X"), _T("Quit the program"));
141 wxMenu
*help_menu
= new wxMenu
;
142 help_menu
->Append(MDI_ABOUT
, _T("&About\tF1"));
144 wxMenuBar
*menu_bar
= new wxMenuBar
;
146 menu_bar
->Append(file_menu
, _T("&File"));
147 menu_bar
->Append(help_menu
, _T("&Help"));
149 // Associate the menu bar with the frame
150 frame
->SetMenuBar(menu_bar
);
153 frame
->CreateStatusBar();
154 #endif // wxUSE_STATUSBAR
163 // ---------------------------------------------------------------------------
165 // ---------------------------------------------------------------------------
167 // Define my frame constructor
168 MyFrame::MyFrame(wxWindow
*parent
,
170 const wxString
& title
,
174 : wxMDIParentFrame(parent
, id
, title
, pos
, size
,
175 style
| wxNO_FULL_REPAINT_ON_RESIZE
)
177 textWindow
= new wxTextCtrl(this, wxID_ANY
, _T("A help window"),
178 wxDefaultPosition
, wxDefaultSize
,
179 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
182 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
183 InitToolBar(GetToolBar());
184 #endif // wxUSE_TOOLBAR
188 wxAcceleratorEntry entries
[3];
189 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
190 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
191 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
192 wxAcceleratorTable
accel(3, entries
);
193 SetAcceleratorTable(accel
);
194 #endif // wxUSE_ACCEL
197 void MyFrame::OnClose(wxCloseEvent
& event
)
199 if ( event
.CanVeto() && (gs_nFrames
> 0) )
202 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
203 if ( wxMessageBox(msg
, _T("Please confirm"),
204 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
215 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
220 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
222 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
223 _T("Author: Julian Smart (c) 1997\n")
224 _T("Usage: mdi.exe"), _T("About MDI Demo"));
227 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
229 // Make another frame, containing a canvas
230 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"));
233 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
235 subframe
->SetTitle(title
);
239 subframe
->SetIcon(wxIcon(_T("chrt_icn")));
241 subframe
->SetIcon(wxIcon( mondrian_xpm
));
246 wxMenu
*file_menu
= new wxMenu
;
248 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window"));
249 file_menu
->Append(MDI_CHILD_QUIT
, _T("&Close child"), _T("Close this window"));
250 file_menu
->Append(MDI_QUIT
, _T("&Exit"));
252 wxMenu
*option_menu
= new wxMenu
;
254 option_menu
->Append(MDI_REFRESH
, _T("&Refresh picture"));
255 option_menu
->Append(MDI_CHANGE_TITLE
, _T("Change &title...\tCtrl-T"));
256 option_menu
->AppendSeparator();
257 option_menu
->Append(MDI_CHANGE_POSITION
, _T("Move frame\tCtrl-M"));
258 option_menu
->Append(MDI_CHANGE_SIZE
, _T("Resize frame\tCtrl-S"));
260 option_menu
->AppendSeparator();
261 option_menu
->Append(wxID_PASTE
, _T("Copy text from clipboard\tCtrl-V"));
262 #endif // wxUSE_CLIPBOARD
264 wxMenu
*help_menu
= new wxMenu
;
265 help_menu
->Append(MDI_ABOUT
, _T("&About"));
267 wxMenuBar
*menu_bar
= new wxMenuBar
;
269 menu_bar
->Append(file_menu
, _T("&File"));
270 menu_bar
->Append(option_menu
, _T("&Child"));
271 menu_bar
->Append(help_menu
, _T("&Help"));
273 // Associate the menu bar with the frame
274 subframe
->SetMenuBar(menu_bar
);
275 #endif // wxUSE_MENUS
278 subframe
->CreateStatusBar();
279 subframe
->SetStatusText(title
);
280 #endif // wxUSE_STATUSBAR
283 subframe
->GetClientSize(&width
, &height
);
284 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
285 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
286 subframe
->canvas
= canvas
;
288 // Give it scrollbars
289 canvas
->SetScrollbars(20, 20, 50, 50);
291 subframe
->Show(true);
294 void MyFrame::OnSize(wxSizeEvent
&
295 #ifdef __WXUNIVERSAL__
303 GetClientSize(&w
, &h
);
305 textWindow
->SetSize(0, 0, 200, h
);
306 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
308 // FIXME: On wxX11, we need the MDI frame to process this
309 // event, but on other platforms this should not
311 #ifdef __WXUNIVERSAL__
317 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
319 wxBitmap
* bitmaps
[8];
321 bitmaps
[0] = new wxBitmap( new_xpm
);
322 bitmaps
[1] = new wxBitmap( open_xpm
);
323 bitmaps
[2] = new wxBitmap( save_xpm
);
324 bitmaps
[3] = new wxBitmap( copy_xpm
);
325 bitmaps
[4] = new wxBitmap( cut_xpm
);
326 bitmaps
[5] = new wxBitmap( paste_xpm
);
327 bitmaps
[6] = new wxBitmap( print_xpm
);
328 bitmaps
[7] = new wxBitmap( help_xpm
);
333 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("New file"));
334 currentX
+= width
+ 5;
335 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Open file"));
336 currentX
+= width
+ 5;
337 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Save file"));
338 currentX
+= width
+ 5;
339 toolBar
->AddSeparator();
340 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Copy"));
341 currentX
+= width
+ 5;
342 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Cut"));
343 currentX
+= width
+ 5;
344 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Paste"));
345 currentX
+= width
+ 5;
346 toolBar
->AddSeparator();
347 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, false, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Print"));
348 currentX
+= width
+ 5;
349 toolBar
->AddSeparator();
350 toolBar
->AddTool( MDI_ABOUT
, *bitmaps
[7], wxNullBitmap
, true, currentX
, wxDefaultCoord
, (wxObject
*) NULL
, _T("Help"));
355 for (i
= 0; i
< 8; i
++)
358 #endif // wxUSE_TOOLBAR
360 // ---------------------------------------------------------------------------
362 // ---------------------------------------------------------------------------
364 // Define a constructor for my canvas
365 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
366 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
368 wxNO_FULL_REPAINT_ON_RESIZE
|
369 wxVSCROLL
| wxHSCROLL
)
371 SetBackgroundColour(wxColour(_T("WHITE")));
376 // Define the repainting behaviour
377 void MyCanvas::OnDraw(wxDC
& dc
)
379 if ( !m_text
.empty() )
380 dc
.DrawText(m_text
, 10, 10);
382 dc
.SetFont(*wxSWISS_FONT
);
383 dc
.SetPen(*wxGREEN_PEN
);
384 dc
.DrawLine(0, 0, 200, 200);
385 dc
.DrawLine(200, 0, 0, 200);
387 dc
.SetBrush(*wxCYAN_BRUSH
);
388 dc
.SetPen(*wxRED_PEN
);
389 dc
.DrawRectangle(100, 100, 100, 50);
390 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
392 dc
.DrawEllipse(250, 250, 100, 50);
394 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
395 #endif // wxUSE_SPLINES
396 dc
.DrawLine(50, 230, 200, 230);
397 dc
.DrawText(_T("This is a test string"), 50, 230);
400 points
[0].x
= 200; points
[0].y
= 300;
401 points
[1].x
= 100; points
[1].y
= 400;
402 points
[2].x
= 300; points
[2].y
= 400;
404 dc
.DrawPolygon(3, points
);
407 // This implements a tiny doodling program! Drag the mouse using the left
409 void MyCanvas::OnEvent(wxMouseEvent
& event
)
414 wxPoint
pt(event
.GetLogicalPosition(dc
));
416 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
418 dc
.SetPen(*wxBLACK_PEN
);
419 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
428 // ---------------------------------------------------------------------------
430 // ---------------------------------------------------------------------------
432 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
)
433 : wxMDIChildFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxDefaultSize
,
434 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
436 canvas
= (MyCanvas
*) NULL
;
437 my_children
.Append(this);
439 // this should work for MDI frames as well as for normal ones
440 SetSizeHints(100, 100);
445 my_children
.DeleteObject(this);
448 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
453 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
459 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
464 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
466 SetClientSize(100, 100);
469 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
472 static wxString s_title
= _T("Canvas Frame");
474 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
475 _T("MDI sample question"),
477 GetParent()->GetParent());
486 void MyChild::OnActivate(wxActivateEvent
& event
)
488 if ( event
.GetActive() && canvas
)
492 void MyChild::OnMove(wxMoveEvent
& event
)
494 // VZ: here everything is totally wrong under MSW, the positions are
495 // different and both wrong (pos2 is off by 2 pixels for me which seems
496 // to be the width of the MDI canvas border)
497 wxPoint pos1
= event
.GetPosition(),
498 pos2
= GetPosition();
499 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
500 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
505 void MyChild::OnSize(wxSizeEvent
& event
)
507 // VZ: under MSW the size event carries the client size (quite
508 // unexpectedly) *except* for the very first one which has the full
509 // size... what should it really be? TODO: check under wxGTK
510 wxSize size1
= event
.GetSize(),
512 size3
= GetClientSize();
513 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
514 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
519 void MyChild::OnClose(wxCloseEvent
& event
)
521 if ( canvas
&& canvas
->IsDirty() )
523 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
524 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
539 #include "wx/clipbrd.h"
541 void MyChild::OnPaste(wxCommandEvent
& WXUNUSED(event
))
543 wxClipboardLocker lock
;
544 wxTextDataObject data
;
545 canvas
->SetText(wxTheClipboard
->GetData(data
) ? data
.GetText().c_str()
546 : _T("No text on clipboard"));
549 void MyChild::OnUpdatePaste(wxUpdateUIEvent
& event
)
551 wxClipboardLocker lock
;
552 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
555 #endif // wxUSE_CLIPBOARD