From 4c07a013d0943985f156101f1123c518da5fc590 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 4 Aug 2019 20:44:54 +0200 Subject: Add puglSetWindowTitle() --- pugl/detail/implementation.c | 2 +- pugl/detail/implementation.h | 3 +++ pugl/detail/mac.m | 18 ++++++++++++++++++ pugl/detail/types.h | 1 + pugl/detail/win.c | 20 ++++++++++++++++---- pugl/detail/x11.c | 20 ++++++++++++++++---- 6 files changed, 55 insertions(+), 9 deletions(-) (limited to 'pugl/detail') diff --git a/pugl/detail/implementation.c b/pugl/detail/implementation.c index 3cefa37..abfd654 100644 --- a/pugl/detail/implementation.c +++ b/pugl/detail/implementation.c @@ -25,7 +25,7 @@ #include #include -static void +void puglSetString(char** dest, const char* string) { const size_t len = strlen(string); diff --git a/pugl/detail/implementation.h b/pugl/detail/implementation.h index d6288a3..7f50ecf 100644 --- a/pugl/detail/implementation.h +++ b/pugl/detail/implementation.h @@ -30,6 +30,9 @@ extern "C" { #endif +/** Reallocate and set `*dest` to `string`. */ +void puglSetString(char** dest, const char* string); + /** Allocate and initialise world internals (implemented once per platform) */ PuglWorldInternals* puglInitWorldInternals(void); diff --git a/pugl/detail/mac.m b/pugl/detail/mac.m index 6d156c6..bf845fc 100644 --- a/pugl/detail/mac.m +++ b/pugl/detail/mac.m @@ -777,6 +777,7 @@ puglCreateWindow(PuglView* view, const char* title) view->minHeight)]; } impl->window = window; + puglSetWindowTitle(view, title); ((NSWindow*)window).delegate = [[PuglWindowDelegate alloc] initWithPuglWindow:window]; @@ -942,6 +943,23 @@ puglGetNativeWindow(PuglView* view) return (PuglNativeWindow)view->impl->wrapperView; } +PuglStatus +puglSetWindowTitle(PuglView* view, const char* title) +{ + puglSetString(&view->title, title); + + NSString* titleString = [[NSString alloc] + initWithBytes:title + length:strlen(title) + encoding:NSUTF8StringEncoding]; + + if (view->impl->window) { + [view->impl->window setTitle:titleString]; + } + + return PUGL_SUCCESS; +} + PuglStatus puglSetFrame(PuglView* view, const PuglRect frame) { diff --git a/pugl/detail/types.h b/pugl/detail/types.h index a413848..7c11f42 100644 --- a/pugl/detail/types.h +++ b/pugl/detail/types.h @@ -52,6 +52,7 @@ struct PuglViewImpl { PuglInternals* impl; PuglHandle handle; PuglEventFunc eventFunc; + char* title; PuglNativeWindow parent; uintptr_t transientParent; PuglHints hints; diff --git a/pugl/detail/win.c b/pugl/detail/win.c index efd6070..e761654 100644 --- a/pugl/detail/win.c +++ b/pugl/detail/win.c @@ -161,10 +161,8 @@ puglCreateWindow(PuglView* view, const char* title) return 3; } - wchar_t* wtitle = puglUtf8ToWideChar(title); - if (wtitle) { - SetWindowTextW(impl->hwnd, wtitle); - free(wtitle); + if (title) { + puglSetWindowTitle(view, title); } SetWindowLongPtr(impl->hwnd, GWLP_USERDATA, (LONG_PTR)view); @@ -775,6 +773,20 @@ puglGetNativeWindow(PuglView* view) return (PuglNativeWindow)view->impl->hwnd; } +PuglStatus +puglSetWindowTitle(PuglView* view, const char* title) +{ + puglSetString(&view->title, title); + + wchar_t* wtitle = puglUtf8ToWideChar(title); + if (wtitle) { + SetWindowTextW(view->impl->hwnd, wtitle); + free(wtitle); + } + + return PUGL_SUCCESS; +} + PuglStatus puglSetFrame(PuglView* view, const PuglRect frame) { diff --git a/pugl/detail/x11.c b/pugl/detail/x11.c index f000b02..e4da9d9 100644 --- a/pugl/detail/x11.c +++ b/pugl/detail/x11.c @@ -215,10 +215,7 @@ puglCreateWindow(PuglView* view, const char* title) XSetClassHint(display, win, &classHint); if (title) { - XStoreName(display, win, title); - XChangeProperty(display, win, atoms->NET_WM_NAME, - atoms->UTF8_STRING, 8, PropModeReplace, - (const uint8_t*)title, (int)strlen(title)); + puglSetWindowTitle(view, title); } if (!view->parent) { @@ -712,6 +709,21 @@ puglGetNativeWindow(PuglView* view) return (PuglNativeWindow)view->impl->win; } +PuglStatus +puglSetWindowTitle(PuglView* view, const char* title) +{ + Display* display = view->world->impl->display; + const PuglX11Atoms* const atoms = &view->world->impl->atoms; + + puglSetString(&view->title, title); + XStoreName(display, view->impl->win, title); + XChangeProperty(display, view->impl->win, atoms->NET_WM_NAME, + atoms->UTF8_STRING, 8, PropModeReplace, + (const uint8_t*)title, (int)strlen(title)); + + return PUGL_SUCCESS; +} + PuglStatus puglSetFrame(PuglView* view, const PuglRect frame) { -- cgit v1.2.1