]> git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
minimal now works in Unicode mode
[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 #if defined(__WXGTK__) || defined(__WXMOTIF__)
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"
33 #include "bitmaps/paste.xpm"
34 #include "bitmaps/print.xpm"
35 #include "bitmaps/help.xpm"
36 #endif
37
38
39 #include "mdi.h"
40
41 MyFrame *frame = (MyFrame *) NULL;
42 wxList my_children;
43
44 IMPLEMENT_APP(MyApp)
45
46 // For drawing lines in a canvas
47 long xpos = -1;
48 long ypos = -1;
49
50 int winNumber = 1;
51
52 // Initialise this in OnInit, not statically
53 bool MyApp::OnInit(void)
54 {
55 // Create the main frame window
56
57 frame = new MyFrame((wxFrame *) NULL, -1, (char *) "MDI Demo", wxPoint(-1, -1), wxSize(500, 400),
58 wxDEFAULT_FRAME_STYLE | wxHSCROLL | wxVSCROLL);
59
60 // Give it an icon
61 #ifdef __WXMSW__
62 frame->SetIcon(wxIcon("mdi_icn"));
63 #else
64 frame->SetIcon(wxIcon( mondrian_xpm ));
65 #endif
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
93 BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
94 EVT_MENU(MDI_ABOUT, MyFrame::OnAbout)
95 EVT_MENU(MDI_NEW_WINDOW, MyFrame::OnNewWindow)
96 EVT_SIZE(MyFrame::OnSize)
97 EVT_MENU(MDI_QUIT, MyFrame::OnQuit)
98 END_EVENT_TABLE()
99
100 // Define my frame constructor
101 MyFrame::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
109 CreateToolBar(wxNO_BORDER|wxTB_FLAT|wxTB_HORIZONTAL);
110 InitToolBar(GetToolBar());
111
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);
119 }
120
121 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event) )
122 {
123 Close(TRUE);
124 }
125
126 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event) )
127 {
128 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
129 }
130
131 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event) )
132 {
133 // Make another frame, containing a canvas
134 MyChild *subframe = new MyChild(frame, "Canvas Frame", wxPoint(-1, -1), wxSize(-1, -1),
135 wxDEFAULT_FRAME_STYLE);
136
137 wxString title;
138 title.Printf("Canvas Frame %d", winNumber);
139 subframe->SetTitle(title);
140 winNumber ++;
141
142 // Give it an icon
143 #ifdef __WXMSW__
144 subframe->SetIcon(wxIcon("chrt_icn"));
145 #else
146 subframe->SetIcon(wxIcon( mondrian_xpm ));
147 #endif
148
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
185 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
186 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
187 END_EVENT_TABLE()
188
189 // Define a constructor for my canvas
190 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size):
191 wxScrolledWindow(parent, -1, pos, size, wxSUNKEN_BORDER|wxVSCROLL|wxHSCROLL)
192 {
193 SetBackgroundColour(wxColour("WHITE"));
194 }
195
196 // Define the repainting behaviour
197 void MyCanvas::OnDraw(wxDC& dc)
198 {
199 dc.SetFont(*wxSWISS_FONT);
200 dc.SetPen(*wxGREEN_PEN);
201 dc.DrawLine(0, 0, 200, 200);
202 dc.DrawLine(200, 0, 0, 200);
203
204 dc.SetBrush(*wxCYAN_BRUSH);
205 dc.SetPen(*wxRED_PEN);
206 dc.DrawRectangle(100, 100, 100, 50);
207 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
208
209 dc.DrawEllipse(250, 250, 100, 50);
210 dc.DrawSpline(50, 200, 50, 100, 200, 10);
211 dc.DrawLine(50, 230, 200, 230);
212 dc.DrawText("This is a test string", 50, 230);
213
214 wxPoint points[3];
215 points[0].x = 200; points[0].y = 300;
216 points[1].x = 100; points[1].y = 400;
217 points[2].x = 300; points[2].y = 400;
218
219 dc.DrawPolygon(3, points);
220 }
221
222 // This implements a tiny doodling program! Drag the mouse using
223 // the left button.
224 void MyCanvas::OnEvent(wxMouseEvent& event)
225 {
226 wxClientDC dc(this);
227 PrepareDC(dc);
228
229 wxPoint pt(event.GetLogicalPosition(dc));
230
231 if (xpos > -1 && ypos > -1 && event.Dragging())
232 {
233 dc.SetPen(*wxBLACK_PEN);
234 dc.DrawLine(xpos, ypos, pt.x, pt.y);
235 }
236 xpos = pt.x;
237 ypos = pt.y;
238 }
239
240 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event) )
241 {
242 int w, h;
243 GetClientSize(&w, &h);
244
245 textWindow->SetSize(0, 0, 200, h);
246 GetClientWindow()->SetSize(200, 0, w - 200, h);
247 }
248
249 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
250 // to the parent window for processing, so no need to
251 // duplicate event handlers here.
252
253 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
254 EVT_MENU(MDI_CHILD_QUIT, MyChild::OnQuit)
255 END_EVENT_TABLE()
256
257 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
258 const long style):
259 wxMDIChildFrame(parent, -1, title, pos, size, style)
260 {
261 canvas = (MyCanvas *) NULL;
262 my_children.Append(this);
263 }
264
265 MyChild::~MyChild(void)
266 {
267 my_children.DeleteObject(this);
268 }
269
270 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
271 {
272 Close(TRUE);
273 }
274
275 void MyChild::OnActivate(wxActivateEvent& event)
276 {
277 if (event.GetActive() && canvas)
278 canvas->SetFocus();
279 }
280
281 void MyFrame::InitToolBar(wxToolBar* toolBar)
282 {
283 wxBitmap* bitmaps[8];
284
285 #ifdef __WXMSW__
286 bitmaps[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE);
287 bitmaps[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE);
288 bitmaps[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE);
289 bitmaps[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE);
290 bitmaps[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE);
291 bitmaps[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE);
292 bitmaps[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE);
293 bitmaps[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE);
294 #else
295 bitmaps[0] = new wxBitmap( new_xpm );
296 bitmaps[1] = new wxBitmap( open_xpm );
297 bitmaps[2] = new wxBitmap( save_xpm );
298 bitmaps[3] = new wxBitmap( copy_xpm );
299 bitmaps[4] = new wxBitmap( cut_xpm );
300 bitmaps[5] = new wxBitmap( paste_xpm );
301 bitmaps[6] = new wxBitmap( print_xpm );
302 bitmaps[7] = new wxBitmap( help_xpm );
303 #endif
304
305 #ifdef __WXMSW__
306 int width = 24;
307 #else
308 int width = 16;
309 #endif
310 int currentX = 5;
311
312 toolBar->AddTool( MDI_NEW_WINDOW, *(bitmaps[0]), wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "New file");
313 currentX += width + 5;
314 toolBar->AddTool(1, *bitmaps[1], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Open file");
315 currentX += width + 5;
316 toolBar->AddTool(2, *bitmaps[2], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Save file");
317 currentX += width + 5;
318 toolBar->AddSeparator();
319 toolBar->AddTool(3, *bitmaps[3], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Copy");
320 currentX += width + 5;
321 toolBar->AddTool(4, *bitmaps[4], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Cut");
322 currentX += width + 5;
323 toolBar->AddTool(5, *bitmaps[5], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Paste");
324 currentX += width + 5;
325 toolBar->AddSeparator();
326 toolBar->AddTool(6, *bitmaps[6], wxNullBitmap, FALSE, currentX, -1, (wxObject *) NULL, "Print");
327 currentX += width + 5;
328 toolBar->AddSeparator();
329 toolBar->AddTool(7, *bitmaps[7], wxNullBitmap, TRUE, currentX, -1, (wxObject *) NULL, "Help");
330
331 toolBar->Realize();
332
333 int i;
334 for (i = 0; i < 8; i++)
335 delete bitmaps[i];
336 }
337