Back to index

unity  6.0.0
nux-base-window-accessible.cpp
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: Alejandro PiƱeiro Iglesias <apinheiro@igalia.com>
00017  */
00018 
00044 #include "nux-base-window-accessible.h"
00045 
00046 #include <Nux/Area.h>
00047 #include <Nux/Layout.h>
00048 
00049 /* GObject */
00050 static void nux_base_window_accessible_class_init(NuxBaseWindowAccessibleClass* klass);
00051 static void nux_base_window_accessible_init(NuxBaseWindowAccessible* base_window_accessible);
00052 
00053 /* AtkObject.h */
00054 static void       nux_base_window_accessible_initialize(AtkObject* accessible,
00055                                                         gpointer   data);
00056 static AtkObject* nux_base_window_accessible_get_parent(AtkObject* obj);
00057 static AtkStateSet* nux_base_window_accessible_ref_state_set(AtkObject* obj);
00058 
00059 /* AtkWindow.h */
00060 static void         atk_window_interface_init(AtkWindowIface* iface);
00061 
00062 
00063 G_DEFINE_TYPE_WITH_CODE(NuxBaseWindowAccessible, nux_base_window_accessible,
00064                         NUX_TYPE_VIEW_ACCESSIBLE,
00065                         G_IMPLEMENT_INTERFACE(ATK_TYPE_WINDOW,
00066                                               atk_window_interface_init))
00067 
00068 struct _NuxBaseWindowAccessiblePrivate
00069 {
00070   /* Cached values (used to avoid extra notifications) */
00071   gboolean active;
00072 };
00073 
00074 #define NUX_BASE_WINDOW_ACCESSIBLE_GET_PRIVATE(obj) \
00075   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), NUX_TYPE_BASE_WINDOW_ACCESSIBLE, \
00076                                 NuxBaseWindowAccessiblePrivate))
00077 
00078 static void
00079 nux_base_window_accessible_class_init(NuxBaseWindowAccessibleClass* klass)
00080 {
00081   GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
00082   AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);
00083 
00084   /* AtkObject */
00085   atk_class->initialize = nux_base_window_accessible_initialize;
00086   atk_class->get_parent = nux_base_window_accessible_get_parent;
00087   atk_class->ref_state_set = nux_base_window_accessible_ref_state_set;
00088 
00089   g_type_class_add_private(gobject_class, sizeof(NuxBaseWindowAccessiblePrivate));
00090 }
00091 
00092 static void
00093 nux_base_window_accessible_init(NuxBaseWindowAccessible* base_window_accessible)
00094 {
00095   NuxBaseWindowAccessiblePrivate* priv =
00096     NUX_BASE_WINDOW_ACCESSIBLE_GET_PRIVATE(base_window_accessible);
00097 
00098   base_window_accessible->priv = priv;
00099 }
00100 
00101 AtkObject*
00102 nux_base_window_accessible_new(nux::Object* object)
00103 {
00104   AtkObject* accessible = NULL;
00105 
00106   g_return_val_if_fail(dynamic_cast<nux::BaseWindow*>(object), NULL);
00107 
00108   accessible = ATK_OBJECT(g_object_new(NUX_TYPE_BASE_WINDOW_ACCESSIBLE, NULL));
00109 
00110   atk_object_initialize(accessible, object);
00111 
00112   return accessible;
00113 }
00114 
00115 /* AtkObject.h */
00116 static void
00117 nux_base_window_accessible_initialize(AtkObject* accessible,
00118                                       gpointer data)
00119 {
00120   ATK_OBJECT_CLASS(nux_base_window_accessible_parent_class)->initialize(accessible, data);
00121 
00122   atk_object_set_role(accessible, ATK_ROLE_WINDOW);
00123 }
00124 
00125 static AtkObject*
00126 nux_base_window_accessible_get_parent(AtkObject* obj)
00127 {
00128   return atk_get_root();
00129 }
00130 
00131 static AtkStateSet*
00132 nux_base_window_accessible_ref_state_set(AtkObject* obj)
00133 {
00134   AtkStateSet* state_set = NULL;
00135   NuxBaseWindowAccessible* self = NULL;
00136   nux::Object* nux_object = NULL;
00137 
00138   g_return_val_if_fail(NUX_IS_BASE_WINDOW_ACCESSIBLE(obj), NULL);
00139 
00140   state_set = ATK_OBJECT_CLASS(nux_base_window_accessible_parent_class)->ref_state_set(obj);
00141 
00142   nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj));
00143   if (nux_object == NULL) /* defunct */
00144     return state_set;
00145 
00146   self = NUX_BASE_WINDOW_ACCESSIBLE(obj);
00147 
00148   atk_state_set_add_state(state_set, ATK_STATE_FOCUSABLE);
00149 
00150   /* HasKeyboardFocus is not a reliable here:
00151      see bug https://bugs.launchpad.net/nux/+bug/745049 */
00152   if (self->priv->active)
00153   {
00154     atk_state_set_add_state(state_set, ATK_STATE_ACTIVE);
00155     atk_state_set_add_state(state_set, ATK_STATE_FOCUSED);
00156   }
00157 
00158   return state_set;
00159 }
00160 
00161 /* AtkWindow */
00162 static void
00163 atk_window_interface_init(AtkWindowIface* iface)
00164 {
00165   /* AtkWindow just define signals at this moment */
00166 }
00167 
00168 /* public */
00169 /*
00170  * Checks if we are the active window.
00171  */
00172 void
00173 nux_base_window_accessible_check_active(NuxBaseWindowAccessible* self,
00174                                         nux::BaseWindow* active_window)
00175 {
00176   const gchar* signal_name;
00177   gboolean is_active;
00178   nux::Object* nux_object = NULL;
00179   nux::BaseWindow* bwindow = NULL;
00180 
00181   g_return_if_fail(NUX_IS_BASE_WINDOW_ACCESSIBLE(self));
00182 
00183   nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(self));
00184   bwindow = dynamic_cast<nux::BaseWindow*>(nux_object);
00185   if (bwindow == NULL) /* defunct */
00186     return;
00187 
00188   is_active = (bwindow == active_window);
00189 
00190   if (self->priv->active != is_active)
00191   {
00192     self->priv->active = is_active;
00193 
00194     if (is_active)
00195       signal_name = "activate";
00196     else
00197       signal_name = "deactivate";
00198 
00199     atk_object_notify_state_change(ATK_OBJECT(self),
00200                                    ATK_STATE_ACTIVE, is_active);
00201     g_signal_emit_by_name(self, signal_name, 0);
00202   }
00203 }