TSP: The Transport Sample Protocol



Main Page | Modules | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

support.c

00001 /*
00002  */
00003 
00004 #ifdef HAVE_CONFIG_H
00005 #  include <config.h>
00006 #endif
00007 
00008 #include <sys/types.h>
00009 #include <sys/stat.h>
00010 #include <unistd.h>
00011 #include <string.h>
00012 
00013 #include <gtk/gtk.h>
00014 
00015 #include "support.h"
00016 
00017 /*inline */int VAR_TYPE_VAL (char *str) {
00018   int i;
00019   for (i=0; variable_type_str[i] != NULL; i++) {
00020     if (g_strcasecmp(str, variable_type_str[i]) == 0)
00021       return i;
00022   }
00023   return -1;
00024 }
00025 
00026 
00027 gboolean is_visible (GtkWidget *widget) {
00028   GtkArg arg;
00029   
00030   arg.type = GTK_TYPE_BOOL;
00031   arg.name = "visible";
00032   
00033   gtk_widget_get(GTK_WIDGET(widget), &arg);
00034   return GTK_VALUE_BOOL(arg);
00035 }
00036 
00037 void set_style_recursively (GtkWidget *widget, gpointer data)
00038 {
00039   GtkStyle *style;
00040   
00041   /* --- Get the style --- */
00042   style = (GtkStyle *) data;
00043   
00044   /* --- Set the style of the widget --- */
00045   gtk_widget_set_style (widget, style);
00046   
00047   /* --- If it may have children widgets --- */
00048   if (GTK_IS_CONTAINER (widget)) {
00049     /* --- Set all the children's styles too. --- */
00050     gtk_container_foreach (GTK_CONTAINER (widget), 
00051                            set_style_recursively, style);
00052   }
00053 }
00054 
00055 
00056 
00057 /* This is an internally used function to check if a pixmap file exists. */
00058 static gchar* check_file_exists        (const gchar     *directory,
00059                                         const gchar     *filename);
00060 
00061 /* This is an internally used function to create pixmaps. */
00062 static GtkWidget* create_dummy_pixmap  (GtkWidget       *widget);
00063 
00064 GtkWidget*
00065 lookup_widget                          (GtkWidget       *widget,
00066                                         const gchar     *widget_name)
00067 {
00068   GtkWidget *parent, *found_widget;
00069 
00070   for (;;)
00071     {
00072       if (GTK_IS_MENU (widget))
00073         parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
00074       else
00075         parent = widget->parent;
00076       if (parent == NULL)
00077         break;
00078       widget = parent;
00079     }
00080 
00081   found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
00082                                                    widget_name);
00083   if (!found_widget)
00084     g_warning ("Widget not found: %s", widget_name);
00085   return found_widget;
00086 }
00087 
00088 /* This is a dummy pixmap we use when a pixmap can't be found. */
00089 static char *dummy_pixmap_xpm[] = {
00090 /* columns rows colors chars-per-pixel */
00091 "1 1 1 1",
00092 "  c None",
00093 /* pixels */
00094 " "
00095 };
00096 
00097 /* This is an internally used function to create pixmaps. */
00098 static GtkWidget*
00099 create_dummy_pixmap                    (GtkWidget       *widget)
00100 {
00101   GdkColormap *colormap;
00102   GdkPixmap *gdkpixmap;
00103   GdkBitmap *mask;
00104   GtkWidget *pixmap;
00105 
00106   colormap = gtk_widget_get_colormap (widget);
00107   gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
00108                                                      NULL, dummy_pixmap_xpm);
00109   if (gdkpixmap == NULL)
00110     g_error ("Couldn't create replacement pixmap.");
00111   pixmap = gtk_pixmap_new (gdkpixmap, mask);
00112   gdk_pixmap_unref (gdkpixmap);
00113   gdk_bitmap_unref (mask);
00114   return pixmap;
00115 }
00116 
00117 static GList *pixmaps_directories = NULL;
00118 
00119 /* Use this function to set the directory containing installed pixmaps. */
00120 void
00121 add_pixmap_directory                   (const gchar     *directory)
00122 {
00123   pixmaps_directories = g_list_prepend (pixmaps_directories,
00124                                         g_strdup (directory));
00125 }
00126 
00127 /* This is an internally used function to create pixmaps. */
00128 GtkWidget*
00129 create_pixmap                          (GtkWidget       *widget,
00130                                         const gchar     *filename)
00131 {
00132   gchar *found_filename = NULL;
00133   GdkColormap *colormap;
00134   GdkPixmap *gdkpixmap;
00135   GdkBitmap *mask;
00136   GtkWidget *pixmap;
00137   GList *elem;
00138 
00139   /* We first try any pixmaps directories set by the application. */
00140   elem = pixmaps_directories;
00141   while (elem)
00142     {
00143       found_filename = check_file_exists ((gchar*)elem->data, filename);
00144       if (found_filename)
00145         break;
00146       elem = elem->next;
00147     }
00148 
00149   /* If we haven't found the pixmap, try the source directory. */
00150   if (!found_filename)
00151     {
00152       found_filename = check_file_exists ("../pixmaps", filename);
00153     }
00154 
00155   if (!found_filename)
00156     {
00157       g_warning ("Couldn't find pixmap file: %s", filename);
00158       return create_dummy_pixmap (widget);
00159     }
00160 
00161   colormap = gtk_widget_get_colormap (widget);
00162   gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
00163                                                    NULL, found_filename);
00164   if (gdkpixmap == NULL)
00165     {
00166       g_warning ("Error loading pixmap file: %s", found_filename);
00167       g_free (found_filename);
00168       return create_dummy_pixmap (widget);
00169     }
00170   g_free (found_filename);
00171   pixmap = gtk_pixmap_new (gdkpixmap, mask);
00172   gdk_pixmap_unref (gdkpixmap);
00173   gdk_bitmap_unref (mask);
00174   return pixmap;
00175 }
00176 
00177 /* This is an internally used function to check if a pixmap file exists. */
00178 gchar*
00179 check_file_exists                      (const gchar     *directory,
00180                                         const gchar     *filename)
00181 {
00182   gchar *full_filename;
00183   struct stat s;
00184   gint status;
00185 
00186   full_filename = (gchar*) g_malloc (strlen (directory) + 1
00187                                      + strlen (filename) + 1);
00188   strcpy (full_filename, directory);
00189   strcat (full_filename, G_DIR_SEPARATOR_S);
00190   strcat (full_filename, filename);
00191 
00192   status = stat (full_filename, &s);
00193   if (status == 0 && S_ISREG (s.st_mode))
00194     return full_filename;
00195   g_free (full_filename);
00196   return NULL;
00197 }
00198 
Framework Home Page.

Beware !! TSP wave is coming...