]> git.saurik.com Git - wxWidgets.git/blame_incremental - contrib/samples/gizmos/dynsash_switch/dynsash_switch.cpp
cleanups, proper event handling/cleanup
[wxWidgets.git] / contrib / samples / gizmos / dynsash_switch / dynsash_switch.cpp
... / ...
CommitLineData
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
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
23// need because it includes almost all "standard" wxWidgets headers)
24#ifndef WX_PRECOMP
25 #include "wx/wx.h"
26#endif
27
28#include <wx/app.h>
29#include <wx/frame.h>
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) {
68 Create(win, wxID_ANY);
69
70 m_dyn_sash = win;
71
72 m_bar = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxRAISED_BORDER, wxT("bar"));
73 m_choice = new wxChoice(m_bar, wxID_ANY);
74 m_choice->SetEventHandler(this);
75 m_view = new wxWindow(this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0, wxT("view"));
76 m_view->SetBackgroundColour(*wxWHITE);
77 m_view->SetEventHandler(this);
78
79 m_choice->Append(wxT("Triangle"));
80 m_choice->Append(wxT("Square"));
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
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
122 Connect(wxID_ANY, wxEVT_SET_FOCUS, (wxObjectEventFunction)
123 (wxEventFunction)
124 (wxFocusEventFunction)&SwitchView::OnFocus);
125 Connect(wxID_ANY, wxEVT_SCROLL_TOP, (wxObjectEventFunction)
126 (wxEventFunction)
127 (wxScrollEventFunction)&SwitchView::OnScroll);
128 Connect(wxID_ANY, wxEVT_SCROLL_BOTTOM, (wxObjectEventFunction)
129 (wxEventFunction)
130 (wxScrollEventFunction)&SwitchView::OnScroll);
131 Connect(wxID_ANY, wxEVT_SCROLL_LINEUP, (wxObjectEventFunction)
132 (wxEventFunction)
133 (wxScrollEventFunction)&SwitchView::OnScroll);
134 Connect(wxID_ANY, wxEVT_SCROLL_LINEDOWN, (wxObjectEventFunction)
135 (wxEventFunction)
136 (wxScrollEventFunction)&SwitchView::OnScroll);
137 Connect(wxID_ANY, wxEVT_SCROLL_PAGEUP, (wxObjectEventFunction)
138 (wxEventFunction)
139 (wxScrollEventFunction)&SwitchView::OnScroll);
140 Connect(wxID_ANY, wxEVT_SCROLL_PAGEDOWN, (wxObjectEventFunction)
141 (wxEventFunction)
142 (wxScrollEventFunction)&SwitchView::OnScroll);
143 Connect(wxID_ANY, wxEVT_SCROLL_THUMBTRACK, (wxObjectEventFunction)
144 (wxEventFunction)
145 (wxScrollEventFunction)&SwitchView::OnScroll);
146 Connect(wxID_ANY, wxEVT_SCROLL_THUMBRELEASE, (wxObjectEventFunction)
147 (wxEventFunction)
148 (wxScrollEventFunction)&SwitchView::OnScroll);
149 Connect(wxID_ANY, wxEVT_ERASE_BACKGROUND, (wxObjectEventFunction)
150 (wxEventFunction)
151 (wxEraseEventFunction)&SwitchView::OnErase);
152
153 Connect(wxID_ANY, wxEVT_DYNAMIC_SASH_SPLIT, (wxObjectEventFunction)
154 (wxEventFunction)
155 (wxDynamicSashSplitEventFunction)&SwitchView::OnSplit);
156 Connect(wxID_ANY, wxEVT_DYNAMIC_SASH_UNIFY, (wxObjectEventFunction)
157 (wxEventFunction)
158 (wxDynamicSashUnifyEventFunction)&SwitchView::OnUnify);
159}
160
161wxSize SwitchView::DoGetBestSize() const {
162 return wxSize(64, 64);
163}
164
165void SwitchView::OnSize(wxSizeEvent& WXUNUSED(event)) {
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
186void SwitchView::OnPaint(wxPaintEvent& WXUNUSED(event)) {
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
207void SwitchView::OnErase(wxEraseEvent& WXUNUSED(event)) {
208 // Do nothing
209}
210
211void SwitchView::OnSplit(wxDynamicSashSplitEvent& WXUNUSED(event)) {
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
222void SwitchView::OnUnify(wxDynamicSashUnifyEvent& WXUNUSED(event)) {
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
230void SwitchView::OnChoice(wxCommandEvent& WXUNUSED(event)) {
231 m_view->Refresh();
232}
233
234void SwitchView::OnScroll(wxScrollEvent& WXUNUSED(event)) {
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
255 frame = new wxFrame(NULL, wxID_ANY, wxT("Dynamic Sash Window Switch Demo"));
256 dyn = new wxDynamicSashWindow(frame, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN);
257 new SwitchView(dyn);
258
259 frame->SetSize(480, 480);
260 frame->Show();
261
262 return true;
263}