splitter.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. #ifndef _WX_GENERIC_SPLITTER_H_
  11. #define _WX_GENERIC_SPLITTER_H_
  12. #include "wx/window.h" // base class declaration
  13. #include "wx/containr.h" // wxControlContainer
  14. class WXDLLIMPEXP_FWD_CORE wxSplitterEvent;
  15. // ---------------------------------------------------------------------------
  16. // splitter constants
  17. // ---------------------------------------------------------------------------
  18. enum wxSplitMode
  19. {
  20. wxSPLIT_HORIZONTAL = 1,
  21. wxSPLIT_VERTICAL
  22. };
  23. enum
  24. {
  25. wxSPLIT_DRAG_NONE,
  26. wxSPLIT_DRAG_DRAGGING,
  27. wxSPLIT_DRAG_LEFT_DOWN
  28. };
  29. // ---------------------------------------------------------------------------
  30. // wxSplitterWindow maintains one or two panes, with
  31. // an optional vertical or horizontal split which
  32. // can be used with the mouse or programmatically.
  33. // ---------------------------------------------------------------------------
  34. // TODO:
  35. // 1) Perhaps make the borders sensitive to dragging in order to create a split.
  36. // The MFC splitter window manages scrollbars as well so is able to
  37. // put sash buttons on the scrollbars, but we probably don't want to go down
  38. // this path.
  39. // 2) for wxWidgets 2.0, we must find a way to set the WS_CLIPCHILDREN style
  40. // to prevent flickering. (WS_CLIPCHILDREN doesn't work in all cases so can't be
  41. // standard).
  42. class WXDLLIMPEXP_CORE wxSplitterWindow: public wxNavigationEnabled<wxWindow>
  43. {
  44. public:
  45. ////////////////////////////////////////////////////////////////////////////
  46. // Public API
  47. // Default constructor
  48. wxSplitterWindow()
  49. {
  50. Init();
  51. }
  52. // Normal constructor
  53. wxSplitterWindow(wxWindow *parent, wxWindowID id = wxID_ANY,
  54. const wxPoint& pos = wxDefaultPosition,
  55. const wxSize& size = wxDefaultSize,
  56. long style = wxSP_3D,
  57. const wxString& name = wxT("splitter"))
  58. {
  59. Init();
  60. Create(parent, id, pos, size, style, name);
  61. }
  62. virtual ~wxSplitterWindow();
  63. bool Create(wxWindow *parent, wxWindowID id = wxID_ANY,
  64. const wxPoint& pos = wxDefaultPosition,
  65. const wxSize& size = wxDefaultSize,
  66. long style = wxSP_3D,
  67. const wxString& name = wxT("splitter"));
  68. // Gets the only or left/top pane
  69. wxWindow *GetWindow1() const { return m_windowOne; }
  70. // Gets the right/bottom pane
  71. wxWindow *GetWindow2() const { return m_windowTwo; }
  72. // Sets the split mode
  73. void SetSplitMode(int mode)
  74. {
  75. wxASSERT_MSG( mode == wxSPLIT_VERTICAL || mode == wxSPLIT_HORIZONTAL,
  76. wxT("invalid split mode") );
  77. m_splitMode = (wxSplitMode)mode;
  78. }
  79. // Gets the split mode
  80. wxSplitMode GetSplitMode() const { return m_splitMode; }
  81. // Initialize with one window
  82. void Initialize(wxWindow *window);
  83. // Associates the given window with window 2, drawing the appropriate sash
  84. // and changing the split mode.
  85. // Does nothing and returns false if the window is already split.
  86. // A sashPosition of 0 means choose a default sash position,
  87. // negative sashPosition specifies the size of right/lower pane as it's
  88. // absolute value rather than the size of left/upper pane.
  89. virtual bool SplitVertically(wxWindow *window1,
  90. wxWindow *window2,
  91. int sashPosition = 0)
  92. { return DoSplit(wxSPLIT_VERTICAL, window1, window2, sashPosition); }
  93. virtual bool SplitHorizontally(wxWindow *window1,
  94. wxWindow *window2,
  95. int sashPosition = 0)
  96. { return DoSplit(wxSPLIT_HORIZONTAL, window1, window2, sashPosition); }
  97. // Removes the specified (or second) window from the view
  98. // Doesn't actually delete the window.
  99. bool Unsplit(wxWindow *toRemove = NULL);
  100. // Replaces one of the windows with another one (neither old nor new
  101. // parameter should be NULL)
  102. bool ReplaceWindow(wxWindow *winOld, wxWindow *winNew);
  103. // Make sure the child window sizes are updated. This is useful
  104. // for reducing flicker by updating the sizes before a
  105. // window is shown, if you know the overall size is correct.
  106. void UpdateSize();
  107. // Is the window split?
  108. bool IsSplit() const { return (m_windowTwo != NULL); }
  109. // Sets the border size
  110. void SetBorderSize(int WXUNUSED(width)) { }
  111. // Hide or show the sash and test whether it's currently hidden.
  112. void SetSashInvisible(bool invisible = true);
  113. bool IsSashInvisible() const { return HasFlag(wxSP_NOSASH); }
  114. // Gets the current sash size which may be 0 if it's hidden and the default
  115. // sash size.
  116. int GetSashSize() const;
  117. int GetDefaultSashSize() const;
  118. // Gets the border size
  119. int GetBorderSize() const;
  120. // Set the sash position
  121. void SetSashPosition(int position, bool redraw = true);
  122. // Gets the sash position
  123. int GetSashPosition() const { return m_sashPosition; }
  124. // Set the sash gravity
  125. void SetSashGravity(double gravity);
  126. // Gets the sash gravity
  127. double GetSashGravity() const { return m_sashGravity; }
  128. // If this is zero, we can remove panes by dragging the sash.
  129. void SetMinimumPaneSize(int min);
  130. int GetMinimumPaneSize() const { return m_minimumPaneSize; }
  131. // NB: the OnXXX() functions below are for backwards compatibility only,
  132. // don't use them in new code but handle the events instead!
  133. // called when the sash position is about to change, may return a new value
  134. // for the sash or -1 to prevent the change from happening at all
  135. virtual int OnSashPositionChanging(int newSashPosition);
  136. // Called when the sash position is about to be changed, return
  137. // false from here to prevent the change from taking place.
  138. // Repositions sash to minimum position if pane would be too small.
  139. // newSashPosition here is always positive or zero.
  140. virtual bool OnSashPositionChange(int newSashPosition);
  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 *removed);
  145. // Called when the sash is double-clicked.
  146. // The default behaviour is to remove the sash if the
  147. // minimum pane size is zero.
  148. virtual void OnDoubleClickSash(int x, int y);
  149. ////////////////////////////////////////////////////////////////////////////
  150. // Implementation
  151. // Paints the border and sash
  152. void OnPaint(wxPaintEvent& event);
  153. // Handles mouse events
  154. void OnMouseEvent(wxMouseEvent& ev);
  155. // Aborts dragging mode
  156. void OnMouseCaptureLost(wxMouseCaptureLostEvent& event);
  157. // Adjusts the panes
  158. void OnSize(wxSizeEvent& event);
  159. // In live mode, resize child windows in idle time
  160. void OnInternalIdle();
  161. // Draws the sash
  162. virtual void DrawSash(wxDC& dc);
  163. // Draws the sash tracker (for whilst moving the sash)
  164. virtual void DrawSashTracker(int x, int y);
  165. // Tests for x, y over sash
  166. virtual bool SashHitTest(int x, int y);
  167. // Resizes subwindows
  168. virtual void SizeWindows();
  169. #ifdef __WXMAC__
  170. virtual bool MacClipGrandChildren() const { return true ; }
  171. #endif
  172. // Sets the sash size: this doesn't do anything and shouldn't be used at
  173. // all any more.
  174. wxDEPRECATED_INLINE( void SetSashSize(int WXUNUSED(width)), return; )
  175. protected:
  176. // event handlers
  177. #if defined(__WXMSW__) || defined(__WXMAC__)
  178. void OnSetCursor(wxSetCursorEvent& event);
  179. #endif // wxMSW
  180. // send the given event, return false if the event was processed and vetoed
  181. // by the user code
  182. bool DoSendEvent(wxSplitterEvent& event);
  183. // common part of all ctors
  184. void Init();
  185. // common part of SplitVertically() and SplitHorizontally()
  186. bool DoSplit(wxSplitMode mode,
  187. wxWindow *window1, wxWindow *window2,
  188. int sashPosition);
  189. // adjusts sash position with respect to min. pane and window sizes
  190. int AdjustSashPosition(int sashPos) const;
  191. // get either width or height depending on the split mode
  192. int GetWindowSize() const;
  193. // convert the user specified sash position which may be > 0 (as is), < 0
  194. // (specifying the size of the right pane) or 0 (use default) to the real
  195. // position to be passed to DoSetSashPosition()
  196. int ConvertSashPosition(int sashPos) const;
  197. // set the real sash position, sashPos here must be positive
  198. //
  199. // returns true if the sash position has been changed, false otherwise
  200. bool DoSetSashPosition(int sashPos);
  201. // set the sash position and send an event about it having been changed
  202. void SetSashPositionAndNotify(int sashPos);
  203. // callbacks executed when we detect that the mouse has entered or left
  204. // the sash
  205. virtual void OnEnterSash();
  206. virtual void OnLeaveSash();
  207. // set the cursor appropriate for the current split mode
  208. void SetResizeCursor();
  209. // redraw the splitter if its "hotness" changed if necessary
  210. void RedrawIfHotSensitive(bool isHot);
  211. // return the best size of the splitter equal to best sizes of its
  212. // subwindows
  213. virtual wxSize DoGetBestSize() const;
  214. wxSplitMode m_splitMode;
  215. wxWindow* m_windowOne;
  216. wxWindow* m_windowTwo;
  217. int m_dragMode;
  218. int m_oldX; // current tracker position if not live mode
  219. int m_oldY; // current tracker position if not live mode
  220. int m_sashPosition; // Number of pixels from left or top
  221. double m_sashGravity;
  222. wxSize m_lastSize;
  223. int m_requestedSashPosition;
  224. int m_sashPositionCurrent; // while dragging
  225. wxPoint m_ptStart; // mouse position when dragging started
  226. int m_sashStart; // sash position when dragging started
  227. int m_minimumPaneSize;
  228. wxCursor m_sashCursorWE;
  229. wxCursor m_sashCursorNS;
  230. wxPen *m_sashTrackerPen;
  231. // when in live mode, set this to true to resize children in idle
  232. bool m_needUpdating:1;
  233. bool m_permitUnsplitAlways:1;
  234. bool m_isHot:1;
  235. private:
  236. DECLARE_DYNAMIC_CLASS(wxSplitterWindow)
  237. DECLARE_EVENT_TABLE()
  238. wxDECLARE_NO_COPY_CLASS(wxSplitterWindow);
  239. };
  240. // ----------------------------------------------------------------------------
  241. // event class and macros
  242. // ----------------------------------------------------------------------------
  243. // we reuse the same class for all splitter event types because this is the
  244. // usual wxWin convention, but the three event types have different kind of
  245. // data associated with them, so the accessors can be only used if the real
  246. // event type matches with the one for which the accessors make sense
  247. class WXDLLIMPEXP_CORE wxSplitterEvent : public wxNotifyEvent
  248. {
  249. public:
  250. wxSplitterEvent(wxEventType type = wxEVT_NULL,
  251. wxSplitterWindow *splitter = NULL)
  252. : wxNotifyEvent(type)
  253. {
  254. SetEventObject(splitter);
  255. if (splitter) m_id = splitter->GetId();
  256. }
  257. wxSplitterEvent(const wxSplitterEvent& event)
  258. : wxNotifyEvent(event), m_data(event.m_data) { }
  259. // SASH_POS_CHANGED methods
  260. // setting the sash position to -1 prevents the change from taking place at
  261. // all
  262. void SetSashPosition(int pos)
  263. {
  264. wxASSERT( GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGED
  265. || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING);
  266. m_data.pos = pos;
  267. }
  268. int GetSashPosition() const
  269. {
  270. wxASSERT( GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGED
  271. || GetEventType() == wxEVT_SPLITTER_SASH_POS_CHANGING);
  272. return m_data.pos;
  273. }
  274. // UNSPLIT event methods
  275. wxWindow *GetWindowBeingRemoved() const
  276. {
  277. wxASSERT( GetEventType() == wxEVT_SPLITTER_UNSPLIT );
  278. return m_data.win;
  279. }
  280. // DCLICK event methods
  281. int GetX() const
  282. {
  283. wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED );
  284. return m_data.pt.x;
  285. }
  286. int GetY() const
  287. {
  288. wxASSERT( GetEventType() == wxEVT_SPLITTER_DOUBLECLICKED );
  289. return m_data.pt.y;
  290. }
  291. virtual wxEvent *Clone() const { return new wxSplitterEvent(*this); }
  292. private:
  293. friend class WXDLLIMPEXP_FWD_CORE wxSplitterWindow;
  294. // data for the different types of event
  295. union
  296. {
  297. int pos; // position for SASH_POS_CHANGED event
  298. wxWindow *win; // window being removed for UNSPLIT event
  299. struct
  300. {
  301. int x, y;
  302. } pt; // position of double click for DCLICK event
  303. } m_data;
  304. DECLARE_DYNAMIC_CLASS_NO_ASSIGN(wxSplitterEvent)
  305. };
  306. typedef void (wxEvtHandler::*wxSplitterEventFunction)(wxSplitterEvent&);
  307. #define wxSplitterEventHandler(func) \
  308. wxEVENT_HANDLER_CAST(wxSplitterEventFunction, func)
  309. #define wx__DECLARE_SPLITTEREVT(evt, id, fn) \
  310. wx__DECLARE_EVT1(wxEVT_SPLITTER_ ## evt, id, wxSplitterEventHandler(fn))
  311. #define EVT_SPLITTER_SASH_POS_CHANGED(id, fn) \
  312. wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGED, id, fn)
  313. #define EVT_SPLITTER_SASH_POS_CHANGING(id, fn) \
  314. wx__DECLARE_SPLITTEREVT(SASH_POS_CHANGING, id, fn)
  315. #define EVT_SPLITTER_DCLICK(id, fn) \
  316. wx__DECLARE_SPLITTEREVT(DOUBLECLICKED, id, fn)
  317. #define EVT_SPLITTER_UNSPLIT(id, fn) \
  318. wx__DECLARE_SPLITTEREVT(UNSPLIT, id, fn)
  319. // old wxEVT_COMMAND_* constants
  320. #define wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGED wxEVT_SPLITTER_SASH_POS_CHANGED
  321. #define wxEVT_COMMAND_SPLITTER_SASH_POS_CHANGING wxEVT_SPLITTER_SASH_POS_CHANGING
  322. #define wxEVT_COMMAND_SPLITTER_DOUBLECLICKED wxEVT_SPLITTER_DOUBLECLICKED
  323. #define wxEVT_COMMAND_SPLITTER_UNSPLIT wxEVT_SPLITTER_UNSPLIT
  324. #endif // _WX_GENERIC_SPLITTER_H_