#include "gtkentrycompletion.h"
#include "gtkentryprivate.h"
#include "gtkcelllayout.h"
-#include "gtkalias.h"
#include "gtkintl.h"
#include "gtkcellrenderertext.h"
#include "gtksignal.h"
#include "gtkmarshalers.h"
+#include "gtkprivate.h"
+#include "gtkalias.h"
+
#include <string.h>
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,
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;
* 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),
* 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),
* 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),
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",
0,
G_MAXINT,
1,
- G_PARAM_READWRITE));
+ GTK_PARAM_READWRITE));
/**
* GtkEntryCompletion:text-column:
*
-1,
G_MAXINT,
-1,
- G_PARAM_READWRITE));
+ GTK_PARAM_READWRITE));
/**
* GtkEntryCompletion:inline-completion:
P_("Inline completion"),
P_("Whether the common prefix should be inserted automatically"),
FALSE,
- G_PARAM_READWRITE));
+ GTK_PARAM_READWRITE));
/**
* GtkEntryCompletion:popup-completion:
*
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));
}
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;
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;
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;
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 */
* 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,
{
completion->priv->minimum_key_length = length;
- g_object_notify (G_OBJECT (completion), "minimum_key_length");
+ g_object_notify (G_OBJECT (completion), "minimum-key-length");
}
}
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
cell,
"text", column);
- g_object_notify (G_OBJECT (completion), "text_column");
+ g_object_notify (G_OBJECT (completion), "text-column");
}
/**
gint matches, items, height, x_border, y_border;
GdkScreen *screen;
gint monitor_num;
+ gint vertical_separator;
GdkRectangle monitor;
GtkRequisition popup_req;
GtkRequisition entry_req;
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
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 */
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;
{
GtkTreeViewColumn *column;
GList *renderers;
+ GtkWidget *toplevel;
if (GTK_WIDGET_MAPPED (completion->priv->popup_window))
return;
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);
_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);
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);
&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);
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);
{
completion->priv->inline_completion = inline_completion;
- g_object_notify (G_OBJECT (completion), "inline_completion");
+ g_object_notify (G_OBJECT (completion), "inline-completion");
}
}
{
completion->priv->popup_completion = popup_completion;
- g_object_notify (G_OBJECT (completion), "popup_completion");
+ g_object_notify (G_OBJECT (completion), "popup-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"