Page 1 of 1

FAQ for Platform-specific issues and WX

Posted: Fri Feb 10, 2006 3:39 am
by Ryan Norton
How do I get the native window handle for my wxWindow or derivative?

This is an easy one - each wxWindow on each platform implements GetHandle().

Here is how each platform handles the GetHandle() call:
  • Windows - HWND
  • Mac - ControlRef (MacGetTopLevelWindowRef Gets the WindowRef)
  • GTK - GtkWidget (GTK_PIZZA(GetHandle())->bin_window gets the GtkWindow

Posted: Fri Feb 10, 2006 3:41 am
by Ryan Norton
How do I SET the native window handle of a wxWindow?

Now this is not as easily done, unless you're on windows.

Here's how to do it:
  • Windows - wxWindow::AssociateHandle (easy case)
  • wxMac:
    Derive, use m_macWindow member for top level windows - likely to require a
    bit of messing around to work right.

    Controls are more straightforward - dispose of the handle through m_peer

    Code: Select all

    if (m_peer && m_peer->Ok() )
    then send m_peer->GetControlRefAddr() to one of the control creation
    functions or assign it etc.
  • wxGTK:
    Most complex case. Difficult to explain. involves messing around with
    GTK_PIZZA(m_wxwindow) (and that's for controls... no idea on top levels -
    maybe its the same there - look at the gtk sources for comments at the top
    of the files which explain it for the most part).

Posted: Fri Feb 10, 2006 3:45 am
by Ryan Norton
How do I get the XWindow ID of my wxGTK wxWindow?

Code: Select all

//m_ctrl is the wxWindow (pretty sure this will work for wxFrame as well)
#ifdef __WXGTK__
#    include "wx/gtk/win_gtk.h"     //for <gdk/gdkx.h>/GDK_WINDOW_XWINDOW

GdkWindow *window = GTK_PIZZA(m_ctrl->m_wxwindow)->bin_window;

//This line contains the real Xid
The problem is that in order to get a valid xid your window needs to be
realized first, and if its not then you need to connect to the realize
event and handle it there.

Code: Select all

        //Not realized yet - set to connect at realization time
        g_signal_connect (m_ctrl->m_wxwindow,
                          G_CALLBACK (gtk_window_realize_callback),
Where gtk_window_realize_callback is something like

Code: Select all

extern "C" {
static gint gtk_window_realize_callback(GtkWidget* theWidget,
					MyClass* myc)
    GdkWindow *window = GTK_PIZZA(theWidget)->bin_window;
    //Now we have a good xid for sure
    GDK_WINDOW_XWINDOW( window )
    return 0;
(well you can use the same one you get early but if you use it before the window is realized the X server quits your program for you with an annoying error message informing you that you probably have a bug).

Edit by DavidHart: For an update, including wx2.9, see this post