Added bitmaps and icons to samples
[wxWidgets.git] / samples / mdi / mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: mdi.cpp
3 // Purpose: MDI sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/mdi.h"
22 #endif
23
24 #include <wx/toolbar.h>
25
26 #ifdef __WXGTK__
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/preview.xpm"
36 #include "bitmaps/help.xpm"
37 #endif
38
39
40 #include "mdi.h"
41
42 MyFrame *frame = NULL;
43 wxList my_children;
44
45 IMPLEMENT_APP(MyApp)
46
47 // For drawing lines in a canvas
48 long xpos = -1;
49 long ypos = -1;
50
51 int winNumber = 1;
52
53 // Initialise this in OnInit, not statically
54 bool MyApp::OnInit(void)
55 {
56 // Create the main frame window
57
58 frame = new MyFrame(NULL, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
59 wxDEFAULT_FRAME | wxHSCROLL | wxVSCROLL);
60
61 // Give it an icon (this is ignored in MDI mode: uses resources)
62 #ifdef __WXMSW__
63 frame->SetIcon(wxIcon("mdi_icn"));
64 #endif
65
66 // Make a menubar
67 wxMenu *file_menu = new wxMenu;
68
69 file_menu->Append(MDI_NEW_WINDOW, "&New window");
70 file_menu->Append(MDI_QUIT, "&Exit");
71
72 wxMenu *help_menu = new wxMenu;
73 help_menu->Append(MDI_ABOUT, "&About");
74
75 wxMenuBar *menu_bar = new wxMenuBar;
76
77 menu_bar->Append(file_menu, "&File");
78 menu_bar->Append(help_menu, "&Help");
79
80 // Associate the menu bar with the frame
81 frame->SetMenuBar(menu_bar);
82
83 frame->CreateStatusBar();
84
85 frame->Show(TRUE);
86
87 SetTopWindow(frame);
88
89 return TRUE;
90 }
91
92 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
93 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
94 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
95 EVT_SIZE(MyFrame::OnSize)
96 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
97 END_EVENT_TABLE()
98
99 // Define my frame constructor
100 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
101 const long style):
102 wxMDIParentFrame(parent, id, title, pos, size, style)
103 {
104 textWindow = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize,
105 wxTE_MULTILINE|wxSUNKEN_BORDER);
106 textWindow->SetValue("A help window");
107
108 CreateToolBar(wxNO_BORDER|wxTB_FLAT|wxTB_HORIZONTAL);
109 InitToolBar(GetToolBar());
110
111 #ifdef __WXMSW__
112 // Accelerators
113 wxAcceleratorEntry entries[3];
114 entries[0].Set(wxACCEL_CTRL, (int) 'N', MDI_NEW_WINDOW);
115 entries[1].Set(wxACCEL_CTRL, (int) 'X', MDI_QUIT);
116 entries[2].Set(wxACCEL_CTRL, (int) 'A', MDI_ABOUT);
117 wxAcceleratorTable accel(3, entries);
118 SetAcceleratorTable(accel);
119 #endif
120 }
121
122 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
123 {
124 Close(TRUE);
125 }
126
127 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
128 {
129 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
130 }
131
132 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
133 {
134 // Make another frame, containing a canvas
135 MyChild *subframe = new MyChild(frame, "Canvas Frame", wxPoint(-1, -1), wxSize(-1, -1),
136 wxDEFAULT_FRAME);
137
138 wxString title;
139 title.Printf("Canvas Frame %d", winNumber);
140 subframe->SetTitle(title);
141 winNumber ++;
142
143 // Give it an icon (this is ignored in MDI mode: uses resources)
144 #ifdef __WXMSW__
145 subframe->SetIcon(wxIcon("chrt_icn"));
146 #else
147 subframe->SetIcon(wxIcon(mondrian_xpm));
148 #endif
149
150 // Give it a status line
151 subframe->CreateStatusBar();
152
153 // Make a menubar
154 wxMenu *file_menu = new wxMenu;
155
156 file_menu->Append(MDI_NEW_WINDOW, "&New window");
157 file_menu->Append(MDI_CHILD_QUIT, "&Close child");
158 file_menu->Append(MDI_QUIT, "&Exit");
159
160 wxMenu *option_menu = new wxMenu;
161
162 // Dummy option
163 option_menu->Append(MDI_REFRESH, "&Refresh picture");
164
165 wxMenu *help_menu = new wxMenu;
166 help_menu->Append(MDI_ABOUT, "&About");
167
168 wxMenuBar *menu_bar = new wxMenuBar;
169
170 menu_bar->Append(file_menu, "&File");
171 menu_bar->Append(option_menu, "&Options");
172 menu_bar->Append(help_menu, "&Help");
173
174 // Associate the menu bar with the frame
175 subframe->SetMenuBar(menu_bar);
176
177 int width, height;
178 subframe->GetClientSize(&width, &height);
179 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
180 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
181 subframe->canvas = canvas;
182
183 // Give it scrollbars
184 canvas->SetScrollbars(20, 20, 50, 50);
185
186 subframe->Show(TRUE);
187 }
188
189 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
190 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
191 END_EVENT_TABLE()
192
193 // Define a constructor for my canvas
194 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
195 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
196 {
197 }
198
199 // Define the repainting behaviour
200 void MyCanvas::OnDraw(wxDC& dc)
201 {
202 dc.SetFont(*wxSWISS_FONT);
203 dc.SetPen(*wxGREEN_PEN);
204 dc.DrawLine(0, 0, 200, 200);
205 dc.DrawLine(200, 0, 0, 200);
206
207 dc.SetBrush(*wxCYAN_BRUSH);
208 dc.SetPen(*wxRED_PEN);
209 dc.DrawRectangle(100, 100, 100, 50);
210 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
211
212 dc.DrawEllipse(250, 250, 100, 50);
213 dc.DrawSpline(50, 200, 50, 100, 200, 10);
214 dc.DrawLine(50, 230, 200, 230);
215 dc.DrawText("This is a test string", 50, 230);
216
217 wxPoint points[3];
218 points[0].x = 200; points[0].y = 300;
219 points[1].x = 100; points[1].y = 400;
220 points[2].x = 300; points[2].y = 400;
221
222 dc.DrawPolygon(3, points);
223 }
224
225 // This implements a tiny doodling program! Drag the mouse using
226 // the left button.
227 void MyCanvas::OnEvent(wxMouseEvent& event)
228 {
229 wxClientDC dc(this);
230 PrepareDC(dc);
231
232 wxPoint pt(event.GetLogicalPosition(dc));
233
234 if (xpos > -1 && ypos > -1 && event.Dragging())
235 {
236 dc.SetPen(*wxBLACK_PEN);
237 dc.DrawLine(xpos, ypos, pt.x, pt.y);
238 }
239 xpos = pt.x;
240 ypos = pt.y;
241 }
242
243 // Define the behaviour for the frame closing
244 // - must delete all frames except for the main one.
245 bool MyFrame::OnClose(void)
246 {
247 // Must delete children
248 wxNode *node = my_children.First();
249 while (node)
250 {
251 MyChild *child = (MyChild *)node->Data();
252 wxNode *next = node->Next();
253 child->OnClose();
254 delete child;
255 node = next;
256 }
257 return TRUE;
258 }
259
260 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
261 {
262 int w, h;
263 GetClientSize(&w, &h);
264
265 textWindow->SetSize(0, 0, 200, h);
266 GetClientWindow()->SetSize(200, 0, w - 200, h);
267 }
268
269 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
270 // to the parent window for processing, so no need to
271 // duplicate event handlers here.
272
273 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
274 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
275 END_EVENT_TABLE()
276
277 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
278 const long style):
279 wxMDIChildFrame(parent, -1, title, pos, size, style)
280 {
281 canvas = NULL;
282 my_children.Append(this);
283 }
284
285 MyChild::~MyChild(void)
286 {
287 my_children.DeleteObject(this);
288 }
289
290 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
291 {
292 Close(TRUE);
293 }
294
295 void MyChild::OnActivate(wxActivateEvent& event)
296 {
297 if (event.GetActive() && canvas)
298 canvas->SetFocus();
299 }
300
301 bool MyChild::OnClose(void)
302 {
303 return TRUE;
304 }
305
306 void MyFrame::InitToolBar(wxToolBar* toolBar)
307 {
308 wxBitmap* bitmaps[8];
309
310 #ifdef __WXMSW__
311 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
312 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
313 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
314 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
315 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
316 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
317 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
318 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
319 #else
320 bitmaps[0] = new wxBitmap( new_xpm );
321 bitmaps[1] = new wxBitmap( open_xpm );
322 bitmaps[2] = new wxBitmap( save_xpm );
323 bitmaps[3] = new wxBitmap( copy_xpm );
324 bitmaps[4] = new wxBitmap( cut_xpm );
325 // bitmaps[5] = new wxBitmap( paste_xpm );
326 bitmaps[5] = new wxBitmap( preview_xpm );
327 bitmaps[6] = new wxBitmap( print_xpm );
328 bitmaps[7] = new wxBitmap( help_xpm );
329 #endif
330
331 #ifdef __WXMSW__
332 int width = 24;
333 #else
334 int width = 16;
335 #endif
336 int currentX = 5;
337
338 toolBar->AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
339 currentX += width + 5;
340 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
341 currentX += width + 5;
342 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
343 currentX += width + 5;
344 toolBar->AddSeparator();
345 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
346 currentX += width + 5;
347 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
348 currentX += width + 5;
349 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
350 currentX += width + 5;
351 toolBar->AddSeparator();
352 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
353 currentX += width + 5;
354 toolBar->AddSeparator();
355 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
356
357 toolBar->Realize();
358
359 int i;
360 for (i = 0; i < 8; i++)
361 delete bitmaps[i];
362 }
363