Commit ed08c9a5 authored by Adrien Plazas's avatar Adrien Plazas Committed by Guido Gunther

dialer-button: Replace digit and letters by symbols

Unify the digit and the letters of a dialer button as its symbols. This
allows to make the code simpler by limiting the number of special cases.
digit. This also handles Unicode characters.
parent 10db9d8a
...@@ -14,14 +14,14 @@ ...@@ -14,14 +14,14 @@
* @Title: HdyDialerButton * @Title: HdyDialerButton
* *
* The #HdyDialerButton widget is a single button on an #HdyDialer. It * The #HdyDialerButton widget is a single button on an #HdyDialer. It
* can represent a single digit (0-9) plus an arbitrary number of * can represent a single symbol (typically a digit) plus an arbitrary
* letters that are displayed below the number. * number of symbols that are displayed below it.
*/ */
enum { enum {
PROP_0, PROP_0,
PROP_DIGIT, PROP_DIGIT,
PROP_LETTERS, PROP_SYMBOLS,
PROP_LAST_PROP, PROP_LAST_PROP,
}; };
static GParamSpec *props[PROP_LAST_PROP]; static GParamSpec *props[PROP_LAST_PROP];
...@@ -29,8 +29,7 @@ static GParamSpec *props[PROP_LAST_PROP]; ...@@ -29,8 +29,7 @@ static GParamSpec *props[PROP_LAST_PROP];
typedef struct typedef struct
{ {
GtkLabel *label, *secondary_label; GtkLabel *label, *secondary_label;
gint digit; gchar *symbols;
gchar *letters;
} HdyDialerButtonPrivate; } HdyDialerButtonPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (HdyDialerButton, hdy_dialer_button, GTK_TYPE_BUTTON) G_DEFINE_TYPE_WITH_PRIVATE (HdyDialerButton, hdy_dialer_button, GTK_TYPE_BUTTON)
...@@ -39,18 +38,23 @@ static void ...@@ -39,18 +38,23 @@ static void
format_label(HdyDialerButton *self) format_label(HdyDialerButton *self)
{ {
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self); HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
GString *str; gchar *symbols = priv->symbols != NULL ? priv->symbols : "";
g_autofree gchar *text; g_autofree gchar *text = NULL;
gchar *secondary_text = NULL;
str = g_string_new(NULL);
if (priv->digit >= 0) { if (*symbols != '\0') {
g_string_sprintf (str, "%d", priv->digit); secondary_text = g_utf8_find_next_char (symbols, NULL);
/* Allocate memory for the first character and '\0'. */
text = g_malloc0 (secondary_text - symbols + 1);
g_utf8_strncpy (text, symbols, 1);
}
else {
text = g_malloc0 (sizeof (gchar));
secondary_text = "";
} }
text = g_string_free (str, FALSE);
gtk_label_set_label (priv->label, text); gtk_label_set_label (priv->label, text);
gtk_label_set_label (priv->secondary_label, priv->letters); gtk_label_set_label (priv->secondary_label, secondary_text);
} }
static void static void
...@@ -63,14 +67,9 @@ hdy_dialer_button_set_property (GObject *object, ...@@ -63,14 +67,9 @@ hdy_dialer_button_set_property (GObject *object,
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self); HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
switch (property_id) { switch (property_id) {
case PROP_DIGIT: case PROP_SYMBOLS:
priv->digit = g_value_get_int (value); g_free (priv->symbols);
format_label(self); priv->symbols = g_value_dup_string (value);
break;
case PROP_LETTERS:
g_free (priv->letters);
priv->letters = g_value_dup_string (value);
format_label(self); format_label(self);
break; break;
...@@ -91,11 +90,11 @@ hdy_dialer_button_get_property (GObject *object, ...@@ -91,11 +90,11 @@ hdy_dialer_button_get_property (GObject *object,
switch (property_id) { switch (property_id) {
case PROP_DIGIT: case PROP_DIGIT:
g_value_set_int (value, priv->digit); g_value_set_int (value, hdy_dialer_button_get_digit (self));
break; break;
case PROP_LETTERS: case PROP_SYMBOLS:
g_value_set_string (value, priv->letters); g_value_set_string (value, priv->symbols);
break; break;
default: default:
...@@ -171,7 +170,7 @@ hdy_dialer_button_finalize (GObject *object) ...@@ -171,7 +170,7 @@ hdy_dialer_button_finalize (GObject *object)
HdyDialerButton *self = HDY_DIALER_BUTTON (object); HdyDialerButton *self = HDY_DIALER_BUTTON (object);
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self); HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
g_clear_pointer (&priv->letters, g_free); g_clear_pointer (&priv->symbols, g_free);
G_OBJECT_CLASS (hdy_dialer_button_parent_class)->finalize (object); G_OBJECT_CLASS (hdy_dialer_button_parent_class)->finalize (object);
} }
...@@ -197,12 +196,12 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass) ...@@ -197,12 +196,12 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass)
_("Digit"), _("Digit"),
_("The dialer digit of the button"), _("The dialer digit of the button"),
-1, INT_MAX, 0, -1, INT_MAX, 0,
G_PARAM_READWRITE); G_PARAM_READABLE);
props[PROP_LETTERS] = props[PROP_SYMBOLS] =
g_param_spec_string ("letters", g_param_spec_string ("symbols",
_("Letters"), _("Symbols"),
_("The dialer letters of the button"), _("The dialer symbols of the button"),
"", "",
G_PARAM_READWRITE); G_PARAM_READWRITE);
...@@ -216,19 +215,17 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass) ...@@ -216,19 +215,17 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass)
/** /**
* hdy_dialer_button_new: * hdy_dialer_button_new:
* @digit: the digit displayed on the #HdyDialerButton * @symbols: (nullable): the symbols displayed on the #HdyDialerButton
* @letters: (nullable): the letters displayed on the #HdyDialerButton
* *
* Create a new #HdyDialerButton which displays @digit and * Create a new #HdyDialerButton which displays
* @letters. If @digit is negative no number will be displayed. If * @symbols. If
* @letters is %NULL no letters will be displayed. * @symbols is %NULL no symbols will be displayed.
* *
* Returns: the newly created #HdyDialerButton widget * Returns: the newly created #HdyDialerButton widget
*/ */
GtkWidget *hdy_dialer_button_new (int digit, GtkWidget *hdy_dialer_button_new (const gchar *symbols)
const gchar *letters)
{ {
return g_object_new (HDY_TYPE_DIALER_BUTTON, "digit", digit, "letters", letters, NULL); return g_object_new (HDY_TYPE_DIALER_BUTTON, "symbols", symbols, NULL);
} }
static void static void
...@@ -238,8 +235,7 @@ hdy_dialer_button_init (HdyDialerButton *self) ...@@ -238,8 +235,7 @@ hdy_dialer_button_init (HdyDialerButton *self)
gtk_widget_init_template (GTK_WIDGET (self)); gtk_widget_init_template (GTK_WIDGET (self));
priv->digit = -1; priv->symbols = NULL;
priv->letters = NULL;
} }
/** /**
...@@ -251,29 +247,36 @@ hdy_dialer_button_init (HdyDialerButton *self) ...@@ -251,29 +247,36 @@ hdy_dialer_button_init (HdyDialerButton *self)
* Returns: the button's digit * Returns: the button's digit
*/ */
gint gint
hdy_dialer_button_get_digit(HdyDialerButton *self) hdy_dialer_button_get_digit (HdyDialerButton *self)
{ {
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self); HdyDialerButtonPrivate *priv;
gchar *symbols;
g_return_val_if_fail (HDY_IS_DIALER_BUTTON (self), -1); g_return_val_if_fail (HDY_IS_DIALER_BUTTON (self), -1);
return priv->digit; priv = hdy_dialer_button_get_instance_private(self);
symbols = priv->symbols;
g_return_val_if_fail (symbols != NULL, -1);
g_return_val_if_fail (g_ascii_isdigit (*symbols), -1);
return *symbols - '0';
} }
/** /**
* hdy_dialer_button_get_letters: * hdy_dialer_button_get_symbols:
* @self: a #HdyDialerButton * @self: a #HdyDialerButton
* *
* Get the #HdyDialerButton's letters. * Get the #HdyDialerButton's symbols.
* *
* Returns: the button's letters. * Returns: the button's symbols.
*/ */
const char* const char*
hdy_dialer_button_get_letters(HdyDialerButton *self) hdy_dialer_button_get_symbols (HdyDialerButton *self)
{ {
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self); HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
g_return_val_if_fail (HDY_IS_DIALER_BUTTON (self), NULL); g_return_val_if_fail (HDY_IS_DIALER_BUTTON (self), NULL);
return priv->letters; return priv->symbols;
} }
...@@ -24,10 +24,9 @@ struct _HdyDialerButtonClass ...@@ -24,10 +24,9 @@ struct _HdyDialerButtonClass
GtkButtonClass parent_class; GtkButtonClass parent_class;
}; };
GtkWidget *hdy_dialer_button_new (int digit, GtkWidget *hdy_dialer_button_new (const gchar *symbols);
const gchar *letters);
gint hdy_dialer_button_get_digit (HdyDialerButton *self); gint hdy_dialer_button_get_digit (HdyDialerButton *self);
const char *hdy_dialer_button_get_letters (HdyDialerButton *self); const char *hdy_dialer_button_get_symbols (HdyDialerButton *self);
G_END_DECLS G_END_DECLS
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
* @Title: HdyDialerCycleButton * @Title: HdyDialerCycleButton
* *
* The #HdyDialerCycleButton widget is a single button on a #HdyDialer * The #HdyDialerCycleButton widget is a single button on a #HdyDialer
* representing symbols such as regular letters or symbols like #, + * representing symbols such as digits, letters, #, +
* or ☃. When the button is pressed multiple times in a row, the * or ☃. When the button is pressed multiple times in a row, the
* symbols are cycled through. That is a call to #get_curent_symbol * symbols are cycled through. That is a call to #get_curent_symbol
* returns another symbol each time the button is pressed. If no * returns another symbol each time the button is pressed. If no
...@@ -75,7 +75,7 @@ button_clicked_cb (HdyDialerCycleButton *self, ...@@ -75,7 +75,7 @@ button_clicked_cb (HdyDialerCycleButton *self,
g_return_val_if_fail (HDY_IS_DIALER_CYCLE_BUTTON (self), FALSE); g_return_val_if_fail (HDY_IS_DIALER_CYCLE_BUTTON (self), FALSE);
/* Only cycle if we have more than one symbol */ /* Only cycle if we have more than one symbol */
if (strlen (hdy_dialer_button_get_letters (HDY_DIALER_BUTTON (self))) < 2) if (strlen (hdy_dialer_button_get_symbols (HDY_DIALER_BUTTON (self))) < 2)
return FALSE; return FALSE;
if (hdy_dialer_cycle_button_is_cycling (self)) { if (hdy_dialer_cycle_button_is_cycling (self)) {
...@@ -213,7 +213,7 @@ hdy_dialer_cycle_button_class_init (HdyDialerCycleButtonClass *klass) ...@@ -213,7 +213,7 @@ hdy_dialer_cycle_button_class_init (HdyDialerCycleButtonClass *klass)
GtkWidget *hdy_dialer_cycle_button_new (const gchar* symbols) GtkWidget *hdy_dialer_cycle_button_new (const gchar* symbols)
{ {
/* FIXME: we should call this 'symbols' in the base class already */ /* FIXME: we should call this 'symbols' in the base class already */
return g_object_new (HDY_TYPE_DIALER_CYCLE_BUTTON, "letters", symbols, NULL); return g_object_new (HDY_TYPE_DIALER_CYCLE_BUTTON, "symbols", symbols, NULL);
} }
static void static void
...@@ -237,7 +237,7 @@ gunichar ...@@ -237,7 +237,7 @@ gunichar
hdy_dialer_cycle_button_get_current_symbol (HdyDialerCycleButton *self) hdy_dialer_cycle_button_get_current_symbol (HdyDialerCycleButton *self)
{ {
HdyDialerCycleButtonPrivate *priv = hdy_dialer_cycle_button_get_instance_private (self); HdyDialerCycleButtonPrivate *priv = hdy_dialer_cycle_button_get_instance_private (self);
const gchar *symbols = hdy_dialer_button_get_letters (HDY_DIALER_BUTTON (self)); const gchar *symbols = hdy_dialer_button_get_symbols (HDY_DIALER_BUTTON (self));
gint off = priv->num % g_utf8_strlen (symbols, -1); gint off = priv->num % g_utf8_strlen (symbols, -1);
return g_utf8_get_char (g_utf8_offset_to_pointer (symbols, off)); return g_utf8_get_char (g_utf8_offset_to_pointer (symbols, off));
......
...@@ -105,7 +105,7 @@ cycle_button_clicked (HdyDialer *self, ...@@ -105,7 +105,7 @@ cycle_button_clicked (HdyDialer *self,
g_signal_emit(self, g_signal_emit(self,
signals[SIGNAL_SYMBOL_CLICKED], signals[SIGNAL_SYMBOL_CLICKED],
0, 0,
hdy_dialer_button_get_letters (HDY_DIALER_BUTTON (btn))[0]); hdy_dialer_button_get_symbols (HDY_DIALER_BUTTON (btn))[0]);
g_object_notify_by_pspec (G_OBJECT (self), props[PROP_NUMBER]); g_object_notify_by_pspec (G_OBJECT (self), props[PROP_NUMBER]);
} }
......
...@@ -17,8 +17,7 @@ ...@@ -17,8 +17,7 @@
<property name="visible">True</property> <property name="visible">True</property>
<child> <child>
<object class="HdyDialerButton" id="btn_1"> <object class="HdyDialerButton" id="btn_1">
<property name="digit">1</property> <property name="symbols">1</property>
<property name="letters"/>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -30,8 +29,7 @@ ...@@ -30,8 +29,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_2"> <object class="HdyDialerButton" id="btn_2">
<property name="digit">2</property> <property name="symbols">2ABC</property>
<property name="letters">ABC</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -43,8 +41,7 @@ ...@@ -43,8 +41,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_3"> <object class="HdyDialerButton" id="btn_3">
<property name="digit">3</property> <property name="symbols">3DEF</property>
<property name="letters">DEF</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -56,8 +53,7 @@ ...@@ -56,8 +53,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_4"> <object class="HdyDialerButton" id="btn_4">
<property name="digit">4</property> <property name="symbols">4GHI</property>
<property name="letters">GHI</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -69,8 +65,7 @@ ...@@ -69,8 +65,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_5"> <object class="HdyDialerButton" id="btn_5">
<property name="digit">5</property> <property name="symbols">5JKL</property>
<property name="letters">JKL</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -82,8 +77,7 @@ ...@@ -82,8 +77,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_6"> <object class="HdyDialerButton" id="btn_6">
<property name="digit">6</property> <property name="symbols">6MNO</property>
<property name="letters">MNO</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -95,8 +89,7 @@ ...@@ -95,8 +89,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_7"> <object class="HdyDialerButton" id="btn_7">
<property name="digit">7</property> <property name="symbols">7PQRS</property>
<property name="letters">PQRS</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -108,8 +101,7 @@ ...@@ -108,8 +101,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_8"> <object class="HdyDialerButton" id="btn_8">
<property name="digit">8</property> <property name="symbols">8TUV</property>
<property name="letters">TUV</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -121,8 +113,7 @@ ...@@ -121,8 +113,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_9"> <object class="HdyDialerButton" id="btn_9">
<property name="digit">9</property> <property name="symbols">9WXYZ</property>
<property name="letters">WXYZ</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -134,8 +125,7 @@ ...@@ -134,8 +125,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerCycleButton" id="btn_star"> <object class="HdyDialerCycleButton" id="btn_star">
<property name="digit">-1</property> <property name="symbols">*+</property>
<property name="letters">*+</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -147,8 +137,7 @@ ...@@ -147,8 +137,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerButton" id="btn_0"> <object class="HdyDialerButton" id="btn_0">
<property name="digit">0</property> <property name="symbols">0</property>
<property name="letters"/>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
...@@ -160,8 +149,7 @@ ...@@ -160,8 +149,7 @@
</child> </child>
<child> <child>
<object class="HdyDialerCycleButton" id="btn_hash"> <object class="HdyDialerCycleButton" id="btn_hash">
<property name="digit">-1</property> <property name="symbols">#</property>
<property name="letters">#</property>
<property name="visible">True</property> <property name="visible">True</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">True</property> <property name="receives_default">True</property>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment