Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder | |
ba937f01 | 3 | * Copyright (C) 2006-2008 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
9 | * | |
e7eb3a27 RK |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
2c348789 RK |
18 | /** @file disobedience/misc.c |
19 | * @brief Miscellaneous GTK+ interfacing stuff | |
20 | */ | |
460b9539 | 21 | |
22 | #include "disobedience.h" | |
54abef2b RK |
23 | #include "table.h" |
24 | ||
25 | struct image { | |
26 | const char *name; | |
27 | const guint8 *data; | |
28 | }; | |
29 | ||
30 | #include "images.h" | |
460b9539 | 31 | |
32 | /* Miscellaneous GTK+ stuff ------------------------------------------------ */ | |
33 | ||
34 | /* Functions */ | |
35 | ||
2c348789 RK |
36 | /** @brief Put scrollbars around a widget |
37 | * @param child Widget to surround | |
2c348789 RK |
38 | * @return Scroll widget |
39 | */ | |
d4ef4132 | 40 | GtkWidget *scroll_widget(GtkWidget *child) { |
460b9539 | 41 | GtkWidget *scroller = gtk_scrolled_window_new(0, 0); |
42 | GtkAdjustment *adj; | |
43 | ||
33288048 | 44 | gtk_widget_set_style(scroller, tool_style); |
460b9539 | 45 | D(("scroll_widget")); |
46 | /* Why isn't _AUTOMATIC the default? */ | |
47 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller), | |
48 | GTK_POLICY_AUTOMATIC, | |
49 | GTK_POLICY_AUTOMATIC); | |
ed464350 RK |
50 | if(GTK_IS_LAYOUT(child) |
51 | || GTK_IS_TREE_VIEW(child)) { | |
460b9539 | 52 | /* Child widget has native scroll support */ |
53 | gtk_container_add(GTK_CONTAINER(scroller), child); | |
54 | /* Fix up the step increments if they are 0 (seems like an odd default?) */ | |
55 | if(GTK_IS_LAYOUT(child)) { | |
56 | adj = gtk_layout_get_hadjustment(GTK_LAYOUT(child)); | |
57 | if(!adj->step_increment) adj->step_increment = 16; | |
58 | adj = gtk_layout_get_vadjustment(GTK_LAYOUT(child)); | |
59 | if(!adj->step_increment) adj->step_increment = 16; | |
60 | } | |
61 | } else { | |
62 | /* Child widget requires a viewport */ | |
63 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroller), | |
64 | child); | |
33288048 | 65 | gtk_widget_set_style(gtk_bin_get_child(GTK_BIN(scroller)), tool_style); |
460b9539 | 66 | } |
33288048 RK |
67 | gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->hscrollbar, tool_style); |
68 | gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->vscrollbar, tool_style); | |
460b9539 | 69 | return scroller; |
70 | } | |
71 | ||
e18c4734 RK |
72 | /** @brief Put a frame round a widget |
73 | * @param w Widget | |
74 | * @param label Label or NULL | |
75 | * @return Frame widget | |
76 | */ | |
77 | GtkWidget *frame_widget(GtkWidget *w, const char *label) { | |
78 | GtkWidget *const frame = gtk_frame_new(label); | |
79 | GtkWidget *const hbox = gtk_hbox_new(FALSE, 0); | |
80 | GtkWidget *const vbox = gtk_vbox_new(FALSE, 0); | |
81 | /* We want 4 pixels outside the frame boundary... */ | |
82 | gtk_container_set_border_width(GTK_CONTAINER(frame), 4); | |
83 | /* ...and 4 pixels inside */ | |
84 | gtk_box_pack_start(GTK_BOX(hbox), w, TRUE/*expand*/, TRUE/*fill*/, 4); | |
85 | gtk_box_pack_start(GTK_BOX(vbox), hbox, TRUE/*expand*/, TRUE/*fill*/, 4); | |
86 | gtk_container_add(GTK_CONTAINER(frame), vbox); | |
87 | return frame; | |
88 | } | |
89 | ||
2c348789 RK |
90 | /** @brief Find an image |
91 | * @param name Relative path to image | |
92 | * @return pixbuf containing image | |
93 | * | |
94 | * Images are cached so it's perfectly sensible to call this lots of times even | |
95 | * for the same image. | |
96 | * | |
97 | * Images are searched for in @c pkgdatadir/static. | |
98 | */ | |
460b9539 | 99 | GdkPixbuf *find_image(const char *name) { |
100 | static const struct cache_type image_cache_type = { INT_MAX }; | |
101 | ||
102 | GdkPixbuf *pb; | |
103 | char *path; | |
104 | GError *err = 0; | |
54abef2b | 105 | int n; |
460b9539 | 106 | |
107 | if(!(pb = (GdkPixbuf *)cache_get(&image_cache_type, name))) { | |
ba937f01 | 108 | if((n = TABLE_FIND(images, name, name)) >= 0) { |
54abef2b RK |
109 | /* Use the built-in copy */ |
110 | if(!(pb = gdk_pixbuf_new_from_inline(-1, images[n].data, FALSE, &err))) { | |
111 | error(0, "%s", err->message); | |
112 | return 0; | |
113 | } | |
114 | } else { | |
115 | /* See if there's a copy on disk */ | |
116 | byte_xasprintf(&path, "%s/static/%s", pkgdatadir, name); | |
117 | if(!(pb = gdk_pixbuf_new_from_file(path, &err))) { | |
118 | error(0, "%s", err->message); | |
119 | return 0; | |
120 | } | |
460b9539 | 121 | } |
122 | cache_put(&image_cache_type, name, pb); | |
123 | } | |
124 | return pb; | |
125 | } | |
126 | ||
043d60b1 RK |
127 | /** @brief Pop up a message */ |
128 | void popup_msg(GtkMessageType mt, const char *msg) { | |
34239ce4 RK |
129 | popup_submsg(toplevel, mt, msg); |
130 | } | |
131 | ||
132 | void popup_submsg(GtkWidget *parent, GtkMessageType mt, const char *msg) { | |
460b9539 | 133 | GtkWidget *w; |
134 | ||
34239ce4 | 135 | w = gtk_message_dialog_new(GTK_WINDOW(parent), |
460b9539 | 136 | GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, |
043d60b1 | 137 | mt, |
460b9539 | 138 | GTK_BUTTONS_CLOSE, |
139 | "%s", msg); | |
33288048 | 140 | gtk_widget_set_style(w, tool_style); |
460b9539 | 141 | gtk_dialog_run(GTK_DIALOG(w)); |
142 | gtk_widget_destroy(w); | |
143 | } | |
144 | ||
73f1b9f3 | 145 | /** @brief Pop up an error message */ |
043d60b1 | 146 | void fpopup_msg(GtkMessageType mt, const char *fmt, ...) { |
73f1b9f3 RK |
147 | va_list ap; |
148 | char *msg; | |
149 | ||
150 | va_start(ap, fmt); | |
151 | byte_xvasprintf(&msg, fmt, ap); | |
152 | va_end(ap); | |
043d60b1 | 153 | popup_msg(mt, msg); |
73f1b9f3 RK |
154 | } |
155 | ||
fcc8b9f7 RK |
156 | /** @brief Create a button with an icon in it |
157 | * @param path (relative) path to image | |
3149c1e2 | 158 | * @param tip Tooltip or NULL to not set one |
fcc8b9f7 RK |
159 | * @return Button |
160 | */ | |
161 | GtkWidget *iconbutton(const char *path, const char *tip) { | |
33288048 | 162 | GtkWidget *button, *content; |
fcc8b9f7 RK |
163 | GdkPixbuf *pb; |
164 | ||
fcc8b9f7 RK |
165 | button = gtk_button_new(); |
166 | if((pb = find_image(path))) { | |
fcc8b9f7 RK |
167 | content = gtk_image_new_from_pixbuf(pb); |
168 | } else { | |
fcc8b9f7 RK |
169 | content = gtk_label_new(path); |
170 | } | |
33288048 RK |
171 | gtk_widget_set_style(button, tool_style); |
172 | gtk_widget_set_style(content, tool_style); | |
fcc8b9f7 RK |
173 | gtk_container_add(GTK_CONTAINER(button), content); |
174 | if(tip) | |
175 | gtk_tooltips_set_tip(tips, button, tip, ""); | |
176 | return button; | |
177 | } | |
178 | ||
ffc4dbaf | 179 | /** @brief Create buttons and pack them into a box, which is returned */ |
f44417cf | 180 | GtkWidget *create_buttons_box(struct button *buttons, |
ffc4dbaf RK |
181 | size_t nbuttons, |
182 | GtkWidget *box) { | |
73f1b9f3 | 183 | size_t n; |
73f1b9f3 RK |
184 | |
185 | for(n = 0; n < nbuttons; ++n) { | |
f44417cf RK |
186 | buttons[n].widget = gtk_button_new_from_stock(buttons[n].stock); |
187 | gtk_widget_set_style(buttons[n].widget, tool_style); | |
188 | g_signal_connect(G_OBJECT(buttons[n].widget), "clicked", | |
73f1b9f3 | 189 | G_CALLBACK(buttons[n].clicked), 0); |
f44417cf RK |
190 | gtk_box_pack_start(GTK_BOX(box), buttons[n].widget, FALSE, FALSE, 1); |
191 | gtk_tooltips_set_tip(tips, buttons[n].widget, buttons[n].tip, ""); | |
73f1b9f3 | 192 | } |
ffc4dbaf RK |
193 | return box; |
194 | } | |
195 | ||
196 | /** @brief Create buttons and pack them into an hbox */ | |
f44417cf | 197 | GtkWidget *create_buttons(struct button *buttons, |
ffc4dbaf RK |
198 | size_t nbuttons) { |
199 | return create_buttons_box(buttons, nbuttons, | |
200 | gtk_hbox_new(FALSE, 1)); | |
73f1b9f3 RK |
201 | } |
202 | ||
ffc4dbaf RK |
203 | |
204 | ||
460b9539 | 205 | /* |
206 | Local Variables: | |
207 | c-basic-offset:2 | |
208 | comment-column:40 | |
209 | fill-column:79 | |
210 | indent-tabs-mode:nil | |
211 | End: | |
212 | */ |