]>
Commit | Line | Data |
---|---|---|
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 | #include <wx/app.h> | |
16 | #include <wx/frame.h> | |
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> | |
22 | ||
23 | class SwitchDemo : public wxApp { | |
24 | public: | |
25 | bool OnInit(); | |
26 | }; | |
27 | ||
28 | ||
29 | class SwitchView : public wxWindow { | |
30 | public: | |
31 | SwitchView(wxDynamicSashWindow *parent); | |
32 | ||
33 | wxSize DoGetBestSize() const; | |
34 | ||
35 | private: | |
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); | |
44 | ||
45 | wxDynamicSashWindow *m_dyn_sash; | |
46 | wxWindow *m_bar; | |
47 | wxChoice *m_choice; | |
48 | wxWindow *m_view; | |
49 | }; | |
50 | ||
51 | IMPLEMENT_APP(SwitchDemo) | |
52 | ||
53 | ||
54 | SwitchView::SwitchView(wxDynamicSashWindow *win) { | |
55 | Create(win, -1); | |
56 | ||
57 | m_dyn_sash = win; | |
58 | ||
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); | |
65 | ||
66 | m_choice->Append("Triangle"); | |
67 | m_choice->Append("Square"); | |
68 | m_choice->SetSelection(0); | |
69 | ||
70 | wxLayoutConstraints *layout; | |
71 | ||
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); | |
78 | ||
79 | layout = new wxLayoutConstraints(); | |
80 | layout->left.Absolute(3); | |
81 | layout->width.AsIs(); | |
82 | layout->height.AsIs(); | |
83 | layout->top.Absolute(3); | |
84 | m_choice->SetConstraints(layout); | |
85 | ||
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); | |
92 | ||
93 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
94 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
95 | ||
96 | hscroll->SetEventHandler(this); | |
97 | vscroll->SetEventHandler(this); | |
98 | ||
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); | |
102 | ||
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); | |
113 | ||
114 | Connect(-1, wxEVT_DYNAMIC_SASH_SPLIT, (wxObjectEventFunction)&SwitchView::OnSplit); | |
115 | Connect(-1, wxEVT_DYNAMIC_SASH_UNIFY, (wxObjectEventFunction)&SwitchView::OnUnify); | |
116 | } | |
117 | ||
118 | wxSize SwitchView::DoGetBestSize() const { | |
119 | return wxSize(64, 64); | |
120 | } | |
121 | ||
122 | void SwitchView::OnSize(wxSizeEvent& event) { | |
123 | Layout(); | |
124 | ||
125 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
126 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
127 | ||
128 | if (hscroll && vscroll) { | |
129 | int hpos = hscroll->GetThumbPosition(); | |
130 | int vpos = vscroll->GetThumbPosition(); | |
131 | ||
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(); | |
137 | ||
138 | hscroll->SetScrollbar(hpos, size.GetWidth(), 300, size.GetWidth()); | |
139 | vscroll->SetScrollbar(vpos, size.GetHeight(), 300, size.GetHeight()); | |
140 | } | |
141 | } | |
142 | ||
143 | void SwitchView::OnPaint(wxPaintEvent& event) { | |
144 | wxPaintDC dc(m_view); | |
145 | ||
146 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
147 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
148 | ||
149 | dc.Clear(); | |
150 | dc.SetDeviceOrigin(-hscroll->GetThumbPosition(), -vscroll->GetThumbPosition()); | |
151 | ||
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); | |
157 | } else { | |
158 | dc.DrawLine(150, 20, 280, 280); | |
159 | dc.DrawLine(280, 280, 20, 280); | |
160 | dc.DrawLine(20, 280, 150, 20); | |
161 | } | |
162 | } | |
163 | ||
164 | void SwitchView::OnErase(wxEraseEvent& event) { | |
165 | // Do nothing | |
166 | } | |
167 | ||
168 | void SwitchView::OnSplit(wxDynamicSashSplitEvent& event) { | |
169 | SwitchView *view = new SwitchView(m_dyn_sash); | |
170 | view->m_choice->SetSelection(m_choice->GetSelection()); | |
171 | ||
172 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
173 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
174 | ||
175 | hscroll->SetEventHandler(this); | |
176 | vscroll->SetEventHandler(this); | |
177 | } | |
178 | ||
179 | void SwitchView::OnUnify(wxDynamicSashUnifyEvent& event) { | |
180 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
181 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
182 | ||
183 | hscroll->SetEventHandler(this); | |
184 | vscroll->SetEventHandler(this); | |
185 | } | |
186 | ||
187 | void SwitchView::OnChoice(wxCommandEvent& event) { | |
188 | m_view->Refresh(); | |
189 | } | |
190 | ||
191 | void SwitchView::OnScroll(wxScrollEvent& event) { | |
192 | m_view->Refresh(); | |
193 | } | |
194 | ||
195 | void SwitchView::OnFocus(wxFocusEvent& event) { | |
196 | wxScrollBar *hscroll = m_dyn_sash->GetHScrollBar(this); | |
197 | wxScrollBar *vscroll = m_dyn_sash->GetVScrollBar(this); | |
198 | ||
199 | if (event.m_eventObject == hscroll || event.m_eventObject == vscroll) { | |
200 | m_view->SetFocus(); | |
201 | } else { | |
202 | event.Skip(); | |
203 | } | |
204 | } | |
205 | ||
206 | ||
207 | ||
208 | bool SwitchDemo::OnInit() { | |
209 | wxFrame *frame; | |
210 | wxDynamicSashWindow *dyn; | |
211 | ||
212 | frame = new wxFrame(NULL, -1, "Dynamic Sash Window Switch Demo"); | |
213 | dyn = new wxDynamicSashWindow(frame, -1, wxDefaultPosition, wxDefaultSize, wxCLIP_CHILDREN); | |
214 | new SwitchView(dyn); | |
215 | ||
216 | frame->SetSize(480, 480); | |
217 | frame->Show(); | |
218 | ||
219 | return TRUE; | |
220 | } |