1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout/sash sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
24 #include "wx/toolbar.h"
25 #include "wx/laywin.h"
29 MyFrame
*frame
= NULL
;
34 // For drawing lines in a canvas
40 // Initialise this in OnInit, not statically
41 bool MyApp::OnInit(void)
43 // Create the main frame window
45 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
46 wxDEFAULT_FRAME_STYLE
|
47 wxNO_FULL_REPAINT_ON_RESIZE
|
48 wxHSCROLL
| wxVSCROLL
);
50 // Give it an icon (this is ignored in MDI mode: uses resources)
52 frame
->SetIcon(wxIcon(_T("sashtest_icn")));
56 wxMenu
*file_menu
= new wxMenu
;
58 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
59 file_menu
->Append(SASHTEST_TOGGLE_WINDOW
, _T("&Toggle window"));
60 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
62 wxMenu
*help_menu
= new wxMenu
;
63 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
65 wxMenuBar
*menu_bar
= new wxMenuBar
;
67 menu_bar
->Append(file_menu
, _T("&File"));
68 menu_bar
->Append(help_menu
, _T("&Help"));
70 // Associate the menu bar with the frame
71 frame
->SetMenuBar(menu_bar
);
73 frame
->CreateStatusBar();
82 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
83 EVT_MENU(SASHTEST_ABOUT
, MyFrame::OnAbout
)
84 EVT_MENU(SASHTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
85 EVT_SIZE(MyFrame::OnSize
)
86 EVT_MENU(SASHTEST_QUIT
, MyFrame::OnQuit
)
87 EVT_MENU(SASHTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
88 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnSashDrag
)
92 // Define my frame constructor
93 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
95 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
97 // Create some dummy layout windows
99 // A window like a toolbar
100 wxSashLayoutWindow
* win
=
101 new wxSashLayoutWindow(this, ID_WINDOW_TOP
,
102 wxDefaultPosition
, wxSize(200, 30),
103 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
105 win
->SetDefaultSize(wxSize(1000, 30));
106 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
107 win
->SetAlignment(wxLAYOUT_TOP
);
108 win
->SetBackgroundColour(wxColour(255, 0, 0));
109 win
->SetSashVisible(wxSASH_BOTTOM
, true);
113 // A window like a statusbar
114 win
= new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM
,
115 wxDefaultPosition
, wxSize(200, 30),
116 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
117 win
->SetDefaultSize(wxSize(1000, 30));
118 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
119 win
->SetAlignment(wxLAYOUT_BOTTOM
);
120 win
->SetBackgroundColour(wxColour(0, 0, 255));
121 win
->SetSashVisible(wxSASH_TOP
, true);
123 m_bottomWindow
= win
;
125 // A window to the left of the client window
126 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
,
127 wxDefaultPosition
, wxSize(200, 30),
128 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
129 win
->SetDefaultSize(wxSize(120, 1000));
130 win
->SetOrientation(wxLAYOUT_VERTICAL
);
131 win
->SetAlignment(wxLAYOUT_LEFT
);
132 win
->SetBackgroundColour(wxColour(0, 255, 0));
133 win
->SetSashVisible(wxSASH_RIGHT
, true);
134 win
->SetExtraBorderSize(10);
136 wxTextCtrl
* textWindow
= new wxTextCtrl(win
, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
,
137 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
138 // wxTE_MULTILINE|wxNO_BORDER);
139 textWindow
->SetValue(_T("A help window"));
143 // Another window to the left of the client window
144 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT2
,
145 wxDefaultPosition
, wxSize(200, 30),
146 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
147 win
->SetDefaultSize(wxSize(120, 1000));
148 win
->SetOrientation(wxLAYOUT_VERTICAL
);
149 win
->SetAlignment(wxLAYOUT_LEFT
);
150 win
->SetBackgroundColour(wxColour(0, 255, 255));
151 win
->SetSashVisible(wxSASH_RIGHT
, true);
156 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
161 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
163 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
166 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
168 if (m_leftWindow1
->IsShown())
170 m_leftWindow1
->Show(false);
174 m_leftWindow1
->Show(true);
176 wxLayoutAlgorithm layout
;
177 layout
.LayoutMDIFrame(this);
180 void MyFrame::OnSashDrag(wxSashEvent
& event
)
182 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
185 switch (event
.GetId())
189 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
192 case ID_WINDOW_LEFT1
:
194 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
197 case ID_WINDOW_LEFT2
:
199 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
202 case ID_WINDOW_BOTTOM
:
204 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
208 wxLayoutAlgorithm layout
;
209 layout
.LayoutMDIFrame(this);
211 // Leaves bits of itself behind sometimes
212 GetClientWindow()->Refresh();
215 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
217 // Make another frame, containing a canvas
218 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"),
219 wxPoint(10, 10), wxSize(300, 300),
220 wxDEFAULT_FRAME_STYLE
|
221 wxNO_FULL_REPAINT_ON_RESIZE
);
223 subframe
->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber
));
226 // Give it an icon (this is ignored in MDI mode: uses resources)
228 subframe
->SetIcon(wxIcon(_T("sashtest_icn")));
231 // Give it a status line
232 subframe
->CreateStatusBar();
235 wxMenu
*file_menu
= new wxMenu
;
237 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
238 file_menu
->Append(SASHTEST_CHILD_QUIT
, _T("&Close child"));
239 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
241 wxMenu
*option_menu
= new wxMenu
;
244 option_menu
->Append(SASHTEST_REFRESH
, _T("&Refresh picture"));
246 wxMenu
*help_menu
= new wxMenu
;
247 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
249 wxMenuBar
*menu_bar
= new wxMenuBar
;
251 menu_bar
->Append(file_menu
, _T("&File"));
252 menu_bar
->Append(option_menu
, _T("&Options"));
253 menu_bar
->Append(help_menu
, _T("&Help"));
255 // Associate the menu bar with the frame
256 subframe
->SetMenuBar(menu_bar
);
259 subframe
->GetClientSize(&width
, &height
);
260 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
261 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
262 subframe
->canvas
= canvas
;
264 // Give it scrollbars
265 canvas
->SetScrollbars(20, 20, 50, 50);
267 subframe
->Show(true);
270 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
271 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
274 // Define a constructor for my canvas
275 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
276 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
277 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
279 SetBackgroundColour(* wxWHITE
);
282 // Define the repainting behaviour
283 void MyCanvas::OnDraw(wxDC
& dc
)
285 dc
.SetFont(*wxSWISS_FONT
);
286 dc
.SetPen(*wxGREEN_PEN
);
287 dc
.DrawLine(0, 0, 200, 200);
288 dc
.DrawLine(200, 0, 0, 200);
290 dc
.SetBrush(*wxCYAN_BRUSH
);
291 dc
.SetPen(*wxRED_PEN
);
292 dc
.DrawRectangle(100, 100, 100, 50);
293 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
295 dc
.DrawEllipse(250, 250, 100, 50);
297 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
298 #endif // wxUSE_SPLINES
299 dc
.DrawLine(50, 230, 200, 230);
300 dc
.DrawText(_T("This is a test string"), 50, 230);
303 points
[0].x
= 200; points
[0].y
= 300;
304 points
[1].x
= 100; points
[1].y
= 400;
305 points
[2].x
= 300; points
[2].y
= 400;
307 dc
.DrawPolygon(3, points
);
310 // This implements a tiny doodling program! Drag the mouse using
312 void MyCanvas::OnEvent(wxMouseEvent
& event
)
317 wxPoint
pt(event
.GetLogicalPosition(dc
));
319 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
321 dc
.SetPen(*wxBLACK_PEN
);
322 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
328 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
330 wxLayoutAlgorithm layout
;
331 layout
.LayoutMDIFrame(this);
334 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
335 // to the parent window for processing, so no need to
336 // duplicate event handlers here.
338 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
339 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
342 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
344 wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
347 my_children
.Append(this);
350 MyChild::~MyChild(void)
352 my_children
.DeleteObject(this);
355 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
360 void MyChild::OnActivate(wxActivateEvent
& event
)
362 if (event
.GetActive() && canvas
)