/xbmc/visualizations/Goom/goom2k4-0/sdl-goom/gtk-support.c

http://github.com/xbmc/xbmc · C · 162 lines · 124 code · 24 blank · 14 comment · 18 complexity · b2e0a1b6ad0d5d48ebb12ec89872fba2 MD5 · raw file

  1. /*
  2. * DO NOT EDIT THIS FILE - it is generated by Glade.
  3. */
  4. #ifdef HAVE_CONFIG_H
  5. # include <config.h>
  6. #endif
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <string.h>
  11. #include <gtk/gtk.h>
  12. #include "gtk-support.h"
  13. /* This is an internally used function to check if a pixmap file exists. */
  14. static gchar* check_file_exists (const gchar *directory,
  15. const gchar *filename);
  16. /* This is an internally used function to create pixmaps. */
  17. static GtkWidget* create_dummy_pixmap (GtkWidget *widget);
  18. GtkWidget*
  19. lookup_widget (GtkWidget *widget,
  20. const gchar *widget_name)
  21. {
  22. GtkWidget *parent, *found_widget;
  23. for (;;)
  24. {
  25. if (GTK_IS_MENU (widget))
  26. parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
  27. else
  28. parent = widget->parent;
  29. if (parent == NULL)
  30. break;
  31. widget = parent;
  32. }
  33. found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget),
  34. widget_name);
  35. if (!found_widget)
  36. g_warning ("Widget not found: %s", widget_name);
  37. return found_widget;
  38. }
  39. /* This is a dummy pixmap we use when a pixmap can't be found. */
  40. static char *dummy_pixmap_xpm[] = {
  41. /* columns rows colors chars-per-pixel */
  42. "1 1 1 1",
  43. " c None",
  44. /* pixels */
  45. " "
  46. };
  47. /* This is an internally used function to create pixmaps. */
  48. static GtkWidget*
  49. create_dummy_pixmap (GtkWidget *widget)
  50. {
  51. GdkColormap *colormap;
  52. GdkPixmap *gdkpixmap;
  53. GdkBitmap *mask;
  54. GtkWidget *pixmap;
  55. colormap = gtk_widget_get_colormap (widget);
  56. gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
  57. NULL, dummy_pixmap_xpm);
  58. if (gdkpixmap == NULL)
  59. g_error ("Couldn't create replacement pixmap.");
  60. pixmap = gtk_pixmap_new (gdkpixmap, mask);
  61. gdk_pixmap_unref (gdkpixmap);
  62. gdk_bitmap_unref (mask);
  63. return pixmap;
  64. }
  65. static GList *pixmaps_directories = NULL;
  66. /* Use this function to set the directory containing installed pixmaps. */
  67. void
  68. add_pixmap_directory (const gchar *directory)
  69. {
  70. pixmaps_directories = g_list_prepend (pixmaps_directories,
  71. g_strdup (directory));
  72. }
  73. /* This is an internally used function to create pixmaps. */
  74. GtkWidget*
  75. create_pixmap (GtkWidget *widget,
  76. const gchar *filename)
  77. {
  78. gchar *found_filename = NULL;
  79. GdkColormap *colormap;
  80. GdkPixmap *gdkpixmap;
  81. GdkBitmap *mask;
  82. GtkWidget *pixmap;
  83. GList *elem;
  84. if (!filename || !filename[0])
  85. return create_dummy_pixmap (widget);
  86. /* We first try any pixmaps directories set by the application. */
  87. elem = pixmaps_directories;
  88. while (elem)
  89. {
  90. found_filename = check_file_exists ((gchar*)elem->data, filename);
  91. if (found_filename)
  92. break;
  93. elem = elem->next;
  94. }
  95. /* If we haven't found the pixmap, try the source directory. */
  96. if (!found_filename)
  97. {
  98. found_filename = check_file_exists ("../pixmaps", filename);
  99. }
  100. if (!found_filename)
  101. {
  102. g_warning (_("Couldn't find pixmap file: %s"), filename);
  103. return create_dummy_pixmap (widget);
  104. }
  105. colormap = gtk_widget_get_colormap (widget);
  106. gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
  107. NULL, found_filename);
  108. if (gdkpixmap == NULL)
  109. {
  110. g_warning (_("Error loading pixmap file: %s"), found_filename);
  111. g_free (found_filename);
  112. return create_dummy_pixmap (widget);
  113. }
  114. g_free (found_filename);
  115. pixmap = gtk_pixmap_new (gdkpixmap, mask);
  116. gdk_pixmap_unref (gdkpixmap);
  117. gdk_bitmap_unref (mask);
  118. return pixmap;
  119. }
  120. /* This is an internally used function to check if a pixmap file exists. */
  121. gchar*
  122. check_file_exists (const gchar *directory,
  123. const gchar *filename)
  124. {
  125. gchar *full_filename;
  126. struct stat s;
  127. gint status;
  128. full_filename = (gchar*) g_malloc (strlen (directory) + 1
  129. + strlen (filename) + 1);
  130. strcpy (full_filename, directory);
  131. strcat (full_filename, G_DIR_SEPARATOR_S);
  132. strcat (full_filename, filename);
  133. status = stat (full_filename, &s);
  134. if (status == 0 && S_ISREG (s.st_mode))
  135. return full_filename;
  136. g_free (full_filename);
  137. return NULL;
  138. }