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"
44 * wxRemotelyScrolledTreeCtrl
47 #if USE_GENERIC_TREECTRL
48 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
50 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
53 #if USE_GENERIC_TREECTRL
54 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
56 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
58 EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize
)
59 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
60 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
61 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
64 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
65 const wxSize
& sz
, long style
):
66 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
68 m_companionWindow
= NULL
;
71 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
75 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
78 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
80 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
85 // Implicit in overriding SetScrollbars
89 // Number of pixels per user unit (0 or -1 for no scrollbar)
90 // Length of virtual canvas in user units
91 // Length of page in user units
92 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
93 int noUnitsX
, int noUnitsY
,
97 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
99 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
100 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
102 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
105 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
110 // In case we're using the generic tree control.
111 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
113 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
115 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
117 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
119 if (orient
== wxHORIZONTAL
)
120 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
123 return scrolledWindow
->GetScrollPos(orient
);
130 // In case we're using the generic tree control.
131 // Get the view start
132 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
134 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
136 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
139 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
141 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
146 scrolledWindow
->GetViewStart(& x2
, & y2
);
151 // x is wrong since the horizontal scrollbar is controlled by the
152 // tree control, but we probably don't need it.
153 scrolledWindow
->GetViewStart(x
, y
);
157 // In case we're using the generic tree control.
158 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
160 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
162 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
164 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
167 GetViewStart(& startX
, & startY
);
169 int xppu1
, yppu1
, xppu2
, yppu2
;
170 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
171 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
173 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
174 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
178 // Scroll to the given line (in scroll units where each unit is
179 // the height of an item)
180 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
183 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
185 UINT sbCode
= SB_THUMBPOSITION
;
186 HWND vertScrollBar
= 0;
187 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
192 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
194 /* Doesn't work yet because scrolling is ignored by Scroll
196 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
199 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
200 win->Scroll(-1, posVert*yppu);
206 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
209 AdjustRemoteScrollbars();
213 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
215 AdjustRemoteScrollbars();
218 // If we don't have this, we get some bits of lines still remaining
219 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
223 if (m_companionWindow
)
224 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
227 // Adjust the containing wxScrolledWindow's scrollbars appropriately
228 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
230 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
232 // This is for the generic tree control.
233 // It calls SetScrollbars which has been overridden
234 // to adjust the parent scrolled window vertical
236 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
241 // This is for the wxMSW tree control
242 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
246 if (GetBoundingRect(GetRootItem(), itemRect
))
248 // Actually, the real height seems to be 1 less than reported
249 // (e.g. 16 instead of 16)
250 int itemHeight
= itemRect
.GetHeight() - 1;
253 GetClientSize(&w
, &h
);
255 wxRect
rect(0, 0, 0, 0);
258 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
259 int treeViewHeight
= (int) ceil(f
);
261 int scrollPixelsPerLine
= itemHeight
;
262 int scrollPos
= - (itemRect
.y
/ itemHeight
);
264 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
266 // Ensure that when a scrollbar becomes hidden or visible,
267 // the contained window sizes are right.
268 // Problem: this is called too early (?)
269 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
270 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
277 // Calculate the area that contains both rectangles
278 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
282 int right1
= rect1
.GetRight();
283 int bottom1
= rect1
.GetBottom();
284 int right2
= rect2
.GetRight();
285 int bottom2
= rect2
.GetBottom();
287 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
288 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
290 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
291 rect
.SetRight(bottomRight
.x
);
292 rect
.SetBottom(bottomRight
.y
);
298 // Calculate the tree overall size so we can set the scrollbar
300 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
302 CalcTreeSize(GetRootItem(), rect
);
305 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
307 // More efficient implementation would be to find the last item (but how?)
308 // Q: is the bounding rect relative to the top of the virtual tree workspace
309 // or the top of the window? How would we convert?
311 if (GetBoundingRect(id
, itemSize
))
313 rect
= CombineRectangles(rect
, itemSize
);
317 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
320 CalcTreeSize(childId
, rect
);
321 childId
= GetNextChild(childId
, cookie
);
325 // Find the scrolled window that contains this control
326 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
328 wxWindow
* parent
= wxWindow::GetParent();
331 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
332 return (wxScrolledWindow
*) parent
;
333 parent
= parent
->GetParent();
338 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
340 int orient
= event
.GetOrientation();
341 if (orient
== wxHORIZONTAL
)
346 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
351 scrollWin
->GetViewStart(& x
, & y
);
357 * wxTreeCompanionWindow
359 * A window displaying values associated with tree control items.
362 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
364 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
365 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
366 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
367 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
368 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
371 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
375 wxWindow(parent
, id
, pos
, sz
, style
)
380 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
386 wxString text
= m_treeCtrl
->GetItemText(id
);
387 dc
.SetTextForeground(* wxBLACK
);
388 dc
.SetBackgroundMode(wxTRANSPARENT
);
391 dc
.GetTextExtent(text
, & textW
, & textH
);
394 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
396 dc
.DrawText(text
, x
, y
);
401 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
408 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
410 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
411 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
414 wxSize clientSize
= GetClientSize();
417 wxTreeItemId h
, lastH
;
418 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
420 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
422 cy
= itemRect
.GetTop();
423 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
427 // Draw the actual item
428 DrawItem(dc
, h
, drawItemRect
);
429 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
432 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
434 cy
= itemRect
.GetBottom();
435 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
439 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
441 int orient
= event
.GetOrientation();
442 if (orient
== wxHORIZONTAL
)
450 // TODO: scroll the window physically instead of just refreshing.
454 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
456 // TODO: something more optimized than simply refresh the whole
457 // window when the tree is expanded/collapsed. Tricky.
462 * wxThinSplitterWindow
465 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
467 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
468 EVT_SIZE(wxThinSplitterWindow::OnSize
)
471 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
475 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
479 void wxThinSplitterWindow::SizeWindows()
481 // The client size may have changed inbetween
482 // the sizing of the first window and the sizing of
483 // the second. So repeat SizeWindows.
484 wxSplitterWindow::SizeWindows();
485 wxSplitterWindow::SizeWindows();
488 // Tests for x, y over sash
489 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
491 return wxSplitterWindow::SashHitTest(x
, y
, 4);
494 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
496 if ( m_sashPosition
== 0 || !m_windowTwo
)
498 if (GetWindowStyle() & wxSP_NOSASH
)
502 GetClientSize(&w
, &h
);
504 if ( m_splitMode
== wxSPLIT_VERTICAL
)
506 dc
.SetPen(* m_facePen
);
507 dc
.SetBrush(* m_faceBrush
);
510 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
511 h1
+= 1; // Not sure why this is necessary...
512 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
516 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
520 dc
.SetPen(* m_facePen
);
521 dc
.SetBrush(* m_faceBrush
);
524 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
526 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
530 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
533 dc
.SetPen(wxNullPen
);
534 dc
.SetBrush(wxNullBrush
);
537 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
539 wxSplitterWindow::OnSize(event
);
543 * wxSplitterScrolledWindow
546 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
548 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
549 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
550 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
553 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
557 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
561 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
563 wxSize sz
= GetClientSize();
564 if (GetChildren().First())
566 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
570 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
572 // Ensure that events being propagated back up the window hierarchy
573 // don't cause an infinite loop
574 static bool inOnScroll
= FALSE
;
582 int orient
= event
.GetOrientation();
584 int nScrollInc
= CalcScrollInc(event
);
591 if (orient
== wxHORIZONTAL
)
597 int newPos
= m_xScrollPosition
+ nScrollInc
;
598 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
603 int newPos
= m_yScrollPosition
+ nScrollInc
;
604 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
607 if (orient
== wxHORIZONTAL
)
609 m_xScrollPosition
+= nScrollInc
;
613 m_yScrollPosition
+= nScrollInc
;
616 // Find targets in splitter window and send the event to them
617 wxNode
* node
= GetChildren().First();
620 wxWindow
* child
= (wxWindow
*) node
->Data();
621 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
623 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
624 if (splitter
->GetWindow1())
625 splitter
->GetWindow1()->ProcessEvent(event
);
626 if (splitter
->GetWindow2())
627 splitter
->GetWindow2()->ProcessEvent(event
);
634 m_targetWindow
->MacUpdateImmediately() ;