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