[gedit] [gedit panel] Split the id managing from the display name when adding an item.



commit b863962b0979930b548a120394bf72051588b3e2
Author: Ignacio Casal Quinteiro <icq gnome org>
Date:   Sat May 1 21:06:04 2010 +0200

    [gedit panel] Split the id managing from the display name when adding an item.
    
    Now it is needed to pass an unique id for the item to be added to
    a gedit panel. This fixes bug #407689.

 gedit/gedit-panel.c                             |  108 ++++++++++++++++-------
 gedit/gedit-panel.h                             |   12 ++-
 gedit/gedit-window.c                            |    1 +
 plugin-loaders/python/bindings/gedit.defs       |   10 ++-
 plugin-loaders/python/bindings/gedit.override   |   24 +++---
 plugins/externaltools/tools/__init__.py         |    1 +
 plugins/filebrowser/gedit-file-browser-plugin.c |    1 +
 plugins/pythonconsole/pythonconsole/__init__.py |    3 +-
 plugins/taglist/gedit-taglist-plugin.c          |    3 +-
 9 files changed, 106 insertions(+), 57 deletions(-)
---
diff --git a/gedit/gedit-panel.c b/gedit/gedit-panel.c
index 9236e64..84fa92b 100644
--- a/gedit/gedit-panel.c
+++ b/gedit/gedit-panel.c
@@ -60,7 +60,8 @@ typedef struct _GeditPanelItem GeditPanelItem;
 
 struct _GeditPanelItem 
 {
-	gchar *name;
+	gchar *id;
+	gchar *display_name;
 	GtkWidget *icon;
 };
 
@@ -346,7 +347,7 @@ sync_title (GeditPanel     *panel,
 	if (item != NULL)
 	{
 		gtk_label_set_text (GTK_LABEL (panel->priv->title_label), 
-				    item->name);
+				    item->display_name);
 
 		set_gtk_image_from_gtk_image (GTK_IMAGE (panel->priv->title_image),
 					      GTK_IMAGE (item->icon));
@@ -642,19 +643,51 @@ build_tab_label (GeditPanel  *panel,
 	return hbox;
 }
 
+static gboolean
+item_exists (GeditPanel  *panel,
+	     const gchar *id)
+{
+	GeditPanelItem *data;
+	GList *items, *l;
+	gboolean exists = FALSE;
+
+	items = gtk_container_get_children (GTK_CONTAINER (panel->priv->notebook));
+
+	for (l = items; l != NULL; l = g_list_next (l))
+	{
+		data = (GeditPanelItem *)g_object_get_data (G_OBJECT (l->data),
+						            PANEL_ITEM_KEY);
+		g_return_val_if_fail (data != NULL, FALSE);
+
+		if (strcmp (data->id, id) == 0)
+		{
+			exists = TRUE;
+			break;
+		}
+	}
+
+	g_list_free (items);
+
+	return exists;
+}
+
 /**
  * gedit_panel_add_item:
  * @panel: a #GeditPanel
  * @item: the #GtkWidget to add to the @panel
- * @name: the name to be shown in the @panel
+ * @id: unique name for the new item
+ * @display_name: the name to be shown in the @panel
  * @image: the image to be shown in the @panel
  *
  * Adds a new item to the @panel.
+ *
+ * Returns: %TRUE is the item was successfully added.
  */
-void
-gedit_panel_add_item (GeditPanel  *panel, 
-		      GtkWidget   *item, 
-		      const gchar *name,
+gboolean
+gedit_panel_add_item (GeditPanel  *panel,
+		      GtkWidget   *item,
+		      const gchar *id,
+		      const gchar *display_name,
 		      GtkWidget   *image)
 {
 	GeditPanelItem *data;
@@ -662,14 +695,21 @@ gedit_panel_add_item (GeditPanel  *panel,
 	GtkWidget *menu_label;
 	gint w, h;
 	
-	g_return_if_fail (GEDIT_IS_PANEL (panel));
-	g_return_if_fail (GTK_IS_WIDGET (item));
-	g_return_if_fail (name != NULL);
-	g_return_if_fail (image == NULL || GTK_IS_IMAGE (image));
+	g_return_val_if_fail (GEDIT_IS_PANEL (panel), FALSE);
+	g_return_val_if_fail (GTK_IS_WIDGET (item), FALSE);
+	g_return_val_if_fail (id != NULL, FALSE);
+	g_return_val_if_fail (display_name != NULL, FALSE);
+	g_return_val_if_fail (image == NULL || GTK_IS_IMAGE (image), FALSE);
 
-	data = g_new (GeditPanelItem, 1);
+	if (item_exists (panel, id))
+	{
+		g_critical ("You are trying to add an item with an id that already exists");
+		return FALSE;
+	}
 
-	data->name = g_strdup (name);
+	data = g_slice_new (GeditPanelItem);
+	data->id = g_strdup (id);
+	data->display_name = g_strdup (display_name);
 
 	if (image == NULL)
 	{
@@ -689,9 +729,9 @@ gedit_panel_add_item (GeditPanel  *panel,
 		           PANEL_ITEM_KEY,
 		           data);
 
-	tab_label = build_tab_label (panel, item, data->name, data->icon);
+	tab_label = build_tab_label (panel, item, data->display_name, data->icon);
 
-	menu_label = gtk_label_new (name);
+	menu_label = gtk_label_new (display_name);
 	gtk_misc_set_alignment (GTK_MISC (menu_label), 0.0, 0.5);
 
 	if (!gtk_widget_get_visible (item))
@@ -703,21 +743,27 @@ gedit_panel_add_item (GeditPanel  *panel,
 				       menu_label);
 
 	g_signal_emit (G_OBJECT (panel), signals[ITEM_ADDED], 0, item);
+
+	return TRUE;
 }
 
 /**
  * gedit_panel_add_item_with_stock_icon:
  * @panel: a #GeditPanel
  * @item: the #GtkWidget to add to the @panel
- * @name: the name to be shown in the @panel
+ * @id: unique name for the new item
+ * @display_name: the name to be shown in the @panel
  * @stock_id: a stock id
  *
  * Same as gedit_panel_add_item() but using an image from stock.
+ *
+ * Returns: %TRUE is the item was successfully added.
  */
-void
-gedit_panel_add_item_with_stock_icon (GeditPanel  *panel, 
-				      GtkWidget   *item, 
-				      const gchar *name,
+gboolean
+gedit_panel_add_item_with_stock_icon (GeditPanel  *panel,
+				      GtkWidget   *item,
+				      const gchar *id,
+				      const gchar *display_name,
 				      const gchar *stock_id)
 {
 	GtkWidget *icon = NULL;
@@ -728,7 +774,7 @@ gedit_panel_add_item_with_stock_icon (GeditPanel  *panel,
 						 GTK_ICON_SIZE_MENU);
 	}
 
-	gedit_panel_add_item (panel, item, name, icon);
+	return gedit_panel_add_item (panel, item, id, display_name, icon);
 }
 
 /**
@@ -753,16 +799,17 @@ gedit_panel_remove_item (GeditPanel *panel,
 
 	page_num = gtk_notebook_page_num (GTK_NOTEBOOK (panel->priv->notebook),
 					  item);
-					  
+
 	if (page_num == -1)
 		return FALSE;
 		
 	data = (GeditPanelItem *)g_object_get_data (G_OBJECT (item),
 					            PANEL_ITEM_KEY);
 	g_return_val_if_fail (data != NULL, FALSE);
-	
-	g_free (data->name);
-	g_free (data);
+
+	g_free (data->id);
+	g_free (data->display_name);
+	g_slice_free (GeditPanelItem, data);
 
 	g_object_set_data (G_OBJECT (item),
 		           PANEL_ITEM_KEY,
@@ -896,18 +943,11 @@ _gedit_panel_get_active_item_id (GeditPanel *panel)
 				GTK_NOTEBOOK (panel->priv->notebook),
 				cur_page);
 
-	/* FIXME: for now we use as the hash of the name as id.
-	 * However the name is not guaranteed to be unique and
-	 * it is a translated string, so it's subotimal, but should
-	 * be good enough for now since we don't want to add an
-	 * ad hoc id argument.
-	 */
-
 	data = (GeditPanelItem *)g_object_get_data (G_OBJECT (item),
 					            PANEL_ITEM_KEY);
 	g_return_val_if_fail (data != NULL, 0);
 
-	return g_str_hash (data->name);
+	return g_str_hash (data->id);
 }
 
 void
@@ -936,7 +976,7 @@ _gedit_panel_set_active_item_by_id (GeditPanel *panel,
 						            PANEL_ITEM_KEY);
 		g_return_if_fail (data != NULL);
 
-		if (g_str_hash (data->name) == id)
+		if (g_str_hash (data->id) == id)
 		{
 			gtk_notebook_set_current_page (
 				GTK_NOTEBOOK (panel->priv->notebook), i);
diff --git a/gedit/gedit-panel.h b/gedit/gedit-panel.h
index 00f2ec8..133ea81 100644
--- a/gedit/gedit-panel.h
+++ b/gedit/gedit-panel.h
@@ -93,14 +93,16 @@ GType 		 gedit_panel_get_type 			(void) G_GNUC_CONST;
 
 GtkWidget 	*gedit_panel_new 			(GtkOrientation	 orientation);
 
-void		 gedit_panel_add_item			(GeditPanel     *panel,
-						      	 GtkWidget      *item,
-						      	 const gchar    *name,
+gboolean	 gedit_panel_add_item			(GeditPanel     *panel,
+							 GtkWidget      *item,
+							 const gchar    *id,
+							 const gchar    *display_name,
 							 GtkWidget      *image);
 
-void		 gedit_panel_add_item_with_stock_icon	(GeditPanel     *panel,
+gboolean	 gedit_panel_add_item_with_stock_icon	(GeditPanel     *panel,
 							 GtkWidget      *item,
-						      	 const gchar    *name,
+							 const gchar    *id,
+						      	 const gchar    *display_name,
 						      	 const gchar    *stock_id);
 
 gboolean	 gedit_panel_remove_item	(GeditPanel     *panel,
diff --git a/gedit/gedit-window.c b/gedit/gedit-window.c
index ac8e7ba..bdd919d 100644
--- a/gedit/gedit-window.c
+++ b/gedit/gedit-window.c
@@ -3690,6 +3690,7 @@ create_side_panel (GeditWindow *window)
 	documents_panel = gedit_documents_panel_new (window);
 	gedit_panel_add_item_with_stock_icon (GEDIT_PANEL (window->priv->side_panel),
 					      documents_panel,
+					      "GeditWindowDocumentsPanel",
 					      _("Documents"),
 					      GTK_STOCK_FILE);
 }
diff --git a/plugin-loaders/python/bindings/gedit.defs b/plugin-loaders/python/bindings/gedit.defs
index f54bb2e..134971d 100644
--- a/plugin-loaders/python/bindings/gedit.defs
+++ b/plugin-loaders/python/bindings/gedit.defs
@@ -596,10 +596,11 @@
 (define-method add_item
   (of-object "GeditPanel")
   (c-name "gedit_panel_add_item")
-  (return-type "none")
+  (return-type "gboolean")
   (parameters
     '("GtkWidget*" "item")
-    '("const-gchar*" "name")
+    '("const-gchar*" "id")
+    '("const-gchar*" "display_name")
     '("GtkWidget*" "image")
   )
 )
@@ -607,10 +608,11 @@
 (define-method add_item_with_stock_icon
   (of-object "GeditPanel")
   (c-name "gedit_panel_add_item_with_stock_icon")
-  (return-type "none")
+  (return-type "gboolean")
   (parameters
     '("GtkWidget*" "item")
-    '("const-gchar*" "name")
+    '("const-gchar*" "id")
+    '("const-gchar*" "display_name")
     '("const-gchar*" "stock_id")
   )
 )
diff --git a/plugin-loaders/python/bindings/gedit.override b/plugin-loaders/python/bindings/gedit.override
index a989b60..e431e91 100644
--- a/plugin-loaders/python/bindings/gedit.override
+++ b/plugin-loaders/python/bindings/gedit.override
@@ -276,29 +276,29 @@ override gedit_panel_add_item kwargs
 static PyObject *
 _wrap_gedit_panel_add_item(PyGObject *self, PyObject *args, PyObject *kwargs)
 {
-    static char *kwlist1[] = { "item", "name", "image", NULL };
-    static char *kwlist2[] = { "item", "name", "stock_id", NULL };
+    static char *kwlist1[] = { "item", "id", "display_name", "image", NULL };
+    static char *kwlist2[] = { "item", "id", "display_name", "stock_id", NULL };
     PyGObject *item, *image;
-    char *name = NULL;
+    char *id = NULL;
+    char *display_name = NULL;
     char *stock_id = NULL;
+    int ret = 0;
 
-    if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!sO!:GeditPanel.add_item", kwlist1, &PyGtkWidget_Type, &item, &name, &PyGtkImage_Type, &image)) {
-        gedit_panel_add_item(GEDIT_PANEL(self->obj), GTK_WIDGET(item->obj), name, GTK_WIDGET(image->obj));
-        Py_INCREF(Py_None);
-        return Py_None;
+    if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!ssO!:GeditPanel.add_item", kwlist1, &PyGtkWidget_Type, &item, &id, &display_name, &PyGtkImage_Type, &image)) {
+        ret = gedit_panel_add_item(GEDIT_PANEL(self->obj), GTK_WIDGET(item->obj), id, display_name, GTK_WIDGET(image->obj));
+        return PyBool_FromLong(ret);
     }
 
     PyErr_Clear();
 
-    if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!ss:GeditPanel.add_item", kwlist2, &PyGtkWidget_Type, &item, &name, &stock_id)) {
-        gedit_panel_add_item_with_stock_icon(GEDIT_PANEL(self->obj), GTK_WIDGET(item->obj), name, stock_id);
-        Py_INCREF(Py_None);
-        return Py_None;
+    if (PyArg_ParseTupleAndKeywords(args, kwargs, "O!sss:GeditPanel.add_item", kwlist2, &PyGtkWidget_Type, &item, &id, &display_name, &stock_id)) {
+        ret = gedit_panel_add_item_with_stock_icon(GEDIT_PANEL(self->obj), GTK_WIDGET(item->obj), id, display_name, stock_id);
+        return PyBool_FromLong(ret);
     }
 
     PyErr_Clear();
     PyErr_SetString(PyExc_TypeError, "the last arg should be either a gtk.Image or a stock_id string");
-    return NULL;
+    return PyBool_FromLong(ret);
 }
 %%
 override gedit_app_get_default_deprecated
diff --git a/plugins/externaltools/tools/__init__.py b/plugins/externaltools/tools/__init__.py
index 3ec55af..efea1e9 100644
--- a/plugins/externaltools/tools/__init__.py
+++ b/plugins/externaltools/tools/__init__.py
@@ -209,6 +209,7 @@ class ExternalToolsWindowHelper(object):
         self._output_buffer = OutputPanel(self._plugin.get_data_dir(), window)
         bottom = window.get_bottom_panel()
         bottom.add_item(self._output_buffer.panel,
+                        "GeditExternalToolsShellOutput",
                         _("Shell Output"),
                         gtk.STOCK_EXECUTE)
 
diff --git a/plugins/filebrowser/gedit-file-browser-plugin.c b/plugins/filebrowser/gedit-file-browser-plugin.c
index 2158fa5..77d5e3a 100644
--- a/plugins/filebrowser/gedit-file-browser-plugin.c
+++ b/plugins/filebrowser/gedit-file-browser-plugin.c
@@ -747,6 +747,7 @@ impl_activate (GeditPlugin *plugin,
 	gtk_widget_show(image);
 	gedit_panel_add_item (panel,
 	                      GTK_WIDGET (data->tree_widget),
+	                      "GeditFileBrowserPanel",
 	                      _("File Browser"),
 	                      image);
 	gtk_widget_show (GTK_WIDGET (data->tree_widget));
diff --git a/plugins/pythonconsole/pythonconsole/__init__.py b/plugins/pythonconsole/pythonconsole/__init__.py
index a3b480b..9fa3b1d 100644
--- a/plugins/pythonconsole/pythonconsole/__init__.py
+++ b/plugins/pythonconsole/pythonconsole/__init__.py
@@ -47,7 +47,8 @@ class PythonConsolePlugin(gedit.Plugin):
         bottom = window.get_bottom_panel()
         image = gtk.Image()
         image.set_from_icon_name(PYTHON_ICON, gtk.ICON_SIZE_MENU)
-        bottom.add_item(console, _('Python Console'), image)
+        bottom.add_item(console, "GeditPythonConsolePanel",
+                        _('Python Console'), image)
         window.set_data('PythonConsolePluginInfo', console)
 
     def deactivate(self, window):
diff --git a/plugins/taglist/gedit-taglist-plugin.c b/plugins/taglist/gedit-taglist-plugin.c
index f6e9e87..4a5e839 100644
--- a/plugins/taglist/gedit-taglist-plugin.c
+++ b/plugins/taglist/gedit-taglist-plugin.c
@@ -94,7 +94,8 @@ impl_activate (GeditPlugin *plugin,
 	g_free (data_dir);
 	
 	gedit_panel_add_item_with_stock_icon (side_panel, 
-					      taglist_panel, 
+					      taglist_panel,
+					      "GeditTagListPanel",
 					      _("Tags"), 
 					      GTK_STOCK_ADD);
 



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