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
 * AtkState:
28
 *
29
 * An AtkState describes a single state of an object.
30
 *
31
 * An AtkState describes a single state of an object. The full set of states
32
 * that apply to an object at a given time are contained in its #AtkStateSet.
33
 *
34
 * See [id@atk_object_ref_state_set] and [id@atk_object_notify_state_change]
35
 */
36

            
37
static guint last_type = ATK_STATE_LAST_DEFINED;
38

            
39
#define NUM_POSSIBLE_STATES (sizeof (AtkState) * 8)
40

            
41
static gchar *state_names[NUM_POSSIBLE_STATES];
42

            
43
/**
44
 * atk_state_type_register:
45
 * @name: a character string describing the new state.
46
 *
47
 * Register a new object state.
48
 *
49
 * Returns: an #AtkState value for the new state.
50
 **/
51
AtkStateType
52
1
atk_state_type_register (const gchar *name)
53
{
54
1
  g_return_val_if_fail (name, ATK_STATE_INVALID);
55

            
56
1
  if (last_type < NUM_POSSIBLE_STATES - 1)
57
    {
58
1
      state_names[++last_type] = g_strdup (name);
59
1
      return (last_type);
60
    }
61
  return ATK_STATE_INVALID; /* caller needs to check */
62
}
63

            
64
/**
65
 * atk_state_type_get_name:
66
 * @type: The #AtkStateType whose name is required
67
 *
68
 * Gets the description string describing the #AtkStateType @type.
69
 *
70
 * Returns: the string describing the AtkStateType
71
 */
72
const gchar *
73
4
atk_state_type_get_name (AtkStateType type)
74
{
75
  GTypeClass *type_class;
76
  GEnumValue *value;
77
4
  const gchar *name = NULL;
78

            
79
4
  type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
80
4
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);
81

            
82
4
  value = g_enum_get_value (G_ENUM_CLASS (type_class), type);
83

            
84
4
  if (value)
85
    {
86
2
      name = value->value_nick;
87
    }
88
  else
89
    {
90
2
      if (type <= last_type)
91
        {
92
          if (type >= 0)
93
1
            name = state_names[type];
94
        }
95
    }
96

            
97
4
  g_type_class_unref (type_class);
98

            
99
4
  return name;
100
}
101

            
102
/**
103
 * atk_state_type_for_name:
104
 * @name: a character string state name
105
 *
106
 * Gets the #AtkStateType corresponding to the description string @name.
107
 *
108
 * Returns: an #AtkStateType corresponding to @name
109
 */
110
AtkStateType
111
274
atk_state_type_for_name (const gchar *name)
112
{
113
  GTypeClass *type_class;
114
  GEnumValue *value;
115
274
  AtkStateType type = ATK_STATE_INVALID;
116

            
117
274
  g_return_val_if_fail (name, ATK_STATE_INVALID);
118

            
119
274
  type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
120
274
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_STATE_INVALID);
121

            
122
274
  value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);
123

            
124
274
  if (value)
125
    {
126
272
      type = value->value;
127
    }
128
  else
129
    {
130
      gint i;
131

            
132
3
      for (i = ATK_STATE_LAST_DEFINED + 1; i <= last_type; i++)
133
        {
134
2
          if (state_names[i] == NULL)
135
            continue;
136
2
          if (!strcmp (name, state_names[i]))
137
            {
138
1
              type = i;
139
1
              break;
140
            }
141
        }
142
    }
143
274
  return type;
144
}