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_PAINT(wxRemotelyScrolledTreeCtrl::OnPaint
)
63 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
64 EVT_TREE_ITEM_COLLAPSED(-1, 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(int pixelsPerUnitX
, int pixelsPerUnitY
,
108 int noUnitsX
, int noUnitsY
,
112 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
113 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
115 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
116 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, pixelsPerUnitY
, noUnitsX
, 0, xPos
, 0, /* noRefresh */ TRUE
);
118 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
121 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
127 // In case we're using the generic tree control.
128 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
130 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
132 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
133 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
135 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
137 if (orient
== wxHORIZONTAL
)
138 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
141 return scrolledWindow
->GetScrollPos(orient
);
149 // In case we're using the generic tree control.
150 // Get the view start
151 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
153 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
155 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
156 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
159 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
161 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
166 scrolledWindow
->GetViewStart(& x2
, & y2
);
172 // x is wrong since the horizontal scrollbar is controlled by the
173 // tree control, but we probably don't need it.
174 scrolledWindow
->GetViewStart(x
, y
);
178 // In case we're using the generic tree control.
179 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
181 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
182 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
184 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
186 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
189 GetViewStart(& startX
, & startY
);
191 int xppu1
, yppu1
, xppu2
, yppu2
;
192 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
193 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
195 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
196 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
201 // Scroll to the given line (in scroll units where each unit is
202 // the height of an item)
203 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
206 #if USE_GENERIC_TREECTRL
207 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
210 UINT sbCode
= SB_THUMBPOSITION
;
211 HWND vertScrollBar
= 0;
212 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
214 #if USE_GENERIC_TREECTRL
218 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
220 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
222 /* Doesn't work yet because scrolling is ignored by Scroll
224 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
227 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
228 win->Scroll(-1, posVert*yppu);
235 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
238 AdjustRemoteScrollbars();
242 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
244 AdjustRemoteScrollbars();
247 // If we don't have this, we get some bits of lines still remaining
248 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
252 if (m_companionWindow
)
253 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
256 void wxRemotelyScrolledTreeCtrl::OnPaint(wxPaintEvent
& event
)
260 wxTreeCtrl::OnPaint(event
);
262 if (! m_drawRowLines
)
265 // Reset the device origin since it may have been set
266 dc
.SetDeviceOrigin(0, 0);
268 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
270 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
272 wxSize clientSize
= GetClientSize();
275 wxTreeItemId h
, lastH
;
276 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
278 if (GetBoundingRect(h
, itemRect
))
280 cy
= itemRect
.GetTop();
281 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
285 if (lastH
.IsOk() && GetBoundingRect(lastH
, itemRect
))
287 cy
= itemRect
.GetBottom();
288 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
293 // Adjust the containing wxScrolledWindow's scrollbars appropriately
294 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
296 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
297 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
299 // This is for the generic tree control.
300 // It calls SetScrollbars which has been overridden
301 // to adjust the parent scrolled window vertical
303 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
309 // This is for the wxMSW tree control
310 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
314 if (GetBoundingRect(GetFirstVisibleItem(), itemRect
))
316 // Actually, the real height seems to be 1 less than reported
317 // (e.g. 16 instead of 16)
318 int itemHeight
= itemRect
.GetHeight() - 1;
321 GetClientSize(&w
, &h
);
323 wxRect
rect(0, 0, 0, 0);
326 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
327 int treeViewHeight
= (int) ceil(f
);
329 int scrollPixelsPerLine
= itemHeight
;
330 int scrollPos
= - (itemRect
.y
/ itemHeight
);
332 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
334 // Ensure that when a scrollbar becomes hidden or visible,
335 // the contained window sizes are right.
336 // Problem: this is called too early (?)
337 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
338 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
345 // Calculate the area that contains both rectangles
346 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
350 int right1
= rect1
.GetRight();
351 int bottom1
= rect1
.GetBottom();
352 int right2
= rect2
.GetRight();
353 int bottom2
= rect2
.GetBottom();
355 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
356 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
358 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
359 rect
.SetRight(bottomRight
.x
);
360 rect
.SetBottom(bottomRight
.y
);
366 // Calculate the tree overall size so we can set the scrollbar
368 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
370 CalcTreeSize(GetRootItem(), rect
);
373 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
375 // More efficient implementation would be to find the last item (but how?)
376 // Q: is the bounding rect relative to the top of the virtual tree workspace
377 // or the top of the window? How would we convert?
379 if (GetBoundingRect(id
, itemSize
))
381 rect
= CombineRectangles(rect
, itemSize
);
385 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
388 CalcTreeSize(childId
, rect
);
389 childId
= GetNextChild(childId
, cookie
);
393 // Find the scrolled window that contains this control
394 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
396 wxWindow
* parent
= wxWindow::GetParent();
399 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
400 return (wxScrolledWindow
*) parent
;
401 parent
= parent
->GetParent();
406 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
408 int orient
= event
.GetOrientation();
409 if (orient
== wxHORIZONTAL
)
414 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
419 scrollWin
->GetViewStart(& x
, & y
);
425 * wxTreeCompanionWindow
427 * A window displaying values associated with tree control items.
430 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
432 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
433 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
434 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
435 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
436 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
439 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
443 wxWindow(parent
, id
, pos
, sz
, style
)
448 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
454 wxString text
= m_treeCtrl
->GetItemText(id
);
455 dc
.SetTextForeground(* wxBLACK
);
456 dc
.SetBackgroundMode(wxTRANSPARENT
);
459 dc
.GetTextExtent(text
, & textW
, & textH
);
462 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
464 dc
.DrawText(text
, x
, y
);
469 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
476 wxPen
pen(wxSystemSettings::GetColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
478 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
479 wxFont
font(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
482 wxSize clientSize
= GetClientSize();
485 wxTreeItemId h
, lastH
;
486 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
488 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
490 cy
= itemRect
.GetTop();
491 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
495 // Draw the actual item
496 DrawItem(dc
, h
, drawItemRect
);
497 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
500 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
502 cy
= itemRect
.GetBottom();
503 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
507 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
509 int orient
= event
.GetOrientation();
510 if (orient
== wxHORIZONTAL
)
518 // TODO: scroll the window physically instead of just refreshing.
522 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
524 // TODO: something more optimized than simply refresh the whole
525 // window when the tree is expanded/collapsed. Tricky.
530 * wxThinSplitterWindow
533 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
535 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
536 EVT_SIZE(wxThinSplitterWindow::OnSize
)
539 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
543 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
547 void wxThinSplitterWindow::SizeWindows()
549 // The client size may have changed inbetween
550 // the sizing of the first window and the sizing of
551 // the second. So repeat SizeWindows.
552 wxSplitterWindow::SizeWindows();
553 wxSplitterWindow::SizeWindows();
556 // Tests for x, y over sash
557 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
559 return wxSplitterWindow::SashHitTest(x
, y
, 4);
562 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
564 if ( m_sashPosition
== 0 || !m_windowTwo
)
566 if (GetWindowStyle() & wxSP_NOSASH
)
570 GetClientSize(&w
, &h
);
572 if ( m_splitMode
== wxSPLIT_VERTICAL
)
574 dc
.SetPen(* m_facePen
);
575 dc
.SetBrush(* m_faceBrush
);
578 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
579 h1
+= 1; // Not sure why this is necessary...
580 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
584 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
588 dc
.SetPen(* m_facePen
);
589 dc
.SetBrush(* m_faceBrush
);
592 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
594 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
598 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
601 dc
.SetPen(wxNullPen
);
602 dc
.SetBrush(wxNullBrush
);
605 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
607 wxSplitterWindow::OnSize(event
);
611 * wxSplitterScrolledWindow
614 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
616 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
617 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
618 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
621 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
625 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
629 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
631 wxSize sz
= GetClientSize();
632 if (GetChildren().GetFirst())
634 ((wxWindow
*) GetChildren().GetFirst()->GetData())->SetSize(0, 0, sz
.x
, sz
.y
);
638 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
640 // Ensure that events being propagated back up the window hierarchy
641 // don't cause an infinite loop
642 static bool inOnScroll
= FALSE
;
650 int orient
= event
.GetOrientation();
652 int nScrollInc
= CalcScrollInc(event
);
659 if (orient
== wxHORIZONTAL
)
665 int newPos
= m_xScrollPosition
+ nScrollInc
;
666 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
671 int newPos
= m_yScrollPosition
+ nScrollInc
;
672 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
675 if (orient
== wxHORIZONTAL
)
677 m_xScrollPosition
+= nScrollInc
;
681 m_yScrollPosition
+= nScrollInc
;
684 // Find targets in splitter window and send the event to them
685 wxWindowListNode
* node
= GetChildren().GetFirst();
688 wxWindow
* child
= (wxWindow
*) node
->GetData();
689 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
691 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
692 if (splitter
->GetWindow1())
693 splitter
->GetWindow1()->ProcessEvent(event
);
694 if (splitter
->GetWindow2())
695 splitter
->GetWindow2()->ProcessEvent(event
);
698 node
= node
->GetNext();
702 m_targetWindow
->MacUpdateImmediately() ;