1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Classes to achieve a remotely-scrolled tree in a splitter
4 // window that can be scrolled by a scrolled window higher in the
6 // Author: Julian Smart
10 // Copyright: (c) Julian Smart
11 // Licence: wxWindows licence
12 /////////////////////////////////////////////////////////////////////////////
14 // ============================================================================
16 // ============================================================================
18 // ----------------------------------------------------------------------------
20 // ----------------------------------------------------------------------------
21 #if defined(__GNUG__) && !defined(__APPLE__)
22 #pragma implementation "splittree.h"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers)
40 #include "wx/msw/winundef.h"
43 #include "wx/gizmos/splittree.h"
47 * wxRemotelyScrolledTreeCtrl
50 #if USE_GENERIC_TREECTRL
51 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
53 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
56 #if USE_GENERIC_TREECTRL
57 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
59 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
61 EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize
)
62 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
63 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
64 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
67 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
68 const wxSize
& sz
, long style
):
69 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
71 m_companionWindow
= NULL
;
74 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
78 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
80 #if defined(__WXMSW__) && USE_GENERIC_TREECTRL
81 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
83 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
88 // Implicit in overriding SetScrollbars
92 // Number of pixels per user unit (0 or -1 for no scrollbar)
93 // Length of virtual canvas in user units
94 // Length of page in user units
95 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
96 int noUnitsX
, int noUnitsY
,
100 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
101 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
103 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
104 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
106 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
109 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
115 // In case we're using the generic tree control.
116 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
118 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
120 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
121 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
123 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
125 if (orient
== wxHORIZONTAL
)
126 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
129 return scrolledWindow
->GetScrollPos(orient
);
137 // In case we're using the generic tree control.
138 // Get the view start
139 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
141 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
143 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
144 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
147 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
149 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
154 scrolledWindow
->GetViewStart(& x2
, & y2
);
160 // x is wrong since the horizontal scrollbar is controlled by the
161 // tree control, but we probably don't need it.
162 scrolledWindow
->GetViewStart(x
, y
);
166 // In case we're using the generic tree control.
167 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
169 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
170 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
172 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
174 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
177 GetViewStart(& startX
, & startY
);
179 int xppu1
, yppu1
, xppu2
, yppu2
;
180 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
181 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
183 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
184 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
189 // Scroll to the given line (in scroll units where each unit is
190 // the height of an item)
191 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
194 #if USE_GENERIC_TREECTRL
195 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
198 UINT sbCode
= SB_THUMBPOSITION
;
199 HWND vertScrollBar
= 0;
200 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
202 #if USE_GENERIC_TREECTRL
206 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
208 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
210 /* Doesn't work yet because scrolling is ignored by Scroll
212 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
215 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
216 win->Scroll(-1, posVert*yppu);
223 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
226 AdjustRemoteScrollbars();
230 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
232 AdjustRemoteScrollbars();
235 // If we don't have this, we get some bits of lines still remaining
236 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
240 if (m_companionWindow
)
241 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
244 // Adjust the containing wxScrolledWindow's scrollbars appropriately
245 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
247 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
248 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
250 // This is for the generic tree control.
251 // It calls SetScrollbars which has been overridden
252 // to adjust the parent scrolled window vertical
254 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
260 // This is for the wxMSW tree control
261 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
265 if (GetBoundingRect(GetRootItem(), itemRect
))
267 // Actually, the real height seems to be 1 less than reported
268 // (e.g. 16 instead of 16)
269 int itemHeight
= itemRect
.GetHeight() - 1;
272 GetClientSize(&w
, &h
);
274 wxRect
rect(0, 0, 0, 0);
277 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
278 int treeViewHeight
= (int) ceil(f
);
280 int scrollPixelsPerLine
= itemHeight
;
281 int scrollPos
= - (itemRect
.y
/ itemHeight
);
283 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
285 // Ensure that when a scrollbar becomes hidden or visible,
286 // the contained window sizes are right.
287 // Problem: this is called too early (?)
288 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
289 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
296 // Calculate the area that contains both rectangles
297 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
301 int right1
= rect1
.GetRight();
302 int bottom1
= rect1
.GetBottom();
303 int right2
= rect2
.GetRight();
304 int bottom2
= rect2
.GetBottom();
306 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
307 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
309 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
310 rect
.SetRight(bottomRight
.x
);
311 rect
.SetBottom(bottomRight
.y
);
317 // Calculate the tree overall size so we can set the scrollbar
319 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
321 CalcTreeSize(GetRootItem(), rect
);
324 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
326 // More efficient implementation would be to find the last item (but how?)
327 // Q: is the bounding rect relative to the top of the virtual tree workspace
328 // or the top of the window? How would we convert?
330 if (GetBoundingRect(id
, itemSize
))
332 rect
= CombineRectangles(rect
, itemSize
);
336 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
339 CalcTreeSize(childId
, rect
);
340 childId
= GetNextChild(childId
, cookie
);
344 // Find the scrolled window that contains this control
345 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
347 wxWindow
* parent
= wxWindow::GetParent();
350 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
351 return (wxScrolledWindow
*) parent
;
352 parent
= parent
->GetParent();
357 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
359 int orient
= event
.GetOrientation();
360 if (orient
== wxHORIZONTAL
)
365 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
370 scrollWin
->GetViewStart(& x
, & y
);
376 * wxTreeCompanionWindow
378 * A window displaying values associated with tree control items.
381 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
383 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
384 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
385 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
386 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
387 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
390 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
394 wxWindow(parent
, id
, pos
, sz
, style
)
399 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
405 wxString text
= m_treeCtrl
->GetItemText(id
);
406 dc
.SetTextForeground(* wxBLACK
);
407 dc
.SetBackgroundMode(wxTRANSPARENT
);
410 dc
.GetTextExtent(text
, & textW
, & textH
);
413 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
415 dc
.DrawText(text
, x
, y
);
420 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
427 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
429 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
430 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
433 wxSize clientSize
= GetClientSize();
436 wxTreeItemId h
, lastH
;
437 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
439 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
441 cy
= itemRect
.GetTop();
442 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
446 // Draw the actual item
447 DrawItem(dc
, h
, drawItemRect
);
448 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
451 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
453 cy
= itemRect
.GetBottom();
454 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
458 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
460 int orient
= event
.GetOrientation();
461 if (orient
== wxHORIZONTAL
)
469 // TODO: scroll the window physically instead of just refreshing.
473 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
475 // TODO: something more optimized than simply refresh the whole
476 // window when the tree is expanded/collapsed. Tricky.
481 * wxThinSplitterWindow
484 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
486 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
487 EVT_SIZE(wxThinSplitterWindow::OnSize
)
490 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
494 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
498 wxThinSplitterWindow::~wxThinSplitterWindow()
502 void wxThinSplitterWindow::SizeWindows()
504 // The client size may have changed inbetween
505 // the sizing of the first window and the sizing of
506 // the second. So repeat SizeWindows.
507 wxSplitterWindow::SizeWindows();
508 wxSplitterWindow::SizeWindows();
511 // Tests for x, y over sash
512 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
514 return wxSplitterWindow::SashHitTest(x
, y
, 4);
517 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
519 if ( GetSashPosition() == 0 || !m_windowTwo
)
521 if (GetWindowStyle() & wxSP_NOSASH
)
525 GetClientSize(&w
, &h
);
527 if ( m_splitMode
== wxSPLIT_VERTICAL
)
529 dc
.SetPen(* m_facePen
);
530 dc
.SetBrush(* m_faceBrush
);
533 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
534 h1
+= 1; // Not sure why this is necessary...
535 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
539 dc
.DrawRectangle(GetSashPosition(), y1
, GetSashSize(), h1
);
543 dc
.SetPen(* m_facePen
);
544 dc
.SetBrush(* m_faceBrush
);
547 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
549 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
553 dc
.DrawRectangle(x1
, GetSashPosition(), w1
, GetSashSize());
556 dc
.SetPen(wxNullPen
);
557 dc
.SetBrush(wxNullBrush
);
560 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
562 wxSplitterWindow::OnSize(event
);
566 * wxSplitterScrolledWindow
569 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
571 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
572 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
573 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
576 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
580 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
584 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
586 wxSize sz
= GetClientSize();
587 if (GetChildren().First())
589 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
593 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
595 // Ensure that events being propagated back up the window hierarchy
596 // don't cause an infinite loop
597 static bool inOnScroll
= FALSE
;
605 int orient
= event
.GetOrientation();
607 int nScrollInc
= CalcScrollInc(event
);
614 if (orient
== wxHORIZONTAL
)
620 int newPos
= m_xScrollPosition
+ nScrollInc
;
621 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
626 int newPos
= m_yScrollPosition
+ nScrollInc
;
627 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
630 if (orient
== wxHORIZONTAL
)
632 m_xScrollPosition
+= nScrollInc
;
636 m_yScrollPosition
+= nScrollInc
;
639 // Find targets in splitter window and send the event to them
640 wxNode
* node
= GetChildren().First();
643 wxWindow
* child
= (wxWindow
*) node
->Data();
644 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
646 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
647 if (splitter
->GetWindow1())
648 splitter
->GetWindow1()->ProcessEvent(event
);
649 if (splitter
->GetWindow2())
650 splitter
->GetWindow2()->ProcessEvent(event
);
657 m_targetWindow
->MacUpdateImmediately() ;