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
);
120 // Experimental: change the window menu
121 wxMenu
* windowMenu
= new wxMenu
;
122 windowMenu
->Append(5000, _T("My menu item!"));
123 frame
->SetWindowMenu(windowMenu
);
127 frame
->SetIcon(wxICON(sample
));
130 wxMenu
*file_menu
= new wxMenu
;
132 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window\tCtrl-N"), _T("Create a new child window"));
133 file_menu
->Append(MDI_QUIT
, _T("&Exit\tAlt-X"), _T("Quit the program"));
135 wxMenu
*help_menu
= new wxMenu
;
136 help_menu
->Append(MDI_ABOUT
, _T("&About\tF1"));
138 wxMenuBar
*menu_bar
= new wxMenuBar
;
140 menu_bar
->Append(file_menu
, _T("&File"));
141 menu_bar
->Append(help_menu
, _T("&Help"));
143 // Associate the menu bar with the frame
144 frame
->SetMenuBar(menu_bar
);
147 frame
->CreateStatusBar();
148 #endif // wxUSE_STATUSBAR
157 // ---------------------------------------------------------------------------
159 // ---------------------------------------------------------------------------
161 // Define my frame constructor
162 MyFrame::MyFrame(wxWindow
*parent
,
164 const wxString
& title
,
168 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
170 textWindow
= new wxTextCtrl(this, wxID_ANY
, _T("A help window"),
171 wxDefaultPosition
, wxDefaultSize
,
172 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
175 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
176 InitToolBar(GetToolBar());
177 #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
);
187 #endif // wxUSE_ACCEL
190 void MyFrame::OnClose(wxCloseEvent
& event
)
192 if ( event
.CanVeto() && (gs_nFrames
> 0) )
195 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
196 if ( wxMessageBox(msg
, _T("Please confirm"),
197 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
208 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
213 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
215 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
216 _T("Author: Julian Smart (c) 1997\n")
217 _T("Usage: mdi.exe"), _T("About MDI Demo"));
220 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
222 // Make another frame, containing a canvas
223 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"));
226 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
228 subframe
->SetTitle(title
);
231 subframe
->SetIcon(wxICON(chart
));
235 wxMenu
*file_menu
= new wxMenu
;
237 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window"));
238 file_menu
->Append(MDI_CHILD_QUIT
, _T("&Close child"), _T("Close this window"));
239 file_menu
->Append(MDI_QUIT
, _T("&Exit"));
241 wxMenu
*option_menu
= new wxMenu
;
243 option_menu
->Append(MDI_REFRESH
, _T("&Refresh picture"));
244 option_menu
->Append(MDI_CHANGE_TITLE
, _T("Change &title...\tCtrl-T"));
245 option_menu
->AppendSeparator();
246 option_menu
->Append(MDI_CHANGE_POSITION
, _T("Move frame\tCtrl-M"));
247 option_menu
->Append(MDI_CHANGE_SIZE
, _T("Resize frame\tCtrl-S"));
249 option_menu
->AppendSeparator();
250 option_menu
->Append(wxID_PASTE
, _T("Copy text from clipboard\tCtrl-V"));
251 #endif // wxUSE_CLIPBOARD
253 wxMenu
*help_menu
= new wxMenu
;
254 help_menu
->Append(MDI_ABOUT
, _T("&About"));
256 wxMenuBar
*menu_bar
= new wxMenuBar
;
258 menu_bar
->Append(file_menu
, _T("&File"));
259 menu_bar
->Append(option_menu
, _T("&Child"));
260 menu_bar
->Append(help_menu
, _T("&Help"));
262 // Associate the menu bar with the frame
263 subframe
->SetMenuBar(menu_bar
);
264 #endif // wxUSE_MENUS
267 subframe
->CreateStatusBar();
268 subframe
->SetStatusText(title
);
269 #endif // wxUSE_STATUSBAR
272 subframe
->GetClientSize(&width
, &height
);
273 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
274 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
275 subframe
->canvas
= canvas
;
277 // Give it scrollbars
278 canvas
->SetScrollbars(20, 20, 50, 50);
280 subframe
->Show(true);
283 void MyFrame::OnSize(wxSizeEvent
&
284 #ifdef __WXUNIVERSAL__
292 GetClientSize(&w
, &h
);
294 textWindow
->SetSize(0, 0, 200, h
);
295 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
297 // FIXME: On wxX11, we need the MDI frame to process this
298 // event, but on other platforms this should not
300 #ifdef __WXUNIVERSAL__
306 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
310 bitmaps
[0] = wxBitmap( new_xpm
);
311 bitmaps
[1] = wxBitmap( open_xpm
);
312 bitmaps
[2] = wxBitmap( save_xpm
);
313 bitmaps
[3] = wxBitmap( copy_xpm
);
314 bitmaps
[4] = wxBitmap( cut_xpm
);
315 bitmaps
[5] = wxBitmap( paste_xpm
);
316 bitmaps
[6] = wxBitmap( print_xpm
);
317 bitmaps
[7] = wxBitmap( help_xpm
);
319 toolBar
->AddTool(MDI_NEW_WINDOW
, _T("New"), bitmaps
[0], _T("New file"));
320 toolBar
->AddTool(1, _T("Open"), bitmaps
[1], _T("Open file"));
321 toolBar
->AddTool(2, _T("Save"), bitmaps
[2], _T("Save file"));
322 toolBar
->AddSeparator();
323 toolBar
->AddTool(3, _T("Copy"), bitmaps
[3], _T("Copy"));
324 toolBar
->AddTool(4, _T("Cut"), bitmaps
[4], _T("Cut"));
325 toolBar
->AddTool(5, _T("Paste"), bitmaps
[5], _T("Paste"));
326 toolBar
->AddSeparator();
327 toolBar
->AddTool(6, _T("Print"), bitmaps
[6], _T("Print"));
328 toolBar
->AddSeparator();
329 toolBar
->AddTool(MDI_ABOUT
, _T("About"), bitmaps
[7], _T("Help"));
333 #endif // wxUSE_TOOLBAR
335 // ---------------------------------------------------------------------------
337 // ---------------------------------------------------------------------------
339 // Define a constructor for my canvas
340 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
341 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
343 wxNO_FULL_REPAINT_ON_RESIZE
|
344 wxVSCROLL
| wxHSCROLL
)
346 SetBackgroundColour(wxColour(_T("WHITE")));
351 // Define the repainting behaviour
352 void MyCanvas::OnDraw(wxDC
& dc
)
354 if ( !m_text
.empty() )
355 dc
.DrawText(m_text
, 10, 10);
357 dc
.SetFont(*wxSWISS_FONT
);
358 dc
.SetPen(*wxGREEN_PEN
);
359 dc
.DrawLine(0, 0, 200, 200);
360 dc
.DrawLine(200, 0, 0, 200);
362 dc
.SetBrush(*wxCYAN_BRUSH
);
363 dc
.SetPen(*wxRED_PEN
);
364 dc
.DrawRectangle(100, 100, 100, 50);
365 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
367 dc
.DrawEllipse(250, 250, 100, 50);
369 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
370 #endif // wxUSE_SPLINES
371 dc
.DrawLine(50, 230, 200, 230);
372 dc
.DrawText(_T("This is a test string"), 50, 230);
375 points
[0].x
= 200; points
[0].y
= 300;
376 points
[1].x
= 100; points
[1].y
= 400;
377 points
[2].x
= 300; points
[2].y
= 400;
379 dc
.DrawPolygon(3, points
);
382 // This implements a tiny doodling program! Drag the mouse using the left
384 void MyCanvas::OnEvent(wxMouseEvent
& event
)
389 wxPoint
pt(event
.GetLogicalPosition(dc
));
391 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
393 dc
.SetPen(*wxBLACK_PEN
);
394 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
403 // ---------------------------------------------------------------------------
405 // ---------------------------------------------------------------------------
407 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
)
408 : wxMDIChildFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxDefaultSize
,
409 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
411 canvas
= (MyCanvas
*) NULL
;
412 my_children
.Append(this);
414 // this should work for MDI frames as well as for normal ones
415 SetSizeHints(100, 100);
420 my_children
.DeleteObject(this);
423 void MyChild::OnQuit(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
= _T("Canvas Frame");
449 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
450 _T("MDI sample question"),
452 GetParent()->GetParent());
458 #endif // wxUSE_TEXTDLG
461 void MyChild::OnActivate(wxActivateEvent
& event
)
463 if ( event
.GetActive() && canvas
)
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(wxT("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(wxT("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::OnClose(wxCloseEvent
& event
)
496 if ( canvas
&& canvas
->IsDirty() )
498 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
499 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
514 #include "wx/clipbrd.h"
516 void MyChild::OnPaste(wxCommandEvent
& WXUNUSED(event
))
518 wxClipboardLocker lock
;
519 wxTextDataObject data
;
520 canvas
->SetText(wxTheClipboard
->GetData(data
) ? data
.GetText().c_str()
521 : _T("No text on clipboard"));
524 void MyChild::OnUpdatePaste(wxUpdateUIEvent
& event
)
526 wxClipboardLocker lock
;
527 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
530 #endif // wxUSE_CLIPBOARD