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
);
74 frame
->CreateStatusBar();
75 #endif // wxUSE_STATUSBAR
84 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
85 EVT_MENU(SASHTEST_ABOUT
, MyFrame::OnAbout
)
86 EVT_MENU(SASHTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
87 EVT_SIZE(MyFrame::OnSize
)
88 EVT_MENU(SASHTEST_QUIT
, MyFrame::OnQuit
)
89 EVT_MENU(SASHTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
90 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnSashDrag
)
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 // Create some dummy layout windows
101 // A window like a toolbar
102 wxSashLayoutWindow
* win
=
103 new wxSashLayoutWindow(this, ID_WINDOW_TOP
,
104 wxDefaultPosition
, wxSize(200, 30),
105 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
107 win
->SetDefaultSize(wxSize(1000, 30));
108 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
109 win
->SetAlignment(wxLAYOUT_TOP
);
110 win
->SetBackgroundColour(wxColour(255, 0, 0));
111 win
->SetSashVisible(wxSASH_BOTTOM
, true);
115 // A window like a statusbar
116 win
= new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM
,
117 wxDefaultPosition
, wxSize(200, 30),
118 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
119 win
->SetDefaultSize(wxSize(1000, 30));
120 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
121 win
->SetAlignment(wxLAYOUT_BOTTOM
);
122 win
->SetBackgroundColour(wxColour(0, 0, 255));
123 win
->SetSashVisible(wxSASH_TOP
, true);
125 m_bottomWindow
= win
;
127 // A window to the left of the client window
128 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
,
129 wxDefaultPosition
, wxSize(200, 30),
130 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
131 win
->SetDefaultSize(wxSize(120, 1000));
132 win
->SetOrientation(wxLAYOUT_VERTICAL
);
133 win
->SetAlignment(wxLAYOUT_LEFT
);
134 win
->SetBackgroundColour(wxColour(0, 255, 0));
135 win
->SetSashVisible(wxSASH_RIGHT
, true);
136 win
->SetExtraBorderSize(10);
138 wxTextCtrl
* textWindow
= new wxTextCtrl(win
, wxID_ANY
, _T(""), wxDefaultPosition
, wxDefaultSize
,
139 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
140 // wxTE_MULTILINE|wxNO_BORDER);
141 textWindow
->SetValue(_T("A help window"));
145 // Another window to the left of the client window
146 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT2
,
147 wxDefaultPosition
, wxSize(200, 30),
148 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
149 win
->SetDefaultSize(wxSize(120, 1000));
150 win
->SetOrientation(wxLAYOUT_VERTICAL
);
151 win
->SetAlignment(wxLAYOUT_LEFT
);
152 win
->SetBackgroundColour(wxColour(0, 255, 255));
153 win
->SetSashVisible(wxSASH_RIGHT
, true);
158 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
163 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
165 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
168 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
170 if (m_leftWindow1
->IsShown())
172 m_leftWindow1
->Show(false);
176 m_leftWindow1
->Show(true);
178 wxLayoutAlgorithm layout
;
179 layout
.LayoutMDIFrame(this);
182 void MyFrame::OnSashDrag(wxSashEvent
& event
)
184 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
187 switch (event
.GetId())
191 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
194 case ID_WINDOW_LEFT1
:
196 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
199 case ID_WINDOW_LEFT2
:
201 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
204 case ID_WINDOW_BOTTOM
:
206 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
210 wxLayoutAlgorithm layout
;
211 layout
.LayoutMDIFrame(this);
213 // Leaves bits of itself behind sometimes
214 GetClientWindow()->Refresh();
217 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
219 // Make another frame, containing a canvas
220 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"),
221 wxPoint(10, 10), wxSize(300, 300),
222 wxDEFAULT_FRAME_STYLE
|
223 wxNO_FULL_REPAINT_ON_RESIZE
);
225 subframe
->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber
));
228 // Give it an icon (this is ignored in MDI mode: uses resources)
230 subframe
->SetIcon(wxIcon(_T("sashtest_icn")));
234 // Give it a status line
235 subframe
->CreateStatusBar();
236 #endif // wxUSE_STATUSBAR
239 wxMenu
*file_menu
= new wxMenu
;
241 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
242 file_menu
->Append(SASHTEST_CHILD_QUIT
, _T("&Close child"));
243 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
245 wxMenu
*option_menu
= new wxMenu
;
248 option_menu
->Append(SASHTEST_REFRESH
, _T("&Refresh picture"));
250 wxMenu
*help_menu
= new wxMenu
;
251 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
253 wxMenuBar
*menu_bar
= new wxMenuBar
;
255 menu_bar
->Append(file_menu
, _T("&File"));
256 menu_bar
->Append(option_menu
, _T("&Options"));
257 menu_bar
->Append(help_menu
, _T("&Help"));
259 // Associate the menu bar with the frame
260 subframe
->SetMenuBar(menu_bar
);
263 subframe
->GetClientSize(&width
, &height
);
264 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
265 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
266 subframe
->canvas
= canvas
;
268 // Give it scrollbars
269 canvas
->SetScrollbars(20, 20, 50, 50);
271 subframe
->Show(true);
274 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
275 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
278 // Define a constructor for my canvas
279 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
280 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
281 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
283 SetBackgroundColour(* wxWHITE
);
286 // Define the repainting behaviour
287 void MyCanvas::OnDraw(wxDC
& dc
)
289 dc
.SetFont(*wxSWISS_FONT
);
290 dc
.SetPen(*wxGREEN_PEN
);
291 dc
.DrawLine(0, 0, 200, 200);
292 dc
.DrawLine(200, 0, 0, 200);
294 dc
.SetBrush(*wxCYAN_BRUSH
);
295 dc
.SetPen(*wxRED_PEN
);
296 dc
.DrawRectangle(100, 100, 100, 50);
297 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
299 dc
.DrawEllipse(250, 250, 100, 50);
301 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
302 #endif // wxUSE_SPLINES
303 dc
.DrawLine(50, 230, 200, 230);
304 dc
.DrawText(_T("This is a test string"), 50, 230);
307 points
[0].x
= 200; points
[0].y
= 300;
308 points
[1].x
= 100; points
[1].y
= 400;
309 points
[2].x
= 300; points
[2].y
= 400;
311 dc
.DrawPolygon(3, points
);
314 // This implements a tiny doodling program! Drag the mouse using
316 void MyCanvas::OnEvent(wxMouseEvent
& event
)
321 wxPoint
pt(event
.GetLogicalPosition(dc
));
323 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
325 dc
.SetPen(*wxBLACK_PEN
);
326 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
332 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
334 wxLayoutAlgorithm layout
;
335 layout
.LayoutMDIFrame(this);
338 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
339 // to the parent window for processing, so no need to
340 // duplicate event handlers here.
342 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
343 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
346 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
348 wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
351 my_children
.Append(this);
354 MyChild::~MyChild(void)
356 my_children
.DeleteObject(this);
359 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
364 void MyChild::OnActivate(wxActivateEvent
& event
)
366 if (event
.GetActive() && canvas
)