For GTK development team (Dynamic label support)



To GIMP development team:

It would be nice if GTK+ supported dynamic label binding much like X
does with its resource files.  This would allow for multilingual support
without needing to rework any code.

What I can propose as a work around for now is a little bit of reworking
in the gtklabel files.  Some sort of token would need to represent the
label.  Personally I like '$label' which could then be decoded by a
hooked decode function which could do any other funky stuff you might
want to do dynamically.

    Ex:    If your label is:    $label=labelInTheResourceFile
    We would then go looking up in the resource file
'labelInTheResourceFile' and associate the corresponding label
    entry:

    Ex.    label labelInTheResourceFile "Label in the resource file"

Why the hooked function?  Well if the GtkLabel struct has a member to a
function pointer for decoding we could manually alter labels
dynamically.  So you could probably create new keywords to your own
decode functions.

    Ex.    If your label is:    $hostname=205.205.100.32
    And your hooked function is:    void getHostName(gchar *str)

The each time the expose function is called, it analyses the label and
sees that it is not an explicit label nor a reference to a label
contained in a resource file.  It therefore calls the hooked function
which subsequently calls getHostByName with the string.

To resume:

    Add a function to set the hooked (externally defined) decode
function which is by default NULL.
        Ex: gtk_label_set_decode_function(gchar *decode_function);

    Add storage for the pointer to the function.
        Ex. void *pDecodeFunction;

    Add storage for the original string...
        Ex. gchar *srcLabel;

    Add a check in the expose function for the "$??=" check and extract
the name of the token.
        Ex. $label=... or $hostname=... or $data=...

For $label perform a check directly into the resource file... (Which
should be stored somewhere with the styles in memory so that everything
is the same)  then do a something like:

        label->label = getFromResourceFile("labelInResourceFile");    //
Looks up in memory the already stored char string

    For any other token call hooked decode function:

        label->label = *decodeFunction("hostname");    // Obviously
replace by a variable not a constant...

And voila you should be able to have dynamic binding of labels...  This
is something for you developers to seriously look at.  I would code this
if only I had the time... <damn>

File view:

In gtklabel.h:
...
struct _GtkLabel
{
  GtkMisc misc;

  gchar    *label;
  gchar    *src_label;
  GdkWChar *label_wc;
  gchar    *pattern;
  gchar    *decode_function;

  GtkLabelWord *words;

  guint   max_width : 16;
  guint   jtype : 2;
  gboolean wrap;
};
...
void gtk_label_set_decode_function(const gchar *func);    // Can be NULL

In gtklabel.c:
...
void gtk_label_set_decode_function(const gchar *func)
{
    decode_function = func;
}
...
static gint
gtk_label_expose (GtkWidget      *widget, GdkEventExpose *event)
{
...
    label->srcLabel = label->label;

    strToken = gtk_is_token(label->label);    // If a token is found,
label->label is what is found after $token=
    if (strToken != '\0')
    {
        if (strcmp(strToken, "label") == 0)
            label->label = getFromResourceFile(label->label);
        else
            label->label = label->decode_function(strToken,
label->label);    // Must be defined in your source code...
    }
    // do the normal processing...
...
}

There are things missing and I know...  But like I said I don't have
time to write this code...  If I did I would not submit this request I
would finish off this brief 'pseudocode'.

Missing points:

    -Loading the strings from the resource file (const gchar
*getFromResourceFile(const gchar *str))
    -Parsing the token and label (const gchar *gtk_is_token(gchar *str))

    -Some initialisation of added members (decode_function and
src_label)
    -Testing and debugging (obviously...)

Anyways keep this in mind.  This would be an excellent bit of added
flexibility and functionality.  You also may want to opt for something
more like X Windows and Motif do:

    grandparent.parent.childname=Label in the resource file

But my structure adheres more to the current GTK+ resource format and is
more flexible for user defined functions and data manipulations.

Long live dynamic labelling...  Happy coding code gurus...


Kristopher Kycia





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]