summaryrefslogtreecommitdiff
path: root/atk/atkstate.c
blob: 5d5bd1752017518ef4252dce869fdcabfad5f7a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* ATK -  Accessibility Toolkit
 * Copyright 2001 Sun Microsystems Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "atk.h"

#include <string.h>

/**
 * SECTION:atkstate
 * @Short_description: An AtkState describes a single state of an object.
 * @Title:AtkState
 *
 * An AtkState describes a single state of an object. The full set of states
 * that apply to an object at a given time are contained in its #AtkStateSet.
 * See also #atk_object_ref_state_set and #atk_object_notify_state_change.
 */

static guint last_type = ATK_STATE_LAST_DEFINED;

#define NUM_POSSIBLE_STATES               (sizeof(AtkState)*8)

static gchar* state_names[NUM_POSSIBLE_STATES];

/**
 * atk_state_type_register:
 * @name: a character string describing the new state.
 *
 * Register a new object state.
 *
 * Returns: an #AtkState value for the new state.
 **/
AtkStateType
atk_state_type_register (const gchar *name)
{
  g_return_val_if_fail (name, ATK_STATE_INVALID);

  if (last_type < NUM_POSSIBLE_STATES -1)
    {
      state_names[++last_type] = g_strdup (name); 
      return (last_type);
    }
  return ATK_STATE_INVALID; /* caller needs to check */
}

/**
 * atk_state_type_get_name:
 * @type: The #AtkStateType whose name is required
 *
 * Gets the description string describing the #AtkStateType @type.
 *
 * Returns: the string describing the AtkStateType
 */
const gchar*
atk_state_type_get_name (AtkStateType type)
{
  GTypeClass *type_class;
  GEnumValue *value;
  const gchar *name = NULL;

  type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), NULL);

  value = g_enum_get_value (G_ENUM_CLASS (type_class), type);

  if (value)
    {
      name = value->value_nick;
    }
  else
    {
      if (type <= last_type)
        {
          if (type >= 0)
            name = state_names[type];
        }
    }

  g_type_class_unref (type_class);

  return name;
}

/**
 * atk_state_type_for_name:
 * @name: a character string state name
 *
 * Gets the #AtkStateType corresponding to the description string @name.
 *
 * Returns: an #AtkStateType corresponding to @name 
 */
AtkStateType
atk_state_type_for_name (const gchar *name)
{
  GTypeClass *type_class;
  GEnumValue *value;
  AtkStateType type = ATK_STATE_INVALID;

  g_return_val_if_fail (name, ATK_STATE_INVALID);

  type_class = g_type_class_ref (ATK_TYPE_STATE_TYPE);
  g_return_val_if_fail (G_IS_ENUM_CLASS (type_class), ATK_STATE_INVALID);

  value = g_enum_get_value_by_nick (G_ENUM_CLASS (type_class), name);

  if (value)
    {
      type = value->value;
    }
  else
    {
      gint i;

      for (i = ATK_STATE_LAST_DEFINED + 1; i <= last_type; i++)
        {
          if (state_names[i] == NULL)
            continue; 
          if (!strcmp(name, state_names[i])) 
            {
              type = i;
              break;
            }
        }
    }
  return type;
}