chiark / gitweb /
Move images.h to images/ directory, and limit to just the images that
[disorder] / disobedience / misc.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder
444be909 3 * Copyright (C) 2006-2008, 2010 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
25struct image {
26 const char *name;
27 const guint8 *data;
28};
29
444be909 30#include "../images/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 40GtkWidget *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 */
77GtkWidget *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 99GdkPixbuf *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))) {
2e9ba080 111 disorder_error(0, "%s", err->message);
54abef2b
RK
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))) {
2e9ba080 118 disorder_error(0, "%s", err->message);
54abef2b
RK
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 */
128void popup_msg(GtkMessageType mt, const char *msg) {
34239ce4
RK
129 popup_submsg(toplevel, mt, msg);
130}
131
132void 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 146void 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 */
161GtkWidget *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)
a31f7a72 175 gtk_widget_set_tooltip_text(button, tip);
fcc8b9f7
RK
176 return button;
177}
178
ffc4dbaf 179/** @brief Create buttons and pack them into a box, which is returned */
f44417cf 180GtkWidget *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);
ad424400
RK
190 void (*pack)(GtkBox *box,
191 GtkWidget *child,
192 gboolean expand,
193 gboolean fill,
194 guint padding);
195 if(!(pack = buttons[n].pack))
196 pack = gtk_box_pack_start;
197 pack(GTK_BOX(box), buttons[n].widget, FALSE, FALSE, 1);
a31f7a72 198 gtk_widget_set_tooltip_text(buttons[n].widget, buttons[n].tip);
73f1b9f3 199 }
ffc4dbaf
RK
200 return box;
201}
202
203/** @brief Create buttons and pack them into an hbox */
f44417cf 204GtkWidget *create_buttons(struct button *buttons,
ffc4dbaf
RK
205 size_t nbuttons) {
206 return create_buttons_box(buttons, nbuttons,
207 gtk_hbox_new(FALSE, 1));
73f1b9f3
RK
208}
209
ffc4dbaf
RK
210
211
460b9539 212/*
213Local Variables:
214c-basic-offset:2
215comment-column:40
216fill-column:79
217indent-tabs-mode:nil
218End:
219*/