]> git.saurik.com Git - wxWidgets.git/blob - include/wx/generic/splitter.h
4b0d641a56e4c66119d35e689cf3129e4b22732a
[wxWidgets.git] / include / wx / generic / splitter.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 and Markus Holzem
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 #ifndef __SPLITTERH_G__
13 #define __SPLITTERH_G__
14
15 #ifdef __GNUG__
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
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 wxWindows 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 = -1,
71 const wxPoint& pos = wxDefaultPosition,
72 const wxSize& size = wxDefaultSize,
73 long style = wxSP_3D,
74 const wxString& name = "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 = -1,
83 const wxPoint& pos = wxDefaultPosition,
84 const wxSize& size = wxDefaultSize,
85 long style = wxSP_3D,
86 const wxString& name = "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) { m_splitMode = mode; }
96
97 // Gets the split mode
98 int GetSplitMode() const { return m_splitMode; };
99
100 // Initialize with one window
101 void Initialize(wxWindow *window);
102
103 // Associates the given window with window 2, drawing the appropriate sash
104 // and changing the split mode.
105 // Does nothing and returns FALSE if the window is already split.
106 // A sashPosition of 0 means choose a default sash position,
107 // negative sashPosition specifies the size of right/lower pane as it's
108 // absolute value rather than the size of left/upper pane.
109 virtual bool SplitVertically(wxWindow *window1,
110 wxWindow *window2,
111 int sashPosition = 0);
112 virtual bool SplitHorizontally(wxWindow *window1,
113 wxWindow *window2,
114 int sashPosition = 0);
115
116 // Removes the specified (or second) window from the view
117 // Doesn't actually delete the window.
118 bool Unsplit(wxWindow *toRemove = (wxWindow *) NULL);
119
120 // Replaces one of the windows with another one (neither old nor new
121 // parameter should be NULL)
122 bool ReplaceWindow(wxWindow *winOld, wxWindow *winNew);
123
124 // Is the window split?
125 bool IsSplit() const { return (m_windowTwo != NULL); }
126
127 // Sets the sash size
128 void SetSashSize(int width) { m_sashSize = width; }
129
130 // Sets the border size
131 void SetBorderSize(int width) { m_borderSize = width; }
132
133 // Gets the sash size
134 int GetSashSize() const { return m_sashSize; }
135
136 // Gets the border size
137 int GetBorderSize() const { return m_borderSize; }
138
139 // Set the sash position
140 void SetSashPosition(int position, bool redraw = TRUE);
141
142 // Gets the sash position
143 int GetSashPosition() const { return m_sashPosition; }
144
145 // If this is zero, we can remove panes by dragging the sash.
146 void SetMinimumPaneSize(int min) { m_minimumPaneSize = min; }
147 int GetMinimumPaneSize() const { return m_minimumPaneSize; }
148
149 // Called when the sash position is about to be changed, return
150 // FALSE from here to prevent the change from taking place.
151 // Repositions sash to minimum position if pane would be too small.
152 // newSashPosition here is always positive or zero.
153 virtual bool OnSashPositionChange(int WXUNUSED(newSashPosition))
154 { return TRUE; }
155
156 // If the sash is moved to an extreme position, a subwindow
157 // is removed from the splitter window, and the app is
158 // notified. The app should delete or hide the window.
159 virtual void OnUnsplit(wxWindow *WXUNUSED(removed)) { }
160
161 // Called when the sash is double-clicked.
162 // The default behaviour is to remove the sash if the
163 // minimum pane size is zero.
164 virtual void OnDoubleClickSash(int WXUNUSED(x), int WXUNUSED(y)) { }
165
166 ////////////////////////////////////////////////////////////////////////////
167 // Implementation
168
169 // Paints the border and sash
170 void OnPaint(wxPaintEvent& event);
171
172 // Handles mouse events
173 void OnMouseEvent(wxMouseEvent& ev);
174
175 // Adjusts the panes
176 void OnSize(wxSizeEvent& event);
177
178 // In live mode, resize child windows in idle time
179 void OnIdle(wxIdleEvent& event);
180
181 // Draws borders
182 virtual void DrawBorders(wxDC& dc);
183
184 // Draws the sash
185 virtual void DrawSash(wxDC& dc);
186
187 // Draws the sash tracker (for whilst moving the sash)
188 virtual void DrawSashTracker(int x, int y);
189
190 // Tests for x, y over sash
191 virtual bool SashHitTest(int x, int y, int tolerance = 2);
192
193 // Resizes subwindows
194 virtual void SizeWindows();
195
196 // Initialize colours
197 void InitColours();
198
199 void SetNeedUpdating(bool needUpdating) { m_needUpdating = needUpdating; }
200 bool GetNeedUpdating() const { return m_needUpdating ; }
201
202 protected:
203 // our event handlers
204 void OnSashPosChanged(wxSplitterEvent& event);
205 void OnSashPosChanging(wxSplitterEvent& event);
206 void OnDoubleClick(wxSplitterEvent& event);
207 void OnUnsplitEvent(wxSplitterEvent& event);
208 void OnSetCursor(wxSetCursorEvent& event);
209
210 void SendUnsplitEvent(wxWindow *winRemoved);
211
212 protected:
213 // common part of all ctors
214 void Init();
215
216 // adjusts sash position with respect to min. pane and window sizes
217 void AdjustSashPosition(int &sashPos);
218
219 int m_splitMode;
220 bool m_permitUnsplitAlways;
221 bool m_needUpdating; // when in live mode, set this to TRUE to resize children in idle
222 wxWindow* m_windowOne;
223 wxWindow* m_windowTwo;
224 int m_dragMode;
225 int m_oldX;
226 int m_oldY;
227 int m_borderSize;
228 int m_sashSize; // Sash width or height
229 int m_sashPosition; // Number of pixels from left or top
230 int m_firstX;
231 int m_firstY;
232 int m_minimumPaneSize;
233 wxCursor* m_sashCursorWE;
234 wxCursor* m_sashCursorNS;
235 wxPen* m_sashTrackerPen;
236 wxPen* m_lightShadowPen;
237 wxPen* m_mediumShadowPen;
238 wxPen* m_darkShadowPen;
239 wxPen* m_hilightPen;
240 wxBrush* m_faceBrush;
241 wxPen* m_facePen;
242
243 private:
244 WX_DECLARE_CONTROL_CONTAINER();
245
246 DECLARE_DYNAMIC_CLASS(wxSplitterWindow)
247 DECLARE_EVENT_TABLE()
248 };
249
250 // ----------------------------------------------------------------------------
251 // event class and macros
252 // ----------------------------------------------------------------------------
253
254 // we reuse the same class for all splitter event types because this is the
255 // usual wxWin convention, but the three event types have different kind of
256 // data associated with them, so the accessors can be only used if the real
257 // event type matches with the one for which the accessors make sense
258 class WXDLLEXPORT wxSplitterEvent : public wxCommandEvent
259 {
260 public:
261 wxSplitterEvent(wxEventType type = wxEVT_NULL,
262 wxSplitterWindow *splitter = (wxSplitterWindow *)NULL)
263 : wxCommandEvent(type)
264 {
265 SetEventObject(splitter);
266 if (splitter) m_id = splitter->GetId();
267 }
268
269 // SASH_POS_CHANGED methods
270
271 // setting the sash position to -1 prevents the change from taking place at
272 // all
273 void SetSashPosition(int pos)
274 {
275 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
276 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
277
278 m_data.pos = pos;
279 }
280
281 int GetSashPosition() const
282 {
283 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
284 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
285
286 return m_data.pos;
287 }
288
289 // UNSPLIT event methods
290 wxWindow *GetWindowBeingRemoved() const
291 {
292 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_UNSPLIT );
293
294 return m_data.win;
295 }
296
297 // DCLICK event methods
298 int GetX() const
299 {
300 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
301
302 return m_data.pt.x;
303 }
304
305 int GetY() const
306 {
307 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
308
309 return m_data.pt.y;
310 }
311
312 private:
313 friend class WXDLLEXPORT wxSplitterWindow;
314
315 // data for the different types of event
316 union
317 {
318 int pos; // position for SASH_POS_CHANGED event
319 wxWindow *win; // window being removed for UNSPLIT event
320 struct
321 {
322 int x, y;
323 } pt; // position of double click for DCLICK event
324 } m_data;
325
326 DECLARE_DYNAMIC_CLASS(wxSplitterEvent)
327 };
328
329 typedef void (wxEvtHandler::*wxSplitterEventFunction)(wxSplitterEvent&);
330
331 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
332 DECLARE_EVENT_TABLE_ENTRY( \
333 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, \
334 id, \
335 -1, \
336 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
337 NULL \
338 ),
339
340 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
341 DECLARE_EVENT_TABLE_ENTRY( \
342 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, \
343 id, \
344 -1, \
345 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
346 NULL \
347 ),
348
349 #define EVT_SPLITTER_DCLICK(id, fn) \
350 DECLARE_EVENT_TABLE_ENTRY( \
351 wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, \
352 id, \
353 -1, \
354 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
355 NULL \
356 ),
357
358 #define EVT_SPLITTER_UNSPLIT(id, fn) \
359 DECLARE_EVENT_TABLE_ENTRY( \
360 wxEVT_COMMAND_SPLITTER_UNSPLIT, \
361 id, \
362 -1, \
363 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
364 NULL \
365 ),
366
367 #endif // __SPLITTERH_G__