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
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" wxWindows 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
) {
72 m_bar
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, wxRAISED_BORDER
, "bar");
73 m_choice
= new wxChoice(m_bar
, -1);
74 m_choice
->SetEventHandler(this);
75 m_view
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, 0, "view");
76 m_view
->SetBackgroundColour(*wxWHITE
);
77 m_view
->SetEventHandler(this);
79 m_choice
->Append("Triangle");
80 m_choice
->Append("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
)&SwitchView::OnSize
);
113 Connect(m_choice
->GetId(), wxEVT_COMMAND_CHOICE_SELECTED
, (wxObjectEventFunction
)&SwitchView::OnChoice
);
114 Connect(m_view
->GetId(), wxEVT_PAINT
, (wxObjectEventFunction
)&SwitchView::OnPaint
);
116 Connect(-1, wxEVT_SET_FOCUS
, (wxObjectEventFunction
)&SwitchView::OnFocus
);
117 Connect(-1, wxEVT_SCROLL_TOP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
118 Connect(-1, wxEVT_SCROLL_BOTTOM
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
119 Connect(-1, wxEVT_SCROLL_LINEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
120 Connect(-1, wxEVT_SCROLL_LINEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
121 Connect(-1, wxEVT_SCROLL_PAGEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
122 Connect(-1, wxEVT_SCROLL_PAGEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
123 Connect(-1, wxEVT_SCROLL_THUMBTRACK
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
124 Connect(-1, wxEVT_SCROLL_THUMBRELEASE
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
125 Connect(-1, wxEVT_ERASE_BACKGROUND
, (wxObjectEventFunction
)&SwitchView::OnErase
);
127 Connect(-1, wxEVT_DYNAMIC_SASH_SPLIT
, (wxObjectEventFunction
)&SwitchView::OnSplit
);
128 Connect(-1, wxEVT_DYNAMIC_SASH_UNIFY
, (wxObjectEventFunction
)&SwitchView::OnUnify
);
131 wxSize
SwitchView::DoGetBestSize() const {
132 return wxSize(64, 64);
135 void SwitchView::OnSize(wxSizeEvent
& event
) {
138 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
139 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
141 if (hscroll
&& vscroll
) {
142 int hpos
= hscroll
->GetThumbPosition();
143 int vpos
= vscroll
->GetThumbPosition();
145 wxSize size
= m_view
->GetSize();
146 if (hpos
+ size
.GetWidth() > 300)
147 hpos
= 300 - size
.GetWidth();
148 if (vpos
+ size
.GetHeight() > 300)
149 vpos
= 300 - size
.GetHeight();
151 hscroll
->SetScrollbar(hpos
, size
.GetWidth(), 300, size
.GetWidth());
152 vscroll
->SetScrollbar(vpos
, size
.GetHeight(), 300, size
.GetHeight());
156 void SwitchView::OnPaint(wxPaintEvent
& event
) {
157 wxPaintDC
dc(m_view
);
159 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
160 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
163 dc
.SetDeviceOrigin(-hscroll
->GetThumbPosition(), -vscroll
->GetThumbPosition());
165 if (m_choice
->GetSelection()) {
166 dc
.DrawLine(20, 20, 280, 20);
167 dc
.DrawLine(280, 20, 280, 280);
168 dc
.DrawLine(280, 280, 20, 280);
169 dc
.DrawLine(20, 280, 20, 20);
171 dc
.DrawLine(150, 20, 280, 280);
172 dc
.DrawLine(280, 280, 20, 280);
173 dc
.DrawLine(20, 280, 150, 20);
177 void SwitchView::OnErase(wxEraseEvent
& event
) {
181 void SwitchView::OnSplit(wxDynamicSashSplitEvent
& event
) {
182 SwitchView
*view
= new SwitchView(m_dyn_sash
);
183 view
->m_choice
->SetSelection(m_choice
->GetSelection());
185 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
186 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
188 hscroll
->SetEventHandler(this);
189 vscroll
->SetEventHandler(this);
192 void SwitchView::OnUnify(wxDynamicSashUnifyEvent
& event
) {
193 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
194 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
196 hscroll
->SetEventHandler(this);
197 vscroll
->SetEventHandler(this);
200 void SwitchView::OnChoice(wxCommandEvent
& event
) {
204 void SwitchView::OnScroll(wxScrollEvent
& event
) {
208 void SwitchView::OnFocus(wxFocusEvent
& event
) {
209 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
210 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
212 if (event
.m_eventObject
== hscroll
|| event
.m_eventObject
== vscroll
) {
221 bool SwitchDemo::OnInit() {
223 wxDynamicSashWindow
*dyn
;
225 frame
= new wxFrame(NULL
, -1, "Dynamic Sash Window Switch Demo");
226 dyn
= new wxDynamicSashWindow(frame
, -1, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
229 frame
->SetSize(480, 480);