/*------------------------------------------------------------------------- * Copyright (c) 1999-2002 Kenneth W. Sodemann (stufflehead@bigfoot.com) *------------------------------------------------------------------------- * gtkutils * * Synopsis: * Some of these utility functions are copied from the code * created by Glade. This code is being placed here so that * I can create UI using Glade, but then split the code out * (something Glade does not currently fully support). * * Other functions and structures are used for passing data * around within a set of widgets. * * $Id: gtkutils.c,v 1.24 2003/12/26 23:20:42 stuffle Exp $ * * 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 of the License, or * (at your option) any later 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 * Free Software Foundation, Inc. * 59 Temple Place, Suite 330 * Boston, MA 02111-1307 USA * *------------------------------------------------------------------------- */ #include #include #include #include #ifdef HAVE_CONFIG_H # include #endif #include #include "gtkutils.h" #define DIRTY_FLAG "i_am_dirty" #define DIRTY_CHK_LIST "dirty_check_list" #define DIRTY 1 #define CLEAN 0 static GList *pixmaps_directories = NULL; void gtkutils_cleanup (void) { GList *list_iter; for (list_iter = g_list_first (pixmaps_directories); list_iter != NULL; list_iter = list_iter->next) { g_free (list_iter->data); } g_list_free (pixmaps_directories); pixmaps_directories = NULL; } void set_dirty (GObject *object, gpointer user_data) { g_object_set_data (object, DIRTY_FLAG, GINT_TO_POINTER (DIRTY)); } void set_clean (GObject *object, gpointer user_data) { g_object_set_data (object, DIRTY_FLAG, GINT_TO_POINTER (CLEAN)); } void set_all_clean (GObject *parent) { GList *child_list; GList *list_iter; child_list = g_object_get_data (G_OBJECT (parent), DIRTY_CHK_LIST); list_iter = g_list_first (child_list); while (list_iter != NULL) { set_clean (G_OBJECT (list_iter->data), NULL); list_iter = list_iter->next; } } void add_object_to_dirty_list (GObject *parent, GObject *object) { GList *dirty_list; dirty_list = g_object_get_data (parent, DIRTY_CHK_LIST); dirty_list = g_list_append (dirty_list, object); g_object_set_data (parent, DIRTY_CHK_LIST, dirty_list); } void destroy_dirty_list (GObject *parent) { GList *dirty_list; dirty_list = g_object_get_data (parent, DIRTY_CHK_LIST); if (dirty_list != NULL) { g_list_free (dirty_list); g_object_set_data (parent, DIRTY_CHK_LIST, NULL); } } gboolean object_is_dirty (GObject *object) { /* * Ya know, if we _really_ wanted to be anal, we could get the top * dog for this widget, then determine if it even had a dirty list, * then determine if this widget was even in it, but in the interest * economy, I will not.... */ return (GPOINTER_TO_INT (g_object_get_data (object, DIRTY_FLAG)) == DIRTY); } gboolean objects_children_are_dirty (GObject *parent) { GList *child_list; GList *list_iter; gboolean is_dirty; child_list = g_object_get_data (parent, DIRTY_CHK_LIST); list_iter = g_list_first (child_list); while (list_iter != NULL) { is_dirty = (GPOINTER_TO_INT (g_object_get_data (G_OBJECT (list_iter->data), DIRTY_FLAG)) == DIRTY); if (is_dirty) { return TRUE; } list_iter = list_iter->next; } return FALSE; } /* * Note that this child list is totally a different concept than the * DIRTY_CHK_LIST, which is technically a list of child widgets. Rather, * these "child" lists are used to setup a parent/child relationship * between top level widgets. */ void child_dead (GObject *child_widget, gchar *list_name) { GList *child_list; GtkWidget *parent; parent = GTK_WIDGET (g_object_get_data (child_widget, MY_PARENT)); g_assert (parent != NULL); child_list = g_object_get_data (G_OBJECT (parent), list_name); g_assert (child_list != NULL); child_list = g_list_remove (child_list, child_widget); g_object_set_data (G_OBJECT (parent), list_name, child_list); } void add_pixmap_directory (const gchar *directory) { pixmaps_directories = g_list_prepend (pixmaps_directories, g_strdup (directory)); } /* This is an internally used function to check if a pixmap file exists. */ #ifndef G_DIR_SEPARATOR_S #define G_DIR_SEPARATOR_S "/" #endif GtkWidget* lookup_widget (const GtkWidget *widget, const gchar *widget_name) { GtkWidget *parent, *found_widget; for (;;) { if (GTK_IS_MENU (widget)) parent = gtk_menu_get_attach_widget (GTK_MENU (widget)); else parent = widget->parent; if (!parent) parent = g_object_get_data (G_OBJECT (widget), "GladeParentKey"); if (parent == NULL) break; widget = parent; } found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget), widget_name); if (!found_widget) g_warning ("Widget not found: %s", widget_name); return found_widget; } GtkWidget* create_pixmap (GtkWidget *widget, const gchar *filename) { GtkWidget *pixmap; gchar *pathname; if (!filename || !filename[0]) return gtk_image_new (); pathname = gnome_program_locate_file (NULL, GNOME_FILE_DOMAIN_APP_PIXMAP, filename, TRUE, NULL); if (!pathname) { pathname = gnome_program_locate_file (NULL, GNOME_FILE_DOMAIN_APP_DATADIR, filename, TRUE, NULL); if (!pathname) { g_warning (_("Couldn't find pixmap file: %s"), filename); return gtk_image_new (); } } pixmap = gtk_image_new_from_file (pathname); g_free (pathname); return pixmap; }