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