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
| 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),
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
)
271 // Define the repainting behaviour
272 void MyCanvas::OnDraw(wxDC
& dc
)
274 dc
.SetFont(*wxSWISS_FONT
);
275 dc
.SetPen(*wxGREEN_PEN
);
276 dc
.DrawLine(0, 0, 200, 200);
277 dc
.DrawLine(200, 0, 0, 200);
279 dc
.SetBrush(*wxCYAN_BRUSH
);
280 dc
.SetPen(*wxRED_PEN
);
281 dc
.DrawRectangle(100, 100, 100, 50);
282 dc
.DrawRoundedRectangle(150, 150, 100, 50, 20);
284 dc
.DrawEllipse(250, 250, 100, 50);
285 dc
.DrawSpline(50, 200, 50, 100, 200, 10);
286 dc
.DrawLine(50, 230, 200, 230);
287 dc
.DrawText("This is a test string", 50, 230);
290 points
[0].x
= 200; points
[0].y
= 300;
291 points
[1].x
= 100; points
[1].y
= 400;
292 points
[2].x
= 300; points
[2].y
= 400;
294 dc
.DrawPolygon(3, points
);
297 // This implements a tiny doodling program! Drag the mouse using
299 void MyCanvas::OnEvent(wxMouseEvent
& event
)
304 wxPoint
pt(event
.GetLogicalPosition(dc
));
306 if (xpos
> -1 && ypos
> -1 && event
.Dragging())
308 dc
.SetPen(*wxBLACK_PEN
);
309 dc
.DrawLine(xpos
, ypos
, pt
.x
, pt
.y
);
315 // Define the behaviour for the frame closing
316 // - must delete all frames except for the main one.
317 bool MyFrame::OnClose(void)
319 // Must delete children
320 wxNode
*node
= my_children
.First();
323 MyChild
*child
= (MyChild
*)node
->Data();
324 wxNode
*next
= node
->Next();
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
, -1, 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
)
370 bool MyChild::OnClose(void)