Institut de Robòtica i Informàtica Industrial
KRD Group

The CuikSuite Project

cuikplay_support.c

Go to the documentation of this file.
00001 
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <unistd.h>
00015 #include <string.h>
00016 #include <stdio.h>
00017 
00018 #include <gtk/gtk.h>
00019 
00020 #include "cuikplay_support.h"
00021 
00022 GtkWidget* lookup_widget(GtkWidget *widget,
00023                          const gchar *widget_name)
00024 {
00025   GtkWidget *parent, *found_widget;
00026 
00027   for (;;)
00028     {
00029       if (GTK_IS_MENU (widget))
00030         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
00031       else
00032         parent = widget->parent;
00033       if (!parent)
00034         parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
00035       if (parent == NULL)
00036         break;
00037       widget = parent;
00038     }
00039 
00040   found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
00041                                                  widget_name);
00042   if (!found_widget)
00043     g_warning ("Widget not found: %s", widget_name);
00044   return found_widget;
00045 }
00046 
00047 static GList *pixmaps_directories = NULL;
00048 
00049 /* Use this function to set the directory containing installed pixmaps. */
00050 void add_pixmap_directory (const gchar *directory)
00051 {
00052   pixmaps_directories = g_list_prepend (pixmaps_directories,
00053                                         g_strdup (directory));
00054 }
00055 
00056 /* This is an internally used function to find pixmap files. */
00057 static gchar* find_pixmap_file (const gchar *filename)
00058 {
00059   GList *elem;
00060 
00061   /* We step through each of the pixmaps directory to find it. */
00062   elem = pixmaps_directories;
00063   while (elem)
00064     {
00065       gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
00066                                          G_DIR_SEPARATOR_S, filename);
00067       if (g_file_test (pathname, G_FILE_TEST_EXISTS))
00068         return pathname;
00069       g_free (pathname);
00070       elem = elem->next;
00071     }
00072   return NULL;
00073 }
00074 
00075 /* This is an internally used function to create pixmaps. */
00076 GtkWidget* create_pixmap (GtkWidget *widget,const gchar *filename)
00077 {
00078   gchar *pathname = NULL;
00079   GtkWidget *pixmap;
00080 
00081   if (!filename || !filename[0])
00082       return gtk_image_new ();
00083 
00084   pathname = find_pixmap_file (filename);
00085 
00086   if (!pathname)
00087     {
00088       g_warning (_("Couldn't find pixmap file: %s"), filename);
00089       return gtk_image_new ();
00090     }
00091 
00092   pixmap = gtk_image_new_from_file (pathname);
00093   g_free (pathname);
00094   return pixmap;
00095 }
00096 
00097 /* This is an internally used function to create pixmaps. */
00098 GdkPixbuf* create_pixbuf(const gchar *filename)
00099 {
00100   gchar *pathname = NULL;
00101   GdkPixbuf *pixbuf;
00102   GError *error = NULL;
00103 
00104   if (!filename || !filename[0])
00105       return NULL;
00106 
00107   pathname = find_pixmap_file (filename);
00108 
00109   if (!pathname)
00110     {
00111       g_warning (_("Couldn't find pixmap file: %s"), filename);
00112       return NULL;
00113     }
00114 
00115   pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
00116   if (!pixbuf)
00117     {
00118       fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
00119                pathname, error->message);
00120       g_error_free (error);
00121     }
00122   g_free (pathname);
00123   return pixbuf;
00124 }
00125 
00126 /* This is used to set ATK action descriptions. */
00127 void glade_set_atk_action_description(AtkAction *action,
00128                                       const gchar *action_name,
00129                                       const gchar *description)
00130 {
00131   gint n_actions, i;
00132 
00133   n_actions = atk_action_get_n_actions (action);
00134   for (i = 0; i < n_actions; i++)
00135     {
00136       if (!strcmp (atk_action_get_name (action, i), action_name))
00137         atk_action_set_description (action, i, description);
00138     }
00139 }
00140