581b64194bd4bfa0abd1e34c4c90474a307e69e1
[platform/upstream/atk.git] / atk / atkstate.c
1 /* ATK -  Accessibility Toolkit
2  * Copyright 2001 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 "atk.h"
21
22 #include <string.h>
23
24 /**
25  * SECTION:atkstate
26  * @Short_description: An AtkState describes a component's particular state.
27  * @Title:AtkState
28  *
29  * An AtkState describes a component's particular state. The actual
30  * state of an component is described by its AtkStateSet, which is a
31  * set of AtkStates.
32  */
33
34 static guint last_type = ATK_STATE_LAST_DEFINED;
35
36 #define NUM_POSSIBLE_STATES               (sizeof(AtkState)*8)
37
38 static gchar* state_names[NUM_POSSIBLE_STATES];
39
40 /**
41  * atk_state_type_register:
42  * @name: a character string describing the new state.
43  *
44  * Register a new object state.
45  *
46  * Returns: an #AtkState value for the new state.
47  **/
48 AtkStateType
49 atk_state_type_register (const gchar *name)
50 {
51   g_return_val_if_fail (name, ATK_STATE_INVALID);
52
53   if (last_type < NUM_POSSIBLE_STATES -1)
54     {
55       state_names[++last_type] = g_strdup (name); 
56       return (last_type);
57     }
58   return ATK_STATE_INVALID; /* caller needs to check */
59 }
60
61 /**
62  * atk_state_type_get_name:
63  * @type: The #AtkStateType whose name is required
64  *
65  * Gets the description string describing the #AtkStateType @type.
66  *
67  * Returns: the string describing the AtkStateType
68  */
69 const gchar*
70 atk_state_type_get_name (AtkStateType type)
71 {
72   GTypeClass *type_class;
73   GEnumValue *value;
74   const gchar *name = NULL;
75
76   type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
77   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
78
79   value = g_enum_get_value (G_ENUM_CLASS (type_class), type);
80
81   if (value)
82     {
83       name = value->value_nick;
84     }
85   else
86     {
87       if (type <= last_type)
88         {
89           if (type >= 0)
90             name = state_names[type];
91         }
92     }
93
94   return name;
95 }
96
97 /**
98  * atk_state_type_for_name:
99  * @name: a character string state name
100  *
101  * Gets the #AtkStateType corresponding to the description string @name.
102  *
103  * Returns: an #AtkStateType corresponding to @name 
104  */
105 AtkStateType
106 atk_state_type_for_name (const gchar *name)
107 {
108   GTypeClass *type_class;
109   GEnumValue *value;
110   AtkStateType type = ATK_STATE_INVALID;
111
112   g_return_val_if_fail (name, ATK_STATE_INVALID);
113
114   type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
115   g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_STATE_INVALID);
116
117   value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
118
119   if (value)
120     {
121       type = value->value;
122     }
123   else
124     {
125       gint i;
126
127       for (i = ATK_STATE_LAST_DEFINED + 1; i <= last_type; i++)
128         {
129           if (state_names[i] == NULL)
130             continue; 
131           if (!strcmp(name, state_names[i])) 
132             {
133               type = i;
134               break;
135             }
136         }
137     }
138   return type;
139 }