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