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