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
|
/** \file
* Provides a class that shows a temporary indicator on the canvas of where the snap was, and what kind of snap
*
* Authors:
* Johan Engelen
* Diederik van Lierop
*
* Copyright (C) Johan Engelen 2009 <j.b.c.engelen@utwente.nl>
* Copyright (C) Diederik van Lierop 2009 <mail@diedenrezi.nl>
*
* Released under GNU GPL, read the file 'COPYING' for more information
*/
#include "display/snap-indicator.h"
#include "desktop.h"
#include "desktop-handles.h"
#include "display/sodipodi-ctrl.h"
#include "knot.h"
#include "preferences.h"
#include <glibmm/i18n.h>
#include <gtk/gtk.h>
namespace Inkscape {
namespace Display {
SnapIndicator::SnapIndicator(SPDesktop * desktop)
: _snaptarget(NULL),
_snapsource(NULL),
_desktop(desktop)
{
}
SnapIndicator::~SnapIndicator()
{
// remove item that might be present
remove_snaptarget();
remove_snapsource();
}
void
SnapIndicator::set_new_snaptarget(Inkscape::SnappedPoint const p)
{
remove_snaptarget();
g_assert(_desktop != NULL);
/* Commented out for now, because this might hide any snapping bug!
if (!p.getSnapped()) {
return; // If we haven't snapped, then it is of no use to draw a snapindicator
}
*/
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
bool value = prefs->getBool("/options/snapindicator/value", true);
if (value) {
gchar *target_name = _("UNDEFINED");
switch (p.getTarget()) {
case SNAPTARGET_UNDEFINED:
target_name = _("UNDEFINED");
break;
case SNAPTARGET_GRID:
target_name = _("grid line");
break;
case SNAPTARGET_GRID_INTERSECTION:
target_name = _("grid intersection");
break;
case SNAPTARGET_GUIDE:
target_name = _("guide");
break;
case SNAPTARGET_GUIDE_INTERSECTION:
target_name = _("guide intersection");
break;
case SNAPTARGET_GRID_GUIDE_INTERSECTION:
target_name = _("grid-guide intersection");
break;
case SNAPTARGET_NODE:
target_name = _("node");
break;
case SNAPTARGET_PATH:
target_name = _("path");
break;
case SNAPTARGET_PATH_INTERSECTION:
target_name = _("path intersection");
break;
case SNAPTARGET_BBOX_CORNER:
target_name = _("bounding box corner");
break;
case SNAPTARGET_BBOX_EDGE:
target_name = _("bounding box side");
break;
case SNAPTARGET_GRADIENT:
target_name = _("gradient");
break;
case SNAPTARGET_PAGE_BORDER:
target_name = _("page border");
break;
default:
g_warning("Snap target has not yet been defined!");
break;
}
// std::cout << "Snapped to: " << target_name << std::endl;
// Display the snap indicator (i.e. the cross)
SPCanvasItem * canvasitem = NULL;
if (p.getTarget() == SNAPTARGET_NODE) {
canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
SP_TYPE_CTRL,
"anchor", GTK_ANCHOR_CENTER,
"size", 10.0,
"stroked", TRUE,
"stroke_color", 0xf000f0ff,
"mode", SP_KNOT_MODE_XOR,
"shape", SP_KNOT_SHAPE_DIAMOND,
NULL );
} else {
canvasitem = sp_canvas_item_new(sp_desktop_tempgroup (_desktop),
SP_TYPE_CTRL,
"anchor", GTK_ANCHOR_CENTER,
"size", 10.0,
"stroked", TRUE,
"stroke_color", 0xf000f0ff,
"mode", SP_KNOT_MODE_XOR,
"shape", SP_KNOT_SHAPE_CROSS,
NULL );
}
const int timeout_val = 1000; // TODO add preference for snap indicator timeout?
SP_CTRL(canvasitem)->moveto(p.getPoint());
remove_snapsource(); // Don't set both the source and target indicators, as these will overlap
_snaptarget = _desktop->add_temporary_canvasitem(canvasitem, timeout_val);
// Display the tooltip
GtkSettings *settings = gtk_widget_get_settings (&(_desktop->canvas->widget));
// If we set the timeout too short, then the tooltip might not show at all (most noticeable when a long snap delay is active)
g_object_set(settings, "gtk-tooltip-timeout", 200, NULL); // tooltip will be shown after x msec.
gtk_widget_set_tooltip_text(&(_desktop->canvas->widget), target_name);
// has_tooltip will be true by now because gtk_widget_set_has_tooltip() has been called implicitly
update_tooltip();
// The snap indicator will be removed automatically because it's a temporary canvas item; the tooltip
// however must be removed manually, so we'll create a timer to that end
Glib::signal_timeout().connect(sigc::mem_fun(*this, &SnapIndicator::remove_tooltip), timeout_val);
}
}
void
SnapIndicator::remove_snaptarget()
{
if (_snaptarget) {
_desktop->remove_temporary_canvasitem(_snaptarget);
_snaptarget = NULL;
}
remove_tooltip();
}
void
SnapIndicator::set_new_snapsource(Geom::Point const p)
{
remove_snapsource();
g_assert(_desktop != NULL);
Inkscape::Preferences *prefs = Inkscape::Preferences::get();
bool value = prefs->getBool("/options/snapindicator/value", true);
if (value) {
SPCanvasItem * canvasitem = sp_canvas_item_new( sp_desktop_tempgroup (_desktop),
SP_TYPE_CTRL,
"anchor", GTK_ANCHOR_CENTER,
"size", 10.0,
"stroked", TRUE,
"stroke_color", 0xf000f0ff,
"mode", SP_KNOT_MODE_XOR,
"shape", SP_KNOT_SHAPE_CIRCLE,
NULL );
SP_CTRL(canvasitem)->moveto(p);
_snapsource = _desktop->add_temporary_canvasitem(canvasitem, 1000);
}
}
void
SnapIndicator::remove_snapsource()
{
if (_snapsource) {
_desktop->remove_temporary_canvasitem(_snapsource);
_snapsource = NULL;
}
}
// Shows or hides the tooltip
void SnapIndicator::update_tooltip() const
{
// When using gtk_widget_trigger_tooltip_query, the tooltip will for some reason always popup
// in the upper-left corner of the screen (probably at (0,0)). As a workaround we'll create
// a motion event instead, which will also trigger the tooltip
gint x, y;
GdkWindow *window;
GdkDisplay *display = gdk_display_get_default();
window = gdk_display_get_window_at_pointer(display, &x, &y);
if (window) {
GdkEvent *event = gdk_event_new(GDK_MOTION_NOTIFY);
event->motion.window = window;
event->motion.x = x;
event->motion.y = y;
event->motion.is_hint = FALSE;
gdk_window_get_origin(window, &x, &y);
event->motion.x_root = event->motion.x + x;
event->motion.y_root = event->motion.y + y;
gtk_main_do_event(event);
}
}
// Can be called either directly or through a timer
bool
SnapIndicator::remove_tooltip() const
{
gtk_widget_set_has_tooltip (&(_desktop->canvas->widget), false);
gtk_widget_trigger_tooltip_query(&(_desktop->canvas->widget));
return false;
}
} //namespace Display
} /* namespace Inkscape */
/*
Local Variables:
mode:c++
c-file-style:"stroustrup"
c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
indent-tabs-mode:nil
fill-column:99
End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4 :
|