]> Pileus Git - ~andy/gtk/blobdiff - gtk/gtkentrycompletion.c
Deprecate widget flag: GTK_WIDGET_VISIBLE
[~andy/gtk] / gtk / gtkentrycompletion.c
index 8c4268a794eff8b128601a03670ce2d13f8fb18d..c997e60a9f8e85fd9bd45018172198a56c81279c 100644 (file)
@@ -17,7 +17,7 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include <config.h>
+#include "config.h"
 #include "gtkentrycompletion.h"
 #include "gtkentryprivate.h"
 #include "gtkcelllayout.h"
@@ -32,7 +32,6 @@
 #include "gtkwindow.h"
 #include "gtkentry.h"
 #include "gtkmain.h"
-#include "gtksignal.h"
 #include "gtkmarshalers.h"
 
 #include "gtkprivate.h"
@@ -99,6 +98,7 @@ static void     gtk_entry_completion_clear_attributes    (GtkCellLayout
 static void     gtk_entry_completion_reorder             (GtkCellLayout         *cell_layout,
                                                           GtkCellRenderer       *cell,
                                                           gint                   position);
+static GList *  gtk_entry_completion_get_cells           (GtkCellLayout         *cell_layout);
 
 static gboolean gtk_entry_completion_visible_func        (GtkTreeModel       *model,
                                                           GtkTreeIter        *iter,
@@ -193,7 +193,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.6
    */ 
   entry_completion_signals[INSERT_PREFIX] =
-    g_signal_new (I_("insert_prefix"),
+    g_signal_new (I_("insert-prefix"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, insert_prefix),
@@ -218,7 +218,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.4
    */ 
   entry_completion_signals[MATCH_SELECTED] =
-    g_signal_new (I_("match_selected"),
+    g_signal_new (I_("match-selected"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, match_selected),
@@ -244,7 +244,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    */ 
 
   entry_completion_signals[CURSOR_ON_MATCH] =
-    g_signal_new (I_("cursor_on_match"),
+    g_signal_new (I_("cursor-on-match"),
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GtkEntryCompletionClass, cursor_on_match),
@@ -264,12 +264,12 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * Since: 2.4
    */
   entry_completion_signals[ACTION_ACTIVATED] =
-    g_signal_new (I_("action_activated"),
+    g_signal_new (I_("action-activated"),
                   G_TYPE_FROM_CLASS (klass),
                   G_SIGNAL_RUN_LAST,
                   G_STRUCT_OFFSET (GtkEntryCompletionClass, action_activated),
                   NULL, NULL,
-                  _gtk_marshal_NONE__INT,
+                  _gtk_marshal_VOID__INT,
                   G_TYPE_NONE, 1,
                   G_TYPE_INT);
 
@@ -293,6 +293,7 @@ gtk_entry_completion_class_init (GtkEntryCompletionClass *klass)
    * GtkEntryCompletion:text-column:
    *
    * The column of the model containing the strings.
+   * Note that the strings must be UTF-8.
    *
    * Since: 2.6
    */
@@ -410,6 +411,7 @@ gtk_entry_completion_cell_layout_init (GtkCellLayoutIface *iface)
   iface->set_cell_data_func = gtk_entry_completion_set_cell_data_func;
   iface->clear_attributes = gtk_entry_completion_clear_attributes;
   iface->reorder = gtk_entry_completion_reorder;
+  iface->get_cells = gtk_entry_completion_get_cells;
 }
 
 static void
@@ -436,13 +438,13 @@ gtk_entry_completion_init (GtkEntryCompletion *completion)
   priv->filter_model = NULL;
 
   priv->tree_view = gtk_tree_view_new ();
-  g_signal_connect (priv->tree_view, "button_press_event",
+  g_signal_connect (priv->tree_view, "button-press-event",
                     G_CALLBACK (gtk_entry_completion_list_button_press),
                     completion);
-  g_signal_connect (priv->tree_view, "enter_notify_event",
+  g_signal_connect (priv->tree_view, "enter-notify-event",
                    G_CALLBACK (gtk_entry_completion_list_enter_notify),
                    completion);
-  g_signal_connect (priv->tree_view, "motion_notify_event",
+  g_signal_connect (priv->tree_view, "motion-notify-event",
                    G_CALLBACK (gtk_entry_completion_list_motion_notify),
                    completion);
 
@@ -476,13 +478,13 @@ gtk_entry_completion_init (GtkEntryCompletion *completion)
   priv->action_view =
     gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->actions));
   g_object_ref_sink (priv->action_view);
-  g_signal_connect (priv->action_view, "button_press_event",
+  g_signal_connect (priv->action_view, "button-press-event",
                     G_CALLBACK (gtk_entry_completion_action_button_press),
                     completion);
-  g_signal_connect (priv->action_view, "enter_notify_event",
+  g_signal_connect (priv->action_view, "enter-notify-event",
                    G_CALLBACK (gtk_entry_completion_list_enter_notify),
                    completion);
-  g_signal_connect (priv->action_view, "motion_notify_event",
+  g_signal_connect (priv->action_view, "motion-notify-event",
                    G_CALLBACK (gtk_entry_completion_list_motion_notify),
                    completion);
   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (priv->action_view), FALSE);
@@ -503,13 +505,15 @@ gtk_entry_completion_init (GtkEntryCompletion *completion)
   /* pack it all */
   priv->popup_window = gtk_window_new (GTK_WINDOW_POPUP);
   gtk_window_set_resizable (GTK_WINDOW (priv->popup_window), FALSE);
-  g_signal_connect (priv->popup_window, "key_press_event",
+  gtk_window_set_type_hint (GTK_WINDOW(priv->popup_window),
+                            GDK_WINDOW_TYPE_HINT_COMBO);
+  g_signal_connect (priv->popup_window, "key-press-event",
                     G_CALLBACK (gtk_entry_completion_popup_key_event),
                     completion);
-  g_signal_connect (priv->popup_window, "key_release_event",
+  g_signal_connect (priv->popup_window, "key-release-event",
                     G_CALLBACK (gtk_entry_completion_popup_key_event),
                     completion);
-  g_signal_connect (priv->popup_window, "button_press_event",
+  g_signal_connect (priv->popup_window, "button-press-event",
                     G_CALLBACK (gtk_entry_completion_popup_button_press),
                     completion);
 
@@ -669,8 +673,6 @@ gtk_entry_completion_pack_start (GtkCellLayout   *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_tree_view_column_pack_start (priv->column, cell, expand);
@@ -683,8 +685,6 @@ gtk_entry_completion_pack_end (GtkCellLayout   *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_tree_view_column_pack_end (priv->column, cell, expand);
@@ -695,8 +695,6 @@ gtk_entry_completion_clear (GtkCellLayout *cell_layout)
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_tree_view_column_clear (priv->column);
@@ -710,8 +708,6 @@ gtk_entry_completion_add_attribute (GtkCellLayout   *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_tree_view_column_add_attribute (priv->column, cell, attribute, column);
@@ -726,8 +722,6 @@ gtk_entry_completion_set_cell_data_func (GtkCellLayout          *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->column),
@@ -740,8 +734,6 @@ gtk_entry_completion_clear_attributes (GtkCellLayout   *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_tree_view_column_clear_attributes (priv->column, cell);
@@ -754,13 +746,21 @@ gtk_entry_completion_reorder (GtkCellLayout   *cell_layout,
 {
   GtkEntryCompletionPrivate *priv;
 
-  g_return_if_fail (GTK_IS_ENTRY_COMPLETION (cell_layout));
-
   priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
 
   gtk_cell_layout_reorder (GTK_CELL_LAYOUT (priv->column), cell, position);
 }
 
+static GList *
+gtk_entry_completion_get_cells (GtkCellLayout *cell_layout)
+{
+  GtkEntryCompletionPrivate *priv;
+
+  priv = GTK_ENTRY_COMPLETION_GET_PRIVATE (cell_layout);
+
+  return gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (priv->column));
+}
+
 /* all those callbacks */
 static gboolean
 gtk_entry_completion_default_completion_func (GtkEntryCompletion *completion,
@@ -1013,7 +1013,7 @@ gtk_entry_completion_get_entry (GtkEntryCompletion *completion)
 /**
  * gtk_entry_completion_set_model:
  * @completion: A #GtkEntryCompletion.
- * @model: The #GtkTreeModel.
+ * @model: (allow-none): The #GtkTreeModel.
  *
  * Sets the model for a #GtkEntryCompletion. If @completion already has
  * a model set, it will remove it before setting the new model.
@@ -1051,7 +1051,7 @@ gtk_entry_completion_set_model (GtkEntryCompletion *completion,
 
   g_object_notify (G_OBJECT (completion), "model");
 
-  if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
+  if (gtk_widget_get_visible (completion->priv->popup_window))
     _gtk_entry_completion_resize_popup (completion);
 }
 
@@ -1180,7 +1180,7 @@ gtk_entry_completion_complete (GtkEntryCompletion *completion)
 
   gtk_tree_model_filter_refilter (completion->priv->filter_model);
 
-  if (GTK_WIDGET_VISIBLE (completion->priv->popup_window))
+  if (gtk_widget_get_visible (completion->priv->popup_window))
     _gtk_entry_completion_resize_popup (completion);
 }
 
@@ -1365,7 +1365,7 @@ gboolean
 _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
 {
   gint x, y;
-  gint matches, items, height, x_border, y_border;
+  gint matches, actions, items, height, x_border, y_border;
   GdkScreen *screen;
   gint monitor_num;
   gint vertical_separator;
@@ -1375,6 +1375,8 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   GtkTreePath *path;
   gboolean above;
   gint width;
+  GtkTreeViewColumn *action_column;
+  gint action_height;
 
   if (!completion->priv->entry->window)
     return FALSE;
@@ -1383,11 +1385,13 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   _gtk_entry_get_borders (GTK_ENTRY (completion->priv->entry), &x_border, &y_border);
 
   matches = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->filter_model), NULL);
-
-  items = MIN (matches, 15);
+  actions = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
+  action_column  = gtk_tree_view_get_column (GTK_TREE_VIEW (completion->priv->action_view), 0);
 
   gtk_tree_view_column_cell_get_size (completion->priv->column, NULL,
                                       NULL, NULL, NULL, &height);
+  gtk_tree_view_column_cell_get_size (action_column, NULL,
+                                      NULL, NULL, NULL, &action_height);
 
   gtk_widget_style_get (GTK_WIDGET (completion->priv->tree_view),
                         "vertical-separator", &vertical_separator,
@@ -1397,16 +1401,23 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   
   gtk_widget_realize (completion->priv->tree_view);
 
-  if (items <= 0)
-    gtk_widget_hide (completion->priv->scrolled_window);
-  else
-    gtk_widget_show (completion->priv->scrolled_window);
-
   screen = gtk_widget_get_screen (GTK_WIDGET (completion->priv->entry));
   monitor_num = gdk_screen_get_monitor_at_window (screen, 
                                                  GTK_WIDGET (completion->priv->entry)->window);
   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
 
+  
+
+  if (y > monitor.height / 2)
+    items = MIN (matches, (((monitor.y + y) - (actions * action_height)) / height) - 1);
+  else
+    items = MIN (matches, (((monitor.height - y) - (actions * action_height)) / height) - 1);
+
+  if (items <= 0)
+    gtk_widget_hide (completion->priv->scrolled_window);
+  else
+    gtk_widget_show (completion->priv->scrolled_window);
+
   if (completion->priv->popup_set_width)
     width = MIN (completion->priv->entry->allocation.width, monitor.width) - 2 * x_border;
   else
@@ -1415,12 +1426,7 @@ _gtk_entry_completion_resize_popup (GtkEntryCompletion *completion)
   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 */
-  completion->priv->current_selected = -1;
-
-  items = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (completion->priv->actions), NULL);
-
-  if (items)
+  if (actions)
     {
       gtk_widget_show (completion->priv->action_view);
       gtk_widget_set_size_request (completion->priv->action_view, width, -1);
@@ -1474,13 +1480,13 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
   if (!GTK_WIDGET_MAPPED (completion->priv->entry))
     return;
 
-  if (!GTK_WIDGET_HAS_FOCUS (completion->priv->entry))
+  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);
+  renderers = gtk_cell_layout_get_cells (GTK_CELL_LAYOUT (column));
   gtk_widget_ensure_style (completion->priv->tree_view);
   g_object_set (GTK_CELL_RENDERER (renderers->data), "cell-background-gdk",
                 &completion->priv->tree_view->style->bg[GTK_STATE_NORMAL],
@@ -1489,6 +1495,9 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
 
   gtk_widget_show_all (completion->priv->vbox);
 
+  /* default on no match */
+  completion->priv->current_selected = -1;
+
   _gtk_entry_completion_resize_popup (completion);
 
   toplevel = gtk_widget_get_toplevel (completion->priv->entry);
@@ -1501,7 +1510,9 @@ _gtk_entry_completion_popup (GtkEntryCompletion *completion)
 
   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_window_set_screen (GTK_WINDOW (completion->priv->popup_window),
+                         gtk_widget_get_screen (completion->priv->entry));
 
   gtk_widget_show (completion->priv->popup_window);
     
@@ -1635,9 +1646,6 @@ gtk_entry_completion_real_insert_prefix (GtkEntryCompletion *completion,
       key = gtk_entry_get_text (GTK_ENTRY (completion->priv->entry));
       key_len = g_utf8_strlen (key, -1);
 
-      if (completion->priv->completion_prefix == NULL)
-        completion->priv->completion_prefix = g_strdup (key);
-
       if (prefix_len > key_len)
        {
          gint pos = prefix_len;
@@ -1688,9 +1696,7 @@ gtk_entry_completion_insert_completion_text (GtkEntryCompletion *completion,
   if (priv->insert_text_id > 0)
     g_signal_handler_block (priv->entry, priv->insert_text_id);
 
-  if (completion->priv->completion_prefix == NULL)
-    completion->priv->completion_prefix = g_strdup (gtk_entry_get_text (GTK_ENTRY (priv->entry)));
-    gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
+  gtk_entry_set_text (GTK_ENTRY (priv->entry), text);
 
   len = strlen (priv->completion_prefix);
   gtk_editable_select_region (GTK_EDITABLE (priv->entry), len, -1);