Add GetHWND() to wxWindowGTK when on Windows (PR #24675)

13 views
Skip to first unread message

Ryan Ogurek

unread,
Jul 3, 2024, 7:55:18 PM (2 days ago) Jul 3
to wx-...@googlegroups.com, Subscribed

As the title says, when using wxGTK on Windows there's not a way (that I can tell) to get an HWND from a wxWindow. This uses a couple of gdk and gtk helpers to get the HWND from the underlying GtkWidget.


You can view, comment on, or merge this pull request online at:

  https://github.com/wxWidgets/wxWidgets/pull/24675

Commit Summary

  • 857e5e9 Add GetHWND() to wxWindowGTK when on Windows

File Changes

(2 files)

Patch Links:


Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675@github.com>

Ryan Ogurek

unread,
Jul 3, 2024, 8:00:12 PM (2 days ago) Jul 3
to wx-...@googlegroups.com, Subscribed

To give some context as to why I want this (although I'm sure there's plenty of other uses), in order to set the title bar to be dark when GTK_CSD=0 (And I always turn CSD off... it's awful under windows), the window's HWND is required:

#ifdef __WINDOWS__
    DWORD useDarkMode{Misc::getTheme() == Misc::Theme::DARK_MODE};
    DwmSetWindowAttribute(
            GetHWND(),
            DWMWINDOWATTRIBUTE::DWMWA_USE_IMMERSIVE_DARK_MODE,
            &useDarkMode,
            sizeof(DWORD)
            );
#endif


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/c2207572149@github.com>

VZ

unread,
Jul 4, 2024, 7:47:38 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

@vadz commented on this pull request.

Thanks, I don't think there is a problem with adding such a function but I'm not sure about the name and it should also be documented in interface/wx/window.h if we mean it to be part of the public API now.


In src/gtk/window.cpp:

> @@ -4240,6 +4243,14 @@ bool wxWindowGTK::GTKShowFromOnIdle()
     return false;
 }
 
+#ifdef __WINDOWS__
+WXHWND wxWindowGTK::GetHWND() const {

Very minor, but wx style is to put braces on their own lines, i.e.

⬇️ Suggested change
-WXHWND wxWindowGTK::GetHWND() const {
+WXHWND wxWindowGTK::GetHWND() const
+{

In include/wx/gtk/window.h:

> @@ -139,6 +139,9 @@ class WXDLLIMPEXP_CORE wxWindowGTK : public wxWindowBase
     // --------------
 
     virtual WXWidget GetHandle() const override { return m_widget; }
+#ifdef __WINDOWS__
+    WXHWND GetHWND() const;

I'm not sure if it's a good or bad idea to use the same name as for the existing function in wxMSW. On one hand, this would allow referring to it without checking for __WXGTK__ but OTOH do we really plan to have much code using either wxMSW or wxGTK and having to manipulate the underlying HWND directly?

Perhaps we should call this GTKGetWin32Handle() instead? Granted, this is much longer but this shouldn't be used very often, hopefully.


In src/gtk/window.cpp:

> +    // There isn't an underlying HWND if the widget hasn't been realized yet.
+    gtk_widget_realize(m_widget);

This is more serious, IMO it's really unexpected for an accessor like this to have significant side effects.

Would it be a problem to just call this function only if the window had been already realized?


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/review/2159567990@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 7:55:52 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Push

@ryancog pushed 1 commit.

  • c3e5f31 Fix wxWindowGTK::GetHWND() styling


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/before/857e5e944fcd60148f80b27beefc66f9bf9b4c4d/after/c3e5f31386161080bb29466e760e74ddb31be0b9@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 7:56:30 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

@ryancog commented on this pull request.


In src/gtk/window.cpp:

> @@ -4240,6 +4243,14 @@ bool wxWindowGTK::GTKShowFromOnIdle()
     return false;
 }
 
+#ifdef __WINDOWS__
+WXHWND wxWindowGTK::GetHWND() const {

Whoops, my bad... I didn't realize the difference in styling it seems.

Fixed


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/review/2159572169@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 8:00:19 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

@ryancog commented on this pull request.


In include/wx/gtk/window.h:

> @@ -139,6 +139,9 @@ class WXDLLIMPEXP_CORE wxWindowGTK : public wxWindowBase
     // --------------
 
     virtual WXWidget GetHandle() const override { return m_widget; }
+#ifdef __WINDOWS__
+    WXHWND GetHWND() const;

but OTOH do we really plan to have much code using either wxMSW or wxGTK and having to manipulate the underlying HWND directly?

I can't imagine so.

Perhaps we should call this GTKGetWin32Handle() instead? Granted, this is much longer but this shouldn't be used very often, hopefully.

Making it a different, distinguishable name makes sense to me.

Mostly out of curiousity, what's the logic stylistically behind prefixing it with GTK?
Since there's other wxWindowGTK functions that aren't...


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/review/2159573337@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 8:02:23 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

@ryancog commented on this pull request.


In src/gtk/window.cpp:

> +    // There isn't an underlying HWND if the widget hasn't been realized yet.
+    gtk_widget_realize(m_widget);

Would need to add an extra check on the first gtk function, since it'll return null if not (and passing that to gdk will cause bad things), but that's easy enough.

However, that'd require a way (for my use case at least) to be able to realize the GTK Window through wxWidgets. I'll see if there's a function for that already. If not, I'd want to add one.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/review/2159574045@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 8:12:04 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

@ryancog commented on this pull request.


In src/gtk/window.cpp:

> +    // There isn't an underlying HWND if the widget hasn't been realized yet.
+    gtk_widget_realize(m_widget);

I don't see a method to "force" realize a gtk window.

Are you aware if there's something like this that exists and/or what sort of side effects would need to be considered by creating another function (GTKRealizeWidget() for example) to do this?


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/review/2159578088@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 8:20:43 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Subscribed

it should also be documented in interface/wx/window.h if we mean it to be part of the public API now.

Should it go in interface/wx/window.h or into a gtk specific one? That file currently seems to be entirely platform-agnostic functions, so I'm not sure how to go about adding that documentation.


Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/c2209662223@github.com>

Ryan Ogurek

unread,
Jul 4, 2024, 8:36:48 PM (13 hours ago) Jul 4
to wx-...@googlegroups.com, Push

@ryancog pushed 1 commit.

  • ce10085 Rename wxWindowGTK::GetHWND() to GTKGetWin32Handle(), add GTKRealizeWidget()


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <wxWidgets/wxWidgets/pull/24675/before/c3e5f31386161080bb29466e760e74ddb31be0b9/after/ce10085e5b6712d533a7101d8ecbcbda44218ae4@github.com>

Reply all
Reply to author
Forward
0 new messages