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