| 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 | |
| 26 | /* Miscellaneous GTK+ stuff ------------------------------------------------ */ |
| 27 | |
| 28 | WT(cached_image); |
| 29 | |
| 30 | /* Functions */ |
| 31 | |
| 32 | /** @brief Put scrollbars around a widget |
| 33 | * @param child Widget to surround |
| 34 | * @param widgetname Name for (both) widgets |
| 35 | * @return Scroll widget |
| 36 | */ |
| 37 | GtkWidget *scroll_widget(GtkWidget *child, |
| 38 | const char *widgetname) { |
| 39 | GtkWidget *scroller = gtk_scrolled_window_new(0, 0); |
| 40 | GtkAdjustment *adj; |
| 41 | |
| 42 | D(("scroll_widget")); |
| 43 | /* Why isn't _AUTOMATIC the default? */ |
| 44 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller), |
| 45 | GTK_POLICY_AUTOMATIC, |
| 46 | GTK_POLICY_AUTOMATIC); |
| 47 | if(GTK_IS_LAYOUT(child)) { |
| 48 | /* Child widget has native scroll support */ |
| 49 | gtk_container_add(GTK_CONTAINER(scroller), child); |
| 50 | /* Fix up the step increments if they are 0 (seems like an odd default?) */ |
| 51 | if(GTK_IS_LAYOUT(child)) { |
| 52 | adj = gtk_layout_get_hadjustment(GTK_LAYOUT(child)); |
| 53 | if(!adj->step_increment) adj->step_increment = 16; |
| 54 | adj = gtk_layout_get_vadjustment(GTK_LAYOUT(child)); |
| 55 | if(!adj->step_increment) adj->step_increment = 16; |
| 56 | } |
| 57 | } else { |
| 58 | /* Child widget requires a viewport */ |
| 59 | gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroller), |
| 60 | child); |
| 61 | } |
| 62 | /* Apply a name to the widget so it can be recolored */ |
| 63 | gtk_widget_set_name(GTK_BIN(scroller)->child, widgetname); |
| 64 | gtk_widget_set_name(scroller, widgetname); |
| 65 | return scroller; |
| 66 | } |
| 67 | |
| 68 | /** @brief Find an image |
| 69 | * @param name Relative path to image |
| 70 | * @return pixbuf containing image |
| 71 | * |
| 72 | * Images are cached so it's perfectly sensible to call this lots of times even |
| 73 | * for the same image. |
| 74 | * |
| 75 | * Images are searched for in @c pkgdatadir/static. |
| 76 | */ |
| 77 | GdkPixbuf *find_image(const char *name) { |
| 78 | static const struct cache_type image_cache_type = { INT_MAX }; |
| 79 | |
| 80 | GdkPixbuf *pb; |
| 81 | char *path; |
| 82 | GError *err = 0; |
| 83 | |
| 84 | if(!(pb = (GdkPixbuf *)cache_get(&image_cache_type, name))) { |
| 85 | byte_xasprintf(&path, "%s/static/%s", pkgdatadir, name); |
| 86 | if(!(pb = gdk_pixbuf_new_from_file(path, &err))) { |
| 87 | error(0, "%s", err->message); |
| 88 | return 0; |
| 89 | } |
| 90 | NW(cached_image); |
| 91 | cache_put(&image_cache_type, name, pb); |
| 92 | } |
| 93 | return pb; |
| 94 | } |
| 95 | |
| 96 | /** @brief Pop up an error message */ |
| 97 | void popup_error(const char *msg) { |
| 98 | GtkWidget *w; |
| 99 | |
| 100 | w = gtk_message_dialog_new(GTK_WINDOW(toplevel), |
| 101 | GTK_DIALOG_MODAL|GTK_DIALOG_DESTROY_WITH_PARENT, |
| 102 | GTK_MESSAGE_ERROR, |
| 103 | GTK_BUTTONS_CLOSE, |
| 104 | "%s", msg); |
| 105 | gtk_dialog_run(GTK_DIALOG(w)); |
| 106 | gtk_widget_destroy(w); |
| 107 | } |
| 108 | |
| 109 | /** @brief Create a button with an icon in it |
| 110 | * @param path (relative) path to image |
| 111 | * @param tooltip Tooltip or NULL to not set one |
| 112 | * @return Button |
| 113 | */ |
| 114 | GtkWidget *iconbutton(const char *path, const char *tip) { |
| 115 | GtkWidget *button, *content;; |
| 116 | GdkPixbuf *pb; |
| 117 | |
| 118 | NW(button); |
| 119 | button = gtk_button_new(); |
| 120 | if((pb = find_image(path))) { |
| 121 | NW(image); |
| 122 | content = gtk_image_new_from_pixbuf(pb); |
| 123 | } else { |
| 124 | NW(label); |
| 125 | content = gtk_label_new(path); |
| 126 | } |
| 127 | gtk_container_add(GTK_CONTAINER(button), content); |
| 128 | if(tip) |
| 129 | gtk_tooltips_set_tip(tips, button, tip, ""); |
| 130 | return button; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | Local Variables: |
| 135 | c-basic-offset:2 |
| 136 | comment-column:40 |
| 137 | fill-column:79 |
| 138 | indent-tabs-mode:nil |
| 139 | End: |
| 140 | */ |