glade: bgo #662288 - Fails to execute program when Glade UI file has unsaved changes
[anjuta.git] / src / shell.c
blobbc8a8beb928458bcaed5fd4ba2073b8b8f9a1ce8
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * shell.c
4 * Copyright (C) 2004 Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the Free
8 * Software Foundation; either version 2 of the License, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc., 59
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <libanjuta/anjuta-shell.h>
25 #include <libanjuta/anjuta-utils.h>
26 #include <libanjuta/anjuta-debug.h>
27 #include "shell.h"
29 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-shell.ui"
31 static void
32 shutdown (AnjutaTestShell *shell)
34 anjuta_plugin_manager_unload_all_plugins (shell->plugin_manager);
35 gtk_main_quit ();
38 static gint
39 on_delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
41 shutdown (ANJUTA_TEST_SHELL (window));
42 return TRUE;
45 static void
46 on_exit_activate (GtkAction *action, AnjutaTestShell *shell)
48 shutdown (shell);
51 static void
52 on_preferences_activate (GtkAction *action, AnjutaTestShell *shell)
54 GtkWidget *preferences_dialog;
56 preferences_dialog = anjuta_preferences_get_dialog (shell->preferences);
58 g_signal_connect_swapped (G_OBJECT (preferences_dialog),
59 "response",
60 G_CALLBACK (gtk_widget_destroy),
61 preferences_dialog);
63 gtk_window_set_transient_for (GTK_WINDOW (preferences_dialog),
64 GTK_WINDOW (shell));
66 gtk_widget_show (preferences_dialog);
69 static void
70 on_shortcuts_activate (GtkAction *action, AnjutaTestShell *shell)
72 GtkWidget *win, *accel_editor;
74 accel_editor = anjuta_ui_get_accel_editor (ANJUTA_UI (shell->ui));
75 win = gtk_dialog_new_with_buttons (_("Anjuta Plugins"), GTK_WINDOW (shell),
76 GTK_DIALOG_DESTROY_WITH_PARENT,
77 GTK_STOCK_CLOSE, GTK_STOCK_CANCEL, NULL);
78 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(win))), accel_editor, TRUE, TRUE, 0);
79 gtk_window_set_default_size (GTK_WINDOW (win), 500, 400);
80 gtk_dialog_run (GTK_DIALOG (win));
81 gtk_widget_destroy (win);
84 static GtkActionEntry action_entries[] = {
85 { "ActionMenuFile", NULL, N_("_File")},
86 { "ActionMenuEdit", NULL, N_("_Edit")},
87 { "ActionMenuView", NULL, N_("_View")},
88 { "ActionMenuSettings", NULL, N_("_Settings")},
89 { "ActionMenuHelp", NULL, N_("_Help")},
91 "ActionExit",
92 GTK_STOCK_QUIT,
93 N_("_Quit"),
94 "<control>q",
95 N_("Quit Anjuta Test Shell"),
96 G_CALLBACK (on_exit_activate)
99 "ActionSettingsPreferences",
100 GTK_STOCK_PROPERTIES,
101 N_("_Preferences..."),
102 NULL,
103 N_("Preferences"),
104 G_CALLBACK (on_preferences_activate)
107 "ActionSettingsShortcuts",
108 NULL,
109 N_("C_ustomize shortcuts"),
110 NULL,
111 N_("Customize shortcuts associated with menu items"),
112 G_CALLBACK (on_shortcuts_activate)
116 static gpointer parent_class = NULL;
118 static void
119 on_add_merge_widget (GtkUIManager *merge, GtkWidget *widget,
120 GtkWidget *ui_container)
122 DEBUG_PRINT ("%s", "Adding UI item...");
123 gtk_box_pack_start (GTK_BOX ((ANJUTA_TEST_SHELL (ui_container))->box),
124 widget, FALSE, FALSE, 0);
127 GtkWidget *
128 anjuta_test_shell_new (void)
130 AnjutaTestShell *shell;
132 shell = ANJUTA_TEST_SHELL (g_object_new (ANJUTA_TYPE_TEST_SHELL,
133 "title", "Anjuta Shell",
134 NULL));
135 return GTK_WIDGET (shell);
138 static void
139 anjuta_test_shell_instance_init (AnjutaTestShell *shell)
141 GtkWidget *plugins;
142 GList *plugins_dirs = NULL;
144 shell->values = g_hash_table_new (g_str_hash, g_str_equal);
145 shell->widgets = g_hash_table_new (g_str_hash, g_str_equal);
147 shell->box = gtk_vbox_new (FALSE, 0);
148 gtk_widget_show (shell->box);
149 gtk_container_add (GTK_CONTAINER (shell), shell->box);
151 /* Status bar */
152 shell->status = ANJUTA_STATUS (anjuta_status_new ());
153 gtk_widget_show (GTK_WIDGET (shell->status));
154 gtk_box_pack_end (GTK_BOX (shell->box), GTK_WIDGET (shell->status),
155 FALSE, FALSE, 0);
157 /* Initialize plugins */
158 plugins_dirs = g_list_prepend (plugins_dirs, PACKAGE_PLUGIN_DIR);
159 shell->plugin_manager = anjuta_plugin_manager_new (G_OBJECT (shell),
160 shell->status,
161 plugins_dirs);
162 g_list_free (plugins_dirs);
164 plugins = anjuta_plugin_manager_get_plugins_page (shell->plugin_manager);
165 gtk_box_pack_end (GTK_BOX (shell->box), plugins, TRUE, TRUE, 0);
167 /* Preferencesnces */
168 shell->preferences = ANJUTA_PREFERENCES (anjuta_preferences_new (shell->plugin_manager));
170 /* UI engine */
171 shell->ui = anjuta_ui_new ();
172 gtk_window_add_accel_group (GTK_WINDOW (shell),
173 anjuta_ui_get_accel_group (shell->ui));
174 g_signal_connect (G_OBJECT (shell->ui),
175 "add_widget", G_CALLBACK (on_add_merge_widget),
176 shell);
178 /*gtk_window_add_accel_group (GTK_WINDOW (shell),
179 anjuta_ui_get_accel_group (shell->ui));*/
180 /* Register actions */
181 anjuta_ui_add_action_group_entries (shell->ui, "ActionGroupTestShell",
182 _("Test shell action group"),
183 action_entries,
184 G_N_ELEMENTS (action_entries),
185 GETTEXT_PACKAGE, TRUE, shell);
186 /* Merge UI */
187 shell->merge_id = anjuta_ui_merge (shell->ui, UI_FILE);
189 gtk_window_set_default_size (GTK_WINDOW (shell), 300, 400);
192 static void
193 anjuta_test_shell_add_value (AnjutaShell *shell,
194 const char *name,
195 const GValue *value,
196 GError **error)
198 GValue *copy;
199 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
201 anjuta_shell_remove_value (shell, name, error);
203 copy = g_new0 (GValue, 1);
204 g_value_init (copy, value->g_type);
205 g_value_copy (value, copy);
207 g_hash_table_insert (window->values, g_strdup (name), copy);
208 g_signal_emit_by_name (shell, "value_added", name, copy);
211 static void
212 anjuta_test_shell_get_value (AnjutaShell *shell,
213 const char *name,
214 GValue *value,
215 GError **error)
217 GValue *val;
218 AnjutaTestShell *s = ANJUTA_TEST_SHELL (shell);
220 val = g_hash_table_lookup (s->values, name);
222 if (val) {
223 if (!value->g_type) {
224 g_value_init (value, val->g_type);
226 g_value_copy (val, value);
227 } else {
228 if (error) {
229 *error = g_error_new (ANJUTA_SHELL_ERROR,
230 ANJUTA_SHELL_ERROR_DOESNT_EXIST,
231 _("Value doesn't exist"));
236 static void
237 anjuta_test_shell_iconify_dockable_widget (AnjutaShell *shell,
238 GtkWidget *widget,
239 GError **error)
241 /* Not implemented. */
242 return;
245 static void
246 anjuta_test_shell_hide_dockable_widget (AnjutaShell *shell,
247 GtkWidget *widget,
248 GError **error)
250 /* Not implemented. */
251 return;
254 static void
255 anjuta_test_shell_show_dockable_widget (AnjutaShell *shell,
256 GtkWidget *widget,
257 GError **error)
259 /* Not implemented. */
260 return;
263 static void
264 anjuta_test_shell_maximize_widget (AnjutaShell *shell,
265 const char *widget_name,
266 GError **error)
268 /* Not implemented. */
269 return;
272 static void
273 anjuta_test_shell_unmaximize (AnjutaShell *shell,
274 GError **error)
276 /* Not implemented. */
277 return;
280 static void
281 anjuta_test_shell_add_widget (AnjutaShell *shell,
282 GtkWidget *w,
283 const char *name,
284 const char *title,
285 const char *stock_id,
286 AnjutaShellPlacement placement,
287 gboolean locked,
288 GError **error)
290 GtkWidget *client_win;
291 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
293 g_return_if_fail (w != NULL);
295 // anjuta_shell_add (shell, name, G_TYPE_FROM_INSTANCE (w), w, NULL);
297 g_hash_table_insert (window->widgets, g_strdup (name), w);
298 client_win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
299 gtk_container_add (GTK_CONTAINER (client_win), w);
300 gtk_widget_show_all (client_win);
303 static gboolean
304 remove_from_widgets_hash (gpointer key, gpointer value, gpointer data)
306 if (value == data) {
307 // AnjutaShell *shell;
309 // shell = g_object_get_data (G_OBJECT (value), "__temp_shell__");
310 // anjuta_shell_remove_value (shell, key, NULL);
311 g_free (key);
312 return TRUE;
314 return FALSE;
317 static void
318 anjuta_test_shell_remove_widget (AnjutaShell *shell,
319 GtkWidget *w,
320 GError **error)
322 // gint old_size;
323 GtkWidget *client_win;
324 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
325 g_return_if_fail (w != NULL);
327 // old_size = g_hash_table_size (window->widgets);
328 // g_object_set_data (G_OBJECT (w), "__temp_shell__", shell);
329 g_hash_table_foreach_steal (window->widgets, remove_from_widgets_hash, w);
330 // if (old_size != g_hash_table_size (window->widgets))
331 // {
332 client_win = gtk_widget_get_toplevel (w);
333 gtk_container_remove (GTK_CONTAINER (client_win), w);
334 gtk_widget_destroy (client_win);
338 static AnjutaPluginManager*
339 anjuta_test_shell_get_plugin_manager (AnjutaShell *shell, GError **error)
341 return ANJUTA_TEST_SHELL(shell)->plugin_manager;
344 static void
345 anjuta_test_shell_present_widget (AnjutaShell *shell,
346 GtkWidget *w,
347 GError **error)
349 GtkWidget *client_win;
350 // AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
352 g_return_if_fail (w != NULL);
354 client_win = gtk_widget_get_toplevel (w);
355 gtk_window_present (GTK_WINDOW (client_win));
358 static void
359 anjuta_test_shell_remove_value (AnjutaShell *shell,
360 const char *name,
361 GError **error)
363 GValue *value;
364 GtkWidget *w;
365 char *key;
366 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
368 if (g_hash_table_lookup_extended (window->widgets, name,
369 (gpointer*)&key, (gpointer*)&w)) {
370 GtkWidget *client_win;
371 client_win = gtk_widget_get_toplevel (w);
372 gtk_container_remove (GTK_CONTAINER (client_win), w);
373 gtk_widget_destroy (client_win);
374 g_free (key);
377 if (g_hash_table_lookup_extended (window->values, name,
378 (gpointer*)&key, (gpointer*)&value)) {
379 g_hash_table_remove (window->values, name);
380 g_signal_emit_by_name (window, "value_removed", name);
381 g_value_unset (value);
382 g_free (value);
386 static GObject*
387 anjuta_test_shell_get_object (AnjutaShell *shell, const char *iface_name,
388 GError **error)
390 return anjuta_plugin_manager_get_plugin (ANJUTA_TEST_SHELL (shell)->plugin_manager,
391 iface_name);
394 static AnjutaStatus*
395 anjuta_test_shell_get_status (AnjutaShell *shell, GError **error)
397 return ANJUTA_TEST_SHELL (shell)->status;
400 static AnjutaUI *
401 anjuta_test_shell_get_ui (AnjutaShell *shell, GError **error)
403 return ANJUTA_TEST_SHELL (shell)->ui;
406 static AnjutaPreferences *
407 anjuta_test_shell_get_preferences (AnjutaShell *shell, GError **error)
409 return ANJUTA_TEST_SHELL (shell)->preferences;
412 static void
413 anjuta_test_shell_dispose (GObject *widget)
415 /* FIXME */
416 G_OBJECT_CLASS (parent_class)->dispose (widget);
419 static void
420 anjuta_test_shell_finalize (GObject *widget)
422 AnjutaTestShell *shell = ANJUTA_TEST_SHELL (widget);
424 g_hash_table_destroy (shell->values);
425 g_hash_table_destroy (shell->widgets);
426 /* FIXME */
427 G_OBJECT_CLASS (parent_class)->finalize (widget);
430 static void
431 anjuta_test_shell_class_init (AnjutaTestShellClass *class)
433 GObjectClass *object_class;
434 GtkWidgetClass *widget_class;
436 parent_class = g_type_class_peek_parent (class);
437 object_class = (GObjectClass*) class;
438 widget_class = (GtkWidgetClass*) class;
439 object_class->dispose = anjuta_test_shell_dispose;
440 object_class->finalize = anjuta_test_shell_finalize;
443 static void
444 anjuta_shell_iface_init (AnjutaShellIface *iface)
446 iface->add_widget_full = anjuta_test_shell_add_widget;
447 iface->remove_widget = anjuta_test_shell_remove_widget;
448 iface->present_widget = anjuta_test_shell_present_widget;
449 iface->iconify_dockable_widget = anjuta_test_shell_iconify_dockable_widget;
450 iface->hide_dockable_widget = anjuta_test_shell_hide_dockable_widget;
451 iface->show_dockable_widget = anjuta_test_shell_show_dockable_widget;
452 iface->maximize_widget = anjuta_test_shell_maximize_widget;
453 iface->unmaximize = anjuta_test_shell_unmaximize;
454 iface->add_value = anjuta_test_shell_add_value;
455 iface->get_value = anjuta_test_shell_get_value;
456 iface->remove_value = anjuta_test_shell_remove_value;
457 iface->get_object = anjuta_test_shell_get_object;
458 iface->get_status = anjuta_test_shell_get_status;
459 iface->get_ui = anjuta_test_shell_get_ui;
460 iface->get_preferences = anjuta_test_shell_get_preferences;
461 iface->get_plugin_manager = anjuta_test_shell_get_plugin_manager;
464 ANJUTA_TYPE_BEGIN(AnjutaTestShell, anjuta_test_shell, GTK_TYPE_WINDOW);
465 ANJUTA_TYPE_ADD_INTERFACE(anjuta_shell, ANJUTA_TYPE_SHELL);
466 ANJUTA_TYPE_END;
469 main (int argc, char *argv[])
471 GtkWidget *shell;
472 #ifdef ENABLE_NLS
473 bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
474 bind_textdomain_codeset(PACKAGE, "UTF-8");
475 textdomain (PACKAGE);
476 #endif
478 /* Init gtk+ */
479 gtk_init (&argc, &argv);
480 g_set_application_name (_("Anjuta Shell Test"));
481 gtk_window_set_default_icon_name ("anjuta-shell");
482 gtk_window_set_auto_startup_notification(FALSE);
484 shell = anjuta_test_shell_new ();
485 g_signal_connect (G_OBJECT (shell), "delete-event",
486 G_CALLBACK (on_delete_event), NULL);
487 gtk_widget_show_all (GTK_WIDGET (shell));
488 gtk_main();
489 return 0;