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
)
89 EVT_MENU(MDI_CHANGE_POSITION
, MyChild::OnChangePosition
)
90 EVT_MENU(MDI_CHANGE_SIZE
, MyChild::OnChangeSize
)
92 EVT_UPDATE_UI(MDI_REFRESH
, MyChild::OnUpdateRefresh
)
94 EVT_SIZE(MyChild::OnSize
)
95 EVT_MOVE(MyChild::OnMove
)
97 EVT_CLOSE(MyChild::OnClose
)
100 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
101 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
104 // ===========================================================================
106 // ===========================================================================
108 // ---------------------------------------------------------------------------
110 // ---------------------------------------------------------------------------
112 // Initialise this in OnInit, not statically
115 // Create the main frame window
117 frame
= new MyFrame((wxFrame
*)NULL
, -1, "MDI Demo",
118 wxPoint(-1, -1), wxSize(500, 400),
119 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
122 // Experimental: change the window menu
123 wxMenu
* windowMenu
= new wxMenu
;
124 windowMenu
->Append(5000, "My menu item!");
125 frame
->SetWindowMenu(windowMenu
);
131 frame
->SetIcon(wxIcon("mdi_icn"));
133 frame
->SetIcon(wxIcon( mondrian_xpm
));
137 wxMenu
*file_menu
= new wxMenu
;
139 file_menu
->Append(MDI_NEW_WINDOW
, "&New window\tCtrl-N", "Create a new child window");
140 file_menu
->Append(MDI_QUIT
, "&Exit\tAlt-X", "Quit the program");
142 wxMenu
*help_menu
= new wxMenu
;
143 help_menu
->Append(MDI_ABOUT
, "&About\tF1");
145 wxMenuBar
*menu_bar
= new wxMenuBar
;
147 menu_bar
->Append(file_menu
, "&File");
148 menu_bar
->Append(help_menu
, "&Help");
150 // Associate the menu bar with the frame
151 frame
->SetMenuBar(menu_bar
);
153 frame
->CreateStatusBar();
162 // ---------------------------------------------------------------------------
164 // ---------------------------------------------------------------------------
166 // Define my frame constructor
167 MyFrame::MyFrame(wxWindow
*parent
,
169 const wxString
& title
,
173 : wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
175 textWindow
= new wxTextCtrl(this, -1, "A help window",
176 wxDefaultPosition
, wxDefaultSize
,
177 wxTE_MULTILINE
| wxSUNKEN_BORDER
);
179 CreateToolBar(wxNO_BORDER
| wxTB_FLAT
| wxTB_HORIZONTAL
);
180 InitToolBar(GetToolBar());
183 wxAcceleratorEntry entries
[3];
184 entries
[0].Set(wxACCEL_CTRL
, (int) 'N', MDI_NEW_WINDOW
);
185 entries
[1].Set(wxACCEL_CTRL
, (int) 'X', MDI_QUIT
);
186 entries
[2].Set(wxACCEL_CTRL
, (int) 'A', MDI_ABOUT
);
187 wxAcceleratorTable
accel(3, entries
);
188 SetAcceleratorTable(accel
);
191 void MyFrame::OnClose(wxCloseEvent
& event
)
193 if ( event
.CanVeto() && (gs_nFrames
> 0) )
196 msg
.Printf(_T("%d windows still open, close anyhow?"), gs_nFrames
);
197 if ( wxMessageBox(msg
, "Please confirm",
198 wxICON_QUESTION
| wxYES_NO
) != wxYES
)
209 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
214 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
) )
216 (void)wxMessageBox("wxWindows 2.0 MDI Demo\n"
217 "Author: Julian Smart (c) 1997\n"
218 "Usage: mdi.exe", "About MDI Demo");
221 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
) )
223 // Make another frame, containing a canvas
224 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame",
225 wxPoint(-1, -1), wxSize(-1, -1),
226 wxDEFAULT_FRAME_STYLE
);
229 title
.Printf(_T("Canvas Frame %d"), ++gs_nFrames
);
231 subframe
->SetTitle(title
);
235 subframe
->SetIcon(wxIcon("chrt_icn"));
237 subframe
->SetIcon(wxIcon( mondrian_xpm
));
241 wxMenu
*file_menu
= new wxMenu
;
243 file_menu
->Append(MDI_NEW_WINDOW
, "&New window\tCtrl-N");
244 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child", "Close this window");
245 file_menu
->Append(MDI_QUIT
, "&Exit\tAlt-X");
247 wxMenu
*option_menu
= new wxMenu
;
249 option_menu
->Append(MDI_REFRESH
, "&Refresh picture\tF5");
250 option_menu
->Append(MDI_CHANGE_TITLE
, "Change &title...\tCtrl-T");
252 wxMenu
*help_menu
= new wxMenu
;
253 help_menu
->Append(MDI_ABOUT
, "&About\tF1");
255 wxMenuBar
*menu_bar
= new wxMenuBar
;
257 menu_bar
->Append(file_menu
, "&File");
258 menu_bar
->Append(option_menu
, "&Options");
259 menu_bar
->Append(help_menu
, "&Help");
261 // Associate the menu bar with the frame
262 subframe
->SetMenuBar(menu_bar
);
264 subframe
->CreateStatusBar();
265 subframe
->SetStatusText(title
);
268 subframe
->GetClientSize(&width
, &height
);
269 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
270 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
271 subframe
->canvas
= canvas
;
273 // Give it scrollbars
274 canvas
->SetScrollbars(20, 20, 50, 50);
276 subframe
->Show(TRUE
);
279 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
282 GetClientSize(&w
, &h
);
284 textWindow
->SetSize(0, 0, 200, h
);
285 GetClientWindow()->SetSize(200, 0, w
- 200, h
);
288 void MyFrame::InitToolBar(wxToolBar
* toolBar
)
290 wxBitmap
* bitmaps
[8];
293 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
294 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
295 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
296 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
297 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
298 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
299 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
300 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
302 bitmaps
[0] = new wxBitmap( new_xpm
);
303 bitmaps
[1] = new wxBitmap( open_xpm
);
304 bitmaps
[2] = new wxBitmap( save_xpm
);
305 bitmaps
[3] = new wxBitmap( copy_xpm
);
306 bitmaps
[4] = new wxBitmap( cut_xpm
);
307 bitmaps
[5] = new wxBitmap( paste_xpm
);
308 bitmaps
[6] = new wxBitmap( print_xpm
);
309 bitmaps
[7] = new wxBitmap( help_xpm
);
319 toolBar
->AddTool( MDI_NEW_WINDOW
, *(bitmaps
[0]), wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "New file");
320 currentX
+= width
+ 5;
321 toolBar
->AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Open file");
322 currentX
+= width
+ 5;
323 toolBar
->AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Save file");
324 currentX
+= width
+ 5;
325 toolBar
->AddSeparator();
326 toolBar
->AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Copy");
327 currentX
+= width
+ 5;
328 toolBar
->AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Cut");
329 currentX
+= width
+ 5;
330 toolBar
->AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Paste");
331 currentX
+= width
+ 5;
332 toolBar
->AddSeparator();
333 toolBar
->AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, (wxObject
*) NULL
, "Print");
334 currentX
+= width
+ 5;
335 toolBar
->AddSeparator();
336 toolBar
->AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, (wxObject
*) NULL
, "Help");
341 for (i
= 0; i
< 8; i
++)
345 // ---------------------------------------------------------------------------
347 // ---------------------------------------------------------------------------
349 // Define a constructor for my canvas
350 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
351 : wxScrolledWindow(parent
, -1, pos
, size
,
352 wxSUNKEN_BORDER
|wxVSCROLL
|wxHSCROLL
)
354 SetBackgroundColour(wxColour("WHITE"));
359 // Define the repainting behaviour
360 void MyCanvas::OnDraw(wxDC
& dc
)
362 dc
.SetFont(*wxSWISS_FONT
);
363 dc
.SetPen(*wxGREEN_PEN
);
364 dc
.DrawLine(0, 0, 200, 200);
365 dc
.DrawLine(200, 0, 0, 200);
367 dc
.SetBrush(*wxCYAN_BRUSH
);
368 dc
.SetPen(*wxRED_PEN
);
369 dc
.DrawRectangle(100, 100, 100, 50);
370 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
372 dc
.DrawEllipse(250, 250, 100, 50);
374 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
375 #endif // wxUSE_SPLINES
376 dc
.DrawLine(50, 230, 200, 230);
377 dc
.DrawText("This is a test string", 50, 230);
380 points
[0].x
= 200; points
[0].y
= 300;
381 points
[1].x
= 100; points
[1].y
= 400;
382 points
[2].x
= 300; points
[2].y
= 400;
384 dc
.DrawPolygon(3, points
);
387 // This implements a tiny doodling program! Drag the mouse using the left
389 void MyCanvas::OnEvent(wxMouseEvent
& event
)
394 wxPoint
pt(event
.GetLogicalPosition(dc
));
396 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
398 dc
.SetPen(*wxBLACK_PEN
);
399 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
408 // ---------------------------------------------------------------------------
410 // ---------------------------------------------------------------------------
412 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
,
413 const wxPoint
& pos
, const wxSize
& size
,
415 : wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
417 canvas
= (MyCanvas
*) NULL
;
418 my_children
.Append(this);
423 my_children
.DeleteObject(this);
426 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
431 void MyChild::OnUpdateRefresh(wxUpdateUIEvent
& event
)
433 event
.Enable( canvas
&& canvas
->IsDirty() );
436 void MyChild::OnRefresh(wxCommandEvent
& WXUNUSED(event
))
442 void MyChild::OnChangeTitle(wxCommandEvent
& WXUNUSED(event
))
444 static wxString s_title
= _T("Canvas Frame");
446 wxString title
= wxGetTextFromUser(_T("Enter the new title for MDI child"),
447 _T("MDI sample question"),
457 void MyChild::OnActivate(wxActivateEvent
& event
)
459 if ( event
.GetActive() && canvas
)
463 void MyChild::OnClose(wxCloseEvent
& event
)
465 if ( canvas
&& canvas
->IsDirty() )
467 if ( wxMessageBox("Really close?", "Please confirm",
468 wxICON_QUESTION
| wxYES_NO
) != wxYES
)