splitter.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/generic/splitter.cpp
  3. // Purpose: wxSplitterWindow implementation
  4. // Author: Julian Smart
  5. // Modified by:
  6. // Created: 01/02/97
  7. // Copyright: (c) Julian Smart
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. // For compilers that support precompilation, includes "wx.h".
  11. #include "wx/wxprec.h"
  12. #ifdef __BORLANDC__
  13. #pragma hdrstop
  14. #endif
  15. #if wxUSE_SPLITTER
  16. #include "wx/splitter.h"
  17. #ifndef WX_PRECOMP
  18. #include "wx/string.h"
  19. #include "wx/utils.h"
  20. #include "wx/log.h"
  21. #include "wx/dcclient.h"
  22. #include "wx/dcscreen.h"
  23. #include "wx/window.h"
  24. #include "wx/dialog.h"
  25. #include "wx/frame.h"
  26. #include "wx/settings.h"
  27. #endif
  28. #include "wx/renderer.h"
  29. #include <stdlib.h>
  30. wxDEFINE_EVENT( wxEVT_SPLITTER_SASH_POS_CHANGED, wxSplitterEvent );
  31. wxDEFINE_EVENT( wxEVT_SPLITTER_SASH_POS_CHANGING, wxSplitterEvent );
  32. wxDEFINE_EVENT( wxEVT_SPLITTER_DOUBLECLICKED, wxSplitterEvent );
  33. wxDEFINE_EVENT( wxEVT_SPLITTER_UNSPLIT, wxSplitterEvent );
  34. IMPLEMENT_DYNAMIC_CLASS(wxSplitterWindow, wxWindow)
  35. /*
  36. TODO PROPERTIES
  37. style wxSP_3D
  38. sashpos (long , 0 )
  39. minsize (long -1 )
  40. object, object_ref
  41. orientation
  42. */
  43. IMPLEMENT_DYNAMIC_CLASS(wxSplitterEvent, wxNotifyEvent)
  44. BEGIN_EVENT_TABLE(wxSplitterWindow, wxWindow)
  45. EVT_PAINT(wxSplitterWindow::OnPaint)
  46. EVT_SIZE(wxSplitterWindow::OnSize)
  47. EVT_MOUSE_EVENTS(wxSplitterWindow::OnMouseEvent)
  48. EVT_MOUSE_CAPTURE_LOST(wxSplitterWindow::OnMouseCaptureLost)
  49. #if defined( __WXMSW__ ) || defined( __WXMAC__)
  50. EVT_SET_CURSOR(wxSplitterWindow::OnSetCursor)
  51. #endif // wxMSW
  52. END_EVENT_TABLE()
  53. static bool IsLive(wxSplitterWindow* wnd)
  54. {
  55. // with wxSP_LIVE_UPDATE style the splitter windows are always resized
  56. // following the mouse movement while it drags the sash, without it we only
  57. // draw the sash at the new position but only resize the windows when the
  58. // dragging is finished
  59. #if defined( __WXMAC__ ) && defined(TARGET_API_MAC_OSX) && TARGET_API_MAC_OSX == 1
  60. return true; // Mac can't paint outside paint event - always need live mode
  61. #else
  62. return wnd->HasFlag(wxSP_LIVE_UPDATE);
  63. #endif
  64. }
  65. bool wxSplitterWindow::Create(wxWindow *parent, wxWindowID id,
  66. const wxPoint& pos,
  67. const wxSize& size,
  68. long style,
  69. const wxString& name)
  70. {
  71. // allow TABbing from one window to the other
  72. style |= wxTAB_TRAVERSAL;
  73. if ( !wxWindow::Create(parent, id, pos, size, style, name) )
  74. return false;
  75. m_lastSize = GetClientSize();
  76. m_permitUnsplitAlways = (style & wxSP_PERMIT_UNSPLIT) != 0;
  77. // FIXME: with this line the background is not erased at all under GTK1,
  78. // so temporary avoid it there
  79. #if !defined(__WXGTK__) || defined(__WXGTK20__)
  80. // don't erase the splitter background, it's pointless as we overwrite it
  81. // anyhow
  82. SetBackgroundStyle(wxBG_STYLE_CUSTOM);
  83. #endif
  84. return true;
  85. }
  86. void wxSplitterWindow::Init()
  87. {
  88. m_splitMode = wxSPLIT_VERTICAL;
  89. m_permitUnsplitAlways = true;
  90. m_windowOne = NULL;
  91. m_windowTwo = NULL;
  92. m_dragMode = wxSPLIT_DRAG_NONE;
  93. m_oldX = 0;
  94. m_oldY = 0;
  95. m_sashStart = 0;
  96. m_sashPosition = 0;
  97. m_requestedSashPosition = INT_MAX;
  98. m_sashGravity = 0.0;
  99. m_lastSize = wxSize(0,0);
  100. m_minimumPaneSize = 0;
  101. m_sashCursorWE = wxCursor(wxCURSOR_SIZEWE);
  102. m_sashCursorNS = wxCursor(wxCURSOR_SIZENS);
  103. m_sashTrackerPen = new wxPen(*wxBLACK, 2, wxPENSTYLE_SOLID);
  104. m_needUpdating = false;
  105. m_isHot = false;
  106. }
  107. wxSplitterWindow::~wxSplitterWindow()
  108. {
  109. delete m_sashTrackerPen;
  110. }
  111. // ----------------------------------------------------------------------------
  112. // entering/leaving sash
  113. // ----------------------------------------------------------------------------
  114. void wxSplitterWindow::RedrawIfHotSensitive(bool isHot)
  115. {
  116. if ( wxRendererNative::Get().GetSplitterParams(this).isHotSensitive )
  117. {
  118. m_isHot = isHot;
  119. wxClientDC dc(this);
  120. DrawSash(dc);
  121. }
  122. //else: we don't change our appearance, don't redraw to avoid flicker
  123. }
  124. void wxSplitterWindow::OnEnterSash()
  125. {
  126. SetResizeCursor();
  127. RedrawIfHotSensitive(true);
  128. }
  129. void wxSplitterWindow::OnLeaveSash()
  130. {
  131. SetCursor(*wxSTANDARD_CURSOR);
  132. RedrawIfHotSensitive(false);
  133. }
  134. void wxSplitterWindow::SetResizeCursor()
  135. {
  136. SetCursor(m_splitMode == wxSPLIT_VERTICAL ? m_sashCursorWE
  137. : m_sashCursorNS);
  138. }
  139. // ----------------------------------------------------------------------------
  140. // other event handlers
  141. // ----------------------------------------------------------------------------
  142. void wxSplitterWindow::OnPaint(wxPaintEvent& WXUNUSED(event))
  143. {
  144. wxPaintDC dc(this);
  145. #ifdef __WXOSX__
  146. // as subpanels might have a transparent background we must erase the background
  147. // at least on OSX, otherwise traces of the sash will remain
  148. // test with: splitter sample->replace right window
  149. dc.Clear();
  150. #endif
  151. DrawSash(dc);
  152. }
  153. void wxSplitterWindow::OnInternalIdle()
  154. {
  155. wxWindow::OnInternalIdle();
  156. // We may need to update the children sizes in two cases: either because
  157. // we're in the middle of a live update as indicated by m_needUpdating or
  158. // because we have a requested but not yet set sash position as indicated
  159. // by m_requestedSashPosition having a valid value.
  160. if ( m_needUpdating )
  161. {
  162. m_needUpdating = false;
  163. }
  164. else if ( m_requestedSashPosition == INT_MAX )
  165. {
  166. // We don't need to resize the children.
  167. return;
  168. }
  169. SizeWindows();
  170. }
  171. void wxSplitterWindow::OnMouseEvent(wxMouseEvent& event)
  172. {
  173. int x = (int)event.GetX(),
  174. y = (int)event.GetY();
  175. if ( GetWindowStyle() & wxSP_NOSASH )
  176. {
  177. event.Skip();
  178. return;
  179. }
  180. bool isLive = IsLive(this);
  181. if (event.LeftDown())
  182. {
  183. if ( SashHitTest(x, y) )
  184. {
  185. // Start the drag now
  186. m_dragMode = wxSPLIT_DRAG_DRAGGING;
  187. // Capture mouse and set the cursor
  188. CaptureMouse();
  189. SetResizeCursor();
  190. if ( !isLive )
  191. {
  192. // remember the initial sash position and draw the initial
  193. // shadow sash
  194. m_sashPositionCurrent = m_sashPosition;
  195. m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
  196. m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
  197. DrawSashTracker(m_oldX, m_oldY);
  198. }
  199. m_ptStart = wxPoint(x,y);
  200. m_sashStart = m_sashPosition;
  201. return;
  202. }
  203. }
  204. else if (event.LeftUp() && m_dragMode == wxSPLIT_DRAG_DRAGGING)
  205. {
  206. // We can stop dragging now and see what we've got.
  207. m_dragMode = wxSPLIT_DRAG_NONE;
  208. // Release mouse and unset the cursor
  209. ReleaseMouse();
  210. SetCursor(* wxSTANDARD_CURSOR);
  211. // exit if unsplit after doubleclick
  212. if ( !IsSplit() )
  213. {
  214. return;
  215. }
  216. // Erase old tracker
  217. if ( !isLive )
  218. {
  219. DrawSashTracker(m_oldX, m_oldY);
  220. }
  221. // the position of the click doesn't exactly correspond to
  222. // m_sashPosition, rather it changes it by the distance by which the
  223. // mouse has moved
  224. int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
  225. int posSashNew = OnSashPositionChanging(m_sashStart + diff);
  226. if ( posSashNew == -1 )
  227. {
  228. // change not allowed
  229. return;
  230. }
  231. if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
  232. {
  233. // Deal with possible unsplit scenarios
  234. if ( posSashNew == 0 )
  235. {
  236. // We remove the first window from the view
  237. wxWindow *removedWindow = m_windowOne;
  238. m_windowOne = m_windowTwo;
  239. m_windowTwo = NULL;
  240. OnUnsplit(removedWindow);
  241. wxSplitterEvent eventUnsplit(wxEVT_SPLITTER_UNSPLIT, this);
  242. eventUnsplit.m_data.win = removedWindow;
  243. (void)DoSendEvent(eventUnsplit);
  244. SetSashPositionAndNotify(0);
  245. }
  246. else if ( posSashNew == GetWindowSize() )
  247. {
  248. // We remove the second window from the view
  249. wxWindow *removedWindow = m_windowTwo;
  250. m_windowTwo = NULL;
  251. OnUnsplit(removedWindow);
  252. wxSplitterEvent eventUnsplit(wxEVT_SPLITTER_UNSPLIT, this);
  253. eventUnsplit.m_data.win = removedWindow;
  254. (void)DoSendEvent(eventUnsplit);
  255. SetSashPositionAndNotify(0);
  256. }
  257. else
  258. {
  259. SetSashPositionAndNotify(posSashNew);
  260. }
  261. }
  262. else
  263. {
  264. SetSashPositionAndNotify(posSashNew);
  265. }
  266. SizeWindows();
  267. } // left up && dragging
  268. else if ((event.Moving() || event.Leaving() || event.Entering()) && (m_dragMode == wxSPLIT_DRAG_NONE))
  269. {
  270. if ( event.Leaving() || !SashHitTest(x, y) )
  271. OnLeaveSash();
  272. else
  273. OnEnterSash();
  274. }
  275. else if (event.Dragging() && (m_dragMode == wxSPLIT_DRAG_DRAGGING))
  276. {
  277. int diff = m_splitMode == wxSPLIT_VERTICAL ? x - m_ptStart.x : y - m_ptStart.y;
  278. int posSashNew = OnSashPositionChanging(m_sashStart + diff);
  279. if ( posSashNew == -1 )
  280. {
  281. // change not allowed
  282. return;
  283. }
  284. if ( !isLive )
  285. {
  286. if ( posSashNew == m_sashPositionCurrent )
  287. return;
  288. m_sashPositionCurrent = posSashNew;
  289. // Erase old tracker
  290. DrawSashTracker(m_oldX, m_oldY);
  291. m_oldX = (m_splitMode == wxSPLIT_VERTICAL ? m_sashPositionCurrent : x);
  292. m_oldY = (m_splitMode != wxSPLIT_VERTICAL ? m_sashPositionCurrent : y);
  293. #ifdef __WXMSW__
  294. // As we captured the mouse, we may get the mouse events from outside
  295. // our window - for example, negative values in x, y. This has a weird
  296. // consequence under MSW where we use unsigned values sometimes and
  297. // signed ones other times: the coordinates turn as big positive
  298. // numbers and so the sash is drawn on the *right* side of the window
  299. // instead of the left (or bottom instead of top). Correct this.
  300. if ( (short)m_oldX < 0 )
  301. m_oldX = 0;
  302. if ( (short)m_oldY < 0 )
  303. m_oldY = 0;
  304. #endif // __WXMSW__
  305. // Draw new one
  306. DrawSashTracker(m_oldX, m_oldY);
  307. }
  308. else
  309. {
  310. if ( posSashNew == m_sashPosition )
  311. return;
  312. DoSetSashPosition(posSashNew);
  313. // in live mode, the new position is the actual sash position, clear requested position!
  314. m_requestedSashPosition = INT_MAX;
  315. m_needUpdating = true;
  316. }
  317. }
  318. else if ( event.LeftDClick() && m_windowTwo )
  319. {
  320. OnDoubleClickSash(x, y);
  321. }
  322. else
  323. {
  324. event.Skip();
  325. }
  326. }
  327. void wxSplitterWindow::OnMouseCaptureLost(wxMouseCaptureLostEvent& WXUNUSED(event))
  328. {
  329. if (m_dragMode != wxSPLIT_DRAG_DRAGGING)
  330. return;
  331. m_dragMode = wxSPLIT_DRAG_NONE;
  332. SetCursor(* wxSTANDARD_CURSOR);
  333. // Erase old tracker
  334. if ( !IsLive(this) )
  335. {
  336. DrawSashTracker(m_oldX, m_oldY);
  337. }
  338. }
  339. void wxSplitterWindow::OnSize(wxSizeEvent& event)
  340. {
  341. // only process this message if we're not iconized - otherwise iconizing
  342. // and restoring a window containing the splitter has a funny side effect
  343. // of changing the splitter position!
  344. wxWindow *parent = wxGetTopLevelParent(this);
  345. bool iconized;
  346. wxTopLevelWindow *winTop = wxDynamicCast(parent, wxTopLevelWindow);
  347. if ( winTop )
  348. {
  349. iconized = winTop->IsIconized();
  350. }
  351. else
  352. {
  353. wxFAIL_MSG(wxT("should have a top level parent!"));
  354. iconized = false;
  355. }
  356. if ( iconized )
  357. {
  358. m_lastSize = wxSize(0,0);
  359. event.Skip();
  360. return;
  361. }
  362. const wxSize curSize = event.GetSize();
  363. // Update the sash position if needed.
  364. //
  365. // Notice that we shouldn't do this if the sash position requested by user
  366. // couldn't be set yet as it would never be taken into account at all if we
  367. // modified it before this happens.
  368. if ( m_windowTwo && m_requestedSashPosition == INT_MAX )
  369. {
  370. int size = m_splitMode == wxSPLIT_VERTICAL ? curSize.x : curSize.y;
  371. int old_size = m_splitMode == wxSPLIT_VERTICAL ? m_lastSize.x : m_lastSize.y;
  372. // Don't do anything if the size didn't really change.
  373. if ( size != old_size )
  374. {
  375. int newPosition = -1;
  376. // Apply gravity if we use it.
  377. int delta = (int) ( (size - old_size)*m_sashGravity );
  378. if ( delta != 0 )
  379. {
  380. newPosition = m_sashPosition + delta;
  381. if( newPosition < m_minimumPaneSize )
  382. newPosition = m_minimumPaneSize;
  383. }
  384. // Also check if the second window became too small.
  385. newPosition = AdjustSashPosition(newPosition == -1
  386. ? m_sashPosition
  387. : newPosition);
  388. if ( newPosition != m_sashPosition )
  389. SetSashPositionAndNotify(newPosition);
  390. }
  391. }
  392. m_lastSize = curSize;
  393. SizeWindows();
  394. }
  395. void wxSplitterWindow::SetSashGravity(double gravity)
  396. {
  397. wxCHECK_RET( gravity >= 0. && gravity <= 1.,
  398. wxT("invalid gravity value") );
  399. m_sashGravity = gravity;
  400. }
  401. bool wxSplitterWindow::SashHitTest(int x, int y)
  402. {
  403. if ( m_windowTwo == NULL || m_sashPosition == 0)
  404. return false; // No sash
  405. int z = m_splitMode == wxSPLIT_VERTICAL ? x : y;
  406. int hitMax = m_sashPosition + GetSashSize() - 1;
  407. return z >= m_sashPosition && z <= hitMax;
  408. }
  409. void wxSplitterWindow::SetSashInvisible(bool invisible)
  410. {
  411. if ( IsSashInvisible() != invisible )
  412. ToggleWindowStyle(wxSP_NOSASH);
  413. }
  414. int wxSplitterWindow::GetSashSize() const
  415. {
  416. return IsSashInvisible() ? 0 : GetDefaultSashSize();
  417. }
  418. int wxSplitterWindow::GetDefaultSashSize() const
  419. {
  420. return wxRendererNative::Get().GetSplitterParams(this).widthSash;
  421. }
  422. int wxSplitterWindow::GetBorderSize() const
  423. {
  424. return wxRendererNative::Get().GetSplitterParams(this).border;
  425. }
  426. // Draw the sash
  427. void wxSplitterWindow::DrawSash(wxDC& dc)
  428. {
  429. if (HasFlag(wxSP_3DBORDER))
  430. wxRendererNative::Get().DrawSplitterBorder
  431. (
  432. this,
  433. dc,
  434. GetClientRect()
  435. );
  436. // don't draw sash if we're not split
  437. if ( m_sashPosition == 0 || !m_windowTwo )
  438. return;
  439. // nor if we're configured to not show it
  440. if ( IsSashInvisible() )
  441. return;
  442. wxRendererNative::Get().DrawSplitterSash
  443. (
  444. this,
  445. dc,
  446. GetClientSize(),
  447. m_sashPosition,
  448. m_splitMode == wxSPLIT_VERTICAL ? wxVERTICAL
  449. : wxHORIZONTAL,
  450. m_isHot ? (int)wxCONTROL_CURRENT : 0
  451. );
  452. }
  453. // Draw the sash tracker (for whilst moving the sash)
  454. void wxSplitterWindow::DrawSashTracker(int x, int y)
  455. {
  456. int w, h;
  457. GetClientSize(&w, &h);
  458. wxScreenDC screenDC;
  459. int x1, y1;
  460. int x2, y2;
  461. if ( m_splitMode == wxSPLIT_VERTICAL )
  462. {
  463. x1 = x2 = wxClip(x, 0, w) + m_sashTrackerPen->GetWidth()/2;
  464. y1 = 2;
  465. y2 = h-2;
  466. }
  467. else
  468. {
  469. y1 = y2 = wxClip(y, 0, h) + m_sashTrackerPen->GetWidth()/2;
  470. x1 = 2;
  471. x2 = w-2;
  472. }
  473. ClientToScreen(&x1, &y1);
  474. ClientToScreen(&x2, &y2);
  475. screenDC.SetLogicalFunction(wxINVERT);
  476. screenDC.SetPen(*m_sashTrackerPen);
  477. screenDC.SetBrush(*wxTRANSPARENT_BRUSH);
  478. screenDC.DrawLine(x1, y1, x2, y2);
  479. screenDC.SetLogicalFunction(wxCOPY);
  480. }
  481. int wxSplitterWindow::GetWindowSize() const
  482. {
  483. wxSize size = GetClientSize();
  484. return m_splitMode == wxSPLIT_VERTICAL ? size.x : size.y;
  485. }
  486. int wxSplitterWindow::AdjustSashPosition(int sashPos) const
  487. {
  488. wxWindow *win;
  489. win = GetWindow1();
  490. if ( win )
  491. {
  492. // the window shouldn't be smaller than its own minimal size nor
  493. // smaller than the minimual pane size specified for this splitter
  494. int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
  495. : win->GetMinHeight();
  496. if ( minSize == -1 || m_minimumPaneSize > minSize )
  497. minSize = m_minimumPaneSize;
  498. minSize += GetBorderSize();
  499. if ( sashPos < minSize )
  500. sashPos = minSize;
  501. }
  502. win = GetWindow2();
  503. if ( win )
  504. {
  505. int minSize = m_splitMode == wxSPLIT_VERTICAL ? win->GetMinWidth()
  506. : win->GetMinHeight();
  507. if ( minSize == -1 || m_minimumPaneSize > minSize )
  508. minSize = m_minimumPaneSize;
  509. int maxSize = GetWindowSize() - minSize - GetBorderSize() - GetSashSize();
  510. if ( maxSize > 0 && sashPos > maxSize && maxSize >= m_minimumPaneSize)
  511. sashPos = maxSize;
  512. }
  513. return sashPos;
  514. }
  515. bool wxSplitterWindow::DoSetSashPosition(int sashPos)
  516. {
  517. int newSashPosition = AdjustSashPosition(sashPos);
  518. if ( newSashPosition == m_sashPosition )
  519. return false;
  520. m_sashPosition = newSashPosition;
  521. return true;
  522. }
  523. void wxSplitterWindow::SetSashPositionAndNotify(int sashPos)
  524. {
  525. // we must reset the request here, otherwise the sash would be stuck at
  526. // old position if the user attempted to move the sash after invalid
  527. // (e.g. smaller than minsize) sash position was requested using
  528. // SetSashPosition():
  529. m_requestedSashPosition = INT_MAX;
  530. // note that we must send the event in any case, i.e. even if the sash
  531. // position hasn't changed and DoSetSashPosition() returns false because we
  532. // must generate a CHANGED event at the end of resizing
  533. DoSetSashPosition(sashPos);
  534. wxSplitterEvent event(wxEVT_SPLITTER_SASH_POS_CHANGED, this);
  535. event.m_data.pos = m_sashPosition;
  536. (void)DoSendEvent(event);
  537. }
  538. // Position and size subwindows.
  539. // Note that the border size applies to each subwindow, not
  540. // including the edges next to the sash.
  541. void wxSplitterWindow::SizeWindows()
  542. {
  543. // check if we have delayed setting the real sash position
  544. if ( m_requestedSashPosition != INT_MAX )
  545. {
  546. int newSashPosition = ConvertSashPosition(m_requestedSashPosition);
  547. if ( newSashPosition != m_sashPosition )
  548. {
  549. DoSetSashPosition(newSashPosition);
  550. }
  551. if ( newSashPosition <= m_sashPosition
  552. && newSashPosition >= m_sashPosition - GetBorderSize() )
  553. {
  554. // don't update it any more
  555. m_requestedSashPosition = INT_MAX;
  556. }
  557. }
  558. int w, h;
  559. GetClientSize(&w, &h);
  560. if ( GetWindow1() && !GetWindow2() )
  561. {
  562. GetWindow1()->SetSize(GetBorderSize(), GetBorderSize(),
  563. w - 2*GetBorderSize(), h - 2*GetBorderSize());
  564. }
  565. else if ( GetWindow1() && GetWindow2() )
  566. {
  567. const int border = GetBorderSize(),
  568. sash = GetSashSize();
  569. int size1 = GetSashPosition() - border,
  570. size2 = GetSashPosition() + sash;
  571. int x2, y2, w1, h1, w2, h2;
  572. if ( GetSplitMode() == wxSPLIT_VERTICAL )
  573. {
  574. w1 = size1;
  575. w2 = w - 2*border - sash - w1;
  576. if (w2 < 0)
  577. w2 = 0;
  578. h2 = h - 2*border;
  579. if (h2 < 0)
  580. h2 = 0;
  581. h1 = h2;
  582. x2 = size2;
  583. y2 = border;
  584. }
  585. else // horz splitter
  586. {
  587. w2 = w - 2*border;
  588. if (w2 < 0)
  589. w2 = 0;
  590. w1 = w2;
  591. h1 = size1;
  592. h2 = h - 2*border - sash - h1;
  593. if (h2 < 0)
  594. h2 = 0;
  595. x2 = border;
  596. y2 = size2;
  597. }
  598. GetWindow2()->SetSize(x2, y2, w2, h2);
  599. GetWindow1()->SetSize(border, border, w1, h1);
  600. }
  601. wxClientDC dc(this);
  602. DrawSash(dc);
  603. }
  604. // Set pane for unsplit window
  605. void wxSplitterWindow::Initialize(wxWindow *window)
  606. {
  607. wxASSERT_MSG( (!window || window->GetParent() == this),
  608. wxT("windows in the splitter should have it as parent!") );
  609. if (window && !window->IsShown())
  610. window->Show();
  611. m_windowOne = window;
  612. m_windowTwo = NULL;
  613. DoSetSashPosition(0);
  614. }
  615. // Associates the given window with window 2, drawing the appropriate sash
  616. // and changing the split mode.
  617. // Does nothing and returns false if the window is already split.
  618. bool wxSplitterWindow::DoSplit(wxSplitMode mode,
  619. wxWindow *window1, wxWindow *window2,
  620. int sashPosition)
  621. {
  622. if ( IsSplit() )
  623. return false;
  624. wxCHECK_MSG( window1 && window2, false,
  625. wxT("cannot split with NULL window(s)") );
  626. wxCHECK_MSG( window1->GetParent() == this && window2->GetParent() == this, false,
  627. wxT("windows in the splitter should have it as parent!") );
  628. if (! window1->IsShown())
  629. window1->Show();
  630. if (! window2->IsShown())
  631. window2->Show();
  632. m_splitMode = mode;
  633. m_windowOne = window1;
  634. m_windowTwo = window2;
  635. SetSashPosition(sashPosition, true);
  636. return true;
  637. }
  638. int wxSplitterWindow::ConvertSashPosition(int sashPosition) const
  639. {
  640. if ( sashPosition > 0 )
  641. {
  642. return sashPosition;
  643. }
  644. else if ( sashPosition < 0 )
  645. {
  646. // It's negative so adding is subtracting
  647. return GetWindowSize() + sashPosition;
  648. }
  649. else // sashPosition == 0
  650. {
  651. // default, put it in the centre
  652. return GetWindowSize() / 2;
  653. }
  654. }
  655. // Remove the specified (or second) window from the view
  656. // Doesn't actually delete the window.
  657. bool wxSplitterWindow::Unsplit(wxWindow *toRemove)
  658. {
  659. if ( ! IsSplit() )
  660. return false;
  661. wxWindow *win;
  662. if ( toRemove == NULL || toRemove == m_windowTwo)
  663. {
  664. win = m_windowTwo ;
  665. m_windowTwo = NULL;
  666. }
  667. else if ( toRemove == m_windowOne )
  668. {
  669. win = m_windowOne ;
  670. m_windowOne = m_windowTwo;
  671. m_windowTwo = NULL;
  672. }
  673. else
  674. {
  675. wxFAIL_MSG(wxT("splitter: attempt to remove a non-existent window"));
  676. return false;
  677. }
  678. OnUnsplit(win);
  679. DoSetSashPosition(0);
  680. SizeWindows();
  681. return true;
  682. }
  683. // Replace a window with another one
  684. bool wxSplitterWindow::ReplaceWindow(wxWindow *winOld, wxWindow *winNew)
  685. {
  686. wxCHECK_MSG( winOld, false, wxT("use one of Split() functions instead") );
  687. wxCHECK_MSG( winNew, false, wxT("use Unsplit() functions instead") );
  688. if ( winOld == m_windowTwo )
  689. {
  690. m_windowTwo = winNew;
  691. }
  692. else if ( winOld == m_windowOne )
  693. {
  694. m_windowOne = winNew;
  695. }
  696. else
  697. {
  698. wxFAIL_MSG(wxT("splitter: attempt to replace a non-existent window"));
  699. return false;
  700. }
  701. SizeWindows();
  702. return true;
  703. }
  704. void wxSplitterWindow::SetMinimumPaneSize(int min)
  705. {
  706. m_minimumPaneSize = min;
  707. int pos = m_requestedSashPosition != INT_MAX ? m_requestedSashPosition : m_sashPosition;
  708. SetSashPosition(pos); // re-check limits
  709. }
  710. void wxSplitterWindow::SetSashPosition(int position, bool redraw)
  711. {
  712. // remember the sash position we want to set for later if we can't set it
  713. // right now (e.g. because the window is too small)
  714. m_requestedSashPosition = position;
  715. DoSetSashPosition(ConvertSashPosition(position));
  716. if ( redraw )
  717. {
  718. SizeWindows();
  719. }
  720. }
  721. // Make sure the child window sizes are updated. This is useful
  722. // for reducing flicker by updating the sizes before a
  723. // window is shown, if you know the overall size is correct.
  724. void wxSplitterWindow::UpdateSize()
  725. {
  726. SizeWindows();
  727. }
  728. bool wxSplitterWindow::DoSendEvent(wxSplitterEvent& event)
  729. {
  730. return !GetEventHandler()->ProcessEvent(event) || event.IsAllowed();
  731. }
  732. wxSize wxSplitterWindow::DoGetBestSize() const
  733. {
  734. // get best sizes of subwindows
  735. wxSize size1, size2;
  736. if ( m_windowOne )
  737. size1 = m_windowOne->GetEffectiveMinSize();
  738. if ( m_windowTwo )
  739. size2 = m_windowTwo->GetEffectiveMinSize();
  740. // sum them
  741. //
  742. // pSash points to the size component to which sash size must be added
  743. int *pSash;
  744. wxSize sizeBest;
  745. if ( m_splitMode == wxSPLIT_VERTICAL )
  746. {
  747. sizeBest.y = wxMax(size1.y, size2.y);
  748. sizeBest.x = wxMax(size1.x, m_minimumPaneSize) +
  749. wxMax(size2.x, m_minimumPaneSize);
  750. pSash = &sizeBest.x;
  751. }
  752. else // wxSPLIT_HORIZONTAL
  753. {
  754. sizeBest.x = wxMax(size1.x, size2.x);
  755. sizeBest.y = wxMax(size1.y, m_minimumPaneSize) +
  756. wxMax(size2.y, m_minimumPaneSize);
  757. pSash = &sizeBest.y;
  758. }
  759. // account for the sash if the window is actually split
  760. if ( m_windowOne && m_windowTwo )
  761. *pSash += GetSashSize();
  762. // account for the border too
  763. int border = 2*GetBorderSize();
  764. sizeBest.x += border;
  765. sizeBest.y += border;
  766. return sizeBest;
  767. }
  768. // ---------------------------------------------------------------------------
  769. // wxSplitterWindow virtual functions: they now just generate the events
  770. // ---------------------------------------------------------------------------
  771. bool wxSplitterWindow::OnSashPositionChange(int WXUNUSED(newSashPosition))
  772. {
  773. // always allow by default
  774. return true;
  775. }
  776. int wxSplitterWindow::OnSashPositionChanging(int newSashPosition)
  777. {
  778. // If within UNSPLIT_THRESHOLD from edge, set to edge to cause closure.
  779. const int UNSPLIT_THRESHOLD = 4;
  780. // first of all, check if OnSashPositionChange() doesn't forbid this change
  781. if ( !OnSashPositionChange(newSashPosition) )
  782. {
  783. // it does
  784. return -1;
  785. }
  786. // Obtain relevant window dimension for bottom / right threshold check
  787. int window_size = GetWindowSize();
  788. bool unsplit_scenario = false;
  789. if ( m_permitUnsplitAlways || m_minimumPaneSize == 0 )
  790. {
  791. // Do edge detection if unsplit premitted
  792. if ( newSashPosition <= UNSPLIT_THRESHOLD )
  793. {
  794. // threshold top / left check
  795. newSashPosition = 0;
  796. unsplit_scenario = true;
  797. }
  798. if ( newSashPosition >= window_size - UNSPLIT_THRESHOLD )
  799. {
  800. // threshold bottom/right check
  801. newSashPosition = window_size;
  802. unsplit_scenario = true;
  803. }
  804. }
  805. if ( !unsplit_scenario )
  806. {
  807. // If resultant pane would be too small, enlarge it
  808. newSashPosition = AdjustSashPosition(newSashPosition);
  809. // If the result is out of bounds it means minimum size is too big,
  810. // so split window in half as best compromise.
  811. if ( newSashPosition < 0 || newSashPosition > window_size )
  812. newSashPosition = window_size / 2;
  813. }
  814. // now let the event handler have it
  815. //
  816. // FIXME: shouldn't we do it before the adjustments above so as to ensure
  817. // that the sash position is always reasonable?
  818. wxSplitterEvent event(wxEVT_SPLITTER_SASH_POS_CHANGING, this);
  819. event.m_data.pos = newSashPosition;
  820. if ( !DoSendEvent(event) )
  821. {
  822. // the event handler vetoed the change
  823. newSashPosition = -1;
  824. }
  825. else
  826. {
  827. // it could have been changed by it
  828. newSashPosition = event.GetSashPosition();
  829. }
  830. return newSashPosition;
  831. }
  832. // Called when the sash is double-clicked. The default behaviour is to remove
  833. // the sash if the minimum pane size is zero.
  834. void wxSplitterWindow::OnDoubleClickSash(int x, int y)
  835. {
  836. wxCHECK_RET(m_windowTwo, wxT("splitter: no window to remove"));
  837. // new code should handle events instead of using the virtual functions
  838. wxSplitterEvent event(wxEVT_SPLITTER_DOUBLECLICKED, this);
  839. event.m_data.pt.x = x;
  840. event.m_data.pt.y = y;
  841. if ( DoSendEvent(event) )
  842. {
  843. if ( GetMinimumPaneSize() == 0 || m_permitUnsplitAlways )
  844. {
  845. wxWindow* win = m_windowTwo;
  846. if ( Unsplit(win) )
  847. {
  848. wxSplitterEvent unsplitEvent(wxEVT_SPLITTER_UNSPLIT, this);
  849. unsplitEvent.m_data.win = win;
  850. (void)DoSendEvent(unsplitEvent);
  851. }
  852. }
  853. }
  854. //else: blocked by user
  855. }
  856. void wxSplitterWindow::OnUnsplit(wxWindow *winRemoved)
  857. {
  858. // call this before calling the event handler which may delete the window
  859. winRemoved->Show(false);
  860. }
  861. #if defined( __WXMSW__ ) || defined( __WXMAC__)
  862. // this is currently called (and needed) under MSW only...
  863. void wxSplitterWindow::OnSetCursor(wxSetCursorEvent& event)
  864. {
  865. // if we don't do it, the resizing cursor might be set for child window:
  866. // and like this we explicitly say that our cursor should not be used for
  867. // children windows which overlap us
  868. if ( SashHitTest(event.GetX(), event.GetY()) )
  869. {
  870. // default processing is ok
  871. event.Skip();
  872. }
  873. //else: do nothing, in particular, don't call Skip()
  874. }
  875. #endif // wxMSW || wxMac
  876. #endif // wxUSE_SPLITTER