1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ===========================================================================
14 // ===========================================================================
16 // ---------------------------------------------------------------------------
18 // ---------------------------------------------------------------------------
21 #define XtDisplay XTDISPLAY
22 #define XtWindow XTWINDOW
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
37 #include <wx/toolbar.h>
39 #if defined(__WXGTK__) || defined(__WXMOTIF__)
40 #include "mondrian.xpm"
41 #include "bitmaps/new.xpm"
42 #include "bitmaps/open.xpm"
43 #include "bitmaps/save.xpm"
44 #include "bitmaps/copy.xpm"
45 #include "bitmaps/cut.xpm"
46 #include "bitmaps/paste.xpm"
47 #include "bitmaps/print.xpm"
48 #include "bitmaps/help.xpm"
55 // ---------------------------------------------------------------------------
57 // ---------------------------------------------------------------------------
59 MyFrame
*frame
= (MyFrame
*) NULL
;
62 // For drawing lines in a canvas
63 static long xpos
= -1;
64 static long ypos
= -1;
66 static int gs_nFrames
= 0;
68 // ---------------------------------------------------------------------------
70 // ---------------------------------------------------------------------------
72 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
73 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
74 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
75 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
77 EVT_CLOSE(MyFrame::OnClose
)
79 EVT_SIZE(MyFrame::OnSize
)
82 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
83 // to the parent window for processing, so no need to
84 // duplicate event handlers here.
85 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
86 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
87 EVT_MENU(MDI_REFRESH
, MyChild::OnRefresh
)
88 EVT_MENU(MDI_CHANGE_TITLE
, MyChild::OnChangeTitle
)
90 EVT_CLOSE(MyChild::OnClose
)
93 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
94 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
97 // ===========================================================================
99 // ===========================================================================
101 // ---------------------------------------------------------------------------
103 // ---------------------------------------------------------------------------
105 // Initialise this in OnInit, not statically
108 // Create the main frame window
110 frame
= new MyFrame((wxFrame
*)NULL
, -1, "MDI Demo",
111 wxPoint(-1, -1), wxSize(500, 400),
112 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
115 // Experimental: change the window menu
116 wxMenu
* windowMenu
= new wxMenu
;
117 windowMenu
->Append(5000, "My menu item!");
118 frame
->SetWindowMenu(windowMenu
);
124 frame
->SetIcon(wxIcon("mdi_icn"));
126 frame
->SetIcon(wxIcon( mondrian_xpm
));
130 wxMenu
*file_menu
= new wxMenu
;
132 file_menu
->Append(MDI_NEW_WINDOW
, "&New window\tCtrl-N", "Create a new child window");
133 file_menu
->Append(MDI_QUIT
, "&Exit\tAlt-X", "Quit the program");
135 wxMenu
*help_menu
= new wxMenu
;
136 help_menu
->Append(MDI_ABOUT
, "&About\tF1");
138 wxMenuBar
*menu_bar
= new wxMenuBar
;
140 menu_bar
->Append(file_menu
, "&File");
141 menu_bar
->Append(help_menu
, "&Help");
143 // Associate the menu bar with the frame
144 frame
->SetMenuBar(menu_bar
);
146 frame
->CreateStatusBar();
155 // ---------------------------------------------------------------------------
157 // ---------------------------------------------------------------------------
159 // Define my frame constructor
160 MyFrame::MyFrame(wxWindow
*parent
,
162 const wxString
& title
,
166 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
168 textWindow
= new wxTextCtrl(this, -1, "A help window",
169 wxDefaultPosition
, wxDefaultSize
,
170 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
172 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
173 InitToolBar(GetToolBar());
176 wxAcceleratorEntry entries
[3];
177 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
178 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
179 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
180 wxAcceleratorTable
accel(3, entries
);
181 SetAcceleratorTable(accel
);
184 void MyFrame::OnClose(wxCloseEvent
& event
)
186 if ( event
.CanVeto() && (gs_nFrames
> 0) )
189 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
190 if ( wxMessageBox(msg
, "Please confirm",
191 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
202 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
207 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
209 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
210 "Author: Julian Smart (c) 1997\n"
211 "Usage: mdi.exe", "About MDI Demo");
214 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
216 // Make another frame, containing a canvas
217 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame",
218 wxPoint(-1, -1), wxSize(-1, -1),
219 wxDEFAULT_FRAME_STYLE
);
222 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
224 subframe
->SetTitle(title
);
228 subframe
->SetIcon(wxIcon("chrt_icn"));
230 subframe
->SetIcon(wxIcon( mondrian_xpm
));
234 wxMenu
*file_menu
= new wxMenu
;
236 file_menu
->Append(MDI_NEW_WINDOW
, "&New window\tCtrl-N");
237 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child", "Close this window");
238 file_menu
->Append(MDI_QUIT
, "&Exit\tAlt-X");
240 wxMenu
*option_menu
= new wxMenu
;
242 option_menu
->Append(MDI_REFRESH
, "&Refresh picture\tF5");
243 option_menu
->Append(MDI_CHANGE_TITLE
, "Change &title...\tCtrl-T");
245 wxMenu
*help_menu
= new wxMenu
;
246 help_menu
->Append(MDI_ABOUT
, "&About\tF1");
248 wxMenuBar
*menu_bar
= new wxMenuBar
;
250 menu_bar
->Append(file_menu
, "&File");
251 menu_bar
->Append(option_menu
, "&Options");
252 menu_bar
->Append(help_menu
, "&Help");
254 // Associate the menu bar with the frame
255 subframe
->SetMenuBar(menu_bar
);
257 subframe
->CreateStatusBar();
258 subframe
->SetStatusText(title
);
261 subframe
->GetClientSize(&width
, &height
);
262 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
263 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
264 subframe
->canvas
= canvas
;
266 // Give it scrollbars
267 canvas
->SetScrollbars(20, 20, 50, 50);
269 subframe
->Show(TRUE
);
272 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
275 GetClientSize(&w
, &h
);
277 textWindow
->SetSize(0, 0, 200, h
);
278 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
281 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
283 wxBitmap
* bitmaps
[8];
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
);
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
);
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");
334 for (i
= 0; i
< 8; i
++)
338 // ---------------------------------------------------------------------------
340 // ---------------------------------------------------------------------------
342 // Define a constructor for my canvas
343 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
344 : wxScrolledWindow(parent
, -1, pos
, size
,
345 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
347 SetBackgroundColour(wxColour("WHITE"));
352 // Define the repainting behaviour
353 void MyCanvas::OnDraw(wxDC
& dc
)
355 dc
.SetFont(*wxSWISS_FONT
);
356 dc
.SetPen(*wxGREEN_PEN
);
357 dc
.DrawLine(0, 0, 200, 200);
358 dc
.DrawLine(200, 0, 0, 200);
360 dc
.SetBrush(*wxCYAN_BRUSH
);
361 dc
.SetPen(*wxRED_PEN
);
362 dc
.DrawRectangle(100, 100, 100, 50);
363 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
365 dc
.DrawEllipse(250, 250, 100, 50);
367 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
368 #endif // wxUSE_SPLINES
369 dc
.DrawLine(50, 230, 200, 230);
370 dc
.DrawText("This is a test string", 50, 230);
373 points
[0].x
= 200; points
[0].y
= 300;
374 points
[1].x
= 100; points
[1].y
= 400;
375 points
[2].x
= 300; points
[2].y
= 400;
377 dc
.DrawPolygon(3, points
);
380 // This implements a tiny doodling program! Drag the mouse using the left
382 void MyCanvas::OnEvent(wxMouseEvent
& event
)
387 wxPoint
pt(event
.GetLogicalPosition(dc
));
389 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
391 dc
.SetPen(*wxBLACK_PEN
);
392 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
401 // ---------------------------------------------------------------------------
403 // ---------------------------------------------------------------------------
405 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
406 const wxPoint
& pos
, const wxSize
& size
,
408 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
410 canvas
= (MyCanvas
*) NULL
;
411 my_children
.Append(this);
416 my_children
.DeleteObject(this);
419 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
424 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
430 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
432 static wxString s_title
= _T("Canvas Frame");
434 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
435 _T("MDI sample question"),
445 void MyChild::OnActivate(wxActivateEvent
& event
)
447 if ( event
.GetActive() && canvas
)
451 void MyChild::OnClose(wxCloseEvent
& event
)
453 if ( canvas
&& canvas
->IsDirty() )
455 if ( wxMessageBox("Really close?", "Please confirm",
456 wxICON_QUESTION
| wxYES_NO
) != wxYES
)