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>
32 MyFrame
*frame
= NULL
;
37 // For drawing lines in a canvas
43 // Initialise this in OnInit, not statically
44 bool MyApp
::OnInit(void)
46 // Create the main frame window
48 frame
= new MyFrame(NULL
, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
49 wxDEFAULT_FRAME
| wxHSCROLL
| wxVSCROLL
);
51 // Give it an icon (this is ignored in MDI mode: uses resources)
53 frame
->SetIcon(wxIcon("mdi_icn"));
56 frame
->SetIcon(wxIcon("aiai.xbm"));
60 wxMenu
*file_menu
= new wxMenu
;
62 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
63 file_menu
->Append(MDI_QUIT
, "&Exit");
65 wxMenu
*help_menu
= new wxMenu
;
66 help_menu
->Append(MDI_ABOUT
, "&About");
68 wxMenuBar
*menu_bar
= new wxMenuBar
;
70 menu_bar
->Append(file_menu
, "&File");
71 menu_bar
->Append(help_menu
, "&Help");
73 // Associate the menu bar with the frame
74 frame
->SetMenuBar(menu_bar
);
76 frame
->CreateStatusBar();
85 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
86 EVT_MENU(MDI_ABOUT
, MyFrame
::OnAbout
)
87 EVT_MENU(MDI_NEW_WINDOW
, MyFrame
::OnNewWindow
)
88 EVT_SIZE(MyFrame
::OnSize
)
89 EVT_MENU(MDI_QUIT
, MyFrame
::OnQuit
)
92 // Define my frame constructor
93 MyFrame
::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
95 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
97 textWindow
= new wxTextCtrl(this, -1, "", wxDefaultPosition
, wxDefaultSize
,
98 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
99 textWindow
->SetValue("A help window");
101 CreateToolBar(wxNO_BORDER
|wxTB_FLAT
|wxTB_HORIZONTAL
);
102 InitToolBar(GetToolBar());
105 void MyFrame
::OnQuit(wxCommandEvent
& event
)
110 void MyFrame
::OnAbout(wxCommandEvent
& event
)
112 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
115 void MyFrame
::OnNewWindow(wxCommandEvent
& event
)
117 // Make another frame, containing a canvas
118 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
122 sprintf(titleBuf
, "Canvas Frame %d", winNumber
);
123 subframe
->SetTitle(titleBuf
);
126 // Give it an icon (this is ignored in MDI mode: uses resources)
128 subframe
->SetIcon(wxIcon("chrt_icn"));
131 subframe
->SetIcon(wxIcon("aiai.xbm"));
134 // Give it a status line
135 subframe
->CreateStatusBar();
138 wxMenu
*file_menu
= new wxMenu
;
140 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
141 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child");
142 file_menu
->Append(MDI_QUIT
, "&Exit");
144 wxMenu
*option_menu
= new wxMenu
;
147 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
149 wxMenu
*help_menu
= new wxMenu
;
150 help_menu
->Append(MDI_ABOUT
, "&About");
152 wxMenuBar
*menu_bar
= new wxMenuBar
;
154 menu_bar
->Append(file_menu
, "&File");
155 menu_bar
->Append(option_menu
, "&Options");
156 menu_bar
->Append(help_menu
, "&Help");
158 // Associate the menu bar with the frame
159 subframe
->SetMenuBar(menu_bar
);
162 subframe
->GetClientSize(&width
, &height
);
163 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
164 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
165 subframe
->canvas
= canvas
;
167 // Give it scrollbars
168 canvas
->SetScrollbars(20, 20, 50, 50);
170 subframe
->Show(TRUE
);
173 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
174 EVT_MOUSE_EVENTS(MyCanvas
::OnEvent
)
177 // Define a constructor for my canvas
178 MyCanvas
::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
179 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
183 // Define the repainting behaviour
184 void MyCanvas
::OnDraw(wxDC
& dc
)
186 dc
.SetFont(*wxSWISS_FONT
);
187 dc
.SetPen(*wxGREEN_PEN
);
188 dc
.DrawLine(0, 0, 200, 200);
189 dc
.DrawLine(200, 0, 0, 200);
191 dc
.SetBrush(*wxCYAN_BRUSH
);
192 dc
.SetPen(*wxRED_PEN
);
193 dc
.DrawRectangle(100, 100, 100, 50);
194 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
196 dc
.DrawEllipse(250, 250, 100, 50);
197 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
198 dc
.DrawLine(50, 230, 200, 230);
199 dc
.DrawText("This is a test string", 50, 230);
202 points
[0].x
= 200; points
[0].y
= 300;
203 points
[1].x
= 100; points
[1].y
= 400;
204 points
[2].x
= 300; points
[2].y
= 400;
206 dc
.DrawPolygon(3, points
);
209 // This implements a tiny doodling program! Drag the mouse using
211 void MyCanvas
::OnEvent(wxMouseEvent
& event
)
216 wxPoint
pt(event
.GetLogicalPosition(dc
));
218 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
220 dc
.SetPen(*wxBLACK_PEN
);
221 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
227 // Define the behaviour for the frame closing
228 // - must delete all frames except for the main one.
229 bool MyFrame
::OnClose(void)
231 // Must delete children
232 wxNode
*node
= my_children
.First();
235 MyChild
*child
= (MyChild
*)node
->Data();
236 wxNode
*next
= node
->Next();
244 void MyFrame
::OnSize(wxSizeEvent
& event
)
247 GetClientSize(&w
, &h
);
251 textWindow
->SetSize(0, 0, 200, h
);
252 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
255 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
256 // to the parent window for processing, so no need to
257 // duplicate event handlers here.
259 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
260 EVT_MENU(MDI_CHILD_QUIT
, MyChild
::OnQuit
)
263 MyChild
::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
265 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
268 my_children
.Append(this);
271 MyChild
::~MyChild(void)
273 my_children
.DeleteObject(this);
276 void MyChild
::OnQuit(wxCommandEvent
& WXUNUSED(event
))
281 void MyChild
::OnActivate(wxActivateEvent
& event
)
283 if (event
.GetActive() && canvas
)
287 bool MyChild
::OnClose(void)
292 void MyFrame
::InitToolBar(wxToolBar
* toolBar
)
294 wxBitmap
* bitmaps
[8];
297 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
298 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
299 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
300 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
301 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
302 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
303 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
304 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
306 bitmaps
[0] = new wxBitmap( folder_xpm
);
307 bitmaps
[1] = new wxBitmap( folder_xpm
);
308 bitmaps
[2] = new wxBitmap( folder_xpm
);
309 bitmaps
[3] = new wxBitmap( folder_xpm
);
310 bitmaps
[4] = new wxBitmap( folder_xpm
);
311 bitmaps
[5] = new wxBitmap( folder_xpm
);
312 bitmaps
[6] = new wxBitmap( folder_xpm
);
313 bitmaps
[7] = new wxBitmap( folder_xpm
);
324 toolBar
->AddTool(0, *bitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "New file");
325 currentX
+= width
+ 5;
326 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Open file");
327 currentX
+= width
+ 5;
328 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Save file");
329 currentX
+= width
+ 5;
330 toolBar
->AddSeparator();
331 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Copy");
332 currentX
+= width
+ 5;
333 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Cut");
334 currentX
+= width
+ 5;
335 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Paste");
336 currentX
+= width
+ 5;
337 toolBar
->AddSeparator();
338 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Print");
339 currentX
+= width
+ 5;
340 toolBar
->AddSeparator();
341 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, NULL
, "Help");
346 for (i
= 0; i
< 8; i
++)