summaryrefslogtreecommitdiffstats
path: root/src/nautilus-selection-canvas-item.c
blob: b54cb68ed161654672b4c6a41e5823853579f487 (plain)
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
/* Nautilus - Canvas item for floating selection.
 *
 * Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation
 * Copyright (C) 2011 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors: Federico Mena <federico@nuclecu.unam.mx>
 *          Cosimo Cecchi <cosimoc@redhat.com>
 */

#include <config.h>

#include "nautilus-selection-canvas-item.h"

#include <math.h>

enum
{
    PROP_X1 = 1,
    PROP_Y1,
    PROP_X2,
    PROP_Y2,
    NUM_PROPERTIES
};

static GParamSpec *properties[NUM_PROPERTIES] = { NULL };

typedef struct
{
    /*< public >*/
    int x0, y0, x1, y1;
}  Rect;

struct _NautilusSelectionCanvasItemDetails
{
    Rect last_update_rect;
    Rect last_outline_update_rect;
    int last_outline_update_width;

    double x1, y1, x2, y2;              /* Corners of item */
};

G_DEFINE_TYPE (NautilusSelectionCanvasItem, nautilus_selection_canvas_item, EEL_TYPE_CANVAS_ITEM);

static void
nautilus_selection_canvas_item_draw (EelCanvasItem  *item,
                                     cairo_t        *cr,
                                     cairo_region_t *region)
{
    NautilusSelectionCanvasItem *self;
    double x1, y1, x2, y2;
    int cx1, cy1, cx2, cy2;
    double i2w_dx, i2w_dy;
    GtkStyleContext *context;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (item);

    /* Get canvas pixel coordinates */
    i2w_dx = 0.0;
    i2w_dy = 0.0;
    eel_canvas_item_i2w (item, &i2w_dx, &i2w_dy);

    x1 = self->priv->x1 + i2w_dx;
    y1 = self->priv->y1 + i2w_dy;
    x2 = self->priv->x2 + i2w_dx;
    y2 = self->priv->y2 + i2w_dy;

    eel_canvas_w2c (item->canvas, x1, y1, &cx1, &cy1);
    eel_canvas_w2c (item->canvas, x2, y2, &cx2, &cy2);

    if (cx2 <= cx1 || cy2 <= cy1)
    {
        return;
    }

    context = gtk_widget_get_style_context (GTK_WIDGET (item->canvas));

    gtk_style_context_save (context);

    gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);

    cairo_save (cr);

    gtk_render_background (context, cr,
                           cx1, cy1,
                           cx2 - cx1,
                           cy2 - cy1);
    gtk_render_frame (context, cr,
                      cx1, cy1,
                      cx2 - cx1,
                      cy2 - cy1);

    cairo_restore (cr);

    gtk_style_context_restore (context);
}

static double
nautilus_selection_canvas_item_point (EelCanvasItem  *item,
                                      double          x,
                                      double          y,
                                      int             cx,
                                      int             cy,
                                      EelCanvasItem **actual_item)
{
    NautilusSelectionCanvasItem *self;
    double x1, y1, x2, y2;
    double hwidth;
    double dx, dy;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (item);
    *actual_item = item;

    /* Find the bounds for the rectangle plus its outline width */

    x1 = self->priv->x1;
    y1 = self->priv->y1;
    x2 = self->priv->x2;
    y2 = self->priv->y2;

    hwidth = (1.0 / item->canvas->pixels_per_unit) / 2.0;

    x1 -= hwidth;
    y1 -= hwidth;
    x2 += hwidth;
    y2 += hwidth;

    /* Is point inside rectangle (which can be hollow if it has no fill set)? */

    if ((x >= x1) && (y >= y1) && (x <= x2) && (y <= y2))
    {
        return 0.0;
    }

    /* Point is outside rectangle */

    if (x < x1)
    {
        dx = x1 - x;
    }
    else if (x > x2)
    {
        dx = x - x2;
    }
    else
    {
        dx = 0.0;
    }

    if (y < y1)
    {
        dy = y1 - y;
    }
    else if (y > y2)
    {
        dy = y - y2;
    }
    else
    {
        dy = 0.0;
    }

    return sqrt (dx * dx + dy * dy);
}

static void
request_redraw_borders (EelCanvas *canvas,
                        Rect      *update_rect,
                        int        width)
{
    /* Top */
    eel_canvas_request_redraw (canvas,
                               update_rect->x0, update_rect->y0,
                               update_rect->x1, update_rect->y0 + width);
    /* Bottom */
    eel_canvas_request_redraw (canvas,
                               update_rect->x0, update_rect->y1 - width,
                               update_rect->x1, update_rect->y1);
    /* Left */
    eel_canvas_request_redraw (canvas,
                               update_rect->x0, update_rect->y0,
                               update_rect->x0 + width, update_rect->y1);
    /* Right */
    eel_canvas_request_redraw (canvas,
                               update_rect->x1 - width, update_rect->y0,
                               update_rect->x1, update_rect->y1);
}

static Rect make_rect (int x0,
                       int y0,
                       int x1,
                       int y1);

static int
rect_empty (const Rect *src)
{
    return (src->x1 <= src->x0 || src->y1 <= src->y0);
}

static gboolean
rects_intersect (Rect r1,
                 Rect r2)
{
    if (r1.x0 >= r2.x1)
    {
        return FALSE;
    }
    if (r2.x0 >= r1.x1)
    {
        return FALSE;
    }
    if (r1.y0 >= r2.y1)
    {
        return FALSE;
    }
    if (r2.y0 >= r1.y1)
    {
        return FALSE;
    }
    return TRUE;
}

static void
diff_rects_guts (Rect ra,
                 Rect rb,
                 int *count,
                 Rect result[4])
{
    if (ra.x0 < rb.x0)
    {
        result[(*count)++] = make_rect (ra.x0, ra.y0, rb.x0, ra.y1);
    }
    if (ra.y0 < rb.y0)
    {
        result[(*count)++] = make_rect (ra.x0, ra.y0, ra.x1, rb.y0);
    }
    if (ra.x1 < rb.x1)
    {
        result[(*count)++] = make_rect (ra.x1, rb.y0, rb.x1, rb.y1);
    }
    if (ra.y1 < rb.y1)
    {
        result[(*count)++] = make_rect (rb.x0, ra.y1, rb.x1, rb.y1);
    }
}

static void
diff_rects (Rect r1,
            Rect r2,
            int *count,
            Rect result[4])
{
    g_assert (count != NULL);
    g_assert (result != NULL);

    *count = 0;

    if (rects_intersect (r1, r2))
    {
        diff_rects_guts (r1, r2, count, result);
        diff_rects_guts (r2, r1, count, result);
    }
    else
    {
        if (!rect_empty (&r1))
        {
            result[(*count)++] = r1;
        }
        if (!rect_empty (&r2))
        {
            result[(*count)++] = r2;
        }
    }
}

static Rect
make_rect (int x0,
           int y0,
           int x1,
           int y1)
{
    Rect r;

    r.x0 = x0;
    r.y0 = y0;
    r.x1 = x1;
    r.y1 = y1;
    return r;
}

static void
nautilus_selection_canvas_item_update (EelCanvasItem *item,
                                       double         i2w_dx,
                                       double         i2w_dy,
                                       gint           flags)
{
    NautilusSelectionCanvasItem *self;
    NautilusSelectionCanvasItemDetails *priv;
    double x1, y1, x2, y2;
    int cx1, cy1, cx2, cy2;
    int repaint_rects_count, i;
    GtkStyleContext *context;
    GtkBorder border;
    Rect update_rect, repaint_rects[4];

    if (EEL_CANVAS_ITEM_CLASS (nautilus_selection_canvas_item_parent_class)->update)
    {
        (*EEL_CANVAS_ITEM_CLASS (nautilus_selection_canvas_item_parent_class)->update)(item, i2w_dx, i2w_dy, flags);
    }

    self = NAUTILUS_SELECTION_CANVAS_ITEM (item);
    priv = self->priv;

    x1 = priv->x1 + i2w_dx;
    y1 = priv->y1 + i2w_dy;
    x2 = priv->x2 + i2w_dx;
    y2 = priv->y2 + i2w_dy;

    eel_canvas_w2c (item->canvas, x1, y1, &cx1, &cy1);
    eel_canvas_w2c (item->canvas, x2, y2, &cx2, &cy2);

    update_rect = make_rect (cx1, cy1, cx2 + 1, cy2 + 1);
    diff_rects (update_rect, priv->last_update_rect,
                &repaint_rects_count, repaint_rects);
    for (i = 0; i < repaint_rects_count; i++)
    {
        eel_canvas_request_redraw (item->canvas,
                                   repaint_rects[i].x0, repaint_rects[i].y0,
                                   repaint_rects[i].x1, repaint_rects[i].y1);
    }

    priv->last_update_rect = update_rect;

    context = gtk_widget_get_style_context (GTK_WIDGET (item->canvas));

    gtk_style_context_save (context);
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
    gtk_style_context_get_border (context, GTK_STATE_FLAG_NORMAL, &border);
    gtk_style_context_restore (context);

    cx1 -= border.left;
    cy1 -= border.top;
    cx2 += border.right;
    cy2 += border.bottom;

    update_rect = make_rect (cx1, cy1, cx2, cy2);
    request_redraw_borders (item->canvas, &update_rect,
                            border.left + border.top + border.right + border.bottom);
    request_redraw_borders (item->canvas, &priv->last_outline_update_rect,
                            priv->last_outline_update_width);
    priv->last_outline_update_rect = update_rect;
    priv->last_outline_update_width = border.left + border.top + border.right + border.bottom;

    item->x1 = cx1;
    item->y1 = cy1;
    item->x2 = cx2;
    item->y2 = cy2;
}

static void
nautilus_selection_canvas_item_translate (EelCanvasItem *item,
                                          double         dx,
                                          double         dy)
{
    NautilusSelectionCanvasItem *self;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (item);

    self->priv->x1 += dx;
    self->priv->y1 += dy;
    self->priv->x2 += dx;
    self->priv->y2 += dy;
}

static void
nautilus_selection_canvas_item_bounds (EelCanvasItem *item,
                                       double        *x1,
                                       double        *y1,
                                       double        *x2,
                                       double        *y2)
{
    NautilusSelectionCanvasItem *self;
    GtkStyleContext *context;
    GtkBorder border;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (item);
    context = gtk_widget_get_style_context (GTK_WIDGET (item->canvas));

    gtk_style_context_save (context);
    gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
    gtk_style_context_get_border (context, GTK_STATE_FLAG_NORMAL, &border);
    gtk_style_context_restore (context);

    *x1 = self->priv->x1 - (border.left / item->canvas->pixels_per_unit) / 2.0;
    *y1 = self->priv->y1 - (border.top / item->canvas->pixels_per_unit) / 2.0;
    *x2 = self->priv->x2 + (border.right / item->canvas->pixels_per_unit) / 2.0;
    *y2 = self->priv->y2 + (border.bottom / item->canvas->pixels_per_unit) / 2.0;
}

static void
nautilus_selection_canvas_item_set_property (GObject      *object,
                                             guint         param_id,
                                             const GValue *value,
                                             GParamSpec   *pspec)
{
    EelCanvasItem *item;
    NautilusSelectionCanvasItem *self;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (object);
    item = EEL_CANVAS_ITEM (object);

    switch (param_id)
    {
        case PROP_X1:
        {
            self->priv->x1 = g_value_get_double (value);

            eel_canvas_item_request_update (item);
        }
        break;

        case PROP_Y1:
        {
            self->priv->y1 = g_value_get_double (value);

            eel_canvas_item_request_update (item);
        }
        break;

        case PROP_X2:
        {
            self->priv->x2 = g_value_get_double (value);

            eel_canvas_item_request_update (item);
        }
        break;

        case PROP_Y2:
        {
            self->priv->y2 = g_value_get_double (value);

            eel_canvas_item_request_update (item);
        }
        break;

        default:
        {
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        }
        break;
    }
}

static void
nautilus_selection_canvas_item_get_property (GObject    *object,
                                             guint       param_id,
                                             GValue     *value,
                                             GParamSpec *pspec)
{
    NautilusSelectionCanvasItem *self;

    self = NAUTILUS_SELECTION_CANVAS_ITEM (object);

    switch (param_id)
    {
        case PROP_X1:
        {
            g_value_set_double (value, self->priv->x1);
        }
        break;

        case PROP_Y1:
        {
            g_value_set_double (value, self->priv->y1);
        }
        break;

        case PROP_X2:
        {
            g_value_set_double (value, self->priv->x2);
        }
        break;

        case PROP_Y2:
        {
            g_value_set_double (value, self->priv->y2);
        }
        break;

        default:
        {
            G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        }
        break;
    }
}

static void
nautilus_selection_canvas_item_class_init (NautilusSelectionCanvasItemClass *klass)
{
    EelCanvasItemClass *item_class;
    GObjectClass *gobject_class;

    gobject_class = G_OBJECT_CLASS (klass);
    item_class = EEL_CANVAS_ITEM_CLASS (klass);

    gobject_class->set_property = nautilus_selection_canvas_item_set_property;
    gobject_class->get_property = nautilus_selection_canvas_item_get_property;

    item_class->draw = nautilus_selection_canvas_item_draw;
    item_class->point = nautilus_selection_canvas_item_point;
    item_class->update = nautilus_selection_canvas_item_update;
    item_class->bounds = nautilus_selection_canvas_item_bounds;
    item_class->translate = nautilus_selection_canvas_item_translate;

    properties[PROP_X1] =
        g_param_spec_double ("x1", NULL, NULL,
                             -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                             G_PARAM_READWRITE);
    properties[PROP_Y1] =
        g_param_spec_double ("y1", NULL, NULL,
                             -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                             G_PARAM_READWRITE);
    properties[PROP_X2] =
        g_param_spec_double ("x2", NULL, NULL,
                             -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                             G_PARAM_READWRITE);
    properties[PROP_Y2] =
        g_param_spec_double ("y2", NULL, NULL,
                             -G_MAXDOUBLE, G_MAXDOUBLE, 0,
                             G_PARAM_READWRITE);

    g_object_class_install_properties (gobject_class, NUM_PROPERTIES, properties);
    g_type_class_add_private (klass, sizeof (NautilusSelectionCanvasItemDetails));
}

static void
nautilus_selection_canvas_item_init (NautilusSelectionCanvasItem *self)
{
    self->priv = G_TYPE_INSTANCE_GET_PRIVATE (self, NAUTILUS_TYPE_SELECTION_CANVAS_ITEM,
                                              NautilusSelectionCanvasItemDetails);
}