]>
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 points
[0].x
= 200; points
[0].y
= 300;
207 points
[1].x
= 100; points
[1].y
= 400;
208 points
[2].x
= 300; points
[2].y
= 400;
210 dc
.DrawPolygon(3, points
);
213 // This implements a tiny doodling program! Drag the mouse using
215 void MyCanvas::OnEvent(wxMouseEvent
& event
)
220 wxPoint
pt(event
.GetLogicalPosition(dc
));
222 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
224 dc
.SetPen(*wxBLACK_PEN
);
225 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
231 // Define the behaviour for the frame closing
232 // - must delete all frames except for the main one.
233 bool MyFrame::OnClose(void)
235 // Must delete children
236 wxNode
*node
= my_children
.First();
239 MyChild
*child
= (MyChild
*)node
->Data();
240 wxNode
*next
= node
->Next();
248 void MyFrame::OnSize(wxSizeEvent
& event
)
251 GetClientSize(&w
, &h
);
256 wxWindow
* tbar
= GetToolBar();
259 tbar
->GetSize(&tw
, &th
);
260 tbar
->SetSize(w
, th
);
264 textWindow
->SetSize(0, th
, 200, h
-th
);
265 GetClientWindow()->SetSize(200, th
, w
- 200, h
-th
);
268 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
269 // to the parent window for processing, so no need to
270 // duplicate event handlers here.
272 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
273 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
276 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
278 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
281 my_children
.Append(this);
284 MyChild::~MyChild(void)
286 my_children
.DeleteObject(this);
289 void MyChild::OnQuit(wxCommandEvent
& event
)
294 void MyChild::OnActivate(wxActivateEvent
& event
)
296 if (event
.GetActive() && canvas
)
300 bool MyChild::OnClose(void)
307 BEGIN_EVENT_TABLE(TestRibbon
, wxToolBar
)
308 EVT_PAINT(TestRibbon::OnPaint
)
311 TestRibbon::TestRibbon(wxFrame
*frame
, int x
, int y
, int w
, int h
,
312 long style
, int direction
, int RowsOrColumns
):
313 wxToolBar(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
, direction
, RowsOrColumns
)
315 wxBitmap
* bitmaps
[8];
317 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
318 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
319 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
320 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
321 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
322 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
323 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
324 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
334 AddTool(0, *bitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "New file");
335 currentX
+= width
+ 5;
336 AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Open file");
337 currentX
+= width
+ 5;
338 AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Save file");
339 currentX
+= width
+ 5;
341 AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Copy");
342 currentX
+= width
+ 5;
343 AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Cut");
344 currentX
+= width
+ 5;
345 AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Paste");
346 currentX
+= width
+ 5;
348 AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Print");
349 currentX
+= width
+ 5;
351 AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, NULL
, "Help");
356 for (i
= 0; i
< 8; i
++)
360 bool TestRibbon::OnLeftClick(int toolIndex
, bool toggled
)
363 sprintf(buf
, "Clicked on tool %d", toolIndex
);
364 frame
->SetStatusText(buf
);
368 void TestRibbon::OnMouseEnter(int toolIndex
)
373 sprintf(buf
, "This is tool number %d", toolIndex
);
374 frame
->SetStatusText(buf
);
376 else frame
->SetStatusText("");
379 void TestRibbon::OnPaint(wxPaintEvent
& event
)
381 wxToolBar::OnPaint(event
);
387 dc
.SetPen(*wxBLACK_PEN
);
388 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
389 dc
.DrawLine(0, h
-1, w
, h
-1);