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/generic/treectlg.h"
45 #include "wx/gizmos/splittree.h"
49 * wxRemotelyScrolledTreeCtrl
52 #if USE_GENERIC_TREECTRL
53 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
55 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
58 #if USE_GENERIC_TREECTRL
59 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxGenericTreeCtrl
)
61 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl
, wxTreeCtrl
)
63 EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize
)
64 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
65 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand
)
66 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll
)
69 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow
* parent
, wxWindowID id
, const wxPoint
& pt
,
70 const wxSize
& sz
, long style
):
71 wxTreeCtrl(parent
, id
, pt
, sz
, style
)
73 m_companionWindow
= NULL
;
76 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
80 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
83 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
85 ::ShowScrollBar((HWND
) GetHWND(), SB_VERT
, FALSE
);
90 // Implicit in overriding SetScrollbars
94 // Number of pixels per user unit (0 or -1 for no scrollbar)
95 // Length of virtual canvas in user units
96 // Length of page in user units
97 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX
, int pixelsPerUnitY
,
98 int noUnitsX
, int noUnitsY
,
102 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
104 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
105 win
->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX
, 0, noUnitsX
, 0, xPos
, 0, noRefresh
);
107 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
110 scrolledWindow
->SetScrollbars(0, pixelsPerUnitY
, 0, noUnitsY
, 0, yPos
, noRefresh
);
115 // In case we're using the generic tree control.
116 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient
) const
118 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
120 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
122 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
124 if (orient
== wxHORIZONTAL
)
125 return win
->wxGenericTreeCtrl::GetScrollPos(orient
);
128 return scrolledWindow
->GetScrollPos(orient
);
135 // In case we're using the generic tree control.
136 // Get the view start
137 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x
, int *y
) const
139 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
141 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
144 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
146 win
->wxGenericTreeCtrl::GetViewStart(& x1
, & y1
);
151 scrolledWindow
->GetViewStart(& x2
, & y2
);
156 // x is wrong since the horizontal scrollbar is controlled by the
157 // tree control, but we probably don't need it.
158 scrolledWindow
->GetViewStart(x
, y
);
162 // In case we're using the generic tree control.
163 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC
& dc
)
165 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
167 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
169 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
172 GetViewStart(& startX
, & startY
);
174 int xppu1
, yppu1
, xppu2
, yppu2
;
175 win
->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1
, & yppu1
);
176 scrolledWindow
->GetScrollPixelsPerUnit(& xppu2
, & yppu2
);
178 dc
.SetDeviceOrigin( -startX
* xppu1
, -startY
* yppu2
);
179 // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
183 // Scroll to the given line (in scroll units where each unit is
184 // the height of an item)
185 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz
, int posVert
)
188 if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
190 UINT sbCode
= SB_THUMBPOSITION
;
191 HWND vertScrollBar
= 0;
192 MSWDefWindowProc((WXUINT
) WM_VSCROLL
, MAKELONG(sbCode
, posVert
), (WXHWND
) vertScrollBar
);
197 wxGenericTreeCtrl
* win
= (wxGenericTreeCtrl
*) this;
199 /* Doesn't work yet because scrolling is ignored by Scroll
201 wxScrolledWindow* scrolledWindow = GetScrolledWindow();
204 scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
205 win->Scroll(-1, posVert*yppu);
211 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent
& event
)
214 AdjustRemoteScrollbars();
218 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent
& event
)
220 AdjustRemoteScrollbars();
223 // If we don't have this, we get some bits of lines still remaining
224 if (event
.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED
)
228 if (m_companionWindow
)
229 m_companionWindow
->GetEventHandler()->ProcessEvent(event
);
232 // Adjust the containing wxScrolledWindow's scrollbars appropriately
233 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
235 if (IsKindOf(CLASSINFO(wxGenericTreeCtrl
)))
237 // This is for the generic tree control.
238 // It calls SetScrollbars which has been overridden
239 // to adjust the parent scrolled window vertical
241 ((wxGenericTreeCtrl
*) this)->AdjustMyScrollbars();
246 // This is for the wxMSW tree control
247 wxScrolledWindow
* scrolledWindow
= GetScrolledWindow();
251 if (GetBoundingRect(GetRootItem(), itemRect
))
253 // Actually, the real height seems to be 1 less than reported
254 // (e.g. 16 instead of 16)
255 int itemHeight
= itemRect
.GetHeight() - 1;
258 GetClientSize(&w
, &h
);
260 wxRect
rect(0, 0, 0, 0);
263 double f
= ((double) (rect
.GetHeight()) / (double) itemHeight
) ;
264 int treeViewHeight
= (int) ceil(f
);
266 int scrollPixelsPerLine
= itemHeight
;
267 int scrollPos
= - (itemRect
.y
/ itemHeight
);
269 scrolledWindow
->SetScrollbars(0, scrollPixelsPerLine
, 0, treeViewHeight
, 0, scrollPos
);
271 // Ensure that when a scrollbar becomes hidden or visible,
272 // the contained window sizes are right.
273 // Problem: this is called too early (?)
274 wxSizeEvent
event(scrolledWindow
->GetSize(), scrolledWindow
->GetId());
275 scrolledWindow
->GetEventHandler()->ProcessEvent(event
);
282 // Calculate the area that contains both rectangles
283 static wxRect
CombineRectangles(const wxRect
& rect1
, const wxRect
& rect2
)
287 int right1
= rect1
.GetRight();
288 int bottom1
= rect1
.GetBottom();
289 int right2
= rect2
.GetRight();
290 int bottom2
= rect2
.GetBottom();
292 wxPoint topLeft
= wxPoint(wxMin(rect1
.x
, rect2
.x
), wxMin(rect1
.y
, rect2
.y
));
293 wxPoint bottomRight
= wxPoint(wxMax(right1
, right2
), wxMax(bottom1
, bottom2
));
295 rect
.x
= topLeft
.x
; rect
.y
= topLeft
.y
;
296 rect
.SetRight(bottomRight
.x
);
297 rect
.SetBottom(bottomRight
.y
);
303 // Calculate the tree overall size so we can set the scrollbar
305 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect
& rect
)
307 CalcTreeSize(GetRootItem(), rect
);
310 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId
& id
, wxRect
& rect
)
312 // More efficient implementation would be to find the last item (but how?)
313 // Q: is the bounding rect relative to the top of the virtual tree workspace
314 // or the top of the window? How would we convert?
316 if (GetBoundingRect(id
, itemSize
))
318 rect
= CombineRectangles(rect
, itemSize
);
322 wxTreeItemId childId
= GetFirstChild(id
, cookie
);
325 CalcTreeSize(childId
, rect
);
326 childId
= GetNextChild(childId
, cookie
);
330 // Find the scrolled window that contains this control
331 wxScrolledWindow
* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
333 wxWindow
* parent
= wxWindow::GetParent();
336 if (parent
->IsKindOf(CLASSINFO(wxScrolledWindow
)))
337 return (wxScrolledWindow
*) parent
;
338 parent
= parent
->GetParent();
343 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent
& event
)
345 int orient
= event
.GetOrientation();
346 if (orient
== wxHORIZONTAL
)
351 wxScrolledWindow
* scrollWin
= GetScrolledWindow();
356 scrollWin
->GetViewStart(& x
, & y
);
362 * wxTreeCompanionWindow
364 * A window displaying values associated with tree control items.
367 IMPLEMENT_CLASS(wxTreeCompanionWindow
, wxWindow
)
369 BEGIN_EVENT_TABLE(wxTreeCompanionWindow
, wxWindow
)
370 EVT_PAINT(wxTreeCompanionWindow::OnPaint
)
371 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll
)
372 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand
)
373 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand
)
376 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow
* parent
, wxWindowID id
,
380 wxWindow(parent
, id
, pos
, sz
, style
)
385 void wxTreeCompanionWindow::DrawItem(wxDC
& dc
, wxTreeItemId id
, const wxRect
& rect
)
391 wxString text
= m_treeCtrl
->GetItemText(id
);
392 dc
.SetTextForeground(* wxBLACK
);
393 dc
.SetBackgroundMode(wxTRANSPARENT
);
396 dc
.GetTextExtent(text
, & textW
, & textH
);
399 int y
= rect
.GetY() + wxMax(0, (rect
.GetHeight() - textH
) / 2);
401 dc
.DrawText(text
, x
, y
);
406 void wxTreeCompanionWindow::OnPaint(wxPaintEvent
& event
)
413 wxPen
pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT
), 1, wxSOLID
);
415 dc
.SetBrush(* wxTRANSPARENT_BRUSH
);
416 wxFont
font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
419 wxSize clientSize
= GetClientSize();
422 wxTreeItemId h
, lastH
;
423 for(h
=m_treeCtrl
->GetFirstVisibleItem();h
;h
=m_treeCtrl
->GetNextVisible(h
))
425 if (m_treeCtrl
->GetBoundingRect(h
, itemRect
))
427 cy
= itemRect
.GetTop();
428 wxRect
drawItemRect(0, cy
, clientSize
.x
, itemRect
.GetHeight());
432 // Draw the actual item
433 DrawItem(dc
, h
, drawItemRect
);
434 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
437 if (lastH
.IsOk() && m_treeCtrl
->GetBoundingRect(lastH
, itemRect
))
439 cy
= itemRect
.GetBottom();
440 dc
.DrawLine(0, cy
, clientSize
.x
, cy
);
444 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent
& event
)
446 int orient
= event
.GetOrientation();
447 if (orient
== wxHORIZONTAL
)
455 // TODO: scroll the window physically instead of just refreshing.
459 void wxTreeCompanionWindow::OnExpand(wxTreeEvent
& event
)
461 // TODO: something more optimized than simply refresh the whole
462 // window when the tree is expanded/collapsed. Tricky.
467 * wxThinSplitterWindow
470 IMPLEMENT_CLASS(wxThinSplitterWindow
, wxSplitterWindow
)
472 BEGIN_EVENT_TABLE(wxThinSplitterWindow
, wxSplitterWindow
)
473 EVT_SIZE(wxThinSplitterWindow::OnSize
)
476 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow
* parent
, wxWindowID id
,
480 wxSplitterWindow(parent
, id
, pos
, sz
, style
)
484 void wxThinSplitterWindow::SizeWindows()
486 // The client size may have changed inbetween
487 // the sizing of the first window and the sizing of
488 // the second. So repeat SizeWindows.
489 wxSplitterWindow::SizeWindows();
490 wxSplitterWindow::SizeWindows();
493 // Tests for x, y over sash
494 bool wxThinSplitterWindow::SashHitTest(int x
, int y
, int tolerance
)
496 return wxSplitterWindow::SashHitTest(x
, y
, 4);
499 void wxThinSplitterWindow::DrawSash(wxDC
& dc
)
501 if ( m_sashPosition
== 0 || !m_windowTwo
)
503 if (GetWindowStyle() & wxSP_NOSASH
)
507 GetClientSize(&w
, &h
);
509 if ( m_splitMode
== wxSPLIT_VERTICAL
)
511 dc
.SetPen(* m_facePen
);
512 dc
.SetBrush(* m_faceBrush
);
515 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
516 h1
+= 1; // Not sure why this is necessary...
517 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
521 dc
.DrawRectangle(m_sashPosition
, y1
, m_sashSize
, h1
);
525 dc
.SetPen(* m_facePen
);
526 dc
.SetBrush(* m_faceBrush
);
529 if ( (GetWindowStyleFlag() & wxSP_BORDER
) != wxSP_BORDER
&& (GetWindowStyleFlag() & wxSP_3DBORDER
) != wxSP_3DBORDER
)
531 if ( (GetWindowStyleFlag() & wxSP_3DBORDER
) == wxSP_3DBORDER
)
535 dc
.DrawRectangle(x1
, m_sashPosition
, w1
, m_sashSize
);
538 dc
.SetPen(wxNullPen
);
539 dc
.SetBrush(wxNullBrush
);
542 void wxThinSplitterWindow::OnSize(wxSizeEvent
& event
)
544 wxSplitterWindow::OnSize(event
);
548 * wxSplitterScrolledWindow
551 IMPLEMENT_CLASS(wxSplitterScrolledWindow
, wxScrolledWindow
)
553 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow
, wxScrolledWindow
)
554 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll
)
555 EVT_SIZE(wxSplitterScrolledWindow::OnSize
)
558 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow
* parent
, wxWindowID id
,
562 wxScrolledWindow(parent
, id
, pos
, sz
, style
)
566 void wxSplitterScrolledWindow::OnSize(wxSizeEvent
& event
)
568 wxSize sz
= GetClientSize();
569 if (GetChildren().First())
571 ((wxWindow
*) GetChildren().First()->Data())->SetSize(0, 0, sz
.x
, sz
.y
);
575 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent
& event
)
577 // Ensure that events being propagated back up the window hierarchy
578 // don't cause an infinite loop
579 static bool inOnScroll
= FALSE
;
587 int orient
= event
.GetOrientation();
589 int nScrollInc
= CalcScrollInc(event
);
596 if (orient
== wxHORIZONTAL
)
602 int newPos
= m_xScrollPosition
+ nScrollInc
;
603 SetScrollPos(wxHORIZONTAL
, newPos
, TRUE
);
608 int newPos
= m_yScrollPosition
+ nScrollInc
;
609 SetScrollPos(wxVERTICAL
, newPos
, TRUE
);
612 if (orient
== wxHORIZONTAL
)
614 m_xScrollPosition
+= nScrollInc
;
618 m_yScrollPosition
+= nScrollInc
;
621 // Find targets in splitter window and send the event to them
622 wxNode
* node
= GetChildren().First();
625 wxWindow
* child
= (wxWindow
*) node
->Data();
626 if (child
->IsKindOf(CLASSINFO(wxSplitterWindow
)))
628 wxSplitterWindow
* splitter
= (wxSplitterWindow
*) child
;
629 if (splitter
->GetWindow1())
630 splitter
->GetWindow1()->ProcessEvent(event
);
631 if (splitter
->GetWindow2())
632 splitter
->GetWindow2()->ProcessEvent(event
);
639 m_targetWindow
->MacUpdateImmediately() ;