1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/splitter.h
3 // Purpose: wxSplitterWindow class
4 // Author: Julian Smart
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 #ifndef _WX_GENERIC_SPLITTER_H_
12 #define _WX_GENERIC_SPLITTER_H_
14 #include "wx/window.h" // base class declaration
15 #include "wx/containr.h" // wxControlContainer
17 class WXDLLIMPEXP_FWD_CORE wxSplitterEvent
;
19 // ---------------------------------------------------------------------------
21 // ---------------------------------------------------------------------------
25 wxSPLIT_HORIZONTAL
= 1,
32 wxSPLIT_DRAG_DRAGGING
,
33 wxSPLIT_DRAG_LEFT_DOWN
36 // ---------------------------------------------------------------------------
37 // wxSplitterWindow maintains one or two panes, with
38 // an optional vertical or horizontal split which
39 // can be used with the mouse or programmatically.
40 // ---------------------------------------------------------------------------
43 // 1) Perhaps make the borders sensitive to dragging in order to create a split.
44 // The MFC splitter window manages scrollbars as well so is able to
45 // put sash buttons on the scrollbars, but we probably don't want to go down
47 // 2) for wxWidgets 2.0, we must find a way to set the WS_CLIPCHILDREN style
48 // to prevent flickering. (WS_CLIPCHILDREN doesn't work in all cases so can't be
51 class WXDLLIMPEXP_CORE wxSplitterWindow
: public wxNavigationEnabled
<wxWindow
>
55 ////////////////////////////////////////////////////////////////////////////
58 // Default constructor
65 wxSplitterWindow(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
66 const wxPoint
& pos
= wxDefaultPosition
,
67 const wxSize
& size
= wxDefaultSize
,
69 const wxString
& name
= wxT("splitter"))
72 Create(parent
, id
, pos
, size
, style
, name
);
75 virtual ~wxSplitterWindow();
77 bool Create(wxWindow
*parent
, wxWindowID id
= wxID_ANY
,
78 const wxPoint
& pos
= wxDefaultPosition
,
79 const wxSize
& size
= wxDefaultSize
,
81 const wxString
& name
= wxT("splitter"));
83 // Gets the only or left/top pane
84 wxWindow
*GetWindow1() const { return m_windowOne
; }
86 // Gets the right/bottom pane
87 wxWindow
*GetWindow2() const { return m_windowTwo
; }
89 // Sets the split mode
90 void SetSplitMode(int mode
)
92 wxASSERT_MSG( mode
== wxSPLIT_VERTICAL
|| mode
== wxSPLIT_HORIZONTAL
,
93 wxT("invalid split mode") );
95 m_splitMode
= (wxSplitMode
)mode
;
98 // Gets the split mode
99 wxSplitMode
GetSplitMode() const { return m_splitMode
; }
101 // Initialize with one window
102 void Initialize(wxWindow
*window
);
104 // Associates the given window with window 2, drawing the appropriate sash
105 // and changing the split mode.
106 // Does nothing and returns false if the window is already split.
107 // A sashPosition of 0 means choose a default sash position,
108 // negative sashPosition specifies the size of right/lower pane as it's
109 // absolute value rather than the size of left/upper pane.
110 virtual bool SplitVertically(wxWindow
*window1
,
112 int sashPosition
= 0)
113 { return DoSplit(wxSPLIT_VERTICAL
, window1
, window2
, sashPosition
); }
114 virtual bool SplitHorizontally(wxWindow
*window1
,
116 int sashPosition
= 0)
117 { return DoSplit(wxSPLIT_HORIZONTAL
, window1
, window2
, sashPosition
); }
119 // Removes the specified (or second) window from the view
120 // Doesn't actually delete the window.
121 bool Unsplit(wxWindow
*toRemove
= NULL
);
123 // Replaces one of the windows with another one (neither old nor new
124 // parameter should be NULL)
125 bool ReplaceWindow(wxWindow
*winOld
, wxWindow
*winNew
);
127 // Make sure the child window sizes are updated. This is useful
128 // for reducing flicker by updating the sizes before a
129 // window is shown, if you know the overall size is correct.
132 // Is the window split?
133 bool IsSplit() const { return (m_windowTwo
!= NULL
); }
135 // Sets the border size
136 void SetBorderSize(int WXUNUSED(width
)) { }
138 // Hide or show the sash and test whether it's currently hidden.
139 void SetSashInvisible(bool invisible
= true);
140 bool IsSashInvisible() const { return HasFlag(wxSP_NOSASH
); }
142 // Gets the current sash size which may be 0 if it's hidden and the default
144 int GetSashSize() const;
145 int GetDefaultSashSize() const;
147 // Gets the border size
148 int GetBorderSize() const;
150 // Set the sash position
151 void SetSashPosition(int position
, bool redraw
= true);
153 // Gets the sash position
154 int GetSashPosition() const { return m_sashPosition
; }
156 // Set the sash gravity
157 void SetSashGravity(double gravity
);
159 // Gets the sash gravity
160 double GetSashGravity() const { return m_sashGravity
; }
162 // If this is zero, we can remove panes by dragging the sash.
163 void SetMinimumPaneSize(int min
);
164 int GetMinimumPaneSize() const { return m_minimumPaneSize
; }
166 // NB: the OnXXX() functions below are for backwards compatibility only,
167 // don't use them in new code but handle the events instead!
169 // called when the sash position is about to change, may return a new value
170 // for the sash or -1 to prevent the change from happening at all
171 virtual int OnSashPositionChanging(int newSashPosition
);
173 // Called when the sash position is about to be changed, return
174 // false from here to prevent the change from taking place.
175 // Repositions sash to minimum position if pane would be too small.
176 // newSashPosition here is always positive or zero.
177 virtual bool OnSashPositionChange(int newSashPosition
);
179 // If the sash is moved to an extreme position, a subwindow
180 // is removed from the splitter window, and the app is
181 // notified. The app should delete or hide the window.
182 virtual void OnUnsplit(wxWindow
*removed
);
184 // Called when the sash is double-clicked.
185 // The default behaviour is to remove the sash if the
186 // minimum pane size is zero.
187 virtual void OnDoubleClickSash(int x
, int y
);
189 ////////////////////////////////////////////////////////////////////////////
192 // Paints the border and sash
193 void OnPaint(wxPaintEvent
& event
);
195 // Handles mouse events
196 void OnMouseEvent(wxMouseEvent
& ev
);
198 // Aborts dragging mode
199 void OnMouseCaptureLost(wxMouseCaptureLostEvent
& event
);
202 void OnSize(wxSizeEvent
& event
);
204 // In live mode, resize child windows in idle time
205 void OnInternalIdle();
208 virtual void DrawSash(wxDC
& dc
);
210 // Draws the sash tracker (for whilst moving the sash)
211 virtual void DrawSashTracker(int x
, int y
);
213 // Tests for x, y over sash
214 virtual bool SashHitTest(int x
, int y
);
216 // Resizes subwindows
217 virtual void SizeWindows();
220 virtual bool MacClipGrandChildren() const { return true ; }
223 // Sets the sash size: this doesn't do anything and shouldn't be used at
225 wxDEPRECATED_INLINE( void SetSashSize(int WXUNUSED(width
)), return; )
229 #if defined(__WXMSW__) || defined(__WXMAC__)
230 void OnSetCursor(wxSetCursorEvent
& event
);
233 // send the given event, return false if the event was processed and vetoed
235 bool DoSendEvent(wxSplitterEvent
& event
);
237 // common part of all ctors
240 // common part of SplitVertically() and SplitHorizontally()
241 bool DoSplit(wxSplitMode mode
,
242 wxWindow
*window1
, wxWindow
*window2
,
245 // adjusts sash position with respect to min. pane and window sizes
246 int AdjustSashPosition(int sashPos
) const;
248 // get either width or height depending on the split mode
249 int GetWindowSize() const;
251 // convert the user specified sash position which may be > 0 (as is), < 0
252 // (specifying the size of the right pane) or 0 (use default) to the real
253 // position to be passed to DoSetSashPosition()
254 int ConvertSashPosition(int sashPos
) const;
256 // set the real sash position, sashPos here must be positive
258 // returns true if the sash position has been changed, false otherwise
259 bool DoSetSashPosition(int sashPos
);
261 // set the sash position and send an event about it having been changed
262 void SetSashPositionAndNotify(int sashPos
);
264 // callbacks executed when we detect that the mouse has entered or left
266 virtual void OnEnterSash();
267 virtual void OnLeaveSash();
269 // set the cursor appropriate for the current split mode
270 void SetResizeCursor();
272 // redraw the splitter if its "hotness" changed if necessary
273 void RedrawIfHotSensitive(bool isHot
);
275 // return the best size of the splitter equal to best sizes of its
277 virtual wxSize
DoGetBestSize() const;
280 wxSplitMode m_splitMode
;
281 wxWindow
* m_windowOne
;
282 wxWindow
* m_windowTwo
;
284 int m_oldX
; // current tracker position if not live mode
285 int m_oldY
; // current tracker position if not live mode
286 int m_sashPosition
; // Number of pixels from left or top
287 double m_sashGravity
;
289 int m_requestedSashPosition
;
290 int m_sashPositionCurrent
; // while dragging
291 wxPoint m_ptStart
; // mouse position when dragging started
292 int m_sashStart
; // sash position when dragging started
293 int m_minimumPaneSize
;
294 wxCursor m_sashCursorWE
;
295 wxCursor m_sashCursorNS
;
296 wxPen
*m_sashTrackerPen
;
298 // when in live mode, set this to true to resize children in idle
299 bool m_needUpdating
:1;
300 bool m_permitUnsplitAlways
:1;
304 DECLARE_DYNAMIC_CLASS(wxSplitterWindow
)
305 DECLARE_EVENT_TABLE()
306 wxDECLARE_NO_COPY_CLASS(wxSplitterWindow
);
309 // ----------------------------------------------------------------------------
310 // event class and macros
311 // ----------------------------------------------------------------------------
313 // we reuse the same class for all splitter event types because this is the
314 // usual wxWin convention, but the three event types have different kind of
315 // data associated with them, so the accessors can be only used if the real
316 // event type matches with the one for which the accessors make sense
317 class WXDLLIMPEXP_CORE wxSplitterEvent
: public wxNotifyEvent
320 wxSplitterEvent(wxEventType type
= wxEVT_NULL
,
321 wxSplitterWindow
*splitter
= NULL
)
322 : wxNotifyEvent(type
)
324 SetEventObject(splitter
);
325 if (splitter
) m_id
= splitter
->GetId();
327 wxSplitterEvent(const wxSplitterEvent
& event
)
328 : wxNotifyEvent(event
), m_data(event
.m_data
) { }
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_SPLITTER_SASH_POS_CHANGED
337 || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING
);
342 int GetSashPosition() const
344 wxASSERT( GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGED
345 || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING
);
350 // UNSPLIT event methods
351 wxWindow
*GetWindowBeingRemoved() const
353 wxASSERT( GetEventType() == wxEVT_SPLITTER_UNSPLIT
);
358 // DCLICK event methods
361 wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED
);
368 wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED
);
373 virtual wxEvent
*Clone() const { return new wxSplitterEvent(*this); }
376 friend class WXDLLIMPEXP_FWD_CORE wxSplitterWindow
;
378 // data for the different types of event
381 int pos
; // position for SASH_POS_CHANGED event
382 wxWindow
*win
; // window being removed for UNSPLIT event
386 } pt
; // position of double click for DCLICK event
389 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSplitterEvent
)
392 typedef void (wxEvtHandler::*wxSplitterEventFunction
)(wxSplitterEvent
&);
394 #define wxSplitterEventHandler(func) \
395 wxEVENT_HANDLER_CAST(wxSplitterEventFunction, func)
397 #define wx__DECLARE_SPLITTEREVT(evt, id, fn) \
398 wx__DECLARE_EVT1(wxEVT_SPLITTER_ ## evt, id, wxSplitterEventHandler(fn))
400 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
401 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGED, id, fn)
403 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
404 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGING, id, fn)
406 #define EVT_SPLITTER_DCLICK(id, fn) \
407 wx__DECLARE_SPLITTEREVT(DOUBLECLICKED, id, fn)
409 #define EVT_SPLITTER_UNSPLIT(id, fn) \
410 wx__DECLARE_SPLITTEREVT(UNSPLIT, id, fn)
413 // old wxEVT_COMMAND_* constants
414 #define wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED wxEVT_SPLITTER_SASH_POS_CHANGED
415 #define wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING wxEVT_SPLITTER_SASH_POS_CHANGING
416 #define wxEVT_COMMAND_SPLITTER_DOUBLECLICKED wxEVT_SPLITTER_DOUBLECLICKED
417 #define wxEVT_COMMAND_SPLITTER_UNSPLIT wxEVT_SPLITTER_UNSPLIT
419 #endif // _WX_GENERIC_SPLITTER_H_