]>
git.saurik.com Git - wxWidgets.git/blob - samples/mdi/mdi.cpp
114eb4c78dd3dc0a8a96ce1f582dd64c71c4e50c
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 //#include "folder.xpm"
39 MyFrame
*frame
= NULL
;
44 // For drawing lines in a canvas
50 // Initialise this in OnInit, not statically
51 bool MyApp::OnInit(void)
53 // Create the main frame window
55 frame
= new MyFrame(NULL
, -1, "MDI Demo", wxPoint(0, 0), wxSize(500, 400),
56 wxDEFAULT_FRAME
| wxHSCROLL
| wxVSCROLL
);
58 // Give it an icon (this is ignored in MDI mode: uses resources)
60 frame
->SetIcon(wxIcon("mdi_icn"));
63 frame
->SetIcon(wxIcon("aiai.xbm"));
67 wxMenu
*file_menu
= new wxMenu
;
69 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
70 file_menu
->Append(MDI_QUIT
, "&Exit");
72 wxMenu
*help_menu
= new wxMenu
;
73 help_menu
->Append(MDI_ABOUT
, "&About");
75 wxMenuBar
*menu_bar
= new wxMenuBar
;
77 menu_bar
->Append(file_menu
, "&File");
78 menu_bar
->Append(help_menu
, "&Help");
80 // Associate the menu bar with the frame
81 frame
->SetMenuBar(menu_bar
);
83 frame
->CreateStatusBar();
92 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
93 EVT_MENU(MDI_ABOUT
, MyFrame::OnAbout
)
94 EVT_MENU(MDI_NEW_WINDOW
, MyFrame::OnNewWindow
)
95 EVT_SIZE(MyFrame::OnSize
)
96 EVT_MENU(MDI_QUIT
, MyFrame::OnQuit
)
99 // Define my frame constructor
100 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
102 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
104 textWindow
= new wxTextCtrl(this, -1, "", wxDefaultPosition
, wxDefaultSize
,
105 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
106 textWindow
->SetValue("A help window");
109 toolBar
= new TestRibbon(this, 0, 0, 100, 30, wxNO_BORDER
, wxVERTICAL
, 1);
114 void MyFrame::OnQuit(wxCommandEvent
& event
)
119 void MyFrame::OnAbout(wxCommandEvent
& event
)
121 (void)wxMessageBox("wxWindows 2.0 MDI Demo\nAuthor: Julian Smart (c) 1997\nUsage: mdi.exe", "About MDI Demo");
124 void MyFrame::OnNewWindow(wxCommandEvent
& event
)
126 // Make another frame, containing a canvas
127 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
131 sprintf(titleBuf
, "Canvas Frame %d", winNumber
);
132 subframe
->SetTitle(titleBuf
);
135 // Give it an icon (this is ignored in MDI mode: uses resources)
137 subframe
->SetIcon(wxIcon("chrt_icn"));
140 subframe
->SetIcon(wxIcon("aiai.xbm"));
143 // Give it a status line
144 subframe
->CreateStatusBar();
147 wxMenu
*file_menu
= new wxMenu
;
149 file_menu
->Append(MDI_NEW_WINDOW
, "&New window");
150 file_menu
->Append(MDI_CHILD_QUIT
, "&Close child");
151 file_menu
->Append(MDI_QUIT
, "&Exit");
153 wxMenu
*option_menu
= new wxMenu
;
156 option_menu
->Append(MDI_REFRESH
, "&Refresh picture");
158 wxMenu
*help_menu
= new wxMenu
;
159 help_menu
->Append(MDI_ABOUT
, "&About");
161 wxMenuBar
*menu_bar
= new wxMenuBar
;
163 menu_bar
->Append(file_menu
, "&File");
164 menu_bar
->Append(option_menu
, "&Options");
165 menu_bar
->Append(help_menu
, "&Help");
167 // Associate the menu bar with the frame
168 subframe
->SetMenuBar(menu_bar
);
171 subframe
->GetClientSize(&width
, &height
);
172 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
173 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
174 subframe
->canvas
= canvas
;
176 // Give it scrollbars
177 canvas
->SetScrollbars(20, 20, 50, 50);
179 subframe
->Show(TRUE
);
182 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
183 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
186 // Define a constructor for my canvas
187 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
188 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
192 // Define the repainting behaviour
193 void MyCanvas::OnDraw(wxDC
& dc
)
195 dc
.SetFont(*wxSWISS_FONT
);
196 dc
.SetPen(*wxGREEN_PEN
);
197 dc
.DrawLine(0, 0, 200, 200);
198 dc
.DrawLine(200, 0, 0, 200);
200 dc
.SetBrush(*wxCYAN_BRUSH
);
201 dc
.SetPen(*wxRED_PEN
);
202 dc
.DrawRectangle(100, 100, 100, 50);
203 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
205 dc
.DrawEllipse(250, 250, 100, 50);
206 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
207 dc
.DrawLine(50, 230, 200, 230);
208 dc
.DrawText("This is a test string", 50, 230);
211 points
[0].x
= 200; points
[0].y
= 300;
212 points
[1].x
= 100; points
[1].y
= 400;
213 points
[2].x
= 300; points
[2].y
= 400;
215 dc
.DrawPolygon(3, points
);
218 // This implements a tiny doodling program! Drag the mouse using
220 void MyCanvas::OnEvent(wxMouseEvent
& event
)
225 wxPoint
pt(event
.GetLogicalPosition(dc
));
227 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
229 dc
.SetPen(*wxBLACK_PEN
);
230 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
236 // Define the behaviour for the frame closing
237 // - must delete all frames except for the main one.
238 bool MyFrame::OnClose(void)
240 // Must delete children
241 wxNode
*node
= my_children
.First();
244 MyChild
*child
= (MyChild
*)node
->Data();
245 wxNode
*next
= node
->Next();
253 void MyFrame::OnSize(wxSizeEvent
& event
)
256 GetClientSize(&w
, &h
);
261 wxWindow
* tbar
= GetToolBar();
264 tbar
->GetSize(&tw
, &th
);
265 tbar
->SetSize(w
, th
);
269 textWindow
->SetSize(0, th
, 200, h
-th
);
270 GetClientWindow()->SetSize(200, th
, w
- 200, h
-th
);
273 // Note that MDI_NEW_WINDOW and MDI_ABOUT commands get passed
274 // to the parent window for processing, so no need to
275 // duplicate event handlers here.
277 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
278 EVT_SIZE( MyChild::OnSize
)
279 EVT_MENU(MDI_CHILD_QUIT
, MyChild::OnQuit
)
282 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
284 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
287 my_children
.Append(this);
290 MyChild::~MyChild(void)
292 my_children
.DeleteObject(this);
295 void MyChild::OnSize(wxSizeEvent
& WXUNUSED(event
))
299 GetClientSize( &x
, &y
);
300 if (canvas
) canvas
->SetSize( x
, y
);
303 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
308 void MyChild::OnActivate(wxActivateEvent
& event
)
310 if (event
.GetActive() && canvas
)
314 bool MyChild::OnClose(void)
321 BEGIN_EVENT_TABLE(TestRibbon
, wxToolBar
)
322 EVT_PAINT(TestRibbon::OnPaint
)
325 TestRibbon::TestRibbon(wxFrame
*frame
, int x
, int y
, int w
, int h
,
326 long style
, int direction
, int RowsOrColumns
):
327 wxToolBar(frame
, -1, wxPoint(x
, y
), wxSize(w
, h
), style
, direction
, RowsOrColumns
)
329 wxBitmap
* bitmaps
[8];
331 bitmaps
[0] = new wxBitmap("icon1", wxBITMAP_TYPE_RESOURCE
);
332 bitmaps
[1] = new wxBitmap("icon2", wxBITMAP_TYPE_RESOURCE
);
333 bitmaps
[2] = new wxBitmap("icon3", wxBITMAP_TYPE_RESOURCE
);
334 bitmaps
[3] = new wxBitmap("icon4", wxBITMAP_TYPE_RESOURCE
);
335 bitmaps
[4] = new wxBitmap("icon5", wxBITMAP_TYPE_RESOURCE
);
336 bitmaps
[5] = new wxBitmap("icon6", wxBITMAP_TYPE_RESOURCE
);
337 bitmaps
[6] = new wxBitmap("icon7", wxBITMAP_TYPE_RESOURCE
);
338 bitmaps
[7] = new wxBitmap("icon8", wxBITMAP_TYPE_RESOURCE
);
348 AddTool(0, *bitmaps
[0], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "New file");
349 currentX
+= width
+ 5;
350 AddTool(1, *bitmaps
[1], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Open file");
351 currentX
+= width
+ 5;
352 AddTool(2, *bitmaps
[2], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Save file");
353 currentX
+= width
+ 5;
355 AddTool(3, *bitmaps
[3], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Copy");
356 currentX
+= width
+ 5;
357 AddTool(4, *bitmaps
[4], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Cut");
358 currentX
+= width
+ 5;
359 AddTool(5, *bitmaps
[5], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Paste");
360 currentX
+= width
+ 5;
362 AddTool(6, *bitmaps
[6], wxNullBitmap
, FALSE
, currentX
, -1, NULL
, "Print");
363 currentX
+= width
+ 5;
365 AddTool(7, *bitmaps
[7], wxNullBitmap
, TRUE
, currentX
, -1, NULL
, "Help");
370 for (i
= 0; i
< 8; i
++)
374 bool TestRibbon::OnLeftClick(int toolIndex
, bool toggled
)
377 sprintf(buf
, "Clicked on tool %d", toolIndex
);
378 frame
->SetStatusText(buf
);
382 void TestRibbon::OnMouseEnter(int toolIndex
)
387 sprintf(buf
, "This is tool number %d", toolIndex
);
388 frame
->SetStatusText(buf
);
390 else frame
->SetStatusText("");
393 void TestRibbon::OnPaint(wxPaintEvent
& event
)
395 wxToolBar::OnPaint(event
);
401 dc
.SetPen(*wxBLACK_PEN
);
402 dc
.SetBrush(*wxTRANSPARENT_BRUSH
);
403 dc
.DrawLine(0, h
-1, w
, h
-1);