aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/idoscalemenuitem.c57
-rw-r--r--src/idoswitchmenuitem.c46
2 files changed, 93 insertions, 10 deletions
diff --git a/src/idoscalemenuitem.c b/src/idoscalemenuitem.c
index 37a9385..50c323a 100644
--- a/src/idoscalemenuitem.c
+++ b/src/idoscalemenuitem.c
@@ -1,6 +1,6 @@
/*
* Copyright 2010 Canonical, Ltd.
- * Copyright 2021 Robert Tari
+ * Copyright 2021-2024 Robert Tari
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of either or both of the following licenses:
@@ -26,6 +26,7 @@
*/
#include <gtk/gtk.h>
+#include <math.h>
#include "idorange.h"
#include "idoscalemenuitem.h"
#include "idotypebuiltins.h"
@@ -77,6 +78,7 @@ typedef struct {
IdoRangeStyle range_style;
gboolean ignore_value_changed;
gboolean has_focus;
+ gboolean bCloseOnChange;
} IdoScaleMenuItemPrivate;
enum {
@@ -142,8 +144,16 @@ ido_scale_menu_item_scale_value_changed (GtkRange *range,
/* The signal is not sent when it was set through
* ido_scale_menu_item_set_value(). */
- if (!priv->ignore_value_changed)
- g_signal_emit (self, signals[VALUE_CHANGED], 0, gtk_range_get_value (range));
+ if (!priv->ignore_value_changed)
+ {
+ g_signal_emit (self, signals[VALUE_CHANGED], 0, gtk_range_get_value (range));
+
+ if (priv->bCloseOnChange)
+ {
+ GtkWidget *pParent = gtk_widget_get_parent (GTK_WIDGET (self));
+ gtk_menu_shell_deactivate (GTK_MENU_SHELL (pParent));
+ }
+ }
}
static void
@@ -366,7 +376,7 @@ static void
ido_scale_menu_item_init (IdoScaleMenuItem *self)
{
IdoScaleMenuItemPrivate *priv = ido_scale_menu_item_get_instance_private (self);
-
+ priv->bCloseOnChange = FALSE;
priv->reverse_scroll = TRUE;
gtk_widget_set_size_request (GTK_WIDGET (self), 200, -1);
@@ -474,8 +484,6 @@ ido_scale_menu_item_select (GtkMenuItem *item)
priv->has_focus = TRUE;
gtk_widget_set_state_flags (priv->scale, GTK_STATE_FLAG_FOCUSED, FALSE);
-
- GTK_MENU_ITEM_CLASS (ido_scale_menu_item_parent_class)->select (item);
}
static void
@@ -486,8 +494,6 @@ ido_scale_menu_item_deselect (GtkMenuItem *item)
priv->has_focus = FALSE;
gtk_widget_unset_state_flags (priv->scale, GTK_STATE_FLAG_FOCUSED);
-
- GTK_MENU_ITEM_CLASS (ido_scale_menu_item_parent_class)->deselect (item);
}
static gboolean
@@ -701,7 +707,7 @@ ido_scale_menu_item_new_with_range (const gchar *label,
gdouble max,
gdouble step)
{
- GObject *adjustment = G_OBJECT (gtk_adjustment_new (value, min, max, step, 10 * step, 0));
+ GObject *adjustment = G_OBJECT (gtk_adjustment_new (value, min, max, step, step, 0));
return GTK_WIDGET (g_object_new (IDO_TYPE_SCALE_MENU_ITEM,
"label", label,
@@ -1048,6 +1054,13 @@ menu_item_get_icon (GMenuItem *menuitem,
return value ? g_icon_deserialize (value) : NULL;
}
+static gchar* onFormatValue (GtkScale *pScale, gdouble fValue)
+{
+ gint nValue = fValue * 100;
+ gchar *sValue = g_strdup_printf ("%i%%", nValue);
+
+ return sValue;
+}
/**
* ido_scale_menu_item_new_from_model:
*
@@ -1089,6 +1102,32 @@ ido_scale_menu_item_new_from_model (GMenuItem *menuitem,
g_free (action);
}
+ IdoScaleMenuItemPrivate *pPrivate = ido_scale_menu_item_get_instance_private (IDO_SCALE_MENU_ITEM (item));
+ guchar nDigits = 0;
+ gboolean bFound = g_menu_item_get_attribute (menuitem, "digits", "y", &nDigits);
+
+ if (bFound)
+ {
+ gtk_scale_set_digits (GTK_SCALE (pPrivate->scale), nDigits);
+ gtk_range_set_round_digits (GTK_RANGE (pPrivate->scale), nDigits);
+ }
+
+ gboolean bMarks = FALSE;
+ bFound = g_menu_item_get_attribute (menuitem, "marks", "b", &bMarks);
+
+ if (bFound)
+ {
+ gtk_scale_set_draw_value (GTK_SCALE (pPrivate->scale), TRUE);
+
+ for (gdouble fValue = min; fValue < (max + step); fValue += step)
+ {
+ gtk_scale_add_mark (GTK_SCALE (pPrivate->scale), round (fValue * 10) / 10, GTK_POS_BOTTOM, NULL);
+ }
+
+ g_signal_connect (pPrivate->scale, "format-value", G_CALLBACK (onFormatValue), NULL);
+ }
+
+ g_menu_item_get_attribute (menuitem, "close-on-change", "b", &pPrivate->bCloseOnChange);
min_icon = menu_item_get_icon (menuitem, "min-icon");
max_icon = menu_item_get_icon (menuitem, "max-icon");
ido_scale_menu_item_set_icons (IDO_SCALE_MENU_ITEM (item), min_icon, max_icon);
diff --git a/src/idoswitchmenuitem.c b/src/idoswitchmenuitem.c
index 9bc9ef9..af90d7b 100644
--- a/src/idoswitchmenuitem.c
+++ b/src/idoswitchmenuitem.c
@@ -33,6 +33,7 @@ typedef struct
GtkWidget * label;
GtkWidget * image;
GtkWidget * switch_w;
+ GtkWidget * accelerator;
} IdoSwitchMenuItemPrivate;
/***
@@ -168,13 +169,47 @@ ido_switch_menu_item_set_label (IdoSwitchMenuItem *item,
priv->label = gtk_label_new (NULL);
gtk_widget_set_halign (priv->label, GTK_ALIGN_START);
gtk_widget_show (priv->label);
- gtk_box_pack_end (GTK_BOX (priv->content_area), priv->label, TRUE, TRUE, 0);
+ gtk_box_pack_start (GTK_BOX (priv->content_area), priv->label, TRUE, TRUE, 0);
}
gtk_label_set_text (GTK_LABEL (priv->label), label);
}
/**
+ * ido_switch_menu_item_set_accelerator:
+ * @item: a #IdoSwitchMenuItem.
+ * @accelerator: a string to set as the accelerator of @item
+ *
+ * Set the accelerator of @item to @accelerator.
+ **/
+
+void ido_switch_menu_item_set_accelerator (IdoSwitchMenuItem *item, const gchar *accelerator)
+{
+ IdoSwitchMenuItemPrivate *priv;
+
+ g_return_if_fail (IDO_IS_SWITCH_MENU_ITEM (item));
+ g_return_if_fail (accelerator != NULL);
+
+ priv = ido_switch_menu_item_get_instance_private (item);
+
+ if (priv->accelerator == NULL)
+ {
+ priv->accelerator = gtk_label_new (NULL);
+ gtk_widget_set_halign (priv->accelerator, GTK_ALIGN_END);
+ GtkStyleContext *pContext = gtk_widget_get_style_context (priv->accelerator);
+ gtk_style_context_add_class (pContext, "accelerator");
+ gtk_widget_show (priv->accelerator);
+ gtk_box_pack_end (GTK_BOX (priv->content_area), priv->accelerator, FALSE, FALSE, 6);
+ }
+
+ guint nKey;
+ GdkModifierType cType;
+ gtk_accelerator_parse (accelerator, &nKey, &cType);
+ gchar *sText = gtk_accelerator_get_label (nKey, cType);
+ gtk_label_set_text (GTK_LABEL (priv->accelerator), sText);
+}
+
+/**
* ido_switch_menu_item_set_icon:
* @item: a #IdoSwitchMenuItem.
* @icon: (allow-none): a #GIcon
@@ -248,6 +283,15 @@ ido_switch_menu_item_new_from_menu_model (GMenuItem *menuitem,
g_free (label);
}
+ gchar *sAccelerator;
+ gboolean bAccelerator = g_menu_item_get_attribute (menuitem, "accel", "s", &sAccelerator);
+
+ if (bAccelerator)
+ {
+ ido_switch_menu_item_set_accelerator (IDO_SWITCH_MENU_ITEM (item), sAccelerator);
+ g_free (sAccelerator);
+ }
+
serialized_icon = g_menu_item_get_attribute_value (menuitem, "icon", NULL);
if (serialized_icon)
{