1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout/sash sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
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
, wxT("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(wxT("sashtest_icn")));
59 wxMenu
*file_menu
= new wxMenu
;
61 file_menu
->Append(SASHTEST_NEW_WINDOW
, wxT("&New window"));
62 file_menu
->Append(SASHTEST_TOGGLE_WINDOW
, wxT("&Toggle window"));
63 file_menu
->Append(SASHTEST_QUIT
, wxT("&Exit"));
65 wxMenu
*help_menu
= new wxMenu
;
66 help_menu
->Append(SASHTEST_ABOUT
, wxT("&About"));
68 wxMenuBar
*menu_bar
= new wxMenuBar
;
70 menu_bar
->Append(file_menu
, wxT("&File"));
71 menu_bar
->Append(help_menu
, wxT("&Help"));
73 // Associate the menu bar with the frame
74 frame
->SetMenuBar(menu_bar
);
77 frame
->CreateStatusBar();
78 #endif // wxUSE_STATUSBAR
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
, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, wxDefaultSize
,
140 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
141 // wxTE_MULTILINE|wxNO_BORDER);
142 textWindow
->SetValue(wxT("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(wxT("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), wxT("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 #if wxUSE_MDI_ARCHITECTURE
180 wxLayoutAlgorithm layout
;
181 layout
.LayoutMDIFrame(this);
182 #endif // wxUSE_MDI_ARCHITECTURE
185 void MyFrame::OnSashDrag(wxSashEvent
& event
)
187 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
190 switch (event
.GetId())
194 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
197 case ID_WINDOW_LEFT1
:
199 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
202 case ID_WINDOW_LEFT2
:
204 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
207 case ID_WINDOW_BOTTOM
:
209 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
214 #if wxUSE_MDI_ARCHITECTURE
215 wxLayoutAlgorithm layout
;
216 layout
.LayoutMDIFrame(this);
217 #endif // wxUSE_MDI_ARCHITECTURE
219 // Leaves bits of itself behind sometimes
220 GetClientWindow()->Refresh();
223 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
225 // Make another frame, containing a canvas
226 MyChild
*subframe
= new MyChild(frame
, wxT("Canvas Frame"),
227 wxPoint(10, 10), wxSize(300, 300),
228 wxDEFAULT_FRAME_STYLE
|
229 wxNO_FULL_REPAINT_ON_RESIZE
);
231 subframe
->SetTitle(wxString::Format(wxT("Canvas Frame %d"), winNumber
));
234 // Give it an icon (this is ignored in MDI mode: uses resources)
236 subframe
->SetIcon(wxIcon(wxT("sashtest_icn")));
240 // Give it a status line
241 subframe
->CreateStatusBar();
242 #endif // wxUSE_STATUSBAR
245 wxMenu
*file_menu
= new wxMenu
;
247 file_menu
->Append(SASHTEST_NEW_WINDOW
, wxT("&New window"));
248 file_menu
->Append(SASHTEST_CHILD_QUIT
, wxT("&Close child"));
249 file_menu
->Append(SASHTEST_QUIT
, wxT("&Exit"));
251 wxMenu
*option_menu
= new wxMenu
;
254 option_menu
->Append(SASHTEST_REFRESH
, wxT("&Refresh picture"));
256 wxMenu
*help_menu
= new wxMenu
;
257 help_menu
->Append(SASHTEST_ABOUT
, wxT("&About"));
259 wxMenuBar
*menu_bar
= new wxMenuBar
;
261 menu_bar
->Append(file_menu
, wxT("&File"));
262 menu_bar
->Append(option_menu
, wxT("&Options"));
263 menu_bar
->Append(help_menu
, wxT("&Help"));
265 // Associate the menu bar with the frame
266 subframe
->SetMenuBar(menu_bar
);
269 subframe
->GetClientSize(&width
, &height
);
270 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
271 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
272 subframe
->canvas
= canvas
;
274 // Give it scrollbars
275 canvas
->SetScrollbars(20, 20, 50, 50);
277 subframe
->Show(true);
280 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
281 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
284 // Define a constructor for my canvas
285 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
)
286 : wxScrolledWindow(parent
, wxID_ANY
, pos
, size
,
287 wxSUNKEN_BORDER
| wxNO_FULL_REPAINT_ON_RESIZE
)
289 SetBackgroundColour(* wxWHITE
);
292 // Define the repainting behaviour
293 void MyCanvas::OnDraw(wxDC
& dc
)
295 dc
.SetFont(*wxSWISS_FONT
);
296 dc
.SetPen(*wxGREEN_PEN
);
297 dc
.DrawLine(0, 0, 200, 200);
298 dc
.DrawLine(200, 0, 0, 200);
300 dc
.SetBrush(*wxCYAN_BRUSH
);
301 dc
.SetPen(*wxRED_PEN
);
302 dc
.DrawRectangle(100, 100, 100, 50);
303 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
305 dc
.DrawEllipse(250, 250, 100, 50);
307 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
308 #endif // wxUSE_SPLINES
309 dc
.DrawLine(50, 230, 200, 230);
310 dc
.DrawText(wxT("This is a test string"), 50, 230);
313 points
[0].x
= 200; points
[0].y
= 300;
314 points
[1].x
= 100; points
[1].y
= 400;
315 points
[2].x
= 300; points
[2].y
= 400;
317 dc
.DrawPolygon(3, points
);
320 // This implements a tiny doodling program! Drag the mouse using
322 void MyCanvas::OnEvent(wxMouseEvent
& event
)
327 wxPoint
pt(event
.GetLogicalPosition(dc
));
329 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
331 dc
.SetPen(*wxBLACK_PEN
);
332 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
338 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
340 #if wxUSE_MDI_ARCHITECTURE
341 wxLayoutAlgorithm layout
;
342 layout
.LayoutMDIFrame(this);
343 #endif // wxUSE_MDI_ARCHITECTURE
346 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
347 // to the parent window for processing, so no need to
348 // duplicate event handlers here.
350 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
351 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
354 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
356 wxMDIChildFrame(parent
, wxID_ANY
, title
, pos
, size
, style
)
359 my_children
.Append(this);
362 MyChild::~MyChild(void)
364 my_children
.DeleteObject(this);
367 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
372 void MyChild::OnActivate(wxActivateEvent
& event
)
374 if (event
.GetActive() && canvas
)