Thursday, January 27, 2011

C, Meet the Haiku GUI

It's official: after a couple of hours of furious code, I have written a graphical Haiku application from C,  courtesy of libcharlemagne. No, not C++ and there is none of that fancy application scripting via BMessage and hey, either. The title? CHaikuRun, of course.

With mappings from C++ to C being pretty ugly, the code won't exactly win elegance contests. Also, there may be some C-to-HTML ickiness here, so bear that in mind.

#include "libcharlemagne.h"
#include <stdio.h>

int32_t InitApp(void *pobject, PArgList *foo_in, PArgList *foo_out)
    /*    This accomplishes the same kinds of tasks as what would
        normally be done in a BApplication's child class constructor */
    void *mainwin = pobject_create("PWindow");
    pdata_set_bool_property(mainwin, "Visible", 0);
    pdata_set_string_property(mainwin, "Title", "CHaikuRun");
    pdata_set_rect_property(mainwin, "Frame", 100, 100, 500, 400);
    pdata_set_bool_property(mainwin, "Visible", 1);
    pdata_set_int_property(mainwin, "Flags", 0x00180000);
    /* Here is an example of how to run a method for an object in C*/
    void *backview = pobject_create("PView");
    pdata_set_rect_property(backview, "Frame", 0, 0, 400, 300);
    pdata_set_color_property(backview, "BackColor", 224, 224, 224, 255);
    /* These have to exist for running a method, but they can be reused */
    PArgList *in = create_parglist();
    PArgList *out = create_parglist();
    /* AddChild requires the object ID of the child view to add */
    add_parg_int64(in, "id", pobject_get_id(backview));
    pobject_run_method(mainwin, "AddChild", in, out);
    void *label = pobject_create("PLabel");
    pdata_set_rect_property(label, "Frame", 10, 10, 350, 60);
    pdata_set_string_property(label, "Text", "Hello Haiku from the C language!");
    add_parg_int64(in, "id", pobject_get_id(label));
    pobject_run_method(backview, "AddChild", in, out);
    /* Free allocated heap memory */
    return B_OK;

    /* Start up the object system. Without this, we can't do anything with libcharlemagne */
    void *papp = pobject_create("PApplication");
    run_app(papp, "application/x-vnd.dw-CHaikuRun", InitApp);
    /* Not absolutely necessary here, but it frees all objects allocated by the broker */
    return 0;


  1. Its not that uggly when you get it into classes etc =) But isnt the BApplication complete yet? ... Events like "ReadyToRun" on application doesnt seem to be called? =)

    i use pobject_connect_event(fApp, "ReadyToRun", &myReadyToRunFunc); but nothing happends? :)

  2. Oops. :D Bleeding edge development... you gotta love it. I must have missed implementing PApplication's event code last night. Consider that next on my to-do list and keep an eye on the repository.