comparison src/modplug/gui/support.cxx @ 1603:3ba928b31aac

Made the GUI dialogs (fileinfo & config) bit neater.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 05 Sep 2007 11:59:44 +0300
parents b1128efde471
children 499656950801
comparison
equal deleted inserted replaced
1602:b0550943c696 1603:3ba928b31aac
8 8
9 #include <sys/types.h> 9 #include <sys/types.h>
10 #include <sys/stat.h> 10 #include <sys/stat.h>
11 #include <unistd.h> 11 #include <unistd.h>
12 #include <string.h> 12 #include <string.h>
13 #include <stdio.h>
13 14
14 #include <gtk/gtk.h> 15 #include <gtk/gtk.h>
15 16
16 #include "support.h" 17 #include "support.h"
17
18 /* This is an internally used function to check if a pixmap file exists. */
19 static gchar* check_file_exists (const gchar *directory,
20 const gchar *filename);
21
22 /* This is an internally used function to create pixmaps. */
23 static GtkWidget* create_dummy_pixmap (GtkWidget *widget);
24 18
25 GtkWidget* 19 GtkWidget*
26 lookup_widget (GtkWidget *widget, 20 lookup_widget (GtkWidget *widget,
27 const gchar *widget_name) 21 const gchar *widget_name)
28 { 22 {
32 { 26 {
33 if (GTK_IS_MENU (widget)) 27 if (GTK_IS_MENU (widget))
34 parent = gtk_menu_get_attach_widget (GTK_MENU (widget)); 28 parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
35 else 29 else
36 parent = widget->parent; 30 parent = widget->parent;
31 if (!parent)
32 parent = (GtkWidget*) g_object_get_data (G_OBJECT (widget), "GladeParentKey");
37 if (parent == NULL) 33 if (parent == NULL)
38 break; 34 break;
39 widget = parent; 35 widget = parent;
40 } 36 }
41 37
42 found_widget = (GtkWidget*) gtk_object_get_data (GTK_OBJECT (widget), 38 found_widget = (GtkWidget*) g_object_get_data (G_OBJECT (widget),
43 widget_name); 39 widget_name);
44 if (!found_widget) 40 if (!found_widget)
45 g_warning ("Widget not found: %s", widget_name); 41 g_warning ("Widget not found: %s", widget_name);
46 return found_widget; 42 return found_widget;
47 }
48
49 /* This is a dummy pixmap we use when a pixmap can't be found. */
50 static const char *dummy_pixmap_xpm[] = {
51 /* columns rows colors chars-per-pixel */
52 "1 1 1 1",
53 " c None",
54 /* pixels */
55 " "
56 };
57
58 /* This is an internally used function to create pixmaps. */
59 static GtkWidget*
60 create_dummy_pixmap (GtkWidget *widget)
61 {
62 GdkColormap *colormap;
63 GdkPixmap *gdkpixmap;
64 GdkBitmap *mask;
65 GtkWidget *pixmap;
66
67 colormap = gtk_widget_get_colormap (widget);
68 gdkpixmap = gdk_pixmap_colormap_create_from_xpm_d (NULL, colormap, &mask,
69 NULL, (gchar **)dummy_pixmap_xpm);
70 if (gdkpixmap == NULL)
71 g_error ("Couldn't create replacement pixmap.");
72 pixmap = gtk_pixmap_new (gdkpixmap, mask);
73 gdk_pixmap_unref (gdkpixmap);
74 gdk_bitmap_unref (mask);
75 return pixmap;
76 } 43 }
77 44
78 static GList *pixmaps_directories = NULL; 45 static GList *pixmaps_directories = NULL;
79 46
80 /* Use this function to set the directory containing installed pixmaps. */ 47 /* Use this function to set the directory containing installed pixmaps. */
83 { 50 {
84 pixmaps_directories = g_list_prepend (pixmaps_directories, 51 pixmaps_directories = g_list_prepend (pixmaps_directories,
85 g_strdup (directory)); 52 g_strdup (directory));
86 } 53 }
87 54
55 /* This is an internally used function to find pixmap files. */
56 static gchar*
57 find_pixmap_file (const gchar *filename)
58 {
59 GList *elem;
60
61 /* We step through each of the pixmaps directory to find it. */
62 elem = pixmaps_directories;
63 while (elem)
64 {
65 gchar *pathname = g_strdup_printf ("%s%s%s", (gchar*)elem->data,
66 G_DIR_SEPARATOR_S, filename);
67 if (g_file_test (pathname, G_FILE_TEST_EXISTS))
68 return pathname;
69 g_free (pathname);
70 elem = elem->next;
71 }
72 return NULL;
73 }
74
88 /* This is an internally used function to create pixmaps. */ 75 /* This is an internally used function to create pixmaps. */
89 GtkWidget* 76 GtkWidget*
90 create_pixmap (GtkWidget *widget, 77 create_pixmap (GtkWidget *widget,
91 const gchar *filename) 78 const gchar *filename)
92 { 79 {
93 gchar *found_filename = NULL; 80 gchar *pathname = NULL;
94 GdkColormap *colormap;
95 GdkPixmap *gdkpixmap;
96 GdkBitmap *mask;
97 GtkWidget *pixmap; 81 GtkWidget *pixmap;
98 GList *elem;
99 82
100 /* We first try any pixmaps directories set by the application. */ 83 if (!filename || !filename[0])
101 elem = pixmaps_directories; 84 return gtk_image_new ();
102 while (elem) 85
86 pathname = find_pixmap_file (filename);
87
88 if (!pathname)
103 { 89 {
104 found_filename = check_file_exists ((gchar*)elem->data, filename); 90 g_warning (_("Couldn't find pixmap file: %s"), filename);
105 if (found_filename) 91 return gtk_image_new ();
106 break;
107 elem = elem->next;
108 } 92 }
109 93
110 /* If we haven't found the pixmap, try the source directory. */ 94 pixmap = gtk_image_new_from_file (pathname);
111 if (!found_filename) 95 g_free (pathname);
112 {
113 found_filename = check_file_exists ("../pixmaps", filename);
114 }
115
116 if (!found_filename)
117 {
118 g_warning (_("Couldn't find pixmap file: %s"), filename);
119 return create_dummy_pixmap (widget);
120 }
121
122 colormap = gtk_widget_get_colormap (widget);
123 gdkpixmap = gdk_pixmap_colormap_create_from_xpm (NULL, colormap, &mask,
124 NULL, found_filename);
125 if (gdkpixmap == NULL)
126 {
127 g_warning (_("Error loading pixmap file: %s"), found_filename);
128 g_free (found_filename);
129 return create_dummy_pixmap (widget);
130 }
131 g_free (found_filename);
132 pixmap = gtk_pixmap_new (gdkpixmap, mask);
133 gdk_pixmap_unref (gdkpixmap);
134 gdk_bitmap_unref (mask);
135 return pixmap; 96 return pixmap;
136 } 97 }
137 98
138 /* This is an internally used function to check if a pixmap file exists. */ 99 /* This is an internally used function to create pixmaps. */
139 gchar* 100 GdkPixbuf*
140 check_file_exists (const gchar *directory, 101 create_pixbuf (const gchar *filename)
141 const gchar *filename)
142 { 102 {
143 gchar *full_filename; 103 gchar *pathname = NULL;
144 struct stat s; 104 GdkPixbuf *pixbuf;
145 gint status; 105 GError *error = NULL;
146 106
147 full_filename = (gchar*) g_malloc (strlen (directory) + 1 107 if (!filename || !filename[0])
148 + strlen (filename) + 1); 108 return NULL;
149 strcpy (full_filename, directory);
150 strcat (full_filename, G_DIR_SEPARATOR_S);
151 strcat (full_filename, filename);
152 109
153 status = stat (full_filename, &s); 110 pathname = find_pixmap_file (filename);
154 if (status == 0 && S_ISREG (s.st_mode)) 111
155 return full_filename; 112 if (!pathname)
156 g_free (full_filename); 113 {
157 return NULL; 114 g_warning (_("Couldn't find pixmap file: %s"), filename);
115 return NULL;
116 }
117
118 pixbuf = gdk_pixbuf_new_from_file (pathname, &error);
119 if (!pixbuf)
120 {
121 fprintf (stderr, "Failed to load pixbuf file: %s: %s\n",
122 pathname, error->message);
123 g_error_free (error);
124 }
125 g_free (pathname);
126 return pixbuf;
158 } 127 }
159 128
129 /* This is used to set ATK action descriptions. */
130 void
131 glade_set_atk_action_description (AtkAction *action,
132 const gchar *action_name,
133 const gchar *description)
134 {
135 gint n_actions, i;
136
137 n_actions = atk_action_get_n_actions (action);
138 for (i = 0; i < n_actions; i++)
139 {
140 if (!strcmp (atk_action_get_name (action, i), action_name))
141 atk_action_set_description (action, i, description);
142 }
143 }
144