wxFrame::CreateToolBar() stuff
[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 #include <wx/toolbar.h>
25
26 #ifdef __WXGTK__
27 #include "folder.xpm"
28 #endif
29
30 #include "mdi.h"
31
32 MyFrame *frame = NULL;
33 wxList my_children;
34
35 IMPLEMENT_APP(MyApp)
36
37 // For drawing lines in a canvas
38 long xpos = -1;
39 long ypos = -1;
40
41 int winNumber = 1;
42
43 // Initialise this in OnInit, not statically
44 bool MyApp::OnInit(void)
45 {
46 // Create the main frame window
47
48 frame = new MyFrame(NULL, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
49 wxDEFAULT_FRAME | wxHSCROLL | wxVSCROLL);
50
51 // Give it an icon (this is ignored in MDI mode: uses resources)
52 #ifdef __WXMSW__
53 frame->SetIcon(wxIcon("mdi_icn"));
54 #endif
55 #ifdef __X__
56 frame->SetIcon(wxIcon("aiai.xbm"));
57 #endif
58
59 // Make a menubar
60 wxMenu *file_menu = new wxMenu;
61
62 file_menu->Append(MDI_NEW_WINDOW, "&New window");
63 file_menu->Append(MDI_QUIT, "&Exit");
64
65 wxMenu *help_menu = new wxMenu;
66 help_menu->Append(MDI_ABOUT, "&About");
67
68 wxMenuBar *menu_bar = new wxMenuBar;
69
70 menu_bar->Append(file_menu, "&File");
71 menu_bar->Append(help_menu, "&Help");
72
73 // Associate the menu bar with the frame
74 frame->SetMenuBar(menu_bar);
75
76 frame->CreateStatusBar();
77
78 frame->Show(TRUE);
79
80 SetTopWindow(frame);
81
82 return TRUE;
83 }
84
85 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
86 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
87 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
88 EVT_SIZE(MyFrame::OnSize)
89 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
90 END_EVENT_TABLE()
91
92 // Define my frame constructor
93 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
94 const long style):
95 wxMDIParentFrame(parent, id, title, pos, size, style)
96 {
97 textWindow = new wxTextCtrl(this, -1, "", wxDefaultPosition, wxDefaultSize,
98 wxTE_MULTILINE|wxSUNKEN_BORDER);
99 textWindow->SetValue("A help window");
100
101 toolBar = new TestRibbon(this, 0, 0, 100, 30, wxNO_BORDER, wxVERTICAL, 1);
102 SetToolBar(toolBar);
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 __WXMSW__
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 wxPoint points[3];
202 points[0].x = 200; points[0].y = 300;
203 points[1].x = 100; points[1].y = 400;
204 points[2].x = 300; points[2].y = 400;
205
206 dc.DrawPolygon(3, points);
207 }
208
209 // This implements a tiny doodling program! Drag the mouse using
210 // the left button.
211 void MyCanvas::OnEvent(wxMouseEvent& event)
212 {
213 wxClientDC dc(this);
214 PrepareDC(dc);
215
216 wxPoint pt(event.GetLogicalPosition(dc));
217
218 if (xpos > -1 && ypos > -1 && event.Dragging())
219 {
220 dc.SetPen(*wxBLACK_PEN);
221 dc.DrawLine(xpos, ypos, pt.x, pt.y);
222 }
223 xpos = pt.x;
224 ypos = pt.y;
225 }
226
227 // Define the behaviour for the frame closing
228 // - must delete all frames except for the main one.
229 bool MyFrame::OnClose(void)
230 {
231 // Must delete children
232 wxNode *node = my_children.First();
233 while (node)
234 {
235 MyChild *child = (MyChild *)node->Data();
236 wxNode *next = node->Next();
237 child->OnClose();
238 delete child;
239 node = next;
240 }
241 return TRUE;
242 }
243
244 void MyFrame::OnSize(wxSizeEvent& event)
245 {
246 int w, h;
247 GetClientSize(&w, &h);
248 int tw = 0;
249 int th = 0;
250
251 wxWindow* tbar = GetToolBar();
252 if (tbar)
253 {
254 tbar->GetSize(&tw, &th);
255 tbar->SetSize(w, th);
256 }
257
258 textWindow->SetSize(0, th, 200, h-th);
259 GetClientWindow()->SetSize(200, th, w - 200, h-th);
260 }
261
262 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
263 // to the parent window for processing, so no need to
264 // duplicate event handlers here.
265
266 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
267 EVT_SIZE( MyChild::OnSize)
268 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
269 END_EVENT_TABLE()
270
271 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
272 const long style):
273 wxMDIChildFrame(parent, -1, title, pos, size, style)
274 {
275 canvas = NULL;
276 my_children.Append(this);
277 }
278
279 MyChild::~MyChild(void)
280 {
281 my_children.DeleteObject(this);
282 }
283
284 void MyChild::OnSize(wxSizeEvent& WXUNUSED(event))
285 {
286 int x = 0;
287 int y = 0;
288 GetClientSize( &x, &y );
289 if (canvas) canvas->SetSize( x, y );
290 }
291
292 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
293 {
294 Close(TRUE);
295 }
296
297 void MyChild::OnActivate(wxActivateEvent& event)
298 {
299 if (event.GetActive() && canvas)
300 canvas->SetFocus();
301 }
302
303 bool MyChild::OnClose(void)
304 {
305 return TRUE;
306 }
307
308 BEGIN_EVENT_TABLE(TestRibbon, wxToolBar)
309 EVT_PAINT(TestRibbon::OnPaint)
310 END_EVENT_TABLE()
311
312 TestRibbon::TestRibbon(wxFrame *frame, int x, int y, int w, int h,
313 long style, int direction, int RowsOrColumns):
314 wxToolBar(frame, -1, wxPoint(x, y), wxSize(w, h), style, direction, RowsOrColumns)
315 {
316 wxBitmap* bitmaps[8];
317
318 #ifdef __WXMSW__
319 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
320 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
321 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
322 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
323 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
324 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
325 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
326 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
327 #else
328 bitmaps[0] = new wxBitmap( folder_xpm );
329 bitmaps[1] = new wxBitmap( folder_xpm );
330 bitmaps[2] = new wxBitmap( folder_xpm );
331 bitmaps[3] = new wxBitmap( folder_xpm );
332 bitmaps[4] = new wxBitmap( folder_xpm );
333 bitmaps[5] = new wxBitmap( folder_xpm );
334 bitmaps[6] = new wxBitmap( folder_xpm );
335 bitmaps[7] = new wxBitmap( folder_xpm );
336 #endif
337
338 #ifdef __WXMSW__
339 int width = 24;
340 #else
341 int width = 16;
342 #endif
343 int offX = 5;
344 int currentX = 5;
345
346 AddTool(0, *bitmaps[0], wxNullBitmap, FALSE, currentX, -1, NULL, "New file");
347 currentX += width + 5;
348 AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, NULL, "Open file");
349 currentX += width + 5;
350 AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, NULL, "Save file");
351 currentX += width + 5;
352 AddSeparator();
353 AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, NULL, "Copy");
354 currentX += width + 5;
355 AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, NULL, "Cut");
356 currentX += width + 5;
357 AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, NULL, "Paste");
358 currentX += width + 5;
359 AddSeparator();
360 AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, NULL, "Print");
361 currentX += width + 5;
362 AddSeparator();
363 AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, NULL, "Help");
364
365 CreateTools();
366
367 int i;
368 for (i = 0; i < 8; i++)
369 delete bitmaps[i];
370 }
371
372 bool TestRibbon::OnLeftClick(int toolIndex, bool toggled)
373 {
374 char buf[200];
375 sprintf(buf, "Clicked on tool %d", toolIndex);
376 frame->SetStatusText(buf);
377 return TRUE;
378 }
379
380 void TestRibbon::OnMouseEnter(int toolIndex)
381 {
382 char buf[200];
383 if (toolIndex > -1)
384 {
385 sprintf(buf, "This is tool number %d", toolIndex);
386 frame->SetStatusText(buf);
387 }
388 else frame->SetStatusText("");
389 }
390
391 void TestRibbon::OnPaint(wxPaintEvent& event)
392 {
393 wxToolBar::OnPaint(event);
394
395 wxPaintDC dc(this);
396
397 int w, h;
398 GetSize(&w, &h);
399 dc.SetPen(*wxBLACK_PEN);
400 dc.SetBrush(*wxTRANSPARENT_BRUSH);
401 dc.DrawLine(0, h-1, w, h-1);
402 }
403