]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkentrycompletion.c
Fix typos in the docs.
[~andy/gtk] / gtk / gtkentrycompletion.c
index e001a3db3ed2c071a7ad7f6e5fbc4979bda13b1c..57ae8ec74a768b3294e8f49fe45d77f9845e6f13 100644 (file)
@@ -21,7 +21,6 @@
 #include "gtkentrycompletion.h"
 #include "gtkentryprivate.h"
 #include "gtkcelllayout.h"
-#include "gtkalias.h"
 
 #include "gtkintl.h"
 #include "gtkcellrenderertext.h"
@@ -36,6 +35,9 @@
 #include "gtksignal.h"
 #include "gtkmarshalers.h"
 
+#include "gtkprivate.h"
+#include "gtkalias.h"
+
 #include <string.h>
 
 
@@ -56,14 +58,14 @@ enum
   PROP_MINIMUM_KEY_LENGTH,
   PROP_TEXT_COLUMN,
   PROP_INLINE_COMPLETION,
-  PROP_POPUP_COMPLETION
+  PROP_POPUP_COMPLETION,
+  PROP_POPUP_SET_WIDTH,
+  PROP_POPUP_SINGLE_MATCH
 };
 
 #define GTK_ENTRY_COMPLETION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_ENTRY_COMPLETION, GtkEntryCompletionPrivate))
 
-static void     gtk_entry_completion_class_init          (GtkEntryCompletionClass *klass);
 static void     gtk_entry_completion_cell_layout_init    (GtkCellLayoutIface      *iface);
-static void     gtk_entry_completion_init                (GtkEntryCompletion      *completion);
 static void     gtk_entry_completion_set_property        (GObject                 *object,
                                                           guint                    prop_id,
                                                           const GValue            *value,
@@ -135,55 +137,17 @@ static gboolean gtk_entry_completion_match_selected      (GtkEntryCompletion *co
 static gboolean gtk_entry_completion_real_insert_prefix  (GtkEntryCompletion *completion,
                                                          const gchar        *prefix);
 
-static GObjectClass *parent_class = NULL;
 static guint entry_completion_signals[LAST_SIGNAL] = { 0 };
 
-
-GType
-gtk_entry_completion_get_type (void)
-{
-  static GType entry_completion_type = 0;
-
-  if (!entry_completion_type)
-    {
-      static const GTypeInfo entry_completion_info =
-      {
-        sizeof (GtkEntryCompletionClass),
-        NULL,
-        NULL,
-        (GClassInitFunc) gtk_entry_completion_class_init,
-        NULL,
-        NULL,
-        sizeof (GtkEntryCompletion),
-        0,
-        (GInstanceInitFunc) gtk_entry_completion_init
-      };
-
-      static const GInterfaceInfo cell_layout_info =
-      {
-        (GInterfaceInitFunc) gtk_entry_completion_cell_layout_init,
-        NULL,
-        NULL
-      };
-
-      entry_completion_type =
-        g_type_register_static (G_TYPE_OBJECT, "GtkEntryCompletion",
-                                &entry_completion_info, 0);
-
-      g_type_add_interface_static (entry_completion_type,
-                                   GTK_TYPE_CELL_LAYOUT,
-                                   &cell_layout_info);
-    }
-
-  return entry_completion_type;
-}
+G_DEFINE_TYPE_WITH_CODE (GtkEntryCompletion, gtk_entry_completion, G_TYPE_OBJECT,
+                        G_IMPLEMENT_INTERFACE (GTK_TYPE_CELL_LAYOUT,
+                                               gtk_entry_completion_cell_layout_init))
 
 static void
 gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
 {
   GObjectClass *object_class;
 
-  parent_class = g_type_class_peek_parent (klass);
   object_class = (GObjectClass *)klass;
 
   object_class->set_property = gtk_entry_completion_set_property;
@@ -212,7 +176,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.6
    */ 
   entry_completion_signals[INSERT_PREFIX] =
-    g_signal_new ("insert_prefix",
+    g_signal_new (I_("insert_prefix"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
@@ -237,7 +201,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.4
    */ 
   entry_completion_signals[MATCH_SELECTED] =
-    g_signal_new ("match_selected",
+    g_signal_new (I_("match_selected"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
@@ -257,7 +221,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.4
    */
   entry_completion_signals[ACTION_ACTIVATED] =
-    g_signal_new ("action_activated",
+    g_signal_new (I_("action_activated"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
@@ -272,7 +236,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
                                                         P_("Completion Model"),
                                                         P_("The model to find matches in"),
                                                         GTK_TYPE_TREE_MODEL,
-                                                        G_PARAM_READWRITE));
+                                                        GTK_PARAM_READWRITE));
   g_object_class_install_property (object_class,
                                    PROP_MINIMUM_KEY_LENGTH,
                                    g_param_spec_int ("minimum-key-length",
@@ -281,7 +245,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
                                                      0,
                                                      G_MAXINT,
                                                      1,
-                                                     G_PARAM_READWRITE));
+                                                     GTK_PARAM_READWRITE));
   /**
    * GtkEntryCompletion:text-column:
    *
@@ -297,7 +261,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
                                                      -1,
                                                      G_MAXINT,
                                                      -1,
-                                                     G_PARAM_READWRITE));
+                                                     GTK_PARAM_READWRITE));
 
   /**
    * GtkEntryCompletion:inline-completion:
@@ -315,7 +279,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
                                                         P_("Inline completion"),
                                                         P_("Whether the common prefix should be inserted automatically"),
                                                         FALSE,
-                                                        G_PARAM_READWRITE));
+                                                        GTK_PARAM_READWRITE));
   /**
    * GtkEntryCompletion:popup-completion:
    * 
@@ -330,7 +294,42 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
                                                         P_("Popup completion"),
                                                         P_("Whether the completions should be shown in a popup window"),
                                                         TRUE,
-                                                        G_PARAM_READWRITE));
+                                                        GTK_PARAM_READWRITE));
+
+  /**
+   * GtkEntryCompletion:popup-set-width:
+   * 
+   * Determines whether the completions popup window will be
+   * resized to the width of the entry.
+   *
+   * Since: 2.8
+   */
+  g_object_class_install_property (object_class,
+                                  PROP_POPUP_SET_WIDTH,
+                                  g_param_spec_boolean ("popup-set-width",
+                                                        P_("Popup set width"),
+                                                        P_("If TRUE, the popup window will have the same size as the entry"),
+                                                        TRUE,
+                                                        GTK_PARAM_READWRITE));
+
+  /**
+   * GtkEntryCompletion:popup-single-match:
+   * 
+   * Determines whether the completions popup window will shown
+   * for a single possible completion. You probably want to set
+   * this to %FALSE if you are using 
+   * <link linkend="GtkEntryCompletion--inline-completion">inline 
+   * completion</link>.
+   *
+   * Since: 2.8
+   */
+  g_object_class_install_property (object_class,
+                                  PROP_POPUP_SINGLE_MATCH,
+                                  g_param_spec_boolean ("popup-single-match",
+                                                        P_("Popup single match"),
+                                                        P_("If TRUE, the popup window will appear for a single match."),
+                                                        TRUE,
+                                                        GTK_PARAM_READWRITE));
 
   g_type_class_add_private (object_class, sizeof (GtkEntryCompletionPrivate));
 }
@@ -363,6 +362,8 @@ gtk_entry_completion_init (GtkEntryCompletion *completion)
   priv->has_completion = FALSE;
   priv->inline_completion = FALSE;
   priv->popup_completion = TRUE;
+  priv->popup_set_width = TRUE;
+  priv->popup_single_match = TRUE;
 
   /* completions */
   priv->filter_model = NULL;
@@ -492,6 +493,14 @@ gtk_entry_completion_set_property (GObject      *object,
        priv->popup_completion = g_value_get_boolean (value);
         break;
 
+      case PROP_POPUP_SET_WIDTH:
+       priv->popup_set_width = g_value_get_boolean (value);
+        break;
+
+      case PROP_POPUP_SINGLE_MATCH:
+       priv->popup_single_match = g_value_get_boolean (value);
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
         break;
@@ -529,6 +538,14 @@ gtk_entry_completion_get_property (GObject    *object,
         g_value_set_boolean (value, gtk_entry_completion_get_popup_completion (completion));
         break;
 
+      case PROP_POPUP_SET_WIDTH:
+        g_value_set_boolean (value, gtk_entry_completion_get_popup_set_width (completion));
+        break;
+
+      case PROP_POPUP_SINGLE_MATCH:
+        g_value_set_boolean (value, gtk_entry_completion_get_popup_single_match (completion));
+        break;
+
       default:
         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
         break;
@@ -555,7 +572,10 @@ gtk_entry_completion_finalize (GObject *object)
   if (completion->priv->popup_window)
     gtk_widget_destroy (completion->priv->popup_window);
 
-  G_OBJECT_CLASS (parent_class)->finalize (object);
+  if (completion->priv->match_notify)
+    (* completion->priv->match_notify) (completion->priv->match_data);
+
+  G_OBJECT_CLASS (gtk_entry_completion_parent_class)->finalize (object);
 }
 
 /* implement cell layout interface */
@@ -983,7 +1003,7 @@ gtk_entry_completion_get_model (GtkEntryCompletion *completion)
  * is used to determine if a row should or should not be in the completion
  * list.
  *
- * Since: 2.4.
+ * Since: 2.4
  */
 void
 gtk_entry_completion_set_match_func (GtkEntryCompletion          *completion,
@@ -1024,7 +1044,7 @@ gtk_entry_completion_set_minimum_key_length (GtkEntryCompletion *completion,
     {
       completion->priv->minimum_key_length = length;
      
-      g_object_notify (G_OBJECT (completion), "minimum_key_length");
+      g_object_notify (G_OBJECT (completion), "minimum-key-length");
     }
 }
 
@@ -1075,6 +1095,9 @@ gtk_entry_completion_complete (GtkEntryCompletion *completion)
   g_free (tmp);
 
   gtk_tree_model_filter_refilter (completion->priv->filter_model);
+
+  if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
+    _gtk_entry_completion_resize_popup (completion);
 }
 
 static void
@@ -1207,7 +1230,7 @@ gtk_entry_completion_set_text_column (GtkEntryCompletion *completion,
                                  cell,
                                  "text", column);
 
-  g_object_notify (G_OBJECT (completion), "text_column");
+  g_object_notify (G_OBJECT (completion), "text-column");
 }
 
 /**
@@ -1261,6 +1284,7 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   gint matches, items, height, x_border, y_border;
   GdkScreen *screen;
   gint monitor_num;
+  gint vertical_separator;
   GdkRectangle monitor;
   GtkRequisition popup_req;
   GtkRequisition entry_req;
@@ -1281,6 +1305,14 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
                                       NULL, NULL, NULL, &height);
 
+  gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
+                        "vertical-separator", &vertical_separator,
+                        NULL);
+
+  height += vertical_separator;
+  
+  gtk_widget_realize (completion->priv->tree_view);
+
   if (items <= 0)
     gtk_widget_hide (completion->priv->scrolled_window);
   else
@@ -1291,7 +1323,12 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
                                                  GTK_WIDGET (completion->priv->entry)->window);
   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
 
-  width = MIN (completion->priv->entry->allocation.width, monitor.width) - 2 * x_border;
+  if (completion->priv->popup_set_width)
+    width = MIN (completion->priv->entry->allocation.width, monitor.width) - 2 * x_border;
+  else
+    width = -1;
+
+  gtk_tree_view_columns_autosize (GTK_TREE_VIEW (completion->priv->tree_view));
   gtk_widget_set_size_request (completion->priv->tree_view, width, items * height);
 
   /* default on no match */
@@ -1315,7 +1352,8 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   else if (x + popup_req.width > monitor.x + monitor.width)
     x = monitor.x + monitor.width - popup_req.width;
   
-  if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height)
+  if (y + entry_req.height + popup_req.height <= monitor.y + monitor.height ||
+      y - monitor.y < (monitor.y + monitor.height) - (y + entry_req.height))
     {
       y += entry_req.height;
       above = FALSE;
@@ -1344,6 +1382,7 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
 {
   GtkTreeViewColumn *column;
   GList *renderers;
+  GtkWidget *toplevel;
 
   if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
     return;
@@ -1351,12 +1390,15 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
   if (!GTK_WIDGET_MAPPED (completion->priv->entry))
     return;
 
+  if (!GTK_WIDGET_HAS_FOCUS (completion->priv->entry))
+    return;
+
   completion->priv->ignore_enter = TRUE;
     
   column = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
   renderers = gtk_tree_view_column_get_cell_renderers (column);
   gtk_widget_ensure_style (completion->priv->tree_view);
-  g_object_set (GTK_CELL_RENDERER (renderers->data), "cell_background_gdk",
+  g_object_set (GTK_CELL_RENDERER (renderers->data), "cell-background-gdk",
                 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
                 NULL);
   g_list_free (renderers);
@@ -1365,6 +1407,18 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
 
   _gtk_entry_completion_resize_popup (completion);
 
+  toplevel = gtk_widget_get_toplevel (completion->priv->entry);
+  if (GTK_IS_WINDOW (toplevel))
+    gtk_window_group_add_window (gtk_window_get_group (GTK_WINDOW (toplevel)), 
+                                GTK_WINDOW (completion->priv->popup_window));
+
+  /* prevent the first row being focused */
+  gtk_widget_grab_focus (completion->priv->tree_view);
+
+  gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->tree_view)));
+  gtk_tree_selection_unselect_all (gtk_tree_view_get_selection (GTK_TREE_VIEW (completion->priv->action_view)));
+  
+
   gtk_widget_show (completion->priv->popup_window);
     
   gtk_grab_add (completion->priv->popup_window);
@@ -1397,7 +1451,7 @@ gtk_entry_completion_match_selected (GtkEntryCompletion *completion,
   gchar *str = NULL;
 
   gtk_tree_model_get (model, iter, completion->priv->text_column, &str, -1);
-  gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str);
+  gtk_entry_set_text (GTK_ENTRY (completion->priv->entry), str ? str : "");
   
   /* move cursor to the end */
   gtk_editable_set_position (GTK_EDITABLE (completion->priv->entry), -1);
@@ -1432,7 +1486,7 @@ gtk_entry_completion_compute_prefix (GtkEntryCompletion *completion)
                          &iter, completion->priv->text_column, &text,
                          -1);
 
-      if (g_str_has_prefix (text, key))
+      if (text && g_str_has_prefix (text, key))
        {
          if (!prefix)
            prefix = g_strdup (text);
@@ -1480,7 +1534,7 @@ gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
          gint pos = prefix_len;
 
          gtk_editable_insert_text (GTK_EDITABLE (completion->priv->entry),
-                                   prefix + key_len, -1, &pos);
+                                   prefix + strlen (key), -1, &pos);
          gtk_editable_select_region (GTK_EDITABLE (completion->priv->entry),
                                      key_len, prefix_len);
 
@@ -1540,7 +1594,7 @@ gtk_entry_completion_set_inline_completion (GtkEntryCompletion *completion,
     {
       completion->priv->inline_completion = inline_completion;
 
-      g_object_notify (G_OBJECT (completion), "inline_completion");
+      g_object_notify (G_OBJECT (completion), "inline-completion");
     }
 }
 
@@ -1585,7 +1639,7 @@ gtk_entry_completion_set_popup_completion (GtkEntryCompletion *completion,
     {
       completion->priv->popup_completion = popup_completion;
 
-      g_object_notify (G_OBJECT (completion), "popup_completion");
+      g_object_notify (G_OBJECT (completion), "popup-completion");
     }
 }
 
@@ -1608,5 +1662,102 @@ gtk_entry_completion_get_popup_completion (GtkEntryCompletion *completion)
   return completion->priv->popup_completion;
 }
 
+/**
+ * gtk_entry_completion_set_popup_set_width:
+ * @completion: a #GtkEntryCompletion
+ * @popup_set_width: %TRUE to make the width of the popup the same as the entry
+ *
+ * Sets whether the completion popup window will be resized to be the same
+ * width as the entry.
+ *
+ * Since: 2.8
+ */
+void 
+gtk_entry_completion_set_popup_set_width (GtkEntryCompletion *completion,
+                                         gboolean            popup_set_width)
+{
+  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
+  
+  popup_set_width = popup_set_width != FALSE;
+
+  if (completion->priv->popup_set_width != popup_set_width)
+    {
+      completion->priv->popup_set_width = popup_set_width;
+
+      g_object_notify (G_OBJECT (completion), "popup-set-width");
+    }
+}
+
+/**
+ * gtk_entry_completion_get_popup_set_width:
+ * @completion: a #GtkEntryCompletion
+ * 
+ * Returns whether the  completion popup window will be resized to the 
+ * width of the entry.
+ * 
+ * Return value: %TRUE if the popup window will be resized to the width of 
+ *   the entry
+ * 
+ * Since: 2.8
+ **/
+gboolean
+gtk_entry_completion_get_popup_set_width (GtkEntryCompletion *completion)
+{
+  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
+  
+  return completion->priv->popup_set_width;
+}
+
+
+/**
+ * gtk_entry_completion_set_popup_single_match:
+ * @completion: a #GtkEntryCompletion
+ * @popup_single_match: %TRUE if the popup should appear even for a single
+ *   match
+ *
+ * Sets whether the completion popup window will appear even if there is
+ * only a single match. You may want to set this to %FALSE if you
+ * are using <link linkend="GtkEntryCompletion--inline-completion">inline
+ * completion</link>.
+ *
+ * Since: 2.8
+ */
+void 
+gtk_entry_completion_set_popup_single_match (GtkEntryCompletion *completion,
+                                            gboolean            popup_single_match)
+{
+  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (completion));
+  
+  popup_single_match = popup_single_match != FALSE;
+
+  if (completion->priv->popup_single_match != popup_single_match)
+    {
+      completion->priv->popup_single_match = popup_single_match;
+
+      g_object_notify (G_OBJECT (completion), "popup-single-match");
+    }
+}
+
+/**
+ * gtk_entry_completion_get_popup_single_match:
+ * @completion: a #GtkEntryCompletion
+ * 
+ * Returns whether the completion popup window will appear even if there is
+ * only a single match. 
+ * 
+ * Return value: %TRUE if the popup window will appear regardless of the
+ *    number of matches.
+ * 
+ * Since: 2.8
+ **/
+gboolean
+gtk_entry_completion_get_popup_single_match (GtkEntryCompletion *completion)
+{
+  g_return_val_if_fail (GTK_IS_ENTRY_COMPLETION (completion), TRUE);
+  
+  return completion->priv->popup_single_match;
+}
+
+
 #define __GTK_ENTRY_COMPLETION_C__
 #include "gtkaliasdef.c"