| 1 |
/* CALLY - The Clutter Accessibility Implementation Library |
| 2 |
* |
| 3 |
* Copyright (C) 2009 Igalia, S.L. |
| 4 |
* |
| 5 |
* Author: Alejandro PiƱeiro Iglesias <[email protected]> |
| 6 |
* |
| 7 |
* Some parts are based on GailLabel, GailEntry, GailTextView from GAIL |
| 8 |
* GAIL - The GNOME Accessibility Implementation Library |
| 9 |
* Copyright 2001, 2002, 2003 Sun Microsystems Inc. |
| 10 |
* |
| 11 |
* Implementation of atk_text_get_text_[before/at/after]_offset |
| 12 |
* copied from gtkpango.c, part of GTK+ project |
| 13 |
* Copyright (c) 2010 Red Hat, Inc. |
| 14 |
* |
| 15 |
* This library is free software; you can redistribute it and/or |
| 16 |
* modify it under the terms of the GNU Lesser General Public |
| 17 |
* License as published by the Free Software Foundation; either |
| 18 |
* version 2 of the License, or (at your option) any later version. |
| 19 |
* |
| 20 |
* This library is distributed in the hope that it will be useful, |
| 21 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 23 |
* Lesser General Public License for more details. |
| 24 |
* |
| 25 |
* You should have received a copy of the GNU Lesser General Public |
| 26 |
* License along with this library; if not, write to the |
| 27 |
* Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 28 |
* Boston, MA 02111-1307, USA. |
| 29 |
*/ |
| 30 |
|
| 31 |
/** |
| 32 |
* SECTION:cally-text |
| 33 |
* @short_description: Implementation of the ATK interfaces for a #ClutterText |
| 34 |
* @see_also: #ClutterText |
| 35 |
* |
| 36 |
* #CallyText implements the required ATK interfaces of |
| 37 |
* #ClutterText, #AtkText and #AtkEditableText |
| 38 |
* |
| 39 |
* |
| 40 |
*/ |
| 41 |
|
| 42 |
#ifdef HAVE_CONFIG_H |
| 43 |
#include "config.h" |
| 44 |
#endif |
| 45 |
|
| 46 |
#include "cally-text.h" |
| 47 |
#include "cally-actor-private.h" |
| 48 |
|
| 49 |
#include "clutter-color.h" |
| 50 |
#include "clutter-main.h" |
| 51 |
#include "clutter-text.h" |
| 52 |
|
| 53 |
static void cally_text_finalize (GObject *obj); |
| 54 |
|
| 55 |
/* AtkObject */ |
| 56 |
static void cally_text_real_initialize (AtkObject *obj, |
| 57 |
gpointer data); |
| 58 |
static AtkStateSet* cally_text_ref_state_set (AtkObject *obj); |
| 59 |
|
| 60 |
/* atkaction */ |
| 61 |
|
| 62 |
static void _cally_text_activate_action (CallyActor *cally_actor); |
| 63 |
static void _check_activate_action (CallyText *cally_text, |
| 64 |
ClutterText *clutter_text); |
| 65 |
|
| 66 |
/* AtkText */ |
| 67 |
static void cally_text_text_interface_init (AtkTextIface *iface); |
| 68 |
static gchar* cally_text_get_text (AtkText *text, |
| 69 |
gint start_offset, |
| 70 |
gint end_offset); |
| 71 |
static gunichar cally_text_get_character_at_offset (AtkText *text, |
| 72 |
gint offset); |
| 73 |
static gchar* cally_text_get_text_before_offset (AtkText *text, |
| 74 |
gint offset, |
| 75 |
AtkTextBoundary boundary_type, |
| 76 |
gint *start_offset, |
| 77 |
gint *end_offset); |
| 78 |
static gchar* cally_text_get_text_at_offset (AtkText *text, |
| 79 |
gint offset, |
| 80 |
AtkTextBoundary boundary_type, |
| 81 |
gint *start_offset, |
| 82 |
gint *end_offset); |
| 83 |
static gchar* cally_text_get_text_after_offset (AtkText *text, |
| 84 |
gint offset, |
| 85 |
AtkTextBoundary boundary_type, |
| 86 |
gint *start_offset, |
| 87 |
gint *end_offset); |
| 88 |
static gint cally_text_get_caret_offset (AtkText *text); |
| 89 |
static gboolean cally_text_set_caret_offset (AtkText *text, |
| 90 |
gint offset); |
| 91 |
static gint cally_text_get_character_count (AtkText *text); |
| 92 |
static gint cally_text_get_n_selections (AtkText *text); |
| 93 |
static gchar* cally_text_get_selection (AtkText *text, |
| 94 |
gint selection_num, |
| 95 |
gint *start_offset, |
| 96 |
gint *end_offset); |
| 97 |
static gboolean cally_text_add_selection (AtkText *text, |
| 98 |
gint start_offset, |
| 99 |
gint end_offset); |
| 100 |
static gboolean cally_text_remove_selection (AtkText *text, |
| 101 |
gint selection_num); |
| 102 |
static gboolean cally_text_set_selection (AtkText *text, |
| 103 |
gint selection_num, |
| 104 |
gint start_offset, |
| 105 |
gint end_offset); |
| 106 |
static AtkAttributeSet* cally_text_get_run_attributes (AtkText *text, |
| 107 |
gint offset, |
| 108 |
gint *start_offset, |
| 109 |
gint *end_offset); |
| 110 |
static AtkAttributeSet* cally_text_get_default_attributes (AtkText *text); |
| 111 |
static void cally_text_get_character_extents (AtkText *text, |
| 112 |
gint offset, |
| 113 |
gint *x, |
| 114 |
gint *y, |
| 115 |
gint *width, |
| 116 |
gint *height, |
| 117 |
AtkCoordType coords); |
| 118 |
static gint cally_text_get_offset_at_point (AtkText *text, |
| 119 |
gint x, |
| 120 |
gint y, |
| 121 |
AtkCoordType coords); |
| 122 |
|
| 123 |
static void _cally_text_get_selection_bounds (ClutterText *clutter_text, |
| 124 |
gint *start_offset, |
| 125 |
gint *end_offset); |
| 126 |
static void _cally_text_insert_text_cb (ClutterText *clutter_text, |
| 127 |
gchar *new_text, |
| 128 |
gint new_text_length, |
| 129 |
gint *position, |
| 130 |
gpointer data); |
| 131 |
static void _cally_text_delete_text_cb (ClutterText *clutter_text, |
| 132 |
gint start_pos, |
| 133 |
gint end_pos, |
| 134 |
gpointer data); |
| 135 |
static gboolean _idle_notify_insert (gpointer data); |
| 136 |
static void _notify_insert (CallyText *cally_text); |
| 137 |
static void _notify_delete (CallyText *cally_text); |
| 138 |
|
| 139 |
/* AtkEditableText */ |
| 140 |
static void cally_text_editable_text_interface_init (AtkEditableTextIface *iface); |
| 141 |
static void cally_text_set_text_contents (AtkEditableText *text, |
| 142 |
const gchar *string); |
| 143 |
static void cally_text_insert_text (AtkEditableText *text, |
| 144 |
const gchar *string, |
| 145 |
gint length, |
| 146 |
gint *position); |
| 147 |
static void cally_text_delete_text (AtkEditableText *text, |
| 148 |
gint start_pos, |
| 149 |
gint end_pos); |
| 150 |
|
| 151 |
/* CallyActor */ |
| 152 |
static void cally_text_notify_clutter (GObject *obj, |
| 153 |
GParamSpec *pspec); |
| 154 |
|
| 155 |
static gboolean _check_for_selection_change (CallyText *cally_text, |
| 156 |
ClutterText *clutter_text); |
| 157 |
|
| 158 |
/* Misc functions */ |
| 159 |
static AtkAttributeSet* _cally_misc_add_attribute (AtkAttributeSet *attrib_set, |
| 160 |
AtkTextAttribute attr, |
| 161 |
gchar *value); |
| 162 |
|
| 163 |
static AtkAttributeSet* _cally_misc_layout_get_run_attributes (AtkAttributeSet *attrib_set, |
| 164 |
ClutterText *clutter_text, |
| 165 |
gint offset, |
| 166 |
gint *start_offset, |
| 167 |
gint *end_offset); |
| 168 |
|
| 169 |
static AtkAttributeSet* _cally_misc_layout_get_default_attributes (AtkAttributeSet *attrib_set, |
| 170 |
ClutterText *text); |
| 171 |
|
| 172 |
static int _cally_misc_get_index_at_point (ClutterText *clutter_text, |
| 173 |
gint x, |
| 174 |
gint y, |
| 175 |
AtkCoordType coords); |
| 176 |
|
| 177 |
struct _CallyTextPrivate |
| 178 |
{ |
| 179 |
/* Cached ClutterText values*/ |
| 180 |
gint cursor_position; |
| 181 |
gint selection_bound; |
| 182 |
|
| 183 |
/* text_changed::insert stuff */ |
| 184 |
const gchar *signal_name_insert; |
| 185 |
gint position_insert; |
| 186 |
gint length_insert; |
| 187 |
guint insert_idle_handler; |
| 188 |
|
| 189 |
/* text_changed::delete stuff */ |
| 190 |
const gchar *signal_name_delete; |
| 191 |
gint position_delete; |
| 192 |
gint length_delete; |
| 193 |
|
| 194 |
/* action */ |
| 195 |
guint activate_action_id; |
| 196 |
}; |
| 197 |
|
| 198 |
G_DEFINE_TYPE_WITH_CODE (CallyText, |
| 199 |
cally_text, |
| 200 |
CALLY_TYPE_ACTOR, |
| 201 |
G_ADD_PRIVATE (CallyText) |
| 202 |
G_IMPLEMENT_INTERFACE (ATK_TYPE_TEXT, |
| 203 |
cally_text_text_interface_init) |
| 204 |
G_IMPLEMENT_INTERFACE (ATK_TYPE_EDITABLE_TEXT, |
| 205 |
cally_text_editable_text_interface_init)); |
| 206 |
|
| 207 |
static void |
| 208 |
cally_text_class_init (CallyTextClass *klass) |
| 209 |
{ |
| 210 |
GObjectClass *gobject_class = G_OBJECT_CLASS (klass); |
| 211 |
AtkObjectClass *class = ATK_OBJECT_CLASS (klass); |
| 212 |
CallyActorClass *cally_class = CALLY_ACTOR_CLASS (klass); |
| 213 |
|
| 214 |
gobject_class->finalize = cally_text_finalize; |
| 215 |
|
| 216 |
class->initialize = cally_text_real_initialize; |
| 217 |
class->ref_state_set = cally_text_ref_state_set; |
| 218 |
|
| 219 |
cally_class->notify_clutter = cally_text_notify_clutter; |
| 220 |
} |
| 221 |
|
| 222 |
static void |
| 223 |
cally_text_init (CallyText *cally_text) |
| 224 |
{ |
| 225 |
CallyTextPrivate *priv = cally_text_get_instance_private (cally_text); |
| 226 |
|
| 227 |
cally_text->priv = priv; |
| 228 |
|
| 229 |
priv->cursor_position = 0; |
| 230 |
priv->selection_bound = 0; |
| 231 |
|
| 232 |
priv->signal_name_insert = NULL; |
| 233 |
priv->position_insert = -1; |
| 234 |
priv->length_insert = -1; |
| 235 |
priv->insert_idle_handler = 0; |
| 236 |
|
| 237 |
priv->signal_name_delete = NULL; |
| 238 |
priv->position_delete = -1; |
| 239 |
priv->length_delete = -1; |
| 240 |
|
| 241 |
priv->activate_action_id = 0; |
| 242 |
} |
| 243 |
|
| 244 |
static void |
| 245 |
cally_text_finalize (GObject *obj) |
| 246 |
{ |
| 247 |
CallyText *cally_text = CALLY_TEXT (obj); |
| 248 |
|
| 249 |
/* g_object_unref (cally_text->priv->textutil); */ |
| 250 |
/* cally_text->priv->textutil = NULL; */ |
| 251 |
|
| 252 |
if (cally_text->priv->insert_idle_handler) |
| 253 |
{ |
| 254 |
g_source_remove (cally_text->priv->insert_idle_handler); |
| 255 |
cally_text->priv->insert_idle_handler = 0; |
| 256 |
} |
| 257 |
|
| 258 |
G_OBJECT_CLASS (cally_text_parent_class)->finalize (obj); |
| 259 |
} |
| 260 |
|
| 261 |
/** |
| 262 |
* cally_text_new: |
| 263 |
* @actor: a #ClutterActor |
| 264 |
* |
| 265 |
* Creates a new #CallyText for the given @actor. @actor must be a |
| 266 |
* #ClutterText. |
| 267 |
* |
| 268 |
* Return value: the newly created #AtkObject |
| 269 |
* |
| 270 |
* Since: 1.4 |
| 271 |
*/ |
| 272 |
AtkObject* |
| 273 |
cally_text_new (ClutterActor *actor) |
| 274 |
{ |
| 275 |
GObject *object = NULL; |
| 276 |
AtkObject *accessible = NULL; |
| 277 |
|
| 278 |
g_return_val_if_fail (CLUTTER_IS_TEXT (actor), NULL); |
| 279 |
|
| 280 |
object = g_object_new (CALLY_TYPE_TEXT, NULL); |
| 281 |
|
| 282 |
accessible = ATK_OBJECT (object); |
| 283 |
atk_object_initialize (accessible, actor); |
| 284 |
|
| 285 |
return accessible; |
| 286 |
} |
| 287 |
|
| 288 |
/* atkobject.h */ |
| 289 |
|
| 290 |
static void |
| 291 |
cally_text_real_initialize(AtkObject *obj, |
| 292 |
gpointer data) |
| 293 |
{ |
| 294 |
ClutterText *clutter_text = NULL; |
| 295 |
CallyText *cally_text = NULL; |
| 296 |
|
| 297 |
ATK_OBJECT_CLASS (cally_text_parent_class)->initialize (obj, data); |
| 298 |
|
| 299 |
g_return_if_fail (CLUTTER_TEXT (data)); |
| 300 |
|
| 301 |
cally_text = CALLY_TEXT (obj); |
| 302 |
clutter_text = CLUTTER_TEXT (data); |
| 303 |
|
| 304 |
cally_text->priv->cursor_position = clutter_text_get_cursor_position (clutter_text); |
| 305 |
cally_text->priv->selection_bound = clutter_text_get_selection_bound (clutter_text); |
| 306 |
|
| 307 |
g_signal_connect (clutter_text, "insert-text", |
| 308 |
G_CALLBACK (_cally_text_insert_text_cb), |
| 309 |
cally_text); |
| 310 |
g_signal_connect (clutter_text, "delete-text", |
| 311 |
G_CALLBACK (_cally_text_delete_text_cb), |
| 312 |
cally_text); |
| 313 |
|
| 314 |
_check_activate_action (cally_text, clutter_text); |
| 315 |
|
| 316 |
if (clutter_text_get_password_char (clutter_text) != 0) |
| 317 |
atk_object_set_role (obj, ATK_ROLE_PASSWORD_TEXT); |
| 318 |
else |
| 319 |
atk_object_set_role (obj, ATK_ROLE_TEXT); |
| 320 |
} |
| 321 |
|
| 322 |
static AtkStateSet* |
| 323 |
cally_text_ref_state_set (AtkObject *obj) |
| 324 |
{ |
| 325 |
AtkStateSet *result = NULL; |
| 326 |
ClutterActor *actor = NULL; |
| 327 |
|
| 328 |
result = ATK_OBJECT_CLASS (cally_text_parent_class)->ref_state_set (obj); |
| 329 |
|
| 330 |
actor = CALLY_GET_CLUTTER_ACTOR (obj); |
| 331 |
|
| 332 |
if (actor == NULL) |
| 333 |
return result; |
| 334 |
|
| 335 |
if (clutter_text_get_editable (CLUTTER_TEXT (actor))) |
| 336 |
atk_state_set_add_state (result, ATK_STATE_EDITABLE); |
| 337 |
|
| 338 |
if (clutter_text_get_selectable (CLUTTER_TEXT (actor))) |
| 339 |
atk_state_set_add_state (result, ATK_STATE_SELECTABLE_TEXT); |
| 340 |
|
| 341 |
return result; |
| 342 |
} |
| 343 |
|
| 344 |
/***** pango stuff **** |
| 345 |
* |
| 346 |
* FIXME: all this pango related code used to implement |
| 347 |
* atk_text_get_text_[before/at/after]_offset was copied from GTK, and |
| 348 |
* should be on a common library (like pango itself). |
| 349 |
* |
| 350 |
*********************/ |
| 351 |
|
| 352 |
/* |
| 353 |
* _gtk_pango_move_chars: |
| 354 |
* @layout: a #PangoLayout |
| 355 |
* @offset: a character offset in @layout |
| 356 |
* @count: the number of characters to move from @offset |
| 357 |
* |
| 358 |
* Returns the position that is @count characters from the |
| 359 |
* given @offset. @count may be positive or negative. |
| 360 |
* |
| 361 |
* For the purpose of this function, characters are defined |
| 362 |
* by what Pango considers cursor positions. |
| 363 |
* |
| 364 |
* Returns: the new position |
| 365 |
*/ |
| 366 |
static gint |
| 367 |
_gtk_pango_move_chars (PangoLayout *layout, |
| 368 |
gint offset, |
| 369 |
gint count) |
| 370 |
{ |
| 371 |
const PangoLogAttr *attrs; |
| 372 |
gint n_attrs; |
| 373 |
|
| 374 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 375 |
|
| 376 |
while (count > 0 && offset < n_attrs - 1) |
| 377 |
{ |
| 378 |
do |
| 379 |
offset++; |
| 380 |
while (offset < n_attrs - 1 && !attrs[offset].is_cursor_position); |
| 381 |
|
| 382 |
count--; |
| 383 |
} |
| 384 |
while (count < 0 && offset > 0) |
| 385 |
{ |
| 386 |
do |
| 387 |
offset--; |
| 388 |
while (offset > 0 && !attrs[offset].is_cursor_position); |
| 389 |
|
| 390 |
count++; |
| 391 |
} |
| 392 |
|
| 393 |
return offset; |
| 394 |
} |
| 395 |
|
| 396 |
/* |
| 397 |
* _gtk_pango_move_words: |
| 398 |
* @layout: a #PangoLayout |
| 399 |
* @offset: a character offset in @layout |
| 400 |
* @count: the number of words to move from @offset |
| 401 |
* |
| 402 |
* Returns the position that is @count words from the |
| 403 |
* given @offset. @count may be positive or negative. |
| 404 |
* |
| 405 |
* If @count is positive, the returned position will |
| 406 |
* be a word end, otherwise it will be a word start. |
| 407 |
* See the Pango documentation for details on how |
| 408 |
* word starts and ends are defined. |
| 409 |
* |
| 410 |
* Returns: the new position |
| 411 |
*/ |
| 412 |
static gint |
| 413 |
_gtk_pango_move_words (PangoLayout *layout, |
| 414 |
gint offset, |
| 415 |
gint count) |
| 416 |
{ |
| 417 |
const PangoLogAttr *attrs; |
| 418 |
gint n_attrs; |
| 419 |
|
| 420 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 421 |
|
| 422 |
while (count > 0 && offset < n_attrs - 1) |
| 423 |
{ |
| 424 |
do |
| 425 |
offset++; |
| 426 |
while (offset < n_attrs - 1 && !attrs[offset].is_word_end); |
| 427 |
|
| 428 |
count--; |
| 429 |
} |
| 430 |
while (count < 0 && offset > 0) |
| 431 |
{ |
| 432 |
do |
| 433 |
offset--; |
| 434 |
while (offset > 0 && !attrs[offset].is_word_start); |
| 435 |
|
| 436 |
count++; |
| 437 |
} |
| 438 |
|
| 439 |
return offset; |
| 440 |
} |
| 441 |
|
| 442 |
/* |
| 443 |
* _gtk_pango_move_sentences: |
| 444 |
* @layout: a #PangoLayout |
| 445 |
* @offset: a character offset in @layout |
| 446 |
* @count: the number of sentences to move from @offset |
| 447 |
* |
| 448 |
* Returns the position that is @count sentences from the |
| 449 |
* given @offset. @count may be positive or negative. |
| 450 |
* |
| 451 |
* If @count is positive, the returned position will |
| 452 |
* be a sentence end, otherwise it will be a sentence start. |
| 453 |
* See the Pango documentation for details on how |
| 454 |
* sentence starts and ends are defined. |
| 455 |
* |
| 456 |
* Returns: the new position |
| 457 |
*/ |
| 458 |
static gint |
| 459 |
_gtk_pango_move_sentences (PangoLayout *layout, |
| 460 |
gint offset, |
| 461 |
gint count) |
| 462 |
{ |
| 463 |
const PangoLogAttr *attrs; |
| 464 |
gint n_attrs; |
| 465 |
|
| 466 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 467 |
|
| 468 |
while (count > 0 && offset < n_attrs - 1) |
| 469 |
{ |
| 470 |
do |
| 471 |
offset++; |
| 472 |
while (offset < n_attrs - 1 && !attrs[offset].is_sentence_end); |
| 473 |
|
| 474 |
count--; |
| 475 |
} |
| 476 |
while (count < 0 && offset > 0) |
| 477 |
{ |
| 478 |
do |
| 479 |
offset--; |
| 480 |
while (offset > 0 && !attrs[offset].is_sentence_start); |
| 481 |
|
| 482 |
count++; |
| 483 |
} |
| 484 |
|
| 485 |
return offset; |
| 486 |
} |
| 487 |
|
| 488 |
/* |
| 489 |
* _gtk_pango_is_inside_word: |
| 490 |
* @layout: a #PangoLayout |
| 491 |
* @offset: a character offset in @layout |
| 492 |
* |
| 493 |
* Returns whether the given position is inside |
| 494 |
* a word. |
| 495 |
* |
| 496 |
* Returns: %TRUE if @offset is inside a word |
| 497 |
*/ |
| 498 |
static gboolean |
| 499 |
_gtk_pango_is_inside_word (PangoLayout *layout, |
| 500 |
gint offset) |
| 501 |
{ |
| 502 |
const PangoLogAttr *attrs; |
| 503 |
gint n_attrs; |
| 504 |
|
| 505 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 506 |
|
| 507 |
while (offset >= 0 && |
| 508 |
!(attrs[offset].is_word_start || attrs[offset].is_word_end)) |
| 509 |
offset--; |
| 510 |
|
| 511 |
if (offset >= 0) |
| 512 |
return attrs[offset].is_word_start; |
| 513 |
|
| 514 |
return FALSE; |
| 515 |
} |
| 516 |
|
| 517 |
/* |
| 518 |
* _gtk_pango_is_inside_sentence: |
| 519 |
* @layout: a #PangoLayout |
| 520 |
* @offset: a character offset in @layout |
| 521 |
* |
| 522 |
* Returns whether the given position is inside |
| 523 |
* a sentence. |
| 524 |
* |
| 525 |
* Returns: %TRUE if @offset is inside a sentence |
| 526 |
*/ |
| 527 |
static gboolean |
| 528 |
_gtk_pango_is_inside_sentence (PangoLayout *layout, |
| 529 |
gint offset) |
| 530 |
{ |
| 531 |
const PangoLogAttr *attrs; |
| 532 |
gint n_attrs; |
| 533 |
|
| 534 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 535 |
|
| 536 |
while (offset >= 0 && |
| 537 |
!(attrs[offset].is_sentence_start || attrs[offset].is_sentence_end)) |
| 538 |
offset--; |
| 539 |
|
| 540 |
if (offset >= 0) |
| 541 |
return attrs[offset].is_sentence_start; |
| 542 |
|
| 543 |
return FALSE; |
| 544 |
} |
| 545 |
|
| 546 |
static void |
| 547 |
pango_layout_get_line_before (PangoLayout *layout, |
| 548 |
AtkTextBoundary boundary_type, |
| 549 |
gint offset, |
| 550 |
gint *start_offset, |
| 551 |
gint *end_offset) |
| 552 |
{ |
| 553 |
PangoLayoutIter *iter; |
| 554 |
PangoLayoutLine *line, *prev_line = NULL, *prev_prev_line = NULL; |
| 555 |
gint index, start_index, end_index; |
| 556 |
const gchar *text; |
| 557 |
gboolean found = FALSE; |
| 558 |
|
| 559 |
text = pango_layout_get_text (layout); |
| 560 |
index = g_utf8_offset_to_pointer (text, offset) - text; |
| 561 |
iter = pango_layout_get_iter (layout); |
| 562 |
do |
| 563 |
{ |
| 564 |
line = pango_layout_iter_get_line (iter); |
| 565 |
start_index = line->start_index; |
| 566 |
end_index = start_index + line->length; |
| 567 |
|
| 568 |
if (index >= start_index && index <= end_index) |
| 569 |
{ |
| 570 |
/* Found line for offset */ |
| 571 |
if (prev_line) |
| 572 |
{ |
| 573 |
switch (boundary_type) |
| 574 |
{ |
| 575 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 576 |
end_index = start_index; |
| 577 |
start_index = prev_line->start_index; |
| 578 |
break; |
| 579 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 580 |
if (prev_prev_line) |
| 581 |
start_index = prev_prev_line->start_index + prev_prev_line->length; |
| 582 |
else |
| 583 |
start_index = 0; |
| 584 |
end_index = prev_line->start_index + prev_line->length; |
| 585 |
break; |
| 586 |
default: |
| 587 |
g_assert_not_reached(); |
| 588 |
} |
| 589 |
} |
| 590 |
else |
| 591 |
start_index = end_index = 0; |
| 592 |
|
| 593 |
found = TRUE; |
| 594 |
break; |
| 595 |
} |
| 596 |
|
| 597 |
prev_prev_line = prev_line; |
| 598 |
prev_line = line; |
| 599 |
} |
| 600 |
while (pango_layout_iter_next_line (iter)); |
| 601 |
|
| 602 |
if (!found) |
| 603 |
{ |
| 604 |
start_index = prev_line->start_index + prev_line->length; |
| 605 |
end_index = start_index; |
| 606 |
} |
| 607 |
pango_layout_iter_free (iter); |
| 608 |
|
| 609 |
*start_offset = g_utf8_pointer_to_offset (text, text + start_index); |
| 610 |
*end_offset = g_utf8_pointer_to_offset (text, text + end_index); |
| 611 |
} |
| 612 |
|
| 613 |
static void |
| 614 |
pango_layout_get_line_at (PangoLayout *layout, |
| 615 |
AtkTextBoundary boundary_type, |
| 616 |
gint offset, |
| 617 |
gint *start_offset, |
| 618 |
gint *end_offset) |
| 619 |
{ |
| 620 |
PangoLayoutIter *iter; |
| 621 |
PangoLayoutLine *line, *prev_line = NULL; |
| 622 |
gint index, start_index, end_index; |
| 623 |
const gchar *text; |
| 624 |
gboolean found = FALSE; |
| 625 |
|
| 626 |
text = pango_layout_get_text (layout); |
| 627 |
index = g_utf8_offset_to_pointer (text, offset) - text; |
| 628 |
iter = pango_layout_get_iter (layout); |
| 629 |
do |
| 630 |
{ |
| 631 |
line = pango_layout_iter_get_line (iter); |
| 632 |
start_index = line->start_index; |
| 633 |
end_index = start_index + line->length; |
| 634 |
|
| 635 |
if (index >= start_index && index <= end_index) |
| 636 |
{ |
| 637 |
/* Found line for offset */ |
| 638 |
switch (boundary_type) |
| 639 |
{ |
| 640 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 641 |
if (pango_layout_iter_next_line (iter)) |
| 642 |
end_index = pango_layout_iter_get_line (iter)->start_index; |
| 643 |
break; |
| 644 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 645 |
if (prev_line) |
| 646 |
start_index = prev_line->start_index + prev_line->length; |
| 647 |
break; |
| 648 |
default: |
| 649 |
g_assert_not_reached(); |
| 650 |
} |
| 651 |
|
| 652 |
found = TRUE; |
| 653 |
break; |
| 654 |
} |
| 655 |
|
| 656 |
prev_line = line; |
| 657 |
} |
| 658 |
while (pango_layout_iter_next_line (iter)); |
| 659 |
|
| 660 |
if (!found) |
| 661 |
{ |
| 662 |
start_index = prev_line->start_index + prev_line->length; |
| 663 |
end_index = start_index; |
| 664 |
} |
| 665 |
pango_layout_iter_free (iter); |
| 666 |
|
| 667 |
*start_offset = g_utf8_pointer_to_offset (text, text + start_index); |
| 668 |
*end_offset = g_utf8_pointer_to_offset (text, text + end_index); |
| 669 |
} |
| 670 |
|
| 671 |
static void |
| 672 |
pango_layout_get_line_after (PangoLayout *layout, |
| 673 |
AtkTextBoundary boundary_type, |
| 674 |
gint offset, |
| 675 |
gint *start_offset, |
| 676 |
gint *end_offset) |
| 677 |
{ |
| 678 |
PangoLayoutIter *iter; |
| 679 |
PangoLayoutLine *line, *prev_line = NULL; |
| 680 |
gint index, start_index, end_index; |
| 681 |
const gchar *text; |
| 682 |
gboolean found = FALSE; |
| 683 |
|
| 684 |
text = pango_layout_get_text (layout); |
| 685 |
index = g_utf8_offset_to_pointer (text, offset) - text; |
| 686 |
iter = pango_layout_get_iter (layout); |
| 687 |
do |
| 688 |
{ |
| 689 |
line = pango_layout_iter_get_line (iter); |
| 690 |
start_index = line->start_index; |
| 691 |
end_index = start_index + line->length; |
| 692 |
|
| 693 |
if (index >= start_index && index <= end_index) |
| 694 |
{ |
| 695 |
/* Found line for offset */ |
| 696 |
if (pango_layout_iter_next_line (iter)) |
| 697 |
{ |
| 698 |
line = pango_layout_iter_get_line (iter); |
| 699 |
switch (boundary_type) |
| 700 |
{ |
| 701 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 702 |
start_index = line->start_index; |
| 703 |
if (pango_layout_iter_next_line (iter)) |
| 704 |
end_index = pango_layout_iter_get_line (iter)->start_index; |
| 705 |
else |
| 706 |
end_index = start_index + line->length; |
| 707 |
break; |
| 708 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 709 |
start_index = end_index; |
| 710 |
end_index = line->start_index + line->length; |
| 711 |
break; |
| 712 |
default: |
| 713 |
g_assert_not_reached(); |
| 714 |
} |
| 715 |
} |
| 716 |
else |
| 717 |
start_index = end_index; |
| 718 |
|
| 719 |
found = TRUE; |
| 720 |
break; |
| 721 |
} |
| 722 |
|
| 723 |
prev_line = line; |
| 724 |
} |
| 725 |
while (pango_layout_iter_next_line (iter)); |
| 726 |
|
| 727 |
if (!found) |
| 728 |
{ |
| 729 |
start_index = prev_line->start_index + prev_line->length; |
| 730 |
end_index = start_index; |
| 731 |
} |
| 732 |
pango_layout_iter_free (iter); |
| 733 |
|
| 734 |
*start_offset = g_utf8_pointer_to_offset (text, text + start_index); |
| 735 |
*end_offset = g_utf8_pointer_to_offset (text, text + end_index); |
| 736 |
} |
| 737 |
|
| 738 |
/* |
| 739 |
* _gtk_pango_get_text_at: |
| 740 |
* @layout: a #PangoLayout |
| 741 |
* @boundary_type: a #AtkTextBoundary |
| 742 |
* @offset: a character offset in @layout |
| 743 |
* @start_offset: return location for the start of the returned text |
| 744 |
* @end_offset: return location for the end of the return text |
| 745 |
* |
| 746 |
* Gets a slice of the text from @layout at @offset. |
| 747 |
* |
| 748 |
* The @boundary_type determines the size of the returned slice of |
| 749 |
* text. For the exact semantics of this function, see |
| 750 |
* atk_text_get_text_after_offset(). |
| 751 |
* |
| 752 |
* Returns: a newly allocated string containing a slice of text |
| 753 |
* from layout. Free with g_free(). |
| 754 |
*/ |
| 755 |
static gchar * |
| 756 |
_gtk_pango_get_text_at (PangoLayout *layout, |
| 757 |
AtkTextBoundary boundary_type, |
| 758 |
gint offset, |
| 759 |
gint *start_offset, |
| 760 |
gint *end_offset) |
| 761 |
{ |
| 762 |
const gchar *text; |
| 763 |
gint start, end; |
| 764 |
const PangoLogAttr *attrs; |
| 765 |
gint n_attrs; |
| 766 |
|
| 767 |
text = pango_layout_get_text (layout); |
| 768 |
|
| 769 |
if (text[0] == 0) |
| 770 |
{ |
| 771 |
*start_offset = 0; |
| 772 |
*end_offset = 0; |
| 773 |
return g_strdup (""); |
| 774 |
} |
| 775 |
|
| 776 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 777 |
|
| 778 |
start = offset; |
| 779 |
end = start; |
| 780 |
|
| 781 |
switch (boundary_type) |
| 782 |
{ |
| 783 |
case ATK_TEXT_BOUNDARY_CHAR: |
| 784 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 785 |
break; |
| 786 |
|
| 787 |
case ATK_TEXT_BOUNDARY_WORD_START: |
| 788 |
if (!attrs[start].is_word_start) |
| 789 |
start = _gtk_pango_move_words (layout, start, -1); |
| 790 |
if (_gtk_pango_is_inside_word (layout, end)) |
| 791 |
end = _gtk_pango_move_words (layout, end, 1); |
| 792 |
while (!attrs[end].is_word_start && end < n_attrs - 1) |
| 793 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 794 |
break; |
| 795 |
|
| 796 |
case ATK_TEXT_BOUNDARY_WORD_END: |
| 797 |
if (_gtk_pango_is_inside_word (layout, start) && |
| 798 |
!attrs[start].is_word_start) |
| 799 |
start = _gtk_pango_move_words (layout, start, -1); |
| 800 |
while (!attrs[start].is_word_end && start > 0) |
| 801 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 802 |
end = _gtk_pango_move_words (layout, end, 1); |
| 803 |
break; |
| 804 |
|
| 805 |
case ATK_TEXT_BOUNDARY_SENTENCE_START: |
| 806 |
if (!attrs[start].is_sentence_start) |
| 807 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 808 |
if (_gtk_pango_is_inside_sentence (layout, end)) |
| 809 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 810 |
while (!attrs[end].is_sentence_start && end < n_attrs - 1) |
| 811 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 812 |
break; |
| 813 |
|
| 814 |
case ATK_TEXT_BOUNDARY_SENTENCE_END: |
| 815 |
if (_gtk_pango_is_inside_sentence (layout, start) && |
| 816 |
!attrs[start].is_sentence_start) |
| 817 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 818 |
while (!attrs[start].is_sentence_end && start > 0) |
| 819 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 820 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 821 |
break; |
| 822 |
|
| 823 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 824 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 825 |
pango_layout_get_line_at (layout, boundary_type, offset, &start, &end); |
| 826 |
break; |
| 827 |
} |
| 828 |
|
| 829 |
*start_offset = start; |
| 830 |
*end_offset = end; |
| 831 |
|
| 832 |
g_assert (start <= end); |
| 833 |
|
| 834 |
return g_utf8_substring (text, start, end); |
| 835 |
} |
| 836 |
|
| 837 |
/* |
| 838 |
* _gtk_pango_get_text_before: |
| 839 |
* @layout: a #PangoLayout |
| 840 |
* @boundary_type: a #AtkTextBoundary |
| 841 |
* @offset: a character offset in @layout |
| 842 |
* @start_offset: return location for the start of the returned text |
| 843 |
* @end_offset: return location for the end of the return text |
| 844 |
* |
| 845 |
* Gets a slice of the text from @layout before @offset. |
| 846 |
* |
| 847 |
* The @boundary_type determines the size of the returned slice of |
| 848 |
* text. For the exact semantics of this function, see |
| 849 |
* atk_text_get_text_before_offset(). |
| 850 |
* |
| 851 |
* Returns: a newly allocated string containing a slice of text |
| 852 |
* from layout. Free with g_free(). |
| 853 |
*/ |
| 854 |
static gchar * |
| 855 |
_gtk_pango_get_text_before (PangoLayout *layout, |
| 856 |
AtkTextBoundary boundary_type, |
| 857 |
gint offset, |
| 858 |
gint *start_offset, |
| 859 |
gint *end_offset) |
| 860 |
{ |
| 861 |
const gchar *text; |
| 862 |
gint start, end; |
| 863 |
const PangoLogAttr *attrs; |
| 864 |
gint n_attrs; |
| 865 |
|
| 866 |
text = pango_layout_get_text (layout); |
| 867 |
|
| 868 |
if (text[0] == 0) |
| 869 |
{ |
| 870 |
*start_offset = 0; |
| 871 |
*end_offset = 0; |
| 872 |
return g_strdup (""); |
| 873 |
} |
| 874 |
|
| 875 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 876 |
|
| 877 |
start = offset; |
| 878 |
end = start; |
| 879 |
|
| 880 |
switch (boundary_type) |
| 881 |
{ |
| 882 |
case ATK_TEXT_BOUNDARY_CHAR: |
| 883 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 884 |
break; |
| 885 |
|
| 886 |
case ATK_TEXT_BOUNDARY_WORD_START: |
| 887 |
if (!attrs[start].is_word_start) |
| 888 |
start = _gtk_pango_move_words (layout, start, -1); |
| 889 |
end = start; |
| 890 |
start = _gtk_pango_move_words (layout, start, -1); |
| 891 |
break; |
| 892 |
|
| 893 |
case ATK_TEXT_BOUNDARY_WORD_END: |
| 894 |
if (_gtk_pango_is_inside_word (layout, start) && |
| 895 |
!attrs[start].is_word_start) |
| 896 |
start = _gtk_pango_move_words (layout, start, -1); |
| 897 |
while (!attrs[start].is_word_end && start > 0) |
| 898 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 899 |
end = start; |
| 900 |
start = _gtk_pango_move_words (layout, start, -1); |
| 901 |
while (!attrs[start].is_word_end && start > 0) |
| 902 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 903 |
break; |
| 904 |
|
| 905 |
case ATK_TEXT_BOUNDARY_SENTENCE_START: |
| 906 |
if (!attrs[start].is_sentence_start) |
| 907 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 908 |
end = start; |
| 909 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 910 |
break; |
| 911 |
|
| 912 |
case ATK_TEXT_BOUNDARY_SENTENCE_END: |
| 913 |
if (_gtk_pango_is_inside_sentence (layout, start) && |
| 914 |
!attrs[start].is_sentence_start) |
| 915 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 916 |
while (!attrs[start].is_sentence_end && start > 0) |
| 917 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 918 |
end = start; |
| 919 |
start = _gtk_pango_move_sentences (layout, start, -1); |
| 920 |
while (!attrs[start].is_sentence_end && start > 0) |
| 921 |
start = _gtk_pango_move_chars (layout, start, -1); |
| 922 |
break; |
| 923 |
|
| 924 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 925 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 926 |
pango_layout_get_line_before (layout, boundary_type, offset, &start, &end); |
| 927 |
break; |
| 928 |
} |
| 929 |
|
| 930 |
*start_offset = start; |
| 931 |
*end_offset = end; |
| 932 |
|
| 933 |
g_assert (start <= end); |
| 934 |
|
| 935 |
return g_utf8_substring (text, start, end); |
| 936 |
} |
| 937 |
|
| 938 |
/* |
| 939 |
* _gtk_pango_get_text_after: |
| 940 |
* @layout: a #PangoLayout |
| 941 |
* @boundary_type: a #AtkTextBoundary |
| 942 |
* @offset: a character offset in @layout |
| 943 |
* @start_offset: return location for the start of the returned text |
| 944 |
* @end_offset: return location for the end of the return text |
| 945 |
* |
| 946 |
* Gets a slice of the text from @layout after @offset. |
| 947 |
* |
| 948 |
* The @boundary_type determines the size of the returned slice of |
| 949 |
* text. For the exact semantics of this function, see |
| 950 |
* atk_text_get_text_after_offset(). |
| 951 |
* |
| 952 |
* Returns: a newly allocated string containing a slice of text |
| 953 |
* from layout. Free with g_free(). |
| 954 |
*/ |
| 955 |
static gchar * |
| 956 |
_gtk_pango_get_text_after (PangoLayout *layout, |
| 957 |
AtkTextBoundary boundary_type, |
| 958 |
gint offset, |
| 959 |
gint *start_offset, |
| 960 |
gint *end_offset) |
| 961 |
{ |
| 962 |
const gchar *text; |
| 963 |
gint start, end; |
| 964 |
const PangoLogAttr *attrs; |
| 965 |
gint n_attrs; |
| 966 |
|
| 967 |
text = pango_layout_get_text (layout); |
| 968 |
|
| 969 |
if (text[0] == 0) |
| 970 |
{ |
| 971 |
*start_offset = 0; |
| 972 |
*end_offset = 0; |
| 973 |
return g_strdup (""); |
| 974 |
} |
| 975 |
|
| 976 |
attrs = pango_layout_get_log_attrs_readonly (layout, &n_attrs); |
| 977 |
|
| 978 |
start = offset; |
| 979 |
end = start; |
| 980 |
|
| 981 |
switch (boundary_type) |
| 982 |
{ |
| 983 |
case ATK_TEXT_BOUNDARY_CHAR: |
| 984 |
start = _gtk_pango_move_chars (layout, start, 1); |
| 985 |
end = start; |
| 986 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 987 |
break; |
| 988 |
|
| 989 |
case ATK_TEXT_BOUNDARY_WORD_START: |
| 990 |
if (_gtk_pango_is_inside_word (layout, end)) |
| 991 |
end = _gtk_pango_move_words (layout, end, 1); |
| 992 |
while (!attrs[end].is_word_start && end < n_attrs - 1) |
| 993 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 994 |
start = end; |
| 995 |
if (end < n_attrs - 1) |
| 996 |
{ |
| 997 |
end = _gtk_pango_move_words (layout, end, 1); |
| 998 |
while (!attrs[end].is_word_start && end < n_attrs - 1) |
| 999 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 1000 |
} |
| 1001 |
break; |
| 1002 |
|
| 1003 |
case ATK_TEXT_BOUNDARY_WORD_END: |
| 1004 |
end = _gtk_pango_move_words (layout, end, 1); |
| 1005 |
start = end; |
| 1006 |
if (end < n_attrs - 1) |
| 1007 |
end = _gtk_pango_move_words (layout, end, 1); |
| 1008 |
break; |
| 1009 |
|
| 1010 |
case ATK_TEXT_BOUNDARY_SENTENCE_START: |
| 1011 |
if (_gtk_pango_is_inside_sentence (layout, end)) |
| 1012 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 1013 |
while (!attrs[end].is_sentence_start && end < n_attrs - 1) |
| 1014 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 1015 |
start = end; |
| 1016 |
if (end < n_attrs - 1) |
| 1017 |
{ |
| 1018 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 1019 |
while (!attrs[end].is_sentence_start && end < n_attrs - 1) |
| 1020 |
end = _gtk_pango_move_chars (layout, end, 1); |
| 1021 |
} |
| 1022 |
break; |
| 1023 |
|
| 1024 |
case ATK_TEXT_BOUNDARY_SENTENCE_END: |
| 1025 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 1026 |
start = end; |
| 1027 |
if (end < n_attrs - 1) |
| 1028 |
end = _gtk_pango_move_sentences (layout, end, 1); |
| 1029 |
break; |
| 1030 |
|
| 1031 |
case ATK_TEXT_BOUNDARY_LINE_START: |
| 1032 |
case ATK_TEXT_BOUNDARY_LINE_END: |
| 1033 |
pango_layout_get_line_after (layout, boundary_type, offset, &start, &end); |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
|
| 1037 |
*start_offset = start; |
| 1038 |
*end_offset = end; |
| 1039 |
|
| 1040 |
g_assert (start <= end); |
| 1041 |
|
| 1042 |
return g_utf8_substring (text, start, end); |
| 1043 |
} |
| 1044 |
|
| 1045 |
/***** atktext.h ******/ |
| 1046 |
|
| 1047 |
static void |
| 1048 |
cally_text_text_interface_init (AtkTextIface *iface) |
| 1049 |
{ |
| 1050 |
g_return_if_fail (iface != NULL); |
| 1051 |
|
| 1052 |
iface->get_text = cally_text_get_text; |
| 1053 |
iface->get_character_at_offset = cally_text_get_character_at_offset; |
| 1054 |
iface->get_text_before_offset = cally_text_get_text_before_offset; |
| 1055 |
iface->get_text_at_offset = cally_text_get_text_at_offset; |
| 1056 |
iface->get_text_after_offset = cally_text_get_text_after_offset; |
| 1057 |
iface->get_character_count = cally_text_get_character_count; |
| 1058 |
iface->get_caret_offset = cally_text_get_caret_offset; |
| 1059 |
iface->set_caret_offset = cally_text_set_caret_offset; |
| 1060 |
iface->get_n_selections = cally_text_get_n_selections; |
| 1061 |
iface->get_selection = cally_text_get_selection; |
| 1062 |
iface->add_selection = cally_text_add_selection; |
| 1063 |
iface->remove_selection = cally_text_remove_selection; |
| 1064 |
iface->set_selection = cally_text_set_selection; |
| 1065 |
iface->get_run_attributes = cally_text_get_run_attributes; |
| 1066 |
iface->get_default_attributes = cally_text_get_default_attributes; |
| 1067 |
iface->get_character_extents = cally_text_get_character_extents; |
| 1068 |
iface->get_offset_at_point = cally_text_get_offset_at_point; |
| 1069 |
|
| 1070 |
} |
| 1071 |
|
| 1072 |
static gchar* |
| 1073 |
cally_text_get_text (AtkText *text, |
| 1074 |
gint start_offset, |
| 1075 |
gint end_offset) |
| 1076 |
{ |
| 1077 |
ClutterActor *actor = NULL; |
| 1078 |
PangoLayout *layout = NULL; |
| 1079 |
const gchar *string = NULL; |
| 1080 |
gint character_count = 0; |
| 1081 |
|
| 1082 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1083 |
if (actor == NULL) /* Object is defunct */ |
| 1084 |
return NULL; |
| 1085 |
|
| 1086 |
/* we use the pango layout instead of clutter_text_get_chars because |
| 1087 |
it take into account password-char */ |
| 1088 |
|
| 1089 |
layout = clutter_text_get_layout (CLUTTER_TEXT (actor)); |
| 1090 |
string = pango_layout_get_text (layout); |
| 1091 |
character_count = pango_layout_get_character_count (layout); |
| 1092 |
|
| 1093 |
if (end_offset == -1 || end_offset > character_count) |
| 1094 |
end_offset = character_count; |
| 1095 |
|
| 1096 |
if (string[0] == 0) |
| 1097 |
return g_strdup(""); |
| 1098 |
else |
| 1099 |
return g_utf8_substring (string, start_offset, end_offset); |
| 1100 |
} |
| 1101 |
|
| 1102 |
static gunichar |
| 1103 |
cally_text_get_character_at_offset (AtkText *text, |
| 1104 |
gint offset) |
| 1105 |
{ |
| 1106 |
ClutterActor *actor = NULL; |
| 1107 |
const gchar *string = NULL; |
| 1108 |
gchar *index = NULL; |
| 1109 |
gunichar unichar; |
| 1110 |
PangoLayout *layout = NULL; |
| 1111 |
|
| 1112 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1113 |
if (actor == NULL) /* State is defunct */ |
| 1114 |
return '\0'; |
| 1115 |
|
| 1116 |
/* we use the pango layout instead of clutter_text_get_chars because |
| 1117 |
it take into account password-char */ |
| 1118 |
|
| 1119 |
layout = clutter_text_get_layout (CLUTTER_TEXT (actor)); |
| 1120 |
string = pango_layout_get_text (layout); |
| 1121 |
|
| 1122 |
if (offset >= g_utf8_strlen (string, -1)) |
| 1123 |
{ |
| 1124 |
unichar = '\0'; |
| 1125 |
} |
| 1126 |
else |
| 1127 |
{ |
| 1128 |
index = g_utf8_offset_to_pointer (string, offset); |
| 1129 |
|
| 1130 |
unichar = g_utf8_get_char (index); |
| 1131 |
} |
| 1132 |
|
| 1133 |
return unichar; |
| 1134 |
} |
| 1135 |
|
| 1136 |
static gchar* |
| 1137 |
cally_text_get_text_before_offset (AtkText *text, |
| 1138 |
gint offset, |
| 1139 |
AtkTextBoundary boundary_type, |
| 1140 |
gint *start_offset, |
| 1141 |
gint *end_offset) |
| 1142 |
{ |
| 1143 |
ClutterActor *actor = NULL; |
| 1144 |
|
| 1145 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1146 |
if (actor == NULL) /* State is defunct */ |
| 1147 |
return NULL; |
| 1148 |
|
| 1149 |
return _gtk_pango_get_text_before (clutter_text_get_layout (CLUTTER_TEXT (actor)), |
| 1150 |
boundary_type, offset, |
| 1151 |
start_offset, end_offset); |
| 1152 |
} |
| 1153 |
|
| 1154 |
static gchar* |
| 1155 |
cally_text_get_text_at_offset (AtkText *text, |
| 1156 |
gint offset, |
| 1157 |
AtkTextBoundary boundary_type, |
| 1158 |
gint *start_offset, |
| 1159 |
gint *end_offset) |
| 1160 |
{ |
| 1161 |
ClutterActor *actor = NULL; |
| 1162 |
|
| 1163 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1164 |
if (actor == NULL) /* State is defunct */ |
| 1165 |
return NULL; |
| 1166 |
|
| 1167 |
return _gtk_pango_get_text_at (clutter_text_get_layout (CLUTTER_TEXT (actor)), |
| 1168 |
boundary_type, offset, |
| 1169 |
start_offset, end_offset); |
| 1170 |
} |
| 1171 |
|
| 1172 |
static gchar* |
| 1173 |
cally_text_get_text_after_offset (AtkText *text, |
| 1174 |
gint offset, |
| 1175 |
AtkTextBoundary boundary_type, |
| 1176 |
gint *start_offset, |
| 1177 |
gint *end_offset) |
| 1178 |
{ |
| 1179 |
ClutterActor *actor = NULL; |
| 1180 |
|
| 1181 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1182 |
if (actor == NULL) /* State is defunct */ |
| 1183 |
return NULL; |
| 1184 |
|
| 1185 |
return _gtk_pango_get_text_after (clutter_text_get_layout (CLUTTER_TEXT (actor)), |
| 1186 |
boundary_type, offset, |
| 1187 |
start_offset, end_offset); |
| 1188 |
} |
| 1189 |
|
| 1190 |
static gint |
| 1191 |
cally_text_get_caret_offset (AtkText *text) |
| 1192 |
{ |
| 1193 |
ClutterActor *actor = NULL; |
| 1194 |
|
| 1195 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1196 |
if (actor == NULL) /* State is defunct */ |
| 1197 |
return -1; |
| 1198 |
|
| 1199 |
return clutter_text_get_cursor_position (CLUTTER_TEXT (actor)); |
| 1200 |
} |
| 1201 |
|
| 1202 |
static gboolean |
| 1203 |
cally_text_set_caret_offset (AtkText *text, |
| 1204 |
gint offset) |
| 1205 |
{ |
| 1206 |
ClutterActor *actor = NULL; |
| 1207 |
|
| 1208 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1209 |
if (actor == NULL) /* State is defunct */ |
| 1210 |
return FALSE; |
| 1211 |
|
| 1212 |
clutter_text_set_cursor_position (CLUTTER_TEXT (actor), offset); |
| 1213 |
|
| 1214 |
/* like in gailentry, we suppose that this always works, as clutter text |
| 1215 |
doesn't return anything */ |
| 1216 |
return TRUE; |
| 1217 |
} |
| 1218 |
|
| 1219 |
static gint |
| 1220 |
cally_text_get_character_count (AtkText *text) |
| 1221 |
{ |
| 1222 |
ClutterActor *actor = NULL; |
| 1223 |
ClutterText *clutter_text = NULL; |
| 1224 |
|
| 1225 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1226 |
if (actor == NULL) /* State is defunct */ |
| 1227 |
return 0; |
| 1228 |
|
| 1229 |
clutter_text = CLUTTER_TEXT (actor); |
| 1230 |
return g_utf8_strlen (clutter_text_get_text (clutter_text), -1); |
| 1231 |
} |
| 1232 |
|
| 1233 |
static gint |
| 1234 |
cally_text_get_n_selections (AtkText *text) |
| 1235 |
{ |
| 1236 |
ClutterActor *actor = NULL; |
| 1237 |
gint selection_bound = -1; |
| 1238 |
gint cursor_position = -1; |
| 1239 |
|
| 1240 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1241 |
if (actor == NULL) /* State is defunct */ |
| 1242 |
return 0; |
| 1243 |
|
| 1244 |
if (!clutter_text_get_selectable (CLUTTER_TEXT (actor))) |
| 1245 |
return 0; |
| 1246 |
|
| 1247 |
selection_bound = clutter_text_get_selection_bound (CLUTTER_TEXT (actor)); |
| 1248 |
cursor_position = clutter_text_get_cursor_position (CLUTTER_TEXT (actor)); |
| 1249 |
|
| 1250 |
if (selection_bound == cursor_position) |
| 1251 |
return 0; |
| 1252 |
else |
| 1253 |
return 1; |
| 1254 |
} |
| 1255 |
|
| 1256 |
static gchar* |
| 1257 |
cally_text_get_selection (AtkText *text, |
| 1258 |
gint selection_num, |
| 1259 |
gint *start_offset, |
| 1260 |
gint *end_offset) |
| 1261 |
{ |
| 1262 |
ClutterActor *actor = NULL; |
| 1263 |
|
| 1264 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1265 |
if (actor == NULL) /* State is defunct */ |
| 1266 |
return NULL; |
| 1267 |
|
| 1268 |
/* As in gailentry, only let the user get the selection if one is set, and if |
| 1269 |
* the selection_num is 0. |
| 1270 |
*/ |
| 1271 |
if (selection_num != 0) |
| 1272 |
return NULL; |
| 1273 |
|
| 1274 |
_cally_text_get_selection_bounds (CLUTTER_TEXT (actor), start_offset, end_offset); |
| 1275 |
|
| 1276 |
if (*start_offset != *end_offset) |
| 1277 |
return clutter_text_get_selection (CLUTTER_TEXT (actor)); |
| 1278 |
else |
| 1279 |
return NULL; |
| 1280 |
} |
| 1281 |
|
| 1282 |
/* ClutterText only allows one selection. So this method will set the selection |
| 1283 |
if no selection exists, but as in gailentry, it will not change the current |
| 1284 |
selection */ |
| 1285 |
static gboolean |
| 1286 |
cally_text_add_selection (AtkText *text, |
| 1287 |
gint start_offset, |
| 1288 |
gint end_offset) |
| 1289 |
{ |
| 1290 |
ClutterActor *actor; |
| 1291 |
gint select_start, select_end; |
| 1292 |
|
| 1293 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1294 |
if (actor == NULL) /* State is defunct */ |
| 1295 |
return FALSE; |
| 1296 |
|
| 1297 |
_cally_text_get_selection_bounds (CLUTTER_TEXT (actor), |
| 1298 |
&select_start, &select_end); |
| 1299 |
|
| 1300 |
/* Like in gailentry, if there is already a selection, then don't allow another |
| 1301 |
* to be added, since ClutterText only supports one selected region. |
| 1302 |
*/ |
| 1303 |
if (select_start == select_end) |
| 1304 |
{ |
| 1305 |
clutter_text_set_selection (CLUTTER_TEXT (actor), |
| 1306 |
start_offset, end_offset); |
| 1307 |
|
| 1308 |
return TRUE; |
| 1309 |
} |
| 1310 |
else |
| 1311 |
return FALSE; |
| 1312 |
} |
| 1313 |
|
| 1314 |
|
| 1315 |
static gboolean |
| 1316 |
cally_text_remove_selection (AtkText *text, |
| 1317 |
gint selection_num) |
| 1318 |
{ |
| 1319 |
ClutterActor *actor = NULL; |
| 1320 |
gint caret_pos = -1; |
| 1321 |
gint select_start = -1; |
| 1322 |
gint select_end = -1; |
| 1323 |
|
| 1324 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1325 |
if (actor == NULL) /* State is defunct */ |
| 1326 |
return FALSE; |
| 1327 |
|
| 1328 |
/* only one selection is allowed */ |
| 1329 |
if (selection_num != 0) |
| 1330 |
return FALSE; |
| 1331 |
|
| 1332 |
_cally_text_get_selection_bounds (CLUTTER_TEXT (actor), |
| 1333 |
&select_start, &select_end); |
| 1334 |
|
| 1335 |
if (select_start != select_end) |
| 1336 |
{ |
| 1337 |
/* Setting the start & end of the selected region to the caret position |
| 1338 |
* turns off the selection. |
| 1339 |
*/ |
| 1340 |
caret_pos = clutter_text_get_cursor_position (CLUTTER_TEXT (actor)); |
| 1341 |
clutter_text_set_selection (CLUTTER_TEXT (actor), |
| 1342 |
caret_pos, caret_pos); |
| 1343 |
return TRUE; |
| 1344 |
} |
| 1345 |
else |
| 1346 |
return FALSE; |
| 1347 |
} |
| 1348 |
|
| 1349 |
static gboolean |
| 1350 |
cally_text_set_selection (AtkText *text, |
| 1351 |
gint selection_num, |
| 1352 |
gint start_offset, |
| 1353 |
gint end_offset) |
| 1354 |
{ |
| 1355 |
ClutterActor *actor = NULL; |
| 1356 |
gint select_start = -1; |
| 1357 |
gint select_end = -1; |
| 1358 |
|
| 1359 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1360 |
if (actor == NULL) /* State is defunct */ |
| 1361 |
return FALSE; |
| 1362 |
|
| 1363 |
/* Like in gailentry, only let the user move the selection if one is set, |
| 1364 |
* and if the selection_num is 0 |
| 1365 |
*/ |
| 1366 |
if (selection_num != 0) |
| 1367 |
return FALSE; |
| 1368 |
|
| 1369 |
_cally_text_get_selection_bounds (CLUTTER_TEXT (actor), |
| 1370 |
&select_start, &select_end); |
| 1371 |
|
| 1372 |
if (select_start != select_end) |
| 1373 |
{ |
| 1374 |
clutter_text_set_selection (CLUTTER_TEXT (actor), |
| 1375 |
start_offset, end_offset); |
| 1376 |
return TRUE; |
| 1377 |
} |
| 1378 |
else |
| 1379 |
return FALSE; |
| 1380 |
} |
| 1381 |
|
| 1382 |
static AtkAttributeSet* |
| 1383 |
cally_text_get_run_attributes (AtkText *text, |
| 1384 |
gint offset, |
| 1385 |
gint *start_offset, |
| 1386 |
gint *end_offset) |
| 1387 |
{ |
| 1388 |
ClutterActor *actor = NULL; |
| 1389 |
ClutterText *clutter_text = NULL; |
| 1390 |
AtkAttributeSet *at_set = NULL; |
| 1391 |
|
| 1392 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1393 |
if (actor == NULL) /* State is defunct */ |
| 1394 |
return NULL; |
| 1395 |
|
| 1396 |
/* Clutter don't have any reference to the direction*/ |
| 1397 |
|
| 1398 |
clutter_text = CLUTTER_TEXT (actor); |
| 1399 |
|
| 1400 |
at_set = _cally_misc_layout_get_run_attributes (at_set, |
| 1401 |
clutter_text, |
| 1402 |
offset, |
| 1403 |
start_offset, |
| 1404 |
end_offset); |
| 1405 |
|
| 1406 |
return at_set; |
| 1407 |
} |
| 1408 |
|
| 1409 |
static AtkAttributeSet* |
| 1410 |
cally_text_get_default_attributes (AtkText *text) |
| 1411 |
{ |
| 1412 |
ClutterActor *actor = NULL; |
| 1413 |
ClutterText *clutter_text = NULL; |
| 1414 |
AtkAttributeSet *at_set = NULL; |
| 1415 |
|
| 1416 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1417 |
if (actor == NULL) /* State is defunct */ |
| 1418 |
return NULL; |
| 1419 |
|
| 1420 |
clutter_text = CLUTTER_TEXT (actor); |
| 1421 |
|
| 1422 |
at_set = _cally_misc_layout_get_default_attributes (at_set, clutter_text); |
| 1423 |
|
| 1424 |
return at_set; |
| 1425 |
} |
| 1426 |
|
| 1427 |
static void cally_text_get_character_extents (AtkText *text, |
| 1428 |
gint offset, |
| 1429 |
gint *xp, |
| 1430 |
gint *yp, |
| 1431 |
gint *widthp, |
| 1432 |
gint *heightp, |
| 1433 |
AtkCoordType coords) |
| 1434 |
{ |
| 1435 |
ClutterActor *actor = NULL; |
| 1436 |
ClutterText *clutter_text = NULL; |
| 1437 |
gint x = 0, y = 0, width = 0, height = 0; |
| 1438 |
gint index, x_window, y_window, x_toplevel, y_toplevel; |
| 1439 |
gint x_layout, y_layout; |
| 1440 |
PangoLayout *layout; |
| 1441 |
PangoRectangle extents; |
| 1442 |
const gchar *text_value; |
| 1443 |
ClutterVertex verts[4]; |
| 1444 |
|
| 1445 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1446 |
if (actor == NULL) /* State is defunct */ |
| 1447 |
goto done; |
| 1448 |
|
| 1449 |
clutter_text = CLUTTER_TEXT (actor); |
| 1450 |
|
| 1451 |
text_value = clutter_text_get_text (clutter_text); |
| 1452 |
index = g_utf8_offset_to_pointer (text_value, offset) - text_value; |
| 1453 |
|
| 1454 |
layout = clutter_text_get_layout (clutter_text); |
| 1455 |
pango_layout_index_to_pos (layout, index, &extents); |
| 1456 |
|
| 1457 |
/* handle RTL text layout */ |
| 1458 |
if (extents.width < 0) |
| 1459 |
{ |
| 1460 |
extents.x += extents.width; |
| 1461 |
extents.width = -extents.width; |
| 1462 |
} |
| 1463 |
|
| 1464 |
clutter_actor_get_abs_allocation_vertices (actor, verts); |
| 1465 |
x_window = verts[0].x; |
| 1466 |
y_window = verts[0].y; |
| 1467 |
|
| 1468 |
clutter_text_get_layout_offsets (clutter_text, &x_layout, &y_layout); |
| 1469 |
|
| 1470 |
x = (extents.x / PANGO_SCALE) + x_layout + x_window; |
| 1471 |
y = (extents.y / PANGO_SCALE) + y_layout + y_window; |
| 1472 |
width = extents.width / PANGO_SCALE; |
| 1473 |
height = extents.height / PANGO_SCALE; |
| 1474 |
|
| 1475 |
if (coords == ATK_XY_SCREEN) |
| 1476 |
{ |
| 1477 |
_cally_actor_get_top_level_origin (actor, &x_toplevel, &y_toplevel); |
| 1478 |
x += x_toplevel; |
| 1479 |
y += y_toplevel; |
| 1480 |
} |
| 1481 |
|
| 1482 |
done: |
| 1483 |
if (widthp) |
| 1484 |
*widthp = width; |
| 1485 |
|
| 1486 |
if (heightp) |
| 1487 |
*heightp = height; |
| 1488 |
|
| 1489 |
if (xp) |
| 1490 |
*xp = x; |
| 1491 |
|
| 1492 |
if (yp) |
| 1493 |
*yp = y; |
| 1494 |
} |
| 1495 |
|
| 1496 |
static gint |
| 1497 |
cally_text_get_offset_at_point (AtkText *text, |
| 1498 |
gint x, |
| 1499 |
gint y, |
| 1500 |
AtkCoordType coords) |
| 1501 |
{ |
| 1502 |
ClutterActor *actor = NULL; |
| 1503 |
ClutterText *clutter_text = NULL; |
| 1504 |
const gchar *text_value; |
| 1505 |
gint index; |
| 1506 |
|
| 1507 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1508 |
if (actor == NULL) /* State is defunct */ |
| 1509 |
return -1; |
| 1510 |
|
| 1511 |
clutter_text = CLUTTER_TEXT (actor); |
| 1512 |
|
| 1513 |
index = _cally_misc_get_index_at_point (clutter_text, x, y, coords); |
| 1514 |
text_value = clutter_text_get_text (clutter_text); |
| 1515 |
if (index == -1) |
| 1516 |
return g_utf8_strlen (text_value, -1); |
| 1517 |
else |
| 1518 |
return g_utf8_pointer_to_offset (text_value, text_value + index); |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
/******** Auxiliar private methods ******/ |
| 1523 |
|
| 1524 |
/* ClutterText only maintains the current cursor position and a extra selection |
| 1525 |
bound, but this could be before or after the cursor. This method returns |
| 1526 |
the start and end positions in a proper order (so start<=end). This is |
| 1527 |
similar to the function gtk_editable_get_selection_bounds */ |
| 1528 |
static void |
| 1529 |
_cally_text_get_selection_bounds (ClutterText *clutter_text, |
| 1530 |
gint *start_offset, |
| 1531 |
gint *end_offset) |
| 1532 |
{ |
| 1533 |
gint pos = -1; |
| 1534 |
gint selection_bound = -1; |
| 1535 |
|
| 1536 |
pos = clutter_text_get_cursor_position (clutter_text); |
| 1537 |
selection_bound = clutter_text_get_selection_bound (clutter_text); |
| 1538 |
|
| 1539 |
if (pos < selection_bound) |
| 1540 |
{ |
| 1541 |
*start_offset = pos; |
| 1542 |
*end_offset = selection_bound; |
| 1543 |
} |
| 1544 |
else |
| 1545 |
{ |
| 1546 |
*start_offset = selection_bound; |
| 1547 |
*end_offset = pos; |
| 1548 |
} |
| 1549 |
} |
| 1550 |
|
| 1551 |
static void |
| 1552 |
_cally_text_delete_text_cb (ClutterText *clutter_text, |
| 1553 |
gint start_pos, |
| 1554 |
gint end_pos, |
| 1555 |
gpointer data) |
| 1556 |
{ |
| 1557 |
CallyText *cally_text = NULL; |
| 1558 |
|
| 1559 |
g_return_if_fail (CALLY_IS_TEXT (data)); |
| 1560 |
|
| 1561 |
/* Ignore zero lengh deletions */ |
| 1562 |
if (end_pos - start_pos == 0) |
| 1563 |
return; |
| 1564 |
|
| 1565 |
cally_text = CALLY_TEXT (data); |
| 1566 |
|
| 1567 |
if (!cally_text->priv->signal_name_delete) |
| 1568 |
{ |
| 1569 |
cally_text->priv->signal_name_delete = "text_changed::delete"; |
| 1570 |
cally_text->priv->position_delete = start_pos; |
| 1571 |
cally_text->priv->length_delete = end_pos - start_pos; |
| 1572 |
} |
| 1573 |
|
| 1574 |
_notify_delete (cally_text); |
| 1575 |
} |
| 1576 |
|
| 1577 |
static void |
| 1578 |
_cally_text_insert_text_cb (ClutterText *clutter_text, |
| 1579 |
gchar *new_text, |
| 1580 |
gint new_text_length, |
| 1581 |
gint *position, |
| 1582 |
gpointer data) |
| 1583 |
{ |
| 1584 |
CallyText *cally_text = NULL; |
| 1585 |
|
| 1586 |
g_return_if_fail (CALLY_IS_TEXT (data)); |
| 1587 |
|
| 1588 |
cally_text = CALLY_TEXT (data); |
| 1589 |
|
| 1590 |
if (!cally_text->priv->signal_name_insert) |
| 1591 |
{ |
| 1592 |
cally_text->priv->signal_name_insert = "text_changed::insert"; |
| 1593 |
cally_text->priv->position_insert = *position; |
| 1594 |
cally_text->priv->length_insert = g_utf8_strlen (new_text, new_text_length); |
| 1595 |
} |
| 1596 |
|
| 1597 |
/* |
| 1598 |
* The signal will be emitted when the cursor position is updated, |
| 1599 |
* or in an idle handler if it not updated. |
| 1600 |
*/ |
| 1601 |
if (cally_text->priv->insert_idle_handler == 0) |
| 1602 |
cally_text->priv->insert_idle_handler = clutter_threads_add_idle (_idle_notify_insert, |
| 1603 |
cally_text); |
| 1604 |
} |
| 1605 |
|
| 1606 |
/***** atkeditabletext.h ******/ |
| 1607 |
|
| 1608 |
static void |
| 1609 |
cally_text_editable_text_interface_init (AtkEditableTextIface *iface) |
| 1610 |
{ |
| 1611 |
g_return_if_fail (iface != NULL); |
| 1612 |
|
| 1613 |
iface->set_text_contents = cally_text_set_text_contents; |
| 1614 |
iface->insert_text = cally_text_insert_text; |
| 1615 |
iface->delete_text = cally_text_delete_text; |
| 1616 |
|
| 1617 |
iface->set_run_attributes = NULL; |
| 1618 |
iface->copy_text = NULL; |
| 1619 |
iface->cut_text = NULL; |
| 1620 |
iface->paste_text = NULL; |
| 1621 |
} |
| 1622 |
|
| 1623 |
static void |
| 1624 |
cally_text_set_text_contents (AtkEditableText *text, |
| 1625 |
const gchar *string) |
| 1626 |
{ |
| 1627 |
ClutterActor *actor = NULL; |
| 1628 |
|
| 1629 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1630 |
if (actor == NULL) |
| 1631 |
return; |
| 1632 |
|
| 1633 |
if (!clutter_text_get_editable (CLUTTER_TEXT (actor))) |
| 1634 |
return; |
| 1635 |
|
| 1636 |
clutter_text_set_text (CLUTTER_TEXT (actor), |
| 1637 |
string); |
| 1638 |
} |
| 1639 |
|
| 1640 |
|
| 1641 |
static void |
| 1642 |
cally_text_insert_text (AtkEditableText *text, |
| 1643 |
const gchar *string, |
| 1644 |
gint length, |
| 1645 |
gint *position) |
| 1646 |
{ |
| 1647 |
ClutterActor *actor = NULL; |
| 1648 |
|
| 1649 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1650 |
if (actor == NULL) |
| 1651 |
return; |
| 1652 |
|
| 1653 |
if (!clutter_text_get_editable (CLUTTER_TEXT (actor))) |
| 1654 |
return; |
| 1655 |
|
| 1656 |
if (length < 0) |
| 1657 |
length = g_utf8_strlen (string, -1); |
| 1658 |
|
| 1659 |
clutter_text_insert_text (CLUTTER_TEXT (actor), |
| 1660 |
string, *position); |
| 1661 |
|
| 1662 |
/* we suppose that the text insertion will be succesful, |
| 1663 |
clutter-text doesn't warn about it. A option would be search for |
| 1664 |
the text, but it seems not really required */ |
| 1665 |
*position += length; |
| 1666 |
} |
| 1667 |
|
| 1668 |
static void cally_text_delete_text (AtkEditableText *text, |
| 1669 |
gint start_pos, |
| 1670 |
gint end_pos) |
| 1671 |
{ |
| 1672 |
ClutterActor *actor = NULL; |
| 1673 |
|
| 1674 |
actor = CALLY_GET_CLUTTER_ACTOR (text); |
| 1675 |
if (actor == NULL) |
| 1676 |
return; |
| 1677 |
|
| 1678 |
if (!clutter_text_get_editable (CLUTTER_TEXT (actor))) |
| 1679 |
return; |
| 1680 |
|
| 1681 |
clutter_text_delete_text (CLUTTER_TEXT (actor), |
| 1682 |
start_pos, end_pos); |
| 1683 |
} |
| 1684 |
|
| 1685 |
/* CallyActor */ |
| 1686 |
static void |
| 1687 |
cally_text_notify_clutter (GObject *obj, |
| 1688 |
GParamSpec *pspec) |
| 1689 |
{ |
| 1690 |
ClutterText *clutter_text = NULL; |
| 1691 |
CallyText *cally_text = NULL; |
| 1692 |
AtkObject *atk_obj = NULL; |
| 1693 |
|
| 1694 |
clutter_text = CLUTTER_TEXT (obj); |
| 1695 |
atk_obj = clutter_actor_get_accessible (CLUTTER_ACTOR (obj)); |
| 1696 |
cally_text = CALLY_TEXT (atk_obj); |
| 1697 |
|
| 1698 |
if (g_strcmp0 (pspec->name, "position") == 0) |
| 1699 |
{ |
| 1700 |
/* the selection can change also for the cursor position */ |
| 1701 |
if (_check_for_selection_change (cally_text, clutter_text)) |
| 1702 |
g_signal_emit_by_name (atk_obj, "text_selection_changed"); |
| 1703 |
|
| 1704 |
g_signal_emit_by_name (atk_obj, "text_caret_moved", |
| 1705 |
clutter_text_get_cursor_position (clutter_text)); |
| 1706 |
} |
| 1707 |
else if (g_strcmp0 (pspec->name, "selection-bound") == 0) |
| 1708 |
{ |
| 1709 |
if (_check_for_selection_change (cally_text, clutter_text)) |
| 1710 |
g_signal_emit_by_name (atk_obj, "text_selection_changed"); |
| 1711 |
} |
| 1712 |
else if (g_strcmp0 (pspec->name, "editable") == 0) |
| 1713 |
{ |
| 1714 |
atk_object_notify_state_change (atk_obj, ATK_STATE_EDITABLE, |
| 1715 |
clutter_text_get_editable (clutter_text)); |
| 1716 |
} |
| 1717 |
else if (g_strcmp0 (pspec->name, "activatable") == 0) |
| 1718 |
{ |
| 1719 |
_check_activate_action (cally_text, clutter_text); |
| 1720 |
} |
| 1721 |
else if (g_strcmp0 (pspec->name, "password-char") == 0) |
| 1722 |
{ |
| 1723 |
if (clutter_text_get_password_char (clutter_text) != 0) |
| 1724 |
atk_object_set_role (atk_obj, ATK_ROLE_PASSWORD_TEXT); |
| 1725 |
else |
| 1726 |
atk_object_set_role (atk_obj, ATK_ROLE_TEXT); |
| 1727 |
} |
| 1728 |
else |
| 1729 |
{ |
| 1730 |
CALLY_ACTOR_CLASS (cally_text_parent_class)->notify_clutter (obj, pspec); |
| 1731 |
} |
| 1732 |
} |
| 1733 |
|
| 1734 |
static gboolean |
| 1735 |
_check_for_selection_change (CallyText *cally_text, |
| 1736 |
ClutterText *clutter_text) |
| 1737 |
{ |
| 1738 |
gboolean ret_val = FALSE; |
| 1739 |
gint clutter_pos = -1; |
| 1740 |
gint clutter_bound = -1; |
| 1741 |
|
| 1742 |
clutter_pos = clutter_text_get_cursor_position (clutter_text); |
| 1743 |
clutter_bound = clutter_text_get_selection_bound (clutter_text); |
| 1744 |
|
| 1745 |
if (clutter_pos != clutter_bound) |
| 1746 |
{ |
| 1747 |
if (clutter_pos != cally_text->priv->cursor_position || |
| 1748 |
clutter_bound != cally_text->priv->selection_bound) |
| 1749 |
/* |
| 1750 |
* This check is here as this function can be called for |
| 1751 |
* notification of selection_bound and current_pos. The |
| 1752 |
* values of current_pos and selection_bound may be the same |
| 1753 |
* for both notifications and we only want to generate one |
| 1754 |
* text_selection_changed signal. |
| 1755 |
*/ |
| 1756 |
ret_val = TRUE; |
| 1757 |
} |
| 1758 |
else |
| 1759 |
{ |
| 1760 |
/* We had a selection */ |
| 1761 |
ret_val = (cally_text->priv->cursor_position != cally_text->priv->selection_bound); |
| 1762 |
} |
| 1763 |
|
| 1764 |
cally_text->priv->cursor_position = clutter_pos; |
| 1765 |
cally_text->priv->selection_bound = clutter_bound; |
| 1766 |
|
| 1767 |
return ret_val; |
| 1768 |
} |
| 1769 |
|
| 1770 |
static gboolean |
| 1771 |
_idle_notify_insert (gpointer data) |
| 1772 |
{ |
| 1773 |
CallyText *cally_text = NULL; |
| 1774 |
|
| 1775 |
cally_text = CALLY_TEXT (data); |
| 1776 |
cally_text->priv->insert_idle_handler = 0; |
| 1777 |
|
| 1778 |
_notify_insert (cally_text); |
| 1779 |
|
| 1780 |
return FALSE; |
| 1781 |
} |
| 1782 |
|
| 1783 |
static void |
| 1784 |
_notify_insert (CallyText *cally_text) |
| 1785 |
{ |
| 1786 |
if (cally_text->priv->signal_name_insert) |
| 1787 |
{ |
| 1788 |
g_signal_emit_by_name (cally_text, |
| 1789 |
cally_text->priv->signal_name_insert, |
| 1790 |
cally_text->priv->position_insert, |
| 1791 |
cally_text->priv->length_insert); |
| 1792 |
cally_text->priv->signal_name_insert = NULL; |
| 1793 |
} |
| 1794 |
} |
| 1795 |
|
| 1796 |
static void |
| 1797 |
_notify_delete (CallyText *cally_text) |
| 1798 |
{ |
| 1799 |
if (cally_text->priv->signal_name_delete) |
| 1800 |
{ |
| 1801 |
g_signal_emit_by_name (cally_text, |
| 1802 |
cally_text->priv->signal_name_delete, |
| 1803 |
cally_text->priv->position_delete, |
| 1804 |
cally_text->priv->length_delete); |
| 1805 |
cally_text->priv->signal_name_delete = NULL; |
| 1806 |
} |
| 1807 |
} |
| 1808 |
/* atkaction */ |
| 1809 |
|
| 1810 |
static void |
| 1811 |
_cally_text_activate_action (CallyActor *cally_actor) |
| 1812 |
{ |
| 1813 |
ClutterActor *actor = NULL; |
| 1814 |
|
| 1815 |
actor = CALLY_GET_CLUTTER_ACTOR (cally_actor); |
| 1816 |
|
| 1817 |
clutter_text_activate (CLUTTER_TEXT (actor)); |
| 1818 |
} |
| 1819 |
|
| 1820 |
static void |
| 1821 |
_check_activate_action (CallyText *cally_text, |
| 1822 |
ClutterText *clutter_text) |
| 1823 |
{ |
| 1824 |
|
| 1825 |
if (clutter_text_get_activatable (clutter_text)) |
| 1826 |
{ |
| 1827 |
if (cally_text->priv->activate_action_id != 0) |
| 1828 |
return; |
| 1829 |
|
| 1830 |
cally_text->priv->activate_action_id = cally_actor_add_action (CALLY_ACTOR (cally_text), |
| 1831 |
"activate", NULL, NULL, |
| 1832 |
_cally_text_activate_action); |
| 1833 |
} |
| 1834 |
else |
| 1835 |
{ |
| 1836 |
if (cally_text->priv->activate_action_id == 0) |
| 1837 |
return; |
| 1838 |
|
| 1839 |
if (cally_actor_remove_action (CALLY_ACTOR (cally_text), |
| 1840 |
cally_text->priv->activate_action_id)) |
| 1841 |
{ |
| 1842 |
cally_text->priv->activate_action_id = 0; |
| 1843 |
} |
| 1844 |
} |
| 1845 |
} |
| 1846 |
|
| 1847 |
/* GailTextUtil/GailMisc reimplementation methods */ |
| 1848 |
|
| 1849 |
/** |
| 1850 |
* _cally_misc_add_attribute: |
| 1851 |
* |
| 1852 |
* Reimplementation of gail_misc_layout_get_run_attributes (check this |
| 1853 |
* function for more documentation). |
| 1854 |
* |
| 1855 |
* Returns: A pointer to the new #AtkAttributeSet. |
| 1856 |
**/ |
| 1857 |
static AtkAttributeSet* |
| 1858 |
_cally_misc_add_attribute (AtkAttributeSet *attrib_set, |
| 1859 |
AtkTextAttribute attr, |
| 1860 |
gchar *value) |
| 1861 |
{ |
| 1862 |
AtkAttributeSet *return_set; |
| 1863 |
AtkAttribute *at = g_malloc (sizeof (AtkAttribute)); |
| 1864 |
at->name = g_strdup (atk_text_attribute_get_name (attr)); |
| 1865 |
at->value = value; |
| 1866 |
return_set = g_slist_prepend(attrib_set, at); |
| 1867 |
return return_set; |
| 1868 |
} |
| 1869 |
|
| 1870 |
|
| 1871 |
static gint |
| 1872 |
_cally_atk_attribute_lookup_func (gconstpointer data, |
| 1873 |
gconstpointer user_data) |
| 1874 |
{ |
| 1875 |
AtkTextAttribute attr = (AtkTextAttribute) user_data; |
| 1876 |
AtkAttribute *at = (AtkAttribute *) data; |
| 1877 |
if (!g_strcmp0 (at->name, atk_text_attribute_get_name (attr))) |
| 1878 |
return 0; |
| 1879 |
return -1; |
| 1880 |
} |
| 1881 |
|
| 1882 |
static gboolean |
| 1883 |
_cally_misc_find_atk_attribute (AtkAttributeSet *attrib_set, |
| 1884 |
AtkTextAttribute attr) |
| 1885 |
{ |
| 1886 |
GSList* result = g_slist_find_custom ((GSList*) attrib_set, |
| 1887 |
(gconstpointer) attr, |
| 1888 |
_cally_atk_attribute_lookup_func); |
| 1889 |
return (result != NULL); |
| 1890 |
} |
| 1891 |
|
| 1892 |
/** |
| 1893 |
* _cally_misc_layout_atk_attributes_from_pango: |
| 1894 |
* |
| 1895 |
* Store the pango attributes as their ATK equivalent in an existing |
| 1896 |
* #AtkAttributeSet. |
| 1897 |
* |
| 1898 |
* Returns: A pointer to the updated #AtkAttributeSet. |
| 1899 |
**/ |
| 1900 |
static AtkAttributeSet* |
| 1901 |
_cally_misc_layout_atk_attributes_from_pango (AtkAttributeSet *attrib_set, |
| 1902 |
PangoAttrIterator *iter) |
| 1903 |
{ |
| 1904 |
PangoAttrString *pango_string; |
| 1905 |
PangoAttrInt *pango_int; |
| 1906 |
PangoAttrColor *pango_color; |
| 1907 |
PangoAttrLanguage *pango_lang; |
| 1908 |
PangoAttrFloat *pango_float; |
| 1909 |
gchar *value = NULL; |
| 1910 |
|
| 1911 |
if ((pango_string = (PangoAttrString*) pango_attr_iterator_get (iter, |
| 1912 |
PANGO_ATTR_FAMILY)) != NULL) |
| 1913 |
{ |
| 1914 |
value = g_strdup_printf("%s", pango_string->value); |
| 1915 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1916 |
ATK_TEXT_ATTR_FAMILY_NAME, |
| 1917 |
value); |
| 1918 |
} |
| 1919 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1920 |
PANGO_ATTR_STYLE)) != NULL) |
| 1921 |
{ |
| 1922 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1923 |
ATK_TEXT_ATTR_STYLE, |
| 1924 |
g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STYLE, pango_int->value))); |
| 1925 |
} |
| 1926 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1927 |
PANGO_ATTR_WEIGHT)) != NULL) |
| 1928 |
{ |
| 1929 |
value = g_strdup_printf("%i", pango_int->value); |
| 1930 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1931 |
ATK_TEXT_ATTR_WEIGHT, |
| 1932 |
value); |
| 1933 |
} |
| 1934 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1935 |
PANGO_ATTR_VARIANT)) != NULL) |
| 1936 |
{ |
| 1937 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1938 |
ATK_TEXT_ATTR_VARIANT, |
| 1939 |
g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_VARIANT, pango_int->value))); |
| 1940 |
} |
| 1941 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1942 |
PANGO_ATTR_STRETCH)) != NULL) |
| 1943 |
{ |
| 1944 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1945 |
ATK_TEXT_ATTR_STRETCH, |
| 1946 |
g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STRETCH, pango_int->value))); |
| 1947 |
} |
| 1948 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1949 |
PANGO_ATTR_SIZE)) != NULL) |
| 1950 |
{ |
| 1951 |
value = g_strdup_printf("%i", pango_int->value / PANGO_SCALE); |
| 1952 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1953 |
ATK_TEXT_ATTR_SIZE, |
| 1954 |
value); |
| 1955 |
} |
| 1956 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1957 |
PANGO_ATTR_UNDERLINE)) != NULL) |
| 1958 |
{ |
| 1959 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1960 |
ATK_TEXT_ATTR_UNDERLINE, |
| 1961 |
g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_UNDERLINE, pango_int->value))); |
| 1962 |
} |
| 1963 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1964 |
PANGO_ATTR_STRIKETHROUGH)) != NULL) |
| 1965 |
{ |
| 1966 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1967 |
ATK_TEXT_ATTR_STRIKETHROUGH, |
| 1968 |
g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STRIKETHROUGH, pango_int->value))); |
| 1969 |
} |
| 1970 |
if ((pango_int = (PangoAttrInt*) pango_attr_iterator_get (iter, |
| 1971 |
PANGO_ATTR_RISE)) != NULL) |
| 1972 |
{ |
| 1973 |
value = g_strdup_printf("%i", pango_int->value); |
| 1974 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1975 |
ATK_TEXT_ATTR_RISE, |
| 1976 |
value); |
| 1977 |
} |
| 1978 |
if ((pango_lang = (PangoAttrLanguage*) pango_attr_iterator_get (iter, |
| 1979 |
PANGO_ATTR_LANGUAGE)) != NULL) |
| 1980 |
{ |
| 1981 |
value = g_strdup( pango_language_to_string( pango_lang->value)); |
| 1982 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1983 |
ATK_TEXT_ATTR_LANGUAGE, |
| 1984 |
value); |
| 1985 |
} |
| 1986 |
if ((pango_float = (PangoAttrFloat*) pango_attr_iterator_get (iter, |
| 1987 |
PANGO_ATTR_SCALE)) != NULL) |
| 1988 |
{ |
| 1989 |
value = g_strdup_printf("%g", pango_float->value); |
| 1990 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 1991 |
ATK_TEXT_ATTR_SCALE, |
| 1992 |
value); |
| 1993 |
} |
| 1994 |
if ((pango_color = (PangoAttrColor*) pango_attr_iterator_get (iter, |
| 1995 |
PANGO_ATTR_FOREGROUND)) != NULL) |
| 1996 |
{ |
| 1997 |
value = g_strdup_printf ("%u,%u,%u", |
| 1998 |
pango_color->color.red, |
| 1999 |
pango_color->color.green, |
| 2000 |
pango_color->color.blue); |
| 2001 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2002 |
ATK_TEXT_ATTR_FG_COLOR, |
| 2003 |
value); |
| 2004 |
} |
| 2005 |
if ((pango_color = (PangoAttrColor*) pango_attr_iterator_get (iter, |
| 2006 |
PANGO_ATTR_BACKGROUND)) != NULL) |
| 2007 |
{ |
| 2008 |
value = g_strdup_printf ("%u,%u,%u", |
| 2009 |
pango_color->color.red, |
| 2010 |
pango_color->color.green, |
| 2011 |
pango_color->color.blue); |
| 2012 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2013 |
ATK_TEXT_ATTR_BG_COLOR, |
| 2014 |
value); |
| 2015 |
} |
| 2016 |
|
| 2017 |
return attrib_set; |
| 2018 |
} |
| 2019 |
|
| 2020 |
static AtkAttributeSet* |
| 2021 |
_cally_misc_add_actor_color_to_attribute_set (AtkAttributeSet *attrib_set, |
| 2022 |
ClutterText *clutter_text) |
| 2023 |
{ |
| 2024 |
ClutterColor color; |
| 2025 |
gchar *value; |
| 2026 |
|
| 2027 |
clutter_text_get_color (clutter_text, &color); |
| 2028 |
value = g_strdup_printf ("%u,%u,%u", |
| 2029 |
(guint) (color.red * 65535 / 255), |
| 2030 |
(guint) (color.green * 65535 / 255), |
| 2031 |
(guint) (color.blue * 65535 / 255)); |
| 2032 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2033 |
ATK_TEXT_ATTR_FG_COLOR, |
| 2034 |
value); |
| 2035 |
return attrib_set; |
| 2036 |
} |
| 2037 |
|
| 2038 |
|
| 2039 |
/** |
| 2040 |
* _cally_misc_layout_get_run_attributes: |
| 2041 |
* |
| 2042 |
* Reimplementation of gail_misc_layout_get_run_attributes (check this |
| 2043 |
* function for more documentation). |
| 2044 |
* |
| 2045 |
* Returns: A pointer to the #AtkAttributeSet. |
| 2046 |
**/ |
| 2047 |
static AtkAttributeSet* |
| 2048 |
_cally_misc_layout_get_run_attributes (AtkAttributeSet *attrib_set, |
| 2049 |
ClutterText *clutter_text, |
| 2050 |
gint offset, |
| 2051 |
gint *start_offset, |
| 2052 |
gint *end_offset) |
| 2053 |
{ |
| 2054 |
PangoAttrIterator *iter; |
| 2055 |
PangoAttrList *attr; |
| 2056 |
gint index, start_index, end_index; |
| 2057 |
gboolean is_next = TRUE; |
| 2058 |
glong len; |
| 2059 |
PangoLayout *layout = clutter_text_get_layout (clutter_text); |
| 2060 |
gchar *text = (gchar*) clutter_text_get_text (clutter_text); |
| 2061 |
|
| 2062 |
len = g_utf8_strlen (text, -1); |
| 2063 |
/* Grab the attributes of the PangoLayout, if any */ |
| 2064 |
if ((attr = pango_layout_get_attributes (layout)) == NULL) |
| 2065 |
{ |
| 2066 |
*start_offset = 0; |
| 2067 |
*end_offset = len; |
| 2068 |
_cally_misc_add_actor_color_to_attribute_set (attrib_set, clutter_text); |
| 2069 |
} |
| 2070 |
else |
| 2071 |
{ |
| 2072 |
iter = pango_attr_list_get_iterator (attr); |
| 2073 |
/* Get invariant range offsets */ |
| 2074 |
/* If offset out of range, set offset in range */ |
| 2075 |
if (offset > len) |
| 2076 |
offset = len; |
| 2077 |
else if (offset < 0) |
| 2078 |
offset = 0; |
| 2079 |
|
| 2080 |
index = g_utf8_offset_to_pointer (text, offset) - text; |
| 2081 |
pango_attr_iterator_range (iter, &start_index, &end_index); |
| 2082 |
while (is_next) |
| 2083 |
{ |
| 2084 |
if (index >= start_index && index < end_index) |
| 2085 |
{ |
| 2086 |
*start_offset = g_utf8_pointer_to_offset (text, |
| 2087 |
text + start_index); |
| 2088 |
if (end_index == G_MAXINT) |
| 2089 |
/* Last iterator */ |
| 2090 |
end_index = len; |
| 2091 |
|
| 2092 |
*end_offset = g_utf8_pointer_to_offset (text, |
| 2093 |
text + end_index); |
| 2094 |
break; |
| 2095 |
} |
| 2096 |
is_next = pango_attr_iterator_next (iter); |
| 2097 |
pango_attr_iterator_range (iter, &start_index, &end_index); |
| 2098 |
} |
| 2099 |
|
| 2100 |
/* Get attributes */ |
| 2101 |
attrib_set = _cally_misc_layout_atk_attributes_from_pango (attrib_set, iter); |
| 2102 |
pango_attr_iterator_destroy (iter); |
| 2103 |
} |
| 2104 |
|
| 2105 |
if (!_cally_misc_find_atk_attribute (attrib_set, ATK_TEXT_ATTR_FG_COLOR)) |
| 2106 |
attrib_set = _cally_misc_add_actor_color_to_attribute_set (attrib_set, clutter_text); |
| 2107 |
|
| 2108 |
return attrib_set; |
| 2109 |
} |
| 2110 |
|
| 2111 |
|
| 2112 |
/** |
| 2113 |
* _cally_misc_layout_get_default_attributes: |
| 2114 |
* |
| 2115 |
* Reimplementation of gail_misc_layout_get_default_attributes (check this |
| 2116 |
* function for more documentation). |
| 2117 |
* |
| 2118 |
* Returns: A pointer to the #AtkAttributeSet. |
| 2119 |
**/ |
| 2120 |
static AtkAttributeSet* |
| 2121 |
_cally_misc_layout_get_default_attributes (AtkAttributeSet *attrib_set, |
| 2122 |
ClutterText *clutter_text) |
| 2123 |
{ |
| 2124 |
PangoLayout *layout; |
| 2125 |
PangoContext *context; |
| 2126 |
PangoLanguage* language; |
| 2127 |
PangoFontDescription* font; |
| 2128 |
PangoWrapMode mode; |
| 2129 |
gchar *value = NULL; |
| 2130 |
gint int_value; |
| 2131 |
ClutterTextDirection text_direction; |
| 2132 |
PangoAttrIterator *iter; |
| 2133 |
PangoAttrList *attr; |
| 2134 |
|
| 2135 |
text_direction = clutter_actor_get_text_direction (CLUTTER_ACTOR (clutter_text)); |
| 2136 |
switch (text_direction) |
| 2137 |
{ |
| 2138 |
case CLUTTER_TEXT_DIRECTION_DEFAULT: |
| 2139 |
value = g_strdup ("none"); |
| 2140 |
break; |
| 2141 |
|
| 2142 |
case CLUTTER_TEXT_DIRECTION_LTR: |
| 2143 |
value = g_strdup ("ltr"); |
| 2144 |
break; |
| 2145 |
|
| 2146 |
case CLUTTER_TEXT_DIRECTION_RTL: |
| 2147 |
value = g_strdup ("rtl"); |
| 2148 |
break; |
| 2149 |
|
| 2150 |
default: |
| 2151 |
value = g_strdup ("none"); |
| 2152 |
break; |
| 2153 |
} |
| 2154 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2155 |
ATK_TEXT_ATTR_DIRECTION, |
| 2156 |
value); |
| 2157 |
|
| 2158 |
layout = clutter_text_get_layout (clutter_text); |
| 2159 |
context = pango_layout_get_context (layout); |
| 2160 |
if (context) |
| 2161 |
{ |
| 2162 |
if ((language = pango_context_get_language (context))) |
| 2163 |
{ |
| 2164 |
value = g_strdup (pango_language_to_string (language)); |
| 2165 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2166 |
ATK_TEXT_ATTR_LANGUAGE, value); |
| 2167 |
} |
| 2168 |
|
| 2169 |
if ((font = pango_context_get_font_description (context))) |
| 2170 |
{ |
| 2171 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STYLE, |
| 2172 |
pango_font_description_get_style (font))); |
| 2173 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2174 |
ATK_TEXT_ATTR_STYLE, |
| 2175 |
value); |
| 2176 |
|
| 2177 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_VARIANT, |
| 2178 |
pango_font_description_get_variant (font))); |
| 2179 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2180 |
ATK_TEXT_ATTR_VARIANT, value); |
| 2181 |
|
| 2182 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_STRETCH, |
| 2183 |
pango_font_description_get_stretch (font))); |
| 2184 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2185 |
ATK_TEXT_ATTR_STRETCH, value); |
| 2186 |
|
| 2187 |
value = g_strdup (pango_font_description_get_family (font)); |
| 2188 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2189 |
ATK_TEXT_ATTR_FAMILY_NAME, value); |
| 2190 |
value = g_strdup_printf ("%d", pango_font_description_get_weight (font)); |
| 2191 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2192 |
ATK_TEXT_ATTR_WEIGHT, value); |
| 2193 |
|
| 2194 |
value = g_strdup_printf ("%i", pango_font_description_get_size (font) / PANGO_SCALE); |
| 2195 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2196 |
ATK_TEXT_ATTR_SIZE, value); |
| 2197 |
|
| 2198 |
} |
| 2199 |
|
| 2200 |
} |
| 2201 |
|
| 2202 |
if (pango_layout_get_justify (layout)) |
| 2203 |
int_value = 3; |
| 2204 |
else |
| 2205 |
{ |
| 2206 |
PangoAlignment align; |
| 2207 |
|
| 2208 |
align = pango_layout_get_alignment (layout); |
| 2209 |
if (align == PANGO_ALIGN_LEFT) |
| 2210 |
int_value = 0; |
| 2211 |
else if (align == PANGO_ALIGN_CENTER) |
| 2212 |
int_value = 2; |
| 2213 |
else /* if (align == PANGO_ALIGN_RIGHT) */ |
| 2214 |
int_value = 1; |
| 2215 |
} |
| 2216 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_JUSTIFICATION, |
| 2217 |
int_value)); |
| 2218 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2219 |
ATK_TEXT_ATTR_JUSTIFICATION, |
| 2220 |
value); |
| 2221 |
|
| 2222 |
mode = pango_layout_get_wrap (layout); |
| 2223 |
if (mode == PANGO_WRAP_WORD) |
| 2224 |
int_value = 2; |
| 2225 |
else /* if (mode == PANGO_WRAP_CHAR) */ |
| 2226 |
int_value = 1; |
| 2227 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_WRAP_MODE, |
| 2228 |
int_value)); |
| 2229 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2230 |
ATK_TEXT_ATTR_WRAP_MODE, value); |
| 2231 |
|
| 2232 |
if ((attr = clutter_text_get_attributes (clutter_text))) |
| 2233 |
{ |
| 2234 |
iter = pango_attr_list_get_iterator (attr); |
| 2235 |
/* Get attributes */ |
| 2236 |
attrib_set = _cally_misc_layout_atk_attributes_from_pango (attrib_set, iter); |
| 2237 |
pango_attr_iterator_destroy (iter); |
| 2238 |
} |
| 2239 |
|
| 2240 |
|
| 2241 |
if (!_cally_misc_find_atk_attribute (attrib_set, ATK_TEXT_ATTR_FG_COLOR)) |
| 2242 |
attrib_set = _cally_misc_add_actor_color_to_attribute_set (attrib_set, |
| 2243 |
clutter_text); |
| 2244 |
|
| 2245 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_FG_STIPPLE, 0)); |
| 2246 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2247 |
ATK_TEXT_ATTR_FG_STIPPLE, |
| 2248 |
value); |
| 2249 |
|
| 2250 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_BG_STIPPLE, 0)); |
| 2251 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2252 |
ATK_TEXT_ATTR_BG_STIPPLE, |
| 2253 |
value); |
| 2254 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2255 |
ATK_TEXT_ATTR_BG_FULL_HEIGHT, |
| 2256 |
g_strdup_printf ("%i", 0)); |
| 2257 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2258 |
ATK_TEXT_ATTR_PIXELS_INSIDE_WRAP, |
| 2259 |
g_strdup_printf ("%i", 0)); |
| 2260 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2261 |
ATK_TEXT_ATTR_PIXELS_BELOW_LINES, |
| 2262 |
g_strdup_printf ("%i", 0)); |
| 2263 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2264 |
ATK_TEXT_ATTR_PIXELS_ABOVE_LINES, |
| 2265 |
g_strdup_printf ("%i", 0)); |
| 2266 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_EDITABLE, |
| 2267 |
clutter_text_get_editable (clutter_text))); |
| 2268 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2269 |
ATK_TEXT_ATTR_EDITABLE, |
| 2270 |
value); |
| 2271 |
|
| 2272 |
value = g_strdup (atk_text_attribute_get_value (ATK_TEXT_ATTR_INVISIBLE, |
| 2273 |
!clutter_actor_is_visible (CLUTTER_ACTOR (clutter_text)))); |
| 2274 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2275 |
ATK_TEXT_ATTR_INVISIBLE, value); |
| 2276 |
|
| 2277 |
value = g_strdup_printf ("%i", pango_layout_get_indent (layout)); |
| 2278 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2279 |
ATK_TEXT_ATTR_INDENT, value); |
| 2280 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2281 |
ATK_TEXT_ATTR_RIGHT_MARGIN, |
| 2282 |
g_strdup_printf ("%i", 0)); |
| 2283 |
attrib_set = _cally_misc_add_attribute (attrib_set, |
| 2284 |
ATK_TEXT_ATTR_LEFT_MARGIN, |
| 2285 |
g_strdup_printf ("%i", 0)); |
| 2286 |
|
| 2287 |
return attrib_set; |
| 2288 |
} |
| 2289 |
|
| 2290 |
static int |
| 2291 |
_cally_misc_get_index_at_point (ClutterText *clutter_text, |
| 2292 |
gint x, |
| 2293 |
gint y, |
| 2294 |
AtkCoordType coords) |
| 2295 |
{ |
| 2296 |
gint index, x_window, y_window, x_toplevel, y_toplevel; |
| 2297 |
gint x_temp, y_temp; |
| 2298 |
gboolean ret; |
| 2299 |
ClutterVertex verts[4]; |
| 2300 |
PangoLayout *layout; |
| 2301 |
gint x_layout, y_layout; |
| 2302 |
|
| 2303 |
clutter_text_get_layout_offsets (clutter_text, &x_layout, &y_layout); |
| 2304 |
|
| 2305 |
clutter_actor_get_abs_allocation_vertices (CLUTTER_ACTOR (clutter_text), verts); |
| 2306 |
x_window = verts[0].x; |
| 2307 |
y_window = verts[0].y; |
| 2308 |
|
| 2309 |
x_temp = x - x_layout - x_window; |
| 2310 |
y_temp = y - y_layout - y_window; |
| 2311 |
|
| 2312 |
if (coords == ATK_XY_SCREEN) |
| 2313 |
{ |
| 2314 |
_cally_actor_get_top_level_origin (CLUTTER_ACTOR (clutter_text), &x_toplevel, |
| 2315 |
&y_toplevel); |
| 2316 |
x_temp -= x_toplevel; |
| 2317 |
y_temp -= y_toplevel; |
| 2318 |
} |
| 2319 |
|
| 2320 |
layout = clutter_text_get_layout (clutter_text); |
| 2321 |
ret = pango_layout_xy_to_index (layout, |
| 2322 |
x_temp * PANGO_SCALE, |
| 2323 |
y_temp * PANGO_SCALE, |
| 2324 |
&index, NULL); |
| 2325 |
|
| 2326 |
if (!ret) |
| 2327 |
{ |
| 2328 |
if (x_temp < 0 || y_temp < 0) |
| 2329 |
index = 0; |
| 2330 |
else |
| 2331 |
index = -1; |
| 2332 |
} |
| 2333 |
return index; |
| 2334 |
} |