PIKApp/app/display/pikacanvascursor.c

233 lines
7.0 KiB
C

/* PIKA - Photo and Image Kooker Application
* a rebranding of The GNU Image Manipulation Program (created with heckimp)
* A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
*
* Original copyright, applying to most contents (license remains unchanged):
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* pikacanvascursor.c
* Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <gegl.h>
#include <gtk/gtk.h>
#include "libpikabase/pikabase.h"
#include "libpikamath/pikamath.h"
#include "display-types.h"
#include "core/pika-cairo.h"
#include "pikacanvascursor.h"
#include "pikadisplayshell.h"
#define PIKA_CURSOR_SIZE 7
enum
{
PROP_0,
PROP_X,
PROP_Y
};
typedef struct _PikaCanvasCursorPrivate PikaCanvasCursorPrivate;
struct _PikaCanvasCursorPrivate
{
gdouble x;
gdouble y;
};
#define GET_PRIVATE(cursor) \
((PikaCanvasCursorPrivate *) pika_canvas_cursor_get_instance_private ((PikaCanvasCursor *) (cursor)))
/* local function prototypes */
static void pika_canvas_cursor_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void pika_canvas_cursor_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void pika_canvas_cursor_draw (PikaCanvasItem *item,
cairo_t *cr);
static cairo_region_t * pika_canvas_cursor_get_extents (PikaCanvasItem *item);
G_DEFINE_TYPE_WITH_PRIVATE (PikaCanvasCursor, pika_canvas_cursor,
PIKA_TYPE_CANVAS_ITEM)
#define parent_class pika_canvas_cursor_parent_class
static void
pika_canvas_cursor_class_init (PikaCanvasCursorClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
PikaCanvasItemClass *item_class = PIKA_CANVAS_ITEM_CLASS (klass);
object_class->set_property = pika_canvas_cursor_set_property;
object_class->get_property = pika_canvas_cursor_get_property;
item_class->draw = pika_canvas_cursor_draw;
item_class->get_extents = pika_canvas_cursor_get_extents;
g_object_class_install_property (object_class, PROP_X,
g_param_spec_double ("x", NULL, NULL,
-PIKA_MAX_IMAGE_SIZE,
PIKA_MAX_IMAGE_SIZE, 0,
PIKA_PARAM_READWRITE));
g_object_class_install_property (object_class, PROP_Y,
g_param_spec_double ("y", NULL, NULL,
-PIKA_MAX_IMAGE_SIZE,
PIKA_MAX_IMAGE_SIZE, 0,
PIKA_PARAM_READWRITE));
}
static void
pika_canvas_cursor_init (PikaCanvasCursor *cursor)
{
pika_canvas_item_set_line_cap (PIKA_CANVAS_ITEM (cursor),
CAIRO_LINE_CAP_SQUARE);
}
static void
pika_canvas_cursor_set_property (GObject *object,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
PikaCanvasCursorPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_X:
private->x = g_value_get_double (value);
break;
case PROP_Y:
private->y = g_value_get_double (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_canvas_cursor_get_property (GObject *object,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
PikaCanvasCursorPrivate *private = GET_PRIVATE (object);
switch (property_id)
{
case PROP_X:
g_value_set_double (value, private->x);
break;
case PROP_Y:
g_value_set_double (value, private->y);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
pika_canvas_cursor_draw (PikaCanvasItem *item,
cairo_t *cr)
{
PikaCanvasCursorPrivate *private = GET_PRIVATE (item);
gdouble x, y;
x = floor (private->x) + 0.5;
y = floor (private->y) + 0.5;
cairo_move_to (cr, x - PIKA_CURSOR_SIZE, y);
cairo_line_to (cr, x + PIKA_CURSOR_SIZE, y);
cairo_move_to (cr, x, y - PIKA_CURSOR_SIZE);
cairo_line_to (cr, x, y + PIKA_CURSOR_SIZE);
_pika_canvas_item_stroke (item, cr);
}
static cairo_region_t *
pika_canvas_cursor_get_extents (PikaCanvasItem *item)
{
PikaCanvasCursorPrivate *private = GET_PRIVATE (item);
cairo_rectangle_int_t rectangle;
gdouble x, y;
x = floor (private->x) + 0.5;
y = floor (private->y) + 0.5;
rectangle.x = floor (x - PIKA_CURSOR_SIZE - 1.5);
rectangle.y = floor (y - PIKA_CURSOR_SIZE - 1.5);
rectangle.width = ceil (x + PIKA_CURSOR_SIZE + 1.5) - rectangle.x;
rectangle.height = ceil (y + PIKA_CURSOR_SIZE + 1.5) - rectangle.y;
return cairo_region_create_rectangle (&rectangle);
}
PikaCanvasItem *
pika_canvas_cursor_new (PikaDisplayShell *shell)
{
g_return_val_if_fail (PIKA_IS_DISPLAY_SHELL (shell), NULL);
return g_object_new (PIKA_TYPE_CANVAS_CURSOR,
"shell", shell,
NULL);
}
void
pika_canvas_cursor_set (PikaCanvasItem *cursor,
gdouble x,
gdouble y)
{
PikaCanvasCursorPrivate *private;
g_return_if_fail (PIKA_IS_CANVAS_CURSOR (cursor));
private = GET_PRIVATE (cursor);
if (private->x != x || private->y != y)
{
pika_canvas_item_begin_change (cursor);
g_object_set (cursor,
"x", x,
"y", y,
NULL);
pika_canvas_item_end_change (cursor);
}
}