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 "../sample.xpm"
39 #include "bitmaps/new.xpm"
40 #include "bitmaps/open.xpm"
41 #include "bitmaps/save.xpm"
42 #include "bitmaps/copy.xpm"
43 #include "bitmaps/cut.xpm"
44 #include "bitmaps/paste.xpm"
45 #include "bitmaps/print.xpm"
46 #include "bitmaps/help.xpm"
53 // ---------------------------------------------------------------------------
55 // ---------------------------------------------------------------------------
57 MyFrame
*frame
= (MyFrame
*) NULL
;
60 // For drawing lines in a canvas
61 static long xpos
= -1;
62 static long ypos
= -1;
64 static int gs_nFrames
= 0;
66 // ---------------------------------------------------------------------------
68 // ---------------------------------------------------------------------------
70 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
71 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
72 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
73 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
75 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
)
90 EVT_MENU(wxID_PASTE
, MyChild::OnPaste
)
91 EVT_UPDATE_UI(wxID_PASTE
, MyChild::OnUpdatePaste
)
92 #endif // wxUSE_CLIPBOARD
94 EVT_SIZE(MyChild::OnSize
)
95 EVT_MOVE(MyChild::OnMove
)
97 EVT_CLOSE(MyChild::OnClose
)
100 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
101 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
104 // ===========================================================================
106 // ===========================================================================
108 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
112 // Initialise this in OnInit, not statically
115 if ( !wxApp::OnInit() )
118 // Create the main frame window
120 frame
= new MyFrame((wxFrame
*)NULL
, wxID_ANY
, _T("MDI Demo"),
121 wxDefaultPosition
, wxSize(500, 400),
122 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
124 // Experimental: change the window menu
125 wxMenu
* windowMenu
= new wxMenu
;
126 windowMenu
->Append(5000, _T("My menu item!"));
127 frame
->SetWindowMenu(windowMenu
);
131 frame
->SetIcon(wxICON(sample
));
134 wxMenu
*file_menu
= new wxMenu
;
136 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window\tCtrl-N"), _T("Create a new child window"));
137 file_menu
->Append(MDI_QUIT
, _T("&Exit\tAlt-X"), _T("Quit the program"));
139 wxMenu
*help_menu
= new wxMenu
;
140 help_menu
->Append(MDI_ABOUT
, _T("&About\tF1"));
142 wxMenuBar
*menu_bar
= new wxMenuBar
;
144 menu_bar
->Append(file_menu
, _T("&File"));
145 menu_bar
->Append(help_menu
, _T("&Help"));
147 // Associate the menu bar with the frame
148 frame
->SetMenuBar(menu_bar
);
151 frame
->CreateStatusBar();
152 #endif // wxUSE_STATUSBAR
161 // ---------------------------------------------------------------------------
163 // ---------------------------------------------------------------------------
165 // Define my frame constructor
166 MyFrame::MyFrame(wxWindow
*parent
,
168 const wxString
& title
,
172 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
174 textWindow
= new wxTextCtrl(this, wxID_ANY
, _T("A help window"),
175 wxDefaultPosition
, wxDefaultSize
,
176 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
179 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
180 InitToolBar(GetToolBar());
181 #endif // wxUSE_TOOLBAR
185 wxAcceleratorEntry entries
[3];
186 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
187 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
188 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
189 wxAcceleratorTable
accel(3, entries
);
190 SetAcceleratorTable(accel
);
191 #endif // wxUSE_ACCEL
194 void MyFrame::OnClose(wxCloseEvent
& event
)
196 if ( event
.CanVeto() && (gs_nFrames
> 0) )
199 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
200 if ( wxMessageBox(msg
, _T("Please confirm"),
201 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
212 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
217 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
219 (void)wxMessageBox(_T("wxWidgets 2.0 MDI Demo\n")
220 _T("Author: Julian Smart (c) 1997\n")
221 _T("Usage: mdi.exe"), _T("About MDI Demo"));
224 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
226 // Make another frame, containing a canvas
227 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"));
230 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
232 subframe
->SetTitle(title
);
235 subframe
->SetIcon(wxICON(chart
));
239 wxMenu
*file_menu
= new wxMenu
;
241 file_menu
->Append(MDI_NEW_WINDOW
, _T("&New window"));
242 file_menu
->Append(MDI_CHILD_QUIT
, _T("&Close child"), _T("Close this window"));
243 file_menu
->Append(MDI_QUIT
, _T("&Exit"));
245 wxMenu
*option_menu
= new wxMenu
;
247 option_menu
->Append(MDI_REFRESH
, _T("&Refresh picture"));
248 option_menu
->Append(MDI_CHANGE_TITLE
, _T("Change &title...\tCtrl-T"));
249 option_menu
->AppendSeparator();
250 option_menu
->Append(MDI_CHANGE_POSITION
, _T("Move frame\tCtrl-M"));
251 option_menu
->Append(MDI_CHANGE_SIZE
, _T("Resize frame\tCtrl-S"));
253 option_menu
->AppendSeparator();
254 option_menu
->Append(wxID_PASTE
, _T("Copy text from clipboard\tCtrl-V"));
255 #endif // wxUSE_CLIPBOARD
257 wxMenu
*help_menu
= new wxMenu
;
258 help_menu
->Append(MDI_ABOUT
, _T("&About"));
260 wxMenuBar
*menu_bar
= new wxMenuBar
;
262 menu_bar
->Append(file_menu
, _T("&File"));
263 menu_bar
->Append(option_menu
, _T("&Child"));
264 menu_bar
->Append(help_menu
, _T("&Help"));
266 // Associate the menu bar with the frame
267 subframe
->SetMenuBar(menu_bar
);
268 #endif // wxUSE_MENUS
271 subframe
->CreateStatusBar();
272 subframe
->SetStatusText(title
);
273 #endif // wxUSE_STATUSBAR
276 subframe
->GetClientSize(&width
, &height
);
277 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
278 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
279 subframe
->canvas
= canvas
;
281 // Give it scrollbars
282 canvas
->SetScrollbars(20, 20, 50, 50);
284 subframe
->Show(true);
287 void MyFrame::OnSize(wxSizeEvent
&
288 #ifdef __WXUNIVERSAL__
296 GetClientSize(&w
, &h
);
298 textWindow
->SetSize(0, 0, 200, h
);
299 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
301 // FIXME: On wxX11, we need the MDI frame to process this
302 // event, but on other platforms this should not
304 #ifdef __WXUNIVERSAL__
310 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
314 bitmaps
[0] = wxBitmap( new_xpm
);
315 bitmaps
[1] = wxBitmap( open_xpm
);
316 bitmaps
[2] = wxBitmap( save_xpm
);
317 bitmaps
[3] = wxBitmap( copy_xpm
);
318 bitmaps
[4] = wxBitmap( cut_xpm
);
319 bitmaps
[5] = wxBitmap( paste_xpm
);
320 bitmaps
[6] = wxBitmap( print_xpm
);
321 bitmaps
[7] = wxBitmap( help_xpm
);
323 toolBar
->AddTool(MDI_NEW_WINDOW
, _T("New"), bitmaps
[0], _T("New file"));
324 toolBar
->AddTool(1, _T("Open"), bitmaps
[1], _T("Open file"));
325 toolBar
->AddTool(2, _T("Save"), bitmaps
[2], _T("Save file"));
326 toolBar
->AddSeparator();
327 toolBar
->AddTool(3, _T("Copy"), bitmaps
[3], _T("Copy"));
328 toolBar
->AddTool(4, _T("Cut"), bitmaps
[4], _T("Cut"));
329 toolBar
->AddTool(5, _T("Paste"), bitmaps
[5], _T("Paste"));
330 toolBar
->AddSeparator();
331 toolBar
->AddTool(6, _T("Print"), bitmaps
[6], _T("Print"));
332 toolBar
->AddSeparator();
333 toolBar
->AddTool(MDI_ABOUT
, _T("About"), bitmaps
[7], _T("Help"));
337 #endif // wxUSE_TOOLBAR
339 // ---------------------------------------------------------------------------
341 // ---------------------------------------------------------------------------
343 // Define a constructor for my canvas
344 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
345 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
347 wxNO_FULL_REPAINT_ON_RESIZE
|
348 wxVSCROLL
| wxHSCROLL
)
350 SetBackgroundColour(wxColour(_T("WHITE")));
355 // Define the repainting behaviour
356 void MyCanvas::OnDraw(wxDC
& dc
)
358 if ( !m_text
.empty() )
359 dc
.DrawText(m_text
, 10, 10);
361 dc
.SetFont(*wxSWISS_FONT
);
362 dc
.SetPen(*wxGREEN_PEN
);
363 dc
.DrawLine(0, 0, 200, 200);
364 dc
.DrawLine(200, 0, 0, 200);
366 dc
.SetBrush(*wxCYAN_BRUSH
);
367 dc
.SetPen(*wxRED_PEN
);
368 dc
.DrawRectangle(100, 100, 100, 50);
369 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
371 dc
.DrawEllipse(250, 250, 100, 50);
373 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
374 #endif // wxUSE_SPLINES
375 dc
.DrawLine(50, 230, 200, 230);
376 dc
.DrawText(_T("This is a test string"), 50, 230);
379 points
[0].x
= 200; points
[0].y
= 300;
380 points
[1].x
= 100; points
[1].y
= 400;
381 points
[2].x
= 300; points
[2].y
= 400;
383 dc
.DrawPolygon(3, points
);
386 // This implements a tiny doodling program! Drag the mouse using the left
388 void MyCanvas::OnEvent(wxMouseEvent
& event
)
393 wxPoint
pt(event
.GetLogicalPosition(dc
));
395 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
397 dc
.SetPen(*wxBLACK_PEN
);
398 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
407 // ---------------------------------------------------------------------------
409 // ---------------------------------------------------------------------------
411 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
)
412 : wxMDIChildFrame(parent
, wxID_ANY
, title
, wxDefaultPosition
, wxDefaultSize
,
413 wxDEFAULT_FRAME_STYLE
| wxNO_FULL_REPAINT_ON_RESIZE
)
415 canvas
= (MyCanvas
*) NULL
;
416 my_children
.Append(this);
418 // this should work for MDI frames as well as for normal ones
419 SetSizeHints(100, 100);
424 my_children
.DeleteObject(this);
427 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
432 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
438 void MyChild::OnChangePosition(wxCommandEvent
& WXUNUSED(event
))
443 void MyChild::OnChangeSize(wxCommandEvent
& WXUNUSED(event
))
445 SetClientSize(100, 100);
448 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
451 static wxString s_title
= _T("Canvas Frame");
453 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
454 _T("MDI sample question"),
456 GetParent()->GetParent());
462 #endif // wxUSE_TEXTDLG
465 void MyChild::OnActivate(wxActivateEvent
& event
)
467 if ( event
.GetActive() && canvas
)
471 void MyChild::OnMove(wxMoveEvent
& event
)
473 // VZ: here everything is totally wrong under MSW, the positions are
474 // different and both wrong (pos2 is off by 2 pixels for me which seems
475 // to be the width of the MDI canvas border)
476 wxPoint pos1
= event
.GetPosition(),
477 pos2
= GetPosition();
478 wxLogStatus(wxT("position from event: (%d, %d), from frame (%d, %d)"),
479 pos1
.x
, pos1
.y
, pos2
.x
, pos2
.y
);
484 void MyChild::OnSize(wxSizeEvent
& event
)
486 // VZ: under MSW the size event carries the client size (quite
487 // unexpectedly) *except* for the very first one which has the full
488 // size... what should it really be? TODO: check under wxGTK
489 wxSize size1
= event
.GetSize(),
491 size3
= GetClientSize();
492 wxLogStatus(wxT("size from event: %dx%d, from frame %dx%d, client %dx%d"),
493 size1
.x
, size1
.y
, size2
.x
, size2
.y
, size3
.x
, size3
.y
);
498 void MyChild::OnClose(wxCloseEvent
& event
)
500 if ( canvas
&& canvas
->IsDirty() )
502 if ( wxMessageBox(_T("Really close?"), _T("Please confirm"),
503 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
518 #include "wx/clipbrd.h"
520 void MyChild::OnPaste(wxCommandEvent
& WXUNUSED(event
))
522 wxClipboardLocker lock
;
523 wxTextDataObject data
;
524 canvas
->SetText(wxTheClipboard
->GetData(data
) ? data
.GetText().c_str()
525 : _T("No text on clipboard"));
528 void MyChild::OnUpdatePaste(wxUpdateUIEvent
& event
)
530 wxClipboardLocker lock
;
531 event
.Enable( wxTheClipboard
->IsSupported(wxDF_TEXT
) );
534 #endif // wxUSE_CLIPBOARD