Skip to content

Instantly share code, notes, and snippets.

@kgilmer
Created January 30, 2022 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kgilmer/63fa35837161e6955e0b66af51df7e93 to your computer and use it in GitHub Desktop.
Save kgilmer/63fa35837161e6955e0b66af51df7e93 to your computer and use it in GitHub Desktop.
using Gtk;
// Creates a dialog style window with a static text block, dynamic list, and static button bar.
// Changes to the list cause the dialog to update its bounds accourding to the items.
public class Example : Window
{
public Example()
{
this.title = "ListBox";
this.set_default_size(200, 20);
this.destroy.connect(Gtk.main_quit);
Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
this.add(box);
box.pack_start (new Label("This demonstrates dynamic resizing\nafter adding/removing widgets"), false, false, 0);
var listbox = new ListBox();
box.pack_start (listbox, false, false, 0);
Widget[] items = new Widget[10];
int index = 0;
Gtk.Box bbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
box.pack_start (bbox, false, true, 0);
var addButton = new Button.with_label("Add");
bbox.pack_start(addButton, true, true, 0);
addButton.clicked.connect(() => {
var label1 = new Label("Label " + index.to_string());
listbox.insert(label1, -1);
label1.show_all();
items[index] = label1;
index++;
});
var removeButton = new Button.with_label("Remove");
bbox.pack_start(removeButton, true, true, 0);
removeButton.clicked.connect(() => {
if (index > 0 && items[index - 1] != null) {
index--;
var w = items[index];
w.get_parent().destroy(); // The Box wraps our widget in another, which it doesn't seem to expose. So we need to manage it via reference.
items[index] = null;
this.resize(200, 20); // Resizing the window to it's default size seems to be the magic way gtk allows to resize after removing a child widget.
}
});
}
public static int main(string[] args)
{
Gtk.init(ref args);
var window = new Example();
window.show_all();
Gtk.main();
return 0;
}
}
@kgilmer
Copy link
Author

kgilmer commented Apr 15, 2022

Here is a variation with horizontal expansion:

using Gtk;

// Creates a dialog style window with a static text block, dynamic list, and static button bar. 
// Changes to the list cause the dialog to update its bounds accourding to the items.
// Compile with: valac box.vala --pkg gtk+-3.0
public class Example : Window
{
    public Example()
    {
        this.title = "ListBox";
        this.set_default_size(200, 20);
        this.destroy.connect(Gtk.main_quit);

        var listbox = new Gtk.Box(Gtk.Orientation.HORIZONTAL, 0);
        this.add(listbox);        

        Gtk.Box box = new Gtk.Box (Gtk.Orientation.VERTICAL, 0);
        listbox.pack_start (box, true, true, 0);

        box.pack_start (new Label("This demonstrates dynamic resizing\nafter adding/removing widgets"), false, false, 0);        

        Widget[] items = new Widget[10];
        int index = 0;

        Gtk.Box bbox = new Gtk.Box (Gtk.Orientation.HORIZONTAL, 0);
        box.pack_start (bbox, false, true, 0);

        var addButton = new Button.with_label("Add");
        bbox.pack_start(addButton, true, true, 0);
        addButton.clicked.connect(() => {
          var label1 = new Label("Label " + index.to_string());
          listbox.pack_start(label1, true, true, 0);
          label1.show_all();
          items[index] = label1;
          index++;
        });

        var removeButton = new Button.with_label("Remove");
        bbox.pack_start(removeButton, true, true, 0);
        removeButton.clicked.connect(() => {
          if (index > 0 && items[index - 1] != null) {
            index--;
            var w = items[index];
            w.destroy(); // The Box wraps our widget in another, which it doesn't seem to expose. So we need to manage it via reference.
            items[index] = null;   
            this.resize(200, 20); // Resizing the window to it's default size seems to be the magic way gtk allows to resize after removing a child widget.
          }
        });
    }

    public static int main(string[] args)
    {
        Gtk.init(ref args);

        var window = new Example();
        window.show_all();

        Gtk.main();

        return 0;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment