From 88afee4222ec3583976f573f52500b9884e12cbc Mon Sep 17 00:00:00 2001 From: Bayemite Date: Mon, 31 May 2021 20:58:29 +1000 Subject: [PATCH] implemented get window title for win32 --- include/GLFW/glfw3.h | 23 ++++++++++++++++++++++- src/internal.h | 1 + src/win32_window.c | 31 +++++++++++++++++++++++++++++++ src/window.c | 9 +++++++++ 4 files changed, 63 insertions(+), 1 deletion(-) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 7728dad1..00da3ce9 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -2895,6 +2895,26 @@ GLFWAPI int glfwWindowShouldClose(GLFWwindow* window); */ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value); +/*! @brief Retrieves the title of the specified window. + * + * This function gets the window title, encoded as UTF-8, of the specified + * window. + * + * @param[in] window The window to query. + * @return A copy of the UTF-8 encoded window title, or NULL if an error has occurred. + * + * @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref + * GLFW_PLATFORM_ERROR. + * + * @thread_safety This function must only be called from the main thread. + * + * @sa @ref window_title + * @sa @ref glfwSetWindowTitle + * + * @ingroup window + */ +GLFWAPI char* glfwGetWindowTitle(GLFWwindow* window); + /*! @brief Sets the title of the specified window. * * This function sets the window title, encoded as UTF-8, of the specified @@ -2912,7 +2932,8 @@ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value); * @thread_safety This function must only be called from the main thread. * * @sa @ref window_title - * + * @sa @ref glfwGetWindowTitle + * * @since Added in version 1.0. * @glfw3 Added window handle parameter. * diff --git a/src/internal.h b/src/internal.h index ce9783f9..4ab44d67 100644 --- a/src/internal.h +++ b/src/internal.h @@ -650,6 +650,7 @@ int _glfwPlatformCreateWindow(_GLFWwindow* window, const _GLFWctxconfig* ctxconfig, const _GLFWfbconfig* fbconfig); void _glfwPlatformDestroyWindow(_GLFWwindow* window); +char* _glfwPlatformGetWindowTitle(_GLFWwindow* window); void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title); void _glfwPlatformSetWindowIcon(_GLFWwindow* window, int count, const GLFWimage* images); diff --git a/src/win32_window.c b/src/win32_window.c index 52a9c680..0fa02e22 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1462,6 +1462,37 @@ void _glfwPlatformDestroyWindow(_GLFWwindow* window) DestroyIcon(window->win32.smallIcon); } +char* _glfwPlatformGetWindowTitle(_GLFWwindow* window) +{ + int count = GetWindowTextLengthW(window->win32.handle); + if(count == 0) + { + SetLastError(0); + int error = GetLastError(); + + if(error != 0) + { + _glfwInputErrorWin32(GLFW_PLATFORM_ERROR, "Win32: Querying window title failed"); + return NULL; + } + else + return calloc(1, sizeof(char)); // single \0 + + } + else + { + count += 1; // the \0 + WCHAR* wideTitle = calloc(count, sizeof(WCHAR)); + GetWindowTextW(window->win32.handle, wideTitle, count); + + char* title = _glfwCreateUTF8FromWideStringWin32(wideTitle); + if(!title) + return calloc(1, sizeof(char)); // single \0 + + return title; + } +} + void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) { WCHAR* wideTitle = _glfwCreateWideStringFromUTF8Win32(title); diff --git a/src/window.c b/src/window.c index 518b27fd..e312342e 100644 --- a/src/window.c +++ b/src/window.c @@ -501,6 +501,15 @@ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value) window->shouldClose = value; } +GLFWAPI char* glfwGetWindowTitle(GLFWwindow* handle) +{ + _GLFWwindow* window = (_GLFWwindow*) handle; + assert(window != NULL); + + _GLFW_REQUIRE_INIT_OR_RETURN(NULL); + return _glfwPlatformGetWindowTitle(window); +} + GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title) { _GLFWwindow* window = (_GLFWwindow*) handle;