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 /////////////////////////////////////////////////////////////////////////////
16 #include <wx/choice.h>
17 #include <wx/dcclient.h>
18 #include <wx/gizmos/dynamicsash.h>
19 #include <wx/layout.h>
20 #include <wx/scrolbar.h>
22 class SwitchDemo
: public wxApp
{
28 class SwitchView
: public wxWindow
{
30 SwitchView(wxDynamicSashWindow
*parent
);
32 wxSize
DoGetBestSize() const;
35 void OnSize(wxSizeEvent
& event
);
36 void OnPaint(wxPaintEvent
& event
);
37 void OnChoice(wxCommandEvent
& event
);
38 void OnScroll(wxScrollEvent
& event
);
39 void OnFocus(wxFocusEvent
& event
);
40 void OnErase(wxEraseEvent
& event
);
41 void OnSplit(wxDynamicSashSplitEvent
& event
);
42 void OnUnify(wxDynamicSashUnifyEvent
& event
);
44 wxDynamicSashWindow
*m_dyn_sash
;
50 IMPLEMENT_APP(SwitchDemo
)
53 SwitchView::SwitchView(wxDynamicSashWindow
*win
) {
58 m_bar
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, wxRAISED_BORDER
, "bar");
59 m_choice
= new wxChoice(m_bar
, -1);
60 m_choice
->SetEventHandler(this);
61 m_view
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, 0, "view");
62 m_view
->SetBackgroundColour(*wxWHITE
);
63 m_view
->SetEventHandler(this);
65 m_choice
->Append("Triangle");
66 m_choice
->Append("Square");
67 m_choice
->SetSelection(0);
69 wxLayoutConstraints
*layout
;
71 layout
= new wxLayoutConstraints();
72 layout
->left
.Absolute(0);
73 layout
->top
.Absolute(0);
74 layout
->height
.Absolute(36);
75 layout
->width
.SameAs(this, wxWidth
);
76 m_bar
->SetConstraints(layout
);
78 layout
= new wxLayoutConstraints();
79 layout
->left
.Absolute(3);
81 layout
->height
.AsIs();
82 layout
->top
.Absolute(3);
83 m_choice
->SetConstraints(layout
);
85 layout
= new wxLayoutConstraints();
86 layout
->left
.Absolute(0);
87 layout
->width
.SameAs(this, wxWidth
);
88 layout
->top
.Below(m_bar
);
89 layout
->bottom
.SameAs(this, wxBottom
);
90 m_view
->SetConstraints(layout
);
92 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
93 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
95 hscroll
->SetEventHandler(this);
96 vscroll
->SetEventHandler(this);
98 Connect(GetId(), wxEVT_SIZE
, (wxObjectEventFunction
)&SwitchView::OnSize
);
99 Connect(m_choice
->GetId(), wxEVT_COMMAND_CHOICE_SELECTED
, (wxObjectEventFunction
)&SwitchView::OnChoice
);
100 Connect(m_view
->GetId(), wxEVT_PAINT
, (wxObjectEventFunction
)&SwitchView::OnPaint
);
102 Connect(-1, wxEVT_SET_FOCUS
, (wxObjectEventFunction
)&SwitchView::OnFocus
);
103 Connect(-1, wxEVT_SCROLL_TOP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
104 Connect(-1, wxEVT_SCROLL_BOTTOM
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
105 Connect(-1, wxEVT_SCROLL_LINEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
106 Connect(-1, wxEVT_SCROLL_LINEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
107 Connect(-1, wxEVT_SCROLL_PAGEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
108 Connect(-1, wxEVT_SCROLL_PAGEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
109 Connect(-1, wxEVT_SCROLL_THUMBTRACK
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
110 Connect(-1, wxEVT_SCROLL_THUMBRELEASE
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
111 Connect(-1, wxEVT_ERASE_BACKGROUND
, (wxObjectEventFunction
)&SwitchView::OnErase
);
113 Connect(-1, wxEVT_DYNAMIC_SASH_SPLIT
, (wxObjectEventFunction
)&SwitchView::OnSplit
);
114 Connect(-1, wxEVT_DYNAMIC_SASH_UNIFY
, (wxObjectEventFunction
)&SwitchView::OnUnify
);
117 wxSize
SwitchView::DoGetBestSize() const {
118 return wxSize(64, 64);
121 void SwitchView::OnSize(wxSizeEvent
& event
) {
124 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
125 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
127 if (hscroll
&& vscroll
) {
128 int hpos
= hscroll
->GetThumbPosition();
129 int vpos
= vscroll
->GetThumbPosition();
131 wxSize size
= m_view
->GetSize();
132 if (hpos
+ size
.GetWidth() > 300)
133 hpos
= 300 - size
.GetWidth();
134 if (vpos
+ size
.GetHeight() > 300)
135 vpos
= 300 - size
.GetHeight();
137 hscroll
->SetScrollbar(hpos
, size
.GetWidth(), 300, size
.GetWidth());
138 vscroll
->SetScrollbar(vpos
, size
.GetHeight(), 300, size
.GetHeight());
142 void SwitchView::OnPaint(wxPaintEvent
& event
) {
143 wxPaintDC
dc(m_view
);
145 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
146 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
149 dc
.SetDeviceOrigin(-hscroll
->GetThumbPosition(), -vscroll
->GetThumbPosition());
151 if (m_choice
->GetSelection()) {
152 dc
.DrawLine(20, 20, 280, 20);
153 dc
.DrawLine(280, 20, 280, 280);
154 dc
.DrawLine(280, 280, 20, 280);
155 dc
.DrawLine(20, 280, 20, 20);
157 dc
.DrawLine(150, 20, 280, 280);
158 dc
.DrawLine(280, 280, 20, 280);
159 dc
.DrawLine(20, 280, 150, 20);
163 void SwitchView::OnErase(wxEraseEvent
& event
) {
167 void SwitchView::OnSplit(wxDynamicSashSplitEvent
& event
) {
168 SwitchView
*view
= new SwitchView(m_dyn_sash
);
169 view
->m_choice
->SetSelection(m_choice
->GetSelection());
171 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
172 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
174 hscroll
->SetEventHandler(this);
175 vscroll
->SetEventHandler(this);
178 void SwitchView::OnUnify(wxDynamicSashUnifyEvent
& event
) {
179 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
180 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
182 hscroll
->SetEventHandler(this);
183 vscroll
->SetEventHandler(this);
186 void SwitchView::OnChoice(wxCommandEvent
& event
) {
190 void SwitchView::OnScroll(wxScrollEvent
& event
) {
194 void SwitchView::OnFocus(wxFocusEvent
& event
) {
195 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
196 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
198 if (event
.m_eventObject
== hscroll
|| event
.m_eventObject
== vscroll
) {
207 bool SwitchDemo::OnInit() {
209 wxDynamicSashWindow
*dyn
;
211 frame
= new wxFrame(NULL
, -1, "Dynamic Sash Window Switch Demo");
212 dyn
= new wxDynamicSashWindow(frame
, -1, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
215 frame
->SetSize(480, 480);