chiark / gitweb /
build fix
[disorder] / disobedience / misc.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2006 Richard Kettlewell
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 */
20/** @file disobedience/misc.c
21 * @brief Miscellaneous GTK+ interfacing stuff
22 */
23
24#include "disobedience.h"
25#include "table.h"
26
27struct image {
28 const char *name;
29 const guint8 *data;
30};
31
32#include "images.h"
33
34/* Miscellaneous GTK+ stuff ------------------------------------------------ */
35
36WT(cached_image);
37
38/* Functions */
39
40/** @brief Put scrollbars around a widget
41 * @param child Widget to surround
42 * @param widgetname Name for (both) widgets
43 * @return Scroll widget
44 */
45GtkWidget *scroll_widget(GtkWidget *child,
46 const char *widgetname) {
47 GtkWidget *scroller = gtk_scrolled_window_new(0, 0);
48 GtkAdjustment *adj;
49
50 D(("scroll_widget"));
51 /* Why isn't _AUTOMATIC the default? */
52 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
53 GTK_POLICY_AUTOMATIC,
54 GTK_POLICY_AUTOMATIC);
55 if(GTK_IS_LAYOUT(child)) {
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);
69 }
70 /* Apply a name to the widget so it can be recolored */
71 gtk_widget_set_name(GTK_BIN(scroller)->child, widgetname);
72 gtk_widget_set_name(scroller, widgetname);
73 return scroller;
74}
75
76/** @brief Find an image
77 * @param name Relative path to image
78 * @return pixbuf containing image
79 *
80 * Images are cached so it's perfectly sensible to call this lots of times even
81 * for the same image.
82 *
83 * Images are searched for in @c pkgdatadir/static.
84 */
85GdkPixbuf *find_image(const char *name) {
86 static const struct cache_type image_cache_type = { INT_MAX };
87
88 GdkPixbuf *pb;
89 char *path;
90 GError *err = 0;
91 int n;
92
93 if(!(pb = (GdkPixbuf *)cache_get(&image_cache_type, name))) {
94 if((n = TABLE_FIND(images, struct image, name, name)) >= 0) {
95 /* Use the built-in copy */
96 if(!(pb = gdk_pixbuf_new_from_inline(-1, images[n].data, FALSE, &err))) {
97 error(0, "%s", err->message);
98 return 0;
99 }
100 } else {
101 /* See if there's a copy on disk */
102 byte_xasprintf(&path, "%s/static/%s", pkgdatadir, name);
103 if(!(pb = gdk_pixbuf_new_from_file(path, &err))) {
104 error(0, "%s", err->message);
105 return 0;
106 }
107 }
108 NW(cached_image);
109 cache_put(&image_cache_type, name, pb);
110 }
111 return pb;
112}
113
114/** @brief Pop up a message */
115void popup_msg(GtkMessageType mt, const char *msg) {
116 GtkWidget *w;
117
118 w = gtk_message_dialog_new(GTK_WINDOW(toplevel),
119 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
120 mt,
121 GTK_BUTTONS_CLOSE,
122 "%s", msg);
123 gtk_dialog_run(GTK_DIALOG(w));
124 gtk_widget_destroy(w);
125}
126
127/** @brief Pop up an error message */
128void fpopup_msg(GtkMessageType mt, const char *fmt, ...) {
129 va_list ap;
130 char *msg;
131
132 va_start(ap, fmt);
133 byte_xvasprintf(&msg, fmt, ap);
134 va_end(ap);
135 popup_msg(mt, msg);
136}
137
138/** @brief Create a button with an icon in it
139 * @param path (relative) path to image
140 * @param tooltip Tooltip or NULL to not set one
141 * @return Button
142 */
143GtkWidget *iconbutton(const char *path, const char *tip) {
144 GtkWidget *button, *content;;
145 GdkPixbuf *pb;
146
147 NW(button);
148 button = gtk_button_new();
149 if((pb = find_image(path))) {
150 NW(image);
151 content = gtk_image_new_from_pixbuf(pb);
152 } else {
153 NW(label);
154 content = gtk_label_new(path);
155 }
156 gtk_container_add(GTK_CONTAINER(button), content);
157 if(tip)
158 gtk_tooltips_set_tip(tips, button, tip, "");
159 return button;
160}
161
162/** @brief Create buttons and pack them into an hbox */
163GtkWidget *create_buttons(const struct button *buttons,
164 size_t nbuttons) {
165 size_t n;
166 GtkWidget *const hbox = gtk_hbox_new(FALSE, 1);
167
168 for(n = 0; n < nbuttons; ++n) {
169 GtkWidget *const button = gtk_button_new_from_stock(buttons[n].stock);
170 g_signal_connect(G_OBJECT(button), "clicked",
171 G_CALLBACK(buttons[n].clicked), 0);
172 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
173 gtk_tooltips_set_tip(tips, button, buttons[n].tip, "");
174 }
175 return hbox;
176}
177
178/*
179Local Variables:
180c-basic-offset:2
181comment-column:40
182fill-column:79
183indent-tabs-mode:nil
184End:
185*/