From ad02525dadb780a4bacb6482ac633e90a10fe25c Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Sun, 25 Mar 2007 22:36:24 +0000
Subject: [PATCH] don't check whether the window is shown and enabled in
 AcceptsFocus() itself as it makes overriding it in derived classes
 problematic; provide a separate non virtual CanAcceptFocus() method checking
 whether the window accepts focus and if it can accept it now and use it
 instead of AcceptsFocus(); documented AcceptsFocus() and
 AcceptsFocusFromKeyboard()

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@45055 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/latex/wx/window.tex    | 23 +++++++++++++++++++++++
 include/wx/window.h         | 17 +++++++++++++++--
 src/dfb/nonownedwnd.cpp     |  2 +-
 src/gtk/window.cpp          |  2 +-
 src/gtk1/window.cpp         |  2 +-
 src/mac/carbon/toplevel.cpp |  2 +-
 src/mgl/toplevel.cpp        |  2 +-
 src/mgl/window.cpp          |  2 +-
 src/msw/control.cpp         |  2 +-
 src/msw/window.cpp          |  4 ++--
 src/os2/control.cpp         |  2 +-
 src/os2/window.cpp          |  2 +-
 src/univ/themes/win32.cpp   |  5 ++---
 src/x11/app.cpp             |  2 +-
 14 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/docs/latex/wx/window.tex b/docs/latex/wx/window.tex
index 4d266a0112..874de4a13a 100644
--- a/docs/latex/wx/window.tex
+++ b/docs/latex/wx/window.tex
@@ -171,6 +171,29 @@ can delete a window only when it is safe to do so, in idle time.
 \helpref{wxCloseEvent}{wxcloseevent}
 
 
+\membersection{wxWindow::AcceptsFocus}\label{wxwindowacceptsfocus}
+
+\constfunc{bool}{AcceptsFocus}{\void}
+
+This method may be overridden in the derived classes to return \false to
+indicate that this control doesn't accept input at all (i.e. behaves like e.g.
+\helpref{wxStaticText}{wxstatictext}) and so doesn't need focus.
+
+\wxheading{See also}
+
+\helpref{AcceptsFocusFromKeyboard}{wxwindowacceptsfocusfromkeyboard}
+
+
+\membersection{wxWindow::AcceptsFocusFromKeyboard}\label{wxwindowacceptsfocusfromkeyboard}
+
+\constfunc{bool}{AcceptsFocusFromKeyboard}{\void}
+
+This method may be overridden in the derived classes to return \false to
+indicate that while this control can, in principle, have focus if the user
+clicks it with the mouse, it shouldn't be included in the TAB traversal chain
+when using the keyboard.
+
+
 \membersection{wxWindow::AddChild}\label{wxwindowaddchild}
 
 \func{virtual void}{AddChild}{\param{wxWindow* }{child}}
diff --git a/include/wx/window.h b/include/wx/window.h
index 9e73245752..c48e7b8579 100644
--- a/include/wx/window.h
+++ b/include/wx/window.h
@@ -571,14 +571,27 @@ public:
 
     static wxWindow *DoFindFocus() /* = 0: implement in derived classes */;
 
-        // can this window have focus?
-    virtual bool AcceptsFocus() const { return IsShown() && IsEnabled(); }
+        // can this window have focus in principle?
+        //
+        // the difference between AcceptsFocus[FromKeyboard]() and CanAcceptFocus
+        // [FromKeyboard]() is that the former functions are meant to be
+        // overridden in the derived classes to simply return false if the
+        // control can't have focus, while the latter are meant to be used by
+        // 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 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 assigned focus from keyboard right now?
+    bool CanAcceptFocusFromKeyboard() const
+        { return AcceptsFocusFromKeyboard() && CanAcceptFocus(); }
+
         // navigates in the specified direction by sending a wxNavigationKeyEvent
     virtual bool Navigate(int flags = wxNavigationKeyEvent::IsForward);
 
diff --git a/src/dfb/nonownedwnd.cpp b/src/dfb/nonownedwnd.cpp
index 4fe1ac999d..886f918b0c 100644
--- a/src/dfb/nonownedwnd.cpp
+++ b/src/dfb/nonownedwnd.cpp
@@ -249,7 +249,7 @@ bool wxNonOwnedWindow::Show(bool show)
         {
             SetDfbFocus();
         }
-        else if ( AcceptsFocus() )
+        else if ( CanAcceptFocus() )
         {
             // FIXME: we should probably always call SetDfbFocus instead
             // and call SetFocus() from wxActivateEvent/DWET_GOTFOCUS
diff --git a/src/gtk/window.cpp b/src/gtk/window.cpp
index 1dbf515853..3d372237ed 100644
--- a/src/gtk/window.cpp
+++ b/src/gtk/window.cpp
@@ -1469,7 +1469,7 @@ gtk_window_button_press_callback( GtkWidget *widget,
 
     g_lastButtonNumber = gdk_event->button;
 
-    if (win->m_wxwindow && (g_focusWindow != win) && win->AcceptsFocus())
+    if (win->m_wxwindow && (g_focusWindow != win) && win->CanAcceptFocus())
     {
         gtk_widget_grab_focus( win->m_wxwindow );
     }
diff --git a/src/gtk1/window.cpp b/src/gtk1/window.cpp
index 9e328dbcde..f9934ba15e 100644
--- a/src/gtk1/window.cpp
+++ b/src/gtk1/window.cpp
@@ -1475,7 +1475,7 @@ static gint gtk_window_button_press_callback( GtkWidget *widget,
 
     g_lastButtonNumber = gdk_event->button;
 
-    if (win->m_wxwindow && (g_focusWindow != win) && win->AcceptsFocus())
+    if (win->m_wxwindow && (g_focusWindow != win) && win->CanAcceptFocus())
     {
         gtk_widget_grab_focus( win->m_wxwindow );
 /*
diff --git a/src/mac/carbon/toplevel.cpp b/src/mac/carbon/toplevel.cpp
index 27d297fac2..d29746a500 100644
--- a/src/mac/carbon/toplevel.cpp
+++ b/src/mac/carbon/toplevel.cpp
@@ -591,7 +591,7 @@ pascal OSStatus wxMacTopLevelMouseEventHandler( EventHandlerCallRef handler , Ev
             if ( wxevent.GetEventType() == wxEVT_LEFT_DOWN )
             {
                 // ... that is set focus to this window
-                if (currentMouseWindow->AcceptsFocus() && wxWindow::FindFocus()!=currentMouseWindow)
+                if (currentMouseWindow->CanAcceptFocus() && wxWindow::FindFocus()!=currentMouseWindow)
                     currentMouseWindow->SetFocus();
             }
 
diff --git a/src/mgl/toplevel.cpp b/src/mgl/toplevel.cpp
index a4f10c308c..ebb4bacd10 100644
--- a/src/mgl/toplevel.cpp
+++ b/src/mgl/toplevel.cpp
@@ -155,7 +155,7 @@ bool wxTopLevelWindowMGL::Show(bool show)
         GetEventHandler()->ProcessEvent(event);
     }
 
-    if ( ret && show && AcceptsFocus() )
+    if ( ret && show && CanAcceptFocus() )
         SetFocus();
         // FIXME_MGL -- don't do this for popup windows?
     return ret;
diff --git a/src/mgl/window.cpp b/src/mgl/window.cpp
index 70a9ffaf64..85f2abeee3 100644
--- a/src/mgl/window.cpp
+++ b/src/mgl/window.cpp
@@ -184,7 +184,7 @@ static ibool MGLAPI wxWindowMouseHandler(window_t *wnd, event_t *e)
     {
         case EVT_MOUSEDOWN:
             // Change focus if the user clicks outside focused window:
-            if ( win->AcceptsFocus() && wxWindow::FindFocus() != win )
+            if ( win->CanAcceptFocus() && wxWindow::FindFocus() != win )
                 win->SetFocus();
 
             if ( e->message & EVT_DBLCLICK )
diff --git a/src/msw/control.cpp b/src/msw/control.cpp
index 8e72df223d..bd6c3cff17 100644
--- a/src/msw/control.cpp
+++ b/src/msw/control.cpp
@@ -237,7 +237,7 @@ WXDWORD wxControl::MSWGetStyle(long style, WXDWORD *exstyle) const
 {
     long msStyle = wxWindow::MSWGetStyle(style, exstyle);
 
-    if ( AcceptsFocus() )
+    if ( AcceptsFocusFromKeyboard() )
     {
         msStyle |= WS_TABSTOP;
     }
diff --git a/src/msw/window.cpp b/src/msw/window.cpp
index b66358af13..a7ea67d2bb 100644
--- a/src/msw/window.cpp
+++ b/src/msw/window.cpp
@@ -2331,7 +2331,7 @@ bool wxWindowMSW::MSWShouldPreProcessMessage(WXMSG* msg)
               node = node->GetNext() )
         {
             wxWindow * const win = node->GetData();
-            if ( win->AcceptsFocus() &&
+            if ( win->CanAcceptFocus() &&
                     !(::GetWindowLong(GetHwndOf(win), GWL_EXSTYLE) &
                         WS_EX_CONTROLPARENT) )
             {
@@ -2741,7 +2741,7 @@ WXLRESULT wxWindowMSW::MSWWindowProc(WXUINT message, WXWPARAM wParam, WXLPARAM l
                     // problems, so don't do it for them (unnecessary anyhow)
                     if ( !win->IsOfStandardClass() )
                     {
-                        if ( message == WM_LBUTTONDOWN && win->AcceptsFocus() )
+                        if ( message == WM_LBUTTONDOWN && win->CanAcceptFocus() )
                             win->SetFocus();
                     }
                 }
diff --git a/src/os2/control.cpp b/src/os2/control.cpp
index 51eff786ee..09bf756b6f 100644
--- a/src/os2/control.cpp
+++ b/src/os2/control.cpp
@@ -227,7 +227,7 @@ WXDWORD wxControl::OS2GetStyle( long lStyle, WXDWORD* pdwExstyle ) const
 {
     long dwStyle = wxWindow::OS2GetStyle( lStyle, pdwExstyle );
 
-    if (AcceptsFocus())
+    if (AcceptsFocusFromKeyboard())
     {
         dwStyle |= WS_TABSTOP;
     }
diff --git a/src/os2/window.cpp b/src/os2/window.cpp
index 3a80c6337d..43c6a8f13a 100644
--- a/src/os2/window.cpp
+++ b/src/os2/window.cpp
@@ -2358,7 +2358,7 @@ MRESULT wxWindowOS2::OS2WindowProc( WXUINT uMsg,
                                                                   );
                     if (!pWin->IsOfStandardClass())
                     {
-                        if (uMsg == WM_BUTTON1DOWN && pWin->AcceptsFocus() )
+                        if (uMsg == WM_BUTTON1DOWN && pWin->CanAcceptFocus() )
                             pWin->SetFocus();
                     }
                     bProcessed = pWin->HandleMouseEvent( uMsg
diff --git a/src/univ/themes/win32.cpp b/src/univ/themes/win32.cpp
index 12b176ac04..3603333f2b 100644
--- a/src/univ/themes/win32.cpp
+++ b/src/univ/themes/win32.cpp
@@ -3196,10 +3196,9 @@ bool wxWin32InputHandler::HandleMouse(wxInputConsumer *control,
     // clicking on the control gives it focus
     if ( event.ButtonDown() )
     {
-        wxWindow *win = control->GetInputWindow();
+        wxWindow * const win = control->GetInputWindow();
 
-        if ( (wxWindow::FindFocus() != control->GetInputWindow()) &&
-             win->AcceptsFocus() )
+        if ( win->CanAcceptFocus() && wxWindow::FindFocus() != win )
         {
             win->SetFocus();
 
diff --git a/src/x11/app.cpp b/src/x11/app.cpp
index a7165cf5ec..afcc29cb00 100644
--- a/src/x11/app.cpp
+++ b/src/x11/app.cpp
@@ -544,7 +544,7 @@ bool wxApp::ProcessXEvent(WXEvent* _event)
 
             if (event->type == ButtonPress)
             {
-                if ((win != wxWindow::FindFocus()) && win->AcceptsFocus())
+                if ((win != wxWindow::FindFocus()) && win->CanAcceptFocus())
                 {
                     // This might actually be done in wxWindow::SetFocus()
                     // and not here. TODO.
-- 
2.50.0

