FAQ for Platform-specific issues and WX

Do you have a typical platform dependent issue you're battling with ? Ask it here. Make sure you mention your platform, compiler, and wxWidgets version.
Post Reply
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

FAQ for Platform-specific issues and WX

Post 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
Last edited by Ryan Norton on Fri Feb 10, 2006 3:43 am, edited 1 time in total.
[Mostly retired moderator, still check in to clean up some stuff]
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post 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() )
            m_peer->Dispose();
    
    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).
Last edited by Ryan Norton on Sun Jul 23, 2006 4:51 am, edited 1 time in total.
[Mostly retired moderator, still check in to clean up some stuff]
User avatar
Ryan Norton
wxWorld Domination!
wxWorld Domination!
Posts: 1319
Joined: Mon Aug 30, 2004 6:01 pm

Post 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
#endif

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

//This line contains the real Xid
GDK_WINDOW_XWINDOW( window )
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

if(!GTK_WIDGET_REALIZED(m_ctrl->m_wxwindow))
    {
        //Not realized yet - set to connect at realization time
        g_signal_connect (m_ctrl->m_wxwindow,
                          "realize",
                          G_CALLBACK (gtk_window_realize_callback),
                          this);
    }
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
[Mostly retired moderator, still check in to clean up some stuff]
Post Reply