chiark / gitweb /
Merge scratch fixes branch
[disorder] / disobedience / misc.c
1 /*
2  * This file is part of DisOrder
3  * Copyright (C) 2006-2008 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 3 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,
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  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file disobedience/misc.c
19  * @brief Miscellaneous GTK+ interfacing stuff
20  */
21
22 #include "disobedience.h"
23 #include "table.h"
24
25 struct image {
26   const char *name;
27   const guint8 *data;
28 };
29
30 #include "images.h"
31
32 /* Miscellaneous GTK+ stuff ------------------------------------------------ */
33
34 /* Functions */
35
36 /** @brief Put scrollbars around a widget
37  * @param child Widget to surround
38  * @return Scroll widget
39  */
40 GtkWidget *scroll_widget(GtkWidget *child) {
41   GtkWidget *scroller = gtk_scrolled_window_new(0, 0);
42   GtkAdjustment *adj;
43
44   gtk_widget_set_style(scroller, tool_style);
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);
50   if(GTK_IS_LAYOUT(child)
51      || GTK_IS_TREE_VIEW(child)) {
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);
65     gtk_widget_set_style(gtk_bin_get_child(GTK_BIN(scroller)), tool_style);
66   }
67   gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->hscrollbar, tool_style);
68   gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->vscrollbar, tool_style);
69   return scroller;
70 }
71
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
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  */
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;
105   int n;
106
107   if(!(pb = (GdkPixbuf *)cache_get(&image_cache_type, name))) {
108     if((n = TABLE_FIND(images, name, name)) >= 0) {
109       /* Use the built-in copy */
110       if(!(pb = gdk_pixbuf_new_from_inline(-1, images[n].data, FALSE, &err))) {
111         disorder_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         disorder_error(0, "%s", err->message);
119         return 0;
120       }
121     }
122     cache_put(&image_cache_type, name,  pb);
123   }
124   return pb;
125 }
126
127 /** @brief Pop up a message */
128 void popup_msg(GtkMessageType mt, const char *msg) {
129   popup_submsg(toplevel, mt, msg);
130 }
131
132 void popup_submsg(GtkWidget *parent, GtkMessageType mt, const char *msg) {
133   GtkWidget *w;
134
135   w = gtk_message_dialog_new(GTK_WINDOW(parent),
136                              GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
137                              mt,
138                              GTK_BUTTONS_CLOSE,
139                              "%s", msg);
140   gtk_widget_set_style(w, tool_style);
141   gtk_dialog_run(GTK_DIALOG(w));
142   gtk_widget_destroy(w);
143 }
144
145 /** @brief Pop up an error message */
146 void fpopup_msg(GtkMessageType mt, const char *fmt, ...) {
147   va_list ap;
148   char *msg;
149
150   va_start(ap, fmt);
151   byte_xvasprintf(&msg, fmt, ap);
152   va_end(ap);
153   popup_msg(mt, msg);
154 }
155
156 /** @brief Create a button with an icon in it
157  * @param path (relative) path to image
158  * @param tip Tooltip or NULL to not set one
159  * @return Button
160  */
161 GtkWidget *iconbutton(const char *path, const char *tip) {
162   GtkWidget *button, *content;
163   GdkPixbuf *pb;
164
165   button = gtk_button_new();
166   if((pb = find_image(path))) {
167     content = gtk_image_new_from_pixbuf(pb);
168   } else {
169     content = gtk_label_new(path);
170   }
171   gtk_widget_set_style(button, tool_style);
172   gtk_widget_set_style(content, tool_style);
173   gtk_container_add(GTK_CONTAINER(button), content);
174   if(tip)
175     gtk_widget_set_tooltip_text(button, tip);
176   return button;
177 }
178
179 /** @brief Create buttons and pack them into a box, which is returned */
180 GtkWidget *create_buttons_box(struct button *buttons,
181                               size_t nbuttons,
182                               GtkWidget *box) {
183   size_t n;
184
185   for(n = 0; n < nbuttons; ++n) {
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",
189                      G_CALLBACK(buttons[n].clicked), 0);
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);
198     gtk_widget_set_tooltip_text(buttons[n].widget, buttons[n].tip);
199   }
200   return box;
201 }
202
203 /** @brief Create buttons and pack them into an hbox */
204 GtkWidget *create_buttons(struct button *buttons,
205                           size_t nbuttons) {
206   return create_buttons_box(buttons, nbuttons,
207                             gtk_hbox_new(FALSE, 1));
208 }
209
210
211
212 /*
213 Local Variables:
214 c-basic-offset:2
215 comment-column:40
216 fill-column:79
217 indent-tabs-mode:nil
218 End:
219 */