]> git.saurik.com Git - wxWidgets.git/commitdiff
when a window inside scrolled window receives focus, make sure the parent is scrolled...
authorVáclav Slavík <vslavik@fastmail.fm>
Wed, 19 Dec 2007 19:21:52 +0000 (19:21 +0000)
committerVáclav Slavík <vslavik@fastmail.fm>
Wed, 19 Dec 2007 19:21:52 +0000 (19:21 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@50835 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/scrolwin.h
src/generic/scrlwing.cpp

index 001eb0bca8b6babfb2ddcb2eee54bb14060f2b1f..01c90d50cb1f48b12b150e3ae810fa59101cf064 100644 (file)
@@ -162,6 +162,7 @@ public:
 #if wxUSE_MOUSEWHEEL
     void HandleOnMouseWheel(wxMouseEvent& event);
 #endif // wxUSE_MOUSEWHEEL
+    void HandleOnChildFocus(wxChildFocusEvent& event);
 
     // FIXME: this is needed for now for wxPlot compilation, should be removed
     //        once it is fixed!
index 4d4c52bc701f7a64c6ba46e96705a3905fed2961..0b6c1fcc6e42143a957702293ae796e29aec20ed 100644 (file)
@@ -241,6 +241,12 @@ bool wxScrollHelperEvtHandler::ProcessEvent(wxEvent& event)
         return true;
     }
 
+    if ( evType == wxEVT_CHILD_FOCUS )
+    {
+        m_scrollHelper->HandleOnChildFocus((wxChildFocusEvent &)event);
+        return true;
+    }
+
     // reset the skipped flag (which might have been set to true in
     // ProcessEvent() above) to be able to test it below
     bool wasSkipped = event.GetSkipped();
@@ -1345,6 +1351,75 @@ void wxScrollHelper::HandleOnMouseWheel(wxMouseEvent& event)
 
 #endif // wxUSE_MOUSEWHEEL
 
+void wxScrollHelper::HandleOnChildFocus(wxChildFocusEvent& event)
+{
+    // this event should be processed by all windows in parenthood chain,
+    // e.g. so that nested wxScrolledWindows work correctly
+    event.Skip();
+
+    // find the immediate child under which the window receiving focus is:
+    wxWindow *win = event.GetWindow();
+    while ( win->GetParent() != m_targetWindow )
+    {
+        win = win->GetParent();
+        wxCHECK_RET( win, "incorrectly sent wxChildFocusEvent - not our child" );
+    }
+
+    // if the child is not fully visible, try to scroll it into view:
+    int stepx, stepy;
+    GetScrollPixelsPerUnit(&stepx, &stepy);
+
+    // NB: we don't call CalcScrolledPosition() on win->GetPosition() here,
+    //     because children' positions are already scrolled
+    wxRect winrect(win->GetPosition(), win->GetSize());
+    wxSize view(m_targetWindow->GetClientSize());
+
+    int startx, starty;
+    GetViewStart(&startx, &starty);
+
+    // first in vertical direction:
+    if ( stepy > 0 )
+    {
+        int diff = 0;
+
+        if ( winrect.GetTop() < 0 )
+        {
+            diff = winrect.GetTop();
+        }
+        else if ( winrect.GetBottom() > view.y )
+        {
+            diff = winrect.GetBottom() - view.y + 1;
+            // round up to next scroll step if we can't get exact position,
+            // so that the window is fully visible:
+            diff += stepy - 1;
+        }
+
+        starty = (starty * stepy + diff) / stepy;
+    }
+
+    // then horizontal:
+    if ( stepx > 0 )
+    {
+        int diff = 0;
+
+        if ( winrect.GetLeft() < 0 )
+        {
+            diff = winrect.GetLeft();
+        }
+        else if ( winrect.GetRight() > view.x )
+        {
+            diff = winrect.GetRight() - view.x + 1;
+            // round up to next scroll step if we can't get exact position,
+            // so that the window is fully visible:
+            diff += stepx - 1;
+        }
+
+        startx = (startx * stepx + diff) / stepx;
+    }
+
+    Scroll(startx, starty);
+}
+
 // ----------------------------------------------------------------------------
 // wxScrolledWindow implementation
 // ----------------------------------------------------------------------------