Back to index

unity  6.0.0
panel-indicator-entry-accessible.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Canonical Ltd
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU General Public License version 3 as
00006  * published by the Free Software Foundation.
00007  *
00008  * This program is distributed in the hope that it will be useful,
00009  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  * GNU General Public License for more details.
00012  *
00013  * You should have received a copy of the GNU General Public License
00014  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015  *
00016  * Authored by: Rodrigo Moya <rodrigo.moya@canonical.com>
00017  */
00018 
00019 #include "panel-indicator-entry-accessible.h"
00020 #include "panel-service.h"
00021 
00022 /* AtkObject methods */
00023 static void         piea_component_interface_init                   (AtkComponentIface *iface);
00024 
00025 static void         panel_indicator_entry_accessible_initialize     (AtkObject *accessible, gpointer data);
00026 static gint         panel_indicator_entry_accessible_get_n_children (AtkObject *accessible);
00027 static AtkObject   *panel_indicator_entry_accessible_ref_child      (AtkObject *accessible, gint i);
00028 static AtkStateSet *panel_indicator_entry_accessible_ref_state_set  (AtkObject *accessible);
00029 
00030 struct _PanelIndicatorEntryAccessiblePrivate
00031 {
00032   IndicatorObjectEntry *entry;
00033   PanelService         *service;
00034   gint                  x;
00035   gint                  y;
00036   gint                  width;
00037   gint                  height;
00038   gboolean              active;
00039 };
00040 
00041 G_DEFINE_TYPE_WITH_CODE(PanelIndicatorEntryAccessible,
00042                      panel_indicator_entry_accessible,
00043                      ATK_TYPE_OBJECT,
00044                      G_IMPLEMENT_INTERFACE (ATK_TYPE_COMPONENT, piea_component_interface_init))
00045 
00046 #define GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), PANEL_TYPE_INDICATOR_ENTRY_ACCESSIBLE, PanelIndicatorEntryAccessiblePrivate))
00047 
00048 static void
00049 on_entry_activated_cb (PanelService *service, const gchar *entry_id,
00050                        gint x, gint y, guint w, guint h, gpointer user_data)
00051 {
00052   gchar *s;
00053   gboolean adding = FALSE;
00054   PanelIndicatorEntryAccessible *piea;
00055 
00056   g_return_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (user_data));
00057 
00058   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (user_data);
00059 
00060   /* The PanelService sends us a string containing the pointer to the IndicatorObjectEntry */
00061   s = g_strdup_printf ("%p", piea->priv->entry);
00062   if (g_str_equal (s, entry_id))
00063     {
00064       adding = TRUE;
00065       piea->priv->active = TRUE;
00066     }
00067   else
00068     {
00069       piea->priv->active = FALSE;
00070     }
00071 
00072   /* Notify AT's about the states' changes */
00073   atk_object_notify_state_change (ATK_OBJECT (piea), ATK_STATE_ACTIVE, adding);
00074   atk_object_notify_state_change (ATK_OBJECT (piea), ATK_STATE_FOCUSED, adding);
00075   atk_object_notify_state_change (ATK_OBJECT (piea), ATK_STATE_SHOWING, adding);
00076 
00077   g_free (s);
00078 }
00079 
00080 static void
00081 on_geometries_changed_cb (PanelService *service,
00082                        IndicatorObject *object,
00083                        IndicatorObjectEntry *entry,
00084                        gint x,
00085                        gint y,
00086                        gint width,
00087                        gint height,
00088                        gpointer user_data)
00089 {
00090   PanelIndicatorEntryAccessible *piea;
00091   AtkRectangle rect;
00092 
00093   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (user_data);
00094 
00095   g_return_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (piea));
00096 
00097   if (entry != piea->priv->entry)
00098     return;
00099 
00100   piea->priv->x = x;
00101   piea->priv->y = y;
00102   piea->priv->width = width;
00103   piea->priv->height = height;
00104 
00105   /* Notify ATK objects of change of coordinates */
00106   rect.x = piea->priv->x;
00107   rect.y = piea->priv->y;
00108   rect.width = piea->priv->width;
00109   rect.height = piea->priv->height;
00110   g_signal_emit_by_name (ATK_COMPONENT (piea), "bounds-changed", &rect);
00111 }
00112 
00113 static void
00114 panel_indicator_entry_accessible_finalize (GObject *object)
00115 {
00116   PanelIndicatorEntryAccessible *piea;
00117 
00118   g_return_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (object));
00119 
00120   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (object);
00121 
00122   if (piea->priv != NULL)
00123     {
00124       g_signal_handlers_disconnect_by_func (piea->priv->service, on_entry_activated_cb, piea);
00125       g_signal_handlers_disconnect_by_func (piea->priv->service, on_geometries_changed_cb, piea);
00126     }
00127 
00128   G_OBJECT_CLASS (panel_indicator_entry_accessible_parent_class)->finalize (object);
00129 }
00130 
00131 static void
00132 panel_indicator_entry_accessible_class_init (PanelIndicatorEntryAccessibleClass *klass)
00133 {
00134   GObjectClass *object_class;
00135   AtkObjectClass *atk_class;
00136 
00137   /* GObject */
00138   object_class = G_OBJECT_CLASS (klass);
00139   object_class->finalize = panel_indicator_entry_accessible_finalize;
00140 
00141   /* AtkObject */
00142   atk_class = ATK_OBJECT_CLASS (klass);
00143   atk_class->initialize = panel_indicator_entry_accessible_initialize;
00144   atk_class->get_n_children = panel_indicator_entry_accessible_get_n_children;
00145   atk_class->ref_child = panel_indicator_entry_accessible_ref_child;
00146   atk_class->ref_state_set = panel_indicator_entry_accessible_ref_state_set;
00147 
00148   g_type_class_add_private (object_class, sizeof (PanelIndicatorEntryAccessiblePrivate));
00149 }
00150 
00151 static void
00152 panel_indicator_entry_accessible_init (PanelIndicatorEntryAccessible *piea)
00153 {
00154   piea->priv = GET_PRIVATE (piea);
00155   piea->priv->x = piea->priv->y = piea->priv->width = piea->priv->height = 0;
00156 
00157   /* Set up signals for listening to service changes */
00158   piea->priv->active = FALSE;
00159   piea->priv->service = panel_service_get_default ();
00160   g_signal_connect (piea->priv->service, "geometries-changed",
00161                   G_CALLBACK (on_geometries_changed_cb), piea);
00162   g_signal_connect (piea->priv->service, "entry-activated",
00163                   G_CALLBACK (on_entry_activated_cb), piea);
00164 }
00165 
00166 AtkObject *
00167 panel_indicator_entry_accessible_new (IndicatorObjectEntry *entry)
00168 {
00169   PanelIndicatorEntryAccessible *piea;
00170 
00171   piea = g_object_new (PANEL_TYPE_INDICATOR_ENTRY_ACCESSIBLE, NULL);
00172   atk_object_initialize (ATK_OBJECT (piea), entry);
00173 
00174   return ATK_OBJECT (piea);
00175 }
00176 
00177 IndicatorObjectEntry *
00178 panel_indicator_entry_accessible_get_entry (PanelIndicatorEntryAccessible *piea)
00179 {
00180   g_return_val_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (piea), NULL);
00181 
00182   return piea->priv->entry;
00183 }
00184 
00185 /* Implementation of AtkObject methods */
00186 
00187 static void
00188 panel_indicator_entry_accessible_get_extents (AtkComponent *component,
00189                                          gint *x,
00190                                          gint *y,
00191                                          gint *width,
00192                                          gint *height,
00193                                          AtkCoordType coord_type)
00194 {
00195   PanelIndicatorEntryAccessible *piea;
00196 
00197   g_return_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (component));
00198 
00199   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (component);
00200 
00201   /* We ignore AtkCoordType for now, as the panel is always at the top left
00202      corner and so relative and absolute coordinates are the same */
00203   *x = piea->priv->x;
00204   *y = piea->priv->y;
00205   *width = piea->priv->width;
00206   *height = piea->priv->height;
00207 }
00208 
00209 static void
00210 piea_component_interface_init (AtkComponentIface *iface)
00211 {
00212   g_return_if_fail (iface != NULL);
00213 
00214   iface->get_extents = panel_indicator_entry_accessible_get_extents;
00215 }
00216 
00217 static void
00218 panel_indicator_entry_accessible_initialize (AtkObject *accessible, gpointer data)
00219 {
00220   PanelIndicatorEntryAccessible *piea;
00221 
00222   g_return_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (accessible));
00223 
00224   ATK_OBJECT_CLASS (panel_indicator_entry_accessible_parent_class)->initialize (accessible, data);
00225 
00226   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (accessible);
00227   piea->priv->entry = (IndicatorObjectEntry *) data;
00228 
00229   if (GTK_IS_LABEL (piea->priv->entry->label))
00230     {
00231       atk_object_set_role (accessible, ATK_ROLE_LABEL);
00232       atk_object_set_name (accessible,
00233                         gtk_label_get_text (piea->priv->entry->label));
00234     }
00235   if (GTK_IS_IMAGE (piea->priv->entry->image))
00236     {
00237       atk_object_set_role (accessible, ATK_ROLE_IMAGE);
00238       if (piea->priv->entry->accessible_desc != NULL)
00239         atk_object_set_name (accessible, piea->priv->entry->accessible_desc);
00240     }
00241 
00242   if (piea->priv->entry->accessible_desc != NULL)
00243     atk_object_set_description (accessible, piea->priv->entry->accessible_desc);
00244 }
00245 
00246 static gint
00247 panel_indicator_entry_accessible_get_n_children (AtkObject *accessible)
00248 {
00249   PanelIndicatorEntryAccessible *piea;
00250   gint n_children = 0;
00251 
00252   g_return_val_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (accessible), 0);
00253 
00254   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (accessible);
00255 
00256   if (piea->priv->entry->parent_object && GTK_IS_MENU (piea->priv->entry->menu))
00257     n_children = 1;
00258 
00259   return n_children;
00260 }
00261 
00262 static AtkObject *
00263 panel_indicator_entry_accessible_ref_child (AtkObject *accessible, gint i)
00264 {
00265   PanelIndicatorEntryAccessible *piea;
00266   AtkObject *child = NULL;
00267 
00268   g_return_val_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (accessible), NULL);
00269 
00270   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (accessible);
00271   if (piea->priv->entry->parent_object && GTK_IS_MENU (piea->priv->entry->menu))
00272     {
00273       child = gtk_widget_get_accessible (GTK_WIDGET (piea->priv->entry->menu));
00274       atk_object_set_parent (child, accessible);
00275     }
00276 
00277   if (child != NULL)
00278     return g_object_ref (child);
00279   else
00280     return NULL;
00281 }
00282 
00283 static AtkStateSet *
00284 panel_indicator_entry_accessible_ref_state_set  (AtkObject *accessible)
00285 {
00286   AtkStateSet *state_set;
00287   PanelIndicatorEntryAccessible *piea;
00288 
00289   g_return_val_if_fail (PANEL_IS_INDICATOR_ENTRY_ACCESSIBLE (accessible), NULL);
00290 
00291   piea = PANEL_INDICATOR_ENTRY_ACCESSIBLE (accessible);
00292 
00293   /* Retrieve state_set from parent_class */
00294   state_set = ATK_OBJECT_CLASS (panel_indicator_entry_accessible_parent_class)->ref_state_set (accessible);
00295 
00296   atk_state_set_add_state (state_set, ATK_STATE_ENABLED);
00297   atk_state_set_add_state (state_set, ATK_STATE_FOCUSABLE);
00298   atk_state_set_add_state (state_set, ATK_STATE_HORIZONTAL);
00299   atk_state_set_add_state (state_set, ATK_STATE_SENSITIVE);
00300   atk_state_set_add_state (state_set, ATK_STATE_VISIBLE);
00301 
00302   if (piea->priv->active)
00303     {
00304       atk_state_set_add_state (state_set, ATK_STATE_ACTIVE);
00305       atk_state_set_add_state (state_set, ATK_STATE_FOCUSED);
00306       atk_state_set_add_state (state_set, ATK_STATE_SHOWING);
00307     }
00308 
00309   return state_set;
00310 }