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