Add glfwGetWindowTitle

This adds a function for querying the current title of a window.  This
currently returns a copy of the last title set via GLFW.

Fixes #1448
Closes #1909
Closes #2482
This commit is contained in:
Doug Binks 2024-02-07 19:26:52 +00:00 committed by Camilla Löwy
parent bafece4cf7
commit 95d464bb4b
8 changed files with 81 additions and 2 deletions

View File

@ -15,6 +15,7 @@ video tutorials.
- Keith Bauer - Keith Bauer
- John Bartholomew - John Bartholomew
- Coşku Baş - Coşku Baş
- Bayemite
- Niklas Behrens - Niklas Behrens
- Andrew Belt - Andrew Belt
- Nevyn Bengtsson - Nevyn Bengtsson

View File

@ -167,6 +167,7 @@ information on what to include when reporting a bug.
- Added support for `XDG_SESSION_TYPE` environment variable - Added support for `XDG_SESSION_TYPE` environment variable
- Added `GLFW_PKG_CONFIG_REQUIRES_PRIVATE` and `GLFW_PKG_CONFIG_LIBS_PRIVATE` CMake - Added `GLFW_PKG_CONFIG_REQUIRES_PRIVATE` and `GLFW_PKG_CONFIG_LIBS_PRIVATE` CMake
variables exposing pkg-config dependencies (#1307) variables exposing pkg-config dependencies (#1307)
- Added `glfwGetWindowTitle` function for GLFWwindow for querying window titles (#1448,#1909)
- Made joystick subsystem initialize at first use (#1284,#1646) - Made joystick subsystem initialize at first use (#1284,#1646)
- Made `GLFW_DOUBLEBUFFER` a read-only window attribute - Made `GLFW_DOUBLEBUFFER` a read-only window attribute
- Updated the minimum required CMake version to 3.1 - Updated the minimum required CMake version to 3.1

View File

@ -41,6 +41,12 @@ to whatever window is behind it. This can also be changed after window
creation with the matching [window attribute](@ref GLFW_MOUSE_PASSTHROUGH_attrib). creation with the matching [window attribute](@ref GLFW_MOUSE_PASSTHROUGH_attrib).
#### Ability to get a window's title {#features_34_get_window_title}
GLFW now supports retrieving a window's title with the @ref glfwGetWindowTitle
function.
#### Wayland libdecor decorations {#wayland_libdecor_34} #### Wayland libdecor decorations {#wayland_libdecor_34}
GLFW now supports improved fallback window decorations via GLFW now supports improved fallback window decorations via
@ -253,6 +259,7 @@ then GLFW will fail to initialize.
- @ref glfwGetPlatform - @ref glfwGetPlatform
- @ref glfwPlatformSupported - @ref glfwPlatformSupported
- @ref glfwInitVulkanLoader - @ref glfwInitVulkanLoader
- @ref glfwGetWindowTitle
#### New types in version 3.4 {#types_34} #### New types in version 3.4 {#types_34}

View File

@ -919,6 +919,15 @@ If you are using C++11 or C11, you can use a UTF-8 string literal.
glfwSetWindowTitle(window, u8"This is always a UTF-8 string"); glfwSetWindowTitle(window, u8"This is always a UTF-8 string");
``` ```
The window title can be retrieved with @ref glfwGetWindowTitle.
```c
const char* title = glfwGetWindowTitle(window);
```
The title returned is an internally managed copy of the title set
by @ref glfwCreateWindow or @ref glfwSetWindowTitle. It does not
include any additional text which may be appended by the platform.
### Window icon {#window_icon} ### Window icon {#window_icon}

View File

@ -3305,6 +3305,30 @@ GLFWAPI int glfwWindowShouldClose(GLFWwindow* window);
*/ */
GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* window, int value); 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, as set by glfwCreateWindow
* or glfwSetWindowTitle, or NULL if there is an error.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED
*
* @pointer_lifetime The returned string is allocated and freed by GLFW. You
* should not free it yourself. It is valid until the next call to @ref
* glfwSetWindowTitle, or until the library is terminated.
*
* @thread_safety This function must only be called from the main thread.
*
* @sa @ref window_title
* @sa @ref glfwSetWindowTitle
*
* @ingroup window
*/
GLFWAPI const char* glfwGetWindowTitle(GLFWwindow* window);
/*! @brief Sets the title of the specified window. /*! @brief Sets the title of the specified window.
* *
* This function sets the window title, encoded as UTF-8, of the specified * This function sets the window title, encoded as UTF-8, of the specified

View File

@ -532,6 +532,7 @@ struct _GLFWwindow
GLFWvidmode videoMode; GLFWvidmode videoMode;
_GLFWmonitor* monitor; _GLFWmonitor* monitor;
_GLFWcursor* cursor; _GLFWcursor* cursor;
char* title;
int minwidth, minheight; int minwidth, minheight;
int maxwidth, maxheight; int maxwidth, maxheight;

View File

@ -242,6 +242,7 @@ GLFWAPI GLFWwindow* glfwCreateWindow(int width, int height,
window->maxheight = GLFW_DONT_CARE; window->maxheight = GLFW_DONT_CARE;
window->numer = GLFW_DONT_CARE; window->numer = GLFW_DONT_CARE;
window->denom = GLFW_DONT_CARE; window->denom = GLFW_DONT_CARE;
window->title = _glfw_strdup(title);
if (!_glfw.platform.createWindow(window, &wndconfig, &ctxconfig, &fbconfig)) if (!_glfw.platform.createWindow(window, &wndconfig, &ctxconfig, &fbconfig))
{ {
@ -493,7 +494,7 @@ GLFWAPI void glfwDestroyWindow(GLFWwindow* handle)
*prev = window->next; *prev = window->next;
} }
_glfw_free(window->title);
_glfw_free(window); _glfw_free(window);
} }
@ -515,6 +516,16 @@ GLFWAPI void glfwSetWindowShouldClose(GLFWwindow* handle, int value)
window->shouldClose = value; window->shouldClose = value;
} }
GLFWAPI const char* glfwGetWindowTitle(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);
_GLFW_REQUIRE_INIT_OR_RETURN(NULL);
return window->title;
}
GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title) GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title)
{ {
_GLFWwindow* window = (_GLFWwindow*) handle; _GLFWwindow* window = (_GLFWwindow*) handle;
@ -522,7 +533,12 @@ GLFWAPI void glfwSetWindowTitle(GLFWwindow* handle, const char* title)
assert(title != NULL); assert(title != NULL);
_GLFW_REQUIRE_INIT(); _GLFW_REQUIRE_INIT();
char* prev = window->title;
window->title = _glfw_strdup(title);
_glfw.platform.setWindowTitle(window, title); _glfw.platform.setWindowTitle(window, title);
_glfw_free(prev);
} }
GLFWAPI void glfwSetWindowIcon(GLFWwindow* handle, GLFWAPI void glfwSetWindowIcon(GLFWwindow* handle,

View File

@ -62,6 +62,7 @@ int main(int argc, char** argv)
char min_width_buffer[12] = "", min_height_buffer[12] = ""; char min_width_buffer[12] = "", min_height_buffer[12] = "";
char max_width_buffer[12] = "", max_height_buffer[12] = ""; char max_width_buffer[12] = "", max_height_buffer[12] = "";
int may_close = true; int may_close = true;
char window_title[64] = "";
if (!glfwInit()) if (!glfwInit())
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
@ -71,7 +72,7 @@ int main(int argc, char** argv)
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
GLFWwindow* window = glfwCreateWindow(600, 630, "Window Features", NULL, NULL); GLFWwindow* window = glfwCreateWindow(600, 660, "Window Features", NULL, NULL);
if (!window) if (!window)
{ {
glfwTerminate(); glfwTerminate();
@ -109,6 +110,11 @@ int main(int argc, char** argv)
nk_glfw3_font_stash_begin(&atlas); nk_glfw3_font_stash_begin(&atlas);
nk_glfw3_font_stash_end(); nk_glfw3_font_stash_end();
// test setting title with result from glfwGetWindowTitle
glfwSetWindowTitle(window, glfwGetWindowTitle(window));
strncpy( window_title, glfwGetWindowTitle(window), sizeof(window_title));
while (!(may_close && glfwWindowShouldClose(window))) while (!(may_close && glfwWindowShouldClose(window)))
{ {
int width, height; int width, height;
@ -188,11 +194,25 @@ int main(int argc, char** argv)
nk_label(nk, "Press Enter in a text field to set value", NK_TEXT_CENTERED); nk_label(nk, "Press Enter in a text field to set value", NK_TEXT_CENTERED);
nk_flags events; nk_flags events;
const nk_flags flags = NK_EDIT_FIELD | const nk_flags flags = NK_EDIT_FIELD |
NK_EDIT_SIG_ENTER | NK_EDIT_SIG_ENTER |
NK_EDIT_GOTO_END_ON_ACTIVATE; NK_EDIT_GOTO_END_ON_ACTIVATE;
nk_layout_row_dynamic(nk, 30, 2);
nk_label(nk, "Window Title:", NK_TEXT_LEFT);
events = nk_edit_string_zero_terminated( nk, flags, window_title, sizeof(window_title), NULL );
if (events & NK_EDIT_COMMITED)
{
glfwSetWindowTitle(window, window_title);
// we do not need to call glfwGetWindowTitle as we already store the title, but using it here for testing purposes
strncpy( window_title, glfwGetWindowTitle(window), sizeof(window_title));
}
if (position_supported) if (position_supported)
{ {
int xpos, ypos; int xpos, ypos;