]> git.saurik.com Git - wxWidgets.git/blame - samples/sashtest/sashtest.cpp
add convenient GetViewStart() and Scroll() overloads taking wxPoint instead of 2...
[wxWidgets.git] / samples / sashtest / sashtest.cpp
CommitLineData
a6d70308
JS
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$
6aa89a22 8// Copyright: (c) Julian Smart
2f6c54eb 9// Licence: wxWindows license
a6d70308
JS
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
012f2cb2
GD
24#include "wx/toolbar.h"
25#include "wx/laywin.h"
a6d70308
JS
26
27#include "sashtest.h"
28
29MyFrame *frame = NULL;
30wxList my_children;
31
32IMPLEMENT_APP(MyApp)
33
34// For drawing lines in a canvas
35long xpos = -1;
36long ypos = -1;
37
38int winNumber = 1;
39
40// Initialise this in OnInit, not statically
41bool MyApp::OnInit(void)
42{
45e6e6f8
VZ
43 if ( !wxApp::OnInit() )
44 return false;
45
a6d70308
JS
46 // Create the main frame window
47
e77d093d 48 frame = new MyFrame(NULL, wxID_ANY, _T("Sash Demo"), wxPoint(0, 0), wxSize(500, 400),
1f23a1c0
VZ
49 wxDEFAULT_FRAME_STYLE |
50 wxNO_FULL_REPAINT_ON_RESIZE |
51 wxHSCROLL | wxVSCROLL);
a6d70308
JS
52
53 // Give it an icon (this is ignored in MDI mode: uses resources)
54#ifdef __WXMSW__
600683ca 55 frame->SetIcon(wxIcon(_T("sashtest_icn")));
a6d70308 56#endif
a6d70308
JS
57
58 // Make a menubar
59 wxMenu *file_menu = new wxMenu;
60
600683ca
MB
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"));
a6d70308
JS
64
65 wxMenu *help_menu = new wxMenu;
600683ca 66 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
a6d70308
JS
67
68 wxMenuBar *menu_bar = new wxMenuBar;
69
600683ca
MB
70 menu_bar->Append(file_menu, _T("&File"));
71 menu_bar->Append(help_menu, _T("&Help"));
a6d70308
JS
72
73 // Associate the menu bar with the frame
74 frame->SetMenuBar(menu_bar);
75
8520f137 76#if wxUSE_STATUSBAR
a6d70308 77 frame->CreateStatusBar();
8520f137 78#endif // wxUSE_STATUSBAR
a6d70308 79
e77d093d 80 frame->Show(true);
a6d70308
JS
81
82 SetTopWindow(frame);
83
e77d093d 84 return true;
a6d70308
JS
85}
86
87BEGIN_EVENT_TABLE(MyFrame, wxMDIParentFrame)
88 EVT_MENU(SASHTEST_ABOUT, MyFrame::OnAbout)
89 EVT_MENU(SASHTEST_NEW_WINDOW, MyFrame::OnNewWindow)
90 EVT_SIZE(MyFrame::OnSize)
91 EVT_MENU(SASHTEST_QUIT, MyFrame::OnQuit)
92 EVT_MENU(SASHTEST_TOGGLE_WINDOW, MyFrame::OnToggleWindow)
93 EVT_SASH_DRAGGED_RANGE(ID_WINDOW_TOP, ID_WINDOW_BOTTOM, MyFrame::OnSashDrag)
94END_EVENT_TABLE()
95
96
97// Define my frame constructor
98MyFrame::MyFrame(wxWindow *parent, const wxWindowID id, const wxString& title, const wxPoint& pos, const wxSize& size,
2f6c54eb 99 const long style):
a6d70308
JS
100 wxMDIParentFrame(parent, id, title, pos, size, style)
101{
102 // Create some dummy layout windows
103
104 // A window like a toolbar
1f23a1c0
VZ
105 wxSashLayoutWindow* win =
106 new wxSashLayoutWindow(this, ID_WINDOW_TOP,
107 wxDefaultPosition, wxSize(200, 30),
108 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
109
a6d70308
JS
110 win->SetDefaultSize(wxSize(1000, 30));
111 win->SetOrientation(wxLAYOUT_HORIZONTAL);
112 win->SetAlignment(wxLAYOUT_TOP);
113 win->SetBackgroundColour(wxColour(255, 0, 0));
e77d093d 114 win->SetSashVisible(wxSASH_BOTTOM, true);
a6d70308
JS
115
116 m_topWindow = win;
117
118 // A window like a statusbar
1f23a1c0
VZ
119 win = new wxSashLayoutWindow(this, ID_WINDOW_BOTTOM,
120 wxDefaultPosition, wxSize(200, 30),
121 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
a6d70308
JS
122 win->SetDefaultSize(wxSize(1000, 30));
123 win->SetOrientation(wxLAYOUT_HORIZONTAL);
124 win->SetAlignment(wxLAYOUT_BOTTOM);
125 win->SetBackgroundColour(wxColour(0, 0, 255));
e77d093d 126 win->SetSashVisible(wxSASH_TOP, true);
a6d70308
JS
127
128 m_bottomWindow = win;
129
130 // A window to the left of the client window
1f23a1c0
VZ
131 win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT1,
132 wxDefaultPosition, wxSize(200, 30),
133 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
a6d70308
JS
134 win->SetDefaultSize(wxSize(120, 1000));
135 win->SetOrientation(wxLAYOUT_VERTICAL);
136 win->SetAlignment(wxLAYOUT_LEFT);
137 win->SetBackgroundColour(wxColour(0, 255, 0));
e77d093d 138 win->SetSashVisible(wxSASH_RIGHT, true);
a6d70308
JS
139 win->SetExtraBorderSize(10);
140
5f4d35b8 141 wxTextCtrl* textWindow = new wxTextCtrl(win, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize,
a6d70308
JS
142 wxTE_MULTILINE|wxSUNKEN_BORDER);
143// wxTE_MULTILINE|wxNO_BORDER);
600683ca 144 textWindow->SetValue(_T("A help window"));
a6d70308
JS
145
146 m_leftWindow1 = win;
147
148 // Another window to the left of the client window
1f23a1c0
VZ
149 win = new wxSashLayoutWindow(this, ID_WINDOW_LEFT2,
150 wxDefaultPosition, wxSize(200, 30),
6138e469 151 wxNO_BORDER | wxSW_3D | wxCLIP_CHILDREN);
a6d70308
JS
152 win->SetDefaultSize(wxSize(120, 1000));
153 win->SetOrientation(wxLAYOUT_VERTICAL);
154 win->SetAlignment(wxLAYOUT_LEFT);
155 win->SetBackgroundColour(wxColour(0, 255, 255));
e77d093d 156 win->SetSashVisible(wxSASH_RIGHT, true);
a6d70308
JS
157
158 m_leftWindow2 = win;
159}
160
8fdca65c 161void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
a6d70308 162{
e77d093d 163 Close(true);
a6d70308
JS
164}
165
8fdca65c 166void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
a6d70308 167{
be5a51fb 168 (void)wxMessageBox(_T("wxWidgets 2.0 Sash Demo\nAuthor: Julian Smart (c) 1998"), _T("About Sash Demo"));
a6d70308
JS
169}
170
8fdca65c 171void MyFrame::OnToggleWindow(wxCommandEvent& WXUNUSED(event))
a6d70308
JS
172{
173 if (m_leftWindow1->IsShown())
174 {
e77d093d 175 m_leftWindow1->Show(false);
a6d70308
JS
176 }
177 else
178 {
e77d093d 179 m_leftWindow1->Show(true);
a6d70308 180 }
5f4d35b8 181#if wxUSE_MDI_ARCHITECTURE
a6d70308
JS
182 wxLayoutAlgorithm layout;
183 layout.LayoutMDIFrame(this);
5f4d35b8 184#endif // wxUSE_MDI_ARCHITECTURE
a6d70308
JS
185}
186
187void MyFrame::OnSashDrag(wxSashEvent& event)
188{
189 if (event.GetDragStatus() == wxSASH_STATUS_OUT_OF_RANGE)
190 return;
191
192 switch (event.GetId())
193 {
194 case ID_WINDOW_TOP:
195 {
196 m_topWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
197 break;
198 }
199 case ID_WINDOW_LEFT1:
200 {
201 m_leftWindow1->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
202 break;
203 }
204 case ID_WINDOW_LEFT2:
205 {
206 m_leftWindow2->SetDefaultSize(wxSize(event.GetDragRect().width, 1000));
207 break;
208 }
209 case ID_WINDOW_BOTTOM:
210 {
211 m_bottomWindow->SetDefaultSize(wxSize(1000, event.GetDragRect().height));
212 break;
213 }
214 }
5f4d35b8
WS
215
216#if wxUSE_MDI_ARCHITECTURE
a6d70308
JS
217 wxLayoutAlgorithm layout;
218 layout.LayoutMDIFrame(this);
5f4d35b8 219#endif // wxUSE_MDI_ARCHITECTURE
a6d70308
JS
220
221 // Leaves bits of itself behind sometimes
222 GetClientWindow()->Refresh();
223}
224
8fdca65c 225void MyFrame::OnNewWindow(wxCommandEvent& WXUNUSED(event))
a6d70308
JS
226{
227 // Make another frame, containing a canvas
600683ca 228 MyChild *subframe = new MyChild(frame, _T("Canvas Frame"),
1f23a1c0
VZ
229 wxPoint(10, 10), wxSize(300, 300),
230 wxDEFAULT_FRAME_STYLE |
231 wxNO_FULL_REPAINT_ON_RESIZE);
a6d70308 232
600683ca 233 subframe->SetTitle(wxString::Format(_T("Canvas Frame %d"), winNumber));
a6d70308
JS
234 winNumber ++;
235
236 // Give it an icon (this is ignored in MDI mode: uses resources)
237#ifdef __WXMSW__
600683ca 238 subframe->SetIcon(wxIcon(_T("sashtest_icn")));
a6d70308
JS
239#endif
240
8520f137 241#if wxUSE_STATUSBAR
a6d70308
JS
242 // Give it a status line
243 subframe->CreateStatusBar();
8520f137 244#endif // wxUSE_STATUSBAR
a6d70308
JS
245
246 // Make a menubar
247 wxMenu *file_menu = new wxMenu;
248
600683ca
MB
249 file_menu->Append(SASHTEST_NEW_WINDOW, _T("&New window"));
250 file_menu->Append(SASHTEST_CHILD_QUIT, _T("&Close child"));
251 file_menu->Append(SASHTEST_QUIT, _T("&Exit"));
a6d70308
JS
252
253 wxMenu *option_menu = new wxMenu;
254
255 // Dummy option
600683ca 256 option_menu->Append(SASHTEST_REFRESH, _T("&Refresh picture"));
a6d70308
JS
257
258 wxMenu *help_menu = new wxMenu;
600683ca 259 help_menu->Append(SASHTEST_ABOUT, _T("&About"));
a6d70308
JS
260
261 wxMenuBar *menu_bar = new wxMenuBar;
262
600683ca
MB
263 menu_bar->Append(file_menu, _T("&File"));
264 menu_bar->Append(option_menu, _T("&Options"));
265 menu_bar->Append(help_menu, _T("&Help"));
a6d70308
JS
266
267 // Associate the menu bar with the frame
268 subframe->SetMenuBar(menu_bar);
269
270 int width, height;
271 subframe->GetClientSize(&width, &height);
272 MyCanvas *canvas = new MyCanvas(subframe, wxPoint(0, 0), wxSize(width, height));
273 canvas->SetCursor(wxCursor(wxCURSOR_PENCIL));
274 subframe->canvas = canvas;
275
276 // Give it scrollbars
277 canvas->SetScrollbars(20, 20, 50, 50);
278
e77d093d 279 subframe->Show(true);
a6d70308
JS
280}
281
282BEGIN_EVENT_TABLE(MyCanvas, wxScrolledWindow)
2f6c54eb 283 EVT_MOUSE_EVENTS(MyCanvas::OnEvent)
a6d70308
JS
284END_EVENT_TABLE()
285
286// Define a constructor for my canvas
1f23a1c0 287MyCanvas::MyCanvas(wxWindow *parent, const wxPoint& pos, const wxSize& size)
e77d093d 288 : wxScrolledWindow(parent, wxID_ANY, pos, size,
1f23a1c0 289 wxSUNKEN_BORDER | wxNO_FULL_REPAINT_ON_RESIZE)
a6d70308 290{
f6bcfd97 291 SetBackgroundColour(* wxWHITE);
a6d70308
JS
292}
293
294// Define the repainting behaviour
295void MyCanvas::OnDraw(wxDC& dc)
296{
5f4d35b8
WS
297 dc.SetFont(*wxSWISS_FONT);
298 dc.SetPen(*wxGREEN_PEN);
299 dc.DrawLine(0, 0, 200, 200);
300 dc.DrawLine(200, 0, 0, 200);
a6d70308 301
5f4d35b8
WS
302 dc.SetBrush(*wxCYAN_BRUSH);
303 dc.SetPen(*wxRED_PEN);
304 dc.DrawRectangle(100, 100, 100, 50);
305 dc.DrawRoundedRectangle(150, 150, 100, 50, 20);
a6d70308 306
5f4d35b8 307 dc.DrawEllipse(250, 250, 100, 50);
9fc3cba7 308#if wxUSE_SPLINES
5f4d35b8 309 dc.DrawSpline(50, 200, 50, 100, 200, 10);
9fc3cba7 310#endif // wxUSE_SPLINES
5f4d35b8
WS
311 dc.DrawLine(50, 230, 200, 230);
312 dc.DrawText(_T("This is a test string"), 50, 230);
313
314 wxPoint points[3];
315 points[0].x = 200; points[0].y = 300;
316 points[1].x = 100; points[1].y = 400;
317 points[2].x = 300; points[2].y = 400;
318
319 dc.DrawPolygon(3, points);
a6d70308
JS
320}
321
322// This implements a tiny doodling program! Drag the mouse using
323// the left button.
324void MyCanvas::OnEvent(wxMouseEvent& event)
325{
326 wxClientDC dc(this);
327 PrepareDC(dc);
328
329 wxPoint pt(event.GetLogicalPosition(dc));
330
331 if (xpos > -1 && ypos > -1 && event.Dragging())
332 {
333 dc.SetPen(*wxBLACK_PEN);
334 dc.DrawLine(xpos, ypos, pt.x, pt.y);
335 }
336 xpos = pt.x;
337 ypos = pt.y;
338}
339
8fdca65c 340void MyFrame::OnSize(wxSizeEvent& WXUNUSED(event))
a6d70308 341{
5f4d35b8 342#if wxUSE_MDI_ARCHITECTURE
a6d70308
JS
343 wxLayoutAlgorithm layout;
344 layout.LayoutMDIFrame(this);
5f4d35b8 345#endif // wxUSE_MDI_ARCHITECTURE
a6d70308
JS
346}
347
348// Note that SASHTEST_NEW_WINDOW and SASHTEST_ABOUT commands get passed
349// to the parent window for processing, so no need to
350// duplicate event handlers here.
351
352BEGIN_EVENT_TABLE(MyChild, wxMDIChildFrame)
353 EVT_MENU(SASHTEST_CHILD_QUIT, MyChild::OnQuit)
354END_EVENT_TABLE()
355
356MyChild::MyChild(wxMDIParentFrame *parent, const wxString& title, const wxPoint& pos, const wxSize& size,
357const long style):
e77d093d 358 wxMDIChildFrame(parent, wxID_ANY, title, pos, size, style)
a6d70308
JS
359{
360 canvas = NULL;
361 my_children.Append(this);
362}
363
364MyChild::~MyChild(void)
365{
366 my_children.DeleteObject(this);
367}
368
369void MyChild::OnQuit(wxCommandEvent& WXUNUSED(event))
370{
e77d093d 371 Close(true);
a6d70308
JS
372}
373
374void MyChild::OnActivate(wxActivateEvent& event)
375{
376 if (event.GetActive() && canvas)
377 canvas->SetFocus();
378}