1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*
* Copyright (C) 2002 Jorn Baayen
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* The Rhythmbox authors hereby grant permission for non-GPL compatible
* GStreamer plugins to be used and distributed together with GStreamer
* and Rhythmbox. This permission is above and beyond the permissions granted
* by the GPL license by which Rhythmbox is covered. If you modify this code
* you may extend this exception to your version of the code, but you are not
* obligated to do so. If you do not wish to do so, delete this exception
* statement from your version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "config.h"
#include <gtk/gtk.h>
#include <string.h>
#include "rb-builder-helpers.h"
#include "rb-file-helpers.h"
/**
* SECTION:rbbuilderhelpers
* @short_description: helper functions for dealing with GtkBuilder files
*
* Some simple helper functions to make it a bit easier to deal with
* widgets built from GtkBuilder files.
*/
/**
* rb_builder_load:
* @file: filename, either absolute or relative to the data directory
* @user_data: user data to pass to autoconnected signal handlers
*
* Locates and reads a GtkBuilder file, automatically connecting
* signal handlers where possible.
*
* The caller can specify an absolute path to the file, a resource path
* starting with /org/gnome/Rhythmbox/ or just a filename.
* The file will be loaded from GResources.
*
* Return value: (transfer full): #GtkBuilder object built from the file
*/
GtkBuilder *
rb_builder_load (const char *file, gpointer user_data)
{
GtkBuilder *builder;
const char *name;
GError *error = NULL;
char *resource;
g_return_val_if_fail (file != NULL, NULL);
if (g_str_has_prefix (file, "/org/gnome/Rhythmbox/")) {
resource = g_strdup (file);
name = NULL;
} else if (g_path_is_absolute (file)) {
name = file;
resource = NULL;
} else {
resource = g_strdup_printf ("/org/gnome/Rhythmbox/ui/%s", file);
name = NULL;
}
builder = gtk_builder_new ();
gtk_builder_set_translation_domain (builder, GETTEXT_PACKAGE);
if (resource != NULL) {
if (gtk_builder_add_from_resource (builder, resource, &error) == 0) {
g_warning ("Error loading GtkBuilder resource %s; %s", resource, error->message);
g_error_free (error);
}
} else {
if (gtk_builder_add_from_file (builder, name, &error) == 0) {
g_warning ("Error loading GtkBuilder file %s: %s", name, error->message);
g_error_free (error);
}
}
gtk_builder_connect_signals (builder, user_data);
g_free (resource);
return builder;
}
/**
* rb_builder_load_plugin_file:
* @plugin: #RBPlugin instance
* @file: name of file to load
* @user_data: user data to pass to autoconnected signal handlers
*
* Like #rb_builder_load, except it finds files associated with
* plugins as well as those in the core data directories.
*
* Return value: (transfer full): #GtkBuilder object built from the file
*/
GtkBuilder *
rb_builder_load_plugin_file (GObject *plugin, const char *file, gpointer user_data)
{
char *path = NULL;
GtkBuilder *builder;
GBytes *bytes;
path = rb_find_plugin_resource (plugin, file);
bytes = g_resources_lookup_data (path, G_RESOURCE_LOOKUP_FLAGS_NONE, NULL);
if (bytes != NULL) {
g_bytes_unref (bytes);
} else {
g_free (path);
path = NULL;
}
if (path == NULL) {
path = rb_find_plugin_data_file (plugin, file);
if (path == NULL) {
return NULL;
}
}
builder = rb_builder_load (path, user_data);
g_free (path);
return builder;
}
/**
* rb_builder_boldify_label:
* @builder: a #GtkBuilder instance
* @name: name of the label to boldify
*
* Makes a label built from a GtkBuilder file bold.
*/
void
rb_builder_boldify_label (GtkBuilder *builder, const char *name)
{
GObject *widget;
gchar *str_final;
/* once we require gtk+ 2.16 or newer, we can set the attributes on
* the labels in the builder files, so we won't need this any more.
*/
widget = gtk_builder_get_object (builder, name);
if (widget == NULL) {
g_warning ("widget '%s' not found", name);
return;
}
str_final = g_strdup_printf ("<b>%s</b>", gtk_label_get_label (GTK_LABEL (widget)));
gtk_label_set_markup_with_mnemonic (GTK_LABEL (widget), str_final);
g_free (str_final);
}
/**
* rb_combo_box_hyphen_separator_func:
* @model: a #GtkTreeModel
* @iter: a #GtkTreeIter
* @data: nothing
*
* A row separator function to use for GtkComboBox widgets.
* It expects the model to contain a string in its first column,
* and interprets a string containing a single hyphen character
* as a separator.
*
* Return value: %TRUE if the row pointed to by @iter is a separator
*/
gboolean
rb_combo_box_hyphen_separator_func (GtkTreeModel *model,
GtkTreeIter *iter,
gpointer data)
{
const char *s;
gtk_tree_model_get (model, iter, 0, &s, -1);
if (s == NULL)
return FALSE;
return (strcmp (s, "-") == 0);
}
|