1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxSplitterWindow class
4 // Author: Julian Smart
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #ifndef __SPLITTERH_G__
13 #define __SPLITTERH_G__
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "splitter.h"
19 #include "wx/window.h" // base class declaration
20 #include "wx/containr.h" // wxControlContainer
22 class WXDLLEXPORT wxSplitterEvent
;
24 // ---------------------------------------------------------------------------
26 // ---------------------------------------------------------------------------
30 wxSPLIT_HORIZONTAL
= 1,
37 wxSPLIT_DRAG_DRAGGING
,
38 wxSPLIT_DRAG_LEFT_DOWN
41 // ---------------------------------------------------------------------------
42 // wxSplitterWindow maintains one or two panes, with
43 // an optional vertical or horizontal split which
44 // can be used with the mouse or programmatically.
45 // ---------------------------------------------------------------------------
48 // 1) Perhaps make the borders sensitive to dragging in order to create a split.
49 // The MFC splitter window manages scrollbars as well so is able to
50 // put sash buttons on the scrollbars, but we probably don't want to go down
52 // 2) for wxWidgets 2.0, we must find a way to set the WS_CLIPCHILDREN style
53 // to prevent flickering. (WS_CLIPCHILDREN doesn't work in all cases so can't be
56 class WXDLLEXPORT wxSplitterWindow
: public wxWindow
60 ////////////////////////////////////////////////////////////////////////////
63 // Default constructor
70 wxSplitterWindow(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
71 const wxPoint
& pos
= wxDefaultPosition
,
72 const wxSize
& size
= wxDefaultSize
,
74 const wxString
& name
= wxT("splitter"))
77 Create(parent
, id
, pos
, size
, style
, name
);
80 virtual ~wxSplitterWindow();
82 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
83 const wxPoint
& pos
= wxDefaultPosition
,
84 const wxSize
& size
= wxDefaultSize
,
86 const wxString
& name
= wxT("splitter"));
88 // Gets the only or left/top pane
89 wxWindow
*GetWindow1() const { return m_windowOne
; }
91 // Gets the right/bottom pane
92 wxWindow
*GetWindow2() const { return m_windowTwo
; }
94 // Sets the split mode
95 void SetSplitMode(int mode
)
97 wxASSERT_MSG( mode
== wxSPLIT_VERTICAL
|| mode
== wxSPLIT_HORIZONTAL
,
98 _T("invalid split mode") );
100 m_splitMode
= (wxSplitMode
)mode
;
103 // Gets the split mode
104 wxSplitMode
GetSplitMode() const { return m_splitMode
; };
106 // Initialize with one window
107 void Initialize(wxWindow
*window
);
109 // Associates the given window with window 2, drawing the appropriate sash
110 // and changing the split mode.
111 // Does nothing and returns false if the window is already split.
112 // A sashPosition of 0 means choose a default sash position,
113 // negative sashPosition specifies the size of right/lower pane as it's
114 // absolute value rather than the size of left/upper pane.
115 virtual bool SplitVertically(wxWindow
*window1
,
117 int sashPosition
= 0)
118 { return DoSplit(wxSPLIT_VERTICAL
, window1
, window2
, sashPosition
); }
119 virtual bool SplitHorizontally(wxWindow
*window1
,
121 int sashPosition
= 0)
122 { return DoSplit(wxSPLIT_HORIZONTAL
, window1
, window2
, sashPosition
); }
124 // Removes the specified (or second) window from the view
125 // Doesn't actually delete the window.
126 bool Unsplit(wxWindow
*toRemove
= (wxWindow
*) NULL
);
128 // Replaces one of the windows with another one (neither old nor new
129 // parameter should be NULL)
130 bool ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
);
132 // Make sure the child window sizes are updated. This is useful
133 // for reducing flicker by updating the sizes before a
134 // window is shown, if you know the overall size is correct.
137 // Is the window split?
138 bool IsSplit() const { return (m_windowTwo
!= NULL
); }
140 // Sets the sash size
141 void SetSashSize(int width
) { m_sashSize
= width
; }
143 // Sets the border size
144 void SetBorderSize(int WXUNUSED(width
)) { }
146 // Gets the sash size
147 int GetSashSize() const;
149 // Gets the border size
150 int GetBorderSize() const;
152 // Set the sash position
153 void SetSashPosition(int position
, bool redraw
= true);
155 // Gets the sash position
156 int GetSashPosition() const { return m_sashPosition
; }
158 // Set the sash gravity
159 void SetSashGravity(double gravity
);
161 // Gets the sash gravity
162 double GetSashGravity() const { return m_sashGravity
; }
164 // If this is zero, we can remove panes by dragging the sash.
165 void SetMinimumPaneSize(int min
);
166 int GetMinimumPaneSize() const { return m_minimumPaneSize
; }
168 // NB: the OnXXX() functions below are for backwards compatibility only,
169 // don't use them in new code but handle the events instead!
171 // called when the sash position is about to change, may return a new value
172 // for the sash or -1 to prevent the change from happening at all
173 virtual int OnSashPositionChanging(int newSashPosition
);
175 // Called when the sash position is about to be changed, return
176 // false from here to prevent the change from taking place.
177 // Repositions sash to minimum position if pane would be too small.
178 // newSashPosition here is always positive or zero.
179 virtual bool OnSashPositionChange(int newSashPosition
);
181 // If the sash is moved to an extreme position, a subwindow
182 // is removed from the splitter window, and the app is
183 // notified. The app should delete or hide the window.
184 virtual void OnUnsplit(wxWindow
*removed
);
186 // Called when the sash is double-clicked.
187 // The default behaviour is to remove the sash if the
188 // minimum pane size is zero.
189 virtual void OnDoubleClickSash(int x
, int y
);
191 ////////////////////////////////////////////////////////////////////////////
194 // Paints the border and sash
195 void OnPaint(wxPaintEvent
& event
);
197 // Handles mouse events
198 void OnMouseEvent(wxMouseEvent
& ev
);
201 void OnSize(wxSizeEvent
& event
);
203 // In live mode, resize child windows in idle time
204 void OnInternalIdle();
207 virtual void DrawSash(wxDC
& dc
);
209 // Draws the sash tracker (for whilst moving the sash)
210 virtual void DrawSashTracker(int x
, int y
);
212 // Tests for x, y over sash
213 virtual bool SashHitTest(int x
, int y
, int tolerance
= 5);
215 // Resizes subwindows
216 virtual void SizeWindows();
218 void SetNeedUpdating(bool needUpdating
) { m_needUpdating
= needUpdating
; }
219 bool GetNeedUpdating() const { return m_needUpdating
; }
222 virtual bool MacClipGrandChildren() const { return true ; }
227 #if defined(__WXMSW__) || defined(__WXMAC__)
228 void OnSetCursor(wxSetCursorEvent
& event
);
231 // send the given event, return false if the event was processed and vetoed
233 inline bool DoSendEvent(wxSplitterEvent
& event
);
235 // common part of all ctors
238 // common part of SplitVertically() and SplitHorizontally()
239 bool DoSplit(wxSplitMode mode
,
240 wxWindow
*window1
, wxWindow
*window2
,
243 // adjusts sash position with respect to min. pane and window sizes
244 int AdjustSashPosition(int sashPos
) const;
246 // get either width or height depending on the split mode
247 int GetWindowSize() const;
249 // convert the user specified sash position which may be > 0 (as is), < 0
250 // (specifying the size of the right pane) or 0 (use default) to the real
251 // position to be passed to DoSetSashPosition()
252 int ConvertSashPosition(int sashPos
) const;
254 // set the real sash position, sashPos here must be positive
256 // returns true if the sash position has been changed, false otherwise
257 bool DoSetSashPosition(int sashPos
);
259 // set the sash position and send an event about it having been changed
260 void SetSashPositionAndNotify(int sashPos
);
262 // callbacks executed when we detect that the mouse has entered or left
264 virtual void OnEnterSash();
265 virtual void OnLeaveSash();
267 // set the cursor appropriate for the current split mode
268 void SetResizeCursor();
270 // redraw the splitter if its "hotness" changed if necessary
271 void RedrawIfHotSensitive(bool isHot
);
273 // return the best size of the splitter equal to best sizes of its
275 virtual wxSize
DoGetBestSize() const;
278 wxSplitMode m_splitMode
;
279 wxWindow
* m_windowOne
;
280 wxWindow
* m_windowTwo
;
284 int m_sashPosition
; // Number of pixels from left or top
285 double m_sashGravity
;
288 int m_requestedSashPosition
;
289 int m_sashPositionCurrent
; // while dragging
292 int m_minimumPaneSize
;
293 wxCursor m_sashCursorWE
;
294 wxCursor m_sashCursorNS
;
295 wxPen
*m_sashTrackerPen
;
297 // when in live mode, set this to true to resize children in idle
298 bool m_needUpdating
:1;
299 bool m_permitUnsplitAlways
:1;
301 bool m_checkRequestedSashPosition
:1;
304 WX_DECLARE_CONTROL_CONTAINER();
306 DECLARE_DYNAMIC_CLASS(wxSplitterWindow
)
307 DECLARE_EVENT_TABLE()
308 DECLARE_NO_COPY_CLASS(wxSplitterWindow
)
311 // ----------------------------------------------------------------------------
312 // event class and macros
313 // ----------------------------------------------------------------------------
315 // we reuse the same class for all splitter event types because this is the
316 // usual wxWin convention, but the three event types have different kind of
317 // data associated with them, so the accessors can be only used if the real
318 // event type matches with the one for which the accessors make sense
319 class WXDLLEXPORT wxSplitterEvent
: public wxNotifyEvent
322 wxSplitterEvent(wxEventType type
= wxEVT_NULL
,
323 wxSplitterWindow
*splitter
= (wxSplitterWindow
*)NULL
)
324 : wxNotifyEvent(type
)
326 SetEventObject(splitter
);
327 if (splitter
) m_id
= splitter
->GetId();
330 // SASH_POS_CHANGED methods
332 // setting the sash position to -1 prevents the change from taking place at
334 void SetSashPosition(int pos
)
336 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
337 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
);
342 int GetSashPosition() const
344 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
345 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING
);
350 // UNSPLIT event methods
351 wxWindow
*GetWindowBeingRemoved() const
353 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_UNSPLIT
);
358 // DCLICK event methods
361 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
);
368 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED
);
374 friend class WXDLLEXPORT wxSplitterWindow
;
376 // data for the different types of event
379 int pos
; // position for SASH_POS_CHANGED event
380 wxWindow
*win
; // window being removed for UNSPLIT event
384 } pt
; // position of double click for DCLICK event
387 DECLARE_DYNAMIC_CLASS_NO_COPY(wxSplitterEvent
)
390 typedef void (wxEvtHandler::*wxSplitterEventFunction
)(wxSplitterEvent
&);
392 #define wxSplitterEventHandler(func) \
393 (wxObjectEventFunction)(wxEventFunction)wxStaticCastEvent(wxSplitterEventFunction, &func)
395 #define wx__DECLARE_SPLITTEREVT(evt, id, fn) \
396 wx__DECLARE_EVT1(wxEVT_COMMAND_SPLITTER_ ## evt, id, wxSplitterEventHandler(fn))
398 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
399 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGED, id, fn)
401 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
402 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGING, id, fn)
404 #define EVT_SPLITTER_DCLICK(id, fn) \
405 wx__DECLARE_SPLITTEREVT(DOUBLECLICKED, id, fn)
407 #define EVT_SPLITTER_UNSPLIT(id, fn) \
408 wx__DECLARE_SPLITTEREVT(UNSPLIT, id, fn)
410 #endif // __SPLITTERH_G__