spin button: Implement and use editable interface

This commit is contained in:
Matthias Clasen 2019-02-14 00:54:07 -05:00
parent 32795963c0
commit b9c8dd64b3

View File

@ -35,6 +35,7 @@
#include "gtkbutton.h" #include "gtkbutton.h"
#include "gtkcssstylepropertyprivate.h" #include "gtkcssstylepropertyprivate.h"
#include "gtkeditable.h" #include "gtkeditable.h"
#include "gtkeditableprivate.h"
#include "gtkentry.h" #include "gtkentry.h"
#include "gtkeventcontrollerkey.h" #include "gtkeventcontrollerkey.h"
#include "gtkeventcontrollermotion.h" #include "gtkeventcontrollermotion.h"
@ -221,9 +222,6 @@ enum {
PROP_WRAP, PROP_WRAP,
PROP_UPDATE_POLICY, PROP_UPDATE_POLICY,
PROP_VALUE, PROP_VALUE,
PROP_WIDTH_CHARS,
PROP_MAX_WIDTH_CHARS,
PROP_TEXT,
NUM_SPINBUTTON_PROPS, NUM_SPINBUTTON_PROPS,
PROP_ORIENTATION, PROP_ORIENTATION,
}; };
@ -385,33 +383,9 @@ gtk_spin_button_class_init (GtkSpinButtonClass *class)
-G_MAXDOUBLE, G_MAXDOUBLE, 0.0, -G_MAXDOUBLE, G_MAXDOUBLE, 0.0,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY); GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
spinbutton_props[PROP_WIDTH_CHARS] =
g_param_spec_int ("width-chars",
P_("Width in chars"),
P_("Number of characters to leave space for in the entry"),
-1, G_MAXINT,
0,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
spinbutton_props[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,
0,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
spinbutton_props[PROP_TEXT] =
g_param_spec_string ("text",
P_("Text"),
P_("The contents of the entry"),
"0", /* Default value of the default adjustment */
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (gobject_class, NUM_SPINBUTTON_PROPS, spinbutton_props); g_object_class_install_properties (gobject_class, NUM_SPINBUTTON_PROPS, spinbutton_props);
g_object_class_override_property (gobject_class, gtk_editable_install_properties (gobject_class);
PROP_ORIENTATION, g_object_class_override_property (gobject_class, PROP_ORIENTATION, "orientation");
"orientation");
/** /**
* GtkSpinButton::input: * GtkSpinButton::input:
@ -552,6 +526,7 @@ gtk_spin_button_class_init (GtkSpinButtonClass *class)
static void static void
gtk_spin_button_editable_init (GtkEditableInterface *iface) gtk_spin_button_editable_init (GtkEditableInterface *iface)
{ {
gtk_editable_delegate_iface_init (iface);
iface->insert_text = gtk_spin_button_insert_text; iface->insert_text = gtk_spin_button_insert_text;
} }
@ -564,6 +539,9 @@ gtk_spin_button_set_property (GObject *object,
GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object); GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button);
if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
return;
switch (prop_id) switch (prop_id)
{ {
GtkAdjustment *adjustment; GtkAdjustment *adjustment;
@ -602,15 +580,6 @@ gtk_spin_button_set_property (GObject *object,
case PROP_ORIENTATION: case PROP_ORIENTATION:
gtk_spin_button_set_orientation (spin_button, g_value_get_enum (value)); gtk_spin_button_set_orientation (spin_button, g_value_get_enum (value));
break; break;
case PROP_WIDTH_CHARS:
gtk_spin_button_set_width_chars (spin_button, g_value_get_int (value));
break;
case PROP_MAX_WIDTH_CHARS:
gtk_spin_button_set_max_width_chars (spin_button, g_value_get_int (value));
break;
case PROP_TEXT:
gtk_spin_button_set_text (spin_button, g_value_get_string (value));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -626,6 +595,9 @@ gtk_spin_button_get_property (GObject *object,
GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object); GtkSpinButton *spin_button = GTK_SPIN_BUTTON (object);
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button);
if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
return;
switch (prop_id) switch (prop_id)
{ {
case PROP_ADJUSTMENT: case PROP_ADJUSTMENT:
@ -655,15 +627,6 @@ gtk_spin_button_get_property (GObject *object,
case PROP_ORIENTATION: case PROP_ORIENTATION:
g_value_set_enum (value, priv->orientation); g_value_set_enum (value, priv->orientation);
break; break;
case PROP_WIDTH_CHARS:
g_value_set_int (value, gtk_spin_button_get_width_chars (spin_button));
break;
case PROP_MAX_WIDTH_CHARS:
g_value_set_int (value, gtk_spin_button_get_max_width_chars (spin_button));
break;
case PROP_TEXT:
g_value_set_string (value, gtk_spin_button_get_text (spin_button));
break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break; break;
@ -867,14 +830,14 @@ gtk_spin_button_init (GtkSpinButton *spin_button)
gtk_widget_set_parent (priv->box, GTK_WIDGET (spin_button)); gtk_widget_set_parent (priv->box, GTK_WIDGET (spin_button));
priv->entry = gtk_entry_new (); priv->entry = gtk_entry_new ();
gtk_entry_set_width_chars (GTK_ENTRY (priv->entry), 0); gtk_editable_set_delegate (GTK_EDITABLE (spin_button), GTK_EDITABLE (priv->entry));
gtk_entry_set_max_width_chars (GTK_ENTRY (priv->entry), 0); gtk_editable_set_width_chars (GTK_EDITABLE (priv->entry), 0);
gtk_editable_set_max_width_chars (GTK_EDITABLE (priv->entry), 0);
gtk_widget_set_hexpand (priv->entry, TRUE); gtk_widget_set_hexpand (priv->entry, TRUE);
gtk_widget_set_vexpand (priv->entry, TRUE); gtk_widget_set_vexpand (priv->entry, TRUE);
g_signal_connect (priv->entry, "activate", G_CALLBACK (gtk_spin_button_activate), spin_button); g_signal_connect (priv->entry, "activate", G_CALLBACK (gtk_spin_button_activate), spin_button);
gtk_container_add (GTK_CONTAINER (priv->box), priv->entry); gtk_container_add (GTK_CONTAINER (priv->box), priv->entry);
priv->down_button = gtk_button_new (); priv->down_button = gtk_button_new ();
gtk_container_add (GTK_CONTAINER (priv->down_button), gtk_image_new_from_icon_name ("value-decrease-symbolic")); gtk_container_add (GTK_CONTAINER (priv->down_button), gtk_image_new_from_icon_name ("value-decrease-symbolic"));
gtk_style_context_add_class (gtk_widget_get_style_context (priv->down_button), "image-button"); gtk_style_context_add_class (gtk_widget_get_style_context (priv->down_button), "image-button");
@ -960,6 +923,7 @@ gtk_spin_button_realize (GtkWidget *widget)
GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget); GtkSpinButton *spin_button = GTK_SPIN_BUTTON (widget);
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button);
gboolean return_val; gboolean return_val;
const char *text;
GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget); GTK_WIDGET_CLASS (gtk_spin_button_parent_class)->realize (widget);
@ -969,8 +933,8 @@ gtk_spin_button_realize (GtkWidget *widget)
/* If output wasn't processed explicitly by the method connected to the /* If output wasn't processed explicitly by the method connected to the
* 'output' signal; and if we don't have any explicit 'text' set initially, * 'output' signal; and if we don't have any explicit 'text' set initially,
* fallback to the default output. */ * fallback to the default output. */
if (!return_val && text = gtk_editable_get_text (GTK_EDITABLE (priv->entry));
(priv->numeric || gtk_entry_get_text (GTK_ENTRY (priv->entry)) == NULL)) if (!return_val && (priv->numeric || text == NULL || *text == '\0'))
gtk_spin_button_default_output (spin_button); gtk_spin_button_default_output (spin_button);
} }
@ -1026,7 +990,7 @@ gtk_spin_button_set_orientation (GtkSpinButton *spin,
GtkOrientation orientation) GtkOrientation orientation)
{ {
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin);
GtkEntry *entry = GTK_ENTRY (priv->entry); GtkEditable *editable = GTK_EDITABLE (priv->entry);
if (priv->orientation == orientation) if (priv->orientation == orientation)
return; return;
@ -1036,11 +1000,11 @@ gtk_spin_button_set_orientation (GtkSpinButton *spin,
/* change alignment if it's the default */ /* change alignment if it's the default */
if (priv->orientation == GTK_ORIENTATION_VERTICAL && if (priv->orientation == GTK_ORIENTATION_VERTICAL &&
gtk_entry_get_alignment (entry) == 0.0) gtk_editable_get_alignment (editable) == 0.0)
gtk_entry_set_alignment (entry, 0.5); gtk_editable_set_alignment (editable, 0.5);
else if (priv->orientation == GTK_ORIENTATION_HORIZONTAL && else if (priv->orientation == GTK_ORIENTATION_HORIZONTAL &&
gtk_entry_get_alignment (entry) == 0.5) gtk_editable_get_alignment (editable) == 0.5)
gtk_entry_set_alignment (entry, 0.0); gtk_editable_set_alignment (editable, 0.0);
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL) if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{ {
@ -1348,11 +1312,6 @@ gtk_spin_button_insert_text (GtkEditable *editable,
{ {
GtkSpinButton *spin = GTK_SPIN_BUTTON (editable); GtkSpinButton *spin = GTK_SPIN_BUTTON (editable);
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin);
GtkEntry *entry = GTK_ENTRY (priv->entry);
GtkEditableInterface *parent_editable_iface;
parent_editable_iface = g_type_interface_peek (gtk_spin_button_parent_class,
GTK_TYPE_EDITABLE);
if (priv->numeric) if (priv->numeric)
{ {
@ -1365,8 +1324,8 @@ gtk_spin_button_insert_text (GtkEditable *editable,
gint entry_length; gint entry_length;
const gchar *entry_text; const gchar *entry_text;
entry_length = gtk_entry_get_text_length (entry); entry_text = gtk_editable_get_text (GTK_EDITABLE (priv->entry));
entry_text = gtk_entry_get_text (entry); entry_length = g_utf8_strlen (entry_text, -1);
lc = localeconv (); lc = localeconv ();
@ -1444,8 +1403,8 @@ gtk_spin_button_insert_text (GtkEditable *editable,
} }
} }
parent_editable_iface->insert_text (editable, new_text, gtk_editable_insert_text (GTK_EDITABLE (priv->entry),
new_text_length, position); new_text, new_text_length, position);
} }
static void static void
@ -1505,8 +1464,9 @@ gtk_spin_button_default_input (GtkSpinButton *spin_button,
{ {
GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button); GtkSpinButtonPrivate *priv = gtk_spin_button_get_instance_private (spin_button);
gchar *err = NULL; gchar *err = NULL;
const char *text = gtk_editable_get_text (GTK_EDITABLE (priv->entry));
*new_val = g_strtod (gtk_entry_get_text (GTK_ENTRY (priv->entry)), &err); *new_val = g_strtod (text, &err);
if (*err) if (*err)
return GTK_INPUT_ERROR; return GTK_INPUT_ERROR;
else else
@ -1520,8 +1480,8 @@ gtk_spin_button_default_output (GtkSpinButton *spin_button)
gchar *buf = gtk_spin_button_format_for_value (spin_button, gchar *buf = gtk_spin_button_format_for_value (spin_button,
gtk_adjustment_get_value (priv->adjustment)); gtk_adjustment_get_value (priv->adjustment));
if (strcmp (buf, gtk_entry_get_text (GTK_ENTRY (priv->entry)))) if (strcmp (buf, gtk_editable_get_text (GTK_EDITABLE (priv->entry))))
gtk_entry_set_text (GTK_ENTRY (priv->entry), buf); gtk_editable_set_text (GTK_EDITABLE (priv->entry), buf);
g_free (buf); g_free (buf);
} }
@ -2260,7 +2220,7 @@ gtk_spin_button_get_text (GtkSpinButton *spin_button)
g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL); g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), NULL);
return gtk_entry_get_text (GTK_ENTRY (priv->entry)); return gtk_editable_get_text (GTK_EDITABLE (priv->entry));
} }
/** /**
@ -2279,9 +2239,7 @@ gtk_spin_button_set_text (GtkSpinButton *spin_button,
g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button)); g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
gtk_entry_set_text (GTK_ENTRY (priv->entry), text); gtk_editable_set_text (GTK_EDITABLE (priv->entry), text);
g_object_notify_by_pspec (G_OBJECT (spin_button), spinbutton_props[PROP_TEXT]);
} }
/** /**
@ -2303,7 +2261,7 @@ gtk_spin_button_get_max_width_chars (GtkSpinButton *spin_button)
g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), -1); g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), -1);
return gtk_entry_get_max_width_chars (GTK_ENTRY (priv->entry)); return gtk_editable_get_max_width_chars (GTK_EDITABLE (priv->entry));
} }
/** /**
@ -2323,11 +2281,7 @@ gtk_spin_button_set_max_width_chars (GtkSpinButton *spin_button,
g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button)); g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
if (max_width_chars != gtk_entry_get_max_width_chars (GTK_ENTRY (priv->entry))) gtk_editable_set_max_width_chars (GTK_EDITABLE (priv->entry), max_width_chars);
{
gtk_entry_set_max_width_chars (GTK_ENTRY (priv->entry), max_width_chars);
g_object_notify_by_pspec (G_OBJECT (spin_button), spinbutton_props[PROP_MAX_WIDTH_CHARS]);
}
} }
/** /**
@ -2347,7 +2301,7 @@ gtk_spin_button_get_width_chars (GtkSpinButton *spin_button)
g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), -1); g_return_val_if_fail (GTK_IS_SPIN_BUTTON (spin_button), -1);
return gtk_entry_get_width_chars (GTK_ENTRY (priv->entry)); return gtk_editable_get_width_chars (GTK_EDITABLE (priv->entry));
} }
/** /**
@ -2366,9 +2320,5 @@ gtk_spin_button_set_width_chars (GtkSpinButton *spin_button,
g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button)); g_return_if_fail (GTK_IS_SPIN_BUTTON (spin_button));
if (width_chars != gtk_entry_get_width_chars (GTK_ENTRY (priv->entry))) gtk_editable_set_width_chars (GTK_EDITABLE (priv->entry), width_chars);
{
gtk_entry_set_width_chars (GTK_ENTRY (priv->entry), width_chars);
g_object_notify_by_pspec (G_OBJECT (spin_button), spinbutton_props[PROP_WIDTH_CHARS]);
}
} }