|
|
1.1 root 1: /*
2: * util.c
3: *
4: * Copyright 2003-2004 Richard Drummond
5: */
6:
7: #include <sys/types.h>
8: #include <sys/stat.h>
9: #include <unistd.h>
10: #include <string.h>
11:
12: #include <gdk/gdkkeysyms.h>
13: #include <gtk/gtk.h>
14:
15: #include "util.h"
16:
17: /*
18: * Some utility functions to make building a GTK+ GUI easier
19: * and more compact, to hide differences between GTK1.x and GTK2.x
20: * and to help maintain consistency
21: */
22:
23: GtkWidget *make_chooser (int count, ...)
24: {
25: GtkWidget *chooser;
26: va_list choices;
27: int i;
28:
29: chooser = gtk_combo_box_new_text ();
30:
31: va_start (choices, count);
32: for (i = 0; i < count; i++)
33: gtk_combo_box_append_text (GTK_COMBO_BOX (chooser), va_arg (choices, char *));
34:
35: gtk_widget_show (chooser);
36: return chooser;
37: }
38:
39: /*
40: * Add some padding to a vbox or hbox which will consume
41: * space when the box is resized to larger than default size
42: */
1.1.1.2 ! root 43: static void add_box_padding (GtkWidget *box)
1.1 root 44: {
45: GtkWidget *vbox;
46:
47: vbox = gtk_vbox_new (FALSE, 0);
48: gtk_widget_show (vbox);
49: gtk_box_pack_start (GTK_BOX (box), vbox, TRUE, TRUE, 0);
50: }
51:
52: /*
53: * Add some padding to a table which will consime space when
54: * when the table is resized to larger than default size
55: */
1.1.1.2 ! root 56: static void add_table_padding (GtkWidget *table, int x, int y)
1.1 root 57: {
58: GtkWidget *vbox;
59: vbox = gtk_vbox_new (FALSE, 0);
60: gtk_widget_show (vbox);
61: gtk_table_attach (GTK_TABLE (table), vbox, x, x+1, y, y+1,
62: (GtkAttachOptions) (GTK_EXPAND | GTK_FILL),
63: (GtkAttachOptions) (GTK_EXPAND | GTK_FILL), 0, 0);
64: }
65:
66: /*
67: * Add a widget to a table with some sensible defaults
68: *
69: * <x,y> is the table position to insert the widget
70: * <width> is the the number of columns the widget will take up
71: * <xflags> are the attachment flags that will apply horizontally
72: */
1.1.1.2 ! root 73: static void add_to_table (GtkWidget *table, GtkWidget *widget,
! 74: int x, int y, int width, int xflags)
1.1 root 75: {
1.1.1.2 ! root 76: gtk_table_attach (GTK_TABLE (table), widget, x, x+width, y, y+1,
! 77: (GtkAttachOptions) (xflags),
! 78: (GtkAttachOptions) (0), 0, 0);
1.1 root 79: }
80:
81:
82: /*
83: * Super-duper, handy table creation tool!
84: *
85: * Creates a table and add a list of widgets to it.
86: */
87: GtkWidget *gtkutil_add_table (GtkWidget *container, ...)
88: {
89: va_list contents;
90: GtkWidget *widget;
91: GtkWidget *table;
92: int row, max_col;
93: int col, width;
94: int flags;
95:
96: table = gtk_table_new (3, 3, FALSE);
97: gtk_container_set_border_width (GTK_CONTAINER (table), TABLE_BORDER_WIDTH);
98: gtk_table_set_row_spacings (GTK_TABLE (table), TABLE_ROW_SPACING);
99: gtk_table_set_col_spacings (GTK_TABLE (table), TABLE_COL_SPACING);
100: gtk_container_add (GTK_CONTAINER (container), table);
101:
102: va_start (contents, container);
103: widget = va_arg (contents, GtkWidget *);
104: row = 1;
105: max_col = 1;
106:
107: while (widget != GTKUTIL_TABLE_END) {
108: if (widget == GTKUTIL_ROW_END) {
109: row += 2;
110: } else {
111: col = va_arg (contents, gint);
112: if (col > max_col) max_col = col;
113: width = va_arg (contents, gint);
114: flags = va_arg (contents, gint);
115:
116: gtk_table_attach (GTK_TABLE (table), widget, col, col+width, row, row+1,
117: (GtkAttachOptions) (flags), (GtkAttachOptions) (0), 0, 0);
118: }
119: widget = va_arg (contents, GtkWidget *);
120: }
121:
122: gtk_table_resize (GTK_TABLE (table), row, max_col + 2);
123: add_table_padding (table, 0, 0);
124: add_table_padding (table, max_col + 1, row - 1);
125:
126: gtk_widget_show_all (table);
127:
128: return table;
129: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.