-1

I'm loading wxWidgets assets in a PHP program using wxXmlResource(). This loads the window very well, but I am not sure how to get an object reference to named window elements.

Here is my code:

// Load the window
$resource = new wxXmlResource();
$resource->InitAllHandlers();
$resource->Load(__DIR__ . '/forms.xrc.xml');

// Get a reference to a window
$frame = new wxFrame();
$resource->LoadFrame($frame, NULL, 'frmOne');
$frame->Show();

// Fetch a named element - the class returned is the base class
$textCtrl = $frame->FindWindow('m_staticText12');
echo get_class($textCtrl) . "\n";

The $textCtrl item should be a wxStaticText object but it has been returned as a wxWindow (which is a parent class). Since it is not cast as the correct object type, I can't call methods belonging to the control's own class (such as Wrap()).

I think the FindWindow() call is working, since if I get the name deliberately wrong, it returns null.

What am I doing wrong?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

1

You will need to use the wxDynamicCast function to cast object to proper type like:

$textCtrl = wxDynamicCast(
    $frame->FindWindow('m_staticText12'), 
    "wxStaticText"
);
JGM
  • 38
  • 5
  • Excellent, thanks! I will try that and if it works send you another PR - this time a simple demo for `wxXmlResource`. Incidentally this seems like a good way to create frame assets - GTK doesn't emit any warnings at all. – halfer Dec 18 '15 at 09:42
  • I've tried this and can confirm this works. Is this approach universal for all wxWidgets wrappers (e.g. wxPython), do you know? It seems rather unexpected for `FindWindow()` to return the base class and not an instance-specific type. – halfer Dec 18 '15 at 21:02
  • Since C++ isn't a dynamic language all that FindWindow() could return is a void* or a parent class pointer from which all child's inherit. In this case, since wxWindow inherits from wxObject, it should be possible to determine the object type by using wxObject methods. – JGM Dec 19 '15 at 18:14
  • Alright, a C++ limitation - fair enough. Thank you again! – halfer Dec 20 '15 at 21:34