searchentry: Drop the Private struct

This commit is contained in:
Matthias Clasen 2020-03-28 10:28:22 -04:00
parent 49427119ab
commit 28eb1fce04

View File

@ -101,6 +101,16 @@ typedef struct _GtkSearchEntryClass GtkSearchEntryClass;
struct _GtkSearchEntry struct _GtkSearchEntry
{ {
GtkWidget parent; GtkWidget parent;
GtkWidget *capture_widget;
GtkEventController *capture_widget_controller;
GtkWidget *entry;
GtkWidget *icon;
guint delayed_changed_id;
gboolean content_changed;
gboolean search_stopped;
}; };
struct _GtkSearchEntryClass struct _GtkSearchEntryClass
@ -114,22 +124,9 @@ struct _GtkSearchEntryClass
void (* stop_search) (GtkSearchEntry *entry); void (* stop_search) (GtkSearchEntry *entry);
}; };
typedef struct {
GtkWidget *capture_widget;
GtkEventController *capture_widget_controller;
GtkWidget *entry;
GtkWidget *icon;
guint delayed_changed_id;
gboolean content_changed;
gboolean search_stopped;
} GtkSearchEntryPrivate;
static void gtk_search_entry_editable_init (GtkEditableInterface *iface); static void gtk_search_entry_editable_init (GtkEditableInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GtkSearchEntry, gtk_search_entry, GTK_TYPE_WIDGET, G_DEFINE_TYPE_WITH_CODE (GtkSearchEntry, gtk_search_entry, GTK_TYPE_WIDGET,
G_ADD_PRIVATE (GtkSearchEntry)
G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE,
gtk_search_entry_editable_init)) gtk_search_entry_editable_init))
@ -139,24 +136,21 @@ G_DEFINE_TYPE_WITH_CODE (GtkSearchEntry, gtk_search_entry, GTK_TYPE_WIDGET,
static void static void
text_changed (GtkSearchEntry *entry) text_changed (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); entry->content_changed = TRUE;
priv->content_changed = TRUE;
} }
static void static void
gtk_search_entry_finalize (GObject *object) gtk_search_entry_finalize (GObject *object)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
gtk_editable_finish_delegate (GTK_EDITABLE (entry)); gtk_editable_finish_delegate (GTK_EDITABLE (entry));
g_clear_pointer (&priv->entry, gtk_widget_unparent); g_clear_pointer (&entry->entry, gtk_widget_unparent);
g_clear_pointer (&priv->icon, gtk_widget_unparent); g_clear_pointer (&entry->icon, gtk_widget_unparent);
if (priv->delayed_changed_id > 0) if (entry->delayed_changed_id > 0)
g_source_remove (priv->delayed_changed_id); g_source_remove (entry->delayed_changed_id);
gtk_search_entry_set_key_capture_widget (GTK_SEARCH_ENTRY (object), NULL); gtk_search_entry_set_key_capture_widget (GTK_SEARCH_ENTRY (object), NULL);
@ -166,9 +160,7 @@ gtk_search_entry_finalize (GObject *object)
static void static void
gtk_search_entry_stop_search (GtkSearchEntry *entry) gtk_search_entry_stop_search (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); entry->search_stopped = TRUE;
priv->search_stopped = TRUE;
} }
static void static void
@ -178,7 +170,6 @@ gtk_search_entry_set_property (GObject *object,
GParamSpec *pspec) GParamSpec *pspec)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
if (gtk_editable_delegate_set_property (object, prop_id, value, pspec)) if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
return; return;
@ -186,13 +177,13 @@ gtk_search_entry_set_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_PLACEHOLDER_TEXT: case PROP_PLACEHOLDER_TEXT:
gtk_text_set_placeholder_text (GTK_TEXT (priv->entry), g_value_get_string (value)); gtk_text_set_placeholder_text (GTK_TEXT (entry->entry), g_value_get_string (value));
break; break;
case PROP_ACTIVATES_DEFAULT: case PROP_ACTIVATES_DEFAULT:
if (gtk_text_get_activates_default (GTK_TEXT (priv->entry)) != g_value_get_boolean (value)) if (gtk_text_get_activates_default (GTK_TEXT (entry->entry)) != g_value_get_boolean (value))
{ {
gtk_text_set_activates_default (GTK_TEXT (priv->entry), g_value_get_boolean (value)); gtk_text_set_activates_default (GTK_TEXT (entry->entry), g_value_get_boolean (value));
g_object_notify_by_pspec (object, pspec); g_object_notify_by_pspec (object, pspec);
} }
break; break;
@ -209,7 +200,6 @@ gtk_search_entry_get_property (GObject *object,
GParamSpec *pspec) GParamSpec *pspec)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (object);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
if (gtk_editable_delegate_get_property (object, prop_id, value, pspec)) if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
return; return;
@ -217,11 +207,11 @@ gtk_search_entry_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_PLACEHOLDER_TEXT: case PROP_PLACEHOLDER_TEXT:
g_value_set_string (value, gtk_text_get_placeholder_text (GTK_TEXT (priv->entry))); g_value_set_string (value, gtk_text_get_placeholder_text (GTK_TEXT (entry->entry)));
break; break;
case PROP_ACTIVATES_DEFAULT: case PROP_ACTIVATES_DEFAULT:
g_value_set_boolean (value, gtk_text_get_activates_default (GTK_TEXT (priv->entry))); g_value_set_boolean (value, gtk_text_get_activates_default (GTK_TEXT (entry->entry)));
break; break;
default: default:
@ -244,9 +234,8 @@ static gboolean
gtk_search_entry_grab_focus (GtkWidget *widget) gtk_search_entry_grab_focus (GtkWidget *widget)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
return gtk_text_grab_focus_without_selecting (GTK_TEXT (priv->entry)); return gtk_text_grab_focus_without_selecting (GTK_TEXT (entry->entry));
} }
static gboolean static gboolean
@ -254,9 +243,8 @@ gtk_search_entry_mnemonic_activate (GtkWidget *widget,
gboolean group_cycling) gboolean group_cycling)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
gtk_widget_grab_focus (priv->entry); gtk_widget_grab_focus (entry->entry);
return TRUE; return TRUE;
} }
@ -420,10 +408,7 @@ gtk_search_entry_class_init (GtkSearchEntryClass *klass)
static GtkEditable * static GtkEditable *
gtk_search_entry_get_delegate (GtkEditable *editable) gtk_search_entry_get_delegate (GtkEditable *editable)
{ {
GtkSearchEntry *entry = GTK_SEARCH_ENTRY (editable); return GTK_EDITABLE (GTK_SEARCH_ENTRY (editable)->entry);
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
return GTK_EDITABLE (priv->entry);
} }
static void static void
@ -439,19 +424,16 @@ gtk_search_entry_icon_release (GtkGestureClick *press,
double y, double y,
GtkSearchEntry *entry) GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); gtk_editable_set_text (GTK_EDITABLE (entry->entry), "");
gtk_editable_set_text (GTK_EDITABLE (priv->entry), "");
} }
static gboolean static gboolean
gtk_search_entry_changed_timeout_cb (gpointer user_data) gtk_search_entry_changed_timeout_cb (gpointer user_data)
{ {
GtkSearchEntry *entry = user_data; GtkSearchEntry *entry = user_data;
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
g_signal_emit (entry, signals[SEARCH_CHANGED], 0); g_signal_emit (entry, signals[SEARCH_CHANGED], 0);
priv->delayed_changed_id = 0; entry->delayed_changed_id = 0;
return G_SOURCE_REMOVE; return G_SOURCE_REMOVE;
} }
@ -459,40 +441,37 @@ gtk_search_entry_changed_timeout_cb (gpointer user_data)
static void static void
reset_timeout (GtkSearchEntry *entry) reset_timeout (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); if (entry->delayed_changed_id > 0)
g_source_remove (entry->delayed_changed_id);
if (priv->delayed_changed_id > 0) entry->delayed_changed_id = g_timeout_add (DELAYED_TIMEOUT_ID,
g_source_remove (priv->delayed_changed_id);
priv->delayed_changed_id = g_timeout_add (DELAYED_TIMEOUT_ID,
gtk_search_entry_changed_timeout_cb, gtk_search_entry_changed_timeout_cb,
entry); entry);
g_source_set_name_by_id (priv->delayed_changed_id, "[gtk] gtk_search_entry_changed_timeout_cb"); g_source_set_name_by_id (entry->delayed_changed_id, "[gtk] gtk_search_entry_changed_timeout_cb");
} }
static void static void
gtk_search_entry_changed (GtkEditable *editable, gtk_search_entry_changed (GtkEditable *editable,
GtkSearchEntry *entry) GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
const char *str; const char *str;
/* Update the icons first */ /* Update the icons first */
str = gtk_editable_get_text (GTK_EDITABLE (priv->entry)); str = gtk_editable_get_text (GTK_EDITABLE (entry->entry));
if (str == NULL || *str == '\0') if (str == NULL || *str == '\0')
{ {
gtk_widget_set_child_visible (priv->icon, FALSE); gtk_widget_set_child_visible (entry->icon, FALSE);
if (priv->delayed_changed_id > 0) if (entry->delayed_changed_id > 0)
{ {
g_source_remove (priv->delayed_changed_id); g_source_remove (entry->delayed_changed_id);
priv->delayed_changed_id = 0; entry->delayed_changed_id = 0;
} }
g_signal_emit (entry, signals[SEARCH_CHANGED], 0); g_signal_emit (entry, signals[SEARCH_CHANGED], 0);
} }
else else
{ {
gtk_widget_set_child_visible (priv->icon, TRUE); gtk_widget_set_child_visible (entry->icon, TRUE);
/* Queue up the timeout */ /* Queue up the timeout */
reset_timeout (entry); reset_timeout (entry);
@ -520,27 +499,26 @@ activate_cb (GtkText *text,
static void static void
gtk_search_entry_init (GtkSearchEntry *entry) gtk_search_entry_init (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
GtkGesture *press; GtkGesture *press;
priv->entry = gtk_text_new (); entry->entry = gtk_text_new ();
gtk_widget_set_parent (priv->entry, GTK_WIDGET (entry)); gtk_widget_set_parent (entry->entry, GTK_WIDGET (entry));
gtk_widget_set_hexpand (priv->entry, TRUE); gtk_widget_set_hexpand (entry->entry, TRUE);
gtk_editable_init_delegate (GTK_EDITABLE (entry)); gtk_editable_init_delegate (GTK_EDITABLE (entry));
g_signal_connect_swapped (priv->entry, "changed", G_CALLBACK (text_changed), entry); g_signal_connect_swapped (entry->entry, "changed", G_CALLBACK (text_changed), entry);
g_signal_connect_after (priv->entry, "changed", G_CALLBACK (gtk_search_entry_changed), entry); g_signal_connect_after (entry->entry, "changed", G_CALLBACK (gtk_search_entry_changed), entry);
g_signal_connect_swapped (priv->entry, "preedit-changed", G_CALLBACK (text_changed), entry); g_signal_connect_swapped (entry->entry, "preedit-changed", G_CALLBACK (text_changed), entry);
g_signal_connect (priv->entry, "notify", G_CALLBACK (notify_cb), entry); g_signal_connect (entry->entry, "notify", G_CALLBACK (notify_cb), entry);
g_signal_connect (priv->entry, "activate", G_CALLBACK (activate_cb), entry); g_signal_connect (entry->entry, "activate", G_CALLBACK (activate_cb), entry);
priv->icon = gtk_image_new_from_icon_name ("edit-clear-all-symbolic"); entry->icon = gtk_image_new_from_icon_name ("edit-clear-all-symbolic");
gtk_widget_set_tooltip_text (priv->icon, _("Clear entry")); gtk_widget_set_tooltip_text (entry->icon, _("Clear entry"));
gtk_widget_set_parent (priv->icon, GTK_WIDGET (entry)); gtk_widget_set_parent (entry->icon, GTK_WIDGET (entry));
gtk_widget_set_child_visible (priv->icon, FALSE); gtk_widget_set_child_visible (entry->icon, FALSE);
press = gtk_gesture_click_new (); press = gtk_gesture_click_new ();
g_signal_connect (press, "released", G_CALLBACK (gtk_search_entry_icon_release), entry); g_signal_connect (press, "released", G_CALLBACK (gtk_search_entry_icon_release), entry);
gtk_widget_add_controller (priv->icon, GTK_EVENT_CONTROLLER (press)); gtk_widget_add_controller (entry->icon, GTK_EVENT_CONTROLLER (press));
gtk_widget_add_css_class (GTK_WIDGET (entry), I_("search")); gtk_widget_add_css_class (GTK_WIDGET (entry), I_("search"));
} }
@ -586,9 +564,9 @@ capture_widget_key_handled (GtkEventControllerKey *controller,
guint keyval, guint keyval,
guint keycode, guint keycode,
GdkModifierType state, GdkModifierType state,
GtkWidget *entry) GtkWidget *widget)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (GTK_SEARCH_ENTRY (entry)); GtkSearchEntry *entry = GTK_SEARCH_ENTRY (widget);
gboolean handled, was_empty; gboolean handled, was_empty;
if (gtk_search_entry_is_keynav (keyval, state) || if (gtk_search_entry_is_keynav (keyval, state) ||
@ -596,13 +574,13 @@ capture_widget_key_handled (GtkEventControllerKey *controller,
keyval == GDK_KEY_Menu) keyval == GDK_KEY_Menu)
return FALSE; return FALSE;
priv->content_changed = FALSE; entry->content_changed = FALSE;
priv->search_stopped = FALSE; entry->search_stopped = FALSE;
was_empty = (gtk_text_get_text_length (GTK_TEXT (priv->entry)) == 0); was_empty = (gtk_text_get_text_length (GTK_TEXT (entry->entry)) == 0);
handled = gtk_event_controller_key_forward (controller, priv->entry); handled = gtk_event_controller_key_forward (controller, entry->entry);
if (handled && priv->content_changed && !priv->search_stopped) if (handled && entry->content_changed && !entry->search_stopped)
{ {
if (was_empty) if (was_empty)
g_signal_emit (entry, signals[SEARCH_STARTED], 0); g_signal_emit (entry, signals[SEARCH_STARTED], 0);
@ -631,34 +609,32 @@ void
gtk_search_entry_set_key_capture_widget (GtkSearchEntry *entry, gtk_search_entry_set_key_capture_widget (GtkSearchEntry *entry,
GtkWidget *widget) GtkWidget *widget)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
g_return_if_fail (GTK_IS_SEARCH_ENTRY (entry)); g_return_if_fail (GTK_IS_SEARCH_ENTRY (entry));
g_return_if_fail (!widget || GTK_IS_WIDGET (widget)); g_return_if_fail (!widget || GTK_IS_WIDGET (widget));
if (priv->capture_widget) if (entry->capture_widget)
{ {
gtk_widget_remove_controller (priv->capture_widget, gtk_widget_remove_controller (entry->capture_widget,
priv->capture_widget_controller); entry->capture_widget_controller);
g_object_remove_weak_pointer (G_OBJECT (priv->capture_widget), g_object_remove_weak_pointer (G_OBJECT (entry->capture_widget),
(gpointer *) &priv->capture_widget); (gpointer *) &entry->capture_widget);
} }
priv->capture_widget = widget; entry->capture_widget = widget;
if (widget) if (widget)
{ {
g_object_add_weak_pointer (G_OBJECT (priv->capture_widget), g_object_add_weak_pointer (G_OBJECT (entry->capture_widget),
(gpointer *) &priv->capture_widget); (gpointer *) &entry->capture_widget);
priv->capture_widget_controller = gtk_event_controller_key_new (); entry->capture_widget_controller = gtk_event_controller_key_new ();
gtk_event_controller_set_propagation_phase (priv->capture_widget_controller, gtk_event_controller_set_propagation_phase (entry->capture_widget_controller,
GTK_PHASE_CAPTURE); GTK_PHASE_CAPTURE);
g_signal_connect (priv->capture_widget_controller, "key-pressed", g_signal_connect (entry->capture_widget_controller, "key-pressed",
G_CALLBACK (capture_widget_key_handled), entry); G_CALLBACK (capture_widget_key_handled), entry);
g_signal_connect (priv->capture_widget_controller, "key-released", g_signal_connect (entry->capture_widget_controller, "key-released",
G_CALLBACK (capture_widget_key_handled), entry); G_CALLBACK (capture_widget_key_handled), entry);
gtk_widget_add_controller (widget, priv->capture_widget_controller); gtk_widget_add_controller (widget, entry->capture_widget_controller);
} }
} }
@ -673,25 +649,19 @@ gtk_search_entry_set_key_capture_widget (GtkSearchEntry *entry,
GtkWidget * GtkWidget *
gtk_search_entry_get_key_capture_widget (GtkSearchEntry *entry) gtk_search_entry_get_key_capture_widget (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry);
g_return_val_if_fail (GTK_IS_SEARCH_ENTRY (entry), NULL); g_return_val_if_fail (GTK_IS_SEARCH_ENTRY (entry), NULL);
return priv->capture_widget; return entry->capture_widget;
} }
GtkEventController * GtkEventController *
gtk_search_entry_get_key_controller (GtkSearchEntry *entry) gtk_search_entry_get_key_controller (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); return gtk_text_get_key_controller (GTK_TEXT (entry->entry));
return gtk_text_get_key_controller (GTK_TEXT (priv->entry));
} }
GtkText * GtkText *
gtk_search_entry_get_text_widget (GtkSearchEntry *entry) gtk_search_entry_get_text_widget (GtkSearchEntry *entry)
{ {
GtkSearchEntryPrivate *priv = gtk_search_entry_get_instance_private (entry); return GTK_TEXT (entry->entry);
return GTK_TEXT (priv->entry);
} }