]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/splitter.h
(blind) fix for Metrowerks/Win32 compilation
[wxWidgets.git] / include / wx / generic / splitter.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/splitter.h
3 // Purpose: wxSplitterWindow class
4 // Author: Julian Smart
5 // Modified by:
6 // Created: 01/02/97
7 // RCS-ID: $Id$
8 // Copyright: (c) Julian Smart
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __SPLITTERH_G__
13 #define __SPLITTERH_G__
14
15 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
16 #pragma interface "splitter.h"
17 #endif
18
19 #include "wx/window.h" // base class declaration
20 #include "wx/containr.h" // wxControlContainer
21
22 class WXDLLEXPORT wxSplitterEvent;
23
24 // ---------------------------------------------------------------------------
25 // splitter constants
26 // ---------------------------------------------------------------------------
27
28 enum wxSplitMode
29 {
30 wxSPLIT_HORIZONTAL = 1,
31 wxSPLIT_VERTICAL
32 };
33
34 enum
35 {
36 wxSPLIT_DRAG_NONE,
37 wxSPLIT_DRAG_DRAGGING,
38 wxSPLIT_DRAG_LEFT_DOWN
39 };
40
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 // ---------------------------------------------------------------------------
46
47 // TODO:
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
51 // this path.
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
54 // standard).
55
56 class WXDLLEXPORT wxSplitterWindow: public wxWindow
57 {
58 public:
59
60 ////////////////////////////////////////////////////////////////////////////
61 // Public API
62
63 // Default constructor
64 wxSplitterWindow()
65 {
66 Init();
67 }
68
69 // Normal constructor
70 wxSplitterWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
71 const wxPoint& pos = wxDefaultPosition,
72 const wxSize& size = wxDefaultSize,
73 long style = wxSP_3D,
74 const wxString& name = wxT("splitter"))
75 {
76 Init();
77 Create(parent, id, pos, size, style, name);
78 }
79
80 virtual ~wxSplitterWindow();
81
82 bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
83 const wxPoint& pos = wxDefaultPosition,
84 const wxSize& size = wxDefaultSize,
85 long style = wxSP_3D,
86 const wxString& name = wxT("splitter"));
87
88 // Gets the only or left/top pane
89 wxWindow *GetWindow1() const { return m_windowOne; }
90
91 // Gets the right/bottom pane
92 wxWindow *GetWindow2() const { return m_windowTwo; }
93
94 // Sets the split mode
95 void SetSplitMode(int mode)
96 {
97 wxASSERT_MSG( mode == wxSPLIT_VERTICAL || mode == wxSPLIT_HORIZONTAL,
98 _T("invalid split mode") );
99
100 m_splitMode = (wxSplitMode)mode;
101 }
102
103 // Gets the split mode
104 wxSplitMode GetSplitMode() const { return m_splitMode; };
105
106 // Initialize with one window
107 void Initialize(wxWindow *window);
108
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,
116 wxWindow *window2,
117 int sashPosition = 0)
118 { return DoSplit(wxSPLIT_VERTICAL, window1, window2, sashPosition); }
119 virtual bool SplitHorizontally(wxWindow *window1,
120 wxWindow *window2,
121 int sashPosition = 0)
122 { return DoSplit(wxSPLIT_HORIZONTAL, window1, window2, sashPosition); }
123
124 // Removes the specified (or second) window from the view
125 // Doesn't actually delete the window.
126 bool Unsplit(wxWindow *toRemove = (wxWindow *) NULL);
127
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);
131
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.
135 void UpdateSize();
136
137 // Is the window split?
138 bool IsSplit() const { return (m_windowTwo != NULL); }
139
140 // Sets the sash size
141 void SetSashSize(int WXUNUSED(width)) { }
142
143 // Sets the border size
144 void SetBorderSize(int WXUNUSED(width)) { }
145
146 // Gets the sash size
147 int GetSashSize() const;
148
149 // Gets the border size
150 int GetBorderSize() const;
151
152 // Set the sash position
153 void SetSashPosition(int position, bool redraw = true);
154
155 // Gets the sash position
156 int GetSashPosition() const { return m_sashPosition; }
157
158 // If this is zero, we can remove panes by dragging the sash.
159 void SetMinimumPaneSize(int min);
160 int GetMinimumPaneSize() const { return m_minimumPaneSize; }
161
162 // NB: the OnXXX() functions below are for backwards compatibility only,
163 // don't use them in new code but handle the events instead!
164
165 // called when the sash position is about to change, may return a new value
166 // for the sash or -1 to prevent the change from happening at all
167 virtual int OnSashPositionChanging(int newSashPosition);
168
169 // Called when the sash position is about to be changed, return
170 // false from here to prevent the change from taking place.
171 // Repositions sash to minimum position if pane would be too small.
172 // newSashPosition here is always positive or zero.
173 virtual bool OnSashPositionChange(int newSashPosition);
174
175 // If the sash is moved to an extreme position, a subwindow
176 // is removed from the splitter window, and the app is
177 // notified. The app should delete or hide the window.
178 virtual void OnUnsplit(wxWindow *removed);
179
180 // Called when the sash is double-clicked.
181 // The default behaviour is to remove the sash if the
182 // minimum pane size is zero.
183 virtual void OnDoubleClickSash(int x, int y);
184
185 ////////////////////////////////////////////////////////////////////////////
186 // Implementation
187
188 // Paints the border and sash
189 void OnPaint(wxPaintEvent& event);
190
191 // Handles mouse events
192 void OnMouseEvent(wxMouseEvent& ev);
193
194 // Adjusts the panes
195 void OnSize(wxSizeEvent& event);
196
197 // In live mode, resize child windows in idle time
198 void OnInternalIdle();
199
200 // Draws the sash
201 virtual void DrawSash(wxDC& dc);
202
203 // Draws the sash tracker (for whilst moving the sash)
204 virtual void DrawSashTracker(int x, int y);
205
206 // Tests for x, y over sash
207 virtual bool SashHitTest(int x, int y, int tolerance = 5);
208
209 // Resizes subwindows
210 virtual void SizeWindows();
211
212 void SetNeedUpdating(bool needUpdating) { m_needUpdating = needUpdating; }
213 bool GetNeedUpdating() const { return m_needUpdating ; }
214
215 #ifdef __WXMAC__
216 virtual bool MacClipGrandChildren() const { return true ; }
217 #endif
218 protected:
219 // event handlers
220 #if defined(__WXMSW__) || defined(__WXMAC__)
221 void OnSetCursor(wxSetCursorEvent& event);
222 #endif // wxMSW
223
224 // send the given event, return false if the event was processed and vetoed
225 // by the user code
226 inline bool DoSendEvent(wxSplitterEvent& event);
227
228 protected:
229 // common part of all ctors
230 void Init();
231
232 // common part of SplitVertically() and SplitHorizontally()
233 bool DoSplit(wxSplitMode mode,
234 wxWindow *window1, wxWindow *window2,
235 int sashPosition);
236
237 // adjusts sash position with respect to min. pane and window sizes
238 int AdjustSashPosition(int sashPos) const;
239
240 // get either width or height depending on the split mode
241 int GetWindowSize() const;
242
243 // convert the user specified sash position which may be > 0 (as is), < 0
244 // (specifying the size of the right pane) or 0 (use default) to the real
245 // position to be passed to DoSetSashPosition()
246 int ConvertSashPosition(int sashPos) const;
247
248 // set the real sash position, sashPos here must be positive
249 //
250 // returns true if the sash position has been changed, false otherwise
251 bool DoSetSashPosition(int sashPos);
252
253 // set the sash position and send an event about it having been changed
254 void SetSashPositionAndNotify(int sashPos);
255
256 // callbacks executed when we detect that the mouse has entered or left
257 // the sash
258 virtual void OnEnterSash();
259 virtual void OnLeaveSash();
260
261 // set the cursor appropriate for the current split mode
262 void SetResizeCursor();
263
264 // redraw the splitter if its "hotness" changed if necessary
265 void RedrawIfHotSensitive(bool isHot);
266
267 wxSplitMode m_splitMode;
268 wxWindow* m_windowOne;
269 wxWindow* m_windowTwo;
270 int m_dragMode;
271 int m_oldX;
272 int m_oldY;
273 int m_sashPosition; // Number of pixels from left or top
274 int m_requestedSashPosition;
275 int m_sashPositionCurrent; // while dragging
276 int m_firstX;
277 int m_firstY;
278 int m_minimumPaneSize;
279 wxCursor m_sashCursorWE;
280 wxCursor m_sashCursorNS;
281 wxPen *m_sashTrackerPen;
282
283 // when in live mode, set this to true to resize children in idle
284 bool m_needUpdating:1;
285 bool m_permitUnsplitAlways:1;
286 bool m_isHot:1;
287 bool m_checkRequestedSashPosition:1;
288
289 private:
290 WX_DECLARE_CONTROL_CONTAINER();
291
292 DECLARE_DYNAMIC_CLASS(wxSplitterWindow)
293 DECLARE_EVENT_TABLE()
294 DECLARE_NO_COPY_CLASS(wxSplitterWindow)
295 };
296
297 // ----------------------------------------------------------------------------
298 // event class and macros
299 // ----------------------------------------------------------------------------
300
301 // we reuse the same class for all splitter event types because this is the
302 // usual wxWin convention, but the three event types have different kind of
303 // data associated with them, so the accessors can be only used if the real
304 // event type matches with the one for which the accessors make sense
305 class WXDLLEXPORT wxSplitterEvent : public wxNotifyEvent
306 {
307 public:
308 wxSplitterEvent(wxEventType type = wxEVT_NULL,
309 wxSplitterWindow *splitter = (wxSplitterWindow *)NULL)
310 : wxNotifyEvent(type)
311 {
312 SetEventObject(splitter);
313 if (splitter) m_id = splitter->GetId();
314 }
315
316 // SASH_POS_CHANGED methods
317
318 // setting the sash position to -1 prevents the change from taking place at
319 // all
320 void SetSashPosition(int pos)
321 {
322 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
323 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
324
325 m_data.pos = pos;
326 }
327
328 int GetSashPosition() const
329 {
330 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
331 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
332
333 return m_data.pos;
334 }
335
336 // UNSPLIT event methods
337 wxWindow *GetWindowBeingRemoved() const
338 {
339 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_UNSPLIT );
340
341 return m_data.win;
342 }
343
344 // DCLICK event methods
345 int GetX() const
346 {
347 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
348
349 return m_data.pt.x;
350 }
351
352 int GetY() const
353 {
354 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
355
356 return m_data.pt.y;
357 }
358
359 private:
360 friend class WXDLLEXPORT wxSplitterWindow;
361
362 // data for the different types of event
363 union
364 {
365 int pos; // position for SASH_POS_CHANGED event
366 wxWindow *win; // window being removed for UNSPLIT event
367 struct
368 {
369 int x, y;
370 } pt; // position of double click for DCLICK event
371 } m_data;
372
373 DECLARE_DYNAMIC_CLASS_NO_COPY(wxSplitterEvent)
374 };
375
376 typedef void (wxEvtHandler::*wxSplitterEventFunction)(wxSplitterEvent&);
377
378 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
379 DECLARE_EVENT_TABLE_ENTRY( \
380 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, \
381 id, \
382 wxID_ANY, \
383 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxSplitterEventFunction, &fn ), \
384 NULL \
385 ),
386
387 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
388 DECLARE_EVENT_TABLE_ENTRY( \
389 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, \
390 id, \
391 wxID_ANY, \
392 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxSplitterEventFunction, &fn ), \
393 NULL \
394 ),
395
396 #define EVT_SPLITTER_DCLICK(id, fn) \
397 DECLARE_EVENT_TABLE_ENTRY( \
398 wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, \
399 id, \
400 wxID_ANY, \
401 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxSplitterEventFunction, &fn ), \
402 NULL \
403 ),
404
405 #define EVT_SPLITTER_UNSPLIT(id, fn) \
406 DECLARE_EVENT_TABLE_ENTRY( \
407 wxEVT_COMMAND_SPLITTER_UNSPLIT, \
408 id, \
409 wxID_ANY, \
410 (wxObjectEventFunction)(wxEventFunction) wxStaticCastEvent( wxSplitterEventFunction, &fn ), \
411 NULL \
412 ),
413
414 #endif // __SPLITTERH_G__