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