1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Layout/sash sample
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart and Markus Holzem
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, "Sash Demo", wxPoint(0, 0), wxSize(500, 400),
46 wxDEFAULT_FRAME_STYLE
| wxHSCROLL
| wxVSCROLL
);
48 // Give it an icon (this is ignored in MDI mode: uses resources)
50 frame
->SetIcon(wxIcon("sashtest_icn"));
53 frame
->SetIcon(wxIcon("sashtest.xbm"));
57 wxMenu
*file_menu
= new wxMenu
;
59 file_menu
->Append(SASHTEST_NEW_WINDOW
, "&New window");
60 file_menu
->Append(SASHTEST_TOGGLE_WINDOW
, "&Toggle window");
61 file_menu
->Append(SASHTEST_QUIT
, "&Exit");
63 wxMenu
*help_menu
= new wxMenu
;
64 help_menu
->Append(SASHTEST_ABOUT
, "&About");
66 wxMenuBar
*menu_bar
= new wxMenuBar
;
68 menu_bar
->Append(file_menu
, "&File");
69 menu_bar
->Append(help_menu
, "&Help");
71 // Associate the menu bar with the frame
72 frame
->SetMenuBar(menu_bar
);
74 frame
->CreateStatusBar();
83 BEGIN_EVENT_TABLE(MyFrame
, wxMDIParentFrame
)
84 EVT_MENU(SASHTEST_ABOUT
, MyFrame::OnAbout
)
85 EVT_MENU(SASHTEST_NEW_WINDOW
, MyFrame::OnNewWindow
)
86 EVT_SIZE(MyFrame::OnSize
)
87 EVT_MENU(SASHTEST_QUIT
, MyFrame::OnQuit
)
88 EVT_MENU(SASHTEST_TOGGLE_WINDOW
, MyFrame::OnToggleWindow
)
89 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP
, ID_WINDOW_BOTTOM
, MyFrame::OnSashDrag
)
93 // Define my frame constructor
94 MyFrame::MyFrame(wxWindow
*parent
, const wxWindowID id
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
96 wxMDIParentFrame(parent
, id
, title
, pos
, size
, style
)
98 // Create some dummy layout windows
100 // A window like a toolbar
101 wxSashLayoutWindow
* win
= new wxSashLayoutWindow(this, ID_WINDOW_TOP
, wxDefaultPosition
, wxSize(200, 30), wxNO_BORDER
|wxSW_3D
);
102 win
->SetDefaultSize(wxSize(1000, 30));
103 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
104 win
->SetAlignment(wxLAYOUT_TOP
);
105 win
->SetBackgroundColour(wxColour(255, 0, 0));
106 win
->SetSashVisible(wxSASH_BOTTOM
, TRUE
);
110 // A window like a statusbar
111 win
= new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM
, wxDefaultPosition
, wxSize(200, 30), wxNO_BORDER
|wxSW_3D
);
112 win
->SetDefaultSize(wxSize(1000, 30));
113 win
->SetOrientation(wxLAYOUT_HORIZONTAL
);
114 win
->SetAlignment(wxLAYOUT_BOTTOM
);
115 win
->SetBackgroundColour(wxColour(0, 0, 255));
116 win
->SetSashVisible(wxSASH_TOP
, TRUE
);
118 m_bottomWindow
= win
;
120 // A window to the left of the client window
121 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT1
, wxDefaultPosition
, wxSize(200, 30), wxNO_BORDER
|wxSW_3D
);
122 win
->SetDefaultSize(wxSize(120, 1000));
123 win
->SetOrientation(wxLAYOUT_VERTICAL
);
124 win
->SetAlignment(wxLAYOUT_LEFT
);
125 win
->SetBackgroundColour(wxColour(0, 255, 0));
126 win
->SetSashVisible(wxSASH_RIGHT
, TRUE
);
127 win
->SetExtraBorderSize(10);
129 wxTextCtrl
* textWindow
= new wxTextCtrl(win
, -1, "", wxDefaultPosition
, wxDefaultSize
,
130 wxTE_MULTILINE
|wxSUNKEN_BORDER
);
131 // wxTE_MULTILINE|wxNO_BORDER);
132 textWindow
->SetValue("A help window");
136 // Another window to the left of the client window
137 win
= new wxSashLayoutWindow(this, ID_WINDOW_LEFT2
, wxDefaultPosition
, wxSize(200, 30), wxNO_BORDER
|wxSW_3D
);
138 win
->SetDefaultSize(wxSize(120, 1000));
139 win
->SetOrientation(wxLAYOUT_VERTICAL
);
140 win
->SetAlignment(wxLAYOUT_LEFT
);
141 win
->SetBackgroundColour(wxColour(0, 255, 255));
142 win
->SetSashVisible(wxSASH_RIGHT
, TRUE
);
147 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
152 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
154 (void)wxMessageBox("wxWindows 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998", "About Sash Demo");
157 void MyFrame::OnToggleWindow(wxCommandEvent
& WXUNUSED(event
))
159 if (m_leftWindow1
->IsShown())
161 m_leftWindow1
->Show(FALSE
);
165 m_leftWindow1
->Show(TRUE
);
167 wxLayoutAlgorithm layout
;
168 layout
.LayoutMDIFrame(this);
171 void MyFrame::OnSashDrag(wxSashEvent
& event
)
173 if (event
.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE
)
176 switch (event
.GetId())
180 m_topWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
183 case ID_WINDOW_LEFT1
:
185 m_leftWindow1
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
188 case ID_WINDOW_LEFT2
:
190 m_leftWindow2
->SetDefaultSize(wxSize(event
.GetDragRect().width
, 1000));
193 case ID_WINDOW_BOTTOM
:
195 m_bottomWindow
->SetDefaultSize(wxSize(1000, event
.GetDragRect().height
));
199 wxLayoutAlgorithm layout
;
200 layout
.LayoutMDIFrame(this);
202 // Leaves bits of itself behind sometimes
203 GetClientWindow()->Refresh();
206 void MyFrame::OnNewWindow(wxCommandEvent
& WXUNUSED(event
))
208 // Make another frame, containing a canvas
209 MyChild
*subframe
= new MyChild(frame
, "Canvas Frame", wxPoint(10, 10), wxSize(300, 300),
210 wxDEFAULT_FRAME_STYLE
);
213 sprintf(titleBuf
, "Canvas Frame %d", winNumber
);
214 subframe
->SetTitle(titleBuf
);
217 // Give it an icon (this is ignored in MDI mode: uses resources)
219 subframe
->SetIcon(wxIcon("sashtest_icn"));
222 // Give it a status line
223 subframe
->CreateStatusBar();
226 wxMenu
*file_menu
= new wxMenu
;
228 file_menu
->Append(SASHTEST_NEW_WINDOW
, "&New window");
229 file_menu
->Append(SASHTEST_CHILD_QUIT
, "&Close child");
230 file_menu
->Append(SASHTEST_QUIT
, "&Exit");
232 wxMenu
*option_menu
= new wxMenu
;
235 option_menu
->Append(SASHTEST_REFRESH
, "&Refresh picture");
237 wxMenu
*help_menu
= new wxMenu
;
238 help_menu
->Append(SASHTEST_ABOUT
, "&About");
240 wxMenuBar
*menu_bar
= new wxMenuBar
;
242 menu_bar
->Append(file_menu
, "&File");
243 menu_bar
->Append(option_menu
, "&Options");
244 menu_bar
->Append(help_menu
, "&Help");
246 // Associate the menu bar with the frame
247 subframe
->SetMenuBar(menu_bar
);
250 subframe
->GetClientSize(&width
, &height
);
251 MyCanvas
*canvas
= new MyCanvas(subframe
, wxPoint(0, 0), wxSize(width
, height
));
252 canvas
->SetCursor(wxCursor(wxCURSOR_PENCIL
));
253 subframe
->canvas
= canvas
;
255 // Give it scrollbars
256 canvas
->SetScrollbars(20, 20, 50, 50);
258 subframe
->Show(TRUE
);
261 BEGIN_EVENT_TABLE(MyCanvas
, wxScrolledWindow
)
262 EVT_MOUSE_EVENTS(MyCanvas::OnEvent
)
265 // Define a constructor for my canvas
266 MyCanvas::MyCanvas(wxWindow
*parent
, const wxPoint
& pos
, const wxSize
& size
):
267 wxScrolledWindow(parent
, -1, pos
, size
, wxSUNKEN_BORDER
)
269 SetBackgroundColour(* wxWHITE
);
272 // Define the repainting behaviour
273 void MyCanvas::OnDraw(wxDC
& dc
)
275 dc
.SetFont(*wxSWISS_FONT
);
276 dc
.SetPen(*wxGREEN_PEN
);
277 dc
.DrawLine(0, 0, 200, 200);
278 dc
.DrawLine(200, 0, 0, 200);
280 dc
.SetBrush(*wxCYAN_BRUSH
);
281 dc
.SetPen(*wxRED_PEN
);
282 dc
.DrawRectangle(100, 100, 100, 50);
283 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
285 dc
.DrawEllipse(250, 250, 100, 50);
287 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
288 #endif // wxUSE_SPLINES
289 dc
.DrawLine(50, 230, 200, 230);
290 dc
.DrawText("This is a test string", 50, 230);
293 points
[0].x
= 200; points
[0].y
= 300;
294 points
[1].x
= 100; points
[1].y
= 400;
295 points
[2].x
= 300; points
[2].y
= 400;
297 dc
.DrawPolygon(3, points
);
300 // This implements a tiny doodling program! Drag the mouse using
302 void MyCanvas::OnEvent(wxMouseEvent
& event
)
307 wxPoint
pt(event
.GetLogicalPosition(dc
));
309 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
311 dc
.SetPen(*wxBLACK_PEN
);
312 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
318 void MyFrame::OnSize(wxSizeEvent
& WXUNUSED(event
))
320 wxLayoutAlgorithm layout
;
321 layout
.LayoutMDIFrame(this);
324 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
325 // to the parent window for processing, so no need to
326 // duplicate event handlers here.
328 BEGIN_EVENT_TABLE(MyChild
, wxMDIChildFrame
)
329 EVT_MENU(SASHTEST_CHILD_QUIT
, MyChild::OnQuit
)
332 MyChild::MyChild(wxMDIParentFrame
*parent
, const wxString
& title
, const wxPoint
& pos
, const wxSize
& size
,
334 wxMDIChildFrame(parent
, -1, title
, pos
, size
, style
)
337 my_children
.Append(this);
340 MyChild::~MyChild(void)
342 my_children
.DeleteObject(this);
345 void MyChild::OnQuit(wxCommandEvent
& WXUNUSED(event
))
350 void MyChild::OnActivate(wxActivateEvent
& event
)
352 if (event
.GetActive() && canvas
)