Blog Post #79: Notebook, Part III - Customized Tabs, Part I

Antonio Corbi acrb at ggmail.com
Wed Oct 16 07:46:42 UTC 2019


On Tuesday, 15 October 2019 at 22:02:35 UTC, WebFreak001 wrote:
> On Tuesday, 15 October 2019 at 20:03:00 UTC, Ron Tarrant wrote:
...
>> Do you have links for these?
>
> thanks! :p
>
> both the packages can simply be found on dub: 
> https://code.dlang.org/search?q=glade
>
> Not sure if there are other ways like directly loading an XML 
> in GTK, haven't looked into it too much yet because I am not so 
> often building GTK GUI applications, but with the new Linux 
> Phones on the market (Librem 5, PinePhone) running GTK Apps 
> natively and really needing some Apps those will be great 
> platforms to start app development on.

Hi WebFreak001, Ron:

There's no need to generate code from glade files.
You can load at runtime the XML file that glade generates.

You have to create an instance of the Gtk.Builder class[1], and 
supply it
the XML file, i.e. from the file that you saved from glade[2] and 
after that you 'load' your UI controls into your program 
variables using getObject[3], a small snippet of this pattern:

---------------------------------------------------
    ....
    auto builder = new Builder();
    
if(!builder.addFromFile(buildPath(pkgdatadir,"ui/MainWindow.ui")))
    {
       writeln("Window ui-file cannot be found");
       return;
    }

    HeaderBar headerBar = cast(HeaderBar) 
builder.getObject("headerBar");
    Box windowContent = cast(Box) 
builder.getObject("windowContent");
    ...
---------------------------------------------------

Once I wrote this extremely simple class to simplify Builder 
usage:

---------------------------------------------------
module gtagui.uibuilder;

private import gobject.ObjectG;
import gtk.Builder;

class UiBuilder : Builder {
   this (string uif) {
     if (!addFromFile (uif))
       throw new Exception ("File not found: " ~ uif);
   }

   public T getObject(T) (string name) {
     return (cast(T) super.getObject (name));
   }
}
---------------------------------------------------

So you can now write things like this:

---------------------------------------------------
   public void loadUiFrom (string uifile) {
     uib = new UiBuilder (uifile);

     topbox = uib.getObject!Box ("box1");

     theCanvas = uib.getObject!DrawingArea("imgwindow");
     assert (theCanvas !is null);
---------------------------------------------------

Hope this helps.
Antonio

[1] https://api.gtkd.org/gtk.Builder.Builder.html
[2] https://api.gtkd.org/gtk.Builder.Builder.addFromFile.html
[3] https://api.gtkd.org/gtk.Builder.Builder.getObject.html


More information about the Digitalmars-d-learn mailing list