Desktop applications with Haxe and Waxe – Part 8: Adding a MessageDialog

Posted on October 19, 2012

8


In Waxe version 1.0.1, there’s no MessageDialog implementation, so we have to create one ourselves. This requires that we have the wxWidgets-2.9.3 source code and that we have compiled the needed obj files from that. (I’ll describe that process later, in part 7…)

In short, this involves the following steps:

  1. Adding the wxMessageDialog implementation to the cpp code
  2. Compiling a new waxw.ndll, including the wx_message_dialog_show function as a result of above.
  3. Creating the wx.MessageDialog.hx class
  4. Use it in your applications!

So, here we go!

Step 1. Adding the c++ code

When trying to figure out how to implement the MessageDialog, i found that the wxFileDialog and wxDirDialog implemetations both reside in the haxe/lib/waxe/1,0,1/src/waxe/Dialog.cpp file. As they both extend the Dialog class, and so does the MessageDialog, I choose to follow that example and put the MessageDialog code there as well.

This is what I added to the end of the Dialog.cpp file:

//-------------------------------------------------------------------------------------------------------------------
// wxMessageDialog implemetation, added to /haxe/lib/waxe/1.0.1/src/waxe/Dialog.cpp
value wx_message_dialog_show(value ioData)
{
    wxWindow *parent = 0;
    wxString message;
    wxString caption;
    int style;

    ValueToWX(val_field(ioData,val_id("parent")),parent);
    ValueToWX(val_field(ioData,val_id("message")),message);
    ValueToWX(val_field(ioData,val_id("caption")),caption);
    ValueToWX(val_field(ioData,val_id("style")),style);

    wxMessageDialog *dlg = new wxMessageDialog(parent,message, caption, style);
    int result = dlg->ShowModal();

    dlg->Destroy();

   return alloc_int(result);
}

DEFINE_PRIM(wx_message_dialog_show,1)

Step 2. Compiling a new waxe.ndll

NOTE: This requres that you have already built the needed obj files from wxWidgets-2.9.3 source code!

Start by making a backup of your waxe.ndll file (typically haxe/lib/waxe/1.0.1/ndll/Windows/waxe.ndll). We’re going to replace this one in the following. Then open a command window in the haxe/lib/waxe/1.0.1/src directory, and run the following command:

haxelib run hxcpp Build.xml

This should build a new waxe.ndll with the included wx_message_dialog_show function that we added in step 1.

Step 3. Creating the haxe code

Create a new haxe source code file named MessageDialog.hx in the haxe/lib/waxe/1,0,1/wx directory:


// haxe/lib/waxe/1.0.1/wx/MessageDialog.hx

package wx;

import wx.Window;

class MessageDialog
{
    public var parent:Dynamic;
    public var message:String;
    public var caption:String;
    public var style:Int;

    public static inline var OK             = 0x00000004;
    public static inline var YES         = 0x00000002;
    public static inline var NO             = 0x00000008;
    public static inline var CANCEL    = 0x00000010;
    public static inline var YES_NO    = YES | NO;

    public static inline var ICON_WARNING          = 0x00000100;
    public static inline var ICON_ERROR               = 0x00000200;
    public static inline var ICON_QUESTION        = 0x00000400;
    public static inline var ICON_INFORMATION    = 0x00000800;

    public function new (
        inParent:Window = null,
        inMessage:String = "Message",
        inCaption:String = "Caption",
        inStyle:Int = OK
    )

    {
        this.parent = (inParent==null) ? null : inParent.wxHandle;
        this.message = inMessage;
        this.caption = inCaption;
        this.style = inStyle;
    }

    public function showModal() : Int
    {
        return wx_message_dialog_show(this);
    }

    static var wx_message_dialog_show = Loader.load("wx_message_dialog_show", 1);

}

I found the values for the constants in the onlined docs for wxWidgets, on this page: http://docs.wxwidgets.org/trunk/defs_8h.html

Step 4. Add dialogs to your application

So, now you should be able to add a message dialog to your Waxe application.

The following gives a simple dialog with the default settings – an info icon and one OK button:


new MessageDialog(null, 'Hello world!', 'Message caption').showModal();

By combining the MessageDialog constants we can combine button alternatives…

  • OK
  • CANCEL
  • YES
  • NO
  • YES_NO

…and icon alternatives:

  • WARNING
  • ERROR
  • QUESTION
  • INFORMATION

By setting the style parameter to MessBelow we get a Yes/No dialog box with a warning icon. The result is an int that we can filter logically to find out wether yes or no was answered:

var style = MessageDialog.YES_NO | MessageDialog.ICON_WARNING;
var result = new MessageDialog(null, 'Important question: Does the pope wear a nice hat?', 'Message caption', style).showModal();

if ((result & MessageDialog.YES) == MessageDialog.YES) {
trace('Yes');
} else {
trace('No');
}

Please note that this is only a basic implementation of the wxMessageDialog. There are other constructor parameters to use (position etc), but this suits my needs for now!

Cheers!

Advertisements
Posted in: Haxe, Waxe