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 @@
* @Title: HdyDialerButton
*
* The #HdyDialerButton widget is a single button on an #HdyDialer. It
* can represent a single digit (0-9) plus an arbitrary number of
* letters that are displayed below the number.
* can represent a single symbol (typically a digit) plus an arbitrary
* number of symbols that are displayed below it.
*/
enum {
PROP_0,
PROP_DIGIT,
PROP_LETTERS,
PROP_SYMBOLS,
PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];
......@@ -29,8 +29,7 @@ static GParamSpec *props[PROP_LAST_PROP];
typedef struct
{
GtkLabel *label, *secondary_label;
gint digit;
gchar *letters;
gchar *symbols;
} HdyDialerButtonPrivate;
G_DEFINE_TYPE_WITH_PRIVATE (HdyDialerButton, hdy_dialer_button, GTK_TYPE_BUTTON)
......@@ -39,18 +38,23 @@ static void
format_label(HdyDialerButton *self)
{
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
GString *str;
g_autofree gchar *text;
str = g_string_new(NULL);
if (priv->digit >= 0) {
g_string_sprintf (str, "%d", priv->digit);
gchar *symbols = priv->symbols != NULL ? priv->symbols : "";
g_autofree gchar *text = NULL;
gchar *secondary_text = NULL;
if (*symbols != '\0') {
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->secondary_label, priv->letters);
gtk_label_set_label (priv->secondary_label, secondary_text);
}
static void
......@@ -63,14 +67,9 @@ hdy_dialer_button_set_property (GObject *object,
HdyDialerButtonPrivate *priv = hdy_dialer_button_get_instance_private(self);
switch (property_id) {
case PROP_DIGIT:
priv->digit = g_value_get_int (value);
format_label(self);
break;
case PROP_LETTERS:
g_free (priv->letters);
priv->letters = g_value_dup_string (value);
case PROP_SYMBOLS:
g_free (priv->symbols);
priv->symbols = g_value_dup_string (value);
format_label(self);
break;
......@@ -91,11 +90,11 @@ hdy_dialer_button_get_property (GObject *object,
switch (property_id) {
case PROP_DIGIT:
g_value_set_int (value, priv->digit);
g_value_set_int (value, hdy_dialer_button_get_digit (self));
break;
case PROP_LETTERS:
g_value_set_string (value, priv->letters);
case PROP_SYMBOLS:
g_value_set_string (value, priv->symbols);
break;
default:
......@@ -171,7 +170,7 @@ hdy_dialer_button_finalize (GObject *object)
HdyDialerButton *self = HDY_DIALER_BUTTON (object);
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);
}
......@@ -197,12 +196,12 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass)
_("Digit"),
_("The dialer digit of the button"),
-1, INT_MAX, 0,
G_PARAM_READWRITE);
G_PARAM_READABLE);
props[PROP_LETTERS] =
g_param_spec_string ("letters",
_("Letters"),
_("The dialer letters of the button"),
props[PROP_SYMBOLS] =
g_param_spec_string ("symbols",
_("Symbols"),
_("The dialer symbols of the button"),
"",
G_PARAM_READWRITE);
......@@ -216,19 +215,17 @@ hdy_dialer_button_class_init (HdyDialerButtonClass *klass)
/**
* hdy_dialer_button_new:
* @digit: the digit displayed on the #HdyDialerButton
* @letters: (nullable): the letters displayed on the #HdyDialerButton
* @symbols: (nullable): the symbols displayed on the #HdyDialerButton
*
* Create a new #HdyDialerButton which displays @digit and
* @letters. If @digit is negative no number will be displayed. If
* @letters is %NULL no letters will be displayed.
* Create a new #HdyDialerButton which displays
* @symbols. If
* @symbols is %NULL no symbols will be displayed.
*
* Returns: the newly created #HdyDialerButton widget
*/
GtkWidget *hdy_dialer_button_new (int digit,
const gchar *letters)
GtkWidget *hdy_dialer_button_new (const gchar *symbols)
{
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
......@@ -238,8 +235,7 @@ hdy_dialer_button_init (HdyDialerButton *self)
gtk_widget_init_template (GTK_WIDGET (self));
priv->digit = -1;
priv->letters = NULL;
priv->symbols = NULL;
}
/**
......@@ -251,29 +247,36 @@ hdy_dialer_button_init (HdyDialerButton *self)
* Returns: the button's digit
*/
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);
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
*
* Get the #HdyDialerButton's letters.
* Get the #HdyDialerButton's symbols.
*
* Returns: the button's letters.
* Returns: the button's symbols.
*/
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);
g_return_val_if_fail (HDY_IS_DIALER_BUTTON (self), NULL);
return priv->letters;
return priv->symbols;
}
......@@ -24,10 +24,9 @@ struct _HdyDialerButtonClass
GtkButtonClass parent_class;
};
GtkWidget *hdy_dialer_button_new (int digit,
const gchar *letters);
GtkWidget *hdy_dialer_button_new (const gchar *symbols);
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
......
......@@ -14,7 +14,7 @@
* @Title: HdyDialerCycleButton
*
* 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
* symbols are cycled through. That is a call to #get_curent_symbol
* returns another symbol each time the button is pressed. If no
......@@ -75,7 +75,7 @@ button_clicked_cb (HdyDialerCycleButton *self,
g_return_val_if_fail (HDY_IS_DIALER_CYCLE_BUTTON (self), FALSE);
/* 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;
if (hdy_dialer_cycle_button_is_cycling (self)) {
......@@ -213,7 +213,7 @@ hdy_dialer_cycle_button_class_init (HdyDialerCycleButtonClass *klass)
GtkWidget *hdy_dialer_cycle_button_new (const gchar* symbols)
{
/* 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
......@@ -237,7 +237,7 @@ gunichar
hdy_dialer_cycle_button_get_current_symbol (HdyDialerCycleButton *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);
return g_utf8_get_char (g_utf8_offset_to_pointer (symbols, off));
......
......@@ -105,7 +105,7 @@ cycle_button_clicked (HdyDialer *self,
g_signal_emit(self,
signals[SIGNAL_SYMBOL_CLICKED],
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]);
}
......
......@@ -17,8 +17,7 @@
<property name="visible">True</property>
<child>
<object class="HdyDialerButton" id="btn_1">
<property name="digit">1</property>
<property name="letters"/>
<property name="symbols">1</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -30,8 +29,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_2">
<property name="digit">2</property>
<property name="letters">ABC</property>
<property name="symbols">2ABC</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -43,8 +41,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_3">
<property name="digit">3</property>
<property name="letters">DEF</property>
<property name="symbols">3DEF</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -56,8 +53,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_4">
<property name="digit">4</property>
<property name="letters">GHI</property>
<property name="symbols">4GHI</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -69,8 +65,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_5">
<property name="digit">5</property>
<property name="letters">JKL</property>
<property name="symbols">5JKL</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -82,8 +77,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_6">
<property name="digit">6</property>
<property name="letters">MNO</property>
<property name="symbols">6MNO</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -95,8 +89,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_7">
<property name="digit">7</property>
<property name="letters">PQRS</property>
<property name="symbols">7PQRS</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -108,8 +101,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_8">
<property name="digit">8</property>
<property name="letters">TUV</property>
<property name="symbols">8TUV</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -121,8 +113,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_9">
<property name="digit">9</property>
<property name="letters">WXYZ</property>
<property name="symbols">9WXYZ</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -134,8 +125,7 @@
</child>
<child>
<object class="HdyDialerCycleButton" id="btn_star">
<property name="digit">-1</property>
<property name="letters">*+</property>
<property name="symbols">*+</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -147,8 +137,7 @@
</child>
<child>
<object class="HdyDialerButton" id="btn_0">
<property name="digit">0</property>
<property name="letters"/>
<property name="symbols">0</property>
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="receives_default">True</property>
......@@ -160,8 +149,7 @@
</child>
<child>
<object class="HdyDialerCycleButton" id="btn_hash">
<property name="digit">-1</property>
<property name="letters">#</property>
<property name="symbols">#</property>
<property name="visible">True</property>
<property name="can_focus">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