atkvalue: add strings to translate value type names
[platform/upstream/atk.git] / atk / atkvalue.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001, 2002, 2003 Sun Microsystems Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #include "config.h"
21
22 #include <string.h>
23 #include <glib/gi18n-lib.h>
24
25 #include "atkvalue.h"
26 #include "atkmarshal.h"
27 #include "atk-enum-types.h"
28 #include "atkprivate.h"
29
30 /**
31  * SECTION:atkvalue
32  * @Short_description: The ATK interface implemented by valuators and
33  *  components which display or select a value from a bounded range of
34  *  values.
35  * @Title:AtkValue
36  *
37  * #AtkValue should be implemented for components which either display
38  * a value from a bounded range, or which allow the user to specify a
39  * value from a bounded range, or both. For instance, most sliders and
40  * range controls, as well as dials, should have #AtkObject
41  * representations which implement #AtkValue on the component's
42  * behalf. #AtKValues may be read-only, in which case attempts to
43  * alter the value return would fail.
44  *
45  * <refsect1 id="current-value-text">
46  * <title>On the subject of current value text</title>
47  * <para>
48  * In addition to providing the current value, implementors can
49  * optionally provide an end-user-consumable textual description
50  * associated with this value. This description should be included
51  * when the numeric value fails to convey the full, on-screen
52  * representation seen by users.
53  * </para>
54  *
55  * <example>
56  * <title>Password strength</title>
57  * A password strength meter whose value changes as the user types
58  * their new password. Red is used for values less than 4.0, yellow
59  * for values between 4.0 and 7.0, and green for values greater than
60  * 7.0. In this instance, value text should be provided by the
61  * implementor. Appropriate value text would be "weak", "acceptable,"
62  * and "strong" respectively.
63  * </example>
64  *
65  * A level bar whose value changes to reflect the battery charge. The
66  * color remains the same regardless of the charge and there is no
67  * on-screen text reflecting the fullness of the battery. In this
68  * case, because the position within the bar is the only indication
69  * the user has of the current charge, value text should not be
70  * provided by the implementor.
71  *
72  * <refsect2 id="implementor-notes">
73  * <title>Implementor Notes</title>
74  * <para>
75  * Implementors should bear in mind that assistive technologies will
76  * likely prefer the value text provided over the numeric value when
77  * presenting a widget's value. As a result, strings not intended for
78  * end users should not be exposed in the value text, and strings
79  * which are exposed should be localized. In the case of widgets which
80  * display value text on screen, for instance through a separate label
81  * in close proximity to the value-displaying widget, it is still
82  * expected that implementors will expose the value text using the
83  * above API.
84  * </para>
85  *
86  * <para>
87  * #AtkValue should NOT be implemented for widgets whose displayed
88  * value is not reflective of a meaningful amount. For instance, a
89  * progress pulse indicator whose value alternates between 0.0 and 1.0
90  * to indicate that some process is still taking place should not
91  * implement #AtkValue because the current value does not reflect
92  * progress towards completion.
93  * </para>
94  * </refsect2>
95  * </refsect1>
96  *
97  * <refsect1 id="ranges">
98  * <title>On the subject of ranges</title>
99  * <para>
100  * In addition to providing the minimum and maximum values,
101  * implementors can optionally provide details about subranges
102  * associated with the widget. These details should be provided by the
103  * implementor when both of the following are communicated visually to
104  * the end user:
105  * </para>
106  * <itemizedlist>
107  *   <listitem>The existence of distinct ranges such as "weak",
108  *   "acceptable", and "strong" indicated by color, bar tick marks,
109  *   and/or on-screen text.</listitem>
110  *   <listitem>Where the current value stands within a given subrange,
111  *   for instance illustrating progression from very "weak" towards
112  *   nearly "acceptable" through changes in shade and/or position on
113  *   the bar within the "weak" subrange.</listitem>
114  * </itemizedlist>
115  * <para>
116  * If both of the above do not apply to the widget, it should be
117  * sufficient to expose the numeric value, along with the value text
118  * if appropriate, to make the widget accessible.
119  * </para>
120  *
121  * <refsect2 id="ranges-implementor-notes">
122  * <title>Implementor Notes</title>
123  * <para>
124  * If providing subrange details is deemed necessary, all possible
125  * values of the widget are expected to fall within one of the
126  * subranges defined by the implementor.
127  * </para>
128  * </refsect2>
129  * </refsect1>
130  *
131  * <refsect1 id="localization">
132  * <title>On the subject of localization of end-user-consumable text
133  * values</title>
134  * <para>
135  * Because value text and subrange descriptors are human-consumable,
136  * implementors are expected to provide localized strings which can be
137  * directly presented to end users via their assistive technology. In
138  * order to simplify this for implementors, implementors can use
139  * atk_value_type_get_localized_name() with the following
140  * already-localized constants for commonly-needed values can be used:
141  * </para>
142  *
143  * <itemizedlist>
144  *   <listitem>ATK_VALUE_VERY_WEAK</listitem>
145  *   <listitem>ATK_VALUE_WEAK</listitem>
146  *   <listitem>ATK_VALUE_ACCEPTABLE</listitem>
147  *   <listitem>ATK_VALUE_STRONG</listitem>
148  *   <listitem>ATK_VALUE_VERY_STRONG</listitem>
149  *   <listitem>ATK_VALUE_VERY_LOW</listitem>
150  *   <listitem>ATK_VALUE_LOW</listitem>
151  *   <listitem>ATK_VALUE_MEDIUM</listitem>
152  *   <listitem>ATK_VALUE_HIGH</listitem>
153  *   <listitem>ATK_VALUE_VERY_HIGH</listitem>
154  *   <listitem>ATK_VALUE_VERY_BAD</listitem>
155  *   <listitem>ATK_VALUE_BAD</listitem>
156  *   <listitem>ATK_VALUE_GOOD</listitem>
157  *   <listitem>ATK_VALUE_VERY_GOOD</listitem>
158  *   <listitem>ATK_VALUE_BEST</listitem>
159  *   <listitem>ATK_VALUE_SUBSUBOPTIMAL</listitem>
160  *   <listitem>ATK_VALUE_SUBOPTIMAL</listitem>
161  *   <listitem>ATK_VALUE_OPTIMAL</listitem>
162  * </itemizedlist>
163  * <para>
164  * Proposals for additional constants, along with their use cases,
165  * should be submitted to the GNOME Accessibility Team.
166  * </para>
167  * </refsect1>
168  *
169  * <refsect1 id="changes">
170  * <title>On the subject of changes</title>
171  * <para>
172  * Note that if there is a textual description associated with the new
173  * numeric value, that description should be included regardless of
174  * whether or not it has also changed.
175  * </para>
176  * </refsect1>
177  */
178
179 static GPtrArray *value_type_names = NULL;
180
181 enum {
182   VALUE_CHANGED,
183   LAST_SIGNAL
184 };
185
186 /* These are listed here for extraction by intltool */
187 #if 0
188 N_("very weak")
189 N_("weak")
190 N_("acceptable")
191 N_("strong")
192 N_("very strong")
193 N_("very low")
194 N_("medium")
195 N_("high")
196 N_("very high")
197 N_("very bad")
198 N_("bad")
199 N_("good")
200 N_("very good")
201 N_("best")
202 #endif
203
204 static void atk_value_base_init (AtkValueIface *class);
205
206 static guint atk_value_signals[LAST_SIGNAL] = {0};
207
208 GType
209 atk_value_get_type (void)
210 {
211   static GType type = 0;
212
213   if (!type) {
214     GTypeInfo tinfo =
215     {
216       sizeof (AtkValueIface),
217       (GBaseInitFunc) atk_value_base_init,
218       (GBaseFinalizeFunc) NULL,
219
220     };
221
222     type = g_type_register_static (G_TYPE_INTERFACE, "AtkValue", &tinfo, 0);
223   }
224
225   return type;
226 }
227
228 static void
229 atk_value_base_init (AtkValueIface *class)
230 {
231   static gboolean initialized = FALSE;
232   if (!initialized)
233     {
234       /**
235        * AtkValue::value-changed:
236        * @atkvalue: the object on which the signal was emitted.
237        * @value: the new value in a numerical form.
238        * @text: human readable text alternative (also called
239        * description) of this object. NULL if not available.
240        *
241        * The 'value-changed' signal is emitted when the current value
242        * that represent the object changes. @value is the numerical
243        * representation of this new value.  @text is the human
244        * readable text alternative of @value, and can be NULL if it is
245        * not available. Note that if there is a textual description
246        * associated with the new numeric value, that description
247        * should be included regardless of whether or not it has also
248        * changed.
249        *
250        * Example: a password meter whose value changes as the user
251        * types their new password. Appropiate value text would be
252        * "weak", "acceptable" and "strong".
253        *
254        * Since: 2.12
255        */
256       atk_value_signals[VALUE_CHANGED] =
257         g_signal_new ("value_changed",
258                       ATK_TYPE_VALUE,
259                       G_SIGNAL_RUN_LAST,
260                       0,
261                       (GSignalAccumulator) NULL, NULL,
262                       atk_marshal_VOID__DOUBLE_STRING,
263                       G_TYPE_NONE,
264                       2, G_TYPE_DOUBLE, G_TYPE_STRING);
265
266       initialized = TRUE;
267     }
268 }
269
270 /**
271  * atk_value_get_current_value:
272  * @obj: a GObject instance that implements AtkValueIface
273  * @value: a #GValue representing the current accessible value
274  *
275  * Gets the value of this object.
276  *
277  * Deprecated: Since 2.12. Use atk_value_get_value_and_text()
278  * instead.
279  **/
280 void
281 atk_value_get_current_value (AtkValue *obj,
282                              GValue   *value)
283 {
284   AtkValueIface *iface;
285
286   g_return_if_fail (value != NULL);
287   g_return_if_fail (ATK_IS_VALUE (obj));
288
289   iface = ATK_VALUE_GET_IFACE (obj);
290
291   if (iface->get_current_value)
292     {
293       if (G_IS_VALUE (value))
294         g_value_unset (value);
295       else
296         memset (value, 0, sizeof (*value));
297
298       (iface->get_current_value) (obj, value);
299     }
300 }
301
302 /**
303  * atk_value_get_maximum_value:
304  * @obj: a GObject instance that implements AtkValueIface
305  * @value: a #GValue representing the maximum accessible value
306  *
307  * Gets the maximum value of this object.
308  *
309  * Deprecated: Since 2.12. Use atk_value_get_range() instead.
310  **/
311 void
312 atk_value_get_maximum_value  (AtkValue *obj,
313                               GValue   *value)
314 {
315   AtkValueIface *iface;
316
317   g_return_if_fail (value != NULL);
318   g_return_if_fail (ATK_IS_VALUE (obj));
319
320   iface = ATK_VALUE_GET_IFACE (obj);
321
322   if (iface->get_maximum_value)
323     {
324       if (G_IS_VALUE (value))
325         g_value_unset (value);
326       else
327         memset (value, 0, sizeof (*value));
328
329       (iface->get_maximum_value) (obj, value);
330     }
331 }
332
333 /**
334  * atk_value_get_minimum_value:
335  * @obj: a GObject instance that implements AtkValueIface
336  * @value: a #GValue representing the minimum accessible value
337  *
338  * Gets the minimum value of this object.
339  *
340  * Deprecated: Since 2.12. Use atk_value_get_range() instead.
341  **/
342 void
343 atk_value_get_minimum_value (AtkValue *obj,
344                              GValue   *value)
345 {
346   AtkValueIface *iface;
347
348   g_return_if_fail (value != NULL);
349   g_return_if_fail (ATK_IS_VALUE (obj));
350
351   iface = ATK_VALUE_GET_IFACE (obj);
352
353   if (iface->get_minimum_value)
354     {
355       if (G_IS_VALUE (value))
356         g_value_unset (value);
357       else
358         memset (value, 0, sizeof (*value));
359
360       (iface->get_minimum_value) (obj, value);
361     }
362 }
363
364 /**
365  * atk_value_get_minimum_increment:
366  * @obj: a GObject instance that implements AtkValueIface
367  * @value: a #GValue representing the minimum increment by which the accessible value may be changed
368  *
369  * Gets the minimum increment by which the value of this object may be changed.  If zero,
370  * the minimum increment is undefined, which may mean that it is limited only by the 
371  * floating point precision of the platform.
372  *
373  * Since: 1.12
374  *
375  * Deprecated: Since 2.12. Use atk_value_get_increment() instead.
376  **/
377 void
378 atk_value_get_minimum_increment (AtkValue *obj,
379                              GValue   *value)
380 {
381   AtkValueIface *iface;
382
383   g_return_if_fail (value != NULL);
384   g_return_if_fail (ATK_IS_VALUE (obj));
385
386   iface = ATK_VALUE_GET_IFACE (obj);
387
388   if (iface->get_minimum_increment)
389     {
390       if (G_IS_VALUE (value))
391         g_value_unset (value);
392       else
393         memset (value, 0, sizeof (*value));
394
395       (iface->get_minimum_increment) (obj, value);
396     }
397 }
398
399 /**
400  * atk_value_set_current_value:
401  * @obj: a GObject instance that implements AtkValueIface
402  * @value: a #GValue which is the desired new accessible value.
403  *
404  * Sets the value of this object.
405  *
406  * Returns: %TRUE if new value is successfully set, %FALSE otherwise.
407  *
408  * Deprecated: Since 2.12. Use atk_value_set_value() instead.
409  **/
410 gboolean
411 atk_value_set_current_value (AtkValue       *obj, 
412                              const GValue   *value)
413 {
414   AtkValueIface *iface;
415
416   g_return_val_if_fail (ATK_IS_VALUE (obj), FALSE);
417   g_return_val_if_fail (G_IS_VALUE (value), FALSE);
418
419   iface = ATK_VALUE_GET_IFACE (obj);
420
421   if (iface->set_current_value)
422     return (iface->set_current_value) (obj, value);
423   else
424     return FALSE;
425 }
426
427
428 /**
429  * atk_value_get_value_and_text:
430  * @obj: a GObject instance that implements AtkValueIface
431  * @value: (out): address of #gdouble to put the current value of @obj
432  * @text: (out) (allow-none): address of #gchar to put the human
433  * readable text alternative for @value
434  *
435  * Gets the current value and the human readable text alternative of
436  * @obj. @text is a newly created string, that must be freed by the
437  * caller. Can be NULL if not descriptor is available.
438  *
439  * Since: 2.12
440  **/
441
442 void
443 atk_value_get_value_and_text (AtkValue *obj,
444                               gdouble *value,
445                               gchar  **text)
446 {
447   AtkValueIface *iface;
448
449   g_return_if_fail (ATK_IS_VALUE (obj));
450
451   iface = ATK_VALUE_GET_IFACE (obj);
452
453   if (iface->get_value_and_text)
454     {
455       (iface->get_value_and_text) (obj, value, text);
456     }
457 }
458
459 /**
460  * atk_value_get_range:
461  * @obj: a GObject instance that implements AtkValueIface
462  *
463  * Gets the range of this object.
464  *
465  * Returns: (transfer full): a newly allocated #AtkRange that
466  * represents the minimum, maximum and descriptor (if available) of
467  * @obj. NULL if that range is not defined.
468  *
469  * Since: 2.12
470  **/
471 AtkRange*
472 atk_value_get_range (AtkValue *obj)
473 {
474   AtkValueIface *iface;
475
476   g_return_val_if_fail (ATK_IS_VALUE (obj), NULL);
477
478   iface = ATK_VALUE_GET_IFACE (obj);
479
480   if (iface->get_range)
481     {
482       return (iface->get_range) (obj);
483     }
484   else
485     return NULL;
486 }
487
488 /**
489  * atk_value_get_increment:
490  * @obj: a GObject instance that implements AtkValueIface
491  *
492  * Gets the minimum increment by which the value of this object may be
493  * changed.  If zero, the minimum increment is undefined, which may
494  * mean that it is limited only by the floating point precision of the
495  * platform.
496  *
497  * Return value: the minimum increment by which the value of this
498  * object may be changed. zero if undefined.
499  *
500  * Since: 2.12
501  **/
502 gdouble
503 atk_value_get_increment (AtkValue *obj)
504 {
505   AtkValueIface *iface;
506
507   g_return_val_if_fail (ATK_IS_VALUE (obj), 0);
508
509   iface = ATK_VALUE_GET_IFACE (obj);
510
511   if (iface->get_increment)
512     {
513       return (iface->get_increment) (obj);
514     }
515   else
516     return 0;
517 }
518
519
520 /**
521  * atk_value_get_sub_ranges:
522  * @obj: a GObject instance that implements AtkValueIface
523  *
524  * Gets the list of subranges defined for this object. See #AtkValue
525  * introduction for examples of subranges and when to expose them.
526  *
527  * Returns: (element-type AtkRange) (transfer full): an #GSList of
528  * #AtkRange which each of the subranges defined for this object. Free
529  * the returns list with g_slist_free().
530  *
531  * Since: 2.12
532  **/
533 GSList*
534 atk_value_get_sub_ranges (AtkValue *obj)
535 {
536   AtkValueIface *iface;
537
538   g_return_val_if_fail (ATK_IS_VALUE (obj), NULL);
539
540   iface = ATK_VALUE_GET_IFACE (obj);
541
542   if (iface->get_sub_ranges)
543     {
544       return (iface->get_sub_ranges) (obj);
545     }
546   else
547     return NULL;
548 }
549
550 /**
551  * atk_value_set_value:
552  * @obj: a GObject instance that implements AtkValueIface
553  * @new_value: a double which is the desired new accessible value.
554  *
555  * Sets the value of this object.
556  *
557  * This method is intended to provide a way to change the value of the
558  * object. In any case, it is possible that the value can't be
559  * modified (ie: a read-only component). If the value changes due this
560  * call, it is possible that the text could change, and will trigger
561  * an #AtkValue::value-changed signal emission.
562  *
563  * Note for implementors: the deprecated atk_value_set_current_value()
564  * method returned TRUE or FALSE depending if the value was assigned
565  * or not. In the practice several implementors were not able to
566  * decide it, and returned TRUE in any case. For that reason it is not
567  * required anymore to return if the value was properly assigned or
568  * not.
569  *
570  * Since: 2.12
571  **/
572 void
573 atk_value_set_value (AtkValue     *obj,
574                      const gdouble new_value)
575 {
576   AtkValueIface *iface;
577
578   g_return_if_fail (ATK_IS_VALUE (obj));
579
580   iface = ATK_VALUE_GET_IFACE (obj);
581
582   if (iface->set_value)
583     {
584       (iface->set_value) (obj, new_value);
585     }
586 }
587
588 static void
589 initialize_value_type_names ()
590 {
591   GTypeClass *enum_class;
592   GEnumValue *enum_value;
593   int i;
594   gchar *value_type_name = NULL;
595
596   if (value_type_names)
597     return;
598
599   value_type_names = g_ptr_array_new ();
600   enum_class = g_type_class_ref (ATK_TYPE_VALUE_TYPE);
601   if (!G_IS_ENUM_CLASS(enum_class))
602     return;
603
604   for (i = 0; i < ATK_VALUE_LAST_DEFINED; i++)
605     {
606       enum_value = g_enum_get_value (G_ENUM_CLASS (enum_class), i);
607       value_type_name = g_strdup (enum_value->value_nick);
608       _compact_name (value_type_name);
609       g_ptr_array_add (value_type_names, value_type_name);
610     }
611
612   g_type_class_unref (enum_class);
613 }
614
615 /**
616  * atk_value_type_get_name:
617  * @role: The #AtkValueType whose name is required
618  *
619  * Gets the description string describing the #AtkValueType @value_type.
620  *
621  * Returns: the string describing the #AtkValueType
622  */
623 const gchar*
624 atk_value_type_get_name (AtkValueType value_type)
625 {
626   g_return_val_if_fail (value_type >= 0, NULL);
627
628   if (!value_type_names)
629     initialize_value_type_names ();
630
631   if (value_type < value_type_names->len)
632     return g_ptr_array_index (value_type_names, value_type);
633
634   return NULL;
635 }
636
637 /**
638  * atk_value_type_get_localized_name:
639  * @value_type: The #AtkValueType whose localized name is required
640  *
641  * Gets the localized description string describing the #AtkValueType @value_type.
642  *
643  * Returns: the localized string describing the #AtkValueType
644  **/
645 const gchar*
646 atk_value_type_get_localized_name (AtkValueType value_type)
647 {
648   _gettext_initialization ();
649
650   return dgettext (GETTEXT_PACKAGE, atk_value_type_get_name (value_type));
651 }