Created
January 30, 2022 18:30
-
-
Save kgilmer/63fa35837161e6955e0b66af51df7e93 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is a variation with horizontal expansion: