1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include <wx/toolbar.h>
27 #include "mondrian.xpm"
28 #include "bitmaps/new.xpm"
29 #include "bitmaps/open.xpm"
30 #include "bitmaps/save.xpm"
31 #include "bitmaps/copy.xpm"
32 #include "bitmaps/cut.xpm"
33 #include "bitmaps/paste.xpm"
34 #include "bitmaps/print.xpm"
35 #include "bitmaps/help.xpm"
41 MyFrame
*frame
= NULL
;
46 // For drawing lines in a canvas
52 // Initialise this in OnInit, not statically
53 bool MyApp::OnInit(void)
55 // Create the main frame window
57 frame
= new MyFrame(NULL
, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
58 wxDEFAULT_FRAME
| wxHSCROLL
| wxVSCROLL
);
60 // Give it an icon (this is ignored in MDI mode: uses resources)
62 frame
->SetIcon(wxIcon("mdi_icn"));
64 frame
->SetIcon(wxIcon( mondrian_xpm
));
68 wxMenu
*file_menu
= new wxMenu
;
70 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
71 file_menu
->Append(MDI_QUIT
, "&Exit");
73 wxMenu
*help_menu
= new wxMenu
;
74 help_menu
->Append(MDI_ABOUT
, "&About");
76 wxMenuBar
*menu_bar
= new wxMenuBar
;
78 menu_bar
->Append(file_menu
, "&File");
79 menu_bar
->Append(help_menu
, "&Help");
81 // Associate the menu bar with the frame
82 frame
->SetMenuBar(menu_bar
);
84 frame
->CreateStatusBar();
93 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
94 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
95 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
96 EVT_SIZE(MyFrame::OnSize
)
97 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
100 // Define my frame constructor
101 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
103 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
105 textWindow
= new wxTextCtrl(this, -1, "", wxDefaultPosition
, wxDefaultSize
,
106 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
107 textWindow
->SetValue("A help window");
109 CreateToolBar(wxNO_BORDER
|wxTB_FLAT
|wxTB_HORIZONTAL
);
110 InitToolBar(GetToolBar());
114 wxAcceleratorEntry entries
[3];
115 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
116 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
117 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
118 wxAcceleratorTable
accel(3, entries
);
119 SetAcceleratorTable(accel
);
123 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
) )
128 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
130 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
133 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
135 // Make another frame, containing a canvas
136 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame", wxPoint(-1, -1), wxSize(-1, -1),
140 title
.Printf("Canvas Frame %d", winNumber
);
141 subframe
->SetTitle(title
);
146 subframe
->SetIcon(wxIcon("chrt_icn"));
150 wxMenu
*file_menu
= new wxMenu
;
152 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
153 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child");
154 file_menu
->Append(MDI_QUIT
, "&Exit");
156 wxMenu
*option_menu
= new wxMenu
;
159 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
161 wxMenu
*help_menu
= new wxMenu
;
162 help_menu
->Append(MDI_ABOUT
, "&About");
164 wxMenuBar
*menu_bar
= new wxMenuBar
;
166 menu_bar
->Append(file_menu
, "&File");
167 menu_bar
->Append(option_menu
, "&Options");
168 menu_bar
->Append(help_menu
, "&Help");
170 // Associate the menu bar with the frame
171 subframe
->SetMenuBar(menu_bar
);
174 subframe
->GetClientSize(&width
, &height
);
175 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
176 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
177 subframe
->canvas
= canvas
;
179 // Give it scrollbars
180 canvas
->SetScrollbars(20, 20, 50, 50);
182 subframe
->Show(TRUE
);
185 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
186 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
189 // Define a constructor for my canvas
190 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
191 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
195 // Define the repainting behaviour
196 void MyCanvas::OnDraw(wxDC
& dc
)
198 dc
.SetFont(*wxSWISS_FONT
);
199 dc
.SetPen(*wxGREEN_PEN
);
200 dc
.DrawLine(0, 0, 200, 200);
201 dc
.DrawLine(200, 0, 0, 200);
203 dc
.SetBrush(*wxCYAN_BRUSH
);
204 dc
.SetPen(*wxRED_PEN
);
205 dc
.DrawRectangle(100, 100, 100, 50);
206 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
208 dc
.DrawEllipse(250, 250, 100, 50);
209 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
210 dc
.DrawLine(50, 230, 200, 230);
211 dc
.DrawText("This is a test string", 50, 230);
214 points
[0].x
= 200; points
[0].y
= 300;
215 points
[1].x
= 100; points
[1].y
= 400;
216 points
[2].x
= 300; points
[2].y
= 400;
218 dc
.DrawPolygon(3, points
);
221 // This implements a tiny doodling program! Drag the mouse using
223 void MyCanvas::OnEvent(wxMouseEvent
& event
)
228 wxPoint
pt(event
.GetLogicalPosition(dc
));
230 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
232 dc
.SetPen(*wxBLACK_PEN
);
233 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
239 // Define the behaviour for the frame closing
240 // - must delete all frames except for the main one.
241 bool MyFrame::OnClose(void)
243 // Must delete children
244 wxNode
*node
= my_children
.First();
247 MyChild
*child
= (MyChild
*)node
->Data();
248 wxNode
*next
= node
->Next();
256 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
) )
259 GetClientSize(&w
, &h
);
261 textWindow
->SetSize(0, 0, 200, h
);
262 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
265 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
266 // to the parent window for processing, so no need to
267 // duplicate event handlers here.
269 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
270 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
273 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
275 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
278 my_children
.Append(this);
281 MyChild::~MyChild(void)
283 my_children
.DeleteObject(this);
286 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
291 void MyChild::OnActivate(wxActivateEvent
& event
)
293 if (event
.GetActive() && canvas
)
297 bool MyChild::OnClose(void)
302 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
304 wxBitmap
* bitmaps
[8];
307 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
308 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
309 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
310 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
311 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
312 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
313 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
314 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
316 bitmaps
[0] = new wxBitmap( new_xpm
);
317 bitmaps
[1] = new wxBitmap( open_xpm
);
318 bitmaps
[2] = new wxBitmap( save_xpm
);
319 bitmaps
[3] = new wxBitmap( copy_xpm
);
320 bitmaps
[4] = new wxBitmap( cut_xpm
);
321 bitmaps
[5] = new wxBitmap( paste_xpm
);
322 bitmaps
[6] = new wxBitmap( print_xpm
);
323 bitmaps
[7] = new wxBitmap( help_xpm
);
333 toolBar
->AddTool(0, *(bitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "New file");
334 currentX
+= width
+ 5;
335 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Open file");
336 currentX
+= width
+ 5;
337 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Save file");
338 currentX
+= width
+ 5;
339 toolBar
->AddSeparator();
340 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Copy");
341 currentX
+= width
+ 5;
342 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Cut");
343 currentX
+= width
+ 5;
344 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Paste");
345 currentX
+= width
+ 5;
346 toolBar
->AddSeparator();
347 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Print");
348 currentX
+= width
+ 5;
349 toolBar
->AddSeparator();
350 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, NULL
, "Help");
355 for (i
= 0; i
< 8; i
++)