NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
wxProportionalSplitterWindow.h
Go to the documentation of this file.
1#ifndef PROPORTIONALSPLITTER
2#define PROPORTIONALSPLITTER
3
4//*****************************************************************************
5// wxProportionalSplitterWindow
6//*****************************************************************************
7
8#include <wx/wxprec.h>
9#ifdef __BORLANDC__
10#pragma hdrstop
11#endif
12#ifndef WX_PRECOMP
13//here the list of needed .h files that are included in wx/wx.h
14#endif
15
16#include <wx/splitter.h>
17#include <cmath>
18
24class ProportionalSplitterWindow : public wxSplitterWindow
25{
26 enum { MIN_PANE_SIZE = 1 };
27
28 public:
32 ProportionalSplitterWindow() : wxSplitterWindow(), splitPercent_(0.5f), m_charHeight(0), m_defaultHeight(false)
33 {}
34
47 ProportionalSplitterWindow(wxWindow* parent, wxWindowID id = wxID_ANY, float proportion = 0.5f,
48 const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxSP_3D)
49 : wxSplitterWindow(parent, id, pos, size, style), splitPercent_(fabs(proportion))
50 {
51 wxASSERT_MSG( GetParent(), wxT("wxProportionalSplitterWindow parent window ptr cannot be null") );
52
53 // Connect event handlers
54 Connect(GetId(), wxEVT_SIZE, (wxObjectEventFunction)(wxEventFunction)&ProportionalSplitterWindow::OnSize);
55
56 // Connect event handlers
57 Connect(GetId(), wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED,
58 (wxObjectEventFunction)(wxEventFunction)&ProportionalSplitterWindow::OnSashPosChanged );
59
60 // prevents double-click unsplit
61 SetMinimumPaneSize(MIN_PANE_SIZE);
62
63 if (proportion < 0)
64 m_defaultHeight = true;
65 else
66 m_defaultHeight = false;
67
68 m_charHeight = 10; // Arbitrary set size to 10px height;
69 if ( splitPercent_ < 0.0 || splitPercent_ > 1.0 )
70 splitPercent_ = 0.5f;
71
72 }
73
84 virtual bool SplitHorizontally(wxWindow* window1, wxWindow* window2, float proportion = 0)
85 {
86 int splitSize = initSplitSize(proportion, true);
87 return wxSplitterWindow::SplitHorizontally(window1, window2, splitSize);
88 }
89
100 virtual bool SplitVertically(wxWindow* window1, wxWindow* window2, float proportion = 0)
101 {
102 int splitSize = initSplitSize(proportion, false);
103 return wxSplitterWindow::SplitVertically(window1, window2, splitSize);
104 }
105
114 void SetSashPositionFloat(float proportion)
115 {
116 int splitSize = initSplitSize(proportion, isHorizontal());
117 SetSashPosition(splitSize);
118 }
119
130 void OnSize(wxSizeEvent& event)
131 {
132 float parSize = parentSize();
133 int nSashSize = GetSashSize();
134
136 {
137 if (rint((1.0-fabs(splitPercent_))*parSize / m_charHeight) <= 20)
138 SetSashPosition(parSize - rint((1.0-fabs(splitPercent_))*parSize / m_charHeight)*m_charHeight + nSashSize, false);
139 else
140 {
141 splitPercent_ = -(1.0 - 20 * m_charHeight / parSize);
142 SetSashPosition(parSize - 20 * m_charHeight + nSashSize, false);
143 }
144 }
145 else
146 SetSashPosition(parSize - rint((1.0-fabs(splitPercent_))*parSize / m_charHeight)*m_charHeight + nSashSize, false);
147
148 event.Skip();
149 }
150
161 void OnSashPosChanged(wxSplitterEvent& event)
162 {
163 float parSize = parentSize();
164 float percent = event.GetSashPosition() / parSize;
165
166 if (percent > 0.0 && percent < 1.0)
167 splitPercent_ = percent;
168
169 m_defaultHeight = false;
170 SetSashPosition(parSize-rint((1.0 - splitPercent_)*parSize / m_charHeight)*m_charHeight + GetSashSize(), false);
171
172 event.Skip();
173 }
174
183 {
184 return splitPercent_;
185 }
186
194 void SetCharHeigth(int _charheight)
195 {
196 m_charHeight = _charheight;
197 }
198
199 private:
200
208 bool isHorizontal() const
209 {
210 return (GetSplitMode() == wxSPLIT_HORIZONTAL);
211 }
212
221 float parentSize() const
222 {
223 return (isHorizontal()
224 ? (float)GetParent()->GetClientSize().GetHeight()
225 : (float)GetParent()->GetClientSize().GetWidth());
226 }
227
237 int initSplitSize(float proportion, bool isHorizontal)
238 {
239 SetSplitMode(isHorizontal ? wxSPLIT_HORIZONTAL : wxSPLIT_VERTICAL);
240
241 // If the proportion is not equal to zero,
242 // we will use it for the final proportion
243 if (proportion != 0)
244 {
245 // Negative values will trigger the automatic
246 // resizing mode
247 if (proportion < 0)
248 {
249 m_defaultHeight = true;
250 proportion = fabs(proportion);
251 }
252 else
253 m_defaultHeight = false;
254
255 splitPercent_ = (proportion < 0.0 || proportion > 1.0) ? 0.5f : proportion;
256 }
257 else if (fabs(splitPercent_) > 1.0)
258 splitPercent_ = 0.5f;
259
260 // Get the free space of the parent
261 int size = parentSize();
262
263 // Use the automatic resizing mode or use the
264 // passed proportion directly
265 if (m_defaultHeight)
266 {
267 if (rint((1.0 - fabs(splitPercent_))*size / m_charHeight) <= 20)
268 return size - rint((1.0 - fabs(splitPercent_)) * size / m_charHeight) * m_charHeight + GetSashSize();
269 else
270 {
271 splitPercent_ = -(1.0 - 20 * m_charHeight / size);
272 return size - 20 * m_charHeight + GetSashSize();
273 }
274 }
275 else
276 return size - rint((1.0 - fabs(splitPercent_)) * size / m_charHeight) * m_charHeight + GetSashSize();
277 }
278
279 private:
283 DECLARE_NO_COPY_CLASS(ProportionalSplitterWindow)
284};
285
286#endif
This class represents a splitter window, which can controlled using a floating point proportion inste...
void SetCharHeigth(int _charheight)
Change the character height in pixels.
ProportionalSplitterWindow(wxWindow *parent, wxWindowID id=wxID_ANY, float proportion=0.5f, const wxPoint &pos=wxDefaultPosition, const wxSize &size=wxDefaultSize, long style=wxSP_3D)
Construct a proportional splitter window using a set of parameters.
virtual bool SplitVertically(wxWindow *window1, wxWindow *window2, float proportion=0)
Split this window vertically using the selected porportion.
void OnSize(wxSizeEvent &event)
Handle the window size event. Will automatically adapt the sash position to achieve a multiple of a c...
void OnSashPosChanged(wxSplitterEvent &event)
Handle the size event, when the sash position has been changed. Will automatically adapt the sash pos...
float GetSplitPercentage()
Return the current splitting proportion.
virtual bool SplitHorizontally(wxWindow *window1, wxWindow *window2, float proportion=0)
Split this window horizontally using the selected porportion.
ProportionalSplitterWindow()
Default constructor.
void SetSashPositionFloat(float proportion)
Set the sash position using a floating point proportion.
float parentSize() const
Return the free space of the parent as a floating point number. Considers the orientation of the spli...
bool isHorizontal() const
Return true, if the splitter is horizontal.
int initSplitSize(float proportion, bool isHorizontal)
Calculates the initial sash position after splitting the window.
value_type rint(value_type v)