Tips for creating widgets library

Hi, I’m creating a library of widgets to use in my app, and I would like to get some advice on how to construct is wisely. The widget I will handle are fairly simple: label, buttons, entries, menus and such. (no trees or documents)
For example, I would like to handle localization, unified look-and-feel and so on.
Are there other issues I should consider?
What would the best way to handle localization? I’m thinking to expose a SetText(int Id), instead of ustring, so that it will only allow text by Id, and to prepare .po files to be used by GNU’s gettext(). IS this a good approach?
Should I use GQuark for ID?

I fail a bit to know why you’d like to write “a library” first. Just write your app, and if splitting some widgets is really needed (for reusing in another application, for example), do it at this point… no? What do you have in mind exactly?

For visual and user interaction, give this a read : https://developer.gnome.org/hig/stable/

Thx for your reply. I was thinking of forcing unified look and localization through by deriving from the gtkmm widget classes. For example override setText() to except GQuark instead of string. I can’t find GQuark in gtkmm so I’m not sure if this is a good direction. I’m new to Gtk, some of the documentation I find baffles me.
Any suggestions are welcome.

If you just want a widget rendering like a GtkLabel, but only exposing methods that are taking a text id (be it a GQuark, a flag, or whatever), I’d suggest that you have a look to Gtk4 (not released yet, but it comes close), instead of Gtk3. It’s quite easier in it to have a custom GtkWidget that contains a private GtkLabel, allowing your code to do whatever with it without exposing its methods.

Untested Vala code:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <template class="MyLabel" parent="GtkWidget">
    <child>
      <object class="GtkLabel" id="private_label"/>
    </child>
  </template>
</interface>
[GtkTemplate (ui = "/org/example/myapp/ui/mylabel.ui")]
public class MyLabel : Gtk.Widget
{
    [GtkChild] private Gtk.Label private_label;

    construct
    {
        Gtk.BinLayout layout = new Gtk.BinLayout ();
        set_layout_manager (layout);
    }

    public void set_label (uint8 id)
    {
        switch (id)
        {
            /* Translators: a possible value of the label */
            case 0: private_label.set_text (_("My First Text")); break;
            /* Translators: a possible value of the label */
            case 1: private_label.set_text (_("My Other Text")); break;
            default: assert_not_reached ();
        }
    }
}

But I continue to think that you should first have fun writing your application, before abstracting its widgets in a library.

Thx for your reply, Gtk4 looks great but I can’t seem to find enough code examples yet, so I need to do some more thinking

And waiting for Gtk4, I’d suggest that you just subclass GtkLabel and add your methods. The transition will be quite easy that way.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.