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 // ----------------------------------------------------------------------------
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 void wxThinSplitterWindow::SizeWindows()
500 // The client size may have changed inbetween
501 // the sizing of the first window and the sizing of
502 // the second. So repeat SizeWindows.
503 wxSplitterWindow::SizeWindows();
504 wxSplitterWindow::SizeWindows();
507 // Tests for x, y over sash
508 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
510 return wxSplitterWindow::SashHitTest(x
, y
, 4);
513 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
515 if ( m_sashPosition
== 0 || !m_windowTwo
)
517 if (GetWindowStyle() & wxSP_NOSASH
)
521 GetClientSize(&w
, &h
);
523 if ( m_splitMode
== wxSPLIT_VERTICAL
)
525 dc
.SetPen(* m_facePen
);
526 dc
.SetBrush(* m_faceBrush
);
529 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
530 h1
+= 1; // Not sure why this is necessary...
531 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
535 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
539 dc
.SetPen(* m_facePen
);
540 dc
.SetBrush(* m_faceBrush
);
543 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
545 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
549 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
552 dc
.SetPen(wxNullPen
);
553 dc
.SetBrush(wxNullBrush
);
556 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
558 wxSplitterWindow::OnSize(event
);
562 * wxSplitterScrolledWindow
565 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
567 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
568 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
569 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
572 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
576 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
580 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
582 wxSize sz
= GetClientSize();
583 if (GetChildren().First())
585 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
589 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
591 // Ensure that events being propagated back up the window hierarchy
592 // don't cause an infinite loop
593 static bool inOnScroll
= FALSE
;
601 int orient
= event
.GetOrientation();
603 int nScrollInc
= CalcScrollInc(event
);
610 if (orient
== wxHORIZONTAL
)
616 int newPos
= m_xScrollPosition
+ nScrollInc
;
617 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
622 int newPos
= m_yScrollPosition
+ nScrollInc
;
623 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
626 if (orient
== wxHORIZONTAL
)
628 m_xScrollPosition
+= nScrollInc
;
632 m_yScrollPosition
+= nScrollInc
;
635 // Find targets in splitter window and send the event to them
636 wxNode
* node
= GetChildren().First();
639 wxWindow
* child
= (wxWindow
*) node
->Data();
640 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
642 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
643 if (splitter
->GetWindow1())
644 splitter
->GetWindow1()->ProcessEvent(event
);
645 if (splitter
->GetWindow2())
646 splitter
->GetWindow2()->ProcessEvent(event
);
653 m_targetWindow
->MacUpdateImmediately() ;