Misc changes for DOS compatibility, plus added wxApp::CreateConfig
[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
206 // This implements a tiny doodling program! Drag the mouse using
207 // the left button.
208 void MyCanvas::OnEvent(wxMouseEvent& event)
209 {
210 wxClientDC dc(this);
211 PrepareDC(dc);
212
213 wxPoint pt(event.GetLogicalPosition(dc));
214
215 if (xpos > -1 && ypos > -1 && event.Dragging())
216 {
217 dc.SetPen(*wxBLACK_PEN);
218 dc.DrawLine(xpos, ypos, pt.x, pt.y);
219 }
220 xpos = pt.x;
221 ypos = pt.y;
222 }
223
224 // Define the behaviour for the frame closing
225 // - must delete all frames except for the main one.
226 bool MyFrame::OnClose(void)
227 {
228 // Must delete children
229 wxNode *node = my_children.First();
230 while (node)
231 {
232 MyChild *child = (MyChild *)node->Data();
233 wxNode *next = node->Next();
234 child->OnClose();
235 delete child;
236 node = next;
237 }
238 return TRUE;
239 }
240
241 void MyFrame::OnSize(wxSizeEvent& event)
242 {
243 int w, h;
244 GetClientSize(&w, &h);
245 int tw = 0;
246 int th = 0;
247
248 #ifdef __WINDOWS__
249 wxWindow* tbar = GetToolBar();
250 if (tbar)
251 {
252 tbar->GetSize(&tw, &th);
253 tbar->SetSize(w, th);
254 }
255 #endif
256
257 textWindow->SetSize(0, th, 200, h-th);
258 GetClientWindow()->SetSize(200, th, w - 200, h-th);
259 }
260
261 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
262 // to the parent window for processing, so no need to
263 // duplicate event handlers here.
264
265 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
266 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
267 END_EVENT_TABLE()
268
269 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
270 const long style):
271 wxMDIChildFrame(parent, -1, title, pos, size, style)
272 {
273 canvas = NULL;
274 my_children.Append(this);
275 }
276
277 MyChild::~MyChild(void)
278 {
279 my_children.DeleteObject(this);
280 }
281
282 void MyChild::OnQuit(wxCommandEvent& event)
283 {
284 Close(TRUE);
285 }
286
287 void MyChild::OnActivate(wxActivateEvent& event)
288 {
289 if (event.GetActive() && canvas)
290 canvas->SetFocus();
291 }
292
293 bool MyChild::OnClose(void)
294 {
295 return TRUE;
296 }
297
298 #ifdef __WINDOWS__
299
300 BEGIN_EVENT_TABLE(TestRibbon, wxToolBar)
301 EVT_PAINT(TestRibbon::OnPaint)
302 END_EVENT_TABLE()
303
304 TestRibbon::TestRibbon(wxFrame *frame, int x, int y, int w, int h,
305 long style, int direction, int RowsOrColumns):
306 wxToolBar(frame, -1, wxPoint(x, y), wxSize(w, h), style, direction, RowsOrColumns)
307 {
308 wxBitmap* bitmaps[8];
309
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);
318
319 #ifdef __WINDOWS__
320 int width = 24;
321 #else
322 int width = 16;
323 #endif
324 int offX = 5;
325 int currentX = 5;
326
327 AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
328 currentX += width + 5;
329 AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
330 currentX += width + 5;
331 AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
332 currentX += width + 5;
333 AddSeparator();
334 AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
335 currentX += width + 5;
336 AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
337 currentX += width + 5;
338 AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
339 currentX += width + 5;
340 AddSeparator();
341 AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
342 currentX += width + 5;
343 AddSeparator();
344 AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
345
346 CreateTools();
347
348 int i;
349 for (i = 0; i < 8; i++)
350 delete bitmaps[i];
351 }
352
353 bool TestRibbon::OnLeftClick(int toolIndex, bool toggled)
354 {
355 char buf[200];
356 sprintf(buf, "Clicked on tool %d", toolIndex);
357 frame->SetStatusText(buf);
358 return TRUE;
359 }
360
361 void TestRibbon::OnMouseEnter(int toolIndex)
362 {
363 char buf[200];
364 if (toolIndex > -1)
365 {
366 sprintf(buf, "This is tool number %d", toolIndex);
367 frame->SetStatusText(buf);
368 }
369 else frame->SetStatusText("");
370 }
371
372 void TestRibbon::OnPaint(wxPaintEvent& event)
373 {
374 wxToolBar::OnPaint(event);
375
376 wxPaintDC dc(this);
377
378 int w, h;
379 GetSize(&w, &h);
380 dc.SetPen(*wxBLACK_PEN);
381 dc.SetBrush(*wxTRANSPARENT_BRUSH);
382 dc.DrawLine(0, h-1, w, h-1);
383 }
384
385 #endif