From 95a42df7a459fb089d2d55b52c7c84f2a296c0f4 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 11 Nov 2015 15:41:12 -0500 Subject: Add API to set window class name --- pugl/pugl.h | 6 ++++++ pugl/pugl_internal.h | 8 ++++++++ pugl/pugl_win.cpp | 34 ++++++++++++++-------------------- 3 files changed, 28 insertions(+), 20 deletions(-) (limited to 'pugl') diff --git a/pugl/pugl.h b/pugl/pugl.h index 074297c..4d5467a 100644 --- a/pugl/pugl.h +++ b/pugl/pugl.h @@ -152,6 +152,12 @@ typedef void (*PuglSpecialFunc)(PuglView* view, bool press, PuglKey key); PUGL_API PuglView* puglInit(int* pargc, char** argv); +/** + Set the window class name before creating a window. +*/ +PUGL_API void +puglInitWindowClass(PuglView* view, const char* name); + /** Set the parent window before creating a window (for embedding). */ diff --git a/pugl/pugl_internal.h b/pugl/pugl_internal.h index 1006c90..7dc9cfa 100644 --- a/pugl/pugl_internal.h +++ b/pugl/pugl_internal.h @@ -47,6 +47,7 @@ struct PuglViewImpl { PuglInternals* impl; + char* windowClass; PuglNativeWindow parent; PuglContextType ctx_type; uintptr_t transient_parent; @@ -116,6 +117,13 @@ puglInitWindowAspectRatio(PuglView* view, view->max_aspect_y = max_y; } +void +puglInitWindowClass(PuglView* view, const char* name) +{ + free(view->windowClass); + view->windowClass = strdup(name); +} + void puglInitWindowParent(PuglView* view, PuglNativeWindow parent) { diff --git a/pugl/pugl_win.cpp b/pugl/pugl_win.cpp index 849decb..251dd9b 100644 --- a/pugl/pugl_win.cpp +++ b/pugl/pugl_win.cpp @@ -112,31 +112,25 @@ puglLeaveContext(PuglView* view, bool flush) int puglCreateWindow(PuglView* view, const char* title) { + static const TCHAR* DEFAULT_CLASSNAME = "Pugl"; + PuglInternals* impl = view->impl; if (!title) { title = "Window"; } - // FIXME: This is nasty, and pugl should not have static anything. - // Should class be a parameter? Does this make sense on other platforms? - static int wc_count = 0; - char classNameBuf[256]; - _snprintf(classNameBuf, sizeof(classNameBuf), "x%d%s", wc_count++, title); - classNameBuf[sizeof(classNameBuf) - 1] = '\0'; - - impl->wc.style = CS_OWNDC; - impl->wc.lpfnWndProc = wndProc; - impl->wc.cbClsExtra = 0; - impl->wc.cbWndExtra = 0; - impl->wc.hInstance = 0; - impl->wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); - impl->wc.hCursor = LoadCursor(NULL, IDC_ARROW); - impl->wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); - impl->wc.lpszMenuName = NULL; - impl->wc.lpszClassName = strdup(classNameBuf); - - if (!RegisterClass(&impl->wc)) { + WNDCLASSEX wc; + memset(&wc, 0, sizeof(wc)); + wc.cbSize = sizeof(wc); + wc.style = CS_OWNDC; + wc.lpfnWndProc = wndProc; + wc.hInstance = GetModuleHandle(NULL); + wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); // TODO: user-specified icon + wc.hCursor = LoadCursor(NULL, IDC_ARROW); + wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); + wc.lpszClassName = view->windowClass ? view->windowClass : DEFAULT_CLASSNAME; + if (!RegisterClassEx(&wc)) { free((void*)impl->wc.lpszClassName); free(impl); free(view); @@ -161,7 +155,7 @@ puglCreateWindow(PuglView* view, const char* title) impl->hwnd = CreateWindowEx( WS_EX_TOPMOST, - classNameBuf, title, + wc.lpszClassName, title, (view->parent ? WS_CHILD : winFlags), CW_USEDEFAULT, CW_USEDEFAULT, wr.right-wr.left, wr.bottom-wr.top, (HWND)view->parent, NULL, NULL, NULL); -- cgit v1.2.1