Thursday, February 18, 2010

gnocl::drawingArea - Dynamic Allocation of Widget Options

The gnocl::drawingArea as a base widget is pretty useless yet it actually forms the basis of many custom Gtk widgets created in C. Part of developing the gnocl::pixBuf code is writing to the gnocl::drawArea and so now is as good as any time to add some features to it. But what? Let the scripter decide! The basic widget class has no default signals or properties so this is the starting point. Let the scripter decide. Once the basic had been set up the task was relatively easy. The snippet below from the test code show how event handling works.

set da1 [gnocl::drawingArea  -tooltip "HELLO" ]

$da1 option add -onMotion
$da1 option add -onEnter
$da1 option add -onButtonPress
$da1 option add -onButtonRelease
$da1 option add -onButtonMotion

$da1 configure -onMotion {puts "%x %y WHEE..."}
$da1 configure -onEnter {puts "%x %y Hello Moto"}
$da1 configure -onButtonPress {puts "%x %y Press.."}
$da1 configure -onButtonRelease {puts "%x %y Release.."}
$da1 configure -onButtonMotion {puts "%x %y MOOOOOOOOOOOOOOOOOTION.."}

Gnocl now does make some additional defaults of its own to make the code implementation harmoize with the other widgets. This show hows its done:

static gint _n = 4; /* counter,number of implements widget options */

static GnoclOption drawingAreaOptions[] =
    /* common widget options */
    { "-tooltip", GNOCL_OBJ, "", gnoclOptTooltip },
    { "-data", GNOCL_OBJ, "", gnoclOptData },
    { "-name", GNOCL_STRING, "name" },
    { "-onShowHelp", GNOCL_OBJ, "", gnoclOptOnShowHelp },
    /* widget specific options */
    { "", 0, "", NULL },
    { "", 0, "", NULL },
    { "", 0, "", NULL },
    { "", 0, "", NULL },
    { NULL },

As can be seen, the options array is statically defined with some extra spaces for run-time additions. At the moment there are only a few blanks, but this can modified later. Initially I thought of dyanamically reallocating memory for the array but this simple method seems the most straightforward solution.

No comments: