[anjuta] Automatically detect which file that will hold member widgets declaration and initialization.



commit cb09248d73a4bcea15c03768bc026f51996bf74b
Author: Marco Diego AurÃlio Mesquita <marcodiegomesquita gmail com>
Date:   Mon Jan 30 09:37:14 2012 -0300

    Automatically detect which file that will hold member widgets declaration and initialization.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=669023

 libanjuta/interfaces/libanjuta.idl                 |    4 +-
 manuals/anjuta-manual/C/anjuta-glade-start.page    |    8 +-
 plugins/glade/plugin.c                             |   31 +++++----
 plugins/language-support-cpp-java/plugin.c         |   65 ++++++++++++++-----
 .../templates/gtkapplication/src/application.c     |    4 +-
 5 files changed, 74 insertions(+), 38 deletions(-)
---
diff --git a/libanjuta/interfaces/libanjuta.idl b/libanjuta/interfaces/libanjuta.idl
index b625476..2453293 100644
--- a/libanjuta/interfaces/libanjuta.idl
+++ b/libanjuta/interfaces/libanjuta.idl
@@ -1277,12 +1277,14 @@ interface IAnjutaEditor
 
 	/**
 	 * IAnjutaEditor::glade-member-add:
+	 * @widget_typename: Name of the type of the widget that will become a member of the class.
 	 * @widget_name: Name of the widget that will become a member of the class.
+	 * @filename: Path for the .ui file that generated the signal.
 	 * @obj: Self
 	 *
 	 * This signal is emitted when code for a widget must be generated.
 	 */
-	void   ::glade_member_add (gchar *widget_name);
+	void   ::glade_member_add (gchar *widget_typename, gchar *widget_name, gchar *filename);
 
 	/**
 	 * IAnjutaEditor::code-added:
diff --git a/manuals/anjuta-manual/C/anjuta-glade-start.page b/manuals/anjuta-manual/C/anjuta-glade-start.page
index a902b15..5ae8b42 100644
--- a/manuals/anjuta-manual/C/anjuta-glade-start.page
+++ b/manuals/anjuta-manual/C/anjuta-glade-start.page
@@ -71,11 +71,11 @@
 		</p>
 
 		<p>Once the glade plug-in is running and the file that will hold the code
-		is being viewed, simply double click a widget in the glade inspector.
+		is opened, simply double click a widget in the glade inspector.
 		The file being viewed will then be scanned for some marker comments
-		(/* ANJUTA: Widgets declaration - DO NOT REMOVE */ and
-		/* ANJUTA: Widgets initialization - DO NOT REMOVE */) and, if found, code
-		will be added right after such comments. So, for this feature to work
+		(/* ANJUTA: Widgets declaration for application.ui - DO NOT REMOVE */ and
+		/* ANJUTA: Widgets initialization for application.ui - DO NOT REMOVE */) and, if
+                found, code will be added right after such comments. So, for this feature to work
 		correctly, it is important not to modify such marker comments.
 		</p>
 	</section>
diff --git a/plugins/glade/plugin.c b/plugins/glade/plugin.c
index dc47706..7cb600c 100644
--- a/plugins/glade/plugin.c
+++ b/plugins/glade/plugin.c
@@ -333,32 +333,37 @@ glade_plugin_add_project (GladePlugin *glade_plugin, GladeProject *project)
 
 	priv->file_count++;
 
-	ianjuta_document_manager_add_document(docman, IANJUTA_DOCUMENT(view), NULL);
+	ianjuta_document_manager_add_document (docman, IANJUTA_DOCUMENT(view), NULL);
 }
 
 static void
 add_glade_member (GladeWidget		 *widget,
 				  AnjutaPlugin       *plugin)
 {
-	IAnjutaEditor* current_editor;
-	IAnjutaDocument *doc;
 	IAnjutaDocumentManager *docman;
+	GladeProject *project = glade_widget_get_project (widget);
+	gchar *path = glade_project_get_path (project);
+	gchar *widget_name = glade_widget_get_name (widget);
+	gchar *widget_typename = G_OBJECT_TYPE_NAME (glade_widget_get_object(widget));
+	GList *docs;
+	GList *item;
 
 	docman = anjuta_shell_get_interface (ANJUTA_PLUGIN (plugin)->shell,
 										 IAnjutaDocumentManager, NULL);
-	if(!docman)
+	if (!docman)
 		return;
 
-	doc = ianjuta_document_manager_get_current_document (docman, NULL);
-	if(!doc)
-		return;
+	docs = ianjuta_document_manager_get_doc_widgets (docman, NULL);
+	if (!docs) return;
 
-	current_editor = IANJUTA_EDITOR (doc);
-	if(!current_editor)
-		return;
+	for (item = docs; item != NULL; item = g_list_next (item))
+	{
+		IAnjutaDocument *curr_doc = IANJUTA_DOCUMENT(item->data);
+		if (!IANJUTA_IS_EDITOR (curr_doc)) continue;
 
-	g_signal_emit_by_name (G_OBJECT (current_editor), "glade-member-add",
-						   glade_widget_get_name (widget));
+		g_signal_emit_by_name (G_OBJECT (curr_doc), "glade-member-add",
+							   widget_typename, widget_name, path);
+	}
 }
 
 static void
@@ -369,8 +374,6 @@ inspector_item_activated_cb (GladeInspector     *inspector,
 	GList *item;
 	g_assert (GLADE_IS_WIDGET (items->data) && (items->next == NULL));
 
-	GladeWidget *widget = GLADE_WIDGET (items->data);
-
 	/* switch to this widget in the workspace */
 	for (item = items; item != NULL; item = g_list_next (item))
 	{
diff --git a/plugins/language-support-cpp-java/plugin.c b/plugins/language-support-cpp-java/plugin.c
index 76dac50..2f4156d 100644
--- a/plugins/language-support-cpp-java/plugin.c
+++ b/plugins/language-support-cpp-java/plugin.c
@@ -600,11 +600,11 @@ static IAnjutaIterable*
 language_support_get_mark_position (IAnjutaEditor* editor, gchar* mark)
 {
 	IAnjutaEditorCell *search_start = IANJUTA_EDITOR_CELL (
-										ianjuta_editor_get_start_position(editor, NULL));
+										ianjuta_editor_get_start_position (editor, NULL));
 	IAnjutaEditorCell *search_end = IANJUTA_EDITOR_CELL (
-										ianjuta_editor_get_end_position(editor, NULL));
-	IAnjutaEditorCell *result_start;
-	IAnjutaEditorCell *result_end;
+										ianjuta_editor_get_end_position (editor, NULL));
+	IAnjutaEditorCell *result_start = NULL;
+	IAnjutaEditorCell *result_end = NULL;
 
 	ianjuta_editor_search_forward (IANJUTA_EDITOR_SEARCH (editor),
 								   mark, FALSE,
@@ -612,9 +612,10 @@ language_support_get_mark_position (IAnjutaEditor* editor, gchar* mark)
 								   &result_start,
 								   &result_end, NULL);
 
-	g_object_unref (result_start);
+	if (result_start)
+		g_object_unref (result_start);
 
-	return IANJUTA_ITERABLE (result_end);
+	return result_end ? IANJUTA_ITERABLE (result_end) : NULL;
 }
 
 static IAnjutaIterable*
@@ -815,23 +816,23 @@ on_glade_drop (IAnjutaEditor* editor,
 	g_strfreev (split_signal_data);
 }
 
-static gchar* generate_widget_member_decl_str(gchar *widget_name)
+static gchar* generate_widget_member_decl_str (gchar* widget_name)
 {
        return g_strdup_printf ("\n\tGtkWidget* %s;", widget_name);
 }
 
-static gchar *generate_widget_member_init_str(gchar *widget_name)
+static gchar* generate_widget_member_init_str (gchar* widget_name)
 {
        return g_strdup_printf ("\n\tpriv->%s = GTK_WIDGET ("
                                "gtk_builder_get_object(builder, \"%s\"));", widget_name, widget_name);
 }
 
 static gboolean insert_after_mark (IAnjutaEditor* editor, gchar* mark,
-gchar* code_to_add)
+                                   gchar* code_to_add, CppJavaPlugin* lang_plugin)
 {
        IAnjutaIterable* mark_position;
        mark_position = language_support_get_mark_position (editor, mark);
-       if(!mark_position)
+       if (!mark_position)
 		return FALSE;
 
        ianjuta_editor_insert (editor, mark_position, code_to_add, -1, NULL);
@@ -843,27 +844,49 @@ gchar* code_to_add)
        return TRUE;
 }
 
+static gchar*
+generate_widget_member_decl_marker (gchar* ui_filename)
+{
+	return g_strdup_printf ("/* ANJUTA: Widgets declaration for %s - DO NOT REMOVE */", ui_filename);
+}
+
+static gchar*
+generate_widget_member_init_marker (gchar* ui_filename)
+{
+	return g_strdup_printf ("/* ANJUTA: Widgets initialization for %s - DO NOT REMOVE */", ui_filename);
+}
+
 static void insert_member_decl_and_init (IAnjutaEditor* editor, gchar* widget_name,
-                                         CppJavaPlugin *lang_plugin)
+                                         gchar* ui_filename, CppJavaPlugin* lang_plugin)
 {
        AnjutaStatus* status;
        gchar* member_decl = generate_widget_member_decl_str(widget_name);
        gchar* member_init = generate_widget_member_init_str(widget_name);
 
+       gchar* member_decl_marker = generate_widget_member_decl_marker (ui_filename);
+       gchar* member_init_marker = generate_widget_member_init_marker (ui_filename);
+
        status = anjuta_shell_get_status (ANJUTA_PLUGIN (lang_plugin)->shell, NULL);
 
-       if(insert_after_mark (editor, "/* ANJUTA: Widgets declaration - DO NOT REMOVE */", member_decl) &&
-          insert_after_mark (editor, "/* ANJUTA: Widgets initialization - DO NOT REMOVE */", member_init))
+       if(insert_after_mark (editor, member_decl_marker, member_decl, lang_plugin) &&
+          insert_after_mark (editor, member_init_marker, member_init, lang_plugin))
               anjuta_status_set (status, _("Code added for widget."));
 
        g_free (member_decl);
        g_free (member_init);
+
+       g_free (member_decl_marker);
+       g_free (member_init_marker);
 }
 
 static void
-on_glade_member_add (IAnjutaEditor *editor, gchar *widget_name, CppJavaPlugin *lang_plugin)
+on_glade_member_add (IAnjutaEditor* editor, gchar* widget_typename,
+                     gchar* widget_name, gchar* path, CppJavaPlugin* lang_plugin)
 {
-	insert_member_decl_and_init (editor, widget_name, lang_plugin);
+	GFile* ui_file = g_file_new_for_path (path);
+	gchar* ui_filename = g_file_get_basename (ui_file);
+
+	insert_member_decl_and_init (editor, widget_name, ui_filename, lang_plugin);
 }
 
 /* Enable/Disable language-support */
@@ -897,6 +920,16 @@ install_support (CppJavaPlugin *lang_plugin)
 						  G_CALLBACK (cpp_indentation),
 						  lang_plugin);
 
+		// Since this signal is not disconnect on plugin uninstall, we have to prevent multiple connection.
+		if (!g_signal_handler_find (lang_plugin->current_editor,
+									G_SIGNAL_MATCH_FUNC,
+									0, //Signal id (ignored)
+									0, //detail (ignored)
+									0, //closure (ignored)
+									G_CALLBACK (on_glade_member_add),
+									0 //data (ignored)
+									)
+		   )
 		g_signal_connect (lang_plugin->current_editor,
 						  "glade-member-add",
 						  G_CALLBACK (on_glade_member_add),
@@ -985,8 +1018,6 @@ uninstall_support (CppJavaPlugin *lang_plugin)
 	                                      on_glade_drop_possible, lang_plugin);
 	g_signal_handlers_disconnect_by_func (lang_plugin->current_editor,
 	                                      on_glade_drop, lang_plugin);
-	g_signal_handlers_disconnect_by_func (lang_plugin->current_editor,
-										  on_glade_member_add, lang_plugin);
 	if (lang_plugin->packages)
 	{
 		g_object_unref (lang_plugin->packages);
diff --git a/plugins/project-wizard/templates/gtkapplication/src/application.c b/plugins/project-wizard/templates/gtkapplication/src/application.c
index 1580b54..298e3b6 100644
--- a/plugins/project-wizard/templates/gtkapplication/src/application.c
+++ b/plugins/project-wizard/templates/gtkapplication/src/application.c
@@ -27,7 +27,7 @@ G_DEFINE_TYPE ([+NameCClass+], [+NameCLower+], GTK_TYPE_APPLICATION);
 
 struct _[+NameCClass+]Private
 {
-	/* ANJUTA: Widgets declaration - DO NOT REMOVE */
+	/* ANJUTA: Widgets declaration for [+NameHLower+].ui - DO NOT REMOVE */
 };
 [+ENDIF+]
 
@@ -64,7 +64,7 @@ static void
         }
 
 	
-	/* ANJUTA: Widgets initialization - DO NOT REMOVE */
+	/* ANJUTA: Widgets initialization for [+NameHLower+].ui - DO NOT REMOVE */
 
 	g_object_unref (builder);
 [+ELSE+]



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]