]> git.saurik.com Git - wxWidgets.git/blame - samples/mdi/mdi.cpp
Added wxString version of wxStripExtension; changed OnClose to return TRUE;
[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
2049ba38 24#ifdef __WXMSW__
6b0eb19f 25#ifdef __WIN95__
c801d85f 26#include <wx/tbar95.h>
6b0eb19f
JS
27#else
28#include <wx/tbarmsw.h>
29#endif
c801d85f
KB
30#endif
31
32#include "mdi.h"
33
34MyFrame *frame = NULL;
35wxList my_children;
36
37IMPLEMENT_APP(MyApp)
38
39// For drawing lines in a canvas
40long xpos = -1;
41long ypos = -1;
42
43int winNumber = 1;
44
45// Initialise this in OnInit, not statically
46bool 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)
2049ba38 54#ifdef __WXMSW__
c801d85f
KB
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
87BEGIN_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)
92END_EVENT_TABLE()
93
94// Define my frame constructor
95MyFrame::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
2049ba38 103#ifdef __WXMSW__
c801d85f
KB
104 toolBar = new TestRibbon(this, 0, 0, 100, 30, wxNO_BORDER, wxVERTICAL, 1);
105 SetToolBar(toolBar);
106#endif
107}
108
109void MyFrame::OnQuit(wxCommandEvent& event)
110{
111 Close(TRUE);
112}
113
114void 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
119void 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)
2049ba38 131#ifdef __WXMSW__
c801d85f
KB
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
177BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
178 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
179END_EVENT_TABLE()
180
181// Define a constructor for my canvas
182MyCanvas::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
188void 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);
6a6c0a8b
JS
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;
33d0b396 209
6a6c0a8b 210 dc.DrawPolygon(3, points);
c801d85f
KB
211}
212
213// This implements a tiny doodling program! Drag the mouse using
214// the left button.
215void 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.
233bool 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
248void MyFrame::OnSize(wxSizeEvent& event)
249{
250 int w, h;
251 GetClientSize(&w, &h);
252 int tw = 0;
253 int th = 0;
254
2049ba38 255#ifdef __WXMSW__
c801d85f
KB
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
272BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
273 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
33d0b396 274 EVT_SIZE( MyChild::OnSize)
c801d85f
KB
275END_EVENT_TABLE()
276
277MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
278const long style):
279 wxMDIChildFrame(parent, -1, title, pos, size, style)
280{
281 canvas = NULL;
282 my_children.Append(this);
283}
284
285MyChild::~MyChild(void)
286{
287 my_children.DeleteObject(this);
288}
289
33d0b396
RR
290void MyChild::OnSize(wxSizeEvent& WXUNUSED(event))
291{
292 int x = 0;
293 int y = 0;
294 GetClientSize( &x, &y );
295 if (canvas) canvas->SetSize( x, y );
296}
297
298void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
c801d85f
KB
299{
300 Close(TRUE);
301}
302
303void MyChild::OnActivate(wxActivateEvent& event)
304{
305 if (event.GetActive() && canvas)
306 canvas->SetFocus();
307}
308
309bool MyChild::OnClose(void)
310{
311 return TRUE;
312}
313
2049ba38 314#ifdef __WXMSW__
c801d85f 315
6b0eb19f 316BEGIN_EVENT_TABLE(TestRibbon, wxToolBar)
c801d85f
KB
317 EVT_PAINT(TestRibbon::OnPaint)
318END_EVENT_TABLE()
319
320TestRibbon::TestRibbon(wxFrame *frame, int x, int y, int w, int h,
321 long style, int direction, int RowsOrColumns):
6b0eb19f 322 wxToolBar(frame, -1, wxPoint(x, y), wxSize(w, h), style, direction, RowsOrColumns)
c801d85f
KB
323{
324 wxBitmap* bitmaps[8];
325
326 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
327 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
328 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
329 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
330 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
331 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
332 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
333 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
334
2049ba38 335#ifdef __WXMSW__
c801d85f
KB
336 int width = 24;
337#else
338 int width = 16;
339#endif
340 int offX = 5;
341 int currentX = 5;
342
6b0eb19f 343 AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
c801d85f 344 currentX += width + 5;
6b0eb19f 345 AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
c801d85f 346 currentX += width + 5;
6b0eb19f 347 AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
c801d85f
KB
348 currentX += width + 5;
349 AddSeparator();
6b0eb19f 350 AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
c801d85f 351 currentX += width + 5;
6b0eb19f 352 AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
c801d85f 353 currentX += width + 5;
6b0eb19f 354 AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
c801d85f
KB
355 currentX += width + 5;
356 AddSeparator();
6b0eb19f 357 AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
c801d85f
KB
358 currentX += width + 5;
359 AddSeparator();
6b0eb19f 360 AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
c801d85f
KB
361
362 CreateTools();
363
364 int i;
365 for (i = 0; i < 8; i++)
366 delete bitmaps[i];
367}
368
369bool TestRibbon::OnLeftClick(int toolIndex, bool toggled)
370{
371 char buf[200];
372 sprintf(buf, "Clicked on tool %d", toolIndex);
373 frame->SetStatusText(buf);
374 return TRUE;
375}
376
377void TestRibbon::OnMouseEnter(int toolIndex)
378{
379 char buf[200];
380 if (toolIndex > -1)
381 {
382 sprintf(buf, "This is tool number %d", toolIndex);
383 frame->SetStatusText(buf);
384 }
385 else frame->SetStatusText("");
386}
387
388void TestRibbon::OnPaint(wxPaintEvent& event)
389{
6b0eb19f 390 wxToolBar::OnPaint(event);
c801d85f
KB
391
392 wxPaintDC dc(this);
393
394 int w, h;
395 GetSize(&w, &h);
6b0eb19f
JS
396 dc.SetPen(*wxBLACK_PEN);
397 dc.SetBrush(*wxTRANSPARENT_BRUSH);
c801d85f
KB
398 dc.DrawLine(0, h-1, w, h-1);
399}
400
401#endif