Remove all lines containing cvs/svn "$Id$" keyword.
[wxWidgets.git] / include / wx / generic / splitter.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/generic/splitter.h
3 // Purpose: wxSplitterWindow class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // Copyright: (c) Julian Smart
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 #ifndef _WX_GENERIC_SPLITTER_H_
12 #define _WX_GENERIC_SPLITTER_H_
13
14 #include "wx/window.h" // base class declaration
15 #include "wx/containr.h" // wxControlContainer
16
17 class WXDLLIMPEXP_FWD_CORE wxSplitterEvent;
18
19 // ---------------------------------------------------------------------------
20 // splitter constants
21 // ---------------------------------------------------------------------------
22
23 enum wxSplitMode
24 {
25 wxSPLIT_HORIZONTAL = 1,
26 wxSPLIT_VERTICAL
27 };
28
29 enum
30 {
31 wxSPLIT_DRAG_NONE,
32 wxSPLIT_DRAG_DRAGGING,
33 wxSPLIT_DRAG_LEFT_DOWN
34 };
35
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 // ---------------------------------------------------------------------------
41
42 // TODO:
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
46 // this path.
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
49 // standard).
50
51 class WXDLLIMPEXP_CORE wxSplitterWindow: public wxNavigationEnabled<wxWindow>
52 {
53 public:
54
55 ////////////////////////////////////////////////////////////////////////////
56 // Public API
57
58 // Default constructor
59 wxSplitterWindow()
60 {
61 Init();
62 }
63
64 // Normal constructor
65 wxSplitterWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
66 const wxPoint& pos = wxDefaultPosition,
67 const wxSize& size = wxDefaultSize,
68 long style = wxSP_3D,
69 const wxString& name = wxT("splitter"))
70 {
71 Init();
72 Create(parent, id, pos, size, style, name);
73 }
74
75 virtual ~wxSplitterWindow();
76
77 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
78 const wxPoint& pos = wxDefaultPosition,
79 const wxSize& size = wxDefaultSize,
80 long style = wxSP_3D,
81 const wxString& name = wxT("splitter"));
82
83 // Gets the only or left/top pane
84 wxWindow *GetWindow1() const { return m_windowOne; }
85
86 // Gets the right/bottom pane
87 wxWindow *GetWindow2() const { return m_windowTwo; }
88
89 // Sets the split mode
90 void SetSplitMode(int mode)
91 {
92 wxASSERT_MSG( mode == wxSPLIT_VERTICAL || mode == wxSPLIT_HORIZONTAL,
93 wxT("invalid split mode") );
94
95 m_splitMode = (wxSplitMode)mode;
96 }
97
98 // Gets the split mode
99 wxSplitMode GetSplitMode() const { return m_splitMode; }
100
101 // Initialize with one window
102 void Initialize(wxWindow *window);
103
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,
111 wxWindow *window2,
112 int sashPosition = 0)
113 { return DoSplit(wxSPLIT_VERTICAL, window1, window2, sashPosition); }
114 virtual bool SplitHorizontally(wxWindow *window1,
115 wxWindow *window2,
116 int sashPosition = 0)
117 { return DoSplit(wxSPLIT_HORIZONTAL, window1, window2, sashPosition); }
118
119 // Removes the specified (or second) window from the view
120 // Doesn't actually delete the window.
121 bool Unsplit(wxWindow *toRemove = NULL);
122
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);
126
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.
130 void UpdateSize();
131
132 // Is the window split?
133 bool IsSplit() const { return (m_windowTwo != NULL); }
134
135 // Sets the border size
136 void SetBorderSize(int WXUNUSED(width)) { }
137
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); }
141
142 // Gets the current sash size which may be 0 if it's hidden and the default
143 // sash size.
144 int GetSashSize() const;
145 int GetDefaultSashSize() const;
146
147 // Gets the border size
148 int GetBorderSize() const;
149
150 // Set the sash position
151 void SetSashPosition(int position, bool redraw = true);
152
153 // Gets the sash position
154 int GetSashPosition() const { return m_sashPosition; }
155
156 // Set the sash gravity
157 void SetSashGravity(double gravity);
158
159 // Gets the sash gravity
160 double GetSashGravity() const { return m_sashGravity; }
161
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; }
165
166 // NB: the OnXXX() functions below are for backwards compatibility only,
167 // don't use them in new code but handle the events instead!
168
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);
172
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);
178
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);
183
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);
188
189 ////////////////////////////////////////////////////////////////////////////
190 // Implementation
191
192 // Paints the border and sash
193 void OnPaint(wxPaintEvent& event);
194
195 // Handles mouse events
196 void OnMouseEvent(wxMouseEvent& ev);
197
198 // Aborts dragging mode
199 void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
200
201 // Adjusts the panes
202 void OnSize(wxSizeEvent& event);
203
204 // In live mode, resize child windows in idle time
205 void OnInternalIdle();
206
207 // Draws the sash
208 virtual void DrawSash(wxDC& dc);
209
210 // Draws the sash tracker (for whilst moving the sash)
211 virtual void DrawSashTracker(int x, int y);
212
213 // Tests for x, y over sash
214 virtual bool SashHitTest(int x, int y);
215
216 // Resizes subwindows
217 virtual void SizeWindows();
218
219 #ifdef __WXMAC__
220 virtual bool MacClipGrandChildren() const { return true ; }
221 #endif
222
223 // Sets the sash size: this doesn't do anything and shouldn't be used at
224 // all any more.
225 wxDEPRECATED_INLINE( void SetSashSize(int WXUNUSED(width)), return; )
226
227 protected:
228 // event handlers
229 #if defined(__WXMSW__) || defined(__WXMAC__)
230 void OnSetCursor(wxSetCursorEvent& event);
231 #endif // wxMSW
232
233 // send the given event, return false if the event was processed and vetoed
234 // by the user code
235 bool DoSendEvent(wxSplitterEvent& event);
236
237 // common part of all ctors
238 void Init();
239
240 // common part of SplitVertically() and SplitHorizontally()
241 bool DoSplit(wxSplitMode mode,
242 wxWindow *window1, wxWindow *window2,
243 int sashPosition);
244
245 // adjusts sash position with respect to min. pane and window sizes
246 int AdjustSashPosition(int sashPos) const;
247
248 // get either width or height depending on the split mode
249 int GetWindowSize() const;
250
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;
255
256 // set the real sash position, sashPos here must be positive
257 //
258 // returns true if the sash position has been changed, false otherwise
259 bool DoSetSashPosition(int sashPos);
260
261 // set the sash position and send an event about it having been changed
262 void SetSashPositionAndNotify(int sashPos);
263
264 // callbacks executed when we detect that the mouse has entered or left
265 // the sash
266 virtual void OnEnterSash();
267 virtual void OnLeaveSash();
268
269 // set the cursor appropriate for the current split mode
270 void SetResizeCursor();
271
272 // redraw the splitter if its "hotness" changed if necessary
273 void RedrawIfHotSensitive(bool isHot);
274
275 // return the best size of the splitter equal to best sizes of its
276 // subwindows
277 virtual wxSize DoGetBestSize() const;
278
279
280 wxSplitMode m_splitMode;
281 wxWindow* m_windowOne;
282 wxWindow* m_windowTwo;
283 int m_dragMode;
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;
288 wxSize m_lastSize;
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;
297
298 // when in live mode, set this to true to resize children in idle
299 bool m_needUpdating:1;
300 bool m_permitUnsplitAlways:1;
301 bool m_isHot:1;
302
303 private:
304 DECLARE_DYNAMIC_CLASS(wxSplitterWindow)
305 DECLARE_EVENT_TABLE()
306 wxDECLARE_NO_COPY_CLASS(wxSplitterWindow);
307 };
308
309 // ----------------------------------------------------------------------------
310 // event class and macros
311 // ----------------------------------------------------------------------------
312
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
318 {
319 public:
320 wxSplitterEvent(wxEventType type = wxEVT_NULL,
321 wxSplitterWindow *splitter = NULL)
322 : wxNotifyEvent(type)
323 {
324 SetEventObject(splitter);
325 if (splitter) m_id = splitter->GetId();
326 }
327 wxSplitterEvent(const wxSplitterEvent& event)
328 : wxNotifyEvent(event), m_data(event.m_data) { }
329
330 // SASH_POS_CHANGED methods
331
332 // setting the sash position to -1 prevents the change from taking place at
333 // all
334 void SetSashPosition(int pos)
335 {
336 wxASSERT( GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGED
337 || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING);
338
339 m_data.pos = pos;
340 }
341
342 int GetSashPosition() const
343 {
344 wxASSERT( GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGED
345 || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING);
346
347 return m_data.pos;
348 }
349
350 // UNSPLIT event methods
351 wxWindow *GetWindowBeingRemoved() const
352 {
353 wxASSERT( GetEventType() == wxEVT_SPLITTER_UNSPLIT );
354
355 return m_data.win;
356 }
357
358 // DCLICK event methods
359 int GetX() const
360 {
361 wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED );
362
363 return m_data.pt.x;
364 }
365
366 int GetY() const
367 {
368 wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED );
369
370 return m_data.pt.y;
371 }
372
373 virtual wxEvent *Clone() const { return new wxSplitterEvent(*this); }
374
375 private:
376 friend class WXDLLIMPEXP_FWD_CORE wxSplitterWindow;
377
378 // data for the different types of event
379 union
380 {
381 int pos; // position for SASH_POS_CHANGED event
382 wxWindow *win; // window being removed for UNSPLIT event
383 struct
384 {
385 int x, y;
386 } pt; // position of double click for DCLICK event
387 } m_data;
388
389 DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSplitterEvent)
390 };
391
392 typedef void (wxEvtHandler::*wxSplitterEventFunction)(wxSplitterEvent&);
393
394 #define wxSplitterEventHandler(func) \
395 wxEVENT_HANDLER_CAST(wxSplitterEventFunction, func)
396
397 #define wx__DECLARE_SPLITTEREVT(evt, id, fn) \
398 wx__DECLARE_EVT1(wxEVT_SPLITTER_ ## evt, id, wxSplitterEventHandler(fn))
399
400 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
401 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGED, id, fn)
402
403 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
404 wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGING, id, fn)
405
406 #define EVT_SPLITTER_DCLICK(id, fn) \
407 wx__DECLARE_SPLITTEREVT(DOUBLECLICKED, id, fn)
408
409 #define EVT_SPLITTER_UNSPLIT(id, fn) \
410 wx__DECLARE_SPLITTEREVT(UNSPLIT, id, fn)
411
412
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
418
419 #endif // _WX_GENERIC_SPLITTER_H_