[gtk+] Add GtkEntry:max-width-chars



commit cf4a41a856b56bcab6edaaf0e0a4f17ed5853525
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Feb 10 23:25:20 2014 -0500

    Add GtkEntry:max-width-chars
    
    This property allows specifying a natural width of
    the entry that is bigger than the minimum width.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=724094

 docs/reference/gtk/gtk3-sections.txt |    2 +
 gtk/gtkentry.c                       |  111 ++++++++++++++++++++++++++++++----
 gtk/gtkentry.h                       |    6 ++
 3 files changed, 107 insertions(+), 12 deletions(-)
---
diff --git a/docs/reference/gtk/gtk3-sections.txt b/docs/reference/gtk/gtk3-sections.txt
index 892e0e6..fc03951 100644
--- a/docs/reference/gtk/gtk3-sections.txt
+++ b/docs/reference/gtk/gtk3-sections.txt
@@ -1117,10 +1117,12 @@ gtk_entry_get_activates_default
 gtk_entry_get_has_frame
 gtk_entry_get_inner_border
 gtk_entry_get_width_chars
+gtk_entry_get_max_width_chars
 gtk_entry_set_activates_default
 gtk_entry_set_has_frame
 gtk_entry_set_inner_border
 gtk_entry_set_width_chars
+gtk_entry_set_max_width_chars
 gtk_entry_get_invisible_char
 gtk_entry_set_alignment
 gtk_entry_get_alignment
diff --git a/gtk/gtkentry.c b/gtk/gtkentry.c
index e12292f..7387f64 100644
--- a/gtk/gtkentry.c
+++ b/gtk/gtkentry.c
@@ -182,6 +182,7 @@ struct _GtkEntryPrivate
   gint          start_x;
   gint          start_y;
   gint          width_chars;
+  gint          max_width_chars;
 
   gunichar      invisible_char;
 
@@ -286,6 +287,7 @@ enum {
   PROP_INVISIBLE_CHAR,
   PROP_ACTIVATES_DEFAULT,
   PROP_WIDTH_CHARS,
+  PROP_MAX_WIDTH_CHARS,
   PROP_SCROLL_OFFSET,
   PROP_TEXT,
   PROP_XALIGN,
@@ -848,6 +850,25 @@ gtk_entry_class_init (GtkEntryClass *class)
                                                      -1,
                                                      GTK_PARAM_READWRITE));
 
+  /**
+   * GtkEntry:max-width-chars:
+   *
+   * The desired maximum width of the entry, in characters.
+   * If this property is set to -1, the width will be calculated
+   * automatically.
+   *
+   * Since: 3.12
+   */ 
+  g_object_class_install_property (gobject_class,
+                                   PROP_MAX_WIDTH_CHARS,
+                                   g_param_spec_int ("max-width-chars",
+                                                     P_("Maximum width in characters"),
+                                                     P_("The desired maximum width of the entry, in 
characters"),
+                                                     -1,
+                                                     G_MAXINT,
+                                                     -1,
+                                                     GTK_PARAM_READWRITE));
+
   g_object_class_install_property (gobject_class,
                                    PROP_SCROLL_OFFSET,
                                    g_param_spec_int ("scroll-offset",
@@ -2128,6 +2149,10 @@ gtk_entry_set_property (GObject         *object,
       gtk_entry_set_width_chars (entry, g_value_get_int (value));
       break;
 
+    case PROP_MAX_WIDTH_CHARS:
+      gtk_entry_set_max_width_chars (entry, g_value_get_int (value));
+      break;
+
     case PROP_TEXT:
       gtk_entry_set_text (entry, g_value_get_string (value));
       break;
@@ -2369,6 +2394,10 @@ gtk_entry_get_property (GObject         *object,
       g_value_set_int (value, priv->width_chars);
       break;
 
+    case PROP_MAX_WIDTH_CHARS:
+      g_value_set_int (value, priv->max_width_chars);
+      break;
+
     case PROP_SCROLL_OFFSET:
       g_value_set_int (value, priv->scroll_offset);
       break;
@@ -2624,6 +2653,7 @@ gtk_entry_init (GtkEntry *entry)
   priv->visible = TRUE;
   priv->dnd_position = -1;
   priv->width_chars = -1;
+  priv->max_width_chars = -1;
   priv->is_cell_renderer = FALSE;
   priv->editing_canceled = FALSE;
   priv->has_frame = TRUE;
@@ -3332,7 +3362,10 @@ gtk_entry_get_preferred_width (GtkWidget *widget,
   PangoContext *context;
   gint icon_widths = 0;
   gint icon_width, i;
-  gint width;
+  gint min, nat;
+  gint char_width;
+  gint digit_width;
+  gint char_pixels;
 
   context = gtk_widget_get_pango_context (widget);
 
@@ -3342,16 +3375,21 @@ gtk_entry_get_preferred_width (GtkWidget *widget,
 
   _gtk_entry_get_borders (entry, &borders);
 
+  char_width = pango_font_metrics_get_approximate_char_width (metrics);
+  digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
+  char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
+
   if (priv->width_chars < 0)
-    width = MIN_ENTRY_WIDTH + borders.left + borders.right;
+    min = MIN_ENTRY_WIDTH + borders.left + borders.right;
   else
-    {
-      gint char_width = pango_font_metrics_get_approximate_char_width (metrics);
-      gint digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
-      gint char_pixels = (MAX (char_width, digit_width) + PANGO_SCALE - 1) / PANGO_SCALE;
+    min = char_pixels * priv->width_chars + borders.left + borders.right;
 
-      width = char_pixels * priv->width_chars + borders.left + borders.right;
-    }
+  if (priv->max_width_chars < 0)
+    nat = MIN_ENTRY_WIDTH + borders.left + borders.right;
+  else
+    nat = char_pixels * priv->max_width_chars + borders.left + borders.right;
+
+  nat = MAX (min, nat);
 
   for (i = 0; i < MAX_ICONS; i++)
     {
@@ -3360,13 +3398,16 @@ gtk_entry_get_preferred_width (GtkWidget *widget,
         icon_widths += icon_width;
     }
 
-  if (icon_widths > width)
-    width += icon_widths;
+  if (icon_widths > min)
+    {
+      min += icon_widths;
+      nat += icon_width;
+    }
 
   pango_font_metrics_unref (metrics);
 
-  *minimum = width;
-  *natural = width;
+  *minimum = min;
+  *natural = nat;
 }
 
 static void
@@ -7892,6 +7933,52 @@ gtk_entry_get_width_chars (GtkEntry *entry)
 }
 
 /**
+ * gtk_entry_set_max_width_chars:
+ * @entry: a #GtkEntry
+ * @n_chars: the new desired maximum width, in characters
+ *
+ * Sets the desired maximum width in characters of @entry.
+ *
+ * Since: 3.12
+ */
+void
+gtk_entry_set_max_width_chars (GtkEntry *entry,
+                               gint      n_chars)
+{
+  GtkEntryPrivate *priv;
+
+  g_return_if_fail (GTK_IS_ENTRY (entry));
+
+  priv = entry->priv;
+
+  if (priv->max_width_chars != n_chars)
+    {
+      priv->max_width_chars = n_chars;
+      g_object_notify (G_OBJECT (entry), "max-width-chars");
+      gtk_widget_queue_resize (GTK_WIDGET (entry));
+    }
+}
+
+/**
+ * gtk_entry_get_max_width_chars:
+ * @entry: a #GtkEntry
+ *
+ * Retrieves the desired maximum width of @entry, in characters.
+ * See gtk_entry_set_max_width_chars().
+ *
+ * Return value: the maximum width of the entry, in characters
+ *
+ * Since: 3.12
+ */
+gint
+gtk_entry_get_max_width_chars (GtkEntry *entry)
+{
+  g_return_val_if_fail (GTK_IS_ENTRY (entry), 0);
+
+  return entry->priv->max_width_chars;
+}
+
+/**
  * gtk_entry_set_has_frame:
  * @entry: a #GtkEntry
  * @setting: new value
diff --git a/gtk/gtkentry.h b/gtk/gtkentry.h
index 5bb79d2..ce3c021 100644
--- a/gtk/gtkentry.h
+++ b/gtk/gtkentry.h
@@ -197,6 +197,12 @@ void       gtk_entry_set_width_chars            (GtkEntry      *entry,
 GDK_AVAILABLE_IN_ALL
 gint       gtk_entry_get_width_chars            (GtkEntry      *entry);
 
+GDK_AVAILABLE_IN_3_12
+void       gtk_entry_set_max_width_chars        (GtkEntry      *entry,
+                                                 gint           n_chars);
+GDK_AVAILABLE_IN_3_12
+gint       gtk_entry_get_max_width_chars        (GtkEntry      *entry);
+
 /* Somewhat more convenient than the GtkEditable generic functions
  */
 GDK_AVAILABLE_IN_ALL


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