1 | /* |
---|
2 | * This is an utter hack |
---|
3 | * |
---|
4 | * Copyright (C) 2001 Eazel, Inc. |
---|
5 | * |
---|
6 | * Author: George Lebl <jirka@5z.com> |
---|
7 | */ |
---|
8 | |
---|
9 | |
---|
10 | #include "config.h" |
---|
11 | #include <gnome.h> |
---|
12 | |
---|
13 | #include "icon-entry-hack.h" |
---|
14 | |
---|
15 | static GtkWidget * |
---|
16 | find_iconsel (GtkWidget *box) |
---|
17 | { |
---|
18 | GList *list, *li; |
---|
19 | |
---|
20 | list = gtk_container_children (GTK_CONTAINER (box)); |
---|
21 | |
---|
22 | for (li = list; li != NULL; li = li->next) { |
---|
23 | GtkWidget *w = li->data; |
---|
24 | if (GNOME_IS_ICON_SELECTION (w)) { |
---|
25 | g_list_free (list); |
---|
26 | return w; |
---|
27 | } |
---|
28 | } |
---|
29 | |
---|
30 | g_list_free (list); |
---|
31 | return NULL; |
---|
32 | } |
---|
33 | |
---|
34 | static void |
---|
35 | sync_new_to_orig (GnomeIconEntry *ientry) |
---|
36 | { |
---|
37 | char *file; |
---|
38 | GnomeIconEntry *origientry; |
---|
39 | GtkWidget *entry; |
---|
40 | |
---|
41 | origientry = gtk_object_get_data (GTK_OBJECT (ientry), |
---|
42 | "IconHackOrigIconEntry"); |
---|
43 | |
---|
44 | file = gnome_icon_entry_get_filename (ientry); |
---|
45 | |
---|
46 | if (origientry != NULL) { |
---|
47 | gtk_object_set_data (GTK_OBJECT (origientry), "IconHackNoSync", |
---|
48 | GINT_TO_POINTER (1)); |
---|
49 | |
---|
50 | gnome_icon_entry_set_icon (origientry, file); |
---|
51 | |
---|
52 | entry = gnome_icon_entry_gtk_entry |
---|
53 | (GNOME_ICON_ENTRY (origientry)); |
---|
54 | gtk_signal_emit_by_name (GTK_OBJECT (entry), "changed"); |
---|
55 | |
---|
56 | gtk_object_remove_data (GTK_OBJECT (origientry), "IconHackNoSync"); |
---|
57 | } else { |
---|
58 | if (file != NULL) |
---|
59 | gtk_object_set_data_full (GTK_OBJECT (ientry), |
---|
60 | "IconHackText", |
---|
61 | g_strdup (file), |
---|
62 | (GtkDestroyNotify) g_free); |
---|
63 | else |
---|
64 | gtk_object_remove_data (GTK_OBJECT (ientry), |
---|
65 | "IconHackText"); |
---|
66 | entry = gnome_icon_entry_gtk_entry (GNOME_ICON_ENTRY (ientry)); |
---|
67 | gtk_signal_emit_by_name (GTK_OBJECT (entry), "changed"); |
---|
68 | } |
---|
69 | |
---|
70 | g_free (file); |
---|
71 | } |
---|
72 | |
---|
73 | static void |
---|
74 | sync_orig_to_new (GnomeIconEntry *ientry) |
---|
75 | { |
---|
76 | char *file; |
---|
77 | GnomeIconEntry *origientry; |
---|
78 | |
---|
79 | origientry = gtk_object_get_data (GTK_OBJECT (ientry), |
---|
80 | "IconHackOrigIconEntry"); |
---|
81 | |
---|
82 | if (origientry != NULL) { |
---|
83 | file = gnome_icon_entry_get_filename (origientry); |
---|
84 | } else { |
---|
85 | file = g_strdup (gtk_object_get_data |
---|
86 | (GTK_OBJECT (ientry), "IconHackText")); |
---|
87 | } |
---|
88 | gnome_icon_entry_set_icon (ientry, file); |
---|
89 | g_free (file); |
---|
90 | } |
---|
91 | |
---|
92 | static void |
---|
93 | idle_remove (gpointer data) |
---|
94 | { |
---|
95 | guint idle = GPOINTER_TO_UINT (data); |
---|
96 | |
---|
97 | gtk_idle_remove (idle); |
---|
98 | } |
---|
99 | |
---|
100 | static gboolean |
---|
101 | sync_orig_to_new_idle (gpointer data) |
---|
102 | { |
---|
103 | sync_orig_to_new (data); |
---|
104 | gtk_object_remove_data (GTK_OBJECT (data), "IconHackToNewIdle"); |
---|
105 | return FALSE; |
---|
106 | } |
---|
107 | |
---|
108 | static void |
---|
109 | add_sync_orig_to_new_idle_for_entry (GnomeIconEntry *ientry) |
---|
110 | { |
---|
111 | guint idle = GPOINTER_TO_UINT (gtk_object_get_data (GTK_OBJECT (ientry), |
---|
112 | "IconHackToNewIdle")); |
---|
113 | if (idle != 0) |
---|
114 | return; |
---|
115 | |
---|
116 | idle = gtk_idle_add (sync_orig_to_new_idle, ientry); |
---|
117 | |
---|
118 | gtk_object_set_data_full (GTK_OBJECT (ientry), "IconHackToNewIdle", |
---|
119 | GUINT_TO_POINTER (idle), |
---|
120 | (GtkDestroyNotify) idle_remove); |
---|
121 | } |
---|
122 | |
---|
123 | static gboolean |
---|
124 | sync_new_to_orig_idle (gpointer data) |
---|
125 | { |
---|
126 | sync_new_to_orig (data); |
---|
127 | gtk_object_remove_data (GTK_OBJECT (data), "IconHackToOrigIdle"); |
---|
128 | return FALSE; |
---|
129 | } |
---|
130 | |
---|
131 | static void |
---|
132 | add_sync_new_to_orig_idle_for_entry (GnomeIconEntry *ientry) |
---|
133 | { |
---|
134 | guint idle = GPOINTER_TO_UINT (gtk_object_get_data (GTK_OBJECT (ientry), |
---|
135 | "IconHackToOrigIdle")); |
---|
136 | if (idle != 0) |
---|
137 | return; |
---|
138 | |
---|
139 | idle = gtk_idle_add (sync_new_to_orig_idle, ientry); |
---|
140 | |
---|
141 | gtk_object_set_data_full (GTK_OBJECT (ientry), "IconHackToOrigIdle", |
---|
142 | GUINT_TO_POINTER (idle), |
---|
143 | (GtkDestroyNotify) idle_remove); |
---|
144 | } |
---|
145 | |
---|
146 | static void |
---|
147 | icon_selected (GtkWidget *w, gint num, GdkEvent *event, gpointer data) |
---|
148 | { |
---|
149 | if(event && |
---|
150 | event->type == GDK_2BUTTON_PRESS && |
---|
151 | ((GdkEventButton *)event)->button == 1) { |
---|
152 | sync_new_to_orig (data); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | static gboolean |
---|
157 | delete_event (GtkWidget *w, GdkEvent *event, gpointer data) |
---|
158 | { |
---|
159 | GnomeIconEntry *ientry = data; |
---|
160 | |
---|
161 | gtk_object_set_data (GTK_OBJECT (ientry->pick_dialog), |
---|
162 | "IconEntryHackCanceled", |
---|
163 | GINT_TO_POINTER (1)); |
---|
164 | |
---|
165 | sync_orig_to_new (ientry); |
---|
166 | |
---|
167 | return FALSE; |
---|
168 | } |
---|
169 | |
---|
170 | static void |
---|
171 | cancel (GtkWidget *w, gpointer data) |
---|
172 | { |
---|
173 | GnomeIconEntry *ientry = data; |
---|
174 | |
---|
175 | gtk_object_set_data (GTK_OBJECT (ientry->pick_dialog), |
---|
176 | "IconEntryHackCanceled", |
---|
177 | GINT_TO_POINTER (1)); |
---|
178 | |
---|
179 | sync_orig_to_new (ientry); |
---|
180 | } |
---|
181 | |
---|
182 | static void |
---|
183 | dialog_hide (GtkWidget *w, gpointer data) |
---|
184 | { |
---|
185 | GnomeIconEntry *ientry = data; |
---|
186 | |
---|
187 | if (gtk_object_get_data (GTK_OBJECT (ientry->pick_dialog), |
---|
188 | "IconEntryHackCanceled") == NULL) { |
---|
189 | sync_new_to_orig (ientry); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | static void |
---|
194 | shown_icon_selection (GtkWidget *w, gpointer data) |
---|
195 | { |
---|
196 | GnomeIconEntry *ientry = data; |
---|
197 | |
---|
198 | if (ientry->pick_dialog == NULL) |
---|
199 | return; |
---|
200 | |
---|
201 | gtk_object_remove_data (GTK_OBJECT (ientry->pick_dialog), |
---|
202 | "IconEntryHackCanceled"); |
---|
203 | |
---|
204 | if ( ! gtk_object_get_data |
---|
205 | (GTK_OBJECT (ientry->pick_dialog), "IconHackDidConnect")) { |
---|
206 | GtkWidget *iconsel; |
---|
207 | |
---|
208 | gnome_dialog_button_connect_object |
---|
209 | (GNOME_DIALOG (ientry->pick_dialog), |
---|
210 | 0, /* OK button */ |
---|
211 | GTK_SIGNAL_FUNC (sync_new_to_orig), |
---|
212 | GTK_OBJECT (ientry)); |
---|
213 | gnome_dialog_button_connect |
---|
214 | (GNOME_DIALOG (ientry->pick_dialog), |
---|
215 | 1, /* Cancel button */ |
---|
216 | GTK_SIGNAL_FUNC (cancel), |
---|
217 | ientry); |
---|
218 | |
---|
219 | iconsel = find_iconsel |
---|
220 | (GNOME_DIALOG (ientry->pick_dialog)->vbox); |
---|
221 | |
---|
222 | g_assert (iconsel != NULL); |
---|
223 | |
---|
224 | gtk_signal_connect_after |
---|
225 | (GTK_OBJECT (GNOME_ICON_SELECTION (iconsel)->gil), |
---|
226 | "select_icon", |
---|
227 | GTK_SIGNAL_FUNC (icon_selected), |
---|
228 | ientry); |
---|
229 | |
---|
230 | gtk_signal_connect (GTK_OBJECT (ientry->pick_dialog), |
---|
231 | "hide", |
---|
232 | GTK_SIGNAL_FUNC (dialog_hide), |
---|
233 | ientry); |
---|
234 | gtk_signal_connect (GTK_OBJECT (ientry->pick_dialog), |
---|
235 | "delete_event", |
---|
236 | GTK_SIGNAL_FUNC (delete_event), |
---|
237 | ientry); |
---|
238 | |
---|
239 | gtk_object_set_data (GTK_OBJECT (ientry->pick_dialog), |
---|
240 | "IconHackDidConnect", |
---|
241 | GINT_TO_POINTER (1)); |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | static void |
---|
246 | orig_entry_changed (GtkWidget *w, gpointer data) |
---|
247 | { |
---|
248 | GnomeIconEntry *ientry = data; |
---|
249 | GnomeIconEntry *origientry; |
---|
250 | |
---|
251 | origientry = gtk_object_get_data (GTK_OBJECT (ientry), |
---|
252 | "IconHackOrigIconEntry"); |
---|
253 | |
---|
254 | if ( ! gtk_object_get_data (GTK_OBJECT (origientry), "IconHackNoSync")) { |
---|
255 | add_sync_orig_to_new_idle_for_entry (ientry); |
---|
256 | } |
---|
257 | } |
---|
258 | |
---|
259 | void |
---|
260 | hack_dentry_edit (GnomeDEntryEdit *dedit) |
---|
261 | { |
---|
262 | GtkWidget *ientry; |
---|
263 | GtkWidget *parent; |
---|
264 | GtkWidget *entry; |
---|
265 | |
---|
266 | g_return_if_fail (GNOME_IS_DENTRY_EDIT (dedit)); |
---|
267 | |
---|
268 | ientry = gnome_icon_entry_new ("icon", _("Choose an icon")); |
---|
269 | gtk_widget_show (ientry); |
---|
270 | gtk_object_set_data (GTK_OBJECT (ientry), |
---|
271 | "IconHackOrigIconEntry", dedit->icon_entry); |
---|
272 | |
---|
273 | entry = gnome_icon_entry_gtk_entry |
---|
274 | (GNOME_ICON_ENTRY (dedit->icon_entry)); |
---|
275 | gtk_signal_connect (GTK_OBJECT (entry), "changed", |
---|
276 | GTK_SIGNAL_FUNC (orig_entry_changed), |
---|
277 | ientry); |
---|
278 | |
---|
279 | gtk_widget_ref (dedit->icon_entry); |
---|
280 | |
---|
281 | /* it gets destroyed, so we lose the last refcount then */ |
---|
282 | gtk_signal_connect (GTK_OBJECT (dedit->icon_entry), "destroy", |
---|
283 | GTK_SIGNAL_FUNC (gtk_widget_unref), |
---|
284 | NULL); |
---|
285 | |
---|
286 | parent = dedit->icon_entry->parent; |
---|
287 | |
---|
288 | gtk_container_remove (GTK_CONTAINER (parent), dedit->icon_entry); |
---|
289 | |
---|
290 | gtk_box_pack_start (GTK_BOX (parent), ientry, |
---|
291 | FALSE, FALSE, 0); |
---|
292 | |
---|
293 | gtk_box_reorder_child (GTK_BOX (parent), ientry, 0); |
---|
294 | |
---|
295 | gtk_signal_connect (GTK_OBJECT (GNOME_ICON_ENTRY (ientry)->pickbutton), |
---|
296 | "clicked", |
---|
297 | GTK_SIGNAL_FUNC (shown_icon_selection), ientry); |
---|
298 | |
---|
299 | sync_orig_to_new (GNOME_ICON_ENTRY (ientry)); |
---|
300 | } |
---|
301 | |
---|
302 | |
---|
303 | void |
---|
304 | hack_icon_entry (GnomeIconEntry *ientry) |
---|
305 | { |
---|
306 | gtk_signal_connect (GTK_OBJECT (GNOME_ICON_ENTRY (ientry)->pickbutton), |
---|
307 | "clicked", |
---|
308 | GTK_SIGNAL_FUNC (shown_icon_selection), |
---|
309 | ientry); |
---|
310 | |
---|
311 | add_sync_new_to_orig_idle_for_entry (ientry); |
---|
312 | } |
---|
313 | |
---|
314 | char * |
---|
315 | hack_icon_entry_get_icon (GnomeIconEntry *ientry) |
---|
316 | { |
---|
317 | return g_strdup (gtk_object_get_data (GTK_OBJECT (ientry), |
---|
318 | "IconHackText")); |
---|
319 | } |
---|
320 | |
---|
321 | void |
---|
322 | hack_icon_entry_set_icon (GnomeIconEntry *ientry, const char *icon) |
---|
323 | { |
---|
324 | gnome_icon_entry_set_icon (ientry, icon); |
---|
325 | add_sync_new_to_orig_idle_for_entry (ientry); |
---|
326 | } |
---|