Added window methods to request and to check focus

Signed-off-by: Stefan Schindler <stefan@boxbox.org>
Signed-off-by: Jan Haller <bromeon@gmail.com>
This commit is contained in:
Foaly 2014-01-12 16:26:21 +01:00 committed by Jan Haller
parent f99035bea1
commit b965ad198b
9 changed files with 182 additions and 15 deletions

View File

@ -403,6 +403,7 @@ public:
/// on the previous thread first if it was active. /// on the previous thread first if it was active.
/// Only one window can be active on a thread at a time, thus /// Only one window can be active on a thread at a time, thus
/// the window previously active (if any) automatically gets deactivated. /// the window previously active (if any) automatically gets deactivated.
/// This is not to be confused with requestFocus().
/// ///
/// \param active True to activate, false to deactivate /// \param active True to activate, false to deactivate
/// ///
@ -411,6 +412,37 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool setActive(bool active = true) const; bool setActive(bool active = true) const;
////////////////////////////////////////////////////////////
/// \brief Request the current window to be made the active
/// foreground window
///
/// At any given time, only one window may have the input focus
/// to receive input events such as keystrokes or mouse
/// events.
/// If a window requests focus, it only hints to the operating
/// system, that it would like to be focused. The operating system
/// is free to deny the request. For example under Windows OS
/// windows are not allowed to steal focus. But the user
/// will be notified through a flashing taskbar button.
/// This is not to be confused with setActive().
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
bool requestFocus();
////////////////////////////////////////////////////////////
/// \brief Determine whether the window has the input focus
///
/// At any given time, only one window may have the input focus
/// to receive input events such as keystrokes or most mouse
/// events.
///
/// \return True if window has focus, false otherwise
///
////////////////////////////////////////////////////////////
bool hasFocus() const;
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
/// \brief Display on screen what has been rendered to the window so far /// \brief Display on screen what has been rendered to the window so far
/// ///

View File

@ -320,6 +320,23 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual void setKeyRepeatEnabled(bool enabled); virtual void setKeyRepeatEnabled(bool enabled);
////////////////////////////////////////////////////////////
/// \brief Request the current window to be made the active
/// foreground window
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool requestFocus();
////////////////////////////////////////////////////////////
/// \brief Determine whether the window has the input focus
///
/// \return True if window has focus, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool hasFocus() const;
protected: protected:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -552,6 +552,20 @@ void WindowImplCocoa::setKeyRepeatEnabled(bool enabled)
} }
////////////////////////////////////////////////////////////
bool WindowImplCocoa::requestFocus()
{
return [m_delegate requestFocus];
}
////////////////////////////////////////////////////////////
bool WindowImplCocoa::hasFocus() const
{
return [m_delegate hasFocus];
}
} // namespace priv } // namespace priv
} // namespace sf } // namespace sf

View File

@ -483,6 +483,26 @@ void WindowImplX11::setKeyRepeatEnabled(bool enabled)
} }
////////////////////////////////////////////////////////////
bool WindowImplX11::requestFocus()
{
XRaiseWindow(m_display, m_window);
XSetInputFocus(m_display, m_window, RevertToPointerRoot, CurrentTime);
return true;
}
////////////////////////////////////////////////////////////
bool WindowImplX11::hasFocus() const
{
::Window focusedWindow = 0;
int revertToReturn = 0;
XGetInputFocus(m_display, &focusedWindow, &revertToReturn);
return m_window == focusedWindow;
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplX11::switchToFullscreen(const VideoMode& mode) void WindowImplX11::switchToFullscreen(const VideoMode& mode)
{ {

View File

@ -154,6 +154,23 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual void setKeyRepeatEnabled(bool enabled); virtual void setKeyRepeatEnabled(bool enabled);
////////////////////////////////////////////////////////////
/// \brief Request the current window to be made the active
/// foreground window
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool requestFocus();
////////////////////////////////////////////////////////////
/// \brief Determine whether the window has the input focus
///
/// \return True if window has focus, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool hasFocus() const;
protected: protected:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -366,6 +366,20 @@ void WindowImplWin32::setKeyRepeatEnabled(bool enabled)
} }
////////////////////////////////////////////////////////////
bool WindowImplWin32::requestFocus()
{
return SetForegroundWindow(m_handle);
}
////////////////////////////////////////////////////////////
bool WindowImplWin32::hasFocus() const
{
return m_handle == GetForegroundWindow();
}
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
void WindowImplWin32::registerWindowClass() void WindowImplWin32::registerWindowClass()
{ {

View File

@ -153,6 +153,23 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual void setKeyRepeatEnabled(bool enabled); virtual void setKeyRepeatEnabled(bool enabled);
////////////////////////////////////////////////////////////
/// \brief Request the current window to be made the active
/// foreground window
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool requestFocus();
////////////////////////////////////////////////////////////
/// \brief Determine whether the window has the input focus
///
/// \return True if window has focus, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool hasFocus() const;
protected: protected:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////

View File

@ -337,6 +337,25 @@ bool Window::setActive(bool active) const
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
bool Window::requestFocus()
{
if (m_impl)
return m_impl->requestFocus();
return false;
}
////////////////////////////////////////////////////////////
bool Window::hasFocus() const
{
if (m_impl)
return m_impl->hasFocus();
return false;
}
////////////////////////////////////////////////////////////
void Window::display() void Window::display()
{ {
// Display the backbuffer on screen // Display the backbuffer on screen

View File

@ -194,6 +194,23 @@ public:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////
virtual void setKeyRepeatEnabled(bool enabled) = 0; virtual void setKeyRepeatEnabled(bool enabled) = 0;
////////////////////////////////////////////////////////////
/// \brief Request the current window to be made the active
/// foreground window
///
/// \return True if operation was successful, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool requestFocus() = 0;
////////////////////////////////////////////////////////////
/// \brief Determine whether the window has the input focus
///
/// \return True if window has focus, false otherwise
///
////////////////////////////////////////////////////////////
virtual bool hasFocus() const = 0;
protected: protected:
//////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////