1 #include <stdlib.h> 2 #include <string.h> 3 #include <gtk/gtk.h> 4 5 #include "gcompat.h" 6 #include "ghelpers.h" 7 8 GtkWidget *new_combo_entry_in_frame(GtkWidget *box, const char *label) 9 { 10 GtkWidget *entry, *frame; 11 12 frame = gtk_frame_new(label); 13 entry = gtk_combo_box_text_new(); 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 20 GtkWidget *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(); 26 gtk_editable_set_editable(GTK_EDITABLE(entry), 0); 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 33 static 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 46 GtkWidget *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 58 GtkWidget *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 71 GtkWidget *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 87 void 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 95 void 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 } 102 103 GtkTreeViewColumn *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); 124 gtk_tree_view_column_set_expand(col, TRUE); 125 gtk_tree_view_column_add_attribute(col, renderer, "text", index); 126 g_object_set(G_OBJECT(renderer), "alignment", align, NULL); 127 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 144 void 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 151 void 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 162 void 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 176 void 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 } 191 192 GtkWidget *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 } 202