Automatically detect which file that will hold member widgets declaration and initial...
[anjuta.git] / plugins / glade / plugin.c
blob7cb600c83ae767c4e40ce5cde88a6457f23056d1
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 /* plugin.c
4 * Copyright (C) 2000 Naba Kumar
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 *Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <config.h>
21 #include <libanjuta/anjuta-shell.h>
22 #include <libanjuta/anjuta-debug.h>
23 #include <libanjuta/interfaces/ianjuta-file.h>
24 #include <libanjuta/interfaces/ianjuta-file-savable.h>
25 #include <libanjuta/interfaces/ianjuta-wizard.h>
26 #include <libanjuta/interfaces/ianjuta-help.h>
27 #include <libanjuta/interfaces/ianjuta-document-manager.h>
29 #include "plugin.h"
30 #include "anjuta-design-document.h"
32 #define UI_FILE PACKAGE_DATA_DIR"/ui/anjuta-glade.xml"
34 static gpointer parent_class;
36 /* This is variable required from libdevhelp */
37 gchar *geometry = NULL;
39 struct _GladePluginPriv
41 gint uiid;
42 GtkActionGroup *action_group;
43 GladeApp *app;
45 GtkWidget *inspector;
46 GtkWidget *palette;
47 GtkWidget *editor;
49 GtkWidget *view_box;
50 GtkWidget *paned;
51 GtkWidget *palette_box;
52 gint editor_watch_id;
53 gboolean destroying;
55 GtkWidget *selector_toggle;
56 GtkWidget *resize_toggle;
58 /* File count, disable plugin when NULL */
59 guint file_count;
61 /* for status */
62 gboolean add_ticks;
65 enum {
66 NAME_COL,
67 PROJECT_COL,
68 N_COLUMNS
71 static void
72 on_pointer_mode_changed (GladeProject *project,
73 GParamSpec *pspec,
74 GladePlugin *plugin);
76 static void
77 value_added_current_editor (AnjutaPlugin *plugin, const char *name,
78 const GValue *value, gpointer data)
80 GladePlugin* glade_plugin = ANJUTA_PLUGIN_GLADE(plugin);
81 GladePluginPriv* priv = glade_plugin->priv;
82 GObject *editor;
83 editor = g_value_get_object (value);
84 if (ANJUTA_IS_DESIGN_DOCUMENT(editor))
86 AnjutaDesignDocument* view = ANJUTA_DESIGN_DOCUMENT(editor);
87 GladeProject* project = glade_design_view_get_project(GLADE_DESIGN_VIEW(view));
88 if (!view->is_project_added)
90 glade_app_add_project (project);
91 g_signal_connect (G_OBJECT (project), "notify::pointer-mode",
92 G_CALLBACK (on_pointer_mode_changed), glade_plugin);
93 view->is_project_added = TRUE;
95 /* Change view components */
96 glade_palette_set_project (GLADE_PALETTE (priv->palette), project);
97 glade_inspector_set_project (GLADE_INSPECTOR (priv->inspector), project);
101 static void
102 value_removed_current_editor (AnjutaPlugin *plugin,
103 const char *name, gpointer data)
108 static void
109 on_signal_editor_created (GladeApp* app,
110 GladeSignalEditor* seditor,
111 gpointer data)
113 glade_signal_editor_enable_dnd (seditor, TRUE);
116 static void
117 on_api_help (GladeEditor* editor,
118 const gchar* book,
119 const gchar* page,
120 const gchar* search,
121 GladePlugin* plugin)
124 AnjutaPlugin* aplugin = ANJUTA_PLUGIN(plugin);
125 AnjutaShell* shell = aplugin->shell;
126 IAnjutaHelp* help;
128 help = anjuta_shell_get_interface(shell, IAnjutaHelp, NULL);
130 /* No API Help Plugin */
131 if (help == NULL)
132 return;
135 if (search)
136 ianjuta_help_search(help, search, NULL);
139 static void
140 glade_do_close (GladePlugin *plugin, GladeProject *project)
142 glade_app_remove_project (project);
145 static void
146 on_document_destroy (GtkWidget* document, GladePlugin *plugin)
148 GladeProject *project;
150 DEBUG_PRINT ("%s", "Destroying Document");
152 if (plugin->priv->destroying)
154 return;
157 project = glade_design_view_get_project(GLADE_DESIGN_VIEW(document));
158 if (!project)
160 return;
163 glade_do_close (plugin, project);
165 plugin->priv->file_count--;
166 if (plugin->priv->file_count <= 0)
167 anjuta_plugin_deactivate (ANJUTA_PLUGIN (plugin));
168 else
169 on_pointer_mode_changed (project, NULL, plugin);
172 static void
173 on_document_mapped (GtkWidget* doc, GladePlugin* plugin)
175 GladeProject* project = glade_design_view_get_project (GLADE_DESIGN_VIEW (doc));
176 GladeEditor* editor = GLADE_EDITOR (plugin->priv->editor);
177 GList* glade_obj_node;
178 GList* list = g_list_copy ((GList*)glade_project_get_objects (project));
181 gboolean first = TRUE;
183 /* Select the all windows in the project, select the first */
184 for (glade_obj_node = list;
185 glade_obj_node != NULL;
186 glade_obj_node = g_list_next (glade_obj_node))
188 GObject *glade_obj = G_OBJECT (glade_obj_node->data);
189 GladeWidget* glade_widget = glade_widget_get_from_gobject (glade_obj);
190 if (glade_widget == glade_widget_get_toplevel (glade_widget))
192 glade_project_widget_visibility_changed (project, glade_widget, TRUE);
193 glade_editor_load_widget (editor, glade_widget);
195 if (first)
197 glade_project_selection_set (project, glade_obj, TRUE);
198 first = FALSE;
202 g_list_free (list);
204 /* Only do this on first map */
205 g_signal_handlers_disconnect_by_func (doc, G_CALLBACK (on_document_mapped),
206 project);
209 static void
210 on_shell_destroy (AnjutaShell* shell, GladePlugin *glade_plugin)
212 glade_plugin->priv->destroying = TRUE;
215 static void
216 register_stock_icons (AnjutaPlugin *plugin)
218 static gboolean registered = FALSE;
220 if (registered)
221 return;
222 registered = TRUE;
224 /* Register stock icons */
225 BEGIN_REGISTER_ICON (plugin);
226 REGISTER_ICON_FULL ("anjuta-glade-plugin",
227 "glade-plugin-icon");
228 REGISTER_ICON_FULL ("anjuta-glade-widgets",
229 "glade-plugin-widgets");
230 REGISTER_ICON_FULL ("anjuta-glade-palette",
231 "glade-plugin-palette");
232 END_REGISTER_ICON;
235 static void
236 on_session_save (AnjutaShell *shell, AnjutaSessionPhase phase,
237 AnjutaSession *session, GladePlugin *plugin)
239 GList *files, *docwids, *node;
240 /* GtkTreeModel *model;
241 GtkTreeIter iter;
243 IAnjutaDocumentManager *docman;
245 if (phase != ANJUTA_SESSION_PHASE_NORMAL)
246 return;
248 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN(plugin)->shell,
249 IAnjutaDocumentManager, NULL);
250 docwids = ianjuta_document_manager_get_doc_widgets (docman, NULL);
251 if (docwids)
253 files = anjuta_session_get_string_list (session, "File Loader", "Files");
254 if (files)
255 files = g_list_reverse (files);
256 for (node = docwids; node != NULL; node = g_list_next (node))
258 if (ANJUTA_IS_DESIGN_DOCUMENT (node->data))
260 GFile* file;
261 file = ianjuta_file_get_file (IANJUTA_FILE (node->data), NULL);
262 if (file != NULL)
264 files = g_list_prepend (files, anjuta_session_get_relative_uri_from_file (session, file, NULL));
265 g_object_unref (file);
266 /* uri is not freed here */
270 g_list_free (docwids);
271 if (files)
273 files = g_list_reverse (files);
274 anjuta_session_set_string_list (session, "File Loader", "Files", files);
275 g_list_foreach (files, (GFunc)g_free, NULL);
276 g_list_free (files);
281 static void
282 glade_plugin_selection_changed (GladeProject *project,
283 GladePlugin *plugin)
285 GladeWidget *glade_widget = NULL;
287 if (glade_project_get_has_selection (project))
289 GList *list;
290 GList *node;
292 list = glade_project_selection_get (project);
294 for (node = list; node != NULL; node = g_list_next (node))
296 glade_widget = glade_widget_get_from_gobject (G_OBJECT (node->data));
297 glade_widget_show (glade_widget);
299 glade_editor_load_widget (GLADE_EDITOR (plugin->priv->editor), glade_widget);
301 else
302 glade_editor_load_widget (GLADE_EDITOR (plugin->priv->editor), NULL);
305 static void
306 glade_plugin_add_project (GladePlugin *glade_plugin, GladeProject *project)
308 GtkWidget *view;
309 GladePluginPriv *priv;
310 IAnjutaDocumentManager* docman =
311 anjuta_shell_get_interface(ANJUTA_PLUGIN(glade_plugin)->shell,
312 IAnjutaDocumentManager, NULL);
314 g_return_if_fail (GLADE_IS_PROJECT (project));
316 priv = glade_plugin->priv;
318 /* Create document */
319 view = anjuta_design_document_new(glade_plugin, project);
320 g_signal_connect (view, "destroy",
321 G_CALLBACK (on_document_destroy), glade_plugin);
322 g_signal_connect (view, "map", G_CALLBACK (on_document_mapped), glade_plugin);
323 gtk_widget_show (view);
324 g_object_set_data (G_OBJECT (project), "design_view", view);
326 /* Change view components */
327 glade_palette_set_project (GLADE_PALETTE (priv->palette), project);
329 /* Connect signal */
330 g_signal_connect (project, "selection-changed",
331 G_CALLBACK (glade_plugin_selection_changed),
332 glade_plugin);
334 priv->file_count++;
336 ianjuta_document_manager_add_document (docman, IANJUTA_DOCUMENT(view), NULL);
339 static void
340 add_glade_member (GladeWidget *widget,
341 AnjutaPlugin *plugin)
343 IAnjutaDocumentManager *docman;
344 GladeProject *project = glade_widget_get_project (widget);
345 gchar *path = glade_project_get_path (project);
346 gchar *widget_name = glade_widget_get_name (widget);
347 gchar *widget_typename = G_OBJECT_TYPE_NAME (glade_widget_get_object(widget));
348 GList *docs;
349 GList *item;
351 docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
352 IAnjutaDocumentManager, NULL);
353 if (!docman)
354 return;
356 docs = ianjuta_document_manager_get_doc_widgets (docman, NULL);
357 if (!docs) return;
359 for (item = docs; item != NULL; item = g_list_next (item))
361 IAnjutaDocument *curr_doc = IANJUTA_DOCUMENT(item->data);
362 if (!IANJUTA_IS_EDITOR (curr_doc)) continue;
364 g_signal_emit_by_name (G_OBJECT (curr_doc), "glade-member-add",
365 widget_typename, widget_name, path);
369 static void
370 inspector_item_activated_cb (GladeInspector *inspector,
371 AnjutaPlugin *plugin)
373 GList *items = glade_inspector_get_selected_items (inspector);
374 GList *item;
375 g_assert (GLADE_IS_WIDGET (items->data) && (items->next == NULL));
377 /* switch to this widget in the workspace */
378 for (item = items; item != NULL; item = g_list_next (item))
380 glade_widget_hide (GLADE_WIDGET (item->data));
381 glade_widget_show (GLADE_WIDGET (item->data));
382 add_glade_member (item->data, plugin);
385 g_list_free (item);
388 static void
389 on_selector_button_toggled (GtkToggleToolButton * button, GladePlugin *plugin)
391 GladeProject *active_project = glade_inspector_get_project(GLADE_INSPECTOR (plugin->priv->inspector));
393 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (plugin->priv->selector_toggle)))
395 glade_project_set_add_item (active_project, NULL);
396 glade_project_set_pointer_mode (active_project, GLADE_POINTER_SELECT);
398 else
399 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->selector_toggle), TRUE);
402 static void
403 on_drag_resize_button_toggled (GtkToggleToolButton *button,
404 GladePlugin *plugin)
406 GladeProject *active_project = glade_inspector_get_project(GLADE_INSPECTOR (plugin->priv->inspector));
408 if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (plugin->priv->resize_toggle)))
409 glade_project_set_pointer_mode (active_project, GLADE_POINTER_DRAG_RESIZE);
410 else
411 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->resize_toggle), TRUE);
415 static void
416 on_pointer_mode_changed (GladeProject *project,
417 GParamSpec *pspec,
418 GladePlugin *plugin)
420 GladeProject *active_project = glade_inspector_get_project(GLADE_INSPECTOR (plugin->priv->inspector));
422 if (!active_project)
424 gtk_widget_set_sensitive (plugin->priv->selector_toggle, FALSE);
425 gtk_widget_set_sensitive (plugin->priv->resize_toggle, FALSE);
426 return;
428 else if (active_project != project)
429 return;
431 gtk_widget_set_sensitive (plugin->priv->selector_toggle, TRUE);
432 gtk_widget_set_sensitive (plugin->priv->resize_toggle, TRUE);
434 g_signal_handlers_block_by_func (plugin->priv->selector_toggle,
435 on_selector_button_toggled, plugin);
436 g_signal_handlers_block_by_func (plugin->priv->resize_toggle,
437 on_drag_resize_button_toggled, plugin);
439 if (glade_project_get_pointer_mode (project) == GLADE_POINTER_SELECT)
441 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->selector_toggle), TRUE);
442 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->resize_toggle), FALSE);
444 else if (glade_project_get_pointer_mode (project) == GLADE_POINTER_DRAG_RESIZE)
446 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->resize_toggle), TRUE);
447 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->selector_toggle), FALSE);
449 else
451 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->resize_toggle), FALSE);
452 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (plugin->priv->selector_toggle), FALSE);
455 g_signal_handlers_unblock_by_func (plugin->priv->selector_toggle,
456 on_selector_button_toggled, plugin);
457 g_signal_handlers_unblock_by_func (plugin->priv->resize_toggle,
458 on_drag_resize_button_toggled, plugin);
461 /* Progress callbacks */
462 static void
463 glade_plugin_parse_began (GladeProject *project,
464 GladePlugin *plugin)
466 AnjutaStatus *status = anjuta_shell_get_status (ANJUTA_PLUGIN(plugin)->shell,
467 NULL);
468 anjuta_status_busy_push (status);
469 plugin->priv->add_ticks = TRUE;
472 static void
473 glade_plugin_parse_finished (GladeProject *project,
474 AnjutaPlugin *plugin)
476 AnjutaStatus *status = anjuta_shell_get_status (ANJUTA_PLUGIN(plugin)->shell,
477 NULL);
478 GladePlugin* gplugin = ANJUTA_PLUGIN_GLADE (plugin);
479 anjuta_status_busy_pop (status);
481 glade_inspector_set_project (GLADE_INSPECTOR (gplugin->priv->inspector), project);
484 static void
485 glade_plugin_load_progress (GladeProject *project,
486 gint total_ticks,
487 gint current_ticks,
488 AnjutaPlugin *plugin)
490 GladePlugin *glade_plugin = ANJUTA_PLUGIN_GLADE (plugin);
491 AnjutaStatus *status = anjuta_shell_get_status (plugin->shell,
492 NULL);
493 gchar *text;
494 gchar *project_name;
495 static GdkPixbuf* icon = NULL;
497 if (!icon)
499 icon = gtk_icon_theme_load_icon (gtk_icon_theme_get_default(),
500 "glade-plugin-icon",
501 GTK_ICON_SIZE_BUTTON,
502 0, NULL);
506 if (glade_plugin->priv->add_ticks)
508 glade_plugin->priv->add_ticks = FALSE;
509 anjuta_status_progress_add_ticks (status, total_ticks);
512 project_name = glade_project_get_name (project);
513 text = g_strdup_printf ("Loading %s…", project_name);
514 anjuta_status_progress_tick (status,
515 icon,
516 text);
517 g_free (text);
518 g_free (project_name);
521 static GtkWidget *
522 create_selector_tool_button ()
524 GtkWidget *button;
525 GtkWidget *image;
526 gchar *image_path;
528 image_path =
529 g_build_filename (glade_app_get_pixmaps_dir (), "selector.png", NULL);
530 image = gtk_image_new_from_file (image_path);
531 g_free (image_path);
533 button = gtk_toggle_button_new ();
534 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
536 gtk_button_set_image (GTK_BUTTON (button), image);
538 gtk_widget_set_tooltip_text (button,
539 _("Select widgets in the workspace"));
541 gtk_widget_show (button);
542 gtk_widget_show (image);
544 return button;
547 static GtkWidget *
548 create_drag_resize_tool_button ()
550 GtkWidget *button;
551 GtkWidget *image;
552 gchar *image_path;
554 image_path =
555 g_build_filename (glade_app_get_pixmaps_dir (), "drag-resize.png", NULL);
556 image = gtk_image_new_from_file (image_path);
557 g_free (image_path);
559 button = gtk_toggle_button_new ();
560 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
562 gtk_button_set_image (GTK_BUTTON (button), image);
564 gtk_widget_set_tooltip_text (button,
565 _("Drag and resize widgets in the workspace"));
567 gtk_widget_show (button);
568 gtk_widget_show (image);
570 return button;
573 static gboolean
574 activate_plugin (AnjutaPlugin *plugin)
576 GladePlugin *glade_plugin;
577 GladePluginPriv *priv;
578 AnjutaStatus* status;
579 GtkWidget* button_box;
581 DEBUG_PRINT ("%s", "GladePlugin: Activating Glade plugin…");
583 glade_plugin = ANJUTA_PLUGIN_GLADE (plugin);
585 status = anjuta_shell_get_status (plugin->shell, NULL);
586 priv = glade_plugin->priv;
588 register_stock_icons (plugin);
590 anjuta_status_busy_push (status);
591 anjuta_status_set (status, "%s", _("Loading Glade…"));
593 priv->app = glade_app_get ();
594 if (!priv->app)
596 priv->app = glade_app_new ();
599 glade_app_set_window (GTK_WIDGET (ANJUTA_PLUGIN(plugin)->shell));
601 priv->inspector = glade_inspector_new ();
603 g_signal_connect (priv->inspector, "item-activated",
604 G_CALLBACK (inspector_item_activated_cb),
605 plugin);
607 priv->paned = gtk_paned_new (GTK_ORIENTATION_VERTICAL);
609 priv->editor = GTK_WIDGET(glade_editor_new());
611 priv->palette = glade_palette_new();
612 priv->palette_box = gtk_vbox_new (FALSE, 5);
613 priv->selector_toggle = create_selector_tool_button ();
614 priv->resize_toggle = create_drag_resize_tool_button ();
615 button_box = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 0);
616 gtk_box_pack_start (GTK_BOX (button_box),
617 priv->selector_toggle,
618 FALSE, FALSE, 0);
619 gtk_box_pack_start (GTK_BOX (button_box),
620 priv->resize_toggle,
621 FALSE, FALSE, 0);
622 gtk_box_pack_start (GTK_BOX (priv->palette_box),
623 button_box,
624 FALSE, FALSE, 0);
625 gtk_box_pack_start (GTK_BOX (priv->palette_box),
626 priv->palette,
627 TRUE, TRUE, 0);
630 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->selector_toggle),
631 TRUE);
632 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->resize_toggle),
633 FALSE);
635 g_signal_connect (G_OBJECT (priv->selector_toggle), "toggled",
636 G_CALLBACK (on_selector_button_toggled), plugin);
637 g_signal_connect (G_OBJECT (priv->resize_toggle), "toggled",
638 G_CALLBACK (on_drag_resize_button_toggled), plugin);
641 glade_palette_set_show_selector_button (GLADE_PALETTE (priv->palette),
642 FALSE);
644 gtk_paned_add1 (GTK_PANED(priv->paned), priv->inspector);
645 gtk_paned_add2 (GTK_PANED(priv->paned), priv->editor);
647 gtk_widget_show_all (priv->paned);
649 anjuta_status_busy_pop (status);
651 g_signal_connect(plugin->shell, "destroy",
652 G_CALLBACK(on_shell_destroy), plugin);
654 g_signal_connect(priv->app, "doc-search",
655 G_CALLBACK(on_api_help), plugin);
657 g_signal_connect(priv->app, "signal-editor-created",
658 G_CALLBACK(on_signal_editor_created), plugin);
660 gtk_widget_show (priv->palette);
661 gtk_widget_show (priv->editor);
662 gtk_widget_show (priv->inspector);
664 /* Add widgets */
665 anjuta_shell_add_widget (anjuta_plugin_get_shell (ANJUTA_PLUGIN (plugin)),
666 priv->paned,
667 "AnjutaGladeTree", _("Widgets"),
668 "glade-plugin-widgets",
669 ANJUTA_SHELL_PLACEMENT_RIGHT, NULL);
670 anjuta_shell_add_widget (anjuta_plugin_get_shell (ANJUTA_PLUGIN (plugin)),
671 priv->palette_box,
672 "AnjutaGladePalette", _("Palette"),
673 "glade-plugin-palette",
674 ANJUTA_SHELL_PLACEMENT_LEFT, NULL);
675 /* Connect to save session */
676 g_signal_connect (G_OBJECT (plugin->shell), "save_session",
677 G_CALLBACK (on_session_save), plugin);
679 /* Watch documents */
680 glade_plugin->priv->editor_watch_id =
681 anjuta_plugin_add_watch (plugin, IANJUTA_DOCUMENT_MANAGER_CURRENT_DOCUMENT,
682 value_added_current_editor,
683 value_removed_current_editor, NULL);
685 return TRUE;
688 static gboolean
689 deactivate_plugin (AnjutaPlugin *plugin)
691 GladePluginPriv *priv;
693 priv = ANJUTA_PLUGIN_GLADE (plugin)->priv;
695 DEBUG_PRINT ("%s", "GladePlugin: Dectivating Glade plugin…");
697 if (glade_app_get_projects ())
699 /* Cannot deactivate plugin if there are still files open */
700 return FALSE;
704 /* Disconnect signals */
705 g_signal_handlers_disconnect_by_func (plugin->shell,
706 G_CALLBACK (on_shell_destroy),
707 plugin);
709 g_signal_handlers_disconnect_by_func (plugin->shell,
710 G_CALLBACK (on_session_save), plugin);
712 g_signal_handlers_disconnect_by_func (priv->app,
713 G_CALLBACK(on_api_help), plugin);
715 /* Remove widgets */
716 anjuta_shell_remove_widget (anjuta_plugin_get_shell (plugin),
717 priv->palette_box,
718 NULL);
719 anjuta_shell_remove_widget (anjuta_plugin_get_shell (plugin),
720 priv->paned,
721 NULL);
723 priv->uiid = 0;
724 priv->action_group = NULL;
726 priv->app = NULL;
728 return TRUE;
731 static void
732 glade_plugin_dispose (GObject *obj)
734 /* GladePlugin *plugin = ANJUTA_PLUGIN_GLADE (obj); */
736 G_OBJECT_CLASS (parent_class)->dispose (obj);
739 static void
740 glade_plugin_finalize (GObject *obj)
742 GladePlugin *plugin = ANJUTA_PLUGIN_GLADE (obj);
743 g_free (plugin->priv);
744 G_OBJECT_CLASS (parent_class)->finalize (obj);
747 static void
748 glade_plugin_instance_init (GObject *obj)
750 GladePluginPriv *priv;
751 GladePlugin *plugin = ANJUTA_PLUGIN_GLADE (obj);
753 plugin->priv = (GladePluginPriv *) g_new0 (GladePluginPriv, 1);
754 priv = plugin->priv;
755 priv->destroying = FALSE;
756 priv->file_count = 0;
757 priv->add_ticks = FALSE;
759 DEBUG_PRINT ("%s", "Intializing Glade plugin");
762 static void
763 glade_plugin_class_init (GObjectClass *klass)
765 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
767 parent_class = g_type_class_peek_parent (klass);
769 plugin_class->activate = activate_plugin;
770 plugin_class->deactivate = deactivate_plugin;
771 klass->dispose = glade_plugin_dispose;
772 klass->finalize = glade_plugin_finalize;
775 static void
776 ifile_open (IAnjutaFile *ifile, GFile* file, GError **err)
778 AnjutaPlugin* plugin = ANJUTA_PLUGIN (ifile);
779 GladePluginPriv *priv;
780 GladeProject *project;
781 gchar *filename;
782 IAnjutaDocumentManager* docman;
783 GList* docwids, *node;
785 g_return_if_fail (file != NULL);
787 priv = ANJUTA_PLUGIN_GLADE (ifile)->priv;
789 filename = g_file_get_path (file);
790 if (!filename)
792 gchar* uri = g_file_get_parse_name(file);
793 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN (ifile)->shell),
794 _("Not local file: %s"), uri);
795 if (priv->file_count <= 0)
796 anjuta_plugin_deactivate (ANJUTA_PLUGIN (plugin));
798 g_free (uri);
799 return;
802 docman = anjuta_shell_get_interface(ANJUTA_PLUGIN(ifile)->shell, IAnjutaDocumentManager,
803 NULL);
804 docwids = ianjuta_document_manager_get_doc_widgets (docman, NULL);
805 if (docwids)
807 for (node = docwids; node != NULL; node = g_list_next (node))
809 if (ANJUTA_IS_DESIGN_DOCUMENT (node->data))
811 GFile* cur_file;
812 cur_file = ianjuta_file_get_file (IANJUTA_FILE (node->data), NULL);
813 if (cur_file)
815 if (g_file_equal (file, cur_file))
817 ianjuta_document_manager_set_current_document (docman,
818 IANJUTA_DOCUMENT (node->data), NULL);
819 g_object_unref (file);
820 return;
822 g_object_unref (file);
826 g_list_free (docwids);
829 project = glade_project_new ();
830 g_signal_connect (project, "parse-began",
831 G_CALLBACK (glade_plugin_parse_began), plugin);
832 g_signal_connect (project, "parse-finished",
833 G_CALLBACK (glade_plugin_parse_finished), plugin);
834 g_signal_connect (project, "load-progress",
835 G_CALLBACK (glade_plugin_load_progress), plugin);
836 if (!glade_project_load_from_file (project, filename))
838 gchar* name = g_file_get_parse_name (file);
839 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN (ifile)->shell),
840 _("Could not open %s"), name);
841 if (priv->file_count <= 0)
842 anjuta_plugin_deactivate (ANJUTA_PLUGIN (plugin));
843 g_free (name);
844 g_free (filename);
845 return;
847 g_free (filename);
849 glade_plugin_add_project (ANJUTA_PLUGIN_GLADE (ifile), project);
851 anjuta_shell_present_widget (ANJUTA_PLUGIN (ifile)->shell, priv->paned, NULL);
854 static GFile*
855 ifile_get_file (IAnjutaFile* ifile, GError** e)
857 GladePlugin* plugin = (GladePlugin*) ifile;
858 const gchar* path =
859 glade_project_get_path(glade_inspector_get_project(GLADE_INSPECTOR (plugin->priv->inspector)));
860 GFile* file = g_file_new_for_path (path);
861 return file;
864 static void
865 ifile_iface_init(IAnjutaFileIface *iface)
867 iface->open = ifile_open;
868 iface->get_file = ifile_get_file;
871 static void
872 iwizard_activate (IAnjutaWizard *iwizard, GError **err)
874 GladePluginPriv *priv;
875 GladeProject *project;
877 priv = ANJUTA_PLUGIN_GLADE (iwizard)->priv;
879 project = glade_project_new ();
880 if (!project)
882 anjuta_util_dialog_warning (GTK_WINDOW (ANJUTA_PLUGIN (iwizard)->shell),
883 _("Could not create a new glade project."));
884 return;
886 glade_plugin_add_project (ANJUTA_PLUGIN_GLADE (iwizard), project);
887 anjuta_shell_present_widget (ANJUTA_PLUGIN (iwizard)->shell,
888 priv->palette_box, NULL);
891 static void
892 iwizard_iface_init(IAnjutaWizardIface *iface)
894 iface->activate = iwizard_activate;
897 ANJUTA_PLUGIN_BEGIN (GladePlugin, glade_plugin);
898 ANJUTA_PLUGIN_ADD_INTERFACE (ifile, IANJUTA_TYPE_FILE);
899 ANJUTA_PLUGIN_ADD_INTERFACE (iwizard, IANJUTA_TYPE_WIZARD);
900 ANJUTA_PLUGIN_END;
902 ANJUTA_SIMPLE_PLUGIN (GladePlugin, glade_plugin);