Project Homepage Sourceforge Page CVS Repository Freshmeat.net Page Download project Author's Homepage

support.c

Go to the documentation of this file.
00001 /*!\file 
00002   \brief glade, blah support functions
00003   DO NOT EDIT THIS FILE - it is generated by Glade.
00004 */
00005 /* NOTICE:
00006     Copyright (C) 2004  Karl N. Redman (SleepingStill.com)
00007 
00008     This program is free software; you can redistribute it and/or modify
00009     it under the terms of the GNU General Public License as published by
00010     the Free Software Foundation; either version 2 of the License, or
00011     (at your option) any later version.
00012 
00013     This program is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016     GNU General Public License for more details.
00017 
00018     You should have received a copy of the GNU General Public License
00019     along with this program; if not, write to the Free Software
00020     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00021 
00022     For further information contact: parasyte@sleepingstill.com
00023 */
00024 
00025 #ifdef HAVE_CONFIG_H
00026 #  include <config.h>
00027 #endif
00028 
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <unistd.h>
00032 #include <string.h>
00033 
00034 #include <gtk/gtk.h>
00035 
00036 #include "support.h"
00037 
00038 /* This is an internally used function to check if a pixmap file exists. */
00039 static gchar* check_file_exists        (const gchar     *directory,
00040                                         const gchar     *filename);
00041 
00042 /* This is an internally used function to create pixmaps. */
00043 static GtkWidget* create_dummy_pixmap  (GtkWidget       *widget);
00044 
00045 GtkWidget*
00046 lookup_widget                          (GtkWidget       *widget,
00047                                         const gchar     *widget_name)
00048 {
00049   GtkWidget *parent, *found_widget;
00050 
00051   for (;;)
00052     {
00053       if (GTK_IS_MENU (widget))
00054         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
00055       else
00056         parent = widget->parent;
00057       if (parent == NULL)
00058         break;
00059       widget = parent;
00060     }
00061 
00062   found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
00063                                                    widget_name);
00064   if (!found_widget)
00065     g_warning ("Widget not found: %s", widget_name);
00066   return found_widget;
00067 }
00068 
00069 /* This is a dummy pixmap we use when a pixmap can't be found. */
00070 static char *dummy_pixmap_xpm[] = {
00071 /* columns rows colors chars-per-pixel */
00072 "1 1 1 1",
00073 "  c None",
00074 /* pixels */
00075 " "
00076 };
00077 
00078 /* This is an internally used function to create pixmaps. */
00079 static GtkWidget*
00080 create_dummy_pixmap                    (GtkWidget       *widget)
00081 {
00082   GdkColormap *colormap;
00083   GdkPixmap *gdkpixmap;
00084   GdkBitmap *mask;
00085   GtkWidget *pixmap;
00086 
00087   colormap = gtk_widget_get_colormap (widget);
00088   gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
00089                                                      NULL, dummy_pixmap_xpm);
00090   if (gdkpixmap == NULL)
00091     g_error ("Couldn't create replacement pixmap.");
00092   pixmap = gtk_pixmap_new (gdkpixmap, mask);
00093   gdk_pixmap_unref (gdkpixmap);
00094   gdk_bitmap_unref (mask);
00095   return pixmap;
00096 }
00097 
00098 static GList *pixmaps_directories = NULL;
00099 
00100 /* Use this function to set the directory containing installed pixmaps. */
00101 void
00102 add_pixmap_directory                   (const gchar     *directory)
00103 {
00104   pixmaps_directories = g_list_prepend (pixmaps_directories,
00105                                         g_strdup (directory));
00106 }
00107 
00108 /* This is an internally used function to create pixmaps. */
00109 GtkWidget*
00110 create_pixmap                          (GtkWidget       *widget,
00111                                         const gchar     *filename)
00112 {
00113   gchar *found_filename = NULL;
00114   GdkColormap *colormap;
00115   GdkPixmap *gdkpixmap;
00116   GdkBitmap *mask;
00117   GtkWidget *pixmap;
00118   GList *elem;
00119 
00120   if (!filename || !filename[0])
00121       return create_dummy_pixmap (widget);
00122 
00123   /* We first try any pixmaps directories set by the application. */
00124   elem = pixmaps_directories;
00125   while (elem)
00126     {
00127       found_filename = check_file_exists ((gchar*)elem->data, filename);
00128       if (found_filename)
00129         break;
00130       elem = elem->next;
00131     }
00132 
00133   /* If we haven't found the pixmap, try the source directory. */
00134   if (!found_filename)
00135     {
00136       found_filename = check_file_exists ("../pixmaps", filename);
00137     }
00138 
00139   if (!found_filename)
00140     {
00141       g_warning ("Couldn't find pixmap file: %s", filename);
00142       return create_dummy_pixmap (widget);
00143     }
00144 
00145   colormap = gtk_widget_get_colormap (widget);
00146   gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
00147                                                    NULL, found_filename);
00148   if (gdkpixmap == NULL)
00149     {
00150       g_warning ("Error loading pixmap file: %s", found_filename);
00151       g_free (found_filename);
00152       return create_dummy_pixmap (widget);
00153     }
00154   g_free (found_filename);
00155   pixmap = gtk_pixmap_new (gdkpixmap, mask);
00156   gdk_pixmap_unref (gdkpixmap);
00157   gdk_bitmap_unref (mask);
00158   return pixmap;
00159 }
00160 
00161 /* This is an internally used function to check if a pixmap file exists. */
00162 static gchar*
00163 check_file_exists                      (const gchar     *directory,
00164                                         const gchar     *filename)
00165 {
00166   gchar *full_filename;
00167   struct stat s;
00168   gint status;
00169 
00170   full_filename = (gchar*) g_malloc (strlen (directory) + 1
00171                                      + strlen (filename) + 1);
00172   strcpy (full_filename, directory);
00173   strcat (full_filename, G_DIR_SEPARATOR_S);
00174   strcat (full_filename, filename);
00175 
00176   status = stat (full_filename, &s);
00177   if (status == 0 && S_ISREG (s.st_mode))
00178     return full_filename;
00179   g_free (full_filename);
00180   return NULL;
00181 }
00182 

Generated on Thu Mar 18 07:26:17 2004 for run-free by doxygen 1.3.5