]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - tools/src/tools/configtool/standalone/wxwin/splittree.cpp
unified MX27, MX25, MX37 trees
[karo-tx-redboot.git] / tools / src / tools / configtool / standalone / wxwin / splittree.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        splittree.cpp
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
5 //              hierarchy
6 // Author:      Julian Smart
7 // Modified by:
8 // Created:     8/7/2000
9 // RCS-ID:      $Id$
10 // Copyright:   (c) Julian Smart
11 //
12 // This program is part of the eCos host tools.
13 //
14 // This program is free software; you can redistribute it and/or modify it
15 // under the terms of the GNU General Public License as published by the Free
16 // Software Foundation; either version 2 of the License, or (at your option)
17 // any later version.
18 //
19 // This program is distributed in the hope that it will be useful, but WITHOUT
20 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
22 // more details.
23 //
24 // You should have received a copy of the GNU General Public License along with
25 // this program; if not, write to the Free Software Foundation, Inc.,
26 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
27 //
28 /////////////////////////////////////////////////////////////////////////////
29
30 // ============================================================================
31 // declarations
32 // ============================================================================
33
34 // ----------------------------------------------------------------------------
35 // headers
36 // ----------------------------------------------------------------------------
37 #ifdef __GNUG__
38 #pragma implementation "splittree.h"
39 #endif
40
41 // For compilers that support precompilation, includes "wx/wx.h".
42 #include "ecpch.h"
43
44 #ifdef __WXMSW__
45 #include "wx/msw/private.h"
46 #endif
47
48 #ifdef __BORLANDC__
49 #pragma hdrstop
50 #endif
51
52 #include "wx/generic/treectlg.h"
53
54 #include "splittree.h"
55 #include <math.h>
56
57 /*
58 * wxRemotelyScrolledTreeCtrl
59 */
60
61 #if USE_GENERIC_TREECTRL
62 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl, wxGenericTreeCtrl)
63 #else
64 IMPLEMENT_CLASS(wxRemotelyScrolledTreeCtrl, wxTreeCtrl)
65 #endif
66
67 #if USE_GENERIC_TREECTRL
68 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl, wxGenericTreeCtrl)
69 #else
70 BEGIN_EVENT_TABLE(wxRemotelyScrolledTreeCtrl, wxTreeCtrl)
71 #endif
72 EVT_SIZE(wxRemotelyScrolledTreeCtrl::OnSize)
73 EVT_TREE_ITEM_EXPANDED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
74 EVT_TREE_ITEM_COLLAPSED(-1, wxRemotelyScrolledTreeCtrl::OnExpand)
75 EVT_SCROLLWIN(wxRemotelyScrolledTreeCtrl::OnScroll)
76 END_EVENT_TABLE()
77
78 wxRemotelyScrolledTreeCtrl::wxRemotelyScrolledTreeCtrl(wxWindow* parent, wxWindowID id, const wxPoint& pt,
79                                                        const wxSize& sz, long style):
80     wxTreeCtrl(parent, id, pt, sz, style
81 #if wxVERSON_NUMBER > 2301
82                & ~wxTR_ROW_LINES
83 #endif         
84                )
85 {
86     m_companionWindow = NULL;
87 }
88
89 wxRemotelyScrolledTreeCtrl::~wxRemotelyScrolledTreeCtrl()
90 {
91 }
92
93 void wxRemotelyScrolledTreeCtrl::HideVScrollbar()
94 {
95 #ifdef __WXMSW__
96     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
97     {
98         ::ShowScrollBar((HWND) GetHWND(), SB_VERT, FALSE);
99     }
100     else
101 #endif
102     {
103         // Implicit in overriding SetScrollbars
104     }
105 }
106
107 // Number of pixels per user unit (0 or -1 for no scrollbar)
108 // Length of virtual canvas in user units
109 // Length of page in user units
110 void wxRemotelyScrolledTreeCtrl::SetScrollbars(int pixelsPerUnitX, int pixelsPerUnitY,
111                                                int noUnitsX, int noUnitsY,
112                                                int xPos, int yPos,
113                                                bool noRefresh)
114 {
115     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
116     {
117         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
118         win->wxGenericTreeCtrl::SetScrollbars(pixelsPerUnitX, 0, noUnitsX, 0, xPos, 0, noRefresh);
119         
120         ecScrolledWindow* scrolledWindow = GetScrolledWindow();
121         if (scrolledWindow)
122         {
123             scrolledWindow->SetScrollbars(0, pixelsPerUnitY, 0, noUnitsY, 0, yPos, noRefresh);
124         }
125     }
126 }
127
128 // In case we're using the generic tree control.
129 int wxRemotelyScrolledTreeCtrl::GetScrollPos(int orient) const
130 {
131     ecScrolledWindow* scrolledWindow = GetScrolledWindow();
132     
133     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
134     {
135         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
136         
137         if (orient == wxHORIZONTAL)
138             return win->wxGenericTreeCtrl::GetScrollPos(orient);
139         else
140         {
141             return scrolledWindow->GetScrollPos(orient);
142         }
143     }
144     return 0;
145 }
146
147
148 // In case we're using the generic tree control.
149 // Get the view start
150 void wxRemotelyScrolledTreeCtrl::GetViewStart(int *x, int *y) const
151 {
152     ecScrolledWindow* scrolledWindow = GetScrolledWindow();
153     
154     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
155     {
156         
157         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
158         int x1, y1, x2, y2;
159         win->wxGenericTreeCtrl::GetViewStart(& x1, & y1);
160         * x = x1; * y = y1;
161         if (!scrolledWindow)
162             return;
163         
164         scrolledWindow->GetViewStart(& x2, & y2);
165         * y = y2;
166     }
167     else
168     {
169         // x is wrong since the horizontal scrollbar is controlled by the
170         // tree control, but we probably don't need it.
171         scrolledWindow->GetViewStart(x, y);
172     }
173 }
174
175 // In case we're using the generic tree control.
176 void wxRemotelyScrolledTreeCtrl::PrepareDC(wxDC& dc)
177 {
178     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
179     {
180         ecScrolledWindow* scrolledWindow = GetScrolledWindow();
181         
182         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
183         
184         int startX, startY;
185         GetViewStart(& startX, & startY);
186         
187         int xppu1, yppu1, xppu2, yppu2;
188         win->wxGenericTreeCtrl::GetScrollPixelsPerUnit(& xppu1, & yppu1);
189         scrolledWindow->GetScrollPixelsPerUnit(& xppu2, & yppu2);
190         
191         dc.SetDeviceOrigin( -startX * xppu1, -startY * yppu2 );
192         // dc.SetUserScale( win->GetScaleX(), win->GetScaleY() );
193     }
194 }
195
196 // Scroll to the given line (in scroll units where each unit is
197 // the height of an item)
198 void wxRemotelyScrolledTreeCtrl::ScrollToLine(int posHoriz, int posVert)
199 {
200 #ifdef __WXMSW__
201     if (!IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
202     {
203         UINT sbCode = SB_THUMBPOSITION;
204         HWND vertScrollBar = 0;
205         MSWDefWindowProc((WXUINT) WM_VSCROLL, MAKELONG(sbCode, posVert), (WXHWND) vertScrollBar);
206     }
207     else
208 #endif
209     {
210         wxGenericTreeCtrl* win = (wxGenericTreeCtrl*) this;
211         win->Refresh();
212         /* Doesn't work yet because scrolling is ignored by Scroll
213         int xppu, yppu;
214         wxScrolledWindow* scrolledWindow = GetScrolledWindow();
215         if (scrolledWindow)
216         {
217         scrolledWindow->GetScrollPixelsPerUnit(& xppu, & yppu);
218         win->Scroll(-1, posVert*yppu);
219         }
220         */
221     }
222 }
223
224 void wxRemotelyScrolledTreeCtrl::OnSize(wxSizeEvent& event)
225 {
226     HideVScrollbar();
227     AdjustRemoteScrollbars();
228     event.Skip();
229 }
230
231 void wxRemotelyScrolledTreeCtrl::OnExpand(wxTreeEvent& event)
232 {
233     AdjustRemoteScrollbars();
234     event.Skip();
235     
236     // If we don't have this, we get some bits of lines still remaining
237     if (event.GetEventType() == wxEVT_COMMAND_TREE_ITEM_COLLAPSED)
238         Refresh();
239     
240     // Pass on the event
241     if (m_companionWindow)
242         m_companionWindow->GetEventHandler()->ProcessEvent(event);
243 }
244
245 // Adjust the containing wxScrolledWindow's scrollbars appropriately
246 void wxRemotelyScrolledTreeCtrl::AdjustRemoteScrollbars()
247 {
248     if (IsKindOf(CLASSINFO(wxGenericTreeCtrl)))
249     {
250         // This is for the generic tree control.
251         // It calls SetScrollbars which has been overridden
252         // to adjust the parent scrolled window vertical
253         // scrollbar.
254         ((wxGenericTreeCtrl*) this)->AdjustMyScrollbars();
255         return;
256     }
257     else
258     {
259         // This is for the wxMSW tree control
260         ecScrolledWindow* scrolledWindow = GetScrolledWindow();
261         if (scrolledWindow)
262         {
263             wxRect itemRect;
264             if (GetBoundingRect(GetRootItem(), itemRect))
265             {
266                 // Actually, the real height seems to be 1 less than reported
267                 // (e.g. 16 instead of 16)
268                 int itemHeight = itemRect.GetHeight() - 1;
269                 
270                 int w, h;
271                 GetClientSize(&w, &h);
272                 
273                 wxRect rect(0, 0, 0, 0);
274                 CalcTreeSize(rect);
275                 
276                 double f = ((double) (rect.GetHeight()) / (double) itemHeight)  ;
277                 int treeViewHeight = (int) ceil(f);
278                 
279                 int scrollPixelsPerLine = itemHeight;
280                 int scrollPos = - (itemRect.y / itemHeight);
281                 
282                 scrolledWindow->SetScrollbars(0, scrollPixelsPerLine, 0, treeViewHeight, 0, scrollPos);
283                 
284                 // Ensure that when a scrollbar becomes hidden or visible,
285                 // the contained window sizes are right.
286                 // Problem: this is called too early (?)
287                 wxSizeEvent event(scrolledWindow->GetSize(), scrolledWindow->GetId());
288                 scrolledWindow->GetEventHandler()->ProcessEvent(event);
289             }
290         }
291     }
292 }
293
294
295 // Calculate the area that contains both rectangles
296 static wxRect CombineRectangles(const wxRect& rect1, const wxRect& rect2)
297 {
298     wxRect rect;
299     
300     int right1 = rect1.GetRight();
301     int bottom1 = rect1.GetBottom();
302     int right2 = rect2.GetRight();
303     int bottom2 = rect2.GetBottom();
304     
305     wxPoint topLeft = wxPoint(wxMin(rect1.x, rect2.x), wxMin(rect1.y, rect2.y));
306     wxPoint bottomRight = wxPoint(wxMax(right1, right2), wxMax(bottom1, bottom2));
307     
308     rect.x = topLeft.x; rect.y = topLeft.y;
309     rect.SetRight(bottomRight.x);
310     rect.SetBottom(bottomRight.y);
311     
312     return rect;
313 }
314
315
316 // Calculate the tree overall size so we can set the scrollbar
317 // correctly
318
319 // static int g_count = 0;
320
321 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(wxRect& rect)
322 {
323     //    g_count = 0;
324     CalcTreeSize(GetRootItem(), rect);
325 }
326
327 void wxRemotelyScrolledTreeCtrl::CalcTreeSize(const wxTreeItemId& id, wxRect& rect)
328 {
329     // More efficient implementation would be to find the last item (but how?)
330     // Q: is the bounding rect relative to the top of the virtual tree workspace
331     // or the top of the window? How would we convert?
332     wxRect itemSize;
333     if (GetBoundingRect(id, itemSize))
334     {
335         rect = CombineRectangles(rect, itemSize);
336     }
337
338     long cookie;
339     wxTreeItemId childId = GetFirstChild(id, cookie);
340     while (childId != 0)
341     {
342         CalcTreeSize(childId, rect);
343         childId = GetNextChild(childId, cookie);
344     }
345 }
346
347 // Find the scrolled window that contains this control
348 ecScrolledWindow* wxRemotelyScrolledTreeCtrl::GetScrolledWindow() const
349 {
350     wxWindow* parent = wxWindow::GetParent();
351     while (parent)
352     {
353         if (parent->IsKindOf(CLASSINFO(ecScrolledWindow)))
354             return (ecScrolledWindow*) parent;
355         parent = parent->GetParent();
356     }
357     return NULL;
358 }
359
360 void wxRemotelyScrolledTreeCtrl::OnScroll(wxScrollWinEvent& event)
361 {
362     int orient = event.GetOrientation();
363     if (orient == wxHORIZONTAL)
364     {
365         event.Skip();
366         return;
367     }
368     ecScrolledWindow* scrollWin = GetScrolledWindow();
369     if (!scrollWin)
370         return;
371     
372     int x, y;
373     scrollWin->GetViewStart(& x, & y);
374     
375     ScrollToLine(-1, y);
376 }
377
378 /*
379 * wxTreeCompanionWindow
380 *
381 * A window displaying values associated with tree control items.
382 */
383
384 IMPLEMENT_CLASS(wxTreeCompanionWindow, wxWindow)
385
386 BEGIN_EVENT_TABLE(wxTreeCompanionWindow, wxWindow)
387 EVT_PAINT(wxTreeCompanionWindow::OnPaint)
388 EVT_SCROLLWIN(wxTreeCompanionWindow::OnScroll)
389 EVT_TREE_ITEM_EXPANDED(-1, wxTreeCompanionWindow::OnExpand)
390 EVT_TREE_ITEM_COLLAPSED(-1, wxTreeCompanionWindow::OnExpand)
391 END_EVENT_TABLE()
392
393 wxTreeCompanionWindow::wxTreeCompanionWindow(wxWindow* parent, wxWindowID id,
394                                              const wxPoint& pos,
395                                              const wxSize& sz,
396                                              long style):
397 wxWindow(parent, id, pos, sz, style)
398 {
399     m_treeCtrl = NULL;
400 }
401
402 void wxTreeCompanionWindow::DrawItem(wxDC& dc, wxTreeItemId id, const wxRect& rect)
403 {
404     if (m_treeCtrl)
405     {
406         wxString text = m_treeCtrl->GetItemText(id);
407         dc.SetTextForeground(* wxBLACK);
408         dc.SetBackgroundMode(wxTRANSPARENT);
409         
410         int textW, textH;
411         dc.GetTextExtent(text, & textW, & textH);
412         
413         int x = 5;
414         int y = rect.GetY() + wxMax(0, (rect.GetHeight() - textH) / 2);
415         
416         dc.DrawText(text, x, y);
417     }
418 }
419
420 void wxTreeCompanionWindow::OnPaint(wxPaintEvent& event)
421 {
422     wxPaintDC dc(this);
423     
424     if (!m_treeCtrl)
425         return;
426     
427     wxPen pen(wxSystemSettings::GetSystemColour(wxSYS_COLOUR_3DLIGHT), 1, wxSOLID);
428     dc.SetPen(pen);
429     dc.SetBrush(* wxTRANSPARENT_BRUSH);
430     wxFont font(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
431     dc.SetFont(font);
432     
433     wxSize clientSize = GetClientSize();
434     wxRect itemRect;
435     int cy=0;
436     wxTreeItemId h, lastH;
437     for(h=m_treeCtrl->GetFirstVisibleItem();h;h=m_treeCtrl->GetNextVisible(h))
438     {
439         if (m_treeCtrl->GetBoundingRect(h, itemRect))
440         {
441             cy = itemRect.GetTop();
442             wxRect drawItemRect(0, cy, clientSize.x, itemRect.GetHeight());
443             
444             lastH = h;
445             
446             // Draw the actual item
447             DrawItem(dc, h, drawItemRect);
448             dc.DrawLine(0, cy, clientSize.x, cy);
449         }
450     }
451     if (lastH.IsOk() && m_treeCtrl->GetBoundingRect(lastH, itemRect))
452     {
453         cy = itemRect.GetBottom();
454         dc.DrawLine(0, cy, clientSize.x, cy);
455     }
456 }
457
458 void wxTreeCompanionWindow::OnScroll(wxScrollWinEvent& event)
459 {
460     int orient = event.GetOrientation();
461     if (orient == wxHORIZONTAL)
462     {
463         event.Skip();
464         return;
465     }
466     if (!m_treeCtrl)
467         return;
468     
469     // TODO: scroll the window physically instead of just refreshing.
470     Refresh(TRUE);
471 }
472
473 void wxTreeCompanionWindow::OnExpand(wxTreeEvent& event)
474 {
475     // TODO: something more optimized than simply refresh the whole
476     // window when the tree is expanded/collapsed. Tricky.
477     Refresh();
478 }
479
480 /*
481 * wxThinSplitterWindow
482 */
483
484 IMPLEMENT_CLASS(wxThinSplitterWindow, wxSplitterWindow)
485
486 BEGIN_EVENT_TABLE(wxThinSplitterWindow, wxSplitterWindow)
487 EVT_SIZE(wxThinSplitterWindow::OnSize)
488 // Not in older versions of wxWindows, unfortunately
489 #if 0
490 EVT_SPLITTER_DOUBLECLICKED(-1, wxThinSplitterWindow::OnDoubleClickSash)
491 #endif
492 END_EVENT_TABLE()
493
494 wxThinSplitterWindow::wxThinSplitterWindow(wxWindow* parent, wxWindowID id,
495                                            const wxPoint& pos,
496                                            const wxSize& sz,
497                                            long style):
498 wxSplitterWindow(parent, id, pos, sz, style)
499 {
500 }
501
502 void wxThinSplitterWindow::SizeWindows()
503 {
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();
509 }
510
511 // Tests for x, y over sash
512 bool wxThinSplitterWindow::SashHitTest(int x, int y, int tolerance)
513 {
514     return wxSplitterWindow::SashHitTest(x, y, 4);
515 }
516
517 void wxThinSplitterWindow::DrawSash(wxDC& dc)
518 {
519     if ( m_sashPosition == 0 || !m_windowTwo)
520         return;
521     if (GetWindowStyle() & wxSP_NOSASH)
522         return;
523     
524     int w, h;
525     GetClientSize(&w, &h);
526     
527     if ( m_splitMode == wxSPLIT_VERTICAL )
528     {
529         dc.SetPen(* m_facePen);
530         dc.SetBrush(* m_faceBrush);
531         int h1 = h-1;
532         int y1 = 0;
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)
536         {
537             y1 = 2; h1 -= 3;
538         }
539         dc.DrawRectangle(m_sashPosition, y1, m_sashSize, h1);
540     }
541     else
542     {
543         dc.SetPen(* m_facePen);
544         dc.SetBrush(* m_faceBrush);
545         int w1 = w-1;
546         int x1 = 0;
547         if ( (GetWindowStyleFlag() & wxSP_BORDER) != wxSP_BORDER && (GetWindowStyleFlag() & wxSP_3DBORDER) != wxSP_3DBORDER )
548             w1 ++;
549         if ( (GetWindowStyleFlag() & wxSP_3DBORDER) == wxSP_3DBORDER)
550         {
551             x1 = 2; w1 -= 3;
552         }
553         dc.DrawRectangle(x1, m_sashPosition, w1, m_sashSize);
554     }
555     
556     dc.SetPen(wxNullPen);
557     dc.SetBrush(wxNullBrush);
558 }
559
560 void wxThinSplitterWindow::OnSize(wxSizeEvent& event)
561 {
562     wxSplitterWindow::OnSize(event);
563 }
564
565 /*
566 * wxSplitterScrolledWindow
567 */
568
569 IMPLEMENT_CLASS(wxSplitterScrolledWindow, ecScrolledWindow)
570
571 BEGIN_EVENT_TABLE(wxSplitterScrolledWindow, ecScrolledWindow)
572 EVT_SCROLLWIN(wxSplitterScrolledWindow::OnScroll)
573 EVT_SIZE(wxSplitterScrolledWindow::OnSize)
574 END_EVENT_TABLE()
575
576 wxSplitterScrolledWindow::wxSplitterScrolledWindow(wxWindow* parent, wxWindowID id,
577                                                    const wxPoint& pos,
578                                                    const wxSize& sz,
579                                                    long style):
580   ecScrolledWindow(parent, id, pos, sz, style)
581 {
582 }
583
584 void wxSplitterScrolledWindow::OnSize(wxSizeEvent& event)
585 {
586     wxSize sz = GetClientSize();
587     if (GetChildren().First())
588     {
589         ((wxWindow*) GetChildren().First()->Data())->SetSize(0, 0, sz.x, sz.y);
590     }
591 }
592
593 void wxSplitterScrolledWindow::OnScroll(wxScrollWinEvent& event)
594 {
595     // Ensure that events being propagated back up the window hierarchy
596     // don't cause an infinite loop
597     static bool inOnScroll = FALSE;
598     if (inOnScroll)
599     {
600         event.Skip();
601         return;
602     }
603     inOnScroll = TRUE;
604     
605     int orient = event.GetOrientation();
606     
607     int nScrollInc = CalcScrollInc(event);
608     if (nScrollInc == 0)
609     {
610         inOnScroll = FALSE;
611         return;
612     }
613     
614     if (orient == wxHORIZONTAL)
615     {
616         inOnScroll = FALSE;
617         event.Skip();
618         return;
619 #if 0
620         int newPos = m_xScrollPosition + nScrollInc;
621         SetScrollPos(wxHORIZONTAL, newPos, TRUE );
622 #endif
623     }
624     else
625     {
626         int newPos = m_yScrollPosition + nScrollInc;
627         SetScrollPos(wxVERTICAL, newPos, TRUE );
628     }
629     
630     if (orient == wxHORIZONTAL)
631     {
632         m_xScrollPosition += nScrollInc;
633     }
634     else
635     {
636         m_yScrollPosition += nScrollInc;
637     }
638     
639     // Find targets in splitter window and send the event to them
640     wxNode* node = GetChildren().First();
641     while (node)
642     {
643         wxWindow* child = (wxWindow*) node->Data();
644         if (child->IsKindOf(CLASSINFO(wxSplitterWindow)))
645         {
646             wxSplitterWindow* splitter = (wxSplitterWindow*) child;
647             if (splitter->GetWindow1())
648                 splitter->GetWindow1()->ProcessEvent(event);
649             if (splitter->GetWindow2())
650                 splitter->GetWindow2()->ProcessEvent(event);
651             break;
652         }
653         node = node->Next();
654     }
655     
656 #ifdef __WXMAC__
657     m_targetWindow->MacUpdateImmediately() ;
658 #endif
659     
660     inOnScroll = FALSE;
661 }
662