]> git.saurik.com Git - wxWidgets.git/commitdiff
CanAcceptFocus() now returns true if either the window itself or one of its children...
authorVadim Zeitlin <vadim@wxwidgets.org>
Thu, 28 Jun 2007 12:04:06 +0000 (12:04 +0000)
committerVadim Zeitlin <vadim@wxwidgets.org>
Thu, 28 Jun 2007 12:04:06 +0000 (12:04 +0000)
git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@46994 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775

include/wx/containr.h
include/wx/window.h
src/common/containr.cpp
src/gtk/window.cpp
src/gtk1/window.cpp
src/msw/window.cpp

index 375caff127dec91b01a65a124a0d4f3898fa2383..e738d679070cd0f79656d639821b8353ffa7c41f 100644 (file)
@@ -57,12 +57,21 @@ public:
     // returns whether we should accept focus ourselves or not
     bool AcceptsFocus() const { return m_acceptsFocus; }
 
     // returns whether we should accept focus ourselves or not
     bool AcceptsFocus() const { return m_acceptsFocus; }
 
+    // returns whether we or one of our children accepts focus: we always do
+    // because if we don't have any focusable children it probably means that
+    // we're not being used as a container at all (think of wxGrid or generic
+    // wxListCtrl) and so should get focus for ourselves
+    bool AcceptsFocusRecursively() const { return true; }
+
     // call this when the number of children of the window changes
     // call this when the number of children of the window changes
-    void UpdateCanFocus() { SetCanFocus(ShouldAcceptFocus()); }
+    //
+    // note that we have any children, this panel (used just as container for
+    // them) shouldn't get focus for itself
+    void UpdateCanFocus() { SetCanFocus(!HasAnyFocusableChildren()); }
 
 protected:
 
 protected:
-    // return true if we should be focusable
-    bool ShouldAcceptFocus() const;
+    // return true if we have any children accepting focus
+    bool HasAnyFocusableChildren() const;
 
     // the parent window we manage the children for
     wxWindow *m_winParent;
 
     // the parent window we manage the children for
     wxWindow *m_winParent;
@@ -78,6 +87,7 @@ private:
 #define WX_DECLARE_CONTROL_CONTAINER_BASE()                                   \
 public:                                                                       \
     virtual bool AcceptsFocus() const;                                        \
 #define WX_DECLARE_CONTROL_CONTAINER_BASE()                                   \
 public:                                                                       \
     virtual bool AcceptsFocus() const;                                        \
+    virtual bool AcceptsFocusRecursively() const;                             \
     virtual void AddChild(wxWindowBase *child);                               \
     virtual void RemoveChild(wxWindowBase *child);                            \
     void SetFocusIgnoringChildren();                                          \
     virtual void AddChild(wxWindowBase *child);                               \
     virtual void RemoveChild(wxWindowBase *child);                            \
     void SetFocusIgnoringChildren();                                          \
@@ -103,6 +113,11 @@ protected:                                                                    \
         m_container.UpdateCanFocus();                                         \
     }                                                                         \
                                                                               \
         m_container.UpdateCanFocus();                                         \
     }                                                                         \
                                                                               \
+    bool classname::AcceptsFocusRecursively() const                           \
+    {                                                                         \
+        return m_container.AcceptsFocusRecursively();                         \
+    }                                                                         \
+                                                                              \
     bool classname::AcceptsFocus() const                                      \
     {                                                                         \
         return m_container.AcceptsFocus();                                    \
     bool classname::AcceptsFocus() const                                      \
     {                                                                         \
         return m_container.AcceptsFocus();                                    \
index 80c4b0961ec0c90fe4adddc2822806a989ad58f5..a9a881c8a8aaeb7d70733b6da892bda224e9049c 100644 (file)
@@ -582,17 +582,35 @@ public:
         // this class clients and take into account the current window state
     virtual bool AcceptsFocus() const { return true; }
 
         // this class clients and take into account the current window state
     virtual bool AcceptsFocus() const { return true; }
 
-        // can this window have focus right now?
-    bool CanAcceptFocus() const { return AcceptsFocus() && IsShown() && IsEnabled(); }
+        // can this window or one of its children accept focus?
+        //
+        // usually it's the same as AcceptsFocus() but is overridden for
+        // container windows
+    virtual bool AcceptsFocusRecursively() const { return AcceptsFocus(); }
 
         // can this window be given focus by keyboard navigation? if not, the
         // only way to give it focus (provided it accepts it at all) is to
         // click it
     virtual bool AcceptsFocusFromKeyboard() const { return AcceptsFocus(); }
 
 
         // can this window be given focus by keyboard navigation? if not, the
         // only way to give it focus (provided it accepts it at all) is to
         // click it
     virtual bool AcceptsFocusFromKeyboard() const { return AcceptsFocus(); }
 
+
+        // this is mostly a helper for the various functions using it below
+    bool CanBeFocused() const { return IsShown() && IsEnabled(); }
+
+        // can this window itself have focus?
+    bool IsFocusable() const { return AcceptsFocus() && CanBeFocused(); }
+
+        // can this window have focus right now?
+        //
+        // if this method returns true, it means that calling SetFocus() will
+        // put focus either to this window or one of its children, if you need
+        // to know whether this window accepts focus itself, use IsFocusable()
+    bool CanAcceptFocus() const
+        { return AcceptsFocusRecursively() && CanBeFocused(); }
+
         // can this window be assigned focus from keyboard right now?
     bool CanAcceptFocusFromKeyboard() const
         // can this window be assigned focus from keyboard right now?
     bool CanAcceptFocusFromKeyboard() const
-        { return AcceptsFocusFromKeyboard() && CanAcceptFocus(); }
+        { return AcceptsFocusFromKeyboard() && CanBeFocused(); }
 
         // call this when the return value of AcceptsFocus() changes
     virtual void SetCanFocus(bool WXUNUSED(canFocus)) { }
 
         // call this when the return value of AcceptsFocus() changes
     virtual void SetCanFocus(bool WXUNUSED(canFocus)) { }
index 16fda35e9d8632efa52957e0b6fae8744e99cf20..f2d237a36d8f2766b9e9a965eda2876b4f0aba1b 100644 (file)
@@ -57,32 +57,24 @@ void wxControlContainerBase::SetCanFocus(bool acceptsFocus)
     m_winParent->SetCanFocus(m_acceptsFocus);
 }
 
     m_winParent->SetCanFocus(m_acceptsFocus);
 }
 
-// if the window has a focusable child, it shouldn't be focusable itself (think
-// of wxPanel used for grouping different controls) but if it doesn't have any
-// (focusable) children, then it should be possible to give it focus (think of
-// wxGrid or generic wxListCtrl)
-bool wxControlContainerBase::ShouldAcceptFocus() const
+bool wxControlContainerBase::HasAnyFocusableChildren() const
 {
 {
-    // we can accept focus either if we have no children at all (in this case
-    // we're probably not used as a container) or only when at least one child
-    // accepts focus
-    wxWindowList::compatibility_iterator node = m_winParent->GetChildren().GetFirst();
-    if ( !node )
-        return true;
-
-    while ( node )
+    const wxWindowList& children = m_winParent->GetChildren();
+    for ( wxWindowList::const_iterator i = children.begin(),
+                                     end = children.end();
+          i != end;
+          ++i )
     {
     {
-        wxWindow *child = node->GetData();
-        node = node->GetNext();
+        const wxWindow * const child = *i;
 
         if ( !m_winParent->IsClientAreaChild(child) )
             continue;
 
         if ( child->CanAcceptFocus() )
 
         if ( !m_winParent->IsClientAreaChild(child) )
             continue;
 
         if ( child->CanAcceptFocus() )
-            return false;
+            return true;
     }
 
     }
 
-    return true;
+    return false;
 }
 
 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
 }
 
 #ifndef wxHAS_NATIVE_TAB_TRAVERSAL
index 9dcf7a83d85837d2a35fe484a1d15a85d2450662..4bb50f45862e27fd96d3e8169b5c725ea499e5ef 100644 (file)
@@ -1434,7 +1434,7 @@ gtk_window_button_press_callback( GtkWidget *widget,
 
     g_lastButtonNumber = gdk_event->button;
 
 
     g_lastButtonNumber = gdk_event->button;
 
-    if (win->m_wxwindow && (g_focusWindow != win) && win->CanAcceptFocus())
+    if (win->m_wxwindow && (g_focusWindow != win) && win->IsFocusable())
     {
         gtk_widget_grab_focus( win->m_wxwindow );
     }
     {
         gtk_widget_grab_focus( win->m_wxwindow );
     }
@@ -1570,7 +1570,7 @@ gtk_window_button_press_callback( GtkWidget *widget,
         return TRUE;
 
     if ((event_type == wxEVT_LEFT_DOWN) && !win->IsOfStandardClass() && 
         return TRUE;
 
     if ((event_type == wxEVT_LEFT_DOWN) && !win->IsOfStandardClass() && 
-        (g_focusWindow != win) && win->CanAcceptFocus())
+        (g_focusWindow != win) && win->IsFocusable())
     {
         gtk_widget_grab_focus( win->m_wxwindow );
     }
     {
         gtk_widget_grab_focus( win->m_wxwindow );
     }
index eb6d484896cade8008974a31eabd9f0f23359a26..d74a0e0e3fae2d1290eb6b0741de7c328f7e94fb 100644 (file)
@@ -1475,7 +1475,7 @@ static gint gtk_window_button_press_callback( GtkWidget *widget,
 
     g_lastButtonNumber = gdk_event->button;
 
 
     g_lastButtonNumber = gdk_event->button;
 
-    if (win->m_wxwindow && (g_focusWindow != win) && win->CanAcceptFocus())
+    if (win->m_wxwindow && (g_focusWindow != win) && win->IsFocusable())
     {
         gtk_widget_grab_focus( win->m_wxwindow );
 /*
     {
         gtk_widget_grab_focus( win->m_wxwindow );
 /*
index 4617bdd7b3a9f84355607f96c9d787e72d359275..484177c10230240763d96b1a2d142df5488645b5 100644 (file)
@@ -2742,7 +2742,7 @@ WXLRESULT wxWindowMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM l
                     // problems, so don't do it for them (unnecessary anyhow)
                     if ( !win->IsOfStandardClass() )
                     {
                     // problems, so don't do it for them (unnecessary anyhow)
                     if ( !win->IsOfStandardClass() )
                     {
-                        if ( message == WM_LBUTTONDOWN && win->CanAcceptFocus() )
+                        if ( message == WM_LBUTTONDOWN && win->IsFocusable() )
                             win->SetFocus();
                     }
                 }
                             win->SetFocus();
                     }
                 }