]>
git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
26 #include <wx/tbar95.h>
28 #include <wx/tbarmsw.h>
34 MyFrame
*frame
= NULL
;
39 // For drawing lines in a canvas
45 // Initialise this in OnInit, not statically
46 bool MyApp::OnInit(void)
48 // Create the main frame window
50 frame
= new MyFrame(NULL
, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
51 wxDEFAULT_FRAME
| wxHSCROLL
| wxVSCROLL
);
53 // Give it an icon (this is ignored in MDI mode: uses resources)
55 frame
->SetIcon(wxIcon("mdi_icn"));
58 frame
->SetIcon(wxIcon("aiai.xbm"));
62 wxMenu
*file_menu
= new wxMenu
;
64 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
65 file_menu
->Append(MDI_QUIT
, "&Exit");
67 wxMenu
*help_menu
= new wxMenu
;
68 help_menu
->Append(MDI_ABOUT
, "&About");
70 wxMenuBar
*menu_bar
= new wxMenuBar
;
72 menu_bar
->Append(file_menu
, "&File");
73 menu_bar
->Append(help_menu
, "&Help");
75 // Associate the menu bar with the frame
76 frame
->SetMenuBar(menu_bar
);
78 frame
->CreateStatusBar();
87 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
88 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
89 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
90 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
91 EVT_SIZE(MyFrame::OnSize
)
94 // Define my frame constructor
95 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
97 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
99 textWindow
= new wxTextCtrl(this, -1, "", wxDefaultPosition
, wxDefaultSize
,
100 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
101 textWindow
->SetValue("A help window");
104 toolBar
= new TestRibbon(this, 0, 0, 100, 30, wxNO_BORDER
, wxVERTICAL
, 1);
109 void MyFrame::OnQuit(wxCommandEvent
& event
)
114 void MyFrame::OnAbout(wxCommandEvent
& event
)
116 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
119 void MyFrame::OnNewWindow(wxCommandEvent
& event
)
121 // Make another frame, containing a canvas
122 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
126 sprintf(titleBuf
, "Canvas Frame %d", winNumber
);
127 subframe
->SetTitle(titleBuf
);
130 // Give it an icon (this is ignored in MDI mode: uses resources)
132 subframe
->SetIcon(wxIcon("chrt_icn"));
135 subframe
->SetIcon(wxIcon("aiai.xbm"));
138 // Give it a status line
139 subframe
->CreateStatusBar();
142 wxMenu
*file_menu
= new wxMenu
;
144 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
145 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child");
146 file_menu
->Append(MDI_QUIT
, "&Exit");
148 wxMenu
*option_menu
= new wxMenu
;
151 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
153 wxMenu
*help_menu
= new wxMenu
;
154 help_menu
->Append(MDI_ABOUT
, "&About");
156 wxMenuBar
*menu_bar
= new wxMenuBar
;
158 menu_bar
->Append(file_menu
, "&File");
159 menu_bar
->Append(option_menu
, "&Options");
160 menu_bar
->Append(help_menu
, "&Help");
162 // Associate the menu bar with the frame
163 subframe
->SetMenuBar(menu_bar
);
166 subframe
->GetClientSize(&width
, &height
);
167 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
168 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
169 subframe
->canvas
= canvas
;
171 // Give it scrollbars
172 canvas
->SetScrollbars(20, 20, 50, 50);
174 subframe
->Show(TRUE
);
177 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
178 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
181 // Define a constructor for my canvas
182 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
183 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
187 // Define the repainting behaviour
188 void MyCanvas::OnDraw(wxDC
& dc
)
190 dc
.SetFont(*wxSWISS_FONT
);
191 dc
.SetPen(*wxGREEN_PEN
);
192 dc
.DrawLine(0, 0, 200, 200);
193 dc
.DrawLine(200, 0, 0, 200);
195 dc
.SetBrush(*wxCYAN_BRUSH
);
196 dc
.SetPen(*wxRED_PEN
);
197 dc
.DrawRectangle(100, 100, 100, 50);
198 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
200 dc
.DrawEllipse(250, 250, 100, 50);
201 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
202 dc
.DrawLine(50, 230, 200, 230);
203 dc
.DrawText("This is a test string", 50, 230);
206 // This implements a tiny doodling program! Drag the mouse using
208 void MyCanvas::OnEvent(wxMouseEvent
& event
)
213 wxPoint
pt(event
.GetLogicalPosition(dc
));
215 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
217 dc
.SetPen(*wxBLACK_PEN
);
218 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
224 // Define the behaviour for the frame closing
225 // - must delete all frames except for the main one.
226 bool MyFrame::OnClose(void)
228 // Must delete children
229 wxNode
*node
= my_children
.First();
232 MyChild
*child
= (MyChild
*)node
->Data();
233 wxNode
*next
= node
->Next();
241 void MyFrame::OnSize(wxSizeEvent
& event
)
244 GetClientSize(&w
, &h
);
249 wxWindow
* tbar
= GetToolBar();
252 tbar
->GetSize(&tw
, &th
);
253 tbar
->SetSize(w
, th
);
257 textWindow
->SetSize(0, th
, 200, h
-th
);
258 GetClientWindow()->SetSize(200, th
, w
- 200, h
-th
);
261 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
262 // to the parent window for processing, so no need to
263 // duplicate event handlers here.
265 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
266 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
269 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
271 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
274 my_children
.Append(this);
277 MyChild::~MyChild(void)
279 my_children
.DeleteObject(this);
282 void MyChild::OnQuit(wxCommandEvent
& event
)
287 void MyChild::OnActivate(wxActivateEvent
& event
)
289 if (event
.GetActive() && canvas
)
293 bool MyChild::OnClose(void)
300 BEGIN_EVENT_TABLE(TestRibbon
, wxToolBar
)
301 EVT_PAINT(TestRibbon::OnPaint
)
304 TestRibbon::TestRibbon(wxFrame
*frame
, int x
, int y
, int w
, int h
,
305 long style
, int direction
, int RowsOrColumns
):
306 wxToolBar(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
, direction
, RowsOrColumns
)
308 wxBitmap
* bitmaps
[8];
310 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
311 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
312 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
313 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
314 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
315 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
316 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
317 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
327 AddTool(0, *bitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "New file");
328 currentX
+= width
+ 5;
329 AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Open file");
330 currentX
+= width
+ 5;
331 AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Save file");
332 currentX
+= width
+ 5;
334 AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Copy");
335 currentX
+= width
+ 5;
336 AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Cut");
337 currentX
+= width
+ 5;
338 AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Paste");
339 currentX
+= width
+ 5;
341 AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Print");
342 currentX
+= width
+ 5;
344 AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, NULL
, "Help");
349 for (i
= 0; i
< 8; i
++)
353 bool TestRibbon::OnLeftClick(int toolIndex
, bool toggled
)
356 sprintf(buf
, "Clicked on tool %d", toolIndex
);
357 frame
->SetStatusText(buf
);
361 void TestRibbon::OnMouseEnter(int toolIndex
)
366 sprintf(buf
, "This is tool number %d", toolIndex
);
367 frame
->SetStatusText(buf
);
369 else frame
->SetStatusText("");
372 void TestRibbon::OnPaint(wxPaintEvent
& event
)
374 wxToolBar::OnPaint(event
);
380 dc
.SetPen(*wxBLACK_PEN
);
381 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
382 dc
.DrawLine(0, h
-1, w
, h
-1);