2 * This file is part of DisOrder
3 * Copyright (C) 2006-2008 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 3 of the License, or
8 * (at your option) any later version.
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.
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/>.
18 /** @file disobedience/misc.c
19 * @brief Miscellaneous GTK+ interfacing stuff
22 #include "disobedience.h"
32 /* Miscellaneous GTK+ stuff ------------------------------------------------ */
36 /** @brief Put scrollbars around a widget
37 * @param child Widget to surround
38 * @return Scroll widget
40 GtkWidget *scroll_widget(GtkWidget *child) {
41 GtkWidget *scroller = gtk_scrolled_window_new(0, 0);
44 gtk_widget_set_style(scroller, tool_style);
46 /* Why isn't _AUTOMATIC the default? */
47 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
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;
62 /* Child widget requires a viewport */
63 gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroller),
65 gtk_widget_set_style(gtk_bin_get_child(GTK_BIN(scroller)), tool_style);
67 gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->hscrollbar, tool_style);
68 gtk_widget_set_style(GTK_SCROLLED_WINDOW(scroller)->vscrollbar, tool_style);
72 /** @brief Put a frame round a widget
74 * @param label Label or NULL
75 * @return Frame widget
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);
90 /** @brief Find an image
91 * @param name Relative path to image
92 * @return pixbuf containing image
94 * Images are cached so it's perfectly sensible to call this lots of times even
97 * Images are searched for in @c pkgdatadir/static.
99 GdkPixbuf *find_image(const char *name) {
100 static const struct cache_type image_cache_type = { INT_MAX };
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 error(0, "%s", err->message);
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 error(0, "%s", err->message);
122 cache_put(&image_cache_type, name, pb);
127 /** @brief Pop up a message */
128 void popup_msg(GtkMessageType mt, const char *msg) {
129 popup_submsg(toplevel, mt, msg);
132 void popup_submsg(GtkWidget *parent, GtkMessageType mt, const char *msg) {
135 w = gtk_message_dialog_new(GTK_WINDOW(parent),
136 GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT,
140 gtk_widget_set_style(w, tool_style);
141 gtk_dialog_run(GTK_DIALOG(w));
142 gtk_widget_destroy(w);
145 /** @brief Pop up an error message */
146 void fpopup_msg(GtkMessageType mt, const char *fmt, ...) {
151 byte_xvasprintf(&msg, fmt, ap);
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
161 GtkWidget *iconbutton(const char *path, const char *tip) {
162 GtkWidget *button, *content;
165 button = gtk_button_new();
166 if((pb = find_image(path))) {
167 content = gtk_image_new_from_pixbuf(pb);
169 content = gtk_label_new(path);
171 gtk_widget_set_style(button, tool_style);
172 gtk_widget_set_style(content, tool_style);
173 gtk_container_add(GTK_CONTAINER(button), content);
175 gtk_tooltips_set_tip(tips, button, tip, "");
179 /** @brief Create buttons and pack them into a box, which is returned */
180 GtkWidget *create_buttons_box(struct button *buttons,
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 gtk_box_pack_start(GTK_BOX(box), buttons[n].widget, FALSE, FALSE, 1);
191 gtk_tooltips_set_tip(tips, buttons[n].widget, buttons[n].tip, "");
196 /** @brief Create buttons and pack them into an hbox */
197 GtkWidget *create_buttons(struct button *buttons,
199 return create_buttons_box(buttons, nbuttons,
200 gtk_hbox_new(FALSE, 1));