1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */ |
---|
2 | /* |
---|
3 | * bonobo-ui-sync-status.h: The Bonobo UI/XML sync engine for status bits. |
---|
4 | * |
---|
5 | * Author: |
---|
6 | * Michael Meeks (michael@helixcode.com) |
---|
7 | * |
---|
8 | * Copyright 2000 Helix Code, Inc. |
---|
9 | */ |
---|
10 | |
---|
11 | #include <config.h> |
---|
12 | #include <stdlib.h> |
---|
13 | |
---|
14 | #include <gdk/gdkkeysyms.h> |
---|
15 | #include <gtk/gtk.h> |
---|
16 | |
---|
17 | #include <libgnomeui/gnome-uidefs.h> |
---|
18 | #include <libgnomeui/gnome-preferences.h> |
---|
19 | |
---|
20 | #include <bonobo/bonobo-ui-xml.h> |
---|
21 | #include <bonobo/bonobo-ui-util.h> |
---|
22 | #include <bonobo/bonobo-ui-engine.h> |
---|
23 | #include <bonobo/bonobo-ui-sync.h> |
---|
24 | #include <bonobo/bonobo-ui-sync-status.h> |
---|
25 | |
---|
26 | #include <bonobo/bonobo-ui-toolbar-separator-item.h> |
---|
27 | |
---|
28 | static GtkObjectClass *parent_class = NULL; |
---|
29 | |
---|
30 | #define PARENT_TYPE bonobo_ui_sync_get_type () |
---|
31 | |
---|
32 | #define HINT_KEY "BonoboWindow:hint" |
---|
33 | |
---|
34 | static void |
---|
35 | set_hint_cb (BonoboUIEngine *engine, |
---|
36 | const char *str, |
---|
37 | BonoboUISyncStatus *msync) |
---|
38 | { |
---|
39 | guint id; |
---|
40 | |
---|
41 | if (msync->main_status) { |
---|
42 | id = gtk_statusbar_get_context_id ( |
---|
43 | msync->main_status, HINT_KEY); |
---|
44 | |
---|
45 | gtk_statusbar_push (msync->main_status, id, str); |
---|
46 | } |
---|
47 | } |
---|
48 | |
---|
49 | static void |
---|
50 | remove_hint_cb (BonoboUIEngine *engine, |
---|
51 | BonoboUISyncStatus *msync) |
---|
52 | { |
---|
53 | if (msync->main_status) { |
---|
54 | guint id; |
---|
55 | |
---|
56 | id = gtk_statusbar_get_context_id ( |
---|
57 | msync->main_status, HINT_KEY); |
---|
58 | |
---|
59 | gtk_statusbar_pop (msync->main_status, id); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | static void |
---|
64 | impl_bonobo_ui_sync_status_state (BonoboUISync *sync, |
---|
65 | BonoboUINode *node, |
---|
66 | BonoboUINode *cmd_node, |
---|
67 | GtkWidget *widget, |
---|
68 | GtkWidget *parent) |
---|
69 | { |
---|
70 | char *name; |
---|
71 | BonoboUISyncStatus *msync = BONOBO_UI_SYNC_STATUS (sync); |
---|
72 | |
---|
73 | name = bonobo_ui_node_get_attr (node, "name"); |
---|
74 | if (!name) |
---|
75 | return; |
---|
76 | |
---|
77 | if (!strcmp (name, "main")) { |
---|
78 | const char *id_str; |
---|
79 | |
---|
80 | id_str = bonobo_ui_engine_node_get_id (sync->engine, node); |
---|
81 | |
---|
82 | msync->main_status = GTK_STATUSBAR (widget); |
---|
83 | |
---|
84 | if (id_str) { |
---|
85 | guint id; |
---|
86 | char *txt; |
---|
87 | |
---|
88 | id = gtk_statusbar_get_context_id ( |
---|
89 | msync->main_status, id_str); |
---|
90 | |
---|
91 | if ((txt = bonobo_ui_node_get_content (node))) { |
---|
92 | gboolean err; |
---|
93 | char *status; |
---|
94 | |
---|
95 | status = bonobo_ui_util_decode_str (txt, &err); |
---|
96 | |
---|
97 | if (err) |
---|
98 | g_warning ("It looks like the status '%s' is not correctly " |
---|
99 | "encoded, use bonobo_ui_component_set_status", txt); |
---|
100 | else |
---|
101 | gtk_statusbar_push (msync->main_status, |
---|
102 | id, status); |
---|
103 | |
---|
104 | g_free (status); |
---|
105 | } else |
---|
106 | gtk_statusbar_pop (msync->main_status, id); |
---|
107 | |
---|
108 | bonobo_ui_node_free_string (txt); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | bonobo_ui_node_free_string (name); |
---|
113 | } |
---|
114 | |
---|
115 | static void |
---|
116 | main_status_null (GtkObject *dummy, |
---|
117 | BonoboUISyncStatus *msync) |
---|
118 | { |
---|
119 | msync->main_status = NULL; |
---|
120 | } |
---|
121 | |
---|
122 | /* |
---|
123 | * This function is to ensure that the status bar |
---|
124 | * does not ask for any space, but fills the free |
---|
125 | * horizontal space in the hbox. |
---|
126 | */ |
---|
127 | static void |
---|
128 | clobber_request_cb (GtkWidget *widget, |
---|
129 | GtkRequisition *requisition, |
---|
130 | gpointer user_data) |
---|
131 | { |
---|
132 | requisition->width = 1; |
---|
133 | } |
---|
134 | |
---|
135 | static GtkWidget * |
---|
136 | impl_bonobo_ui_sync_status_build (BonoboUISync *sync, |
---|
137 | BonoboUINode *node, |
---|
138 | BonoboUINode *cmd_node, |
---|
139 | int *pos, |
---|
140 | GtkWidget *parent) |
---|
141 | { |
---|
142 | char *name; |
---|
143 | GtkWidget *widget = NULL; |
---|
144 | BonoboUISyncStatus *msync = BONOBO_UI_SYNC_STATUS (sync); |
---|
145 | |
---|
146 | name = bonobo_ui_node_get_attr (node, "name"); |
---|
147 | if (!name) |
---|
148 | return NULL; |
---|
149 | |
---|
150 | if (!strcmp (name, "main")) { |
---|
151 | |
---|
152 | widget = gtk_statusbar_new (); |
---|
153 | |
---|
154 | gtk_signal_connect (GTK_OBJECT (widget), |
---|
155 | "size_request", |
---|
156 | clobber_request_cb, NULL); |
---|
157 | |
---|
158 | msync->main_status = GTK_STATUSBAR (widget); |
---|
159 | |
---|
160 | gtk_signal_connect (GTK_OBJECT (widget), "destroy", |
---|
161 | (GtkSignalFunc) main_status_null, msync); |
---|
162 | |
---|
163 | /* insert a little padding so text isn't jammed against frame */ |
---|
164 | gtk_misc_set_padding ( |
---|
165 | GTK_MISC (GTK_STATUSBAR (widget)->label), |
---|
166 | GNOME_PAD, 0); |
---|
167 | gtk_widget_show (GTK_WIDGET (widget)); |
---|
168 | |
---|
169 | gtk_box_pack_start (GTK_BOX (parent), widget, TRUE, TRUE, 0); |
---|
170 | |
---|
171 | } else if (bonobo_ui_node_has_name (node, "control")) { |
---|
172 | |
---|
173 | widget = bonobo_ui_engine_build_control (sync->engine, node); |
---|
174 | |
---|
175 | if (widget) |
---|
176 | gtk_box_pack_end (GTK_BOX (parent), widget, |
---|
177 | FALSE, FALSE, 0); |
---|
178 | } |
---|
179 | bonobo_ui_node_free_string (name); |
---|
180 | |
---|
181 | if (widget) |
---|
182 | gtk_box_reorder_child (msync->status, widget, (*pos)++); |
---|
183 | |
---|
184 | return widget; |
---|
185 | } |
---|
186 | |
---|
187 | static GtkWidget * |
---|
188 | impl_bonobo_ui_sync_status_build_placeholder (BonoboUISync *sync, |
---|
189 | BonoboUINode *node, |
---|
190 | BonoboUINode *cmd_node, |
---|
191 | int *pos, |
---|
192 | GtkWidget *parent) |
---|
193 | { |
---|
194 | GtkWidget *widget; |
---|
195 | BonoboUISyncStatus *msync = BONOBO_UI_SYNC_STATUS (sync); |
---|
196 | |
---|
197 | g_warning ("TESTME: status bar placeholders"); |
---|
198 | |
---|
199 | widget = bonobo_ui_toolbar_separator_item_new (); |
---|
200 | gtk_widget_set_sensitive (widget, FALSE); |
---|
201 | |
---|
202 | gtk_box_pack_end (GTK_BOX (parent), widget, |
---|
203 | FALSE, FALSE, 0); |
---|
204 | |
---|
205 | if (widget) |
---|
206 | gtk_box_reorder_child (msync->status, widget, (*pos)++); |
---|
207 | |
---|
208 | return widget; |
---|
209 | } |
---|
210 | |
---|
211 | static GList * |
---|
212 | box_get_children_in_order (GtkBox *box) |
---|
213 | { |
---|
214 | GList *ret = NULL; |
---|
215 | GList *l; |
---|
216 | |
---|
217 | g_return_val_if_fail (GTK_IS_BOX (box), NULL); |
---|
218 | |
---|
219 | for (l = box->children; l; l = l->next) { |
---|
220 | GtkBoxChild *child = l->data; |
---|
221 | |
---|
222 | ret = g_list_prepend (ret, child->widget); |
---|
223 | } |
---|
224 | |
---|
225 | return g_list_reverse (ret); |
---|
226 | } |
---|
227 | |
---|
228 | static void |
---|
229 | impl_bonobo_ui_sync_status_stamp_root (BonoboUISync *sync) |
---|
230 | { |
---|
231 | BonoboUISyncStatus *sstatus = BONOBO_UI_SYNC_STATUS (sync); |
---|
232 | BonoboUINode *node; |
---|
233 | GtkWidget *widget; |
---|
234 | |
---|
235 | node = bonobo_ui_engine_get_path (sync->engine, "/status"); |
---|
236 | |
---|
237 | if (node) { |
---|
238 | widget = GTK_WIDGET (sstatus->status); |
---|
239 | |
---|
240 | bonobo_ui_engine_stamp_root (sync->engine, node, widget); |
---|
241 | |
---|
242 | bonobo_ui_sync_do_show_hide (sync, node, NULL, widget); |
---|
243 | } |
---|
244 | } |
---|
245 | |
---|
246 | static GList * |
---|
247 | impl_bonobo_ui_sync_status_get_widgets (BonoboUISync *sync, |
---|
248 | BonoboUINode *node) |
---|
249 | { |
---|
250 | if (bonobo_ui_node_has_name (node, "status")) |
---|
251 | return box_get_children_in_order ( |
---|
252 | GTK_BOX (BONOBO_UI_SYNC_STATUS (sync)->status)); |
---|
253 | else |
---|
254 | return NULL; |
---|
255 | } |
---|
256 | |
---|
257 | static void |
---|
258 | impl_destroy (GtkObject *object) |
---|
259 | { |
---|
260 | parent_class->destroy (object); |
---|
261 | } |
---|
262 | |
---|
263 | static gboolean |
---|
264 | impl_bonobo_ui_sync_status_can_handle (BonoboUISync *sync, |
---|
265 | BonoboUINode *node) |
---|
266 | { |
---|
267 | return bonobo_ui_node_has_name (node, "status"); |
---|
268 | } |
---|
269 | |
---|
270 | /* We need to map the shell to the item */ |
---|
271 | |
---|
272 | static void |
---|
273 | class_init (BonoboUISyncClass *sync_class) |
---|
274 | { |
---|
275 | GtkObjectClass *object_class; |
---|
276 | |
---|
277 | parent_class = gtk_type_class (BONOBO_TYPE_UI_SYNC); |
---|
278 | |
---|
279 | object_class = GTK_OBJECT_CLASS (sync_class); |
---|
280 | object_class->destroy = impl_destroy; |
---|
281 | |
---|
282 | sync_class->sync_state = impl_bonobo_ui_sync_status_state; |
---|
283 | sync_class->build = impl_bonobo_ui_sync_status_build; |
---|
284 | sync_class->build_placeholder = impl_bonobo_ui_sync_status_build_placeholder; |
---|
285 | |
---|
286 | sync_class->get_widgets = impl_bonobo_ui_sync_status_get_widgets; |
---|
287 | sync_class->stamp_root = impl_bonobo_ui_sync_status_stamp_root; |
---|
288 | |
---|
289 | sync_class->can_handle = impl_bonobo_ui_sync_status_can_handle; |
---|
290 | } |
---|
291 | |
---|
292 | static void |
---|
293 | init (BonoboUISyncStatus *msync) |
---|
294 | { |
---|
295 | } |
---|
296 | |
---|
297 | GtkType |
---|
298 | bonobo_ui_sync_status_get_type (void) |
---|
299 | { |
---|
300 | static GtkType type = 0; |
---|
301 | |
---|
302 | if (type == 0) { |
---|
303 | static const GtkTypeInfo info = { |
---|
304 | "BonoboUISyncStatus", |
---|
305 | sizeof (BonoboUISyncStatus), |
---|
306 | sizeof (BonoboUISyncStatusClass), |
---|
307 | (GtkClassInitFunc) class_init, |
---|
308 | (GtkObjectInitFunc) init, |
---|
309 | /* reserved_1 */ NULL, |
---|
310 | /* reserved_2 */ NULL, |
---|
311 | (GtkClassInitFunc) NULL, |
---|
312 | }; |
---|
313 | |
---|
314 | type = gtk_type_unique (PARENT_TYPE, &info); |
---|
315 | } |
---|
316 | |
---|
317 | return type; |
---|
318 | } |
---|
319 | |
---|
320 | BonoboUISync * |
---|
321 | bonobo_ui_sync_status_new (BonoboUIEngine *engine, |
---|
322 | GtkBox *status) |
---|
323 | { |
---|
324 | BonoboUISyncStatus *sync; |
---|
325 | |
---|
326 | g_return_val_if_fail (BONOBO_IS_UI_ENGINE (engine), NULL); |
---|
327 | |
---|
328 | sync = gtk_type_new (BONOBO_TYPE_UI_SYNC_STATUS); |
---|
329 | |
---|
330 | sync->status = status; |
---|
331 | |
---|
332 | gtk_signal_connect (GTK_OBJECT (engine), "add_hint", |
---|
333 | (GtkSignalFunc) set_hint_cb, sync); |
---|
334 | |
---|
335 | gtk_signal_connect (GTK_OBJECT (engine), "remove_hint", |
---|
336 | (GtkSignalFunc) remove_hint_cb, sync); |
---|
337 | |
---|
338 | return bonobo_ui_sync_construct ( |
---|
339 | BONOBO_UI_SYNC (sync), engine, FALSE, TRUE); |
---|
340 | } |
---|