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)
38 #include "wx/generic/treectlg.h"
40 #include "wx/gizmos/splittree.h"
43 * wxRemotelyScrolledTreeCtrl
46 #if USE_GENERIC_TREECTRL
47 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
49 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
52 #if USE_GENERIC_TREECTRL
53 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
55 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
57 EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize
)
58 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
59 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
60 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
63 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
64 const wxSize
& sz
, long style
):
65 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
67 m_companionWindow
= NULL
;
70 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
74 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
77 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
79 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
84 // Implicit in overriding SetScrollbars
88 // Number of pixels per user unit (0 or -1 for no scrollbar)
89 // Length of virtual canvas in user units
90 // Length of page in user units
91 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
92 int noUnitsX
, int noUnitsY
,
96 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
98 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
99 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
101 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
104 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
109 // In case we're using the generic tree control.
110 // Get the view start
111 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
113 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
115 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
118 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
120 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
125 scrolledWindow
->GetViewStart(& x2
, & y2
);
130 // x is wrong since the horizontal scrollbar is controlled by the
131 // tree control, but we probably don't need it.
132 scrolledWindow
->GetViewStart(x
, y
);
136 // In case we're using the generic tree control.
137 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
139 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
141 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
143 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
146 GetViewStart(& startX
, & startY
);
148 int xppu1
, yppu1
, xppu2
, yppu2
;
149 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
150 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
152 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
153 dc
.SetUserScale( win
->GetScaleX(), win
->GetScaleY() );
157 // Scroll to the given line (in scroll units where each unit is
158 // the height of an item)
159 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
162 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
164 UINT sbCode
= SB_THUMBPOSITION
;
165 HWND vertScrollBar
= 0;
166 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
171 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
173 /* Doesn't work yet because scrolling is ignored by Scroll
175 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
178 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
179 win->Scroll(-1, posVert*yppu);
185 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
188 AdjustRemoteScrollbars();
192 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
194 AdjustRemoteScrollbars();
197 // If we don't have this, we get some bits of lines still remaining
198 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
202 if (m_companionWindow
)
203 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
206 // Adjust the containing wxScrolledWindow's scrollbars appropriately
207 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
209 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
211 // This is for the generic tree control.
212 // It calls SetScrollbars which has been overridden
213 // to adjust the parent scrolled window vertical
215 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
220 // This is for the wxMSW tree control
221 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
225 if (GetBoundingRect(GetRootItem(), itemRect
))
227 int itemHeight
= itemRect
.GetHeight();
230 GetClientSize(&w
, &h
);
232 wxRect
rect(0, 0, 0, 0);
234 int treeViewHeight
= rect
.GetHeight()/itemHeight
;
236 int scrollPixelsPerLine
= itemHeight
;
237 int scrollPos
= - (itemRect
.y
/ itemHeight
);
239 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
241 // Ensure that when a scrollbar becomes hidden or visible,
242 // the contained window sizes are right.
243 // Problem: this is called too early (?)
244 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
245 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
252 // Calculate the area that contains both rectangles
253 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
257 int right1
= rect1
.GetRight();
258 int bottom1
= rect1
.GetBottom();
259 int right2
= rect2
.GetRight();
260 int bottom2
= rect2
.GetBottom();
262 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
263 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
265 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
266 rect
.SetRight(bottomRight
.x
);
267 rect
.SetBottom(bottomRight
.y
);
273 // Calculate the tree overall size so we can set the scrollbar
275 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
277 CalcTreeSize(GetRootItem(), rect
);
280 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
282 // TODO: implement GetFirst/NextVisibleItem
283 // for wxGenericTreeCtrl, plus GetBoundingRect.
285 // More efficient implementation would be to find the last item (but how?)
286 // Q: is the bounding rect relative to the top of the virtual tree workspace
287 // or the top of the window? How would we convert?
289 if (GetBoundingRect(id
, itemSize
))
291 rect
= CombineRectangles(rect
, itemSize
);
295 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
298 CalcTreeSize(childId
, rect
);
299 childId
= GetNextChild(childId
, cookie
);
303 // Find the scrolled window that contains this control
304 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
306 wxWindow
* parent
= wxWindow::GetParent();
309 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
310 return (wxScrolledWindow
*) parent
;
311 parent
= parent
->GetParent();
316 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
318 int orient
= event
.GetOrientation();
319 if (orient
== wxHORIZONTAL
)
321 // Don't 'skip' or we'd get into infinite recursion
324 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
329 scrollWin
->GetViewStart(& x
, & y
);
335 * wxTreeCompanionWindow
337 * A window displaying values associated with tree control items.
340 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
342 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
343 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
344 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
345 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
346 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
349 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
353 wxWindow(parent
, id
, pos
, sz
, style
)
358 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
364 wxString text
= m_treeCtrl
->GetItemText(id
);
365 dc
.SetTextForeground(* wxBLACK
);
366 dc
.SetBackgroundMode(wxTRANSPARENT
);
369 dc
.GetTextExtent(text
, & textW
, & textH
);
372 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
374 dc
.DrawText(text
, x
, y
);
379 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
386 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
388 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
389 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
392 wxSize clientSize
= GetClientSize();
395 wxTreeItemId h
, lastH
;
396 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
398 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
400 cy
= itemRect
.GetTop();
401 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
403 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
406 // Draw the actual item
407 DrawItem(dc
, h
, drawItemRect
);
410 if (m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
412 cy
= itemRect
.GetBottom();
413 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
417 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
419 int orient
= event
.GetOrientation();
420 if (orient
== wxHORIZONTAL
)
422 // Don't 'skip' or we'd get into infinite recursion
428 // TODO: scroll the window physically instead of just refreshing.
432 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
434 // TODO: something more optimized than simply refresh the whole
435 // window when the tree is expanded/collapsed. Tricky.
440 * wxThinSplitterWindow
443 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
445 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
446 EVT_SIZE(wxThinSplitterWindow::OnSize
)
449 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
453 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
457 void wxThinSplitterWindow::SizeWindows()
459 // The client size may have changed inbetween
460 // the sizing of the first window and the sizing of
461 // the second. So repeat SizeWindows.
462 wxSplitterWindow::SizeWindows();
463 wxSplitterWindow::SizeWindows();
466 // Tests for x, y over sash
467 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
469 return wxSplitterWindow::SashHitTest(x
, y
, 4);
472 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
474 if ( m_sashPosition
== 0 || !m_windowTwo
)
476 if (GetWindowStyle() & wxSP_NOSASH
)
480 GetClientSize(&w
, &h
);
482 if ( m_splitMode
== wxSPLIT_VERTICAL
)
484 dc
.SetPen(* m_facePen
);
485 dc
.SetBrush(* m_faceBrush
);
488 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
489 h1
+= 1; // Not sure why this is necessary...
490 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
494 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
498 dc
.SetPen(* m_facePen
);
499 dc
.SetBrush(* m_faceBrush
);
502 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
504 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
508 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
511 dc
.SetPen(wxNullPen
);
512 dc
.SetBrush(wxNullBrush
);
515 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
517 wxSplitterWindow::OnSize(event
);
521 * wxSplitterScrolledWindow
524 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
526 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
527 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
528 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
531 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
535 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
539 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
541 wxSize sz
= GetClientSize();
542 if (GetChildren().First())
544 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
548 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
550 // Ensure that events being propagated back up the window hierarchy
551 // don't cause an infinite loop
552 static bool inOnScroll
= FALSE
;
557 int orient
= event
.GetOrientation();
559 int nScrollInc
= CalcScrollInc(event
);
566 if (orient
== wxHORIZONTAL
)
568 int newPos
= m_xScrollPosition
+ nScrollInc
;
569 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
573 int newPos
= m_yScrollPosition
+ nScrollInc
;
574 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
577 if (orient
== wxHORIZONTAL
)
579 m_xScrollPosition
+= nScrollInc
;
583 m_yScrollPosition
+= nScrollInc
;
586 // Find targets in splitter window and send the event to them
587 wxNode
* node
= GetChildren().First();
590 wxWindow
* child
= (wxWindow
*) node
->Data();
591 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
593 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
594 if (splitter
->GetWindow1())
595 splitter
->GetWindow1()->ProcessEvent(event
);
596 if (splitter
->GetWindow2())
597 splitter
->GetWindow2()->ProcessEvent(event
);
604 m_targetWindow
->MacUpdateImmediately() ;