aboutsummaryrefslogtreecommitdiffstats
path: root/e-util/e-canvas.c
blob: 74af105b36b15419eb899e9abe58e415ff222e25 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* 
 * e-canvas.c
 * Copyright (C) 2000  Helix Code, Inc.
 * Author: Chris Lahey <clahey@helixcode.com>
 *
 * This library 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 library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <gnome.h>
#include "e-canvas.h"
static void e_canvas_init       (ECanvas         *card);
static void e_canvas_class_init (ECanvasClass    *klass);
static gint e_canvas_key            (GtkWidget        *widget,
                     GdkEventKey      *event);

static int emit_event (GnomeCanvas *canvas, GdkEvent *event);

static GnomeCanvasClass *parent_class = NULL;

GtkType
e_canvas_get_type (void)
{
  static GtkType canvas_type = 0;

  if (!canvas_type)
    {
      static const GtkTypeInfo canvas_info =
      {
        "ECanvas",
        sizeof (ECanvas),
        sizeof (ECanvasClass),
        (GtkClassInitFunc) e_canvas_class_init,
        (GtkObjectInitFunc) e_canvas_init,
        /* reserved_1 */ NULL,
        /* reserved_2 */ NULL,
        (GtkClassInitFunc) NULL,
      };

      canvas_type = gtk_type_unique (gnome_canvas_get_type (), &canvas_info);
    }

  return canvas_type;
}

static void
e_canvas_class_init (ECanvasClass *klass)
{
    GtkObjectClass *object_class;
    GnomeCanvasClass *canvas_class;
    GtkWidgetClass *widget_class;
    
    object_class = (GtkObjectClass*) klass;
    canvas_class = (GnomeCanvasClass *) klass;
    widget_class = (GtkWidgetClass *) klass;
    
    parent_class = gtk_type_class (gnome_canvas_get_type ());

    widget_class->key_press_event = e_canvas_key;
    widget_class->key_release_event = e_canvas_key;
}

static void
e_canvas_init (ECanvas *canvas)
{
}

GtkWidget *
e_canvas_new()
{
    return GTK_WIDGET (gtk_type_new (e_canvas_get_type ()));
}


/* Returns whether the item is an inferior of or is equal to the parent. */
static int
is_descendant (GnomeCanvasItem *item, GnomeCanvasItem *parent)
{
    for (; item; item = item->parent)
        if (item == parent)
            return TRUE;

    return FALSE;
}

/* Emits an event for an item in the canvas, be it the current item, grabbed
 * item, or focused item, as appropriate.
 */
static int
emit_event (GnomeCanvas *canvas, GdkEvent *event)
{
    GdkEvent ev;
    gint finished;
    GnomeCanvasItem *item;
    GnomeCanvasItem *parent;
    guint mask;

    /* Perform checks for grabbed items */

    if (canvas->grabbed_item && !is_descendant (canvas->current_item, canvas->grabbed_item))
        return FALSE;

    if (canvas->grabbed_item) {
        switch (event->type) {
        case GDK_ENTER_NOTIFY:
            mask = GDK_ENTER_NOTIFY_MASK;
            break;

        case GDK_LEAVE_NOTIFY:
            mask = GDK_LEAVE_NOTIFY_MASK;
            break;

        case GDK_MOTION_NOTIFY:
            mask = GDK_POINTER_MOTION_MASK;
            break;

        case GDK_BUTTON_PRESS:
        case GDK_2BUTTON_PRESS:
        case GDK_3BUTTON_PRESS:
            mask = GDK_BUTTON_PRESS_MASK;
            break;

        case GDK_BUTTON_RELEASE:
            mask = GDK_BUTTON_RELEASE_MASK;
            break;

        case GDK_KEY_PRESS:
            mask = GDK_KEY_PRESS_MASK;
            break;

        case GDK_KEY_RELEASE:
            mask = GDK_KEY_RELEASE_MASK;
            break;

        default:
            mask = 0;
            break;
        }

        if (!(mask & canvas->grabbed_event_mask))
            return FALSE;
    }

    /* Convert to world coordinates -- we have two cases because of diferent
     * offsets of the fields in the event structures.
     */

    ev = *event;

    switch (ev.type) {
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
        gnome_canvas_window_to_world (canvas,
                          ev.crossing.x, ev.crossing.y,
                          &ev.crossing.x, &ev.crossing.y);
        break;

    case GDK_MOTION_NOTIFY:
    case GDK_BUTTON_PRESS:
    case GDK_2BUTTON_PRESS:
    case GDK_3BUTTON_PRESS:
    case GDK_BUTTON_RELEASE:
        gnome_canvas_window_to_world (canvas,
                          ev.motion.x, ev.motion.y,
                          &ev.motion.x, &ev.motion.y);
        break;

    default:
        break;
    }

    /* Choose where we send the event */

    item = canvas->current_item;

    if (canvas->focused_item
        && ((event->type == GDK_KEY_PRESS) || (event->type == GDK_KEY_RELEASE) || (event->type == GDK_FOCUS_CHANGE)))
        item = canvas->focused_item;

    /* The event is propagated up the hierarchy (for if someone connected to
     * a group instead of a leaf event), and emission is stopped if a
     * handler returns TRUE, just like for GtkWidget events.
     */

    finished = FALSE;

    while (item && !finished) {
        gtk_object_ref (GTK_OBJECT (item));

        gtk_signal_emit_by_name (GTK_OBJECT (item), "event",
                     &ev,
                     &finished);

        if (GTK_OBJECT_DESTROYED (item))
            finished = TRUE;

        parent = item->parent;
        gtk_object_unref (GTK_OBJECT (item));

        item = parent;
    }

    return finished;
}

/* Key event handler for the canvas */
static gint
e_canvas_key (GtkWidget *widget, GdkEventKey *event)
{
    GnomeCanvas *canvas;

    g_return_val_if_fail (widget != NULL, FALSE);
    g_return_val_if_fail (GNOME_IS_CANVAS (widget), FALSE);
    g_return_val_if_fail (event != NULL, FALSE);

    canvas = GNOME_CANVAS (widget);

    return emit_event (canvas, (GdkEvent *) event);
}