1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 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 326 327 328 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 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 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 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 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 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 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 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
|
/*
* $Id: ellipse.c 20677 2017-12-18 18:22:52Z yeti-dn $
* Copyright (C) 2005-2006 David Necas (Yeti), Petr Klapetek.
* E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libgwydgets/gwydataview.h>
#include <libgwydgets/gwydgetutils.h>
#include <libgwymodule/gwymodule-layer.h>
#include "layer.h"
#define GWY_TYPE_LAYER_ELLIPSE (gwy_layer_ellipse_get_type())
#define GWY_LAYER_ELLIPSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GWY_TYPE_LAYER_ELLIPSE, GwyLayerEllipse))
#define GWY_IS_LAYER_ELLIPSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GWY_TYPE_LAYER_ELLIPSE))
#define GWY_LAYER_ELLIPSE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GWY_TYPE_LAYER_ELLIPSE, GwyLayerEllipseClass))
#define GWY_TYPE_SELECTION_ELLIPSE (gwy_selection_ellipse_get_type())
#define GWY_SELECTION_ELLIPSE(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), GWY_TYPE_SELECTION_ELLIPSE, GwySelectionEllipse))
#define GWY_IS_SELECTION_ELLIPSE(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), GWY_TYPE_SELECTION_ELLIPSE))
#define GWY_SELECTION_ELLIPSE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), GWY_TYPE_SELECTION_ELLIPSE, GwySelectionEllipseClass))
enum {
OBJECT_SIZE = 4
};
enum {
PROP_0,
PROP_DRAW_REFLECTION,
PROP_SNAP_TO_CENTER,
};
typedef struct _GwyLayerEllipse GwyLayerEllipse;
typedef struct _GwyLayerEllipseClass GwyLayerEllipseClass;
typedef struct _GwySelectionEllipse GwySelectionEllipse;
typedef struct _GwySelectionEllipseClass GwySelectionEllipseClass;
struct _GwyLayerEllipse {
GwyVectorLayer parent_instance;
GdkCursor *corner_cursor[4];
GdkCursor *resize_cursor;
/* Properties */
gboolean draw_reflection;
gboolean snap;
/* Dynamic state */
gboolean circle;
};
struct _GwyLayerEllipseClass {
GwyVectorLayerClass parent_class;
};
struct _GwySelectionEllipse {
GwySelection parent_instance;
};
struct _GwySelectionEllipseClass {
GwySelectionClass parent_class;
};
static gboolean module_register (void);
static GType gwy_layer_ellipse_get_type (void) G_GNUC_CONST;
static GType gwy_selection_ellipse_get_type (void) G_GNUC_CONST;
static gboolean gwy_selection_ellipse_crop_object(GwySelection *selection,
gint i,
gpointer user_data);
static void gwy_selection_ellipse_crop (GwySelection *selection,
gdouble xmin,
gdouble ymin,
gdouble xmax,
gdouble ymax);
static void gwy_selection_ellipse_move (GwySelection *selection,
gdouble vx,
gdouble vy);
static void gwy_layer_ellipse_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gwy_layer_ellipse_get_property (GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gwy_layer_ellipse_draw (GwyVectorLayer *layer,
GdkDrawable *drawable,
GwyRenderingTarget target);
static void gwy_layer_ellipse_draw_object (GwyVectorLayer *layer,
GdkDrawable *drawable,
GwyRenderingTarget target,
gint i);
static void gwy_layer_ellipse_draw_ellipse (GwyVectorLayer *layer,
GwyDataView *data_view,
GdkDrawable *drawable,
GwyRenderingTarget target,
const gdouble *xy);
static gboolean gwy_layer_ellipse_motion_notify (GwyVectorLayer *layer,
GdkEventMotion *event);
static gboolean gwy_layer_ellipse_button_pressed (GwyVectorLayer *layer,
GdkEventButton *event);
static gboolean gwy_layer_ellipse_button_released(GwyVectorLayer *layer,
GdkEventButton *event);
static void gwy_layer_ellipse_set_reflection (GwyLayerEllipse *layer,
gboolean draw_reflection);
static void gwy_layer_ellipse_set_snap (GwyLayerEllipse *layer,
gboolean snap);
static void gwy_layer_ellipse_realize (GwyDataViewLayer *dlayer);
static void gwy_layer_ellipse_unrealize (GwyDataViewLayer *dlayer);
static gint gwy_layer_ellipse_near_point (GwyVectorLayer *layer,
gdouble xreal,
gdouble yreal);
static void gwy_layer_ellipse_squarize (GwyDataView *data_view,
gint x,
gint y,
gdouble *xy);
/* Allow to express intent. */
#define gwy_layer_ellipse_undraw gwy_layer_ellipse_draw
#define gwy_layer_ellipse_undraw_object gwy_layer_ellipse_draw_object
static GwyModuleInfo module_info = {
GWY_MODULE_ABI_VERSION,
&module_register,
N_("Layer allowing selection of elliptic areas."),
"Yeti <yeti@gwyddion.net>",
"1.7",
"David Nečas (Yeti) & Petr Klapetek",
"2005",
};
GWY_MODULE_QUERY2(module_info, ellipse)
G_DEFINE_TYPE(GwySelectionEllipse, gwy_selection_ellipse,
GWY_TYPE_SELECTION)
G_DEFINE_TYPE(GwyLayerEllipse, gwy_layer_ellipse,
GWY_TYPE_VECTOR_LAYER)
static gboolean
module_register(void)
{
gwy_layer_func_register(GWY_TYPE_LAYER_ELLIPSE);
return TRUE;
}
static void
gwy_selection_ellipse_class_init(GwySelectionEllipseClass *klass)
{
GwySelectionClass *sel_class = GWY_SELECTION_CLASS(klass);
sel_class->object_size = OBJECT_SIZE;
sel_class->crop = gwy_selection_ellipse_crop;
sel_class->move = gwy_selection_ellipse_move;
}
static void
gwy_layer_ellipse_class_init(GwyLayerEllipseClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
GwyDataViewLayerClass *layer_class = GWY_DATA_VIEW_LAYER_CLASS(klass);
GwyVectorLayerClass *vector_class = GWY_VECTOR_LAYER_CLASS(klass);
gwy_debug("");
gobject_class->set_property = gwy_layer_ellipse_set_property;
gobject_class->get_property = gwy_layer_ellipse_get_property;
layer_class->realize = gwy_layer_ellipse_realize;
layer_class->unrealize = gwy_layer_ellipse_unrealize;
vector_class->selection_type = GWY_TYPE_SELECTION_ELLIPSE;
vector_class->draw = gwy_layer_ellipse_draw;
vector_class->motion_notify = gwy_layer_ellipse_motion_notify;
vector_class->button_press = gwy_layer_ellipse_button_pressed;
vector_class->button_release = gwy_layer_ellipse_button_released;
g_object_class_install_property
(gobject_class,
PROP_DRAW_REFLECTION,
g_param_spec_boolean("draw-reflection",
"Draw reflection",
"Whether central reflection of selection should "
"be drawn too",
FALSE,
G_PARAM_READWRITE));
g_object_class_install_property
(gobject_class,
PROP_SNAP_TO_CENTER,
g_param_spec_boolean("snap-to-center",
"Snap to Center",
"Whether the selection should snap to the center.",
FALSE,
G_PARAM_READWRITE));
}
static void
gwy_selection_ellipse_init(GwySelectionEllipse *selection)
{
/* Set max. number of objects to one */
g_array_set_size(GWY_SELECTION(selection)->objects, OBJECT_SIZE);
}
static gboolean
gwy_selection_ellipse_crop_object(GwySelection *selection,
gint i,
gpointer user_data)
{
const gdouble *minmax = (const gdouble*)user_data;
gdouble xy[OBJECT_SIZE];
gwy_selection_get_object(selection, i, xy);
return (MIN(xy[0], xy[2]) >= minmax[0]
&& MIN(xy[1], xy[3]) >= minmax[1]
&& MAX(xy[0], xy[2]) <= minmax[2]
&& MAX(xy[1], xy[3]) <= minmax[3]);
}
static void
gwy_selection_ellipse_crop(GwySelection *selection,
gdouble xmin,
gdouble ymin,
gdouble xmax,
gdouble ymax)
{
gdouble minmax[4] = { xmin, ymin, xmax, ymax };
gwy_selection_filter(selection, gwy_selection_ellipse_crop_object, minmax);
}
static void
gwy_selection_ellipse_move(GwySelection *selection,
gdouble vx,
gdouble vy)
{
gdouble *data = (gdouble*)selection->objects->data;
guint i, n = selection->objects->len/OBJECT_SIZE;
for (i = 0; i < n; i++) {
data[OBJECT_SIZE*i + 0] += vx;
data[OBJECT_SIZE*i + 1] += vy;
data[OBJECT_SIZE*i + 2] += vx;
data[OBJECT_SIZE*i + 3] += vy;
}
}
static void
gwy_layer_ellipse_init(G_GNUC_UNUSED GwyLayerEllipse *layer)
{
}
static void
gwy_layer_ellipse_set_property(GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GwyLayerEllipse *layer = GWY_LAYER_ELLIPSE(object);
switch (prop_id) {
case PROP_DRAW_REFLECTION:
gwy_layer_ellipse_set_reflection(layer, g_value_get_boolean(value));
break;
case PROP_SNAP_TO_CENTER:
gwy_layer_ellipse_set_snap(layer, g_value_get_boolean(value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gwy_layer_ellipse_get_property(GObject*object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GwyLayerEllipse *layer = GWY_LAYER_ELLIPSE(object);
switch (prop_id) {
case PROP_DRAW_REFLECTION:
g_value_set_boolean(value, layer->draw_reflection);
break;
case PROP_SNAP_TO_CENTER:
g_value_set_boolean(value, layer->snap);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
break;
}
}
static void
gwy_layer_ellipse_draw(GwyVectorLayer *layer,
GdkDrawable *drawable,
GwyRenderingTarget target)
{
gint i, n;
g_return_if_fail(GDK_IS_DRAWABLE(drawable));
if (!layer->selection)
return;
n = gwy_selection_get_data(layer->selection, NULL);
for (i = 0; i < n; i++)
gwy_layer_ellipse_draw_object(layer, drawable, target, i);
}
static void
gwy_layer_ellipse_draw_object(GwyVectorLayer *layer,
GdkDrawable *drawable,
GwyRenderingTarget target,
gint i)
{
GwyDataView *data_view;
gdouble xy[OBJECT_SIZE];
gdouble xreal, yreal;
gboolean has_object;
gint xy_pixel[OBJECT_SIZE], j;
data_view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent);
has_object = gwy_selection_get_object(layer->selection, i, xy);
g_return_if_fail(has_object);
gwy_layer_ellipse_draw_ellipse(layer, data_view, drawable, target, xy);
if (GWY_LAYER_ELLIPSE(layer)->draw_reflection) {
gwy_data_view_get_real_data_sizes(data_view, &xreal, &yreal);
xy[0] = xreal - xy[0];
xy[1] = yreal - xy[1];
xy[2] = xreal - xy[2];
xy[3] = yreal - xy[3];
/* XXX - this should be controlled by a property */
if (TRUE) {
/* shift down/right one pixel */
gwy_data_view_coords_real_to_xy(data_view, xy[0], xy[1],
&xy_pixel[0], &xy_pixel[1]);
gwy_data_view_coords_real_to_xy(data_view, xy[2], xy[3],
&xy_pixel[2], &xy_pixel[3]);
for (j = 0; j < OBJECT_SIZE; j++)
xy_pixel[j]++;
gwy_data_view_coords_xy_to_real(data_view, xy_pixel[0], xy_pixel[1],
&xy[0], &xy[1]);
gwy_data_view_coords_xy_to_real(data_view, xy_pixel[2], xy_pixel[3],
&xy[2], &xy[3]);
}
gwy_layer_ellipse_draw_ellipse(layer, data_view, drawable, target, xy);
}
}
static void
gwy_layer_ellipse_draw_ellipse(GwyVectorLayer *layer,
GwyDataView *data_view,
GdkDrawable *drawable,
GwyRenderingTarget target,
const gdouble *xy)
{
gint xmin, ymin, xmax, ymax, width, height;
gdouble xreal, yreal;
switch (target) {
case GWY_RENDERING_TARGET_SCREEN:
gwy_data_view_coords_real_to_xy(data_view, xy[0], xy[1], &xmin, &ymin);
gwy_data_view_coords_real_to_xy(data_view, xy[2], xy[3], &xmax, &ymax);
break;
case GWY_RENDERING_TARGET_PIXMAP_IMAGE:
gwy_data_view_get_real_data_sizes(data_view, &xreal, &yreal);
gdk_drawable_get_size(drawable, &width, &height);
xmin = floor(xy[0]*width/xreal);
ymin = floor(xy[1]*height/yreal);
xmax = floor(xy[2]*width/xreal);
ymax = floor(xy[3]*height/yreal);
break;
default:
g_return_if_reached();
break;
}
if (xmax < xmin)
GWY_SWAP(gint, xmin, xmax);
if (ymax < ymin)
GWY_SWAP(gint, ymin, ymax);
gdk_draw_arc(drawable, layer->gc, FALSE,
xmin, ymin, xmax - xmin, ymax - ymin,
0, 64*360);
}
static gboolean
gwy_layer_ellipse_motion_notify(GwyVectorLayer *layer,
GdkEventMotion *event)
{
GwyDataView *data_view;
GdkWindow *window;
GdkCursor *cursor;
gint x, y, i;
gdouble xreal, yreal, xsize, ysize, xy[OBJECT_SIZE];
gdouble circleW, circleH;
gboolean circle;
GwyLayerEllipse *layer_ellipse = GWY_LAYER_ELLIPSE(layer);
if (!layer->selection)
return FALSE;
/* FIXME: No cursor change hint -- a bit too crude? */
if (!layer->editable)
return FALSE;
data_view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent);
g_return_val_if_fail(data_view, FALSE);
window = GTK_WIDGET(data_view)->window;
i = layer->selecting;
if (event->is_hint)
gdk_window_get_pointer(window, &x, &y, NULL);
else {
x = event->x;
y = event->y;
}
circle = event->state & GDK_SHIFT_MASK;
gwy_debug("x = %d, y = %d", x, y);
gwy_data_view_coords_xy_clamp(data_view, &x, &y);
gwy_data_view_coords_xy_to_real(data_view, x, y, &xreal, &yreal);
if (i > -1)
gwy_selection_get_object(layer->selection, i, xy);
if (i > -1 && xreal == xy[2] && yreal == xy[3])
return FALSE;
if (!layer->button) {
i = gwy_layer_ellipse_near_point(layer, xreal, yreal);
if (i >= 0) {
i = i % OBJECT_SIZE;
cursor = GWY_LAYER_ELLIPSE(layer)->corner_cursor[i];
}
else
cursor = NULL;
gdk_window_set_cursor(window, cursor);
return FALSE;
}
g_assert(layer->selecting != -1);
GWY_LAYER_ELLIPSE(layer)->circle = circle;
gwy_layer_ellipse_undraw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN, i);
if (circle)
gwy_layer_ellipse_squarize(data_view, x, y, xy);
else {
xy[2] = xreal;
xy[3] = yreal;
}
if (layer_ellipse->snap) {
/* shift selection to be centered around origin */
gwy_data_view_get_real_data_sizes(data_view, &xsize, &ysize);
if (circle) {
circleW = xy[2] - xy[0];
circleH = xy[3] - xy[1];
xy[0] = xsize / 2.0 - circleW / 2.0;
xy[1] = ysize / 2.0 - circleH / 2.0;
xy[2] = xy[0] + circleW;
xy[3] = xy[1] + circleH;
}
else {
xy[0] = -xy[2] + xsize;
xy[1] = -xy[3] + ysize;
}
/* XXX - this should be controlled by a property */
if (TRUE) {
/* shift down/right one pixel */
gwy_data_view_coords_real_to_xy(data_view, xy[0], xy[1], &x, &y);
x++;
y++;
gwy_data_view_coords_xy_to_real(data_view, x, y, &xy[0], &xy[1]);
}
}
gwy_selection_set_object(layer->selection, i, xy);
gwy_layer_ellipse_draw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN, i);
return FALSE;
}
static gboolean
gwy_layer_ellipse_button_pressed(GwyVectorLayer *layer,
GdkEventButton *event)
{
GwyDataView *data_view;
GdkWindow *window;
gint x, y, i;
gdouble xreal, yreal, xy[OBJECT_SIZE];
gboolean circle;
GwyLayerEllipse *layer_ellipse = GWY_LAYER_ELLIPSE(layer);
if (!layer->selection)
return FALSE;
if (event->button != 1)
return FALSE;
data_view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent);
g_return_val_if_fail(data_view, FALSE);
window = GTK_WIDGET(data_view)->window;
x = event->x;
y = event->y;
circle = event->state & GDK_SHIFT_MASK;
gwy_data_view_coords_xy_clamp(data_view, &x, &y);
gwy_debug("[%d,%d]", x, y);
/* do nothing when we are outside */
if (x != event->x || y != event->y)
return FALSE;
gwy_data_view_coords_xy_to_real(data_view, x, y, &xreal, &yreal);
i = gwy_layer_ellipse_near_point(layer, xreal, yreal);
/* just emit "object-chosen" when selection is not editable */
if (!layer->editable) {
if (i >= 0)
gwy_vector_layer_object_chosen(layer, i/4);
return FALSE;
}
/* handle existing selection */
if (i >= 0) {
layer->selecting = i/4;
gwy_layer_ellipse_undraw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN,
layer->selecting);
gwy_selection_get_object(layer->selection, layer->selecting, xy);
if (i/2)
xy[0] = MIN(xy[0], xy[2]);
else
xy[0] = MAX(xy[0], xy[2]);
if (i%2)
xy[1] = MIN(xy[1], xy[3]);
else
xy[1] = MAX(xy[1], xy[3]);
if (circle)
gwy_layer_ellipse_squarize(data_view, x, y, xy);
else {
xy[2] = xreal;
xy[3] = yreal;
}
gwy_selection_set_object(layer->selection, layer->selecting, xy);
}
else {
xy[2] = xy[0] = xreal;
xy[3] = xy[1] = yreal;
/* add an object, or do nothing when maximum is reached */
i = -1;
if (gwy_selection_is_full(layer->selection)) {
if (gwy_selection_get_max_objects(layer->selection) > 1)
return FALSE;
i = 0;
gwy_layer_ellipse_undraw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN, i);
}
layer->selecting = 0; /* avoid "update" signal emission */
layer->selecting = gwy_selection_set_object(layer->selection, i, xy);
}
layer_ellipse->circle = circle;
layer->button = event->button;
gwy_layer_ellipse_draw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN,
layer->selecting);
gdk_window_set_cursor(window, layer_ellipse->resize_cursor);
gwy_vector_layer_object_chosen(layer, layer->selecting);
return FALSE;
}
static gboolean
gwy_layer_ellipse_button_released(GwyVectorLayer *layer,
GdkEventButton *event)
{
GwyDataView *data_view;
GdkWindow *window;
GdkCursor *cursor;
gint x, y, xx, yy, i;
gdouble xreal, yreal, xy[OBJECT_SIZE];
gboolean outside;
GwyLayerEllipse *layer_ellipse = GWY_LAYER_ELLIPSE(layer);
if (!layer->selection)
return FALSE;
if (!layer->button)
return FALSE;
data_view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent);
g_return_val_if_fail(data_view, FALSE);
window = GTK_WIDGET(data_view)->window;
layer->button = 0;
x = event->x;
y = event->y;
i = layer->selecting;
gwy_debug("i = %d", i);
gwy_data_view_coords_xy_clamp(data_view, &x, &y);
outside = (event->x != x) || (event->y != y);
gwy_data_view_coords_xy_to_real(data_view, x, y, &xreal, &yreal);
gwy_layer_ellipse_undraw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN, i);
gwy_selection_get_object(layer->selection, i, xy);
gwy_data_view_coords_real_to_xy(data_view, xy[0], xy[1], &xx, &yy);
gwy_debug("event: [%f, %f], xy: [%d, %d]", event->x, event->y, xx, yy);
if (xx == event->x || yy == event->y)
gwy_selection_delete_object(layer->selection, i);
else {
if (layer_ellipse->circle)
gwy_layer_ellipse_squarize(data_view, x, y, xy);
else {
xy[2] = xreal;
xy[3] = yreal;
}
if (xy[2] < xy[0])
GWY_SWAP(gdouble, xy[0], xy[2]);
if (xy[3] < xy[1])
GWY_SWAP(gdouble, xy[1], xy[3]);
gwy_selection_set_object(layer->selection, i, xy);
gwy_layer_ellipse_draw_object(layer, window,
GWY_RENDERING_TARGET_SCREEN, i);
}
layer->selecting = -1;
i = gwy_layer_ellipse_near_point(layer, xreal, yreal);
if (i >= 0) {
i = i % OBJECT_SIZE;
cursor = layer_ellipse->corner_cursor[i];
}
else
cursor = NULL;
outside = outside || (i == -1);
gdk_window_set_cursor(window, cursor);
gwy_selection_finished(layer->selection);
return FALSE;
}
static void
gwy_layer_ellipse_set_reflection(GwyLayerEllipse *layer,
gboolean draw_reflection)
{
GwyVectorLayer *vector_layer;
GtkWidget *parent;
g_return_if_fail(GWY_IS_LAYER_ELLIPSE(layer));
vector_layer = GWY_VECTOR_LAYER(layer);
parent = GWY_DATA_VIEW_LAYER(layer)->parent;
if (draw_reflection == layer->draw_reflection)
return;
if (parent && GTK_WIDGET_REALIZED(parent))
gwy_layer_ellipse_undraw(vector_layer, parent->window,
GWY_RENDERING_TARGET_SCREEN);
layer->draw_reflection = draw_reflection;
if (parent && GTK_WIDGET_REALIZED(parent))
gwy_layer_ellipse_draw(vector_layer, parent->window,
GWY_RENDERING_TARGET_SCREEN);
g_object_notify(G_OBJECT(layer), "draw-reflection");
}
static void
gwy_layer_ellipse_set_snap(GwyLayerEllipse *layer, gboolean snap)
{
GwyVectorLayer *vector_layer;
GtkWidget *parent;
g_return_if_fail(GWY_IS_LAYER_ELLIPSE(layer));
vector_layer = GWY_VECTOR_LAYER(layer);
parent = GWY_DATA_VIEW_LAYER(layer)->parent;
if (snap == layer->snap)
return;
if (parent && GTK_WIDGET_REALIZED(parent))
gwy_layer_ellipse_undraw(vector_layer, parent->window,
GWY_RENDERING_TARGET_SCREEN);
layer->snap = snap;
if (parent && GTK_WIDGET_REALIZED(parent))
gwy_layer_ellipse_draw(vector_layer, parent->window,
GWY_RENDERING_TARGET_SCREEN);
g_object_notify(G_OBJECT(layer), "snap-to-center");
}
static void
gwy_layer_ellipse_realize(GwyDataViewLayer *dlayer)
{
GwyLayerEllipse *layer;
GdkDisplay *display;
gwy_debug("");
GWY_DATA_VIEW_LAYER_CLASS(gwy_layer_ellipse_parent_class)->realize(dlayer);
layer = GWY_LAYER_ELLIPSE(dlayer);
display = gtk_widget_get_display(dlayer->parent);
layer->resize_cursor = gdk_cursor_new_for_display(display, GDK_CROSS);
layer->corner_cursor[0] = gdk_cursor_new_for_display(display, GDK_UL_ANGLE);
layer->corner_cursor[1] = gdk_cursor_new_for_display(display, GDK_LL_ANGLE);
layer->corner_cursor[2] = gdk_cursor_new_for_display(display, GDK_UR_ANGLE);
layer->corner_cursor[3] = gdk_cursor_new_for_display(display, GDK_LR_ANGLE);
}
static void
gwy_layer_ellipse_unrealize(GwyDataViewLayer *dlayer)
{
GwyLayerEllipse *layer;
gint i;
gwy_debug("");
layer = GWY_LAYER_ELLIPSE(dlayer);
gdk_cursor_unref(layer->resize_cursor);
for (i = 0; i < 4; i++)
gdk_cursor_unref(layer->corner_cursor[i]);
GWY_DATA_VIEW_LAYER_CLASS(gwy_layer_ellipse_parent_class)->unrealize(dlayer);
}
#if 0
/* FIXME: This is a fake. For circle-like ellpises it's good, but for
* eccentric ellipses it's sometimes too large */
static gdouble
gwy_layer_ellipse_distance(gdouble *coords,
gdouble x,
gdouble y,
gint *quadrant)
{
gdouble a, b, theta, r;
a = (coords[2] - coords[0])/2;
b = (coords[3] - coords[1])/2;
x -= (coords[0] + coords[2])/2;
y -= (coords[1] + coords[3])/2;
theta = atan2(y, x);
r = a*b/hypot(b*cos(theta), a*sin(theta));
*quadrant = ((x >= 0) ? 2 : 0) | ((y >= 0) ? 1 : 0);
return fabs(hypot(x, y) - r);
}
static int
gwy_layer_ellipse_near_point(GwyVectorLayer *layer,
gdouble xreal, gdouble yreal)
{
GwyDataView *view;
gdouble d, dmin, xy[OBJECT_SIZE];
gint i, n, imin, quadrant;
if (!(n = gwy_selection_get_data(layer->selection, NULL)))
return -1;
dmin = G_MAXDOUBLE;
imin = -1;
for (i = 0; i < n; i++) {
gwy_selection_get_object(layer->selection, i, xy);
d = gwy_layer_ellipse_distance(xy, xreal, yreal, &quadrant);
if (d < dmin) {
imin = 4*i + quadrant;
dmin = d;
}
}
view = GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent);
/* FIXME: this is simply nonsense when x measure != y measure */
dmin /= sqrt(gwy_data_view_get_xmeasure(view)
*gwy_data_view_get_ymeasure(view));
if (dmin > PROXIMITY_DISTANCE)
return -1;
return imin;
}
#endif
static int
gwy_layer_ellipse_near_point(GwyVectorLayer *layer,
gdouble xreal, gdouble yreal)
{
gdouble d2min, xy[OBJECT_SIZE], metric[4];
gint i, n;
if (!(n = gwy_selection_get_data(layer->selection, NULL))
|| layer->focus >= n)
return -1;
gwy_data_view_get_metric(GWY_DATA_VIEW(GWY_DATA_VIEW_LAYER(layer)->parent),
metric);
if (layer->focus >= 0) {
gdouble coords[8];
gwy_selection_get_object(layer->selection, layer->focus, xy);
coords[0] = coords[2] = xy[0];
coords[1] = coords[5] = xy[1];
coords[4] = coords[6] = xy[2];
coords[3] = coords[7] = xy[3];
i = gwy_math_find_nearest_point(xreal, yreal, &d2min, 4, coords,
metric);
}
else {
gdouble *coords = g_newa(gdouble, 8*n);
for (i = 0; i < n; i++) {
gwy_selection_get_object(layer->selection, i, xy);
coords[8*i + 0] = coords[8*i + 2] = xy[0];
coords[8*i + 1] = coords[8*i + 5] = xy[1];
coords[8*i + 4] = coords[8*i + 6] = xy[2];
coords[8*i + 3] = coords[8*i + 7] = xy[3];
}
i = gwy_math_find_nearest_point(xreal, yreal, &d2min, 4*n, coords,
metric);
}
if (d2min > PROXIMITY_DISTANCE*PROXIMITY_DISTANCE)
return -1;
return i;
}
static void
gwy_layer_ellipse_squarize(GwyDataView *data_view,
gint x, gint y,
gdouble *xy)
{
gint size, xb, yb, xx, yy;
gwy_data_view_coords_real_to_xy(data_view, xy[0], xy[1], &xb, &yb);
size = MAX(ABS(x - xb), ABS(y - yb));
x = xx = (x >= xb) ? xb + size : xb - size;
y = yy = (y >= yb) ? yb + size : yb - size;
gwy_data_view_coords_xy_clamp(data_view, &xx, &yy);
if (xx != x || yy != y) {
size = MIN(ABS(xx - xb), ABS(yy - yb));
x = (xx >= xb) ? xb + size : xb - size;
y = (yy >= yb) ? yb + size : yb - size;
}
gwy_data_view_coords_xy_to_real(data_view, x, y, &xy[2], &xy[3]);
}
/* vim: set cin et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
|