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
);
110 frame
->SetIcon(wxIcon("mdi_icn"));
112 frame
->SetIcon(wxIcon( mondrian_xpm
));
116 wxMenu
*file_menu
= new wxMenu
;
118 file_menu
->Append(MDI_NEW_WINDOW
, "&New window", "Create a new child window");
119 file_menu
->Append(MDI_QUIT
, "&Exit", "Quit the program");
121 wxMenu
*help_menu
= new wxMenu
;
122 help_menu
->Append(MDI_ABOUT
, "&About");
124 wxMenuBar
*menu_bar
= new wxMenuBar
;
126 menu_bar
->Append(file_menu
, "&File");
127 menu_bar
->Append(help_menu
, "&Help");
129 // Associate the menu bar with the frame
130 frame
->SetMenuBar(menu_bar
);
132 frame
->CreateStatusBar();
141 // ---------------------------------------------------------------------------
143 // ---------------------------------------------------------------------------
145 // Define my frame constructor
146 MyFrame::MyFrame(wxWindow
*parent
,
148 const wxString
& title
,
152 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
154 textWindow
= new wxTextCtrl(this, -1, "A help window",
155 wxDefaultPosition
, wxDefaultSize
,
156 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
158 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
159 InitToolBar(GetToolBar());
162 wxAcceleratorEntry entries
[3];
163 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
164 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
165 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
166 wxAcceleratorTable
accel(3, entries
);
167 SetAcceleratorTable(accel
);
170 void MyFrame::OnClose(wxCloseEvent
& event
)
172 if ( event
.CanVeto() && (gs_nFrames
> 0) )
175 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
176 if ( wxMessageBox(msg
, "Please confirm",
177 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
188 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
193 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
195 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
196 "Author: Julian Smart (c) 1997\n"
197 "Usage: mdi.exe", "About MDI Demo");
200 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
202 // Make another frame, containing a canvas
203 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame",
204 wxPoint(-1, -1), wxSize(-1, -1),
205 wxDEFAULT_FRAME_STYLE
);
208 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
210 subframe
->SetTitle(title
);
214 subframe
->SetIcon(wxIcon("chrt_icn"));
216 subframe
->SetIcon(wxIcon( mondrian_xpm
));
220 wxMenu
*file_menu
= new wxMenu
;
222 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
223 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child", "Close this window");
224 file_menu
->Append(MDI_QUIT
, "&Exit");
226 wxMenu
*option_menu
= new wxMenu
;
229 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
231 wxMenu
*help_menu
= new wxMenu
;
232 help_menu
->Append(MDI_ABOUT
, "&About");
234 wxMenuBar
*menu_bar
= new wxMenuBar
;
236 menu_bar
->Append(file_menu
, "&File");
237 menu_bar
->Append(option_menu
, "&Options");
238 menu_bar
->Append(help_menu
, "&Help");
240 // Associate the menu bar with the frame
241 subframe
->SetMenuBar(menu_bar
);
244 subframe
->GetClientSize(&width
, &height
);
245 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
246 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
247 subframe
->canvas
= canvas
;
249 // Give it scrollbars
250 canvas
->SetScrollbars(20, 20, 50, 50);
252 subframe
->CreateStatusBar();
253 subframe
->SetStatusText(title
);
255 subframe
->Show(TRUE
);
258 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
261 GetClientSize(&w
, &h
);
263 textWindow
->SetSize(0, 0, 200, h
);
264 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
267 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
269 wxBitmap
* bitmaps
[8];
272 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
273 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
274 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
275 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
276 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
277 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
278 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
279 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
281 bitmaps
[0] = new wxBitmap( new_xpm
);
282 bitmaps
[1] = new wxBitmap( open_xpm
);
283 bitmaps
[2] = new wxBitmap( save_xpm
);
284 bitmaps
[3] = new wxBitmap( copy_xpm
);
285 bitmaps
[4] = new wxBitmap( cut_xpm
);
286 bitmaps
[5] = new wxBitmap( paste_xpm
);
287 bitmaps
[6] = new wxBitmap( print_xpm
);
288 bitmaps
[7] = new wxBitmap( help_xpm
);
298 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
299 currentX
+= width
+ 5;
300 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
301 currentX
+= width
+ 5;
302 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Save file");
303 currentX
+= width
+ 5;
304 toolBar
->AddSeparator();
305 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Copy");
306 currentX
+= width
+ 5;
307 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Cut");
308 currentX
+= width
+ 5;
309 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
310 currentX
+= width
+ 5;
311 toolBar
->AddSeparator();
312 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Print");
313 currentX
+= width
+ 5;
314 toolBar
->AddSeparator();
315 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help");
320 for (i
= 0; i
< 8; i
++)
324 // ---------------------------------------------------------------------------
326 // ---------------------------------------------------------------------------
328 // Define a constructor for my canvas
329 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
330 : wxScrolledWindow(parent
, -1, pos
, size
,
331 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
333 SetBackgroundColour(wxColour("WHITE"));
338 // Define the repainting behaviour
339 void MyCanvas::OnDraw(wxDC
& dc
)
341 dc
.SetFont(*wxSWISS_FONT
);
342 dc
.SetPen(*wxGREEN_PEN
);
343 dc
.DrawLine(0, 0, 200, 200);
344 dc
.DrawLine(200, 0, 0, 200);
346 dc
.SetBrush(*wxCYAN_BRUSH
);
347 dc
.SetPen(*wxRED_PEN
);
348 dc
.DrawRectangle(100, 100, 100, 50);
349 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
351 dc
.DrawEllipse(250, 250, 100, 50);
352 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
353 dc
.DrawLine(50, 230, 200, 230);
354 dc
.DrawText("This is a test string", 50, 230);
357 points
[0].x
= 200; points
[0].y
= 300;
358 points
[1].x
= 100; points
[1].y
= 400;
359 points
[2].x
= 300; points
[2].y
= 400;
361 dc
.DrawPolygon(3, points
);
364 // This implements a tiny doodling program! Drag the mouse using the left
366 void MyCanvas::OnEvent(wxMouseEvent
& event
)
371 wxPoint
pt(event
.GetLogicalPosition(dc
));
373 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
375 dc
.SetPen(*wxBLACK_PEN
);
376 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
385 // ---------------------------------------------------------------------------
387 // ---------------------------------------------------------------------------
389 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
390 const wxPoint
& pos
, const wxSize
& size
,
392 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
394 canvas
= (MyCanvas
*) NULL
;
395 my_children
.Append(this);
400 my_children
.DeleteObject(this);
403 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
408 void MyChild::OnRefresh(wxCommandEvent
& event
)
413 void MyChild::OnActivate(wxActivateEvent
& event
)
415 if ( event
.GetActive() && canvas
)
419 void MyChild::OnClose(wxCloseEvent
& event
)
421 if ( canvas
&& canvas
->IsDirty() )
423 if ( wxMessageBox("Really close?", "Please confirm",
424 wxICON_QUESTION
| wxYES_NO
) != wxYES
)