]> git.saurik.com Git - wxWidgets.git/blob - samples/sashtest/sashtest.cpp
fixed the bug with the order of 2 size events when the scrollbar[s] (dis)appear in...
[wxWidgets.git] / samples / sashtest / sashtest.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: sashtest.cpp
3 // Purpose: Layout/sash sample
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 04/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // For compilers that support precompilation, includes "wx/wx.h".
13 #include "wx/wxprec.h"
14
15 #ifdef __BORLANDC__
16 #pragma hdrstop
17 #endif
18
19 #ifndef WX_PRECOMP
20 #include "wx/wx.h"
21 #include "wx/mdi.h"
22 #endif
23
24 #include <wx/toolbar.h>
25 #include <wx/laywin.h>
26
27 #include "sashtest.h"
28
29 MyFrame *frame = NULL;
30 wxList my_children;
31
32 IMPLEMENT_APP(MyApp)
33
34 // For drawing lines in a canvas
35 long xpos = -1;
36 long ypos = -1;
37
38 int winNumber = 1;
39
40 // Initialise this in OnInit, not statically
41 bool MyApp::OnInit(void)
42 {
43 // Create the main frame window
44
45 frame = new MyFrame(NULL, -1, "Sash Demo", wxPoint(0, 0), wxSize(500, 400),
46 wxDEFAULT_FRAME_STYLE |
47 wxNO_FULL_REPAINT_ON_RESIZE |
48 wxHSCROLL | wxVSCROLL);
49
50 // Give it an icon (this is ignored in MDI mode: uses resources)
51 #ifdef __WXMSW__
52 frame->SetIcon(wxIcon("sashtest_icn"));
53 #endif
54 #ifdef __X__
55 frame->SetIcon(wxIcon("sashtest.xbm"));
56 #endif
57
58 // Make a menubar
59 wxMenu *file_menu = new wxMenu;
60
61 file_menu->Append(SASHTEST_NEW_WINDOW, "&New window");
62 file_menu->Append(SASHTEST_TOGGLE_WINDOW, "&Toggle window");
63 file_menu->Append(SASHTEST_QUIT, "&Exit");
64
65 wxMenu *help_menu = new wxMenu;
66 help_menu->Append(SASHTEST_ABOUT, "&About");
67
68 wxMenuBar *menu_bar = new wxMenuBar;
69
70 menu_bar->Append(file_menu, "&File");
71 menu_bar->Append(help_menu, "&Help");
72
73 // Associate the menu bar with the frame
74 frame->SetMenuBar(menu_bar);
75
76 frame->CreateStatusBar();
77
78 frame->Show(TRUE);
79
80 SetTopWindow(frame);
81
82 return TRUE;
83 }
84
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)
92 END_EVENT_TABLE()
93
94
95 // Define my frame constructor
96 MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
97 const long style):
98 wxMDIParentFrame(parent, id, title, pos, size, style)
99 {
100 // Create some dummy layout windows
101
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);
107
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);
113
114 m_topWindow = win;
115
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);
125
126 m_bottomWindow = win;
127
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);
138
139 wxTextCtrl* textWindow = new wxTextCtrl(win, -1, "", wxDefaultPosition, wxDefaultSize,
140 wxTE_MULTILINE|wxSUNKEN_BORDER);
141 // wxTE_MULTILINE|wxNO_BORDER);
142 textWindow->SetValue("A help window");
143
144 m_leftWindow1 = win;
145
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);
155
156 m_leftWindow2 = win;
157 }
158
159 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
160 {
161 Close(TRUE);
162 }
163
164 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
165 {
166 (void)wxMessageBox("wxWindows 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998", "About Sash Demo");
167 }
168
169 void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event))
170 {
171 if (m_leftWindow1->IsShown())
172 {
173 m_leftWindow1->Show(FALSE);
174 }
175 else
176 {
177 m_leftWindow1->Show(TRUE);
178 }
179 wxLayoutAlgorithm layout;
180 layout.LayoutMDIFrame(this);
181 }
182
183 void MyFrame::OnSashDrag(wxSashEvent& event)
184 {
185 if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
186 return;
187
188 switch (event.GetId())
189 {
190 case ID_WINDOW_TOP:
191 {
192 m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
193 break;
194 }
195 case ID_WINDOW_LEFT1:
196 {
197 m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
198 break;
199 }
200 case ID_WINDOW_LEFT2:
201 {
202 m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
203 break;
204 }
205 case ID_WINDOW_BOTTOM:
206 {
207 m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
208 break;
209 }
210 }
211 wxLayoutAlgorithm layout;
212 layout.LayoutMDIFrame(this);
213
214 // Leaves bits of itself behind sometimes
215 GetClientWindow()->Refresh();
216 }
217
218 void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
219 {
220 // Make another frame, containing a canvas
221 MyChild *subframe = new MyChild(frame, "Canvas Frame",
222 wxPoint(10, 10), wxSize(300, 300),
223 wxDEFAULT_FRAME_STYLE |
224 wxNO_FULL_REPAINT_ON_RESIZE);
225
226 char titleBuf[100];
227 sprintf(titleBuf, "Canvas Frame %d", winNumber);
228 subframe->SetTitle(titleBuf);
229 winNumber ++;
230
231 // Give it an icon (this is ignored in MDI mode: uses resources)
232 #ifdef __WXMSW__
233 subframe->SetIcon(wxIcon("sashtest_icn"));
234 #endif
235
236 // Give it a status line
237 subframe->CreateStatusBar();
238
239 // Make a menubar
240 wxMenu *file_menu = new wxMenu;
241
242 file_menu->Append(SASHTEST_NEW_WINDOW, "&New window");
243 file_menu->Append(SASHTEST_CHILD_QUIT, "&Close child");
244 file_menu->Append(SASHTEST_QUIT, "&Exit");
245
246 wxMenu *option_menu = new wxMenu;
247
248 // Dummy option
249 option_menu->Append(SASHTEST_REFRESH, "&Refresh picture");
250
251 wxMenu *help_menu = new wxMenu;
252 help_menu->Append(SASHTEST_ABOUT, "&About");
253
254 wxMenuBar *menu_bar = new wxMenuBar;
255
256 menu_bar->Append(file_menu, "&File");
257 menu_bar->Append(option_menu, "&Options");
258 menu_bar->Append(help_menu, "&Help");
259
260 // Associate the menu bar with the frame
261 subframe->SetMenuBar(menu_bar);
262
263 int width, height;
264 subframe->GetClientSize(&width, &height);
265 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
266 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
267 subframe->canvas = canvas;
268
269 // Give it scrollbars
270 canvas->SetScrollbars(20, 20, 50, 50);
271
272 subframe->Show(TRUE);
273 }
274
275 BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
276 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
277 END_EVENT_TABLE()
278
279 // Define a constructor for my canvas
280 MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
281 : wxScrolledWindow(parent, -1, pos, size,
282 wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
283 {
284 SetBackgroundColour(* wxWHITE);
285 }
286
287 // Define the repainting behaviour
288 void MyCanvas::OnDraw(wxDC& dc)
289 {
290 dc.SetFont(*wxSWISS_FONT);
291 dc.SetPen(*wxGREEN_PEN);
292 dc.DrawLine(0, 0, 200, 200);
293 dc.DrawLine(200, 0, 0, 200);
294
295 dc.SetBrush(*wxCYAN_BRUSH);
296 dc.SetPen(*wxRED_PEN);
297 dc.DrawRectangle(100, 100, 100, 50);
298 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
299
300 dc.DrawEllipse(250, 250, 100, 50);
301 #if wxUSE_SPLINES
302 dc.DrawSpline(50, 200, 50, 100, 200, 10);
303 #endif // wxUSE_SPLINES
304 dc.DrawLine(50, 230, 200, 230);
305 dc.DrawText("This is a test string", 50, 230);
306
307 wxPoint points[3];
308 points[0].x = 200; points[0].y = 300;
309 points[1].x = 100; points[1].y = 400;
310 points[2].x = 300; points[2].y = 400;
311
312 dc.DrawPolygon(3, points);
313 }
314
315 // This implements a tiny doodling program! Drag the mouse using
316 // the left button.
317 void MyCanvas::OnEvent(wxMouseEvent& event)
318 {
319 wxClientDC dc(this);
320 PrepareDC(dc);
321
322 wxPoint pt(event.GetLogicalPosition(dc));
323
324 if (xpos > -1 && ypos > -1 && event.Dragging())
325 {
326 dc.SetPen(*wxBLACK_PEN);
327 dc.DrawLine(xpos, ypos, pt.x, pt.y);
328 }
329 xpos = pt.x;
330 ypos = pt.y;
331 }
332
333 void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
334 {
335 wxLayoutAlgorithm layout;
336 layout.LayoutMDIFrame(this);
337 }
338
339 // Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
340 // to the parent window for processing, so no need to
341 // duplicate event handlers here.
342
343 BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
344 EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
345 END_EVENT_TABLE()
346
347 MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
348 const long style):
349 wxMDIChildFrame(parent, -1, title, pos, size, style)
350 {
351 canvas = NULL;
352 my_children.Append(this);
353 }
354
355 MyChild::~MyChild(void)
356 {
357 my_children.DeleteObject(this);
358 }
359
360 void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
361 {
362 Close(TRUE);
363 }
364
365 void MyChild::OnActivate(wxActivateEvent& event)
366 {
367 if (event.GetActive() && canvas)
368 canvas->SetFocus();
369 }
370