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