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_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
63 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
64 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
67 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
68 const wxSize
& sz
, long style
):
69 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
71 m_companionWindow
= NULL
;
74 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
78 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
80 #if defined(__WXMSW__)
81 #if USE_GENERIC_TREECTRL
82 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
85 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
87 #if USE_GENERIC_TREECTRL
90 // Implicit in overriding SetScrollbars
96 // Number of pixels per user unit (0 or -1 for no scrollbar)
97 // Length of virtual canvas in user units
98 // Length of page in user units
99 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
100 int noUnitsX
, int noUnitsY
,
104 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
105 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
107 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
108 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
110 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
113 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
119 // In case we're using the generic tree control.
120 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
122 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
124 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
125 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
127 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
129 if (orient
== wxHORIZONTAL
)
130 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
133 return scrolledWindow
->GetScrollPos(orient
);
141 // In case we're using the generic tree control.
142 // Get the view start
143 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
145 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
147 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
148 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
151 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
153 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
158 scrolledWindow
->GetViewStart(& x2
, & y2
);
164 // x is wrong since the horizontal scrollbar is controlled by the
165 // tree control, but we probably don't need it.
166 scrolledWindow
->GetViewStart(x
, y
);
170 // In case we're using the generic tree control.
171 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
173 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
174 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
176 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
178 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
181 GetViewStart(& startX
, & startY
);
183 int xppu1
, yppu1
, xppu2
, yppu2
;
184 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
185 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
187 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
188 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
193 // Scroll to the given line (in scroll units where each unit is
194 // the height of an item)
195 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
198 #if USE_GENERIC_TREECTRL
199 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
202 UINT sbCode
= SB_THUMBPOSITION
;
203 HWND vertScrollBar
= 0;
204 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
206 #if USE_GENERIC_TREECTRL
210 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
212 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
214 /* Doesn't work yet because scrolling is ignored by Scroll
216 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
219 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
220 win->Scroll(-1, posVert*yppu);
227 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
230 AdjustRemoteScrollbars();
234 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
236 AdjustRemoteScrollbars();
239 // If we don't have this, we get some bits of lines still remaining
240 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
244 if (m_companionWindow
)
245 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
248 // Adjust the containing wxScrolledWindow's scrollbars appropriately
249 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
251 #if USE_GENERIC_TREECTRL || !defined(__WXMSW__)
252 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
254 // This is for the generic tree control.
255 // It calls SetScrollbars which has been overridden
256 // to adjust the parent scrolled window vertical
258 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
264 // This is for the wxMSW tree control
265 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
269 if (GetBoundingRect(GetRootItem(), itemRect
))
271 // Actually, the real height seems to be 1 less than reported
272 // (e.g. 16 instead of 16)
273 int itemHeight
= itemRect
.GetHeight() - 1;
276 GetClientSize(&w
, &h
);
278 wxRect
rect(0, 0, 0, 0);
281 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
282 int treeViewHeight
= (int) ceil(f
);
284 int scrollPixelsPerLine
= itemHeight
;
285 int scrollPos
= - (itemRect
.y
/ itemHeight
);
287 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
289 // Ensure that when a scrollbar becomes hidden or visible,
290 // the contained window sizes are right.
291 // Problem: this is called too early (?)
292 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
293 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
300 // Calculate the area that contains both rectangles
301 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
305 int right1
= rect1
.GetRight();
306 int bottom1
= rect1
.GetBottom();
307 int right2
= rect2
.GetRight();
308 int bottom2
= rect2
.GetBottom();
310 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
311 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
313 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
314 rect
.SetRight(bottomRight
.x
);
315 rect
.SetBottom(bottomRight
.y
);
321 // Calculate the tree overall size so we can set the scrollbar
323 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
325 CalcTreeSize(GetRootItem(), rect
);
328 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
330 // More efficient implementation would be to find the last item (but how?)
331 // Q: is the bounding rect relative to the top of the virtual tree workspace
332 // or the top of the window? How would we convert?
334 if (GetBoundingRect(id
, itemSize
))
336 rect
= CombineRectangles(rect
, itemSize
);
340 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
343 CalcTreeSize(childId
, rect
);
344 childId
= GetNextChild(childId
, cookie
);
348 // Find the scrolled window that contains this control
349 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
351 wxWindow
* parent
= wxWindow::GetParent();
354 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
355 return (wxScrolledWindow
*) parent
;
356 parent
= parent
->GetParent();
361 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
363 int orient
= event
.GetOrientation();
364 if (orient
== wxHORIZONTAL
)
369 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
374 scrollWin
->GetViewStart(& x
, & y
);
380 * wxTreeCompanionWindow
382 * A window displaying values associated with tree control items.
385 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
387 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
388 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
389 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
390 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
391 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
394 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
398 wxWindow(parent
, id
, pos
, sz
, style
)
403 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
409 wxString text
= m_treeCtrl
->GetItemText(id
);
410 dc
.SetTextForeground(* wxBLACK
);
411 dc
.SetBackgroundMode(wxTRANSPARENT
);
414 dc
.GetTextExtent(text
, & textW
, & textH
);
417 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
419 dc
.DrawText(text
, x
, y
);
424 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
431 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
433 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
434 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
437 wxSize clientSize
= GetClientSize();
440 wxTreeItemId h
, lastH
;
441 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
443 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
445 cy
= itemRect
.GetTop();
446 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
450 // Draw the actual item
451 DrawItem(dc
, h
, drawItemRect
);
452 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
455 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
457 cy
= itemRect
.GetBottom();
458 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
462 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
464 int orient
= event
.GetOrientation();
465 if (orient
== wxHORIZONTAL
)
473 // TODO: scroll the window physically instead of just refreshing.
477 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
479 // TODO: something more optimized than simply refresh the whole
480 // window when the tree is expanded/collapsed. Tricky.
485 * wxThinSplitterWindow
488 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
490 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
491 EVT_SIZE(wxThinSplitterWindow::OnSize
)
494 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
498 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
502 void wxThinSplitterWindow::SizeWindows()
504 // The client size may have changed inbetween
505 // the sizing of the first window and the sizing of
506 // the second. So repeat SizeWindows.
507 wxSplitterWindow::SizeWindows();
508 wxSplitterWindow::SizeWindows();
511 // Tests for x, y over sash
512 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
514 return wxSplitterWindow::SashHitTest(x
, y
, 4);
517 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
519 if ( m_sashPosition
== 0 || !m_windowTwo
)
521 if (GetWindowStyle() & wxSP_NOSASH
)
525 GetClientSize(&w
, &h
);
527 if ( m_splitMode
== wxSPLIT_VERTICAL
)
529 dc
.SetPen(* m_facePen
);
530 dc
.SetBrush(* m_faceBrush
);
533 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
534 h1
+= 1; // Not sure why this is necessary...
535 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
539 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
543 dc
.SetPen(* m_facePen
);
544 dc
.SetBrush(* m_faceBrush
);
547 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
549 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
553 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
556 dc
.SetPen(wxNullPen
);
557 dc
.SetBrush(wxNullBrush
);
560 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
562 wxSplitterWindow::OnSize(event
);
566 * wxSplitterScrolledWindow
569 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
571 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
572 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
573 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
576 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
580 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
584 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
586 wxSize sz
= GetClientSize();
587 if (GetChildren().First())
589 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
593 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
595 // Ensure that events being propagated back up the window hierarchy
596 // don't cause an infinite loop
597 static bool inOnScroll
= FALSE
;
605 int orient
= event
.GetOrientation();
607 int nScrollInc
= CalcScrollInc(event
);
614 if (orient
== wxHORIZONTAL
)
620 int newPos
= m_xScrollPosition
+ nScrollInc
;
621 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
626 int newPos
= m_yScrollPosition
+ nScrollInc
;
627 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
630 if (orient
== wxHORIZONTAL
)
632 m_xScrollPosition
+= nScrollInc
;
636 m_yScrollPosition
+= nScrollInc
;
639 // Find targets in splitter window and send the event to them
640 wxNode
* node
= GetChildren().First();
643 wxWindow
* child
= (wxWindow
*) node
->Data();
644 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
646 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
647 if (splitter
->GetWindow1())
648 splitter
->GetWindow1()->ProcessEvent(event
);
649 if (splitter
->GetWindow2())
650 splitter
->GetWindow2()->ProcessEvent(event
);
657 m_targetWindow
->MacUpdateImmediately() ;