[libgd/wip/taggedentry] Improve tagged entry.



commit a8333d3d3249af2e98cb4bd499866b75d47c34a4
Author: Ignacio Casal Quinteiro <ignacio casal nice-software com>
Date:   Sat Jul 13 12:08:26 2013 +0200

    Improve tagged entry.
    
     * It adds api to add a tag with a specific style.
     * It adds api to specify whether one tag has a close button or not

 libgd/gd-tagged-entry.c |   91 ++++++++++++++++++++++++++++++++++++-----------
 libgd/gd-tagged-entry.h |   10 +++++
 2 files changed, 80 insertions(+), 21 deletions(-)
---
diff --git a/libgd/gd-tagged-entry.c b/libgd/gd-tagged-entry.c
index 8baff53..f679fa3 100644
--- a/libgd/gd-tagged-entry.c
+++ b/libgd/gd-tagged-entry.c
@@ -33,6 +33,8 @@ typedef struct {
 
   gchar *id;
   gchar *label;
+  gchar *style;
+  gboolean has_close_button;
 
   GdkPixbuf *close_pixbuf;
   GtkStateFlags last_button_state;
@@ -70,15 +72,17 @@ static void gd_tagged_entry_get_text_area_size (GtkEntry *entry,
                                                 gint *height);
 static gint gd_tagged_entry_tag_get_width (GdTaggedEntryTag *tag,
                                            GdTaggedEntry *entry);
-static GtkStyleContext * gd_tagged_entry_tag_get_context (GdTaggedEntry *entry);
+static GtkStyleContext * gd_tagged_entry_tag_get_context (GdTaggedEntryTag *tag,
+                                                          GdTaggedEntry *entry);
 
 static void
-gd_tagged_entry_tag_get_margin (GdTaggedEntry *entry,
+gd_tagged_entry_tag_get_margin (GdTaggedEntryTag *tag,
+                                GdTaggedEntry *entry,
                                 GtkBorder *margin)
 {
   GtkStyleContext *context;
 
-  context = gd_tagged_entry_tag_get_context (entry);
+  context = gd_tagged_entry_tag_get_context (tag, entry);
   gtk_style_context_get_margin (context, 0, margin);
   g_object_unref (context);
 }
@@ -109,7 +113,8 @@ gd_tagged_entry_tag_ensure_close_pixbuf (GdTaggedEntryTag *tag,
 }
 
 static gint
-gd_tagged_entry_tag_panel_get_height (GdTaggedEntry *entry)
+gd_tagged_entry_tag_panel_get_height (GdTaggedEntryTag *tag,
+                                      GdTaggedEntry *entry)
 {
   GtkWidget *widget = GTK_WIDGET (entry);
   gint height, req_height;
@@ -119,7 +124,7 @@ gd_tagged_entry_tag_panel_get_height (GdTaggedEntry *entry)
 
   gtk_widget_get_allocation (widget, &allocation);
   gtk_widget_get_preferred_size (widget, &requisition, NULL);
-  gd_tagged_entry_tag_get_margin (entry, &margin);
+  gd_tagged_entry_tag_get_margin (tag, entry, &margin);
 
   /* the tag panel height is the whole entry height, minus the tag margins */
   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom 
(widget);
@@ -129,7 +134,8 @@ gd_tagged_entry_tag_panel_get_height (GdTaggedEntry *entry)
 }
 
 static void
-gd_tagged_entry_tag_panel_get_position (GdTaggedEntry *self,
+gd_tagged_entry_tag_panel_get_position (GdTaggedEntryTag *tag,
+                                        GdTaggedEntry *self,
                                         gint *x_out, 
                                         gint *y_out)
 {
@@ -144,7 +150,7 @@ gd_tagged_entry_tag_panel_get_position (GdTaggedEntry *self,
   req_height = requisition.height - gtk_widget_get_margin_top (widget) - gtk_widget_get_margin_bottom 
(widget);
 
   gd_tagged_entry_get_text_area_size (GTK_ENTRY (self), &text_x, &text_y, &text_width, &text_height);
-  gd_tagged_entry_tag_get_margin (self, &margin);
+  gd_tagged_entry_tag_get_margin (tag, self, &margin);
 
   /* allocate the panel immediately after the text area */
   if (x_out)
@@ -214,7 +220,8 @@ gd_tagged_entry_tag_get_button_state (GdTaggedEntryTag *tag,
 }
 
 static GtkStyleContext *
-gd_tagged_entry_tag_get_context (GdTaggedEntry *entry)
+gd_tagged_entry_tag_get_context (GdTaggedEntryTag *tag,
+                                 GdTaggedEntry    *entry)
 {
   GtkWidget *widget = GTK_WIDGET (entry);
   GtkWidgetPath *path;
@@ -225,7 +232,7 @@ gd_tagged_entry_tag_get_context (GdTaggedEntry *entry)
   path = gtk_widget_path_copy (gtk_widget_get_path (widget));
 
   pos = gtk_widget_path_append_type (path, GD_TYPE_TAGGED_ENTRY);
-  gtk_widget_path_iter_add_class (path, pos, "documents-entry-tag");
+  gtk_widget_path_iter_add_class (path, pos, tag->style);
 
   gtk_style_context_set_path (retval, path);
 
@@ -247,7 +254,7 @@ gd_tagged_entry_tag_get_width (GdTaggedEntryTag *tag,
   gd_tagged_entry_tag_ensure_layout (tag, entry);
   pango_layout_get_pixel_size (tag->layout, &layout_width, NULL);
 
-  context = gd_tagged_entry_tag_get_context (entry);
+  context = gd_tagged_entry_tag_get_context (tag, entry);
   state = gd_tagged_entry_tag_get_state (tag, entry);
 
   gtk_style_context_get_padding (context, state, &button_padding);
@@ -277,7 +284,7 @@ gd_tagged_entry_tag_get_size (GdTaggedEntryTag *tag,
   gint width, panel_height;
 
   width = gd_tagged_entry_tag_get_width (tag, entry);
-  panel_height = gd_tagged_entry_tag_panel_get_height (entry);
+  panel_height = gd_tagged_entry_tag_panel_get_height (tag, entry);
 
   if (width_out)
     *width_out = width;
@@ -362,7 +369,7 @@ gd_tagged_entry_tag_event_is_button (GdTaggedEntryTag *tag,
   if (!entry->priv->button_visible)
     return FALSE;
 
-  context = gd_tagged_entry_tag_get_context (entry);
+  context = gd_tagged_entry_tag_get_context (tag, entry);
   gd_tagged_entry_tag_get_relative_allocations (tag, entry, context, NULL, NULL, &button_allocation);
 
   g_object_unref (context);
@@ -386,7 +393,7 @@ gd_tagged_entry_tag_draw (GdTaggedEntryTag *tag,
   GtkStateFlags state;
   GtkAllocation background_allocation, layout_allocation, button_allocation;
 
-  context = gd_tagged_entry_tag_get_context (entry);
+  context = gd_tagged_entry_tag_get_context (tag, entry);
   gd_tagged_entry_tag_get_relative_allocations (tag, entry, context,
                                                 &background_allocation,
                                                 &layout_allocation,
@@ -412,7 +419,7 @@ gd_tagged_entry_tag_draw (GdTaggedEntryTag *tag,
 
   gtk_style_context_restore (context);
 
-  if (!entry->priv->button_visible)
+  if (!entry->priv->button_visible || !tag->has_close_button)
     goto done;
 
   gtk_style_context_add_class (context, GTK_STYLE_CLASS_BUTTON);
@@ -492,7 +499,9 @@ gd_tagged_entry_tag_realize (GdTaggedEntryTag *tag,
 
 static GdTaggedEntryTag *
 gd_tagged_entry_tag_new (const gchar *id,
-                         const gchar *label)
+                         const gchar *label,
+                         const gchar *style,
+                         gboolean     has_close_button)
 {
   GdTaggedEntryTag *tag;
 
@@ -500,6 +509,8 @@ gd_tagged_entry_tag_new (const gchar *id,
 
   tag->id = g_strdup (id);
   tag->label = g_strdup (label);
+  tag->style = g_strdup (style);
+  tag->has_close_button = has_close_button;
   tag->last_button_state = GTK_STATE_FLAG_NORMAL;
 
   return tag;
@@ -642,11 +653,11 @@ gd_tagged_entry_size_allocate (GtkWidget *widget,
 
   if (gtk_widget_get_realized (widget))
     {
-      gd_tagged_entry_tag_panel_get_position (self, &x, &y);
 
       for (l = self->priv->tags; l != NULL; l = l->next)
         {
           tag = l->data;
+          gd_tagged_entry_tag_panel_get_position (tag, self, &x, &y);
           gd_tagged_entry_tag_get_size (tag, self, &width, &height);
           gdk_window_move_resize (tag->window, x, y, width, height);
 
@@ -942,16 +953,19 @@ gd_tagged_entry_new (void)
 }
 
 gboolean
-gd_tagged_entry_add_tag (GdTaggedEntry *self,
-                         const gchar *id,
-                         const gchar *name)
+gd_tagged_entry_add_tag_styled (GdTaggedEntry *self,
+                                const gchar   *id,
+                                const gchar   *name,
+                                const gchar   *style,
+                                gboolean       has_close_button)
 {
   GdTaggedEntryTag *tag;
 
   if (gd_tagged_entry_find_tag_by_id (self, id) != NULL)
     return FALSE;
 
-  tag = gd_tagged_entry_tag_new (id, name);
+  has_close_button = has_close_button != FALSE;
+  tag = gd_tagged_entry_tag_new (id, name, style, has_close_button);
   self->priv->tags = g_list_append (self->priv->tags, tag);
 
   if (gtk_widget_get_mapped (GTK_WIDGET (self)))
@@ -966,6 +980,14 @@ gd_tagged_entry_add_tag (GdTaggedEntry *self,
 }
 
 gboolean
+gd_tagged_entry_add_tag (GdTaggedEntry *self,
+                         const gchar   *id,
+                         const gchar   *name)
+{
+  return gd_tagged_entry_add_tag_styled (self, id, name, "documents-entry-tag", TRUE);
+}
+
+gboolean
 gd_tagged_entry_remove_tag (GdTaggedEntry *self,
                             const gchar *id)
 {
@@ -1010,7 +1032,34 @@ gd_tagged_entry_set_tag_label (GdTaggedEntry *self,
         }
     }
 
-  return res;  
+  return res;
+}
+
+gboolean
+gd_tagged_entry_set_tag_has_close_button (GdTaggedEntry *self,
+                                          const gchar *tag_id,
+                                          gboolean has_close_button)
+{
+  GdTaggedEntryTag *tag;
+  gboolean res = FALSE;
+
+  has_close_button = has_close_button != FALSE;
+  tag = gd_tagged_entry_find_tag_by_id (self, tag_id);
+
+  if (tag != NULL)
+    {
+      res = TRUE;
+
+      if (tag->has_close_button != has_close_button)
+        {
+          tag->has_close_button = has_close_button;
+          g_clear_object (&tag->layout);
+
+          gtk_widget_queue_resize (GTK_WIDGET (self));
+        }
+    }
+
+  return res;
 }
 
 void
diff --git a/libgd/gd-tagged-entry.h b/libgd/gd-tagged-entry.h
index 7a29140..e13ac28 100644
--- a/libgd/gd-tagged-entry.h
+++ b/libgd/gd-tagged-entry.h
@@ -74,6 +74,12 @@ void     gd_tagged_entry_set_tag_button_visible (GdTaggedEntry *self,
                                                  gboolean       visible);
 gboolean gd_tagged_entry_get_tag_button_visible (GdTaggedEntry *self);
 
+gboolean gd_tagged_entry_add_tag_styled (GdTaggedEntry *self,
+                                         const gchar   *id,
+                                         const gchar   *name,
+                                         const gchar   *style,
+                                         gboolean       has_close_button);
+
 gboolean gd_tagged_entry_add_tag (GdTaggedEntry *entry,
                                   const gchar *id,
                                   const gchar *label);
@@ -85,6 +91,10 @@ gboolean gd_tagged_entry_set_tag_label (GdTaggedEntry *self,
                                         const gchar *tag_id,
                                         const gchar *label);
 
+gboolean gd_tagged_entry_set_tag_has_close_button (GdTaggedEntry *self,
+                                                   const gchar *tag_id,
+                                                   gboolean has_close_button);
+
 G_END_DECLS
 
 #endif /* __GD_TAGGED_ENTRY_H__ */


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