gfio: color parse stack removal
[fio.git] / ghelpers.c
CommitLineData
49c34176
JA
1#include <stdlib.h>
2#include <string.h>
53e0e85d
JA
3#include <gtk/gtk.h>
4
f762cef9 5#include "gcompat.h"
49c34176
JA
6#include "ghelpers.h"
7
53e0e85d
JA
8GtkWidget *new_combo_entry_in_frame(GtkWidget *box, const char *label)
9{
10 GtkWidget *entry, *frame;
11
12 frame = gtk_frame_new(label);
f762cef9 13 entry = gtk_combo_box_text_new();
53e0e85d
JA
14 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
15 gtk_container_add(GTK_CONTAINER(frame), entry);
16
17 return entry;
18}
19
20GtkWidget *new_info_entry_in_frame(GtkWidget *box, const char *label)
21{
22 GtkWidget *entry, *frame;
23
24 frame = gtk_frame_new(label);
25 entry = gtk_entry_new();
f762cef9 26 gtk_editable_set_editable(GTK_EDITABLE(entry), 0);
53e0e85d
JA
27 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
28 gtk_container_add(GTK_CONTAINER(frame), entry);
29
30 return entry;
31}
32
33GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
34{
35 GtkWidget *label_widget;
36 GtkWidget *frame;
37
38 frame = gtk_frame_new(label);
39 label_widget = gtk_label_new(NULL);
40 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
41 gtk_container_add(GTK_CONTAINER(frame), label_widget);
42
43 return label_widget;
44}
45
46GtkWidget *create_spinbutton(GtkWidget *hbox, double min, double max, double defval)
47{
48 GtkWidget *button, *box;
49
50 box = gtk_hbox_new(FALSE, 3);
51 gtk_container_add(GTK_CONTAINER(hbox), box);
52
53 button = gtk_spin_button_new_with_range(min, max, 1.0);
54 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
55
56 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
57 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), defval);
58
59 return button;
60}
61
62void label_set_int_value(GtkWidget *entry, unsigned int val)
63{
64 char tmp[80];
65
66 sprintf(tmp, "%u", val);
67 gtk_label_set_text(GTK_LABEL(entry), tmp);
68}
69
70void entry_set_int_value(GtkWidget *entry, unsigned int val)
71{
72 char tmp[80];
73
74 sprintf(tmp, "%u", val);
75 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
76}
49c34176
JA
77
78GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title, unsigned int flags)
79{
80 GtkCellRenderer *renderer;
81 GtkTreeViewColumn *col;
82 double xalign = 0.0; /* left as default */
83 PangoAlignment align;
84 gboolean visible;
85
86 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
87 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
88 PANGO_ALIGN_CENTER;
89 visible = !(flags & INVISIBLE);
90
91 renderer = gtk_cell_renderer_text_new();
92 col = gtk_tree_view_column_new();
93
94 gtk_tree_view_column_set_title(col, title);
95 if (!(flags & UNSORTABLE))
96 gtk_tree_view_column_set_sort_column_id(col, index);
97 gtk_tree_view_column_set_resizable(col, TRUE);
98 gtk_tree_view_column_pack_start(col, renderer, TRUE);
b4d669e6 99 gtk_tree_view_column_set_expand(col, TRUE);
49c34176 100 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
cd972fdd 101 g_object_set(G_OBJECT(renderer), "alignment", align, NULL);
49c34176
JA
102 switch (align) {
103 case PANGO_ALIGN_LEFT:
104 xalign = 0.0;
105 break;
106 case PANGO_ALIGN_CENTER:
107 xalign = 0.5;
108 break;
109 case PANGO_ALIGN_RIGHT:
110 xalign = 1.0;
111 break;
112 }
113 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
114 gtk_tree_view_column_set_visible(col, visible);
115 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
116 return col;
117}
118
119void multitext_add_entry(struct multitext_widget *mt, const char *text)
120{
121 mt->text = realloc(mt->text, (mt->max_text + 1) * sizeof(char *));
122 mt->text[mt->max_text] = strdup(text);
123 mt->max_text++;
124}
125
126void multitext_set_entry(struct multitext_widget *mt, unsigned int index)
127{
128 if (index >= mt->max_text)
129 return;
130 if (!mt->text || !mt->text[index])
131 return;
132
133 mt->cur_text = index;
134 gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
135}
136
137void multitext_update_entry(struct multitext_widget *mt, unsigned int index,
138 const char *text)
139{
140 if (!mt->text)
141 return;
142
143 if (mt->text[index])
144 free(mt->text[index]);
145
146 mt->text[index] = strdup(text);
147 if (mt->cur_text == index)
148 gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
149}
150
151void multitext_free(struct multitext_widget *mt)
152{
153 int i;
154
155 gtk_entry_set_text(GTK_ENTRY(mt->entry), "");
156
157 for (i = 0; i < mt->max_text; i++) {
158 if (mt->text[i])
159 free(mt->text[i]);
160 }
161
162 free(mt->text);
163 mt->cur_text = -1;
164 mt->max_text = 0;
165}
1252d8f2
JA
166
167GtkWidget *get_scrolled_window(gint border_width)
168{
169 GtkWidget *scroll;
170
171 scroll = gtk_scrolled_window_new(NULL, NULL);
172 gtk_container_set_border_width(GTK_CONTAINER(scroll), border_width);
173 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
174
175 return scroll;
176}