1 | /* gtkatspivalue.c: AT-SPI Value implementation |
2 | * |
3 | * Copyright 2020 Red Hat, Inc. |
4 | * |
5 | * SPDX-License-Identifier: LGPL-2.1-or-later |
6 | * |
7 | * This library is free software; you can redistribute it and/or |
8 | * modify it under the terms of the GNU Lesser General Public |
9 | * License as published by the Free Software Foundation; either |
10 | * version 2.1 of the License, or (at your option) any later version. |
11 | * |
12 | * This library is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
15 | * Lesser General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU Lesser General Public |
18 | * License along with this library; if not, see <http://www.gnu.org/licenses/>. |
19 | */ |
20 | |
21 | #include "config.h" |
22 | |
23 | #include "gtkatspivalueprivate.h" |
24 | |
25 | #include "a11y/atspi/atspi-value.h" |
26 | |
27 | #include "gtkatcontextprivate.h" |
28 | #include "gtkdebug.h" |
29 | #include "gtklevelbar.h" |
30 | #include "gtkpaned.h" |
31 | #include "gtkprogressbar.h" |
32 | #include "gtkrange.h" |
33 | #include "gtkscalebutton.h" |
34 | #include "gtkspinbutton.h" |
35 | |
36 | #include <gio/gio.h> |
37 | |
38 | static GVariant * |
39 | handle_value_get_property (GDBusConnection *connection, |
40 | const gchar *sender, |
41 | const gchar *object_path, |
42 | const gchar *interface_name, |
43 | const gchar *property_name, |
44 | GError **error, |
45 | gpointer user_data) |
46 | { |
47 | GtkATContext *ctx = GTK_AT_CONTEXT (ptr: user_data); |
48 | struct { |
49 | const char *name; |
50 | GtkAccessibleProperty property; |
51 | } properties[] = { |
52 | { "MinimumValue" , GTK_ACCESSIBLE_PROPERTY_VALUE_MIN }, |
53 | { "MaximumValue" , GTK_ACCESSIBLE_PROPERTY_VALUE_MAX }, |
54 | { "CurrentValue" , GTK_ACCESSIBLE_PROPERTY_VALUE_NOW }, |
55 | }; |
56 | int i; |
57 | |
58 | for (i = 0; i < G_N_ELEMENTS (properties); i++) |
59 | { |
60 | if (g_strcmp0 (str1: property_name, str2: properties[i].name) == 0) |
61 | { |
62 | if (gtk_at_context_has_accessible_property (self: ctx, property: properties[i].property)) |
63 | { |
64 | GtkAccessibleValue *value; |
65 | |
66 | value = gtk_at_context_get_accessible_property (self: ctx, property: properties[i].property); |
67 | return g_variant_new_double (value: gtk_number_accessible_value_get (value)); |
68 | } |
69 | } |
70 | } |
71 | |
72 | /* fall back for a) MinimumIncrement b) widgets that should have the |
73 | * properties but don't |
74 | */ |
75 | return g_variant_new_double (value: 0.0); |
76 | } |
77 | |
78 | static gboolean |
79 | handle_value_set_property (GDBusConnection *connection, |
80 | const gchar *sender, |
81 | const gchar *object_path, |
82 | const gchar *interface_name, |
83 | const gchar *property_name, |
84 | GVariant *value, |
85 | GError **error, |
86 | gpointer user_data) |
87 | { |
88 | GtkATContext *self = user_data; |
89 | GtkWidget *widget = GTK_WIDGET (gtk_at_context_get_accessible (self)); |
90 | |
91 | if (g_strcmp0 (str1: property_name, str2: "CurrentValue" ) == 0) |
92 | { |
93 | /* we only allow setting values if that is part of the user-exposed |
94 | * functionality of the widget. |
95 | */ |
96 | if (GTK_IS_RANGE (widget)) |
97 | gtk_range_set_value (GTK_RANGE (widget), value: g_variant_get_double (value)); |
98 | else if (GTK_IS_PANED (widget)) |
99 | gtk_paned_set_position (GTK_PANED (widget), position: (int)(g_variant_get_double (value) + 0.5)); |
100 | else if (GTK_IS_SPIN_BUTTON (widget)) |
101 | gtk_spin_button_set_value (GTK_SPIN_BUTTON (widget), value: g_variant_get_double (value)); |
102 | else if (GTK_IS_SCALE_BUTTON (widget)) |
103 | gtk_scale_button_set_value (GTK_SCALE_BUTTON (widget), value: g_variant_get_double (value)); |
104 | return TRUE; |
105 | } |
106 | |
107 | return FALSE; |
108 | } |
109 | |
110 | static const GDBusInterfaceVTable value_vtable = { |
111 | NULL, |
112 | handle_value_get_property, |
113 | handle_value_set_property, |
114 | }; |
115 | |
116 | const GDBusInterfaceVTable * |
117 | gtk_atspi_get_value_vtable (GtkAccessible *accessible) |
118 | { |
119 | if (GTK_IS_LEVEL_BAR (accessible) || |
120 | GTK_IS_PANED (accessible) || |
121 | GTK_IS_PROGRESS_BAR (accessible) || |
122 | GTK_IS_RANGE (accessible) || |
123 | GTK_IS_SCALE_BUTTON (accessible) || |
124 | GTK_IS_SPIN_BUTTON (accessible)) |
125 | return &value_vtable; |
126 | |
127 | return NULL; |
128 | } |
129 | |
130 | |