Desktop applications with Haxe and Waxe – Part 3: Adding a missing event handler

Posted on May 19, 2012

0


In part 2, we created a simple HelloWaxe application with a static text field and a quit button. Now, let’s handle some text dynamically.

We start by adding a text control (wx.TextCtrl) to our HelloWaxe.hx. Now it should look something like this:

import wx.Button;
import wx.Frame;
import wx.Panel;
import wx.StaticText;
import wx.TextCtrl;

class HelloWaxe {

    public function new() {
        var frame = ApplicationMain.frame;
        var panel = Panel.create(frame);
        var staticText = StaticText.create(panel, null, 'Hello Waxe!', { x:20, y:10 } );

        var btnQuit = Button.create(panel, null, 'Quit', { x:500, y:320 } );
        btnQuit.onClick = function(_) { wx.App.quit(); }

        var textCtrl = TextCtrl.create(panel, null, 'Hello World', {x:20, y:80}, {width:200, height:22});
    }

    static public function main() {
        new HelloWaxe();
    }
}

Compiling and running should give the following app:

What about some interaction with the textCtrl? Let’s say that we want to change the staticText label to reflect the text of our textCtrl whenever it changes. Hmm… We have three event handlers for the textCtrl to use…

  • onClose
  • onPaint
  • onSize

…but none of them takes care of our problem. We should have something like a onChange event method, or equivalent.

Time for some exploration. It seems that the wx.ComboBox component has what we’re looking for: a onTextUpdated event handler. Let’s copy it an insert it in wx.TextCtrl! Open wx.TextCtrl and add the following lines at the end, just befor the closing brace:

//-----------------------------------------------------------------------------------------------------
// The following lines are copied from wx.ComboBox
public var onTextUpdated(null, setOnTextUpdated):Dynamic->Void;
function setOnTextUpdated(f:Dynamic->Void) {setHandler(wx.EventID.COMMAND_TEXT_UPDATED,f); return f;}

Now, it should be possible to add the following handler to the textCtrl:

    textCtrl.onTextUpdated = function(_) {
        staticText.label = textCtrl.getValue();
    }

After compiling and running, whenever changing the textCtrl text, the staticText should be displaying the same!
Please keep in mind that we now have altered the waxe code, and further haxelib upgrades will install new version that most probably will break this!

Anyway, we have hacked some useful functionality into our waxe application!

About these ads
Posted in: Haxe, Waxe