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 /////////////////////////////////////////////////////////////////////////////
17 #include <wx/choice.h>
18 #include <wx/dcclient.h>
19 #include <wx/gizmos/dynamicsash.h>
20 #include <wx/layout.h>
21 #include <wx/scrolbar.h>
23 class SwitchDemo
: public wxApp
{
29 class SwitchView
: public wxWindow
{
31 SwitchView(wxDynamicSashWindow
*parent
);
33 wxSize
DoGetBestSize() const;
36 void OnSize(wxSizeEvent
& event
);
37 void OnPaint(wxPaintEvent
& event
);
38 void OnChoice(wxCommandEvent
& event
);
39 void OnScroll(wxScrollEvent
& event
);
40 void OnFocus(wxFocusEvent
& event
);
41 void OnErase(wxEraseEvent
& event
);
42 void OnSplit(wxDynamicSashSplitEvent
& event
);
43 void OnUnify(wxDynamicSashUnifyEvent
& event
);
45 wxDynamicSashWindow
*m_dyn_sash
;
51 IMPLEMENT_APP(SwitchDemo
)
54 SwitchView::SwitchView(wxDynamicSashWindow
*win
) {
59 m_bar
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, wxRAISED_BORDER
, "bar");
60 m_choice
= new wxChoice(m_bar
, -1);
61 m_choice
->SetEventHandler(this);
62 m_view
= new wxWindow(this, -1, wxDefaultPosition
, wxDefaultSize
, 0, "view");
63 m_view
->SetBackgroundColour(*wxWHITE
);
64 m_view
->SetEventHandler(this);
66 m_choice
->Append("Triangle");
67 m_choice
->Append("Square");
68 m_choice
->SetSelection(0);
70 wxLayoutConstraints
*layout
;
72 layout
= new wxLayoutConstraints();
73 layout
->left
.Absolute(0);
74 layout
->top
.Absolute(0);
75 layout
->height
.Absolute(36);
76 layout
->width
.SameAs(this, wxWidth
);
77 m_bar
->SetConstraints(layout
);
79 layout
= new wxLayoutConstraints();
80 layout
->left
.Absolute(3);
82 layout
->height
.AsIs();
83 layout
->top
.Absolute(3);
84 m_choice
->SetConstraints(layout
);
86 layout
= new wxLayoutConstraints();
87 layout
->left
.Absolute(0);
88 layout
->width
.SameAs(this, wxWidth
);
89 layout
->top
.Below(m_bar
);
90 layout
->bottom
.SameAs(this, wxBottom
);
91 m_view
->SetConstraints(layout
);
93 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
94 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
96 hscroll
->SetEventHandler(this);
97 vscroll
->SetEventHandler(this);
99 Connect(GetId(), wxEVT_SIZE
, (wxObjectEventFunction
)&SwitchView::OnSize
);
100 Connect(m_choice
->GetId(), wxEVT_COMMAND_CHOICE_SELECTED
, (wxObjectEventFunction
)&SwitchView::OnChoice
);
101 Connect(m_view
->GetId(), wxEVT_PAINT
, (wxObjectEventFunction
)&SwitchView::OnPaint
);
103 Connect(-1, wxEVT_SET_FOCUS
, (wxObjectEventFunction
)&SwitchView::OnFocus
);
104 Connect(-1, wxEVT_SCROLL_TOP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
105 Connect(-1, wxEVT_SCROLL_BOTTOM
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
106 Connect(-1, wxEVT_SCROLL_LINEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
107 Connect(-1, wxEVT_SCROLL_LINEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
108 Connect(-1, wxEVT_SCROLL_PAGEUP
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
109 Connect(-1, wxEVT_SCROLL_PAGEDOWN
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
110 Connect(-1, wxEVT_SCROLL_THUMBTRACK
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
111 Connect(-1, wxEVT_SCROLL_THUMBRELEASE
, (wxObjectEventFunction
)&SwitchView::OnScroll
);
112 Connect(-1, wxEVT_ERASE_BACKGROUND
, (wxObjectEventFunction
)&SwitchView::OnErase
);
114 Connect(-1, wxEVT_DYNAMIC_SASH_SPLIT
, (wxObjectEventFunction
)&SwitchView::OnSplit
);
115 Connect(-1, wxEVT_DYNAMIC_SASH_UNIFY
, (wxObjectEventFunction
)&SwitchView::OnUnify
);
118 wxSize
SwitchView::DoGetBestSize() const {
119 return wxSize(64, 64);
122 void SwitchView::OnSize(wxSizeEvent
& event
) {
125 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
126 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
128 if (hscroll
&& vscroll
) {
129 int hpos
= hscroll
->GetThumbPosition();
130 int vpos
= vscroll
->GetThumbPosition();
132 wxSize size
= m_view
->GetSize();
133 if (hpos
+ size
.GetWidth() > 300)
134 hpos
= 300 - size
.GetWidth();
135 if (vpos
+ size
.GetHeight() > 300)
136 vpos
= 300 - size
.GetHeight();
138 hscroll
->SetScrollbar(hpos
, size
.GetWidth(), 300, size
.GetWidth());
139 vscroll
->SetScrollbar(vpos
, size
.GetHeight(), 300, size
.GetHeight());
143 void SwitchView::OnPaint(wxPaintEvent
& event
) {
144 wxPaintDC
dc(m_view
);
146 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
147 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
150 dc
.SetDeviceOrigin(-hscroll
->GetThumbPosition(), -vscroll
->GetThumbPosition());
152 if (m_choice
->GetSelection()) {
153 dc
.DrawLine(20, 20, 280, 20);
154 dc
.DrawLine(280, 20, 280, 280);
155 dc
.DrawLine(280, 280, 20, 280);
156 dc
.DrawLine(20, 280, 20, 20);
158 dc
.DrawLine(150, 20, 280, 280);
159 dc
.DrawLine(280, 280, 20, 280);
160 dc
.DrawLine(20, 280, 150, 20);
164 void SwitchView::OnErase(wxEraseEvent
& event
) {
168 void SwitchView::OnSplit(wxDynamicSashSplitEvent
& event
) {
169 SwitchView
*view
= new SwitchView(m_dyn_sash
);
170 view
->m_choice
->SetSelection(m_choice
->GetSelection());
172 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
173 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
175 hscroll
->SetEventHandler(this);
176 vscroll
->SetEventHandler(this);
179 void SwitchView::OnUnify(wxDynamicSashUnifyEvent
& event
) {
180 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
181 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
183 hscroll
->SetEventHandler(this);
184 vscroll
->SetEventHandler(this);
187 void SwitchView::OnChoice(wxCommandEvent
& event
) {
191 void SwitchView::OnScroll(wxScrollEvent
& event
) {
195 void SwitchView::OnFocus(wxFocusEvent
& event
) {
196 wxScrollBar
*hscroll
= m_dyn_sash
->GetHScrollBar(this);
197 wxScrollBar
*vscroll
= m_dyn_sash
->GetVScrollBar(this);
199 if (event
.m_eventObject
== hscroll
|| event
.m_eventObject
== vscroll
) {
208 bool SwitchDemo::OnInit() {
210 wxDynamicSashWindow
*dyn
;
212 frame
= new wxFrame(NULL
, -1, "Dynamic Sash Window Switch Demo");
213 dyn
= new wxDynamicSashWindow(frame
, -1, wxDefaultPosition
, wxDefaultSize
, wxCLIP_CHILDREN
);
216 frame
->SetSize(480, 480);