gtktexthandle.c 22.8 KB
Newer Older
Carlos Garnacho's avatar
Carlos Garnacho committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* GTK - The GIMP Toolkit
 * Copyright © 2012 Carlos Garnacho <carlosg@gnome.org>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"
#include "gtkprivatetypebuiltins.h"
#include "gtktexthandleprivate.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
23
#include "gtkwindowprivate.h"
Carlos Garnacho's avatar
Carlos Garnacho committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#include "gtkintl.h"

#include <gtk/gtk.h>

typedef struct _GtkTextHandlePrivate GtkTextHandlePrivate;
typedef struct _HandleWindow HandleWindow;

enum {
  HANDLE_DRAGGED,
  DRAG_FINISHED,
  LAST_SIGNAL
};

enum {
  PROP_0,
39
  PROP_PARENT
Carlos Garnacho's avatar
Carlos Garnacho committed
40 41 42 43
};

struct _HandleWindow
{
44
  GtkWidget *widget;
Carlos Garnacho's avatar
Carlos Garnacho committed
45 46 47 48
  GdkRectangle pointing_to;
  gint dx;
  gint dy;
  guint dragged : 1;
49 50 51
  guint mode_visible : 1;
  guint user_visible : 1;
  guint has_point : 1;
Carlos Garnacho's avatar
Carlos Garnacho committed
52 53 54 55 56 57
};

struct _GtkTextHandlePrivate
{
  HandleWindow windows[2];
  GtkWidget *parent;
58 59 60
  GtkScrollable *parent_scrollable;
  GtkAdjustment *vadj;
  GtkAdjustment *hadj;
61
  guint hierarchy_changed_id;
62
  guint scrollable_notify_id;
Carlos Garnacho's avatar
Carlos Garnacho committed
63 64 65
  guint mode : 2;
};

66
G_DEFINE_TYPE_WITH_PRIVATE (GtkTextHandle, _gtk_text_handle, G_TYPE_OBJECT)
Carlos Garnacho's avatar
Carlos Garnacho committed
67 68 69

static guint signals[LAST_SIGNAL] = { 0 };

70 71 72
static void _gtk_text_handle_update (GtkTextHandle         *handle,
                                     GtkTextHandlePosition  pos);

Carlos Garnacho's avatar
Carlos Garnacho committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
static void
_gtk_text_handle_get_size (GtkTextHandle *handle,
                           gint          *width,
                           gint          *height)
{
  GtkTextHandlePrivate *priv;
  gint w, h;

  priv = handle->priv;

  gtk_widget_style_get (priv->parent,
                        "text-handle-width", &w,
                        "text-handle-height", &h,
                        NULL);
  if (width)
    *width = w;

  if (height)
    *height = h;
}

static void
_gtk_text_handle_draw (GtkTextHandle         *handle,
                       cairo_t               *cr,
                       GtkTextHandlePosition  pos)
{
  GtkTextHandlePrivate *priv;
100
  GtkStyleContext *context;
Carlos Garnacho's avatar
Carlos Garnacho committed
101 102 103
  gint width, height;

  priv = handle->priv;
104
  context = gtk_widget_get_style_context (priv->windows[pos].widget);
105
  _gtk_text_handle_get_size (handle, &width, &height);
106

Carlos Garnacho's avatar
Carlos Garnacho committed
107 108
  cairo_save (cr);

109
  if (pos == GTK_TEXT_HANDLE_POSITION_SELECTION_END)
110 111 112
    cairo_translate (cr, width, priv->windows[pos].pointing_to.height);
  else
    cairo_translate (cr, width, height);
113

114 115
  gtk_style_context_save (context);
  gtk_style_context_add_class (context,
116
                               GTK_STYLE_CLASS_CURSOR_HANDLE);
Carlos Garnacho's avatar
Carlos Garnacho committed
117 118

  if (pos == GTK_TEXT_HANDLE_POSITION_SELECTION_END)
119
    {
120
      gtk_style_context_add_class (context,
121 122 123
                                   GTK_STYLE_CLASS_BOTTOM);

      if (priv->mode == GTK_TEXT_HANDLE_MODE_CURSOR)
124
        gtk_style_context_add_class (context,
125 126
                                     GTK_STYLE_CLASS_INSERTION_CURSOR);
    }
Carlos Garnacho's avatar
Carlos Garnacho committed
127
  else
128
    gtk_style_context_add_class (context,
129
                                 GTK_STYLE_CLASS_TOP);
Carlos Garnacho's avatar
Carlos Garnacho committed
130

131
  gtk_render_background (context, cr, 0, 0, width, height);
132 133
  gtk_render_frame (context, cr, 0, 0, width, height);
  gtk_render_handle (context, cr, 0, 0, width, height);
Carlos Garnacho's avatar
Carlos Garnacho committed
134

135
  gtk_style_context_restore (context);
Carlos Garnacho's avatar
Carlos Garnacho committed
136 137 138
  cairo_restore (cr);
}

139 140 141
static gint
_text_handle_pos_from_widget (GtkTextHandle *handle,
                              GtkWidget     *widget)
Carlos Garnacho's avatar
Carlos Garnacho committed
142
{
143
  GtkTextHandlePrivate *priv = handle->priv;
Carlos Garnacho's avatar
Carlos Garnacho committed
144

145 146 147 148
  if (widget == priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget)
    return GTK_TEXT_HANDLE_POSITION_SELECTION_START;
  else if (widget == priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget)
    return GTK_TEXT_HANDLE_POSITION_SELECTION_END;
Carlos Garnacho's avatar
Carlos Garnacho committed
149
  else
150
    return -1;
Carlos Garnacho's avatar
Carlos Garnacho committed
151 152 153 154 155 156 157
}

static gboolean
gtk_text_handle_widget_draw (GtkWidget     *widget,
                             cairo_t       *cr,
                             GtkTextHandle *handle)
{
158
  gint pos;
Carlos Garnacho's avatar
Carlos Garnacho committed
159

160
  pos = _text_handle_pos_from_widget (handle, widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
161

162
  if (pos < 0)
Carlos Garnacho's avatar
Carlos Garnacho committed
163 164
    return FALSE;

165 166
  _gtk_text_handle_draw (handle, cr, pos);
  return TRUE;
Carlos Garnacho's avatar
Carlos Garnacho committed
167 168
}

169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
static void
gtk_text_handle_set_state (GtkTextHandle *handle,
                           GtkStateFlags  state)
{
  GtkTextHandlePrivate *priv;
  gint i;

  priv = handle->priv;

  for (i = 0; i <= GTK_TEXT_HANDLE_POSITION_SELECTION_START; i++)
    {
      if (!priv->windows[i].widget)
        continue;

      gtk_widget_set_state_flags (priv->windows[i].widget, state, TRUE);
      gtk_widget_queue_draw (priv->windows[i].widget);
    }
}

Carlos Garnacho's avatar
Carlos Garnacho committed
188 189 190 191 192 193
static gboolean
gtk_text_handle_widget_event (GtkWidget     *widget,
                              GdkEvent      *event,
                              GtkTextHandle *handle)
{
  GtkTextHandlePrivate *priv;
194
  gint pos;
Carlos Garnacho's avatar
Carlos Garnacho committed
195 196

  priv = handle->priv;
197
  pos = _text_handle_pos_from_widget (handle, widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
198

199
  if (pos < 0)
Carlos Garnacho's avatar
Carlos Garnacho committed
200 201 202 203 204 205 206
    return FALSE;

  if (event->type == GDK_BUTTON_PRESS)
    {
      priv->windows[pos].dx = event->button.x;
      priv->windows[pos].dy = event->button.y;
      priv->windows[pos].dragged = TRUE;
207
      gtk_text_handle_set_state (handle, GTK_STATE_FLAG_ACTIVE);
Carlos Garnacho's avatar
Carlos Garnacho committed
208 209 210 211 212
    }
  else if (event->type == GDK_BUTTON_RELEASE)
    {
      g_signal_emit (handle, signals[DRAG_FINISHED], 0, pos);
      priv->windows[pos].dragged = FALSE;
213
      gtk_text_handle_set_state (handle, GTK_STATE_FLAG_NORMAL);
Carlos Garnacho's avatar
Carlos Garnacho committed
214
    }
215 216 217
  else if (event->type == GDK_MOTION_NOTIFY &&
           event->motion.state & GDK_BUTTON1_MASK &&
           priv->windows[pos].dragged)
Carlos Garnacho's avatar
Carlos Garnacho committed
218
    {
219 220
      gint x, y, width, height, handle_height;
      GtkAllocation allocation;
Carlos Garnacho's avatar
Carlos Garnacho committed
221

222 223 224 225
      gtk_widget_get_allocation (priv->windows[pos].widget, &allocation);
      width = allocation.width;
      height = allocation.height;
      _gtk_text_handle_get_size (handle, NULL, &handle_height);
226 227
      x = event->motion.x - priv->windows[pos].dx + (width / 2);
      y = event->motion.y - priv->windows[pos].dy;
Carlos Garnacho's avatar
Carlos Garnacho committed
228

229
      if (pos != GTK_TEXT_HANDLE_POSITION_CURSOR)
230
        y += height - handle_height;
Carlos Garnacho's avatar
Carlos Garnacho committed
231

232 233
      y += priv->windows[pos].pointing_to.height / 2;

234
      gtk_widget_translate_coordinates (widget, priv->parent, x, y, &x, &y);
Carlos Garnacho's avatar
Carlos Garnacho committed
235 236 237 238 239 240 241
      g_signal_emit (handle, signals[HANDLE_DRAGGED], 0, pos, x, y);
    }

  return TRUE;
}

static void
242 243
gtk_text_handle_widget_style_updated (GtkWidget     *widget,
                                      GtkTextHandle *handle)
244
{
245 246 247 248 249 250
  GtkTextHandlePrivate *priv;

  priv = handle->priv;
  gtk_style_context_set_parent (gtk_widget_get_style_context (widget),
                                gtk_widget_get_style_context (priv->parent));

251 252 253 254 255 256 257
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_START);
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_END);
}

static GtkWidget *
_gtk_text_handle_ensure_widget (GtkTextHandle         *handle,
                                GtkTextHandlePosition  pos)
Carlos Garnacho's avatar
Carlos Garnacho committed
258 259 260 261 262
{
  GtkTextHandlePrivate *priv;

  priv = handle->priv;

263
  if (!priv->windows[pos].widget)
Carlos Garnacho's avatar
Carlos Garnacho committed
264
    {
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
      GtkWidget *widget, *window;

      widget = gtk_event_box_new ();
      gtk_widget_set_events (widget,
                             GDK_BUTTON_PRESS_MASK |
                             GDK_BUTTON_RELEASE_MASK |
                             GDK_POINTER_MOTION_MASK);

      g_signal_connect (widget, "draw",
                        G_CALLBACK (gtk_text_handle_widget_draw), handle);
      g_signal_connect (widget, "event",
                        G_CALLBACK (gtk_text_handle_widget_event), handle);
      g_signal_connect (widget, "style-updated",
                        G_CALLBACK (gtk_text_handle_widget_style_updated),
                        handle);

281
      priv->windows[pos].widget = g_object_ref_sink (widget);
282
      window = gtk_widget_get_ancestor (priv->parent, GTK_TYPE_WINDOW);
283
      _gtk_window_add_popover (GTK_WINDOW (window), widget);
284 285 286

      gtk_style_context_set_parent (gtk_widget_get_style_context (widget),
                                    gtk_widget_get_style_context (priv->parent));
Carlos Garnacho's avatar
Carlos Garnacho committed
287
    }
288 289

  return priv->windows[pos].widget;
290
}
Carlos Garnacho's avatar
Carlos Garnacho committed
291

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
static void
_handle_update_child_visible (GtkTextHandle         *handle,
                              GtkTextHandlePosition  pos)
{
  HandleWindow *handle_window;
  GtkTextHandlePrivate *priv;
  cairo_rectangle_int_t rect;
  GtkAllocation allocation;
  GtkWidget *parent;

  priv = handle->priv;
  handle_window = &priv->windows[pos];

  if (!priv->parent_scrollable)
    {
      gtk_widget_set_child_visible (handle_window->widget, TRUE);
      return;
    }

  parent = gtk_widget_get_parent (GTK_WIDGET (priv->parent_scrollable));
  rect = handle_window->pointing_to;

  gtk_widget_translate_coordinates (priv->parent, parent,
                                    rect.x, rect.y, &rect.x, &rect.y);

  gtk_widget_get_allocation (GTK_WIDGET (parent), &allocation);

  if (rect.x < 0 || rect.x + rect.width > allocation.width ||
      rect.y < 0 || rect.y + rect.height > allocation.height)
    gtk_widget_set_child_visible (handle_window->widget, FALSE);
  else
    gtk_widget_set_child_visible (handle_window->widget, TRUE);
}

326
static void
327 328
_gtk_text_handle_update (GtkTextHandle         *handle,
                         GtkTextHandlePosition  pos)
329 330 331
{
  GtkTextHandlePrivate *priv;
  HandleWindow *handle_window;
Carlos Garnacho's avatar
Carlos Garnacho committed
332

333 334
  priv = handle->priv;
  handle_window = &priv->windows[pos];
Carlos Garnacho's avatar
Carlos Garnacho committed
335

336
  if (!priv->parent || !gtk_widget_is_drawable (priv->parent))
337 338
    return;

339 340
  if (handle_window->has_point &&
      handle_window->mode_visible && handle_window->user_visible)
Carlos Garnacho's avatar
Carlos Garnacho committed
341
    {
342 343 344 345 346 347
      cairo_rectangle_int_t rect;
      GtkPositionType handle_pos;
      gint width, height;
      GtkWidget *window;

      _gtk_text_handle_ensure_widget (handle, pos);
348
      _gtk_text_handle_get_size (handle, &width, &height);
349 350
      rect.x = handle_window->pointing_to.x;
      rect.y = handle_window->pointing_to.y;
351 352 353
      rect.width = width;
      rect.height = 0;

354 355 356 357 358 359 360
      /* Make the window 3 times as wide, and 2 times as high (plus
       * handle_window->pointing_to.height), the handle will be rendered
       * in the center. Making the rest an invisible input area.
       */
      width *= 3;
      height *= 2;

361
      _handle_update_child_visible (handle, pos);
362

363 364 365
      window = gtk_widget_get_parent (handle_window->widget);
      gtk_widget_translate_coordinates (priv->parent, window,
                                        rect.x, rect.y, &rect.x, &rect.y);
Carlos Garnacho's avatar
Carlos Garnacho committed
366

367 368 369 370 371 372 373
      if (pos == GTK_TEXT_HANDLE_POSITION_CURSOR)
        handle_pos = GTK_POS_BOTTOM;
      else
        {
          handle_pos = GTK_POS_TOP;
          rect.y += handle_window->pointing_to.height;
        }
Carlos Garnacho's avatar
Carlos Garnacho committed
374

375 376 377 378 379
      height += handle_window->pointing_to.height;
      rect.x -= rect.width / 2;

      gtk_widget_set_size_request (handle_window->widget, width, height);
      gtk_widget_show (handle_window->widget);
380 381 382
      _gtk_window_set_popover_position (GTK_WINDOW (window),
                                        handle_window->widget,
                                        handle_pos, &rect);
383 384 385
    }
  else if (handle_window->widget)
    gtk_widget_hide (handle_window->widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
386 387
}

388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
static void
adjustment_changed_cb (GtkAdjustment *adjustment,
                       GtkTextHandle *handle)
{
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_START);
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_END);
}

static void
_gtk_text_handle_set_scrollable (GtkTextHandle *handle,
                                 GtkScrollable *scrollable)
{
  GtkTextHandlePrivate *priv;

  priv = handle->priv;

  if (priv->parent_scrollable)
    {
      if (priv->vadj)
        {
          g_signal_handlers_disconnect_by_data (priv->vadj, handle);
          g_object_unref (priv->vadj);
          priv->vadj = NULL;
        }

      if (priv->hadj)
        {
          g_signal_handlers_disconnect_by_data (priv->hadj, handle);
          g_object_unref (priv->hadj);
          priv->hadj = NULL;
        }
419 420
 
      g_object_remove_weak_pointer (G_OBJECT (priv->parent_scrollable), (gpointer *) &priv->parent_scrollable);
421 422 423 424 425 426
    }

  priv->parent_scrollable = scrollable;

  if (scrollable)
    {
427 428
      g_object_add_weak_pointer (G_OBJECT (priv->parent_scrollable), (gpointer *) &priv->parent_scrollable);

429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      priv->vadj = gtk_scrollable_get_vadjustment (scrollable);
      priv->hadj = gtk_scrollable_get_hadjustment (scrollable);

      if (priv->vadj)
        {
          g_object_ref (priv->vadj);
          g_signal_connect (priv->vadj, "changed",
                            G_CALLBACK (adjustment_changed_cb), handle);
          g_signal_connect (priv->vadj, "value-changed",
                            G_CALLBACK (adjustment_changed_cb), handle);
        }

      if (priv->hadj)
        {
          g_object_ref (priv->hadj);
          g_signal_connect (priv->hadj, "changed",
                            G_CALLBACK (adjustment_changed_cb), handle);
          g_signal_connect (priv->hadj, "value-changed",
                            G_CALLBACK (adjustment_changed_cb), handle);
        }
    }
}

static void
_gtk_text_handle_scrollable_notify (GObject       *object,
                                    GParamSpec    *pspec,
                                    GtkTextHandle *handle)
{
  if (pspec->value_type == GTK_TYPE_ADJUSTMENT)
    _gtk_text_handle_set_scrollable (handle, GTK_SCROLLABLE (object));
}

static void
_gtk_text_handle_update_scrollable (GtkTextHandle *handle,
                                    GtkScrollable *scrollable)
{
  GtkTextHandlePrivate *priv;

  priv = handle->priv;

  if (priv->parent_scrollable == scrollable)
    return;

  if (priv->parent_scrollable && priv->scrollable_notify_id &&
      g_signal_handler_is_connected (priv->parent_scrollable,
                                     priv->scrollable_notify_id))
    g_signal_handler_disconnect (priv->parent_scrollable,
                                 priv->scrollable_notify_id);

  _gtk_text_handle_set_scrollable (handle, scrollable);

  if (priv->parent_scrollable)
    priv->scrollable_notify_id =
      g_signal_connect (priv->parent_scrollable, "notify",
                        G_CALLBACK (_gtk_text_handle_scrollable_notify),
                        handle);
}

487 488 489 490 491
static void
_gtk_text_handle_parent_hierarchy_changed (GtkWidget     *widget,
                                           GtkWindow     *previous_toplevel,
                                           GtkTextHandle *handle)
{
492
  GtkWidget *toplevel, *scrollable;
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
  GtkTextHandlePrivate *priv;

  priv = handle->priv;
  toplevel = gtk_widget_get_ancestor (widget, GTK_TYPE_WINDOW);

  if (previous_toplevel && !toplevel)
    {
      if (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget)
        {
          _gtk_window_remove_popover (GTK_WINDOW (previous_toplevel),
                                      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget);
          g_object_unref (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget);
          priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget = NULL;
        }

      if (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget)
        {
          _gtk_window_remove_popover (GTK_WINDOW (previous_toplevel),
                                      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget);
          g_object_unref (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget);
          priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget = NULL;
        }
    }
516 517 518

  scrollable = gtk_widget_get_ancestor (widget, GTK_TYPE_SCROLLABLE);
  _gtk_text_handle_update_scrollable (handle, GTK_SCROLLABLE (scrollable));
519 520 521 522 523 524 525
}

static void
_gtk_text_handle_set_parent (GtkTextHandle *handle,
                             GtkWidget     *parent)
{
  GtkTextHandlePrivate *priv;
526
  GtkWidget *scrollable = NULL;
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544

  priv = handle->priv;

  if (priv->parent == parent)
    return;

  if (priv->parent && priv->hierarchy_changed_id &&
      g_signal_handler_is_connected (priv->parent, priv->hierarchy_changed_id))
    g_signal_handler_disconnect (priv->parent, priv->hierarchy_changed_id);

  priv->parent = parent;

  if (parent)
    {
      priv->hierarchy_changed_id =
        g_signal_connect (parent, "hierarchy-changed",
                          G_CALLBACK (_gtk_text_handle_parent_hierarchy_changed),
                          handle);
545 546

      scrollable = gtk_widget_get_ancestor (parent, GTK_TYPE_SCROLLABLE);
547
    }
548 549

  _gtk_text_handle_update_scrollable (handle, GTK_SCROLLABLE (scrollable));
550 551
}

Carlos Garnacho's avatar
Carlos Garnacho committed
552 553 554 555 556 557 558
static void
gtk_text_handle_finalize (GObject *object)
{
  GtkTextHandlePrivate *priv;

  priv = GTK_TEXT_HANDLE (object)->priv;

559 560
  _gtk_text_handle_set_parent (GTK_TEXT_HANDLE (object), NULL);

561
  /* We sank the references, unref here */
562
  if (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget)
563
    g_object_unref (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
564

565
  if (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget)
566
    g_object_unref (priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].widget);
Carlos Garnacho's avatar
Carlos Garnacho committed
567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

  G_OBJECT_CLASS (_gtk_text_handle_parent_class)->finalize (object);
}

static void
gtk_text_handle_set_property (GObject      *object,
                              guint         prop_id,
                              const GValue *value,
                              GParamSpec   *pspec)
{
  GtkTextHandle *handle;

  handle = GTK_TEXT_HANDLE (object);

  switch (prop_id)
    {
    case PROP_PARENT:
584
      _gtk_text_handle_set_parent (handle, g_value_get_object (value));
Carlos Garnacho's avatar
Carlos Garnacho committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
gtk_text_handle_get_property (GObject    *object,
                              guint       prop_id,
                              GValue     *value,
                              GParamSpec *pspec)
{
  GtkTextHandlePrivate *priv;

  priv = GTK_TEXT_HANDLE (object)->priv;

  switch (prop_id)
    {
    case PROP_PARENT:
      g_value_set_object (value, priv->parent);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static void
_gtk_text_handle_class_init (GtkTextHandleClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = gtk_text_handle_finalize;
  object_class->set_property = gtk_text_handle_set_property;
  object_class->get_property = gtk_text_handle_get_property;

  signals[HANDLE_DRAGGED] =
    g_signal_new (I_("handle-dragged"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkTextHandleClass, handle_dragged),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM_INT_INT,
		  G_TYPE_NONE, 3,
                  GTK_TYPE_TEXT_HANDLE_POSITION,
                  G_TYPE_INT, G_TYPE_INT);
  signals[DRAG_FINISHED] =
    g_signal_new (I_("drag-finished"),
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST, 0,
		  NULL, NULL,
                  g_cclosure_marshal_VOID__ENUM,
                  G_TYPE_NONE, 1,
                  GTK_TYPE_TEXT_HANDLE_POSITION);

  g_object_class_install_property (object_class,
                                   PROP_PARENT,
                                   g_param_spec_object ("parent",
                                                        P_("Parent widget"),
                                                        P_("Parent widget"),
                                                        GTK_TYPE_WIDGET,
                                                        GTK_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));
}

static void
_gtk_text_handle_init (GtkTextHandle *handle)
{
652
  handle->priv = _gtk_text_handle_get_instance_private (handle);
Carlos Garnacho's avatar
Carlos Garnacho committed
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675
}

GtkTextHandle *
_gtk_text_handle_new (GtkWidget *parent)
{
  return g_object_new (GTK_TYPE_TEXT_HANDLE,
                       "parent", parent,
                       NULL);
}

void
_gtk_text_handle_set_mode (GtkTextHandle     *handle,
                           GtkTextHandleMode  mode)
{
  GtkTextHandlePrivate *priv;

  g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));

  priv = handle->priv;

  if (priv->mode == mode)
    return;

676 677
  priv->mode = mode;

Carlos Garnacho's avatar
Carlos Garnacho committed
678 679 680
  switch (mode)
    {
    case GTK_TEXT_HANDLE_MODE_CURSOR:
681 682
      priv->windows[GTK_TEXT_HANDLE_POSITION_CURSOR].mode_visible = TRUE;
      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].mode_visible = FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
683
      break;
684 685 686
    case GTK_TEXT_HANDLE_MODE_SELECTION:
      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].mode_visible = TRUE;
      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].mode_visible = TRUE;
Carlos Garnacho's avatar
Carlos Garnacho committed
687 688 689
      break;
    case GTK_TEXT_HANDLE_MODE_NONE:
    default:
690 691
      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_START].mode_visible = FALSE;
      priv->windows[GTK_TEXT_HANDLE_POSITION_SELECTION_END].mode_visible = FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
692 693 694
      break;
    }

695 696
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_START);
  _gtk_text_handle_update (handle, GTK_TEXT_HANDLE_POSITION_SELECTION_END);
Carlos Garnacho's avatar
Carlos Garnacho committed
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
}

GtkTextHandleMode
_gtk_text_handle_get_mode (GtkTextHandle *handle)
{
  GtkTextHandlePrivate *priv;

  g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), GTK_TEXT_HANDLE_MODE_NONE);

  priv = handle->priv;
  return priv->mode;
}

void
_gtk_text_handle_set_position (GtkTextHandle         *handle,
                               GtkTextHandlePosition  pos,
                               GdkRectangle          *rect)
{
  GtkTextHandlePrivate *priv;
  HandleWindow *handle_window;

  g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));

  priv = handle->priv;
  pos = CLAMP (pos, GTK_TEXT_HANDLE_POSITION_CURSOR,
               GTK_TEXT_HANDLE_POSITION_SELECTION_START);
723
  handle_window = &priv->windows[pos];
Carlos Garnacho's avatar
Carlos Garnacho committed
724 725 726 727 728 729

  if (priv->mode == GTK_TEXT_HANDLE_MODE_NONE ||
      (priv->mode == GTK_TEXT_HANDLE_MODE_CURSOR &&
       pos != GTK_TEXT_HANDLE_POSITION_CURSOR))
    return;

730 731
  handle_window->pointing_to = *rect;
  handle_window->has_point = TRUE;
732

733 734
  if (gtk_widget_is_visible (priv->parent))
    _gtk_text_handle_update (handle, pos);
Carlos Garnacho's avatar
Carlos Garnacho committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749
}

void
_gtk_text_handle_set_visible (GtkTextHandle         *handle,
                              GtkTextHandlePosition  pos,
                              gboolean               visible)
{
  GtkTextHandlePrivate *priv;

  g_return_if_fail (GTK_IS_TEXT_HANDLE (handle));

  priv = handle->priv;
  pos = CLAMP (pos, GTK_TEXT_HANDLE_POSITION_CURSOR,
               GTK_TEXT_HANDLE_POSITION_SELECTION_START);

750
  priv->windows[pos].user_visible = visible;
751 752 753

  if (gtk_widget_is_visible (priv->parent))
    _gtk_text_handle_update (handle, pos);
Carlos Garnacho's avatar
Carlos Garnacho committed
754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
}

gboolean
_gtk_text_handle_get_is_dragged (GtkTextHandle         *handle,
                                 GtkTextHandlePosition  pos)
{
  GtkTextHandlePrivate *priv;

  g_return_val_if_fail (GTK_IS_TEXT_HANDLE (handle), FALSE);

  priv = handle->priv;
  pos = CLAMP (pos, GTK_TEXT_HANDLE_POSITION_CURSOR,
               GTK_TEXT_HANDLE_POSITION_SELECTION_START);

  return priv->windows[pos].dragged;
}