2020-04-26 03:33:33 +00:00
|
|
|
/* Spin Buttons
|
2020-09-13 18:59:33 +00:00
|
|
|
* #Keywords: GtkEntry
|
2015-06-28 14:11:54 +00:00
|
|
|
*
|
|
|
|
* GtkSpinButton provides convenient ways to input data
|
|
|
|
* that can be seen as a value in a range. The examples
|
|
|
|
* here show that this does not necessarily mean numeric
|
|
|
|
* values, and it can include custom formatting.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <glib/gi18n.h>
|
|
|
|
#include <gtk/gtk.h>
|
|
|
|
#include <math.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_hex_spin_input (GtkSpinButton *spin_button,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *new_val)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *buf;
|
|
|
|
char *err;
|
2020-07-24 20:32:16 +00:00
|
|
|
double res;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
2019-02-28 19:31:36 +00:00
|
|
|
buf = gtk_editable_get_text (GTK_EDITABLE (spin_button));
|
2015-06-28 14:11:54 +00:00
|
|
|
res = strtol (buf, &err, 16);
|
|
|
|
*new_val = res;
|
|
|
|
if (*err)
|
|
|
|
return GTK_INPUT_ERROR;
|
|
|
|
else
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_hex_spin_output (GtkSpinButton *spin_button)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
|
|
|
GtkAdjustment *adjustment;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *buf;
|
2020-07-24 20:32:16 +00:00
|
|
|
double val;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
adjustment = gtk_spin_button_get_adjustment (spin_button);
|
2018-04-22 13:51:36 +00:00
|
|
|
val = gtk_adjustment_get_value (adjustment);
|
2015-06-28 14:11:54 +00:00
|
|
|
if (fabs (val) < 1e-5)
|
|
|
|
buf = g_strdup ("0x00");
|
|
|
|
else
|
2020-07-24 13:54:49 +00:00
|
|
|
buf = g_strdup_printf ("0x%.2X", (int) val);
|
2019-02-28 19:31:36 +00:00
|
|
|
if (strcmp (buf, gtk_editable_get_text (GTK_EDITABLE (spin_button))))
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (spin_button), buf);
|
2015-06-28 14:11:54 +00:00
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_time_spin_input (GtkSpinButton *spin_button,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *new_val)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
2020-07-24 18:40:36 +00:00
|
|
|
const char *text;
|
|
|
|
char **str;
|
2015-06-28 14:11:54 +00:00
|
|
|
gboolean found = FALSE;
|
2020-07-24 13:54:49 +00:00
|
|
|
int hours;
|
|
|
|
int minutes;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *endh;
|
|
|
|
char *endm;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
2019-02-28 19:31:36 +00:00
|
|
|
text = gtk_editable_get_text (GTK_EDITABLE (spin_button));
|
2015-06-28 14:11:54 +00:00
|
|
|
str = g_strsplit (text, ":", 2);
|
|
|
|
|
|
|
|
if (g_strv_length (str) == 2)
|
|
|
|
{
|
|
|
|
hours = strtol (str[0], &endh, 10);
|
|
|
|
minutes = strtol (str[1], &endm, 10);
|
|
|
|
if (!*endh && !*endm &&
|
|
|
|
0 <= hours && hours < 24 &&
|
|
|
|
0 <= minutes && minutes < 60)
|
|
|
|
{
|
|
|
|
*new_val = hours * 60 + minutes;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev (str);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
*new_val = 0.0;
|
|
|
|
return GTK_INPUT_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_time_spin_output (GtkSpinButton *spin_button)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
|
|
|
GtkAdjustment *adjustment;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *buf;
|
2020-07-24 20:32:16 +00:00
|
|
|
double hours;
|
|
|
|
double minutes;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
adjustment = gtk_spin_button_get_adjustment (spin_button);
|
|
|
|
hours = gtk_adjustment_get_value (adjustment) / 60.0;
|
|
|
|
minutes = (hours - floor (hours)) * 60.0;
|
|
|
|
buf = g_strdup_printf ("%02.0f:%02.0f", floor (hours), floor (minutes + 0.5));
|
2019-02-28 19:31:36 +00:00
|
|
|
if (strcmp (buf, gtk_editable_get_text (GTK_EDITABLE (spin_button))))
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (spin_button), buf);
|
2015-06-28 14:11:54 +00:00
|
|
|
g_free (buf);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2020-03-07 14:01:21 +00:00
|
|
|
static const char *month[12] = {
|
2015-06-28 14:11:54 +00:00
|
|
|
"January",
|
|
|
|
"February",
|
|
|
|
"March",
|
|
|
|
"April",
|
|
|
|
"May",
|
|
|
|
"June",
|
|
|
|
"July",
|
|
|
|
"August",
|
|
|
|
"September",
|
|
|
|
"October",
|
|
|
|
"November",
|
|
|
|
"December"
|
|
|
|
};
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_month_spin_input (GtkSpinButton *spin_button,
|
2020-07-24 20:32:16 +00:00
|
|
|
double *new_val)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2020-07-24 18:40:36 +00:00
|
|
|
char *tmp1, *tmp2;
|
2015-06-28 14:11:54 +00:00
|
|
|
gboolean found = FALSE;
|
|
|
|
|
|
|
|
for (i = 1; i <= 12; i++)
|
|
|
|
{
|
|
|
|
tmp1 = g_ascii_strup (month[i - 1], -1);
|
2019-02-28 19:31:36 +00:00
|
|
|
tmp2 = g_ascii_strup (gtk_editable_get_text (GTK_EDITABLE (spin_button)), -1);
|
2015-06-28 14:11:54 +00:00
|
|
|
if (strstr (tmp1, tmp2) == tmp1)
|
|
|
|
found = TRUE;
|
|
|
|
g_free (tmp1);
|
|
|
|
g_free (tmp2);
|
|
|
|
if (found)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
*new_val = 0.0;
|
|
|
|
return GTK_INPUT_ERROR;
|
|
|
|
}
|
2020-07-24 20:32:16 +00:00
|
|
|
*new_val = (double) i;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2021-05-06 09:21:46 +00:00
|
|
|
G_MODULE_EXPORT int
|
2019-11-29 07:48:48 +00:00
|
|
|
spinbutton_month_spin_output (GtkSpinButton *spin_button)
|
2015-06-28 14:11:54 +00:00
|
|
|
{
|
|
|
|
GtkAdjustment *adjustment;
|
2020-07-24 20:32:16 +00:00
|
|
|
double value;
|
2020-07-24 13:54:49 +00:00
|
|
|
int i;
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
adjustment = gtk_spin_button_get_adjustment (spin_button);
|
|
|
|
value = gtk_adjustment_get_value (adjustment);
|
|
|
|
for (i = 1; i <= 12; i++)
|
|
|
|
if (fabs (value - (double)i) < 1e-5)
|
|
|
|
{
|
2019-02-28 19:31:36 +00:00
|
|
|
if (strcmp (month[i-1], gtk_editable_get_text (GTK_EDITABLE (spin_button))))
|
|
|
|
gtk_editable_set_text (GTK_EDITABLE (spin_button), month[i-1]);
|
2015-06-28 14:11:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
value_to_label (GBinding *binding,
|
|
|
|
const GValue *from,
|
|
|
|
GValue *to,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
g_value_take_string (to, g_strdup_printf ("%g", g_value_get_double (from)));
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
GtkWidget *
|
|
|
|
do_spinbutton (GtkWidget *do_widget)
|
|
|
|
{
|
|
|
|
static GtkWidget *window;
|
|
|
|
|
|
|
|
if (!window)
|
2020-08-09 15:14:28 +00:00
|
|
|
{
|
|
|
|
GtkBuilder *builder;
|
|
|
|
GtkAdjustment *adj;
|
|
|
|
GtkWidget *label;
|
|
|
|
|
|
|
|
builder = gtk_builder_new_from_resource ("/spinbutton/spinbutton.ui");
|
|
|
|
window = GTK_WIDGET (gtk_builder_get_object (builder, "window"));
|
|
|
|
gtk_window_set_display (GTK_WINDOW (window),
|
|
|
|
gtk_widget_get_display (do_widget));
|
|
|
|
gtk_window_set_title (GTK_WINDOW (window), "Spin Buttons");
|
|
|
|
gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
|
|
|
|
g_object_add_weak_pointer (G_OBJECT (window), (gpointer *)&window);
|
|
|
|
|
|
|
|
adj = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "basic_adjustment"));
|
|
|
|
label = GTK_WIDGET (gtk_builder_get_object (builder, "basic_label"));
|
|
|
|
g_object_bind_property_full (adj, "value",
|
|
|
|
label, "label",
|
|
|
|
G_BINDING_SYNC_CREATE,
|
|
|
|
value_to_label,
|
|
|
|
NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
adj = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "hex_adjustment"));
|
|
|
|
label = GTK_WIDGET (gtk_builder_get_object (builder, "hex_label"));
|
|
|
|
g_object_bind_property_full (adj, "value",
|
|
|
|
label, "label",
|
|
|
|
G_BINDING_SYNC_CREATE,
|
|
|
|
value_to_label,
|
|
|
|
NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
adj = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "time_adjustment"));
|
|
|
|
label = GTK_WIDGET (gtk_builder_get_object (builder, "time_label"));
|
|
|
|
g_object_bind_property_full (adj, "value",
|
|
|
|
label, "label",
|
|
|
|
G_BINDING_SYNC_CREATE,
|
|
|
|
value_to_label,
|
|
|
|
NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
adj = GTK_ADJUSTMENT (gtk_builder_get_object (builder, "month_adjustment"));
|
|
|
|
label = GTK_WIDGET (gtk_builder_get_object (builder, "month_label"));
|
|
|
|
g_object_bind_property_full (adj, "value",
|
|
|
|
label, "label",
|
|
|
|
G_BINDING_SYNC_CREATE,
|
|
|
|
value_to_label,
|
|
|
|
NULL,
|
|
|
|
NULL, NULL);
|
|
|
|
|
|
|
|
g_object_unref (builder);
|
|
|
|
}
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
if (!gtk_widget_get_visible (window))
|
2017-01-19 09:02:04 +00:00
|
|
|
gtk_widget_show (window);
|
2015-06-28 14:11:54 +00:00
|
|
|
else
|
2020-05-09 14:26:22 +00:00
|
|
|
gtk_window_destroy (GTK_WINDOW (window));
|
2015-06-28 14:11:54 +00:00
|
|
|
|
|
|
|
return window;
|
|
|
|
}
|