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