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" wxWidgets 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_PAINT(wxRemotelyScrolledTreeCtrl::OnPaint
)
63 EVT_TREE_ITEM_EXPANDED(wxID_ANY
, wxRemotelyScrolledTreeCtrl::OnExpand
)
64 EVT_TREE_ITEM_COLLAPSED(wxID_ANY
, wxRemotelyScrolledTreeCtrl::OnExpand
)
65 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
68 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(
69 wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
70 const wxSize
& sz
, long style
)
71 : wxTreeCtrl(parent
, id
, pt
, sz
, style
& ~wxTR_ROW_LINES
)
73 m_companionWindow
= NULL
;
75 // We draw the row lines ourself so they match what's done
76 // by the companion window. That is why the flag is turned
77 // off above, so wxGenericTreeCtrl doesn't draw them in a
79 m_drawRowLines
= (style
& wxTR_ROW_LINES
) != 0;
82 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
86 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
88 #if defined(__WXMSW__)
89 #if USE_GENERIC_TREECTRL
90 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
93 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, false);
95 #if USE_GENERIC_TREECTRL
98 // Implicit in overriding SetScrollbars
104 // Number of pixels per user unit (0 or -1 for no scrollbar)
105 // Length of virtual canvas in user units
106 // Length of page in user units
107 void wxRemotelyScrolledTreeCtrl::SetScrollbars(
108 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
109 int pixelsPerUnitX
, int pixelsPerUnitY
,
110 int noUnitsX
, int noUnitsY
,
114 int WXUNUSED(pixelsPerUnitX
), int WXUNUSED(pixelsPerUnitY
),
115 int WXUNUSED(noUnitsX
), int WXUNUSED(noUnitsY
),
116 int WXUNUSED(xPos
), int WXUNUSED(yPos
),
117 bool WXUNUSED(noRefresh
)
121 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
122 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
124 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
125 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, pixelsPerUnitY
, noUnitsX
, 0, xPos
, 0, /* noRefresh */ true);
127 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
130 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
136 // In case we're using the generic tree control.
137 int wxRemotelyScrolledTreeCtrl::GetScrollPos(
138 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
146 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
147 // this condition fixes extsitence of warning but
148 wxScrolledWindow
* scrolledWindow
=
149 // but GetScrolledWindow is still executed in case internally does something
153 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
154 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
156 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
158 if (orient
== wxHORIZONTAL
)
159 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
162 return scrolledWindow
->GetScrollPos(orient
);
170 // In case we're using the generic tree control.
171 // Get the view start
172 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
174 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
176 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
177 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
180 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
182 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
187 scrolledWindow
->GetViewStart(& x2
, & y2
);
193 // x is wrong since the horizontal scrollbar is controlled by the
194 // tree control, but we probably don't need it.
195 scrolledWindow
->GetViewStart(x
, y
);
199 // In case we're using the generic tree control.
200 void wxRemotelyScrolledTreeCtrl::PrepareDC(
201 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
208 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
209 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
211 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
213 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
216 GetViewStart(& startX
, & startY
);
218 int xppu1
, yppu1
, xppu2
, yppu2
;
219 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
220 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
222 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
223 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
228 // Scroll to the given line (in scroll units where each unit is
229 // the height of an item)
230 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int WXUNUSED(posHoriz
), int posVert
)
233 #if USE_GENERIC_TREECTRL
234 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
237 UINT sbCode
= SB_THUMBPOSITION
;
238 HWND vertScrollBar
= 0;
239 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXLPARAM
) vertScrollBar
);
241 #if USE_GENERIC_TREECTRL
245 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
247 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
249 /* Doesn't work yet because scrolling is ignored by Scroll
251 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
254 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
255 win->Scroll(-1, posVert*yppu);
262 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
265 AdjustRemoteScrollbars();
269 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
271 AdjustRemoteScrollbars();
274 // If we don't have this, we get some bits of lines still remaining
275 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
279 if (m_companionWindow
)
280 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
283 void wxRemotelyScrolledTreeCtrl::OnPaint(wxPaintEvent
& event
)
287 wxTreeCtrl::OnPaint(event
);
289 if (! m_drawRowLines
)
292 // Reset the device origin since it may have been set
293 dc
.SetDeviceOrigin(0, 0);
295 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
297 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
299 wxSize clientSize
= GetClientSize();
301 wxTreeItemId h
, lastH
;
302 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
304 if (GetBoundingRect(h
, itemRect
))
306 int cy
= itemRect
.GetTop();
307 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
311 if (lastH
.IsOk() && GetBoundingRect(lastH
, itemRect
))
313 int cy
= itemRect
.GetBottom();
314 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
319 // Adjust the containing wxScrolledWindow's scrollbars appropriately
320 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
322 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
323 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
325 // This is for the generic tree control.
326 // It calls SetScrollbars which has been overridden
327 // to adjust the parent scrolled window vertical
329 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
335 // This is for the wxMSW tree control
336 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
340 if (GetBoundingRect(GetFirstVisibleItem(), itemRect
))
342 // Actually, the real height seems to be 1 less than reported
343 // (e.g. 16 instead of 16)
344 int itemHeight
= itemRect
.GetHeight() - 1;
347 GetClientSize(&w
, &h
);
349 wxRect
rect(0, 0, 0, 0);
352 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
353 int treeViewHeight
= (int) ceil(f
);
355 int scrollPixelsPerLine
= itemHeight
;
356 int scrollPos
= - (itemRect
.y
/ itemHeight
);
358 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
360 // Ensure that when a scrollbar becomes hidden or visible,
361 // the contained window sizes are right.
362 // Problem: this is called too early (?)
363 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
364 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
371 // Calculate the area that contains both rectangles
372 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
376 int right1
= rect1
.GetRight();
377 int bottom1
= rect1
.GetBottom();
378 int right2
= rect2
.GetRight();
379 int bottom2
= rect2
.GetBottom();
381 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
382 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
384 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
385 rect
.SetRight(bottomRight
.x
);
386 rect
.SetBottom(bottomRight
.y
);
392 // Calculate the tree overall size so we can set the scrollbar
394 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
396 CalcTreeSize(GetRootItem(), rect
);
399 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
401 // More efficient implementation would be to find the last item (but how?)
402 // Q: is the bounding rect relative to the top of the virtual tree workspace
403 // or the top of the window? How would we convert?
405 if (GetBoundingRect(id
, itemSize
))
407 rect
= CombineRectangles(rect
, itemSize
);
410 wxTreeItemIdValue cookie
;
411 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
414 CalcTreeSize(childId
, rect
);
415 childId
= GetNextChild(childId
, cookie
);
419 // Find the scrolled window that contains this control
420 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
422 wxWindow
* parent
= wxWindow::GetParent();
425 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
426 return (wxScrolledWindow
*) parent
;
427 parent
= parent
->GetParent();
432 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
434 int orient
= event
.GetOrientation();
435 if (orient
== wxHORIZONTAL
)
440 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
445 scrollWin
->GetViewStart(& x
, & y
);
451 * wxTreeCompanionWindow
453 * A window displaying values associated with tree control items.
456 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
458 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
459 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
460 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
461 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
462 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
465 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
469 wxWindow(parent
, id
, pos
, sz
, style
)
474 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
480 wxString text
= m_treeCtrl
->GetItemText(id
);
481 dc
.SetTextForeground(* wxBLACK
);
482 dc
.SetBackgroundMode(wxTRANSPARENT
);
485 dc
.GetTextExtent(text
, & textW
, & textH
);
488 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
490 dc
.DrawText(text
, x
, y
);
495 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& WXUNUSED(event
))
502 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
504 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
505 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
508 wxSize clientSize
= GetClientSize();
510 wxTreeItemId h
, lastH
;
511 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
513 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
515 int cy
= itemRect
.GetTop();
516 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
520 // Draw the actual item
521 DrawItem(dc
, h
, drawItemRect
);
522 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
525 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
527 int cy
= itemRect
.GetBottom();
528 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
532 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
534 int orient
= event
.GetOrientation();
535 if (orient
== wxHORIZONTAL
)
543 // TODO: scroll the window physically instead of just refreshing.
547 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& WXUNUSED(event
))
549 // TODO: something more optimized than simply refresh the whole
550 // window when the tree is expanded/collapsed. Tricky.
555 * wxThinSplitterWindow
558 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
560 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
561 EVT_SIZE(wxThinSplitterWindow::OnSize
)
564 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
568 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
570 wxColour
faceColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
571 m_facePen
= new wxPen(faceColour
, 1, wxSOLID
);
572 m_faceBrush
= new wxBrush(faceColour
, wxSOLID
);
575 wxThinSplitterWindow::~wxThinSplitterWindow()
582 void wxThinSplitterWindow::SizeWindows()
584 // The client size may have changed inbetween
585 // the sizing of the first window and the sizing of
586 // the second. So repeat SizeWindows.
587 wxSplitterWindow::SizeWindows();
588 wxSplitterWindow::SizeWindows();
591 // Tests for x, y over sash
592 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int WXUNUSED(tolerance
))
594 return wxSplitterWindow::SashHitTest(x
, y
, 4);
597 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
599 if ( m_sashPosition
== 0 || !m_windowTwo
)
601 if (GetWindowStyle() & wxSP_NOSASH
)
605 GetClientSize(&w
, &h
);
607 if ( m_splitMode
== wxSPLIT_VERTICAL
)
609 dc
.SetPen(* m_facePen
);
610 dc
.SetBrush(* m_faceBrush
);
613 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
614 h1
+= 1; // Not sure why this is necessary...
615 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
619 dc
.DrawRectangle(m_sashPosition
, y1
, GetSashSize(), h1
);
623 dc
.SetPen(* m_facePen
);
624 dc
.SetBrush(* m_faceBrush
);
627 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
629 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
633 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, GetSashSize());
636 dc
.SetPen(wxNullPen
);
637 dc
.SetBrush(wxNullBrush
);
640 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
642 wxSplitterWindow::OnSize(event
);
646 * wxSplitterScrolledWindow
649 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
651 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
652 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
653 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
656 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
660 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
664 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& WXUNUSED(event
))
666 wxSize sz
= GetClientSize();
667 if (GetChildren().GetFirst())
669 ((wxWindow
*) GetChildren().GetFirst()->GetData())->SetSize(0, 0, sz
.x
, sz
.y
);
673 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
675 // Ensure that events being propagated back up the window hierarchy
676 // don't cause an infinite loop
677 static bool inOnScroll
= false;
685 int orient
= event
.GetOrientation();
687 int nScrollInc
= CalcScrollInc(event
);
694 if (orient
== wxHORIZONTAL
)
700 int newPos
= m_xScrollPosition
+ nScrollInc
;
701 SetScrollPos(wxHORIZONTAL
, newPos
, true );
706 int newPos
= m_yScrollPosition
+ nScrollInc
;
707 SetScrollPos(wxVERTICAL
, newPos
, true );
710 if (orient
== wxHORIZONTAL
)
712 m_xScrollPosition
+= nScrollInc
;
716 m_yScrollPosition
+= nScrollInc
;
719 // Find targets in splitter window and send the event to them
720 wxWindowList::compatibility_iterator node
= GetChildren().GetFirst();
723 wxWindow
* child
= (wxWindow
*) node
->GetData();
724 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
726 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
727 if (splitter
->GetWindow1())
728 splitter
->GetWindow1()->ProcessEvent(event
);
729 if (splitter
->GetWindow2())
730 splitter
->GetWindow2()->ProcessEvent(event
);
733 node
= node
->GetNext();
736 m_targetWindow
->Update() ;