]> Pileus Git - ~andy/gtk/blob - modules/other/gail/gailmenu.c
Use G_DEFINE_TYPE[_WITH_CODE] instead of hand-coding the get_type functions. Bug...
[~andy/gtk] / modules / other / gail / gailmenu.c
1 /* GAIL - The GNOME Accessibility Implementation Library
2  * Copyright 2001, 2002, 2003 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 <gtk/gtkmenu.h>
21 #include <gtk/gtkmenuitem.h>
22 #include <gtk/gtkoptionmenu.h>
23 #include <gtk/gtkcombobox.h>
24 #include "gailmenu.h"
25
26 static void gail_menu_class_init (GailMenuClass *klass);
27 static void gail_menu_init       (GailMenu      *accessible);
28
29 static void       gail_menu_real_initialize     (AtkObject *obj,
30                                                  gpointer  data);
31
32 static AtkObject* gail_menu_get_parent          (AtkObject *accessible);
33 static gint       gail_menu_get_index_in_parent (AtkObject *accessible);
34
35 G_DEFINE_TYPE (GailMenu, gail_menu, GAIL_TYPE_MENU_SHELL)
36
37 static void
38 gail_menu_class_init (GailMenuClass *klass)
39 {
40   AtkObjectClass *class = ATK_OBJECT_CLASS (klass);
41
42   class->get_parent = gail_menu_get_parent;
43   class->get_index_in_parent = gail_menu_get_index_in_parent;
44   class->initialize = gail_menu_real_initialize;
45 }
46
47 static void
48 gail_menu_init (GailMenu *accessible)
49 {
50 }
51
52 AtkObject*
53 gail_menu_new (GtkWidget *widget)
54 {
55   GObject *object;
56   AtkObject *accessible;
57
58   g_return_val_if_fail (GTK_IS_MENU (widget), NULL);
59
60   object = g_object_new (GAIL_TYPE_MENU, NULL);
61
62   accessible = ATK_OBJECT (object);
63   atk_object_initialize (accessible, widget);
64
65   g_object_set_data (G_OBJECT (accessible), "atk-component-layer",
66                      GINT_TO_POINTER (ATK_LAYER_POPUP));
67   return accessible;
68 }
69
70 static void
71 gail_menu_real_initialize (AtkObject *obj,
72                            gpointer  data)
73 {
74   ATK_OBJECT_CLASS (gail_menu_parent_class)->initialize (obj, data);
75
76   obj->role = ATK_ROLE_MENU;
77 }
78
79 static AtkObject*
80 gail_menu_get_parent (AtkObject *accessible)
81 {
82   AtkObject *parent;
83
84   parent = accessible->accessible_parent;
85
86   if (parent != NULL)
87     {
88       g_return_val_if_fail (ATK_IS_OBJECT (parent), NULL);
89     }
90   else
91     {
92       GtkWidget *widget, *parent_widget;
93
94       widget = GTK_ACCESSIBLE (accessible)->widget;
95       if (widget == NULL)
96         {
97           /*
98            * State is defunct
99            */
100           return NULL;
101         }
102       g_return_val_if_fail (GTK_IS_MENU (widget), NULL);
103
104       /*
105        * If the menu is attached to a menu item or a button (Gnome Menu)
106        * report the menu item as parent.
107        */
108       parent_widget = gtk_menu_get_attach_widget (GTK_MENU (widget));
109
110       if (!GTK_IS_MENU_ITEM (parent_widget) && !GTK_IS_BUTTON (parent_widget) && !GTK_IS_COMBO_BOX (parent_widget) && !GTK_IS_OPTION_MENU (parent_widget))
111         parent_widget = widget->parent;
112
113       if (parent_widget == NULL)
114         return NULL;
115
116       parent = gtk_widget_get_accessible (parent_widget);
117       atk_object_set_parent (accessible, parent);
118     }
119   return parent;
120 }
121
122 static gint
123 gail_menu_get_index_in_parent (AtkObject *accessible)
124 {
125   GtkWidget *widget;
126
127   widget = GTK_ACCESSIBLE (accessible)->widget;
128
129   if (widget == NULL)
130     {
131       /*
132        * State is defunct
133        */
134       return -1;
135     }
136   g_return_val_if_fail (GTK_IS_MENU (widget), -1);
137
138   if (gtk_menu_get_attach_widget (GTK_MENU (widget)))
139     {
140       return 0;
141     }
142   return ATK_OBJECT_CLASS (gail_menu_parent_class)->get_index_in_parent (accessible);
143 }