1. status bar can now be positioned on top (and anywhere, in fact)
[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
21 class WXDLLEXPORT wxSplitterEvent;
22
23 // ---------------------------------------------------------------------------
24 // splitter constants
25 // ---------------------------------------------------------------------------
26
27 enum
28 {
29 wxSPLIT_HORIZONTAL = 1,
30 wxSPLIT_VERTICAL
31 };
32
33 enum
34 {
35 wxSPLIT_DRAG_NONE,
36 wxSPLIT_DRAG_DRAGGING,
37 wxSPLIT_DRAG_LEFT_DOWN
38 };
39
40 // ---------------------------------------------------------------------------
41 // wxSplitterWindow maintains one or two panes, with
42 // an optional vertical or horizontal split which
43 // can be used with the mouse or programmatically.
44 // ---------------------------------------------------------------------------
45
46 // TODO:
47 // 1) Perhaps make the borders sensitive to dragging in order to create a split.
48 // The MFC splitter window manages scrollbars as well so is able to
49 // put sash buttons on the scrollbars, but we probably don't want to go down
50 // this path.
51 // 2) for wxWindows 2.0, we must find a way to set the WS_CLIPCHILDREN style
52 // to prevent flickering. (WS_CLIPCHILDREN doesn't work in all cases so can't be
53 // standard).
54
55 class WXDLLEXPORT wxSplitterWindow: public wxWindow
56 {
57 public:
58
59 ////////////////////////////////////////////////////////////////////////////
60 // Public API
61
62 // Default constructor
63 wxSplitterWindow();
64
65 // Normal constructor
66 wxSplitterWindow(wxWindow *parent, wxWindowID id = -1,
67 const wxPoint& pos = wxDefaultPosition,
68 const wxSize& size = wxDefaultSize,
69 long style = wxSP_3D|wxCLIP_CHILDREN,
70 const wxString& name = "splitter");
71 ~wxSplitterWindow();
72
73 // Gets the only or left/top pane
74 wxWindow *GetWindow1() const { return m_windowOne; }
75
76 // Gets the right/bottom pane
77 wxWindow *GetWindow2() const { return m_windowTwo; }
78
79 // Sets the split mode
80 void SetSplitMode(int mode) { m_splitMode = mode; }
81
82 // Gets the split mode
83 int GetSplitMode() const { return m_splitMode; };
84
85 // Initialize with one window
86 void Initialize(wxWindow *window);
87
88 // Associates the given window with window 2, drawing the appropriate sash
89 // and changing the split mode.
90 // Does nothing and returns FALSE if the window is already split.
91 // A sashPosition of 0 means choose a default sash position,
92 // negative sashPosition specifies the size of right/lower pane as it's
93 // absolute value rather than the size of left/upper pane.
94 virtual bool SplitVertically(wxWindow *window1,
95 wxWindow *window2,
96 int sashPosition = 0);
97 virtual bool SplitHorizontally(wxWindow *window1,
98 wxWindow *window2,
99 int sashPosition = 0);
100
101 // Removes the specified (or second) window from the view
102 // Doesn't actually delete the window.
103 bool Unsplit(wxWindow *toRemove = (wxWindow *) NULL);
104
105 // Replaces one of the windows with another one (neither old nor new
106 // parameter should be NULL)
107 bool ReplaceWindow(wxWindow *winOld, wxWindow *winNew);
108
109 // Is the window split?
110 bool IsSplit() const { return (m_windowTwo != NULL); }
111
112 // Sets the sash size
113 void SetSashSize(int width) { m_sashSize = width; }
114
115 // Sets the border size
116 void SetBorderSize(int width) { m_borderSize = width; }
117
118 // Gets the sash size
119 int GetSashSize() const { return m_sashSize; }
120
121 // Gets the border size
122 int GetBorderSize() const { return m_borderSize; }
123
124 // Set the sash position
125 void SetSashPosition(int position, bool redraw = TRUE);
126
127 // Gets the sash position
128 int GetSashPosition() const { return m_sashPosition; }
129
130 // If this is zero, we can remove panes by dragging the sash.
131 void SetMinimumPaneSize(int min) { m_minimumPaneSize = min; }
132 int GetMinimumPaneSize() const { return m_minimumPaneSize; }
133
134 // Called when the sash position is about to be changed, return
135 // FALSE from here to prevent the change from taking place.
136 // Repositions sash to minimum position if pane would be too small.
137 // newSashPosition here is always positive or zero.
138 virtual bool OnSashPositionChange(int WXUNUSED(newSashPosition))
139 { return TRUE; }
140
141 // If the sash is moved to an extreme position, a subwindow
142 // is removed from the splitter window, and the app is
143 // notified. The app should delete or hide the window.
144 virtual void OnUnsplit(wxWindow *WXUNUSED(removed)) { }
145
146 // Called when the sash is double-clicked.
147 // The default behaviour is to remove the sash if the
148 // minimum pane size is zero.
149 virtual void OnDoubleClickSash(int WXUNUSED(x), int WXUNUSED(y)) { }
150
151 ////////////////////////////////////////////////////////////////////////////
152 // Implementation
153
154 // Paints the border and sash
155 void OnPaint(wxPaintEvent& event);
156
157 // Handles mouse events
158 void OnMouseEvent(wxMouseEvent& ev);
159
160 // Adjusts the panes
161 void OnSize(wxSizeEvent& event);
162
163 // In live mode, resize child windows in idle time
164 void OnIdle(wxIdleEvent& event);
165
166 // Draws borders
167 void DrawBorders(wxDC& dc);
168
169 // Draws the sash
170 void DrawSash(wxDC& dc);
171
172 // Draws the sash tracker (for whilst moving the sash)
173 void DrawSashTracker(int x, int y);
174
175 // Tests for x, y over sash
176 bool SashHitTest(int x, int y, int tolerance = 2);
177
178 // Resizes subwindows
179 void SizeWindows();
180
181 // Initialize colours
182 void InitColours();
183
184 protected:
185 // our event handlers
186 void OnSashPosChanged(wxSplitterEvent& event);
187 void OnSashPosChanging(wxSplitterEvent& event);
188 void OnDoubleClick(wxSplitterEvent& event);
189 void OnUnsplitEvent(wxSplitterEvent& event);
190 void OnSetCursor(wxSetCursorEvent& event);
191
192 void SendUnsplitEvent(wxWindow *winRemoved);
193
194 int m_splitMode;
195 bool m_permitUnsplitAlways;
196 bool m_needUpdating; // when in live mode, set the to TRUE to resize children in idle
197 wxWindow* m_windowOne;
198 wxWindow* m_windowTwo;
199 int m_dragMode;
200 int m_oldX;
201 int m_oldY;
202 int m_borderSize;
203 int m_sashSize; // Sash width or height
204 int m_sashPosition; // Number of pixels from left or top
205 int m_firstX;
206 int m_firstY;
207 int m_minimumPaneSize;
208 wxCursor* m_sashCursorWE;
209 wxCursor* m_sashCursorNS;
210 wxPen* m_sashTrackerPen;
211 wxPen* m_lightShadowPen;
212 wxPen* m_mediumShadowPen;
213 wxPen* m_darkShadowPen;
214 wxPen* m_hilightPen;
215 wxBrush* m_faceBrush;
216 wxPen* m_facePen;
217
218 private:
219 DECLARE_DYNAMIC_CLASS(wxSplitterWindow)
220 DECLARE_EVENT_TABLE()
221 };
222
223 // ----------------------------------------------------------------------------
224 // event class and macros
225 // ----------------------------------------------------------------------------
226
227 // we reuse the same class for all splitter event types because this is the
228 // usual wxWin convention, but the three event types have different kind of
229 // data associated with them, so the accessors can be only used if the real
230 // event type matches with the one for which the accessors make sense
231 class WXDLLEXPORT wxSplitterEvent : public wxCommandEvent
232 {
233 public:
234 wxSplitterEvent(wxEventType type = wxEVT_NULL,
235 wxSplitterWindow *splitter = (wxSplitterWindow *)NULL)
236 : wxCommandEvent(type)
237 {
238 SetEventObject(splitter);
239 if (splitter) m_id = splitter->GetId();
240 }
241
242 // SASH_POS_CHANGED methods
243
244 // setting the sash position to -1 prevents the change from taking place at
245 // all
246 void SetSashPosition(int pos)
247 {
248 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
249 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
250
251 m_data.pos = pos;
252 }
253
254 int GetSashPosition() const
255 {
256 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED
257 || GetEventType() == wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING);
258
259 return m_data.pos;
260 }
261
262 // UNSPLIT event methods
263 wxWindow *GetWindowBeingRemoved() const
264 {
265 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_UNSPLIT );
266
267 return m_data.win;
268 }
269
270 // DCLICK event methods
271 int GetX() const
272 {
273 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
274
275 return m_data.pt.x;
276 }
277
278 int GetY() const
279 {
280 wxASSERT( GetEventType() == wxEVT_COMMAND_SPLITTER_DOUBLECLICKED );
281
282 return m_data.pt.y;
283 }
284
285 private:
286 friend class WXDLLEXPORT wxSplitterWindow;
287
288 // data for the different types of event
289 union
290 {
291 int pos; // position for SASH_POS_CHANGED event
292 wxWindow *win; // window being removed for UNSPLIT event
293 struct
294 {
295 int x, y;
296 } pt; // position of double click for DCLICK event
297 } m_data;
298
299 DECLARE_DYNAMIC_CLASS(wxSplitterEvent)
300 };
301
302 typedef void (wxEvtHandler::*wxSplitterEventFunction)(wxSplitterEvent&);
303
304 #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
305 { \
306 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED, \
307 id, \
308 -1, \
309 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
310 NULL \
311 },
312
313 #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
314 { \
315 wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING, \
316 id, \
317 -1, \
318 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
319 NULL \
320 },
321
322 #define EVT_SPLITTER_DCLICK(id, fn) \
323 { \
324 wxEVT_COMMAND_SPLITTER_DOUBLECLICKED, \
325 id, \
326 -1, \
327 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
328 NULL \
329 },
330
331 #define EVT_SPLITTER_UNSPLIT(id, fn) \
332 { \
333 wxEVT_COMMAND_SPLITTER_UNSPLIT, \
334 id, \
335 -1, \
336 (wxObjectEventFunction)(wxEventFunction)(wxSplitterEventFunction) &fn, \
337 NULL \
338 },
339
340 #endif // __SPLITTERH_G__