1 /////////////////////////////////////////////////////////////////////////////
2 // Name: contrib/samples/gizmos/dynsash_switch/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
7 // Author: Matt Kimball
11 // Copyright: (c) 2001 Matt Kimball
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 // For compilers that support precompilation, includes "wx/wx.h".
16 #include "wx/wxprec.h"
22 // for all others, include the necessary headers (this file is usually all you
23 // need because it includes almost all "standard" wxWidgets headers)
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"
36 class SwitchDemo
: public wxApp
{
42 class SwitchView
: public wxWindow
{
44 SwitchView(wxDynamicSashWindow
*parent
);
46 wxSize
DoGetBestSize() const;
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
);
58 wxDynamicSashWindow
*m_dyn_sash
;
64 IMPLEMENT_APP(SwitchDemo
)
67 SwitchView::SwitchView(wxDynamicSashWindow
*win
) {
68 Create(win
, wxID_ANY
);
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);
79 m_choice
->Append(wxT("Triangle"));
80 m_choice
->Append(wxT("Square"));
81 m_choice
->SetSelection(0);
83 wxLayoutConstraints
*layout
;
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
);
92 layout
= new wxLayoutConstraints();
93 layout
->left
.Absolute(3);
95 layout
->height
.AsIs();
96 layout
->top
.Absolute(3);
97 m_choice
->SetConstraints(layout
);
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
);
106 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
107 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
109 hscroll
->SetEventHandler(this);
110 vscroll
->SetEventHandler(this);
112 Connect(GetId(), wxEVT_SIZE
, (wxObjectEventFunction
)
114 (wxSizeEventFunction
)&SwitchView::OnSize
);
115 Connect(m_choice
->GetId(), wxEVT_COMMAND_CHOICE_SELECTED
, (wxObjectEventFunction
)
117 (wxCommandEventFunction
)&SwitchView::OnChoice
);
118 Connect(m_view
->GetId(), wxEVT_PAINT
, (wxObjectEventFunction
)
120 (wxPaintEventFunction
)&SwitchView::OnPaint
);
122 Connect(wxID_ANY
, wxEVT_SET_FOCUS
, (wxObjectEventFunction
)
124 (wxFocusEventFunction
)&SwitchView::OnFocus
);
125 Connect(wxID_ANY
, wxEVT_SCROLL_TOP
, (wxObjectEventFunction
)
127 (wxScrollEventFunction
)&SwitchView::OnScroll
);
128 Connect(wxID_ANY
, wxEVT_SCROLL_BOTTOM
, (wxObjectEventFunction
)
130 (wxScrollEventFunction
)&SwitchView::OnScroll
);
131 Connect(wxID_ANY
, wxEVT_SCROLL_LINEUP
, (wxObjectEventFunction
)
133 (wxScrollEventFunction
)&SwitchView::OnScroll
);
134 Connect(wxID_ANY
, wxEVT_SCROLL_LINEDOWN
, (wxObjectEventFunction
)
136 (wxScrollEventFunction
)&SwitchView::OnScroll
);
137 Connect(wxID_ANY
, wxEVT_SCROLL_PAGEUP
, (wxObjectEventFunction
)
139 (wxScrollEventFunction
)&SwitchView::OnScroll
);
140 Connect(wxID_ANY
, wxEVT_SCROLL_PAGEDOWN
, (wxObjectEventFunction
)
142 (wxScrollEventFunction
)&SwitchView::OnScroll
);
143 Connect(wxID_ANY
, wxEVT_SCROLL_THUMBTRACK
, (wxObjectEventFunction
)
145 (wxScrollEventFunction
)&SwitchView::OnScroll
);
146 Connect(wxID_ANY
, wxEVT_SCROLL_THUMBRELEASE
, (wxObjectEventFunction
)
148 (wxScrollEventFunction
)&SwitchView::OnScroll
);
149 Connect(wxID_ANY
, wxEVT_ERASE_BACKGROUND
, (wxObjectEventFunction
)
151 (wxEraseEventFunction
)&SwitchView::OnErase
);
153 Connect(wxID_ANY
, wxEVT_DYNAMIC_SASH_SPLIT
, (wxObjectEventFunction
)
155 (wxDynamicSashSplitEventFunction
)&SwitchView::OnSplit
);
156 Connect(wxID_ANY
, wxEVT_DYNAMIC_SASH_UNIFY
, (wxObjectEventFunction
)
158 (wxDynamicSashUnifyEventFunction
)&SwitchView::OnUnify
);
161 wxSize
SwitchView::DoGetBestSize() const {
162 return wxSize(64, 64);
165 void SwitchView::OnSize(wxSizeEvent
& WXUNUSED(event
)) {
168 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
169 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
171 if (hscroll
&& vscroll
) {
172 int hpos
= hscroll
->GetThumbPosition();
173 int vpos
= vscroll
->GetThumbPosition();
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();
181 hscroll
->SetScrollbar(hpos
, size
.GetWidth(), 300, size
.GetWidth());
182 vscroll
->SetScrollbar(vpos
, size
.GetHeight(), 300, size
.GetHeight());
186 void SwitchView::OnPaint(wxPaintEvent
& WXUNUSED(event
)) {
187 wxPaintDC
dc(m_view
);
189 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
190 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
193 dc
.SetDeviceOrigin(-hscroll
->GetThumbPosition(), -vscroll
->GetThumbPosition());
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);
201 dc
.DrawLine(150, 20, 280, 280);
202 dc
.DrawLine(280, 280, 20, 280);
203 dc
.DrawLine(20, 280, 150, 20);
207 void SwitchView::OnErase(wxEraseEvent
& WXUNUSED(event
)) {
211 void SwitchView::OnSplit(wxDynamicSashSplitEvent
& WXUNUSED(event
)) {
212 SwitchView
*view
= new SwitchView(m_dyn_sash
);
213 view
->m_choice
->SetSelection(m_choice
->GetSelection());
215 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
216 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
218 hscroll
->SetEventHandler(this);
219 vscroll
->SetEventHandler(this);
222 void SwitchView::OnUnify(wxDynamicSashUnifyEvent
& WXUNUSED(event
)) {
223 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
224 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
226 hscroll
->SetEventHandler(this);
227 vscroll
->SetEventHandler(this);
230 void SwitchView::OnChoice(wxCommandEvent
& WXUNUSED(event
)) {
234 void SwitchView::OnScroll(wxScrollEvent
& WXUNUSED(event
)) {
238 void SwitchView::OnFocus(wxFocusEvent
& event
) {
239 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
240 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
242 if (event
.GetEventObject() == hscroll
|| event
.GetEventObject() == vscroll
) {
251 bool SwitchDemo::OnInit() {
253 wxDynamicSashWindow
*dyn
;
255 frame
= new wxFrame(NULL
, wxID_ANY
, wxT("Dynamic Sash Window Switch Demo"));
256 dyn
= new wxDynamicSashWindow(frame
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
259 frame
->SetSize(480, 480);