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
, -1, _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")));
55 frame
->SetIcon(wxIcon(_T("sashtest.xbm")));
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
);
76 frame
->CreateStatusBar();
85 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
86 EVT_MENU(SASHTEST_ABOUT
, MyFrame::OnAbout
)
87 EVT_MENU(SASHTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
88 EVT_SIZE(MyFrame::OnSize
)
89 EVT_MENU(SASHTEST_QUIT
, MyFrame::OnQuit
)
90 EVT_MENU(SASHTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
91 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnSashDrag
)
95 // Define my frame constructor
96 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
98 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
100 // Create some dummy layout windows
102 // A window like a toolbar
103 wxSashLayoutWindow
* win
=
104 new wxSashLayoutWindow(this, ID_WINDOW_TOP
,
105 wxDefaultPosition
, wxSize(200, 30),
106 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
108 win
->SetDefaultSize(wxSize(1000, 30));
109 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
110 win
->SetAlignment(wxLAYOUT_TOP
);
111 win
->SetBackgroundColour(wxColour(255, 0, 0));
112 win
->SetSashVisible(wxSASH_BOTTOM
, TRUE
);
116 // A window like a statusbar
117 win
= new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM
,
118 wxDefaultPosition
, wxSize(200, 30),
119 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
120 win
->SetDefaultSize(wxSize(1000, 30));
121 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
122 win
->SetAlignment(wxLAYOUT_BOTTOM
);
123 win
->SetBackgroundColour(wxColour(0, 0, 255));
124 win
->SetSashVisible(wxSASH_TOP
, TRUE
);
126 m_bottomWindow
= win
;
128 // A window to the left of the client window
129 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
,
130 wxDefaultPosition
, wxSize(200, 30),
131 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
132 win
->SetDefaultSize(wxSize(120, 1000));
133 win
->SetOrientation(wxLAYOUT_VERTICAL
);
134 win
->SetAlignment(wxLAYOUT_LEFT
);
135 win
->SetBackgroundColour(wxColour(0, 255, 0));
136 win
->SetSashVisible(wxSASH_RIGHT
, TRUE
);
137 win
->SetExtraBorderSize(10);
139 wxTextCtrl
* textWindow
= new wxTextCtrl(win
, -1, _T(""), wxDefaultPosition
, wxDefaultSize
,
140 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
141 // wxTE_MULTILINE|wxNO_BORDER);
142 textWindow
->SetValue(_T("A help window"));
146 // Another window to the left of the client window
147 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT2
,
148 wxDefaultPosition
, wxSize(200, 30),
149 wxNO_BORDER
| wxSW_3D
| wxCLIP_CHILDREN
);
150 win
->SetDefaultSize(wxSize(120, 1000));
151 win
->SetOrientation(wxLAYOUT_VERTICAL
);
152 win
->SetAlignment(wxLAYOUT_LEFT
);
153 win
->SetBackgroundColour(wxColour(0, 255, 255));
154 win
->SetSashVisible(wxSASH_RIGHT
, TRUE
);
159 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
164 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
166 (void)wxMessageBox(_T("wxWindows 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
169 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
171 if (m_leftWindow1
->IsShown())
173 m_leftWindow1
->Show(FALSE
);
177 m_leftWindow1
->Show(TRUE
);
179 wxLayoutAlgorithm layout
;
180 layout
.LayoutMDIFrame(this);
183 void MyFrame::OnSashDrag(wxSashEvent
& event
)
185 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
188 switch (event
.GetId())
192 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
195 case ID_WINDOW_LEFT1
:
197 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
200 case ID_WINDOW_LEFT2
:
202 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
205 case ID_WINDOW_BOTTOM
:
207 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
211 wxLayoutAlgorithm layout
;
212 layout
.LayoutMDIFrame(this);
214 // Leaves bits of itself behind sometimes
215 GetClientWindow()->Refresh();
218 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
220 // Make another frame, containing a canvas
221 MyChild
*subframe
= new MyChild(frame
, _T("Canvas Frame"),
222 wxPoint(10, 10), wxSize(300, 300),
223 wxDEFAULT_FRAME_STYLE
|
224 wxNO_FULL_REPAINT_ON_RESIZE
);
226 subframe
->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber
));
229 // Give it an icon (this is ignored in MDI mode: uses resources)
231 subframe
->SetIcon(wxIcon(_T("sashtest_icn")));
234 // Give it a status line
235 subframe
->CreateStatusBar();
238 wxMenu
*file_menu
= new wxMenu
;
240 file_menu
->Append(SASHTEST_NEW_WINDOW
, _T("&New window"));
241 file_menu
->Append(SASHTEST_CHILD_QUIT
, _T("&Close child"));
242 file_menu
->Append(SASHTEST_QUIT
, _T("&Exit"));
244 wxMenu
*option_menu
= new wxMenu
;
247 option_menu
->Append(SASHTEST_REFRESH
, _T("&Refresh picture"));
249 wxMenu
*help_menu
= new wxMenu
;
250 help_menu
->Append(SASHTEST_ABOUT
, _T("&About"));
252 wxMenuBar
*menu_bar
= new wxMenuBar
;
254 menu_bar
->Append(file_menu
, _T("&File"));
255 menu_bar
->Append(option_menu
, _T("&Options"));
256 menu_bar
->Append(help_menu
, _T("&Help"));
258 // Associate the menu bar with the frame
259 subframe
->SetMenuBar(menu_bar
);
262 subframe
->GetClientSize(&width
, &height
);
263 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
264 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
265 subframe
->canvas
= canvas
;
267 // Give it scrollbars
268 canvas
->SetScrollbars(20, 20, 50, 50);
270 subframe
->Show(TRUE
);
273 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
274 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
277 // Define a constructor for my canvas
278 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
279 : wxScrolledWindow(parent
, -1, pos
, size
,
280 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
282 SetBackgroundColour(* wxWHITE
);
285 // Define the repainting behaviour
286 void MyCanvas::OnDraw(wxDC
& dc
)
288 dc
.SetFont(*wxSWISS_FONT
);
289 dc
.SetPen(*wxGREEN_PEN
);
290 dc
.DrawLine(0, 0, 200, 200);
291 dc
.DrawLine(200, 0, 0, 200);
293 dc
.SetBrush(*wxCYAN_BRUSH
);
294 dc
.SetPen(*wxRED_PEN
);
295 dc
.DrawRectangle(100, 100, 100, 50);
296 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
298 dc
.DrawEllipse(250, 250, 100, 50);
300 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
301 #endif // wxUSE_SPLINES
302 dc
.DrawLine(50, 230, 200, 230);
303 dc
.DrawText(_T("This is a test string"), 50, 230);
306 points
[0].x
= 200; points
[0].y
= 300;
307 points
[1].x
= 100; points
[1].y
= 400;
308 points
[2].x
= 300; points
[2].y
= 400;
310 dc
.DrawPolygon(3, points
);
313 // This implements a tiny doodling program! Drag the mouse using
315 void MyCanvas::OnEvent(wxMouseEvent
& event
)
320 wxPoint
pt(event
.GetLogicalPosition(dc
));
322 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
324 dc
.SetPen(*wxBLACK_PEN
);
325 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
331 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
333 wxLayoutAlgorithm layout
;
334 layout
.LayoutMDIFrame(this);
337 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
338 // to the parent window for processing, so no need to
339 // duplicate event handlers here.
341 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
342 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
345 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
347 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
350 my_children
.Append(this);
353 MyChild::~MyChild(void)
355 my_children
.DeleteObject(this);
358 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
363 void MyChild::OnActivate(wxActivateEvent
& event
)
365 if (event
.GetActive() && canvas
)