eric’s extremeboredom

adventures into and out of extreme boredom.

Disabling a Gtk Paned

If you have ever come across a time when you need to disable a Paned widget in such a way where the children are still enabled but the Paned’s resizer bar is not movable, here are two simple methods that will convert a Paned to a Box and back again, making it appear to the user as if the resize bar is just being disabled.

public static Box PanedToBox(Paned paned) {
	Box newBox = null;

	if (paned is VPaned)
		newBox = new Gtk.VBox(false, 5);
	else if (paned is HPaned)
		newBox = new Gtk.HBox(false, 5);
	else
		throw new ArgumentException("paned must be of type VPaned or HPaned. paned was " + paned.GetType());

	Container parent = (Container)paned.Parent;
	Widget widget1 = paned.Child1;
	Widget widget2 = paned.Child2;

	paned.Remove(widget1);
	paned.Remove(widget2);

	newBox.PackStart(widget1,true,true,0);
	newBox.PackEnd(widget2,false,false,0);

	parent.Remove(paned);
	paned.Destroy();

	parent.Add(newBox);
	newBox.Show();
	widget1.Show();
	widget2.Show();

	return newBox;
}

public static Paned BoxToPaned(Box box) {
	Paned newPaned = null;

	if (box is VBox)
		newPaned = new VPaned();
	else if (box is HBox)
		newPaned = new HPaned();
	else
		throw new ArgumentException("box must be of type HBox or VBox. box was " + box.GetType().ToString());

	Container parent = (Container)box.Parent;
	Widget widget1 = box.Children[0];
	Widget widget2 = box.Children[1];

	box.Remove(widget1);
	box.Remove(widget2);

	newPaned.Add1(widget1);
	newPaned.Add2(widget2);

	newPaned.Pack1(widget1, false, false);
	newPaned.Pack2(widget2, true, true);

	parent.Remove(box);
	box.Destroy();

	parent.Add(newPaned);
	newPaned.Show();
	widget1.Show();
	widget2.Show();

	return newPaned;

}

Categorized as Technology, Software Development, Technology

Leave a Reply