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