GtkD: New widget

Mike Wey via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Aug 21 13:54:04 PDT 2017


On 21-08-17 03:45, Johnson Jones wrote:
> Hey Mike, I bet you can answer this!
> 
> I'd like to extend a widget to add some functionality.
> 
> class MyBox : Box
> {
>      protected GtkBox* gtkBox;
> 
>      import std.typecons;
>      _gtk.Box Wrapped;
>      mixin Proxy!Wrapped;
> 
>      public this(Box b)
>      {
>          this.gtkBox = b.getBoxStruct();
>          super(gtkBox, false);
>      }
> 
> }
> 
> Trying something like the above does extend the box, as far as allowing 
> one to replace it, I think(using the code);
> 
> auto b = new MyBox(W1);
> auto p = W1.getParent();
> auto c = cast(Box)W4;
> c.remove(W1);
> c.add(b);
> 
> So, W4 is the main boxx, W1 is the box inside the main box I replaced 
> with the new box b.
> 
> When running that code, nothing changes, which, assuming we are actually 
> using the new box, then that is fine.
> 
> But I'm pretty sure that gtk never has a clue about `MyBox`? I say this 
> because I'd like to simply modify the reported sizes of the box.
> 
> A gtkBox is not the same as a gtk.Box.
> 
> It seems like the best I can do is use a gtk.Container and inherit from 
> that.
> 
> 
> e.g.,
> 
> class FixableSizedBox : Container
> {
>      protected GtkContainer* gtkContainer;
> 
>      import std.typecons;
>      _gtk.Container Wrapped;
>      mixin Proxy!Wrapped;
> 
>      public this(Container b)
>      {
>          this.gtkContainer = b.getContainerStruct();
>          super(gtkContainer, false);
>      }
> 
> }
> 
> But even the GtkD container doesn't seem to contain any code to deal 
> with handling the sizes.
> 
> 
> All I'm really looking to do is set the size of a container to whatever 
> I want.
> 

If you want gtk to know about the functions you override you could use 
gtkd.Implement.ImplementCLass.

It's only in master and not completely finished yet, but you could use 
it to for example overrride the getPreferredHeight and getPreferredWidth 
functions.

I'm not completely clear on what you want to do with the size so they 
might not be the correct functions to override.


```
class MyBox : Box
{
   import gtkd.Implement;
   import gobject.c.functions : g_object_newv;

   mixin ImplementClass!GtkBox;

   this()
   {
     //TODO: sort out the constructor.
     super(cast(GtkApplication*)g_object_newv(getType(), 0, null), true);

   }

   override public void getPreferredHeight(out int minimumHeight, out 
int naturalHeight)
   {
     //Set minimumHeight and naturalHeight.
   }

   override public void getPreferredWidth(out int minimumWidth, out int 
naturalWidth)
   {
     //Set minimumWidth and naturalWidth.
   }
}
```

-- 
Mike Wey


More information about the Digitalmars-d-learn mailing list