1 | /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
---|
2 | /* |
---|
3 | * Categories configuration. |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * Rodrigo Moya <rodrigo@ximian.com> |
---|
7 | * |
---|
8 | * Copyright 2001, Ximian, Inc. |
---|
9 | */ |
---|
10 | |
---|
11 | #ifdef HAVE_CONFIG_H |
---|
12 | #include <config.h> |
---|
13 | #endif |
---|
14 | |
---|
15 | #include <string.h> |
---|
16 | #include <libgnomeui/gnome-dialog.h> |
---|
17 | #include <libgnome/gnome-i18n.h> |
---|
18 | #include <gdk-pixbuf/gdk-pixbuf.h> |
---|
19 | #include <gal/widgets/e-unicode.h> |
---|
20 | #include <gal/widgets/e-categories.h> |
---|
21 | #include "e-categories-config.h" |
---|
22 | #include "e-categories-master-list-wombat.h" |
---|
23 | |
---|
24 | static gboolean initialized = FALSE; |
---|
25 | static ECategoriesMasterListWombat *ecmlw = NULL; |
---|
26 | static GHashTable *icons_table = NULL; |
---|
27 | |
---|
28 | static void |
---|
29 | initialize_categories_config (void) |
---|
30 | { |
---|
31 | g_return_if_fail (initialized == FALSE); |
---|
32 | |
---|
33 | ecmlw = E_CATEGORIES_MASTER_LIST_WOMBAT (e_categories_master_list_wombat_new ()); |
---|
34 | icons_table = g_hash_table_new (g_str_hash, g_str_equal); |
---|
35 | /* FIXME: must free the two objects above when exiting */ |
---|
36 | |
---|
37 | initialized = TRUE; |
---|
38 | } |
---|
39 | |
---|
40 | /** |
---|
41 | * e_categories_config_get_color_for: |
---|
42 | * @category: Category to get the color for. |
---|
43 | * |
---|
44 | * Returns the representation of the color configured for the given |
---|
45 | * category |
---|
46 | * |
---|
47 | * Returns: An X color specification. |
---|
48 | */ |
---|
49 | const char * |
---|
50 | e_categories_config_get_color_for (const char *category) |
---|
51 | { |
---|
52 | int n; |
---|
53 | |
---|
54 | g_return_val_if_fail (category != NULL, NULL); |
---|
55 | |
---|
56 | if (!initialized) |
---|
57 | initialize_categories_config (); |
---|
58 | |
---|
59 | for (n = 0; |
---|
60 | n < e_categories_master_list_count (E_CATEGORIES_MASTER_LIST (ecmlw)); |
---|
61 | n++) { |
---|
62 | char *tmp_cat; |
---|
63 | |
---|
64 | tmp_cat = (char *) e_categories_master_list_nth (E_CATEGORIES_MASTER_LIST (ecmlw), n); |
---|
65 | if (tmp_cat && !strcmp (tmp_cat, category)) |
---|
66 | return e_categories_master_list_nth_color (E_CATEGORIES_MASTER_LIST (ecmlw), n); |
---|
67 | } |
---|
68 | |
---|
69 | return NULL; /* not found */ |
---|
70 | } |
---|
71 | |
---|
72 | /** |
---|
73 | * e_categories_config_set_color_for |
---|
74 | */ |
---|
75 | void |
---|
76 | e_categories_config_set_color_for (const char *category, const char *color) |
---|
77 | { |
---|
78 | /* FIXME: implement */ |
---|
79 | } |
---|
80 | |
---|
81 | /** |
---|
82 | * e_categories_config_get_icon_for: |
---|
83 | * @category: Category for which to get the icon. |
---|
84 | * @icon: A pointer to where the pixmap will be returned. |
---|
85 | * @mask: A pointer to where the mask will be returned. |
---|
86 | * |
---|
87 | * Returns the icon (and associated mask) configured for the |
---|
88 | * given category. |
---|
89 | */ |
---|
90 | gboolean |
---|
91 | e_categories_config_get_icon_for (const char *category, GdkPixmap **pixmap, GdkBitmap **mask) |
---|
92 | { |
---|
93 | char *icon_file; |
---|
94 | GdkPixbuf *pixbuf; |
---|
95 | GdkBitmap *tmp_mask; |
---|
96 | |
---|
97 | g_return_val_if_fail (pixmap != NULL, FALSE); |
---|
98 | |
---|
99 | icon_file = (char *) e_categories_config_get_icon_file_for (category); |
---|
100 | if (!icon_file) { |
---|
101 | *pixmap = NULL; |
---|
102 | if (mask != NULL) |
---|
103 | *mask = NULL; |
---|
104 | return FALSE; |
---|
105 | } |
---|
106 | |
---|
107 | /* load the icon in our list */ |
---|
108 | pixbuf = g_hash_table_lookup (icons_table, icon_file); |
---|
109 | if (!pixbuf) { |
---|
110 | pixbuf = gdk_pixbuf_new_from_file (icon_file); |
---|
111 | if (!pixbuf) { |
---|
112 | *pixmap = NULL; |
---|
113 | if (mask != NULL) |
---|
114 | *mask = NULL; |
---|
115 | return FALSE; |
---|
116 | } |
---|
117 | |
---|
118 | g_hash_table_insert (icons_table, g_strdup (icon_file), pixbuf); |
---|
119 | } |
---|
120 | |
---|
121 | /* render the pixbuf to the pixmap and mask passed */ |
---|
122 | gdk_pixbuf_render_pixmap_and_mask (pixbuf, pixmap, &tmp_mask, 1); |
---|
123 | if (mask != NULL) |
---|
124 | *mask = tmp_mask; |
---|
125 | |
---|
126 | return TRUE; |
---|
127 | } |
---|
128 | |
---|
129 | /** |
---|
130 | * e_categories_config_get_icon_file_for |
---|
131 | * @category: Category for which to get the icon file |
---|
132 | */ |
---|
133 | const char * |
---|
134 | e_categories_config_get_icon_file_for (const char *category) |
---|
135 | { |
---|
136 | int n; |
---|
137 | |
---|
138 | g_return_val_if_fail (category != NULL, NULL); |
---|
139 | |
---|
140 | if (!initialized) |
---|
141 | initialize_categories_config (); |
---|
142 | |
---|
143 | for (n = 0; |
---|
144 | n < e_categories_master_list_count (E_CATEGORIES_MASTER_LIST (ecmlw)); |
---|
145 | n++) { |
---|
146 | char *tmp_cat; |
---|
147 | |
---|
148 | tmp_cat = (char *) e_categories_master_list_nth (E_CATEGORIES_MASTER_LIST (ecmlw), n); |
---|
149 | if (tmp_cat && !strcmp (tmp_cat, category)) |
---|
150 | return e_categories_master_list_nth_icon (E_CATEGORIES_MASTER_LIST (ecmlw), n); |
---|
151 | } |
---|
152 | |
---|
153 | return NULL; /* not found */ |
---|
154 | } |
---|
155 | |
---|
156 | /** |
---|
157 | * e_categories_config_set_icon_for |
---|
158 | * @category: Category for which to set the icon. |
---|
159 | * @icon_file: Full path of the icon file. |
---|
160 | */ |
---|
161 | void |
---|
162 | e_categories_config_set_icon_for (const char *category, const char *icon_file) |
---|
163 | { |
---|
164 | } |
---|
165 | |
---|
166 | /** |
---|
167 | * e_categories_config_open_dialog_for_entry: |
---|
168 | * entry: A GtkEntry on which to get/set the categories list. |
---|
169 | * |
---|
170 | * This is a self-contained function that lets you open a popup dialog for |
---|
171 | * the user to select a list of categories. |
---|
172 | * |
---|
173 | * The @entry parameter is used, at initialization time, as the list of |
---|
174 | * initial categories that are selected in the categories selection dialog. |
---|
175 | * Then, when the user commits its changes, the list of selected categories |
---|
176 | * is put back on the entry widget. |
---|
177 | */ |
---|
178 | void |
---|
179 | e_categories_config_open_dialog_for_entry (GtkEntry *entry) |
---|
180 | { |
---|
181 | char *categories; |
---|
182 | GnomeDialog *dialog; |
---|
183 | int result; |
---|
184 | |
---|
185 | g_return_if_fail (entry != NULL); |
---|
186 | g_return_if_fail (GTK_IS_ENTRY (entry)); |
---|
187 | |
---|
188 | if (!initialized) |
---|
189 | initialize_categories_config (); |
---|
190 | |
---|
191 | categories = e_utf8_gtk_entry_get_text (GTK_ENTRY (entry)); |
---|
192 | dialog = GNOME_DIALOG (e_categories_new (categories)); |
---|
193 | |
---|
194 | gtk_object_set (GTK_OBJECT (dialog), |
---|
195 | "ecml", ecmlw, |
---|
196 | NULL); |
---|
197 | |
---|
198 | /* run the dialog */ |
---|
199 | result = gnome_dialog_run (dialog); |
---|
200 | g_free (categories); |
---|
201 | |
---|
202 | if (result == 0) { |
---|
203 | gtk_object_get (GTK_OBJECT (dialog), |
---|
204 | "categories", &categories, |
---|
205 | NULL); |
---|
206 | e_utf8_gtk_entry_set_text (GTK_ENTRY (entry), categories); |
---|
207 | g_free (categories); |
---|
208 | } |
---|
209 | |
---|
210 | gtk_object_destroy (GTK_OBJECT (dialog)); |
---|
211 | } |
---|