anjuta: Save GDL settings correctly
[anjuta.git] / src / shell.c
blob3499c018a25d41f4945c6a3e602bd59372f08e8e
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>
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 #define PREF_SCHEMA "org.gnome.anjuta"
33 static void
34 shutdown (AnjutaTestShell *shell)
36 anjuta_plugin_manager_unload_all_plugins (shell->plugin_manager);
37 gtk_main_quit ();
40 static gint
41 on_delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
43 shutdown (ANJUTA_TEST_SHELL (window));
44 return TRUE;
47 static void
48 on_exit_activate (GtkAction *action, AnjutaTestShell *shell)
50 shutdown (shell);
53 static void
54 on_preferences_activate (GtkAction *action, AnjutaTestShell *shell)
56 GtkWidget *preferences_dialog;
58 preferences_dialog = anjuta_preferences_get_dialog (shell->preferences);
60 g_signal_connect_swapped (G_OBJECT (preferences_dialog),
61 "response",
62 G_CALLBACK (gtk_widget_destroy),
63 preferences_dialog);
65 gtk_window_set_transient_for (GTK_WINDOW (preferences_dialog),
66 GTK_WINDOW (shell));
68 gtk_widget_show (preferences_dialog);
71 static void
72 on_shortcuts_activate (GtkAction *action, AnjutaTestShell *shell)
74 GtkWidget *win, *accel_editor;
76 accel_editor = anjuta_ui_get_accel_editor (ANJUTA_UI (shell->ui));
77 win = gtk_dialog_new_with_buttons (_("Anjuta Plugins"), GTK_WINDOW (shell),
78 GTK_DIALOG_DESTROY_WITH_PARENT,
79 GTK_STOCK_CLOSE, GTK_STOCK_CANCEL, NULL);
80 gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG(win))), accel_editor, TRUE, TRUE, 0);
81 gtk_window_set_default_size (GTK_WINDOW (win), 500, 400);
82 gtk_dialog_run (GTK_DIALOG (win));
83 gtk_widget_destroy (win);
86 static GtkActionEntry action_entries[] = {
87 { "ActionMenuFile", NULL, N_("_File")},
88 { "ActionMenuEdit", NULL, N_("_Edit")},
89 { "ActionMenuView", NULL, N_("_View")},
90 { "ActionMenuSettings", NULL, N_("_Settings")},
91 { "ActionMenuHelp", NULL, N_("_Help")},
93 "ActionExit",
94 GTK_STOCK_QUIT,
95 N_("_Quit"),
96 "<control>q",
97 N_("Quit Anjuta Test Shell"),
98 G_CALLBACK (on_exit_activate)
101 "ActionSettingsPreferences",
102 GTK_STOCK_PROPERTIES,
103 N_("_Preferences..."),
104 NULL,
105 N_("Preferences"),
106 G_CALLBACK (on_preferences_activate)
109 "ActionSettingsShortcuts",
110 NULL,
111 N_("C_ustomize shortcuts"),
112 NULL,
113 N_("Customize shortcuts associated with menu items"),
114 G_CALLBACK (on_shortcuts_activate)
118 static gpointer parent_class = NULL;
120 static void
121 on_add_merge_widget (GtkUIManager *merge, GtkWidget *widget,
122 GtkWidget *ui_container)
124 DEBUG_PRINT ("%s", "Adding UI item...");
125 gtk_box_pack_start (GTK_BOX ((ANJUTA_TEST_SHELL (ui_container))->box),
126 widget, FALSE, FALSE, 0);
129 GtkWidget *
130 anjuta_test_shell_new (void)
132 AnjutaTestShell *shell;
134 shell = ANJUTA_TEST_SHELL (g_object_new (ANJUTA_TYPE_TEST_SHELL,
135 "title", "Anjuta Shell",
136 NULL));
137 return GTK_WIDGET (shell);
140 static void
141 anjuta_test_shell_instance_init (AnjutaTestShell *shell)
143 GtkWidget *plugins;
144 GList *plugins_dirs = NULL;
146 shell->values = g_hash_table_new (g_str_hash, g_str_equal);
147 shell->widgets = g_hash_table_new (g_str_hash, g_str_equal);
149 shell->box = gtk_vbox_new (FALSE, 0);
150 gtk_widget_show (shell->box);
151 gtk_container_add (GTK_CONTAINER (shell), shell->box);
153 /* Status bar */
154 shell->status = ANJUTA_STATUS (anjuta_status_new ());
155 gtk_widget_show (GTK_WIDGET (shell->status));
156 gtk_box_pack_end (GTK_BOX (shell->box), GTK_WIDGET (shell->status),
157 FALSE, FALSE, 0);
159 /* Initialize plugins */
160 plugins_dirs = g_list_prepend (plugins_dirs, PACKAGE_PLUGIN_DIR);
161 shell->plugin_manager = anjuta_plugin_manager_new (G_OBJECT (shell),
162 shell->status,
163 plugins_dirs);
164 g_list_free (plugins_dirs);
166 plugins = anjuta_plugin_manager_get_plugins_page (shell->plugin_manager);
167 gtk_box_pack_end (GTK_BOX (shell->box), plugins, TRUE, TRUE, 0);
169 /* Preferencesnces */
170 shell->preferences = ANJUTA_PREFERENCES (anjuta_preferences_new (shell->plugin_manager, PREF_SCHEMA));
172 /* UI engine */
173 shell->ui = anjuta_ui_new ();
174 gtk_window_add_accel_group (GTK_WINDOW (shell),
175 anjuta_ui_get_accel_group (shell->ui));
176 g_signal_connect (G_OBJECT (shell->ui),
177 "add_widget", G_CALLBACK (on_add_merge_widget),
178 shell);
180 /*gtk_window_add_accel_group (GTK_WINDOW (shell),
181 anjuta_ui_get_accel_group (shell->ui));*/
182 /* Register actions */
183 anjuta_ui_add_action_group_entries (shell->ui, "ActionGroupTestShell",
184 _("Test shell action group"),
185 action_entries,
186 G_N_ELEMENTS (action_entries),
187 GETTEXT_PACKAGE, TRUE, shell);
188 /* Merge UI */
189 shell->merge_id = anjuta_ui_merge (shell->ui, UI_FILE);
191 gtk_window_set_default_size (GTK_WINDOW (shell), 300, 400);
194 static void
195 anjuta_test_shell_add_value (AnjutaShell *shell,
196 const char *name,
197 const GValue *value,
198 GError **error)
200 GValue *copy;
201 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
203 anjuta_shell_remove_value (shell, name, error);
205 copy = g_new0 (GValue, 1);
206 g_value_init (copy, value->g_type);
207 g_value_copy (value, copy);
209 g_hash_table_insert (window->values, g_strdup (name), copy);
210 g_signal_emit_by_name (shell, "value_added", name, copy);
213 static void
214 anjuta_test_shell_get_value (AnjutaShell *shell,
215 const char *name,
216 GValue *value,
217 GError **error)
219 GValue *val;
220 AnjutaTestShell *s = ANJUTA_TEST_SHELL (shell);
222 val = g_hash_table_lookup (s->values, name);
224 if (val) {
225 if (!value->g_type) {
226 g_value_init (value, val->g_type);
228 g_value_copy (val, value);
229 } else {
230 if (error) {
231 *error = g_error_new (ANJUTA_SHELL_ERROR,
232 ANJUTA_SHELL_ERROR_DOESNT_EXIST,
233 _("Value doesn't exist"));
238 static void
239 anjuta_test_shell_iconify_dockable_widget (AnjutaShell *shell,
240 GtkWidget *widget,
241 GError **error)
243 /* Not implemented. */
244 return;
247 static void
248 anjuta_test_shell_hide_dockable_widget (AnjutaShell *shell,
249 GtkWidget *widget,
250 GError **error)
252 /* Not implemented. */
253 return;
256 static void
257 anjuta_test_shell_show_dockable_widget (AnjutaShell *shell,
258 GtkWidget *widget,
259 GError **error)
261 /* Not implemented. */
262 return;
265 static void
266 anjuta_test_shell_maximize_widget (AnjutaShell *shell,
267 const char *widget_name,
268 GError **error)
270 /* Not implemented. */
271 return;
274 static void
275 anjuta_test_shell_unmaximize (AnjutaShell *shell,
276 GError **error)
278 /* Not implemented. */
279 return;
282 static void
283 anjuta_test_shell_add_widget (AnjutaShell *shell,
284 GtkWidget *w,
285 const char *name,
286 const char *title,
287 const char *stock_id,
288 AnjutaShellPlacement placement,
289 gboolean locked,
290 GError **error)
292 GtkWidget *client_win;
293 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
295 g_return_if_fail (w != NULL);
297 // anjuta_shell_add (shell, name, G_TYPE_FROM_INSTANCE (w), w, NULL);
299 g_hash_table_insert (window->widgets, g_strdup (name), w);
300 client_win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
301 gtk_container_add (GTK_CONTAINER (client_win), w);
302 gtk_widget_show_all (client_win);
305 static gboolean
306 remove_from_widgets_hash (gpointer key, gpointer value, gpointer data)
308 if (value == data) {
309 // AnjutaShell *shell;
311 // shell = g_object_get_data (G_OBJECT (value), "__temp_shell__");
312 // anjuta_shell_remove_value (shell, key, NULL);
313 g_free (key);
314 return TRUE;
316 return FALSE;
319 static void
320 anjuta_test_shell_remove_widget (AnjutaShell *shell,
321 GtkWidget *w,
322 GError **error)
324 // gint old_size;
325 GtkWidget *client_win;
326 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
327 g_return_if_fail (w != NULL);
329 // old_size = g_hash_table_size (window->widgets);
330 // g_object_set_data (G_OBJECT (w), "__temp_shell__", shell);
331 g_hash_table_foreach_steal (window->widgets, remove_from_widgets_hash, w);
332 // if (old_size != g_hash_table_size (window->widgets))
333 // {
334 client_win = gtk_widget_get_toplevel (w);
335 gtk_container_remove (GTK_CONTAINER (client_win), w);
336 gtk_widget_destroy (client_win);
340 static AnjutaPluginManager*
341 anjuta_test_shell_get_plugin_manager (AnjutaShell *shell, GError **error)
343 return ANJUTA_TEST_SHELL(shell)->plugin_manager;
346 static void
347 anjuta_test_shell_present_widget (AnjutaShell *shell,
348 GtkWidget *w,
349 GError **error)
351 GtkWidget *client_win;
352 // AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
354 g_return_if_fail (w != NULL);
356 client_win = gtk_widget_get_toplevel (w);
357 gtk_window_present (GTK_WINDOW (client_win));
360 static void
361 anjuta_test_shell_remove_value (AnjutaShell *shell,
362 const char *name,
363 GError **error)
365 GValue *value;
366 GtkWidget *w;
367 char *key;
368 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
370 if (g_hash_table_lookup_extended (window->widgets, name,
371 (gpointer*)&key, (gpointer*)&w)) {
372 GtkWidget *client_win;
373 client_win = gtk_widget_get_toplevel (w);
374 gtk_container_remove (GTK_CONTAINER (client_win), w);
375 gtk_widget_destroy (client_win);
376 g_free (key);
379 if (g_hash_table_lookup_extended (window->values, name,
380 (gpointer*)&key, (gpointer*)&value)) {
381 g_hash_table_remove (window->values, name);
382 g_signal_emit_by_name (window, "value_removed", name);
383 g_value_unset (value);
384 g_free (value);
388 static GObject*
389 anjuta_test_shell_get_object (AnjutaShell *shell, const char *iface_name,
390 GError **error)
392 return anjuta_plugin_manager_get_plugin (ANJUTA_TEST_SHELL (shell)->plugin_manager,
393 iface_name);
396 static AnjutaStatus*
397 anjuta_test_shell_get_status (AnjutaShell *shell, GError **error)
399 return ANJUTA_TEST_SHELL (shell)->status;
402 static AnjutaUI *
403 anjuta_test_shell_get_ui (AnjutaShell *shell, GError **error)
405 return ANJUTA_TEST_SHELL (shell)->ui;
408 static AnjutaPreferences *
409 anjuta_test_shell_get_preferences (AnjutaShell *shell, GError **error)
411 return ANJUTA_TEST_SHELL (shell)->preferences;
414 static void
415 anjuta_test_shell_dispose (GObject *widget)
417 /* FIXME */
418 G_OBJECT_CLASS (parent_class)->dispose (widget);
421 static void
422 anjuta_test_shell_finalize (GObject *widget)
424 AnjutaTestShell *shell = ANJUTA_TEST_SHELL (widget);
426 g_hash_table_destroy (shell->values);
427 g_hash_table_destroy (shell->widgets);
428 /* FIXME */
429 G_OBJECT_CLASS (parent_class)->finalize (widget);
432 static void
433 anjuta_test_shell_class_init (AnjutaTestShellClass *class)
435 GObjectClass *object_class;
436 GtkWidgetClass *widget_class;
438 parent_class = g_type_class_peek_parent (class);
439 object_class = (GObjectClass*) class;
440 widget_class = (GtkWidgetClass*) class;
441 object_class->dispose = anjuta_test_shell_dispose;
442 object_class->finalize = anjuta_test_shell_finalize;
445 static void
446 anjuta_shell_iface_init (AnjutaShellIface *iface)
448 iface->add_widget_full = anjuta_test_shell_add_widget;
449 iface->remove_widget = anjuta_test_shell_remove_widget;
450 iface->present_widget = anjuta_test_shell_present_widget;
451 iface->iconify_dockable_widget = anjuta_test_shell_iconify_dockable_widget;
452 iface->hide_dockable_widget = anjuta_test_shell_hide_dockable_widget;
453 iface->show_dockable_widget = anjuta_test_shell_show_dockable_widget;
454 iface->maximize_widget = anjuta_test_shell_maximize_widget;
455 iface->unmaximize = anjuta_test_shell_unmaximize;
456 iface->add_value = anjuta_test_shell_add_value;
457 iface->get_value = anjuta_test_shell_get_value;
458 iface->remove_value = anjuta_test_shell_remove_value;
459 iface->get_object = anjuta_test_shell_get_object;
460 iface->get_status = anjuta_test_shell_get_status;
461 iface->get_ui = anjuta_test_shell_get_ui;
462 iface->get_preferences = anjuta_test_shell_get_preferences;
463 iface->get_plugin_manager = anjuta_test_shell_get_plugin_manager;
466 ANJUTA_TYPE_BEGIN(AnjutaTestShell, anjuta_test_shell, GTK_TYPE_WINDOW);
467 ANJUTA_TYPE_ADD_INTERFACE(anjuta_shell, ANJUTA_TYPE_SHELL);
468 ANJUTA_TYPE_END;
471 main (int argc, char *argv[])
473 GtkWidget *shell;
474 #ifdef ENABLE_NLS
475 bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
476 bind_textdomain_codeset(PACKAGE, "UTF-8");
477 textdomain (PACKAGE);
478 #endif
480 /* Init gtk+ */
481 gtk_init (&argc, &argv);
482 g_set_application_name (_("Anjuta Shell Test"));
483 gtk_window_set_default_icon_name ("anjuta-shell");
484 gtk_window_set_auto_startup_notification(FALSE);
486 shell = anjuta_test_shell_new ();
487 g_signal_connect (G_OBJECT (shell), "delete-event",
488 G_CALLBACK (on_delete_event), NULL);
489 gtk_widget_show_all (GTK_WIDGET (shell));
490 gtk_main();
491 return 0;