]> git.saurik.com Git - wxWidgets.git/blame - contrib/samples/gizmos/dynsash_switch/dynsash_switch.cpp
cleanups, proper event handling/cleanup
[wxWidgets.git] / contrib / samples / gizmos / dynsash_switch / dynsash_switch.cpp
CommitLineData
eacb91fc
VZ
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynsash_switch.cpp
3// Purpose: Test custom scrollbar handling of wxDynamicSashWindow by
4// creating a dynamic sash window where the client scrolls a
5// a subwindow of the client window by responding to scroll
6// events itself
7// Author: Matt Kimball
8// Modified by:
9// Created: 7/15/2001
10// RCS-ID: $Id$
11// Copyright: (c) 2001 Matt Kimball
12// Licence: wxWindows licence
13/////////////////////////////////////////////////////////////////////////////
14
cd72551c
JS
15// For compilers that support precompilation, includes "wx/wx.h".
16#include "wx/wxprec.h"
17
18#ifdef __BORLANDC__
19 #pragma hdrstop
20#endif
21
22// for all others, include the necessary headers (this file is usually all you
be5a51fb 23// need because it includes almost all "standard" wxWidgets headers)
cd72551c
JS
24#ifndef WX_PRECOMP
25 #include "wx/wx.h"
26#endif
27
eacb91fc 28#include <wx/app.h>
8cdb648e 29#include <wx/frame.h>
eacb91fc
VZ
30#include <wx/choice.h>
31#include <wx/dcclient.h>
32#include <wx/gizmos/dynamicsash.h>
33#include <wx/layout.h>
34#include <wx/scrolbar.h>
35
36class SwitchDemo : public wxApp {
37public:
38 bool OnInit();
39};
40
41
42class SwitchView : public wxWindow {
43public:
44 SwitchView(wxDynamicSashWindow *parent);
45
46 wxSize DoGetBestSize() const;
47
48private:
49 void OnSize(wxSizeEvent& event);
50 void OnPaint(wxPaintEvent& event);
51 void OnChoice(wxCommandEvent& event);
52 void OnScroll(wxScrollEvent& event);
53 void OnFocus(wxFocusEvent& event);
54 void OnErase(wxEraseEvent& event);
55 void OnSplit(wxDynamicSashSplitEvent& event);
56 void OnUnify(wxDynamicSashUnifyEvent& event);
57
58 wxDynamicSashWindow *m_dyn_sash;
59 wxWindow *m_bar;
60 wxChoice *m_choice;
61 wxWindow *m_view;
62};
63
64IMPLEMENT_APP(SwitchDemo)
65
66
67SwitchView::SwitchView(wxDynamicSashWindow *win) {
a2d49353 68 Create(win, wxID_ANY);
eacb91fc
VZ
69
70 m_dyn_sash = win;
71
a2d49353
WS
72 m_bar = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER, wxT("bar"));
73 m_choice = new wxChoice(m_bar, wxID_ANY);
eacb91fc 74 m_choice->SetEventHandler(this);
a2d49353 75 m_view = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxT("view"));
eacb91fc
VZ
76 m_view->SetBackgroundColour(*wxWHITE);
77 m_view->SetEventHandler(this);
78
019abbf2
VZ
79 m_choice->Append(wxT("Triangle"));
80 m_choice->Append(wxT("Square"));
eacb91fc
VZ
81 m_choice->SetSelection(0);
82
83 wxLayoutConstraints *layout;
84
85 layout = new wxLayoutConstraints();
86 layout->left.Absolute(0);
87 layout->top.Absolute(0);
88 layout->height.Absolute(36);
89 layout->width.SameAs(this, wxWidth);
90 m_bar->SetConstraints(layout);
91
92 layout = new wxLayoutConstraints();
93 layout->left.Absolute(3);
94 layout->width.AsIs();
95 layout->height.AsIs();
96 layout->top.Absolute(3);
97 m_choice->SetConstraints(layout);
98
99 layout = new wxLayoutConstraints();
100 layout->left.Absolute(0);
101 layout->width.SameAs(this, wxWidth);
102 layout->top.Below(m_bar);
103 layout->bottom.SameAs(this, wxBottom);
104 m_view->SetConstraints(layout);
105
106 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
107 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
108
109 hscroll->SetEventHandler(this);
110 vscroll->SetEventHandler(this);
111
ba597ca8
JS
112 Connect(GetId(), wxEVT_SIZE, (wxObjectEventFunction)
113 (wxEventFunction)
114 (wxSizeEventFunction)&SwitchView::OnSize);
115 Connect(m_choice->GetId(), wxEVT_COMMAND_CHOICE_SELECTED, (wxObjectEventFunction)
116 (wxEventFunction)
117 (wxCommandEventFunction)&SwitchView::OnChoice);
118 Connect(m_view->GetId(), wxEVT_PAINT, (wxObjectEventFunction)
119 (wxEventFunction)
120 (wxPaintEventFunction)&SwitchView::OnPaint);
121
a2d49353 122 Connect(wxID_ANY, wxEVT_SET_FOCUS, (wxObjectEventFunction)
ba597ca8
JS
123 (wxEventFunction)
124 (wxFocusEventFunction)&SwitchView::OnFocus);
a2d49353 125 Connect(wxID_ANY, wxEVT_SCROLL_TOP, (wxObjectEventFunction)
ba597ca8
JS
126 (wxEventFunction)
127 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 128 Connect(wxID_ANY, wxEVT_SCROLL_BOTTOM, (wxObjectEventFunction)
ba597ca8
JS
129 (wxEventFunction)
130 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 131 Connect(wxID_ANY, wxEVT_SCROLL_LINEUP, (wxObjectEventFunction)
ba597ca8
JS
132 (wxEventFunction)
133 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 134 Connect(wxID_ANY, wxEVT_SCROLL_LINEDOWN, (wxObjectEventFunction)
ba597ca8
JS
135 (wxEventFunction)
136 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 137 Connect(wxID_ANY, wxEVT_SCROLL_PAGEUP, (wxObjectEventFunction)
ba597ca8
JS
138 (wxEventFunction)
139 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 140 Connect(wxID_ANY, wxEVT_SCROLL_PAGEDOWN, (wxObjectEventFunction)
ba597ca8
JS
141 (wxEventFunction)
142 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 143 Connect(wxID_ANY, wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)
ba597ca8
JS
144 (wxEventFunction)
145 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 146 Connect(wxID_ANY, wxEVT_SCROLL_THUMBRELEASE, (wxObjectEventFunction)
ba597ca8
JS
147 (wxEventFunction)
148 (wxScrollEventFunction)&SwitchView::OnScroll);
a2d49353 149 Connect(wxID_ANY, wxEVT_ERASE_BACKGROUND, (wxObjectEventFunction)
ba597ca8
JS
150 (wxEventFunction)
151 (wxEraseEventFunction)&SwitchView::OnErase);
152
a2d49353 153 Connect(wxID_ANY, wxEVT_DYNAMIC_SASH_SPLIT, (wxObjectEventFunction)
ba597ca8
JS
154 (wxEventFunction)
155 (wxDynamicSashSplitEventFunction)&SwitchView::OnSplit);
a2d49353 156 Connect(wxID_ANY, wxEVT_DYNAMIC_SASH_UNIFY, (wxObjectEventFunction)
ba597ca8
JS
157 (wxEventFunction)
158 (wxDynamicSashUnifyEventFunction)&SwitchView::OnUnify);
eacb91fc
VZ
159}
160
161wxSize SwitchView::DoGetBestSize() const {
162 return wxSize(64, 64);
163}
164
ba597ca8 165void SwitchView::OnSize(wxSizeEvent& WXUNUSED(event)) {
eacb91fc
VZ
166 Layout();
167
168 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
169 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
170
171 if (hscroll && vscroll) {
172 int hpos = hscroll->GetThumbPosition();
173 int vpos = vscroll->GetThumbPosition();
174
175 wxSize size = m_view->GetSize();
176 if (hpos + size.GetWidth() > 300)
177 hpos = 300 - size.GetWidth();
178 if (vpos + size.GetHeight() > 300)
179 vpos = 300 - size.GetHeight();
180
181 hscroll->SetScrollbar(hpos, size.GetWidth(), 300, size.GetWidth());
182 vscroll->SetScrollbar(vpos, size.GetHeight(), 300, size.GetHeight());
183 }
184}
185
ba597ca8 186void SwitchView::OnPaint(wxPaintEvent& WXUNUSED(event)) {
eacb91fc
VZ
187 wxPaintDC dc(m_view);
188
189 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
190 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
191
192 dc.Clear();
193 dc.SetDeviceOrigin(-hscroll->GetThumbPosition(), -vscroll->GetThumbPosition());
194
195 if (m_choice->GetSelection()) {
196 dc.DrawLine(20, 20, 280, 20);
197 dc.DrawLine(280, 20, 280, 280);
198 dc.DrawLine(280, 280, 20, 280);
199 dc.DrawLine(20, 280, 20, 20);
200 } else {
201 dc.DrawLine(150, 20, 280, 280);
202 dc.DrawLine(280, 280, 20, 280);
203 dc.DrawLine(20, 280, 150, 20);
204 }
205}
206
ba597ca8 207void SwitchView::OnErase(wxEraseEvent& WXUNUSED(event)) {
eacb91fc
VZ
208 // Do nothing
209}
210
ba597ca8 211void SwitchView::OnSplit(wxDynamicSashSplitEvent& WXUNUSED(event)) {
eacb91fc
VZ
212 SwitchView *view = new SwitchView(m_dyn_sash);
213 view->m_choice->SetSelection(m_choice->GetSelection());
214
215 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
216 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
217
218 hscroll->SetEventHandler(this);
219 vscroll->SetEventHandler(this);
220}
221
ba597ca8 222void SwitchView::OnUnify(wxDynamicSashUnifyEvent& WXUNUSED(event)) {
eacb91fc
VZ
223 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
224 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
225
226 hscroll->SetEventHandler(this);
227 vscroll->SetEventHandler(this);
228}
229
ba597ca8 230void SwitchView::OnChoice(wxCommandEvent& WXUNUSED(event)) {
eacb91fc
VZ
231 m_view->Refresh();
232}
233
ba597ca8 234void SwitchView::OnScroll(wxScrollEvent& WXUNUSED(event)) {
eacb91fc
VZ
235 m_view->Refresh();
236}
237
238void SwitchView::OnFocus(wxFocusEvent& event) {
239 wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this);
240 wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this);
241
242 if (event.m_eventObject == hscroll || event.m_eventObject == vscroll) {
243 m_view->SetFocus();
244 } else {
245 event.Skip();
246 }
247}
248
249
250
251bool SwitchDemo::OnInit() {
252 wxFrame *frame;
253 wxDynamicSashWindow *dyn;
254
a2d49353
WS
255 frame = new wxFrame(NULL, wxID_ANY, wxT("Dynamic Sash Window Switch Demo"));
256 dyn = new wxDynamicSashWindow(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
eacb91fc
VZ
257 new SwitchView(dyn);
258
259 frame->SetSize(480, 480);
260 frame->Show();
261
a2d49353 262 return true;
eacb91fc 263}