1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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(__WXGTK__) || defined(__WXMOTIF__)
35 #include "mondrian.xpm"
36 #include "bitmaps/new.xpm"
37 #include "bitmaps/open.xpm"
38 #include "bitmaps/save.xpm"
39 #include "bitmaps/copy.xpm"
40 #include "bitmaps/cut.xpm"
41 #include "bitmaps/paste.xpm"
42 #include "bitmaps/print.xpm"
43 #include "bitmaps/help.xpm"
50 // ---------------------------------------------------------------------------
52 // ---------------------------------------------------------------------------
54 MyFrame
*frame
= (MyFrame
*) NULL
;
57 // For drawing lines in a canvas
58 static long xpos
= -1;
59 static long ypos
= -1;
61 static int gs_nFrames
= 0;
63 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
68 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
69 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
70 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
72 EVT_CLOSE(MyFrame::OnClose
)
74 EVT_SIZE(MyFrame::OnSize
)
77 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
78 // to the parent window for processing, so no need to
79 // duplicate event handlers here.
80 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
81 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
82 EVT_MENU(MDI_REFRESH
, MyChild::OnRefresh
)
83 EVT_MENU(MDI_CHANGE_TITLE
, MyChild::OnChangeTitle
)
85 EVT_CLOSE(MyChild::OnClose
)
88 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
89 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
92 // ===========================================================================
94 // ===========================================================================
96 // ---------------------------------------------------------------------------
98 // ---------------------------------------------------------------------------
100 // Initialise this in OnInit, not statically
103 // Create the main frame window
105 frame
= new MyFrame((wxFrame
*)NULL
, -1, "MDI Demo",
106 wxPoint(-1, -1), wxSize(500, 400),
107 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
110 // Experimental: change the window menu
111 wxMenu
* windowMenu
= new wxMenu
;
112 windowMenu
->Append(5000, "My menu item!");
113 frame
->SetWindowMenu(windowMenu
);
119 frame
->SetIcon(wxIcon("mdi_icn"));
121 frame
->SetIcon(wxIcon( mondrian_xpm
));
125 wxMenu
*file_menu
= new wxMenu
;
127 file_menu
->Append(MDI_NEW_WINDOW
, "&New window\tCtrl-N", "Create a new child window");
128 file_menu
->Append(MDI_QUIT
, "&Exit\tAlt-X", "Quit the program");
130 wxMenu
*help_menu
= new wxMenu
;
131 help_menu
->Append(MDI_ABOUT
, "&About\tF1");
133 wxMenuBar
*menu_bar
= new wxMenuBar
;
135 menu_bar
->Append(file_menu
, "&File");
136 menu_bar
->Append(help_menu
, "&Help");
138 // Associate the menu bar with the frame
139 frame
->SetMenuBar(menu_bar
);
141 frame
->CreateStatusBar();
150 // ---------------------------------------------------------------------------
152 // ---------------------------------------------------------------------------
154 // Define my frame constructor
155 MyFrame::MyFrame(wxWindow
*parent
,
157 const wxString
& title
,
161 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
163 textWindow
= new wxTextCtrl(this, -1, "A help window",
164 wxDefaultPosition
, wxDefaultSize
,
165 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
167 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
168 InitToolBar(GetToolBar());
171 wxAcceleratorEntry entries
[3];
172 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
173 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
174 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
175 wxAcceleratorTable
accel(3, entries
);
176 SetAcceleratorTable(accel
);
179 void MyFrame::OnClose(wxCloseEvent
& event
)
181 if ( event
.CanVeto() && (gs_nFrames
> 0) )
184 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
185 if ( wxMessageBox(msg
, "Please confirm",
186 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
197 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
202 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
204 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
205 "Author: Julian Smart (c) 1997\n"
206 "Usage: mdi.exe", "About MDI Demo");
209 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
211 // Make another frame, containing a canvas
212 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame",
213 wxPoint(-1, -1), wxSize(-1, -1),
214 wxDEFAULT_FRAME_STYLE
);
217 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
219 subframe
->SetTitle(title
);
223 subframe
->SetIcon(wxIcon("chrt_icn"));
225 subframe
->SetIcon(wxIcon( mondrian_xpm
));
229 wxMenu
*file_menu
= new wxMenu
;
231 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
232 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child", "Close this window");
233 file_menu
->Append(MDI_QUIT
, "&Exit");
235 wxMenu
*option_menu
= new wxMenu
;
237 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
238 option_menu
->Append(MDI_CHANGE_TITLE
, "Change &title...\tCtrl-T");
240 wxMenu
*help_menu
= new wxMenu
;
241 help_menu
->Append(MDI_ABOUT
, "&About");
243 wxMenuBar
*menu_bar
= new wxMenuBar
;
245 menu_bar
->Append(file_menu
, "&File");
246 menu_bar
->Append(option_menu
, "&Options");
247 menu_bar
->Append(help_menu
, "&Help");
249 // Associate the menu bar with the frame
250 subframe
->SetMenuBar(menu_bar
);
252 subframe
->CreateStatusBar();
253 subframe
->SetStatusText(title
);
256 subframe
->GetClientSize(&width
, &height
);
257 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
258 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
259 subframe
->canvas
= canvas
;
261 // Give it scrollbars
262 canvas
->SetScrollbars(20, 20, 50, 50);
264 subframe
->Show(TRUE
);
267 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
270 GetClientSize(&w
, &h
);
272 textWindow
->SetSize(0, 0, 200, h
);
273 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
276 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
278 wxBitmap
* bitmaps
[8];
281 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
282 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
283 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
284 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
285 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
286 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
287 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
288 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
290 bitmaps
[0] = new wxBitmap( new_xpm
);
291 bitmaps
[1] = new wxBitmap( open_xpm
);
292 bitmaps
[2] = new wxBitmap( save_xpm
);
293 bitmaps
[3] = new wxBitmap( copy_xpm
);
294 bitmaps
[4] = new wxBitmap( cut_xpm
);
295 bitmaps
[5] = new wxBitmap( paste_xpm
);
296 bitmaps
[6] = new wxBitmap( print_xpm
);
297 bitmaps
[7] = new wxBitmap( help_xpm
);
307 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
308 currentX
+= width
+ 5;
309 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
310 currentX
+= width
+ 5;
311 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Save file");
312 currentX
+= width
+ 5;
313 toolBar
->AddSeparator();
314 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Copy");
315 currentX
+= width
+ 5;
316 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Cut");
317 currentX
+= width
+ 5;
318 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
319 currentX
+= width
+ 5;
320 toolBar
->AddSeparator();
321 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Print");
322 currentX
+= width
+ 5;
323 toolBar
->AddSeparator();
324 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help");
329 for (i
= 0; i
< 8; i
++)
333 // ---------------------------------------------------------------------------
335 // ---------------------------------------------------------------------------
337 // Define a constructor for my canvas
338 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
339 : wxScrolledWindow(parent
, -1, pos
, size
,
340 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
342 SetBackgroundColour(wxColour("WHITE"));
347 // Define the repainting behaviour
348 void MyCanvas::OnDraw(wxDC
& dc
)
350 dc
.SetFont(*wxSWISS_FONT
);
351 dc
.SetPen(*wxGREEN_PEN
);
352 dc
.DrawLine(0, 0, 200, 200);
353 dc
.DrawLine(200, 0, 0, 200);
355 dc
.SetBrush(*wxCYAN_BRUSH
);
356 dc
.SetPen(*wxRED_PEN
);
357 dc
.DrawRectangle(100, 100, 100, 50);
358 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
360 dc
.DrawEllipse(250, 250, 100, 50);
362 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
363 #endif // wxUSE_SPLINES
364 dc
.DrawLine(50, 230, 200, 230);
365 dc
.DrawText("This is a test string", 50, 230);
368 points
[0].x
= 200; points
[0].y
= 300;
369 points
[1].x
= 100; points
[1].y
= 400;
370 points
[2].x
= 300; points
[2].y
= 400;
372 dc
.DrawPolygon(3, points
);
375 // This implements a tiny doodling program! Drag the mouse using the left
377 void MyCanvas::OnEvent(wxMouseEvent
& event
)
382 wxPoint
pt(event
.GetLogicalPosition(dc
));
384 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
386 dc
.SetPen(*wxBLACK_PEN
);
387 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
396 // ---------------------------------------------------------------------------
398 // ---------------------------------------------------------------------------
400 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
401 const wxPoint
& pos
, const wxSize
& size
,
403 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
405 canvas
= (MyCanvas
*) NULL
;
406 my_children
.Append(this);
411 my_children
.DeleteObject(this);
414 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
419 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
425 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
427 static wxString s_title
= _T("Canvas Frame");
429 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
430 _T("MDI sample question"),
432 GetParent()->GetParent());
440 void MyChild::OnActivate(wxActivateEvent
& event
)
442 if ( event
.GetActive() && canvas
)
446 void MyChild::OnClose(wxCloseEvent
& event
)
448 if ( canvas
&& canvas
->IsDirty() )
450 if ( wxMessageBox("Really close?", "Please confirm",
451 wxICON_QUESTION
| wxYES_NO
) != wxYES
)