2008-04-30 Johannes Schmid <jhs@gnome.org>
[anjuta-git-plugin.git] / src / shell.c
blob257d6820622b1664bce0085d67fbfcbba13d7cf3
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>
28 #include "shell.h"
30 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-shell.ui"
32 static void
33 shutdown (AnjutaTestShell *shell)
35 anjuta_plugin_manager_unload_all_plugins (shell->plugin_manager);
36 gtk_main_quit ();
39 static gint
40 on_delete_event (GtkWidget *window, GdkEvent *event, gpointer data)
42 shutdown (ANJUTA_TEST_SHELL (window));
43 return TRUE;
46 static void
47 on_exit_activate (GtkAction *action, AnjutaTestShell *shell)
49 shutdown (shell);
52 static void
53 on_preferences_activate (GtkAction *action, AnjutaTestShell *shell)
55 GtkWidget *preferences_dialog;
57 preferences_dialog = anjuta_preferences_get_dialog (shell->preferences);
59 g_signal_connect_swapped (G_OBJECT (preferences_dialog),
60 "response",
61 G_CALLBACK (gtk_widget_destroy),
62 preferences_dialog);
64 gtk_window_set_transient_for (GTK_WINDOW (preferences_dialog),
65 GTK_WINDOW (shell));
67 gtk_widget_show (preferences_dialog);
70 static void
71 on_shortcuts_activate (GtkAction *action, AnjutaTestShell *shell)
73 GtkWidget *win, *accel_editor;
75 accel_editor = anjuta_ui_get_accel_editor (ANJUTA_UI (shell->ui));
76 win = gtk_dialog_new_with_buttons (_("Anjuta Plugins"), GTK_WINDOW (shell),
77 GTK_DIALOG_DESTROY_WITH_PARENT,
78 GTK_STOCK_CLOSE, GTK_STOCK_CANCEL, NULL);
79 gtk_box_pack_start_defaults (GTK_BOX (GTK_DIALOG(win)->vbox), accel_editor);
80 gtk_window_set_default_size (GTK_WINDOW (win), 500, 400);
81 gtk_dialog_run (GTK_DIALOG (win));
82 gtk_widget_destroy (win);
85 static GtkActionEntry action_entries[] = {
86 { "ActionMenuFile", NULL, N_("_File")},
87 { "ActionMenuEdit", NULL, N_("_Edit")},
88 { "ActionMenuView", NULL, N_("_View")},
89 { "ActionMenuSettings", NULL, N_("_Settings")},
90 { "ActionMenuHelp", NULL, N_("_Help")},
92 "ActionExit",
93 GTK_STOCK_QUIT,
94 N_("_Quit"),
95 "<control>q",
96 N_("Quit Anjuta Test Shell"),
97 G_CALLBACK (on_exit_activate)
100 "ActionSettingsPreferences",
101 GTK_STOCK_PROPERTIES,
102 N_("_Preferences..."),
103 NULL,
104 N_("Preferences"),
105 G_CALLBACK (on_preferences_activate)
108 "ActionSettingsShortcuts",
109 NULL,
110 N_("C_ustomize shortcuts"),
111 NULL,
112 N_("Customize shortcuts associated with menu items"),
113 G_CALLBACK (on_shortcuts_activate)
117 static gpointer parent_class = NULL;
119 static void
120 on_add_merge_widget (GtkUIManager *merge, GtkWidget *widget,
121 GtkWidget *ui_container)
123 DEBUG_PRINT ("Adding UI item...");
124 gtk_box_pack_start (GTK_BOX ((ANJUTA_TEST_SHELL (ui_container))->box),
125 widget, FALSE, FALSE, 0);
128 GtkWidget *
129 anjuta_test_shell_new (void)
131 AnjutaTestShell *shell;
133 shell = ANJUTA_TEST_SHELL (g_object_new (ANJUTA_TYPE_TEST_SHELL,
134 "title", "Anjuta Shell",
135 NULL));
136 return GTK_WIDGET (shell);
139 static void
140 anjuta_test_shell_instance_init (AnjutaTestShell *shell)
142 GtkWidget *plugins;
143 GList *plugins_dirs = NULL;
145 shell->values = g_hash_table_new (g_str_hash, g_str_equal);
146 shell->widgets = g_hash_table_new (g_str_hash, g_str_equal);
148 shell->box = gtk_vbox_new (FALSE, 0);
149 gtk_widget_show (shell->box);
150 gtk_container_add (GTK_CONTAINER (shell), shell->box);
152 /* Status bar */
153 shell->status = ANJUTA_STATUS (anjuta_status_new ());
154 gtk_widget_show (GTK_WIDGET (shell->status));
155 gtk_box_pack_end (GTK_BOX (shell->box), GTK_WIDGET (shell->status),
156 FALSE, FALSE, 0);
158 /* Initialize plugins */
159 plugins_dirs = g_list_prepend (plugins_dirs, PACKAGE_PLUGIN_DIR);
160 shell->plugin_manager = anjuta_plugin_manager_new (G_OBJECT (shell),
161 shell->status,
162 plugins_dirs);
163 g_list_free (plugins_dirs);
165 plugins = anjuta_plugin_manager_get_plugins_page (shell->plugin_manager);
166 gtk_box_pack_end_defaults (GTK_BOX (shell->box), plugins);
168 /* Preferencesnces */
169 shell->preferences = ANJUTA_PREFERENCES (anjuta_preferences_new (shell->plugin_manager));
171 /* UI engine */
172 shell->ui = anjuta_ui_new ();
173 gtk_window_add_accel_group (GTK_WINDOW (shell),
174 anjuta_ui_get_accel_group (shell->ui));
175 g_signal_connect (G_OBJECT (shell->ui),
176 "add_widget", G_CALLBACK (on_add_merge_widget),
177 shell);
179 /*gtk_window_add_accel_group (GTK_WINDOW (shell),
180 anjuta_ui_get_accel_group (shell->ui));*/
181 /* Register actions */
182 anjuta_ui_add_action_group_entries (shell->ui, "ActionGroupTestShell",
183 _("Test shell action group"),
184 action_entries,
185 G_N_ELEMENTS (action_entries),
186 GETTEXT_PACKAGE, TRUE, shell);
187 /* Merge UI */
188 shell->merge_id = anjuta_ui_merge (shell->ui, UI_FILE);
190 gtk_window_set_default_size (GTK_WINDOW (shell), 300, 400);
193 static void
194 anjuta_test_shell_add_value (AnjutaShell *shell,
195 const char *name,
196 const GValue *value,
197 GError **error)
199 GValue *copy;
200 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
202 anjuta_shell_remove_value (shell, name, error);
204 copy = g_new0 (GValue, 1);
205 g_value_init (copy, value->g_type);
206 g_value_copy (value, copy);
208 g_hash_table_insert (window->values, g_strdup (name), copy);
209 g_signal_emit_by_name (shell, "value_added", name, copy);
212 static void
213 anjuta_test_shell_get_value (AnjutaShell *shell,
214 const char *name,
215 GValue *value,
216 GError **error)
218 GValue *val;
219 AnjutaTestShell *s = ANJUTA_TEST_SHELL (shell);
221 val = g_hash_table_lookup (s->values, name);
223 if (val) {
224 if (!value->g_type) {
225 g_value_init (value, val->g_type);
227 g_value_copy (val, value);
228 } else {
229 if (error) {
230 *error = g_error_new (ANJUTA_SHELL_ERROR,
231 ANJUTA_SHELL_ERROR_DOESNT_EXIST,
232 _("Value doesn't exist"));
237 static void
238 anjuta_test_shell_add_widget (AnjutaShell *shell,
239 GtkWidget *w,
240 const char *name,
241 const char *title,
242 const char *stock_id,
243 AnjutaShellPlacement placement,
244 gboolean locked,
245 GError **error)
247 GtkWidget *client_win;
248 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
250 g_return_if_fail (w != NULL);
252 // anjuta_shell_add (shell, name, G_TYPE_FROM_INSTANCE (w), w, NULL);
254 g_hash_table_insert (window->widgets, g_strdup (name), w);
255 client_win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
256 gtk_container_add (GTK_CONTAINER (client_win), w);
257 gtk_widget_show_all (client_win);
260 static gboolean
261 remove_from_widgets_hash (gpointer key, gpointer value, gpointer data)
263 if (value == data) {
264 // AnjutaShell *shell;
266 // shell = g_object_get_data (G_OBJECT (value), "__temp_shell__");
267 // anjuta_shell_remove_value (shell, key, NULL);
268 g_free (key);
269 return TRUE;
271 return FALSE;
274 static void
275 anjuta_test_shell_remove_widget (AnjutaShell *shell,
276 GtkWidget *w,
277 GError **error)
279 // gint old_size;
280 GtkWidget *client_win;
281 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
282 g_return_if_fail (w != NULL);
284 // old_size = g_hash_table_size (window->widgets);
285 // g_object_set_data (G_OBJECT (w), "__temp_shell__", shell);
286 g_hash_table_foreach_steal (window->widgets, remove_from_widgets_hash, w);
287 // if (old_size != g_hash_table_size (window->widgets))
288 // {
289 client_win = gtk_widget_get_toplevel (w);
290 gtk_container_remove (GTK_CONTAINER (client_win), w);
291 gtk_widget_destroy (client_win);
295 static AnjutaPluginManager*
296 anjuta_test_shell_get_plugin_manager (AnjutaShell *shell, GError **error)
298 return ANJUTA_TEST_SHELL(shell)->plugin_manager;
301 static void
302 anjuta_test_shell_present_widget (AnjutaShell *shell,
303 GtkWidget *w,
304 GError **error)
306 GtkWidget *client_win;
307 // AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
309 g_return_if_fail (w != NULL);
311 client_win = gtk_widget_get_toplevel (w);
312 gtk_window_present (GTK_WINDOW (client_win));
315 static void
316 anjuta_test_shell_remove_value (AnjutaShell *shell,
317 const char *name,
318 GError **error)
320 GValue *value;
321 GtkWidget *w;
322 char *key;
323 AnjutaTestShell *window = ANJUTA_TEST_SHELL (shell);
325 if (g_hash_table_lookup_extended (window->widgets, name,
326 (gpointer*)&key, (gpointer*)&w)) {
327 GtkWidget *client_win;
328 client_win = gtk_widget_get_toplevel (w);
329 gtk_container_remove (GTK_CONTAINER (client_win), w);
330 gtk_widget_destroy (client_win);
331 g_free (key);
334 if (g_hash_table_lookup_extended (window->values, name,
335 (gpointer*)&key, (gpointer*)&value)) {
336 g_hash_table_remove (window->values, name);
337 g_signal_emit_by_name (window, "value_removed", name);
338 g_value_unset (value);
339 g_free (value);
343 static GObject*
344 anjuta_test_shell_get_object (AnjutaShell *shell, const char *iface_name,
345 GError **error)
347 return anjuta_plugin_manager_get_plugin (ANJUTA_TEST_SHELL (shell)->plugin_manager,
348 iface_name);
351 static AnjutaStatus*
352 anjuta_test_shell_get_status (AnjutaShell *shell, GError **error)
354 return ANJUTA_TEST_SHELL (shell)->status;
357 static AnjutaUI *
358 anjuta_test_shell_get_ui (AnjutaShell *shell, GError **error)
360 return ANJUTA_TEST_SHELL (shell)->ui;
363 static AnjutaPreferences *
364 anjuta_test_shell_get_preferences (AnjutaShell *shell, GError **error)
366 return ANJUTA_TEST_SHELL (shell)->preferences;
369 static void
370 anjuta_test_shell_dispose (GObject *widget)
372 /* FIXME */
373 GNOME_CALL_PARENT (G_OBJECT_CLASS, dispose, (widget));
376 static void
377 anjuta_test_shell_finalize (GObject *widget)
379 AnjutaTestShell *shell = ANJUTA_TEST_SHELL (widget);
381 g_hash_table_destroy (shell->values);
382 g_hash_table_destroy (shell->widgets);
383 /* FIXME */
384 GNOME_CALL_PARENT (G_OBJECT_CLASS, finalize, (widget));
387 static void
388 anjuta_test_shell_class_init (AnjutaTestShellClass *class)
390 GObjectClass *object_class;
391 GtkWidgetClass *widget_class;
393 parent_class = g_type_class_peek_parent (class);
394 object_class = (GObjectClass*) class;
395 widget_class = (GtkWidgetClass*) class;
396 object_class->dispose = anjuta_test_shell_dispose;
397 object_class->finalize = anjuta_test_shell_finalize;
400 static void
401 anjuta_shell_iface_init (AnjutaShellIface *iface)
403 iface->add_widget_full = anjuta_test_shell_add_widget;
404 iface->remove_widget = anjuta_test_shell_remove_widget;
405 iface->present_widget = anjuta_test_shell_present_widget;
406 iface->add_value = anjuta_test_shell_add_value;
407 iface->get_value = anjuta_test_shell_get_value;
408 iface->remove_value = anjuta_test_shell_remove_value;
409 iface->get_object = anjuta_test_shell_get_object;
410 iface->get_status = anjuta_test_shell_get_status;
411 iface->get_ui = anjuta_test_shell_get_ui;
412 iface->get_preferences = anjuta_test_shell_get_preferences;
413 iface->get_plugin_manager = anjuta_test_shell_get_plugin_manager;
416 ANJUTA_TYPE_BEGIN(AnjutaTestShell, anjuta_test_shell, GTK_TYPE_WINDOW);
417 ANJUTA_TYPE_ADD_INTERFACE(anjuta_shell, ANJUTA_TYPE_SHELL);
418 ANJUTA_TYPE_END;
421 main (int argc, char *argv[])
423 GtkWidget *shell;
424 GnomeProgram *program;
425 gchar *data_dir;
426 // GList* command_args;
428 #ifdef ENABLE_NLS
429 bindtextdomain (PACKAGE, PACKAGE_LOCALE_DIR);
430 bind_textdomain_codeset(PACKAGE, "UTF-8");
431 textdomain (PACKAGE);
432 #endif
434 data_dir = g_strdup (PACKAGE_DATA_DIR);
435 data_dir[strlen (data_dir) - strlen (PACKAGE) - 1] = '\0';
437 /* Initialize gnome program */
438 program = gnome_program_init ("Anjuta Test Shell", VERSION,
439 LIBGNOMEUI_MODULE, argc, argv,
440 GNOME_PARAM_POPT_TABLE, NULL,
441 GNOME_PARAM_HUMAN_READABLE_NAME,
442 _("Anjuta test shell"),
443 GNOME_PARAM_APP_DATADIR, data_dir,
444 NULL);
445 g_free (data_dir);
447 shell = anjuta_test_shell_new ();
448 g_signal_connect (G_OBJECT (shell), "delete-event",
449 G_CALLBACK (on_delete_event), NULL);
450 gtk_widget_show_all (GTK_WIDGET (shell));
451 gtk_main();
452 return 0;