gtkmagnifier.c 13.2 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 23 24
/* GTK - The GIMP Toolkit
 * Copyright © 2013 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 "gtk/gtk.h"
#include "gtkmagnifierprivate.h"
#include "gtkintl.h"

enum {
  PROP_INSPECTED = 1,
25
  PROP_RESIZE,
Carlos Garnacho's avatar
Carlos Garnacho committed
26 27 28 29 30 31 32 33 34 35 36
  PROP_MAGNIFICATION
};

typedef struct _GtkMagnifierPrivate GtkMagnifierPrivate;

struct _GtkMagnifierPrivate
{
  GtkWidget *inspected;
  gdouble magnification;
  gint x;
  gint y;
37 38 39
  gboolean resize;
  gulong draw_handler;
  gulong resize_handler;
Carlos Garnacho's avatar
Carlos Garnacho committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
};

G_DEFINE_TYPE_WITH_PRIVATE (GtkMagnifier, _gtk_magnifier,
                            GTK_TYPE_WIDGET)

static void
_gtk_magnifier_set_property (GObject      *object,
                             guint         param_id,
                             const GValue *value,
                             GParamSpec   *pspec)
{
  switch (param_id)
    {
    case PROP_INSPECTED:
      _gtk_magnifier_set_inspected (GTK_MAGNIFIER (object),
                                    g_value_get_object (value));
      break;
    case PROP_MAGNIFICATION:
      _gtk_magnifier_set_magnification (GTK_MAGNIFIER (object),
                                        g_value_get_double (value));
      break;
61 62 63 64
    case PROP_RESIZE:
      _gtk_magnifier_set_resize (GTK_MAGNIFIER (object),
                                 g_value_get_boolean (value));
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
    }
}

static void
_gtk_magnifier_get_property (GObject    *object,
                             guint       param_id,
                             GValue     *value,
                             GParamSpec *pspec)
{
  GtkMagnifier *magnifier;
  GtkMagnifierPrivate *priv;

  magnifier = GTK_MAGNIFIER (object);
  priv = _gtk_magnifier_get_instance_private (magnifier);

  switch (param_id)
    {
    case PROP_INSPECTED:
      g_value_set_object (value, priv->inspected);
      break;
    case PROP_MAGNIFICATION:
      g_value_set_double (value, priv->magnification);
      break;
90 91 92
    case PROP_RESIZE:
      g_value_set_boolean (value, priv->resize);
      break;
Carlos Garnacho's avatar
Carlos Garnacho committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
    }
}

static gboolean
_gtk_magnifier_draw (GtkWidget *widget,
                     cairo_t   *cr)
{
  GtkAllocation allocation, inspected_alloc;
  GtkMagnifier *magnifier;
  GtkMagnifierPrivate *priv;
  gdouble x, y;

  magnifier = GTK_MAGNIFIER (widget);
  priv = _gtk_magnifier_get_instance_private (magnifier);

110 111 112
  if (priv->inspected == NULL)
    return FALSE;

Carlos Garnacho's avatar
Carlos Garnacho committed
113 114 115 116 117
  if (!gtk_widget_is_visible (priv->inspected))
    return FALSE;

  gtk_widget_get_allocation (widget, &allocation);
  gtk_widget_get_allocation (priv->inspected, &inspected_alloc);
118 119 120

  if (!priv->resize)
    cairo_translate (cr, allocation.width / 2, allocation.height / 2);
Carlos Garnacho's avatar
Carlos Garnacho committed
121 122 123 124 125 126 127

  x = CLAMP (priv->x, 0, inspected_alloc.width);
  y = CLAMP (priv->y, 0, inspected_alloc.height);

  cairo_save (cr);
  cairo_scale (cr, priv->magnification, priv->magnification);
  cairo_translate (cr, -x, -y);
128
  g_signal_handler_block (priv->inspected, priv->draw_handler);
Carlos Garnacho's avatar
Carlos Garnacho committed
129
  gtk_widget_draw (priv->inspected, cr);
130
  g_signal_handler_unblock (priv->inspected, priv->draw_handler);
Carlos Garnacho's avatar
Carlos Garnacho committed
131 132 133 134 135
  cairo_restore (cr);

  return TRUE;
}

136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
static void
gtk_magnifier_get_preferred_width (GtkWidget *widget,
                                   gint      *minimum_width,
                                   gint      *natural_width)
{
  GtkMagnifier *magnifier;
  GtkMagnifierPrivate *priv;
  gint width;

  magnifier = GTK_MAGNIFIER (widget);
  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->resize && priv->inspected)
    width = priv->magnification * gtk_widget_get_allocated_width (priv->inspected);
  else
    width = 0;

  *minimum_width = width;
  *natural_width = width;
}

static void
gtk_magnifier_get_preferred_height (GtkWidget *widget,
                                    gint      *minimum_height,
                                    gint      *natural_height)
{
  GtkMagnifier *magnifier;
  GtkMagnifierPrivate *priv;
  gint height;

  magnifier = GTK_MAGNIFIER (widget);
  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->resize && priv->inspected)
    height = priv->magnification * gtk_widget_get_allocated_height (priv->inspected);
  else
    height = 0;

  *minimum_height = height;
  *natural_height = height;
}

static void
resize_handler (GtkWidget     *widget,
                GtkAllocation *alloc,
                GtkWidget     *magnifier)
{
  gtk_widget_queue_resize (magnifier);
}

static void
connect_resize_handler (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->inspected && priv->resize)
    priv->resize_handler = g_signal_connect (priv->inspected, "size-allocate",
                                             G_CALLBACK (resize_handler), magnifier);
}

static void
disconnect_resize_handler (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->resize_handler)
    {
207 208
      if (priv->inspected)
        g_signal_handler_disconnect (priv->inspected, priv->resize_handler);
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
      priv->resize_handler = 0;
    }
}

static gboolean
draw_handler (GtkWidget     *widget,
              cairo_t       *cr,
              GtkWidget     *magnifier)
{
  gtk_widget_queue_draw (magnifier);
  return FALSE;
}

static void
connect_draw_handler (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  priv = _gtk_magnifier_get_instance_private (magnifier);

229 230 231 232 233 234
  if (!priv->draw_handler)
    {
      if (priv->inspected)
        priv->draw_handler = g_signal_connect (priv->inspected, "draw",
                                               G_CALLBACK (draw_handler), magnifier);
    }
235 236 237 238 239 240 241 242 243 244 245
}

static void
disconnect_draw_handler (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->draw_handler)
    {
246 247
      if (priv->inspected)
        g_signal_handler_disconnect (priv->inspected, priv->draw_handler);
248 249 250 251 252 253 254 255 256 257 258 259
      priv->draw_handler = 0;
    }
}

static void
_gtk_magnifier_destroy (GtkWidget *widget)
{
  _gtk_magnifier_set_inspected (GTK_MAGNIFIER (widget), NULL);

  GTK_WIDGET_CLASS (_gtk_magnifier_parent_class)->destroy (widget);
}

260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
static void
gtk_magnifier_map (GtkWidget *widget)
{
  connect_draw_handler (GTK_MAGNIFIER (widget));

  GTK_WIDGET_CLASS (_gtk_magnifier_parent_class)->map (widget);
}

static void
gtk_magnifier_unmap (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (_gtk_magnifier_parent_class)->unmap (widget);

  disconnect_draw_handler (GTK_MAGNIFIER (widget));
}

Carlos Garnacho's avatar
Carlos Garnacho committed
276 277 278 279 280 281 282 283 284
static void
_gtk_magnifier_class_init (GtkMagnifierClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->set_property = _gtk_magnifier_set_property;
  object_class->get_property = _gtk_magnifier_get_property;

285
  widget_class->destroy = _gtk_magnifier_destroy;
Carlos Garnacho's avatar
Carlos Garnacho committed
286
  widget_class->draw = _gtk_magnifier_draw;
287 288
  widget_class->get_preferred_width = gtk_magnifier_get_preferred_width;
  widget_class->get_preferred_height = gtk_magnifier_get_preferred_height;
289 290
  widget_class->map = gtk_magnifier_map;
  widget_class->unmap = gtk_magnifier_unmap;
Carlos Garnacho's avatar
Carlos Garnacho committed
291 292 293

  g_object_class_install_property (object_class,
                                   PROP_INSPECTED,
294
                                   g_param_spec_object ("inspected",
Carlos Garnacho's avatar
Carlos Garnacho committed
295 296 297 298 299 300
                                                        P_("Inspected"),
                                                        P_("Inspected widget"),
                                                        GTK_TYPE_WIDGET,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (object_class,
                                   PROP_MAGNIFICATION,
301
                                   g_param_spec_double ("magnification",
Carlos Garnacho's avatar
Carlos Garnacho committed
302 303 304 305
                                                        P_("magnification"),
                                                        P_("magnification"),
                                                        1, G_MAXDOUBLE, 1,
                                                        G_PARAM_READWRITE));
306 307 308 309 310 311 312
  g_object_class_install_property (object_class,
                                   PROP_RESIZE,
                                   g_param_spec_boolean ("resize",
                                                         P_("resize"),
                                                         P_("resize"),
                                                         FALSE,
                                                         G_PARAM_READWRITE));
Carlos Garnacho's avatar
Carlos Garnacho committed
313 314 315 316 317 318 319 320 321 322 323
}

static void
_gtk_magnifier_init (GtkMagnifier *magnifier)
{
  GtkWidget *widget = GTK_WIDGET (magnifier);
  GtkMagnifierPrivate *priv;

  priv = _gtk_magnifier_get_instance_private (magnifier);

  gtk_widget_set_events (widget,
324
                         gtk_widget_get_events (widget));
Carlos Garnacho's avatar
Carlos Garnacho committed
325 326 327

  gtk_widget_set_has_window (widget, FALSE);
  priv->magnification = 1;
328
  priv->resize = FALSE;
Carlos Garnacho's avatar
Carlos Garnacho committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
}

GtkWidget *
_gtk_magnifier_new (GtkWidget *inspected)
{
  g_return_val_if_fail (GTK_IS_WIDGET (inspected), NULL);

  return g_object_new (GTK_TYPE_MAGNIFIER,
                       "inspected", inspected,
                       NULL);
}

GtkWidget *
_gtk_magnifier_get_inspected (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), NULL);

  priv = _gtk_magnifier_get_instance_private (magnifier);

  return priv->inspected;
}

void
_gtk_magnifier_set_inspected (GtkMagnifier *magnifier,
                              GtkWidget    *inspected)
{
  GtkMagnifierPrivate *priv;

359
  g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));
Carlos Garnacho's avatar
Carlos Garnacho committed
360 361 362 363 364 365

  priv = _gtk_magnifier_get_instance_private (magnifier);

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

366 367 368
  disconnect_draw_handler (magnifier);
  disconnect_resize_handler (magnifier);

369 370 371
  if (priv->inspected)
    g_object_remove_weak_pointer (G_OBJECT (priv->inspected),
                                  (gpointer *) &priv->inspected);
Carlos Garnacho's avatar
Carlos Garnacho committed
372
  priv->inspected = inspected;
373 374 375
  if (priv->inspected)
    g_object_add_weak_pointer (G_OBJECT (priv->inspected),
                               (gpointer *) &priv->inspected);
376

377 378
  if (gtk_widget_get_mapped (GTK_WIDGET (magnifier)))
    connect_draw_handler (magnifier);
379 380
  connect_resize_handler (magnifier);

Carlos Garnacho's avatar
Carlos Garnacho committed
381 382 383 384 385 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 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
  g_object_notify (G_OBJECT (magnifier), "inspected");
}

void
_gtk_magnifier_set_coords (GtkMagnifier *magnifier,
                           gdouble       x,
                           gdouble       y)
{
  GtkMagnifierPrivate *priv;

  g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));

  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (priv->x == x && priv->y == y)
    return;

  priv->x = x;
  priv->y = y;

  if (gtk_widget_is_visible (GTK_WIDGET (magnifier)))
    gtk_widget_queue_draw (GTK_WIDGET (magnifier));
}

void
_gtk_magnifier_get_coords (GtkMagnifier *magnifier,
                           gdouble      *x,
                           gdouble      *y)
{
  GtkMagnifierPrivate *priv;

  g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));

  priv = _gtk_magnifier_get_instance_private (magnifier);

  if (x)
    *x = priv->x;

  if (y)
    *y = priv->y;
}

void
_gtk_magnifier_set_magnification (GtkMagnifier *magnifier,
                                  gdouble       magnification)
{
  GtkMagnifierPrivate *priv;

  g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));

  priv = _gtk_magnifier_get_instance_private (magnifier);

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

  priv->magnification = magnification;
  g_object_notify (G_OBJECT (magnifier), "magnification");

439 440 441
  if (priv->resize)
    gtk_widget_queue_resize (GTK_WIDGET (magnifier));

Carlos Garnacho's avatar
Carlos Garnacho committed
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
  if (gtk_widget_is_visible (GTK_WIDGET (magnifier)))
    gtk_widget_queue_draw (GTK_WIDGET (magnifier));
}

gdouble
_gtk_magnifier_get_magnification (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), 1);

  priv = _gtk_magnifier_get_instance_private (magnifier);

  return priv->magnification;
}
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 487 488 489 490

void
_gtk_magnifier_set_resize (GtkMagnifier *magnifier,
                           gboolean      resize)
{
  GtkMagnifierPrivate *priv;

  g_return_if_fail (GTK_IS_MAGNIFIER (magnifier));

  priv = _gtk_magnifier_get_instance_private (magnifier);

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

  priv->resize = resize;

  gtk_widget_queue_resize (GTK_WIDGET (magnifier));
  if (resize)
    connect_resize_handler (magnifier);
  else
    disconnect_resize_handler (magnifier);
}

gboolean
_gtk_magnifier_get_resize (GtkMagnifier *magnifier)
{
  GtkMagnifierPrivate *priv;

  g_return_val_if_fail (GTK_IS_MAGNIFIER (magnifier), FALSE);

  priv = _gtk_magnifier_get_instance_private (magnifier);

  return priv->resize;
}