blob: 7acf588ab311ef9bb02a73746b40e74594e0ec81 [file] [log] [blame]
Jens Axboe49c34172012-03-16 12:05:17 +01001#include <stdlib.h>
2#include <string.h>
Jens Axboe53e0e852012-03-15 19:38:01 +01003#include <gtk/gtk.h>
4
Jens Axboef762cef2012-03-26 13:51:11 +02005#include "gcompat.h"
Jens Axboe49c34172012-03-16 12:05:17 +01006#include "ghelpers.h"
7
Jens Axboe53e0e852012-03-15 19:38:01 +01008GtkWidget *new_combo_entry_in_frame(GtkWidget *box, const char *label)
9{
10 GtkWidget *entry, *frame;
11
12 frame = gtk_frame_new(label);
Jens Axboef762cef2012-03-26 13:51:11 +020013 entry = gtk_combo_box_text_new();
Jens Axboe53e0e852012-03-15 19:38:01 +010014 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();
Jens Axboef762cef2012-03-26 13:51:11 +020026 gtk_editable_set_editable(GTK_EDITABLE(entry), 0);
Jens Axboe53e0e852012-03-15 19:38:01 +010027 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
Jens Axboeb78ca652012-09-25 14:39:36 +020033static void fill_color_from_rgb(GdkColor *c, gfloat r, gfloat g, gfloat b)
34{
35 gint R, G, B;
36 gchar tmp[8];
37
38 memset(c, 0, sizeof(*c));
39 R = r * 255;
40 G = g * 255;
41 B = b * 255;
42 snprintf(tmp, sizeof(tmp), "#%02x%02x%02x", R, G, B);
43 gdk_color_parse(tmp, c);
44}
45
46GtkWidget *new_info_entry_in_frame_rgb(GtkWidget *box, const char *label,
47 gfloat r, gfloat g, gfloat b)
48{
49 GtkWidget *entry;
50 GdkColor c;
51
52 entry = new_info_entry_in_frame(box, label);
53 fill_color_from_rgb(&c, r, g, b);
54 gtk_widget_modify_text(entry, GTK_STATE_NORMAL, &c);
55 return entry;
56}
57
Jens Axboe53e0e852012-03-15 19:38:01 +010058GtkWidget *new_info_label_in_frame(GtkWidget *box, const char *label)
59{
60 GtkWidget *label_widget;
61 GtkWidget *frame;
62
63 frame = gtk_frame_new(label);
64 label_widget = gtk_label_new(NULL);
65 gtk_box_pack_start(GTK_BOX(box), frame, TRUE, TRUE, 3);
66 gtk_container_add(GTK_CONTAINER(frame), label_widget);
67
68 return label_widget;
69}
70
71GtkWidget *create_spinbutton(GtkWidget *hbox, double min, double max, double defval)
72{
73 GtkWidget *button, *box;
74
75 box = gtk_hbox_new(FALSE, 3);
76 gtk_container_add(GTK_CONTAINER(hbox), box);
77
78 button = gtk_spin_button_new_with_range(min, max, 1.0);
79 gtk_box_pack_start(GTK_BOX(box), button, TRUE, TRUE, 0);
80
81 gtk_spin_button_set_update_policy(GTK_SPIN_BUTTON(button), GTK_UPDATE_IF_VALID);
82 gtk_spin_button_set_value(GTK_SPIN_BUTTON(button), defval);
83
84 return button;
85}
86
87void label_set_int_value(GtkWidget *entry, unsigned int val)
88{
89 char tmp[80];
90
91 sprintf(tmp, "%u", val);
92 gtk_label_set_text(GTK_LABEL(entry), tmp);
93}
94
95void entry_set_int_value(GtkWidget *entry, unsigned int val)
96{
97 char tmp[80];
98
99 sprintf(tmp, "%u", val);
100 gtk_entry_set_text(GTK_ENTRY(entry), tmp);
101}
Jens Axboe49c34172012-03-16 12:05:17 +0100102
103GtkTreeViewColumn *tree_view_column(GtkWidget *tree_view, int index, const char *title, unsigned int flags)
104{
105 GtkCellRenderer *renderer;
106 GtkTreeViewColumn *col;
107 double xalign = 0.0; /* left as default */
108 PangoAlignment align;
109 gboolean visible;
110
111 align = (flags & ALIGN_LEFT) ? PANGO_ALIGN_LEFT :
112 (flags & ALIGN_RIGHT) ? PANGO_ALIGN_RIGHT :
113 PANGO_ALIGN_CENTER;
114 visible = !(flags & INVISIBLE);
115
116 renderer = gtk_cell_renderer_text_new();
117 col = gtk_tree_view_column_new();
118
119 gtk_tree_view_column_set_title(col, title);
120 if (!(flags & UNSORTABLE))
121 gtk_tree_view_column_set_sort_column_id(col, index);
122 gtk_tree_view_column_set_resizable(col, TRUE);
123 gtk_tree_view_column_pack_start(col, renderer, TRUE);
Stephen M. Cameronb4d669e2012-03-23 07:48:37 +0100124 gtk_tree_view_column_set_expand(col, TRUE);
Jens Axboe49c34172012-03-16 12:05:17 +0100125 gtk_tree_view_column_add_attribute(col, renderer, "text", index);
Jens Axboecd972fd2012-03-26 12:55:33 +0200126 g_object_set(G_OBJECT(renderer), "alignment", align, NULL);
Jens Axboe49c34172012-03-16 12:05:17 +0100127 switch (align) {
128 case PANGO_ALIGN_LEFT:
129 xalign = 0.0;
130 break;
131 case PANGO_ALIGN_CENTER:
132 xalign = 0.5;
133 break;
134 case PANGO_ALIGN_RIGHT:
135 xalign = 1.0;
136 break;
137 }
138 gtk_cell_renderer_set_alignment(GTK_CELL_RENDERER(renderer), xalign, 0.5);
139 gtk_tree_view_column_set_visible(col, visible);
140 gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view), col);
141 return col;
142}
143
144void multitext_add_entry(struct multitext_widget *mt, const char *text)
145{
146 mt->text = realloc(mt->text, (mt->max_text + 1) * sizeof(char *));
147 mt->text[mt->max_text] = strdup(text);
148 mt->max_text++;
149}
150
151void multitext_set_entry(struct multitext_widget *mt, unsigned int index)
152{
153 if (index >= mt->max_text)
154 return;
155 if (!mt->text || !mt->text[index])
156 return;
157
158 mt->cur_text = index;
159 gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
160}
161
162void multitext_update_entry(struct multitext_widget *mt, unsigned int index,
163 const char *text)
164{
165 if (!mt->text)
166 return;
167
168 if (mt->text[index])
169 free(mt->text[index]);
170
171 mt->text[index] = strdup(text);
172 if (mt->cur_text == index)
173 gtk_entry_set_text(GTK_ENTRY(mt->entry), mt->text[index]);
174}
175
176void multitext_free(struct multitext_widget *mt)
177{
178 int i;
179
180 gtk_entry_set_text(GTK_ENTRY(mt->entry), "");
181
182 for (i = 0; i < mt->max_text; i++) {
183 if (mt->text[i])
184 free(mt->text[i]);
185 }
186
187 free(mt->text);
188 mt->cur_text = -1;
189 mt->max_text = 0;
190}
Jens Axboe1252d8f2012-03-21 11:13:31 +0100191
192GtkWidget *get_scrolled_window(gint border_width)
193{
194 GtkWidget *scroll;
195
196 scroll = gtk_scrolled_window_new(NULL, NULL);
197 gtk_container_set_border_width(GTK_CONTAINER(scroll), border_width);
198 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
199
200 return scroll;
201}