2 * This file is part of DisOrder
3 * Copyright (C) 2006 Richard Kettlewell
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.
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.
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
20 /** @file disobedience/misc.c
21 * @brief Miscellaneous GTK+ interfacing stuff
24 #include "disobedience.h"
34 /* Miscellaneous GTK+ stuff ------------------------------------------------ */
40 /** @brief Put scrollbars around a widget
41 * @param child Widget to surround
42 * @return Scroll widget
44 GtkWidget *scroll_widget(GtkWidget *child) {
45 GtkWidget *scroller = gtk_scrolled_window_new(0, 0);
49 /* Why isn't _AUTOMATIC the default? */
50 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
52 GTK_POLICY_AUTOMATIC);
53 if(GTK_IS_LAYOUT(child)) {
54 /* Child widget has native scroll support */
55 gtk_container_add(GTK_CONTAINER(scroller), child);
56 /* Fix up the step increments if they are 0 (seems like an odd default?) */
57 if(GTK_IS_LAYOUT(child)) {
58 adj = gtk_layout_get_hadjustment(GTK_LAYOUT(child));
59 if(!adj->step_increment) adj->step_increment = 16;
60 adj = gtk_layout_get_vadjustment(GTK_LAYOUT(child));
61 if(!adj->step_increment) adj->step_increment = 16;
64 /* Child widget requires a viewport */
65 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroller),
68 set_slider_colors(GTK_SCROLLED_WINDOW(scroller)->hscrollbar);
69 set_slider_colors(GTK_SCROLLED_WINDOW(scroller)->vscrollbar);
73 /** @brief Find an image
74 * @param name Relative path to image
75 * @return pixbuf containing image
77 * Images are cached so it's perfectly sensible to call this lots of times even
80 * Images are searched for in @c pkgdatadir/static.
82 GdkPixbuf *find_image(const char *name) {
83 static const struct cache_type image_cache_type = { INT_MAX };
90 if(!(pb = (GdkPixbuf *)cache_get(&image_cache_type, name))) {
91 if((n = TABLE_FIND(images, struct image, name, name)) >= 0) {
92 /* Use the built-in copy */
93 if(!(pb = gdk_pixbuf_new_from_inline(-1, images[n].data, FALSE, &err))) {
94 error(0, "%s", err->message);
98 /* See if there's a copy on disk */
99 byte_xasprintf(&path, "%s/static/%s", pkgdatadir, name);
100 if(!(pb = gdk_pixbuf_new_from_file(path, &err))) {
101 error(0, "%s", err->message);
106 cache_put(&image_cache_type, name, pb);
111 /** @brief Pop up a message */
112 void popup_msg(GtkMessageType mt, const char *msg) {
115 w = gtk_message_dialog_new(GTK_WINDOW(toplevel),
116 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
120 gtk_dialog_run(GTK_DIALOG(w));
121 gtk_widget_destroy(w);
124 /** @brief Pop up an error message */
125 void fpopup_msg(GtkMessageType mt, const char *fmt, ...) {
130 byte_xvasprintf(&msg, fmt, ap);
135 /** @brief Create a button with an icon in it
136 * @param path (relative) path to image
137 * @param tooltip Tooltip or NULL to not set one
140 GtkWidget *iconbutton(const char *path, const char *tip) {
141 GtkWidget *button, *content;;
145 button = gtk_button_new();
146 if((pb = find_image(path))) {
148 content = gtk_image_new_from_pixbuf(pb);
151 content = gtk_label_new(path);
153 gtk_container_add(GTK_CONTAINER(button), content);
155 gtk_tooltips_set_tip(tips, button, tip, "");
159 /** @brief Create buttons and pack them into an hbox */
160 GtkWidget *create_buttons(const struct button *buttons,
163 GtkWidget *const hbox = gtk_hbox_new(FALSE, 1);
165 for(n = 0; n < nbuttons; ++n) {
166 GtkWidget *const button = gtk_button_new_from_stock(buttons[n].stock);
167 g_signal_connect(G_OBJECT(button), "clicked",
168 G_CALLBACK(buttons[n].clicked), 0);
169 gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 1);
170 gtk_tooltips_set_tip(tips, button, buttons[n].tip, "");