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
)
84 EVT_CLOSE(MyChild::OnClose
)
87 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
88 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
91 // ===========================================================================
93 // ===========================================================================
95 // ---------------------------------------------------------------------------
97 // ---------------------------------------------------------------------------
99 // Initialise this in OnInit, not statically
102 // Create the main frame window
104 frame
= new MyFrame((wxFrame
*)NULL
, -1, "MDI Demo",
105 wxPoint(-1, -1), wxSize(500, 400),
106 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
109 // Experimental: change the window menu
110 wxMenu
* windowMenu
= new wxMenu
;
111 windowMenu
->Append(5000, "My menu item!");
112 frame
->SetWindowMenu(windowMenu
);
118 frame
->SetIcon(wxIcon("mdi_icn"));
120 frame
->SetIcon(wxIcon( mondrian_xpm
));
124 wxMenu
*file_menu
= new wxMenu
;
126 file_menu
->Append(MDI_NEW_WINDOW
, "&New window", "Create a new child window");
127 file_menu
->Append(MDI_QUIT
, "&Exit", "Quit the program");
129 wxMenu
*help_menu
= new wxMenu
;
130 help_menu
->Append(MDI_ABOUT
, "&About");
132 wxMenuBar
*menu_bar
= new wxMenuBar
;
134 menu_bar
->Append(file_menu
, "&File");
135 menu_bar
->Append(help_menu
, "&Help");
137 // Associate the menu bar with the frame
138 frame
->SetMenuBar(menu_bar
);
140 frame
->CreateStatusBar();
149 // ---------------------------------------------------------------------------
151 // ---------------------------------------------------------------------------
153 // Define my frame constructor
154 MyFrame::MyFrame(wxWindow
*parent
,
156 const wxString
& title
,
160 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
162 textWindow
= new wxTextCtrl(this, -1, "A help window",
163 wxDefaultPosition
, wxDefaultSize
,
164 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
166 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
167 InitToolBar(GetToolBar());
170 wxAcceleratorEntry entries
[3];
171 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
172 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
173 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
174 wxAcceleratorTable
accel(3, entries
);
175 SetAcceleratorTable(accel
);
178 void MyFrame::OnClose(wxCloseEvent
& event
)
180 if ( event
.CanVeto() && (gs_nFrames
> 0) )
183 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
184 if ( wxMessageBox(msg
, "Please confirm",
185 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
196 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
201 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
203 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
204 "Author: Julian Smart (c) 1997\n"
205 "Usage: mdi.exe", "About MDI Demo");
208 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
210 // Make another frame, containing a canvas
211 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame",
212 wxPoint(-1, -1), wxSize(-1, -1),
213 wxDEFAULT_FRAME_STYLE
);
216 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
218 subframe
->SetTitle(title
);
222 subframe
->SetIcon(wxIcon("chrt_icn"));
224 subframe
->SetIcon(wxIcon( mondrian_xpm
));
228 wxMenu
*file_menu
= new wxMenu
;
230 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
231 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child", "Close this window");
232 file_menu
->Append(MDI_QUIT
, "&Exit");
234 wxMenu
*option_menu
= new wxMenu
;
237 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
239 wxMenu
*help_menu
= new wxMenu
;
240 help_menu
->Append(MDI_ABOUT
, "&About");
242 wxMenuBar
*menu_bar
= new wxMenuBar
;
244 menu_bar
->Append(file_menu
, "&File");
245 menu_bar
->Append(option_menu
, "&Options");
246 menu_bar
->Append(help_menu
, "&Help");
248 // Associate the menu bar with the frame
249 subframe
->SetMenuBar(menu_bar
);
251 subframe
->CreateStatusBar();
252 subframe
->SetStatusText(title
);
255 subframe
->GetClientSize(&width
, &height
);
256 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
257 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
258 subframe
->canvas
= canvas
;
260 // Give it scrollbars
261 canvas
->SetScrollbars(20, 20, 50, 50);
263 subframe
->Show(TRUE
);
266 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
269 GetClientSize(&w
, &h
);
271 textWindow
->SetSize(0, 0, 200, h
);
272 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
275 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
277 wxBitmap
* bitmaps
[8];
280 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
281 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
282 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
283 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
284 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
285 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
286 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
287 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
289 bitmaps
[0] = new wxBitmap( new_xpm
);
290 bitmaps
[1] = new wxBitmap( open_xpm
);
291 bitmaps
[2] = new wxBitmap( save_xpm
);
292 bitmaps
[3] = new wxBitmap( copy_xpm
);
293 bitmaps
[4] = new wxBitmap( cut_xpm
);
294 bitmaps
[5] = new wxBitmap( paste_xpm
);
295 bitmaps
[6] = new wxBitmap( print_xpm
);
296 bitmaps
[7] = new wxBitmap( help_xpm
);
306 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
307 currentX
+= width
+ 5;
308 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
309 currentX
+= width
+ 5;
310 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Save file");
311 currentX
+= width
+ 5;
312 toolBar
->AddSeparator();
313 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Copy");
314 currentX
+= width
+ 5;
315 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Cut");
316 currentX
+= width
+ 5;
317 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
318 currentX
+= width
+ 5;
319 toolBar
->AddSeparator();
320 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Print");
321 currentX
+= width
+ 5;
322 toolBar
->AddSeparator();
323 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help");
328 for (i
= 0; i
< 8; i
++)
332 // ---------------------------------------------------------------------------
334 // ---------------------------------------------------------------------------
336 // Define a constructor for my canvas
337 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
338 : wxScrolledWindow(parent
, -1, pos
, size
,
339 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
341 SetBackgroundColour(wxColour("WHITE"));
346 // Define the repainting behaviour
347 void MyCanvas::OnDraw(wxDC
& dc
)
349 dc
.SetFont(*wxSWISS_FONT
);
350 dc
.SetPen(*wxGREEN_PEN
);
351 dc
.DrawLine(0, 0, 200, 200);
352 dc
.DrawLine(200, 0, 0, 200);
354 dc
.SetBrush(*wxCYAN_BRUSH
);
355 dc
.SetPen(*wxRED_PEN
);
356 dc
.DrawRectangle(100, 100, 100, 50);
357 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
359 dc
.DrawEllipse(250, 250, 100, 50);
361 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
362 #endif // wxUSE_SPLINES
363 dc
.DrawLine(50, 230, 200, 230);
364 dc
.DrawText("This is a test string", 50, 230);
367 points
[0].x
= 200; points
[0].y
= 300;
368 points
[1].x
= 100; points
[1].y
= 400;
369 points
[2].x
= 300; points
[2].y
= 400;
371 dc
.DrawPolygon(3, points
);
374 // This implements a tiny doodling program! Drag the mouse using the left
376 void MyCanvas::OnEvent(wxMouseEvent
& event
)
381 wxPoint
pt(event
.GetLogicalPosition(dc
));
383 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
385 dc
.SetPen(*wxBLACK_PEN
);
386 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
395 // ---------------------------------------------------------------------------
397 // ---------------------------------------------------------------------------
399 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
400 const wxPoint
& pos
, const wxSize
& size
,
402 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
404 canvas
= (MyCanvas
*) NULL
;
405 my_children
.Append(this);
410 my_children
.DeleteObject(this);
413 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
418 void MyChild::OnRefresh(wxCommandEvent
& event
)
423 void MyChild::OnActivate(wxActivateEvent
& event
)
425 if ( event
.GetActive() && canvas
)
429 void MyChild::OnClose(wxCloseEvent
& event
)
431 if ( canvas
&& canvas
->IsDirty() )
433 if ( wxMessageBox("Really close?", "Please confirm",
434 wxICON_QUESTION
| wxYES_NO
) != wxYES
)