Removed redundant auto-setting code in wxDC; corrected utilscmn.cpp pragma;
[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 #ifdef __WINDOWS__
25 #ifdef __WIN95__
26 #include <wx/tbar95.h>
27 #else
28 #include <wx/tbarmsw.h>
29 #endif
30 #endif
31
32 #include "mdi.h"
33
34 MyFrame *frame = NULL;
35 wxList my_children;
36
37 IMPLEMENT_APP(MyApp)
38
39 // For drawing lines in a canvas
40 long xpos = -1;
41 long ypos = -1;
42
43 int winNumber = 1;
44
45 // Initialise this in OnInit, not statically
46 bool MyApp::OnInit(void)
47 {
48 // Create the main frame window
49
50 frame = new MyFrame(NULL, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
51 wxDEFAULT_FRAME | wxHSCROLL | wxVSCROLL);
52
53 // Give it an icon (this is ignored in MDI mode: uses resources)
54 #ifdef __WINDOWS__
55 frame->SetIcon(wxIcon("mdi_icn"));
56 #endif
57 #ifdef __X__
58 frame->SetIcon(wxIcon("aiai.xbm"));
59 #endif
60
61 // Make a menubar
62 wxMenu *file_menu = new wxMenu;
63
64 file_menu->Append(MDI_NEW_WINDOW, "&New window");
65 file_menu->Append(MDI_QUIT, "&Exit");
66
67 wxMenu *help_menu = new wxMenu;
68 help_menu->Append(MDI_ABOUT, "&About");
69
70 wxMenuBar *menu_bar = new wxMenuBar;
71
72 menu_bar->Append(file_menu, "&File");
73 menu_bar->Append(help_menu, "&Help");
74
75 // Associate the menu bar with the frame
76 frame->SetMenuBar(menu_bar);
77
78 frame->CreateStatusBar();
79
80 frame->Show(TRUE);
81
82 SetTopWindow(frame);
83
84 return TRUE;
85 }
86
87 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
88 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
89 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
90 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
91 EVT_SIZE(MyFrame::OnSize)
92 END_EVENT_TABLE()
93
94 // Define my frame constructor
95 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
96 const long style):
97 wxMDIParentFrame(parent, id, title, pos, size, style)
98 {
99 textWindow = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize,
100 wxTE_MULTILINE|wxSUNKEN_BORDER);
101 textWindow->SetValue("A help window");
102
103 #ifdef __WINDOWS__
104 toolBar = new TestRibbon(this, 0, 0, 100, 30, wxNO_BORDER, wxVERTICAL, 1);
105 SetToolBar(toolBar);
106 #endif
107 }
108
109 void MyFrame::OnQuit(wxCommandEvent& event)
110 {
111 Close(TRUE);
112 }
113
114 void MyFrame::OnAbout(wxCommandEvent& event)
115 {
116 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
117 }
118
119 void MyFrame::OnNewWindow(wxCommandEvent& event)
120 {
121 // Make another frame, containing a canvas
122 MyChild *subframe = new MyChild(frame, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
123 wxDEFAULT_FRAME);
124
125 char titleBuf[100];
126 sprintf(titleBuf, "Canvas Frame %d", winNumber);
127 subframe->SetTitle(titleBuf);
128 winNumber ++;
129
130 // Give it an icon (this is ignored in MDI mode: uses resources)
131 #ifdef __WINDOWS__
132 subframe->SetIcon(wxIcon("chrt_icn"));
133 #endif
134 #ifdef __X__
135 subframe->SetIcon(wxIcon("aiai.xbm"));
136 #endif
137
138 // Give it a status line
139 subframe->CreateStatusBar();
140
141 // Make a menubar
142 wxMenu *file_menu = new wxMenu;
143
144 file_menu->Append(MDI_NEW_WINDOW, "&New window");
145 file_menu->Append(MDI_CHILD_QUIT, "&Close child");
146 file_menu->Append(MDI_QUIT, "&Exit");
147
148 wxMenu *option_menu = new wxMenu;
149
150 // Dummy option
151 option_menu->Append(MDI_REFRESH, "&Refresh picture");
152
153 wxMenu *help_menu = new wxMenu;
154 help_menu->Append(MDI_ABOUT, "&About");
155
156 wxMenuBar *menu_bar = new wxMenuBar;
157
158 menu_bar->Append(file_menu, "&File");
159 menu_bar->Append(option_menu, "&Options");
160 menu_bar->Append(help_menu, "&Help");
161
162 // Associate the menu bar with the frame
163 subframe->SetMenuBar(menu_bar);
164
165 int width, height;
166 subframe->GetClientSize(&width, &height);
167 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
168 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
169 subframe->canvas = canvas;
170
171 // Give it scrollbars
172 canvas->SetScrollbars(20, 20, 50, 50);
173
174 subframe->Show(TRUE);
175 }
176
177 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
178 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
179 END_EVENT_TABLE()
180
181 // Define a constructor for my canvas
182 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
183 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER)
184 {
185 }
186
187 // Define the repainting behaviour
188 void MyCanvas::OnDraw(wxDC& dc)
189 {
190 dc.SetFont(*wxSWISS_FONT);
191 dc.SetPen(*wxGREEN_PEN);
192 dc.DrawLine(0, 0, 200, 200);
193 dc.DrawLine(200, 0, 0, 200);
194
195 dc.SetBrush(*wxCYAN_BRUSH);
196 dc.SetPen(*wxRED_PEN);
197 dc.DrawRectangle(100, 100, 100, 50);
198 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
199
200 dc.DrawEllipse(250, 250, 100, 50);
201 dc.DrawSpline(50, 200, 50, 100, 200, 10);
202 dc.DrawLine(50, 230, 200, 230);
203 dc.DrawText("This is a test string", 50, 230);
204
205 wxPoint points[3];
206 points[0].x = 200; points[0].y = 300;
207 points[1].x = 100; points[1].y = 400;
208 points[2].x = 300; points[2].y = 400;
209
210 dc.DrawPolygon(3, points);
211 }
212
213 // This implements a tiny doodling program! Drag the mouse using
214 // the left button.
215 void MyCanvas::OnEvent(wxMouseEvent& event)
216 {
217 wxClientDC dc(this);
218 PrepareDC(dc);
219
220 wxPoint pt(event.GetLogicalPosition(dc));
221
222 if (xpos > -1 && ypos > -1 && event.Dragging())
223 {
224 dc.SetPen(*wxBLACK_PEN);
225 dc.DrawLine(xpos, ypos, pt.x, pt.y);
226 }
227 xpos = pt.x;
228 ypos = pt.y;
229 }
230
231 // Define the behaviour for the frame closing
232 // - must delete all frames except for the main one.
233 bool MyFrame::OnClose(void)
234 {
235 // Must delete children
236 wxNode *node = my_children.First();
237 while (node)
238 {
239 MyChild *child = (MyChild *)node->Data();
240 wxNode *next = node->Next();
241 child->OnClose();
242 delete child;
243 node = next;
244 }
245 return TRUE;
246 }
247
248 void MyFrame::OnSize(wxSizeEvent& event)
249 {
250 int w, h;
251 GetClientSize(&w, &h);
252 int tw = 0;
253 int th = 0;
254
255 #ifdef __WINDOWS__
256 wxWindow* tbar = GetToolBar();
257 if (tbar)
258 {
259 tbar->GetSize(&tw, &th);
260 tbar->SetSize(w, th);
261 }
262 #endif
263
264 textWindow->SetSize(0, th, 200, h-th);
265 GetClientWindow()->SetSize(200, th, w - 200, h-th);
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
272 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
273 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
274 END_EVENT_TABLE()
275
276 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
277 const long style):
278 wxMDIChildFrame(parent, -1, title, pos, size, style)
279 {
280 canvas = NULL;
281 my_children.Append(this);
282 }
283
284 MyChild::~MyChild(void)
285 {
286 my_children.DeleteObject(this);
287 }
288
289 void MyChild::OnQuit(wxCommandEvent& event)
290 {
291 Close(TRUE);
292 }
293
294 void MyChild::OnActivate(wxActivateEvent& event)
295 {
296 if (event.GetActive() && canvas)
297 canvas->SetFocus();
298 }
299
300 bool MyChild::OnClose(void)
301 {
302 return TRUE;
303 }
304
305 #ifdef __WINDOWS__
306
307 BEGIN_EVENT_TABLE(TestRibbon, wxToolBar)
308 EVT_PAINT(TestRibbon::OnPaint)
309 END_EVENT_TABLE()
310
311 TestRibbon::TestRibbon(wxFrame *frame, int x, int y, int w, int h,
312 long style, int direction, int RowsOrColumns):
313 wxToolBar(frame, -1, wxPoint(x, y), wxSize(w, h), style, direction, RowsOrColumns)
314 {
315 wxBitmap* bitmaps[8];
316
317 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
318 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
319 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
320 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
321 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
322 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
323 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
324 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
325
326 #ifdef __WINDOWS__
327 int width = 24;
328 #else
329 int width = 16;
330 #endif
331 int offX = 5;
332 int currentX = 5;
333
334 AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
335 currentX += width + 5;
336 AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
337 currentX += width + 5;
338 AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
339 currentX += width + 5;
340 AddSeparator();
341 AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
342 currentX += width + 5;
343 AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
344 currentX += width + 5;
345 AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
346 currentX += width + 5;
347 AddSeparator();
348 AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
349 currentX += width + 5;
350 AddSeparator();
351 AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
352
353 CreateTools();
354
355 int i;
356 for (i = 0; i < 8; i++)
357 delete bitmaps[i];
358 }
359
360 bool TestRibbon::OnLeftClick(int toolIndex, bool toggled)
361 {
362 char buf[200];
363 sprintf(buf, "Clicked on tool %d", toolIndex);
364 frame->SetStatusText(buf);
365 return TRUE;
366 }
367
368 void TestRibbon::OnMouseEnter(int toolIndex)
369 {
370 char buf[200];
371 if (toolIndex > -1)
372 {
373 sprintf(buf, "This is tool number %d", toolIndex);
374 frame->SetStatusText(buf);
375 }
376 else frame->SetStatusText("");
377 }
378
379 void TestRibbon::OnPaint(wxPaintEvent& event)
380 {
381 wxToolBar::OnPaint(event);
382
383 wxPaintDC dc(this);
384
385 int w, h;
386 GetSize(&w, &h);
387 dc.SetPen(*wxBLACK_PEN);
388 dc.SetBrush(*wxTRANSPARENT_BRUSH);
389 dc.DrawLine(0, h-1, w, h-1);
390 }
391
392 #endif