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(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
69 const wxSize
& sz
, long style
):
70 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
72 m_companionWindow
= NULL
;
75 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
79 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
81 #if defined(__WXMSW__)
82 #if USE_GENERIC_TREECTRL
83 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
86 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
88 #if USE_GENERIC_TREECTRL
91 // Implicit in overriding SetScrollbars
97 // Number of pixels per user unit (0 or -1 for no scrollbar)
98 // Length of virtual canvas in user units
99 // Length of page in user units
100 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
101 int noUnitsX
, int noUnitsY
,
105 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
106 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
108 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
109 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
111 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
114 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
120 // In case we're using the generic tree control.
121 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
123 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
125 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
126 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
128 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
130 if (orient
== wxHORIZONTAL
)
131 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
134 return scrolledWindow
->GetScrollPos(orient
);
142 // In case we're using the generic tree control.
143 // Get the view start
144 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
146 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
148 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
149 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
152 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
154 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
159 scrolledWindow
->GetViewStart(& x2
, & y2
);
165 // x is wrong since the horizontal scrollbar is controlled by the
166 // tree control, but we probably don't need it.
167 scrolledWindow
->GetViewStart(x
, y
);
171 // In case we're using the generic tree control.
172 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
174 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
175 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
177 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
179 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
182 GetViewStart(& startX
, & startY
);
184 int xppu1
, yppu1
, xppu2
, yppu2
;
185 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
186 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
188 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
189 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
194 // Scroll to the given line (in scroll units where each unit is
195 // the height of an item)
196 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
199 #if USE_GENERIC_TREECTRL
200 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
203 UINT sbCode
= SB_THUMBPOSITION
;
204 HWND vertScrollBar
= 0;
205 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
207 #if USE_GENERIC_TREECTRL
211 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
213 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
215 /* Doesn't work yet because scrolling is ignored by Scroll
217 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
220 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
221 win->Scroll(-1, posVert*yppu);
228 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
231 AdjustRemoteScrollbars();
235 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
237 AdjustRemoteScrollbars();
240 // If we don't have this, we get some bits of lines still remaining
241 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
245 if (m_companionWindow
)
246 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
249 void wxRemotelyScrolledTreeCtrl::OnPaint(wxPaintEvent
& event
)
253 wxTreeCtrl::OnPaint(event
);
255 if ((GetWindowStyle() & wxTR_ROW_LINES
) == 0)
258 // Reset the device origin since it may have been set
259 dc
.SetDeviceOrigin(0, 0);
261 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
263 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
265 wxSize clientSize
= GetClientSize();
268 wxTreeItemId h
, lastH
;
269 for(h
=GetFirstVisibleItem();h
;h
=GetNextVisible(h
))
271 if (GetBoundingRect(h
, itemRect
))
273 cy
= itemRect
.GetTop();
274 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
278 if (GetBoundingRect(lastH
, itemRect
))
280 cy
= itemRect
.GetBottom();
281 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
286 // Adjust the containing wxScrolledWindow's scrollbars appropriately
287 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
289 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
290 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
292 // This is for the generic tree control.
293 // It calls SetScrollbars which has been overridden
294 // to adjust the parent scrolled window vertical
296 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
302 // This is for the wxMSW tree control
303 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
307 if (GetBoundingRect(GetRootItem(), itemRect
))
309 // Actually, the real height seems to be 1 less than reported
310 // (e.g. 16 instead of 16)
311 int itemHeight
= itemRect
.GetHeight() - 1;
314 GetClientSize(&w
, &h
);
316 wxRect
rect(0, 0, 0, 0);
319 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
320 int treeViewHeight
= (int) ceil(f
);
322 int scrollPixelsPerLine
= itemHeight
;
323 int scrollPos
= - (itemRect
.y
/ itemHeight
);
325 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
327 // Ensure that when a scrollbar becomes hidden or visible,
328 // the contained window sizes are right.
329 // Problem: this is called too early (?)
330 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
331 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
338 // Calculate the area that contains both rectangles
339 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
343 int right1
= rect1
.GetRight();
344 int bottom1
= rect1
.GetBottom();
345 int right2
= rect2
.GetRight();
346 int bottom2
= rect2
.GetBottom();
348 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
349 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
351 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
352 rect
.SetRight(bottomRight
.x
);
353 rect
.SetBottom(bottomRight
.y
);
359 // Calculate the tree overall size so we can set the scrollbar
361 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
363 CalcTreeSize(GetRootItem(), rect
);
366 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
368 // More efficient implementation would be to find the last item (but how?)
369 // Q: is the bounding rect relative to the top of the virtual tree workspace
370 // or the top of the window? How would we convert?
372 if (GetBoundingRect(id
, itemSize
))
374 rect
= CombineRectangles(rect
, itemSize
);
378 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
381 CalcTreeSize(childId
, rect
);
382 childId
= GetNextChild(childId
, cookie
);
386 // Find the scrolled window that contains this control
387 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
389 wxWindow
* parent
= wxWindow::GetParent();
392 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
393 return (wxScrolledWindow
*) parent
;
394 parent
= parent
->GetParent();
399 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
401 int orient
= event
.GetOrientation();
402 if (orient
== wxHORIZONTAL
)
407 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
412 scrollWin
->GetViewStart(& x
, & y
);
418 * wxTreeCompanionWindow
420 * A window displaying values associated with tree control items.
423 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
425 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
426 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
427 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
428 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
429 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
432 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
436 wxWindow(parent
, id
, pos
, sz
, style
)
441 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
447 wxString text
= m_treeCtrl
->GetItemText(id
);
448 dc
.SetTextForeground(* wxBLACK
);
449 dc
.SetBackgroundMode(wxTRANSPARENT
);
452 dc
.GetTextExtent(text
, & textW
, & textH
);
455 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
457 dc
.DrawText(text
, x
, y
);
462 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
469 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
471 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
472 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
475 wxSize clientSize
= GetClientSize();
478 wxTreeItemId h
, lastH
;
479 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
481 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
483 cy
= itemRect
.GetTop();
484 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
488 // Draw the actual item
489 DrawItem(dc
, h
, drawItemRect
);
490 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
493 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
495 cy
= itemRect
.GetBottom();
496 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
500 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
502 int orient
= event
.GetOrientation();
503 if (orient
== wxHORIZONTAL
)
511 // TODO: scroll the window physically instead of just refreshing.
515 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
517 // TODO: something more optimized than simply refresh the whole
518 // window when the tree is expanded/collapsed. Tricky.
523 * wxThinSplitterWindow
526 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
528 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
529 EVT_SIZE(wxThinSplitterWindow::OnSize
)
532 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
536 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
540 void wxThinSplitterWindow::SizeWindows()
542 // The client size may have changed inbetween
543 // the sizing of the first window and the sizing of
544 // the second. So repeat SizeWindows.
545 wxSplitterWindow::SizeWindows();
546 wxSplitterWindow::SizeWindows();
549 // Tests for x, y over sash
550 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
552 return wxSplitterWindow::SashHitTest(x
, y
, 4);
555 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
557 if ( m_sashPosition
== 0 || !m_windowTwo
)
559 if (GetWindowStyle() & wxSP_NOSASH
)
563 GetClientSize(&w
, &h
);
565 if ( m_splitMode
== wxSPLIT_VERTICAL
)
567 dc
.SetPen(* m_facePen
);
568 dc
.SetBrush(* m_faceBrush
);
571 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
572 h1
+= 1; // Not sure why this is necessary...
573 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
577 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
581 dc
.SetPen(* m_facePen
);
582 dc
.SetBrush(* m_faceBrush
);
585 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
587 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
591 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
594 dc
.SetPen(wxNullPen
);
595 dc
.SetBrush(wxNullBrush
);
598 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
600 wxSplitterWindow::OnSize(event
);
604 * wxSplitterScrolledWindow
607 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
609 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
610 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
611 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
614 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
618 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
622 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
624 wxSize sz
= GetClientSize();
625 if (GetChildren().First())
627 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
631 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
633 // Ensure that events being propagated back up the window hierarchy
634 // don't cause an infinite loop
635 static bool inOnScroll
= FALSE
;
643 int orient
= event
.GetOrientation();
645 int nScrollInc
= CalcScrollInc(event
);
652 if (orient
== wxHORIZONTAL
)
658 int newPos
= m_xScrollPosition
+ nScrollInc
;
659 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
664 int newPos
= m_yScrollPosition
+ nScrollInc
;
665 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
668 if (orient
== wxHORIZONTAL
)
670 m_xScrollPosition
+= nScrollInc
;
674 m_yScrollPosition
+= nScrollInc
;
677 // Find targets in splitter window and send the event to them
678 wxNode
* node
= GetChildren().First();
681 wxWindow
* child
= (wxWindow
*) node
->Data();
682 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
684 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
685 if (splitter
->GetWindow1())
686 splitter
->GetWindow1()->ProcessEvent(event
);
687 if (splitter
->GetWindow2())
688 splitter
->GetWindow2()->ProcessEvent(event
);
695 m_targetWindow
->MacUpdateImmediately() ;