changeset 13939:5d5c84239eea

[gaim-migrate @ 16470] Add an alignment option in a GntBox to align the widgets in it. committer: Tailor Script <tailor@pidgin.im>
author Sadrul Habib Chowdhury <imadil@gmail.com>
date Sun, 09 Jul 2006 04:44:58 +0000
parents 82684a39f1ec
children fe1c92e1494a
files console/libgnt/gntbox.c console/libgnt/gntbox.h console/libgnt/test/combo.c
diffstat 3 files changed, 71 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/console/libgnt/gntbox.c	Sun Jul 09 01:23:12 2006 +0000
+++ b/console/libgnt/gntbox.c	Sun Jul 09 04:44:58 2006 +0000
@@ -372,6 +372,10 @@
 	box->pad = 1;
 	gnt_widget_set_take_focus(widget, TRUE);
 	GNT_WIDGET_SET_FLAGS(widget, GNT_WIDGET_NO_BORDER | GNT_WIDGET_NO_SHADOW);
+	if (vert)
+		box->alignment = GNT_ALIGN_LEFT;
+	else
+		box->alignment = GNT_ALIGN_MID;
 
 	return widget;
 }
@@ -431,18 +435,42 @@
 	{
 		GntWidget *w = GNT_WIDGET(iter->data);
 		int height, width;
+		int x, y;
 
 		if (GNT_IS_BOX(w))
 			gnt_box_sync_children(GNT_BOX(w));
 
 		gnt_widget_get_size(w, &width, &height);
 
+		x = w->priv.x - widget->priv.x;
+		y = w->priv.y - widget->priv.y;
+
+		if (box->vertical)
+		{
+			if (box->alignment == GNT_ALIGN_RIGHT)
+				x += widget->priv.width - width;
+			else if (box->alignment == GNT_ALIGN_MID)
+				x += (widget->priv.width - width)/2;
+			if (x + width > widget->priv.width - 1)
+				x -= x + width - (widget->priv.width - 1);
+		}
+		else
+		{
+			if (box->alignment == GNT_ALIGN_BOTTOM)
+				y += widget->priv.height - height;
+			else if (box->alignment == GNT_ALIGN_MID)
+				y += (widget->priv.height - height)/2;
+			if (y + height > widget->priv.height - 1)
+				y -= y + height - (widget->priv.height - 1);
+		}
+
 		copywin(w->window, widget->window, 0, 0,
-				w->priv.y - widget->priv.y,
-				w->priv.x - widget->priv.x,
-				w->priv.y - widget->priv.y + height - 1,
-				w->priv.x - widget->priv.x + width - 1,
-				FALSE);
+				y, x, y + height - 1, x + width - 1, FALSE);
 	}
 }
 
+void gnt_box_set_alignment(GntBox *box, GntAlignment alignment)
+{
+	box->alignment = alignment;
+}
+
--- a/console/libgnt/gntbox.h	Sun Jul 09 01:23:12 2006 +0000
+++ b/console/libgnt/gntbox.h	Sun Jul 09 04:44:58 2006 +0000
@@ -14,6 +14,19 @@
 typedef struct _GnBox			GntBox;
 typedef struct _GnBoxClass		GntBoxClass;
 
+typedef enum
+{
+	/* These for vertical boxes */
+	GNT_ALIGN_LEFT,
+	GNT_ALIGN_RIGHT,
+
+	GNT_ALIGN_MID,
+
+	/* These for horizontal boxes */
+	GNT_ALIGN_TOP,
+	GNT_ALIGN_BOTTOM
+} GntAlignment;
+
 struct _GnBox
 {
 	GntWidget parent;
@@ -24,6 +37,7 @@
 
 	GntWidget *active;
 	int pad;			/* Number of spaces to use between widgets */
+	GntAlignment alignment;  /* How are the widgets going to be aligned? */
 
 	char *title;
 	GList *focus;		/* List of widgets to cycle focus (only valid for parent boxes) */
@@ -60,6 +74,8 @@
 
 void gnt_box_sync_children(GntBox *box);
 
+void gnt_box_set_alignment(GntBox *box, GntAlignment alignment);
+
 G_END_DECLS
 
 #endif /* GNT_BOX_H */
--- a/console/libgnt/test/combo.c	Sun Jul 09 01:23:12 2006 +0000
+++ b/console/libgnt/test/combo.c	Sun Jul 09 04:44:58 2006 +0000
@@ -1,19 +1,29 @@
 #include <gnt.h>
 #include <gntbox.h>
+#include <gntbutton.h>
 #include <gntcombobox.h>
 #include <gntlabel.h>
 
 int main()
 {
 	GntWidget *box, *combo, *button;
+	GntWidget *hbox;
 
 	gnt_init();
 	
-	box = gnt_box_new(FALSE, FALSE);
+	box = gnt_box_new(FALSE, TRUE);
+	gnt_widget_set_name(box, "box");
+	gnt_box_set_alignment(GNT_BOX(box), GNT_ALIGN_MID);
+	gnt_box_set_pad(GNT_BOX(box), 0);
 
 	gnt_box_set_toplevel(GNT_BOX(box), TRUE);
 	gnt_box_set_title(GNT_BOX(box), "Checkbox");
 
+	hbox = gnt_box_new(FALSE, FALSE);
+	gnt_box_set_pad(GNT_BOX(hbox), 0);
+	gnt_box_set_alignment(GNT_BOX(hbox), GNT_ALIGN_MID);
+	gnt_widget_set_name(hbox, "upper");
+
 	combo = gnt_combo_box_new();
 	gnt_combo_box_add_data(GNT_COMBO_BOX(combo), "1", "1");
 	gnt_combo_box_add_data(GNT_COMBO_BOX(combo), "2", "2");
@@ -22,11 +32,19 @@
 	gnt_combo_box_add_data(GNT_COMBO_BOX(combo), "5", "5");
 	gnt_combo_box_add_data(GNT_COMBO_BOX(combo), "6", "6");
 
-	gnt_box_add_widget(GNT_BOX(box), gnt_label_new("Select"));
-	gnt_box_add_widget(GNT_BOX(box), combo);
+	gnt_box_add_widget(GNT_BOX(hbox), gnt_label_new("Select"));
+	gnt_box_add_widget(GNT_BOX(hbox), combo);
+
+	gnt_box_add_widget(GNT_BOX(box), hbox);
+
+	hbox = gnt_box_new(TRUE, FALSE);
+	gnt_box_set_alignment(GNT_BOX(hbox), GNT_ALIGN_MID);
+	gnt_widget_set_name(hbox, "lower");
 
 	button = gnt_button_new("OK");
-	gnt_box_add_widget(GNT_BOX(box), button);
+	gnt_box_add_widget(GNT_BOX(hbox), button);
+
+	gnt_box_add_widget(GNT_BOX(box), hbox);
 
 	gnt_widget_show(box);