diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 00256d4df..241508e8d 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -819,6 +819,12 @@ extern "C" { * Mouse cursor hover [window attribute](@ref GLFW_HOVERED_attrib). */ #define GLFW_HOVERED 0x0002000B +/*! @brief Input focus on calling show window hint and attribute + * + * Input focus [window hint](@ref GLFW_FOCUS_ON_SHOW_hint) or + * [window attribute](@ref GLFW_FOCUS_ON_SHOW_attrib). + */ +#define GLFW_FOCUS_ON_SHOW 0x0002000C /*! @brief Framebuffer bit depth hint. * @@ -3085,6 +3091,10 @@ GLFWAPI void glfwMaximizeWindow(GLFWwindow* window); * hidden. If the window is already visible or is in full screen mode, this * function does nothing. * + * By default, windowed mode windows are focused when shown + * Set the [GLFW_FOCUS_ON_SHOW](@ref GLFW_FOCUS_ON_SHOW_hint) to + * disable this behavior. + * * @param[in] window The window to make visible. * * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref @@ -3132,6 +3142,10 @@ GLFWAPI void glfwHideWindow(GLFWwindow* window); * initially created. Set the [GLFW_FOCUSED](@ref GLFW_FOCUSED_hint) to * disable this behavior. * + * Also by default, windowed mode windows are focused when shown + * with @ref glfwShowWindow. Set the + * [GLFW_FOCUS_ON_SHOW](@ref GLFW_FOCUS_ON_SHOW_hint) to disable this behavior. + * * __Do not use this function__ to steal focus from other applications unless * you are certain that is what the user wants. Focus stealing can be * extremely disruptive. diff --git a/src/internal.h b/src/internal.h index 92bbfccec..daf05b001 100644 --- a/src/internal.h +++ b/src/internal.h @@ -267,6 +267,7 @@ struct _GLFWwndconfig GLFWbool floating; GLFWbool maximized; GLFWbool centerCursor; + GLFWbool focusOnShow; struct { GLFWbool retina; char frameName[256]; @@ -372,6 +373,7 @@ struct _GLFWwindow GLFWbool decorated; GLFWbool autoIconify; GLFWbool floating; + GLFWbool focusOnShow; GLFWbool shouldClose; void* userPointer; GLFWvidmode videoMode; diff --git a/src/window.c b/src/window.c index 38a8982bf..1677c89b0 100644 --- a/src/window.c +++ b/src/window.c @@ -201,6 +201,7 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height, window->decorated = wndconfig.decorated; window->autoIconify = wndconfig.autoIconify; window->floating = wndconfig.floating; + window->focusOnShow = wndconfig.focusOnShow; window->cursorMode = GLFW_CURSOR_NORMAL; window->minwidth = GLFW_DONT_CARE; @@ -267,6 +268,7 @@ void glfwDefaultWindowHints(void) _glfw.hints.window.focused = GLFW_TRUE; _glfw.hints.window.autoIconify = GLFW_TRUE; _glfw.hints.window.centerCursor = GLFW_TRUE; + _glfw.hints.window.focusOnShow = GLFW_TRUE; // The default is 24 bits of color, 24 bits of depth and 8 bits of stencil, // double buffered @@ -370,6 +372,9 @@ GLFWAPI void glfwWindowHint(int hint, int value) case GLFW_CENTER_CURSOR: _glfw.hints.window.centerCursor = value ? GLFW_TRUE : GLFW_FALSE; return; + case GLFW_FOCUS_ON_SHOW: + _glfw.hints.window.focusOnShow = value ? GLFW_TRUE : GLFW_FALSE; + return; case GLFW_CLIENT_API: _glfw.hints.context.client = value; return; @@ -755,7 +760,9 @@ GLFWAPI void glfwShowWindow(GLFWwindow* handle) return; _glfwPlatformShowWindow(window); - _glfwPlatformFocusWindow(window); + + if(window->focusOnShow) + _glfwPlatformFocusWindow(window); } GLFWAPI void glfwRequestWindowAttention(GLFWwindow* handle) @@ -810,6 +817,8 @@ GLFWAPI int glfwGetWindowAttrib(GLFWwindow* handle, int attrib) return _glfwPlatformWindowMaximized(window); case GLFW_HOVERED: return _glfwPlatformWindowHovered(window); + case GLFW_FOCUS_ON_SHOW: + return window->focusOnShow; case GLFW_TRANSPARENT_FRAMEBUFFER: return _glfwPlatformFramebufferTransparent(window); case GLFW_RESIZABLE: diff --git a/tests/windows.c b/tests/windows.c index 1c1297397..e7c17645c 100644 --- a/tests/windows.c +++ b/tests/windows.c @@ -56,9 +56,10 @@ static const struct static void usage(void) { - printf("Usage: windows [-h] [-b]\n"); + printf("Usage: windows [-h] [-b] [-f] \n"); printf("Options:\n"); printf(" -b create decorated windows\n"); + printf(" -f set focus on show off for all but first window\n"); printf(" -h show this help\n"); } @@ -92,16 +93,20 @@ int main(int argc, char** argv) { int i, ch; int decorated = GLFW_FALSE; + int focusOnShow = GLFW_TRUE; int running = GLFW_TRUE; GLFWwindow* windows[4]; - while ((ch = getopt(argc, argv, "bh")) != -1) + while ((ch = getopt(argc, argv, "bfh")) != -1) { switch (ch) { case 'b': decorated = GLFW_TRUE; break; + case 'f': + focusOnShow = GLFW_FALSE; + break; case 'h': usage(); exit(EXIT_SUCCESS); @@ -122,6 +127,8 @@ int main(int argc, char** argv) for (i = 0; i < 4; i++) { int left, top, right, bottom; + if(i) + glfwWindowHint(GLFW_FOCUS_ON_SHOW, focusOnShow); windows[i] = glfwCreateWindow(200, 200, titles[i], NULL, NULL); if (!windows[i])