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