Back to index

unity  6.0.0
unity-sctext-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 
00031 #include <glib/gi18n.h>
00032 #include <pango/pango.h>
00033 #include <pango/pangocairo.h>
00034 
00035 #include "unity-sctext-accessible.h"
00036 
00037 #include "unitya11y.h"
00038 #include "StaticCairoText.h"
00039 
00040 /* GObject */
00041 static void unity_sctext_accessible_class_init(UnitySctextAccessibleClass* klass);
00042 static void unity_sctext_accessible_init(UnitySctextAccessible* self);
00043 
00044 /* AtkObject.h */
00045 static void         unity_sctext_accessible_initialize(AtkObject* accessible,
00046                                                        gpointer   data);
00047 static const gchar* unity_sctext_accessible_get_name(AtkObject* obj);
00048 
00049 G_DEFINE_TYPE(UnitySctextAccessible, unity_sctext_accessible,  NUX_TYPE_VIEW_ACCESSIBLE);
00050 
00051 
00052 #define UNITY_SCTEXT_ACCESSIBLE_GET_PRIVATE(obj)                      \
00053   (G_TYPE_INSTANCE_GET_PRIVATE ((obj), UNITY_TYPE_SCTEXT_ACCESSIBLE,  \
00054                                 UnitySctextAccessiblePrivate))
00055 
00056 struct _UnitySctextAccessiblePrivate
00057 {
00058   gchar* stripped_name;
00059 };
00060 
00061 
00062 static void
00063 unity_sctext_accessible_class_init(UnitySctextAccessibleClass* klass)
00064 {
00065   GObjectClass* gobject_class = G_OBJECT_CLASS(klass);
00066   AtkObjectClass* atk_class = ATK_OBJECT_CLASS(klass);
00067 
00068   /* AtkObject */
00069   atk_class->get_name = unity_sctext_accessible_get_name;
00070   atk_class->initialize = unity_sctext_accessible_initialize;
00071 
00072   g_type_class_add_private(gobject_class, sizeof(UnitySctextAccessiblePrivate));
00073 }
00074 
00075 static void
00076 unity_sctext_accessible_init(UnitySctextAccessible* self)
00077 {
00078   UnitySctextAccessiblePrivate* priv =
00079     UNITY_SCTEXT_ACCESSIBLE_GET_PRIVATE(self);
00080 
00081   self->priv = priv;
00082   priv->stripped_name = NULL;
00083 }
00084 
00085 AtkObject*
00086 unity_sctext_accessible_new(nux::Object* object)
00087 {
00088   AtkObject* accessible = NULL;
00089 
00090   g_return_val_if_fail(dynamic_cast<nux::StaticCairoText*>(object), NULL);
00091 
00092   accessible = ATK_OBJECT(g_object_new(UNITY_TYPE_SCTEXT_ACCESSIBLE, NULL));
00093 
00094   atk_object_initialize(accessible, object);
00095 
00096   return accessible;
00097 }
00098 
00099 /* AtkObject.h */
00100 static void
00101 on_label_text_change_cb(nux::StaticCairoText* label, UnitySctextAccessible* self)
00102 {
00103   g_object_notify(G_OBJECT(self), "accessible-name");
00104 }
00105 
00106 static void
00107 unity_sctext_accessible_initialize(AtkObject* accessible,
00108                                    gpointer data)
00109 {
00110   nux::Object* nux_object = NULL;
00111   nux::StaticCairoText* label = NULL;
00112 
00113   ATK_OBJECT_CLASS(unity_sctext_accessible_parent_class)->initialize(accessible, data);
00114 
00115   atk_object_set_role(accessible, ATK_ROLE_LABEL);
00116 
00117   nux_object = nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(accessible));
00118   label = dynamic_cast<nux::StaticCairoText*>(nux_object);
00119 
00120   if (label == NULL) /* status defunct */
00121     return;
00122 
00123   label->sigTextChanged.connect(sigc::bind(sigc::ptr_fun(on_label_text_change_cb),
00124                                            UNITY_SCTEXT_ACCESSIBLE(accessible)));
00125 }
00126 
00127 static const gchar*
00128 unity_sctext_accessible_get_name(AtkObject* obj)
00129 {
00130   const gchar* name = NULL;
00131   UnitySctextAccessible* self = NULL;
00132 
00133   g_return_val_if_fail(UNITY_IS_SCTEXT_ACCESSIBLE(obj), NULL);
00134   self = UNITY_SCTEXT_ACCESSIBLE(obj);
00135 
00136   name = ATK_OBJECT_CLASS(unity_sctext_accessible_parent_class)->get_name(obj);
00137   if (name == NULL)
00138   {
00139     nux::StaticCairoText* text = NULL;
00140 
00141     if (self->priv->stripped_name != NULL)
00142     {
00143       g_free(self->priv->stripped_name);
00144       self->priv->stripped_name = NULL;
00145     }
00146 
00147     text = dynamic_cast<nux::StaticCairoText*>(nux_object_accessible_get_object(NUX_OBJECT_ACCESSIBLE(obj)));
00148     if (text != NULL)
00149     {
00150       name = text->GetText().c_str();
00151       pango_parse_markup(name, -1, 0, NULL,
00152                          &self->priv->stripped_name,
00153                          NULL, NULL);
00154       name = self->priv->stripped_name;
00155     }
00156   }
00157 
00158   return name;
00159 }