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 if ( !wxApp::OnInit() )
46 // Create the main frame window
48 frame
= new MyFrame(NULL
, wxID_ANY
, _T("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
49 wxDEFAULT_FRAME_STYLE
|
50 wxNO_FULL_REPAINT_ON_RESIZE
|
51 wxHSCROLL
| wxVSCROLL
);
53 // Give it an icon (this is ignored in MDI mode: uses resources)
55 frame
->SetIcon(wxIcon(_T("sashtest_icn")));
59 wxMenu
*file_menu
= new wxMenu
;
61 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
62 file_menu
->Append(SASHTEST_TOGGLE_WINDOW
, _T("&Toggle window"));
63 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
65 wxMenu
*help_menu
= new wxMenu
;
66 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
68 wxMenuBar
*menu_bar
= new wxMenuBar
;
70 menu_bar
->Append(file_menu
, _T("&File"));
71 menu_bar
->Append(help_menu
, _T("&Help"));
73 // Associate the menu bar with the frame
74 frame
->SetMenuBar(menu_bar
);
77 frame
->CreateStatusBar();
78 #endif // wxUSE_STATUSBAR
87 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
88 EVT_MENU(SASHTEST_ABOUT
, MyFrame::OnAbout
)
89 EVT_MENU(SASHTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
90 EVT_SIZE(MyFrame::OnSize
)
91 EVT_MENU(SASHTEST_QUIT
, MyFrame::OnQuit
)
92 EVT_MENU(SASHTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
93 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnSashDrag
)
97 // Define my frame constructor
98 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
100 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
102 // Create some dummy layout windows
104 // A window like a toolbar
105 wxSashLayoutWindow
* win
=
106 new wxSashLayoutWindow(this, ID_WINDOW_TOP
,
107 wxDefaultPosition
, wxSize(200, 30),
108 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
110 win
->SetDefaultSize(wxSize(1000, 30));
111 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
112 win
->SetAlignment(wxLAYOUT_TOP
);
113 win
->SetBackgroundColour(wxColour(255, 0, 0));
114 win
->SetSashVisible(wxSASH_BOTTOM
, true);
118 // A window like a statusbar
119 win
= new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM
,
120 wxDefaultPosition
, wxSize(200, 30),
121 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
122 win
->SetDefaultSize(wxSize(1000, 30));
123 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
124 win
->SetAlignment(wxLAYOUT_BOTTOM
);
125 win
->SetBackgroundColour(wxColour(0, 0, 255));
126 win
->SetSashVisible(wxSASH_TOP
, true);
128 m_bottomWindow
= win
;
130 // A window to the left of the client window
131 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
,
132 wxDefaultPosition
, wxSize(200, 30),
133 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
134 win
->SetDefaultSize(wxSize(120, 1000));
135 win
->SetOrientation(wxLAYOUT_VERTICAL
);
136 win
->SetAlignment(wxLAYOUT_LEFT
);
137 win
->SetBackgroundColour(wxColour(0, 255, 0));
138 win
->SetSashVisible(wxSASH_RIGHT
, true);
139 win
->SetExtraBorderSize(10);
141 wxTextCtrl
* textWindow
= new wxTextCtrl(win
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
,
142 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
143 // wxTE_MULTILINE|wxNO_BORDER);
144 textWindow
->SetValue(_T("A help window"));
148 // Another window to the left of the client window
149 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT2
,
150 wxDefaultPosition
, wxSize(200, 30),
151 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
152 win
->SetDefaultSize(wxSize(120, 1000));
153 win
->SetOrientation(wxLAYOUT_VERTICAL
);
154 win
->SetAlignment(wxLAYOUT_LEFT
);
155 win
->SetBackgroundColour(wxColour(0, 255, 255));
156 win
->SetSashVisible(wxSASH_RIGHT
, true);
161 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
166 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
168 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
171 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
173 if (m_leftWindow1
->IsShown())
175 m_leftWindow1
->Show(false);
179 m_leftWindow1
->Show(true);
181 #if wxUSE_MDI_ARCHITECTURE
182 wxLayoutAlgorithm layout
;
183 layout
.LayoutMDIFrame(this);
184 #endif // wxUSE_MDI_ARCHITECTURE
187 void MyFrame::OnSashDrag(wxSashEvent
& event
)
189 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
192 switch (event
.GetId())
196 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
199 case ID_WINDOW_LEFT1
:
201 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
204 case ID_WINDOW_LEFT2
:
206 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
209 case ID_WINDOW_BOTTOM
:
211 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
216 #if wxUSE_MDI_ARCHITECTURE
217 wxLayoutAlgorithm layout
;
218 layout
.LayoutMDIFrame(this);
219 #endif // wxUSE_MDI_ARCHITECTURE
221 // Leaves bits of itself behind sometimes
222 GetClientWindow()->Refresh();
225 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
227 // Make another frame, containing a canvas
228 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"),
229 wxPoint(10, 10), wxSize(300, 300),
230 wxDEFAULT_FRAME_STYLE
|
231 wxNO_FULL_REPAINT_ON_RESIZE
);
233 subframe
->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber
));
236 // Give it an icon (this is ignored in MDI mode: uses resources)
238 subframe
->SetIcon(wxIcon(_T("sashtest_icn")));
242 // Give it a status line
243 subframe
->CreateStatusBar();
244 #endif // wxUSE_STATUSBAR
247 wxMenu
*file_menu
= new wxMenu
;
249 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
250 file_menu
->Append(SASHTEST_CHILD_QUIT
, _T("&Close child"));
251 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
253 wxMenu
*option_menu
= new wxMenu
;
256 option_menu
->Append(SASHTEST_REFRESH
, _T("&Refresh picture"));
258 wxMenu
*help_menu
= new wxMenu
;
259 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
261 wxMenuBar
*menu_bar
= new wxMenuBar
;
263 menu_bar
->Append(file_menu
, _T("&File"));
264 menu_bar
->Append(option_menu
, _T("&Options"));
265 menu_bar
->Append(help_menu
, _T("&Help"));
267 // Associate the menu bar with the frame
268 subframe
->SetMenuBar(menu_bar
);
271 subframe
->GetClientSize(&width
, &height
);
272 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
273 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
274 subframe
->canvas
= canvas
;
276 // Give it scrollbars
277 canvas
->SetScrollbars(20, 20, 50, 50);
279 subframe
->Show(true);
282 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
283 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
286 // Define a constructor for my canvas
287 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
288 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
289 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
291 SetBackgroundColour(* wxWHITE
);
294 // Define the repainting behaviour
295 void MyCanvas::OnDraw(wxDC
& dc
)
297 dc
.SetFont(*wxSWISS_FONT
);
298 dc
.SetPen(*wxGREEN_PEN
);
299 dc
.DrawLine(0, 0, 200, 200);
300 dc
.DrawLine(200, 0, 0, 200);
302 dc
.SetBrush(*wxCYAN_BRUSH
);
303 dc
.SetPen(*wxRED_PEN
);
304 dc
.DrawRectangle(100, 100, 100, 50);
305 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
307 dc
.DrawEllipse(250, 250, 100, 50);
309 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
310 #endif // wxUSE_SPLINES
311 dc
.DrawLine(50, 230, 200, 230);
312 dc
.DrawText(_T("This is a test string"), 50, 230);
315 points
[0].x
= 200; points
[0].y
= 300;
316 points
[1].x
= 100; points
[1].y
= 400;
317 points
[2].x
= 300; points
[2].y
= 400;
319 dc
.DrawPolygon(3, points
);
322 // This implements a tiny doodling program! Drag the mouse using
324 void MyCanvas::OnEvent(wxMouseEvent
& event
)
329 wxPoint
pt(event
.GetLogicalPosition(dc
));
331 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
333 dc
.SetPen(*wxBLACK_PEN
);
334 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
340 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
342 #if wxUSE_MDI_ARCHITECTURE
343 wxLayoutAlgorithm layout
;
344 layout
.LayoutMDIFrame(this);
345 #endif // wxUSE_MDI_ARCHITECTURE
348 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
349 // to the parent window for processing, so no need to
350 // duplicate event handlers here.
352 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
353 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
356 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
358 wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
361 my_children
.Append(this);
364 MyChild::~MyChild(void)
366 my_children
.DeleteObject(this);
369 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
374 void MyChild::OnActivate(wxActivateEvent
& event
)
376 if (event
.GetActive() && canvas
)