Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
tool-base.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Authors:
4 * Lauris Kaplinski <lauris@kaplinski.com>
5 * Frank Felfe <innerspace@iname.com>
6 *
7 * Copyright (C) 1999-2002 authors
8 * Copyright (C) 2001-2002 Ximian, Inc.
9 *
10 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
11 */
12
13#ifndef INKSCAPE_UI_TOOLS_TOOL_BASE_H
14#define INKSCAPE_UI_TOOLS_TOOL_BASE_H
15
16#include <cstddef>
17#include <string>
18#include <memory>
19#include <optional>
20#include <boost/noncopyable.hpp>
21
22#include <2geom/point.h>
23
24#include <sigc++/trackable.h>
25#include <gdk/gdk.h>
26#include <gtk/gtk.h> // GtkEventControllerKey
27#include <gdkmm/cursor.h>
28
29#include <sigc++/scoped_connection.h>
30#include "preferences.h"
32#include "util/action-accel.h"
33
34namespace Gtk {
35class EventControllerKey;
36class Widget;
37} // namespace Gtk
38
39class GrDrag;
40class SPDesktop;
41class SPObject;
42class SPItem;
43class SPGroup;
44class KnotHolder;
45
46namespace Inkscape {
47class MessageContext;
48class SelCue;
49struct CanvasEvent;
50struct MotionEvent;
51struct KeyEvent;
52struct ButtonPressEvent;
53
54namespace UI {
55class ShapeEditor;
56
57namespace Tools {
58class ToolBase;
59
61{
62public:
74
75 DelayedSnapEvent(ToolBase *tool, gpointer item, gpointer item2, MotionEvent const &event, DelayedSnapEvent::Origin origin);
76
77 ToolBase *getTool() const { return _tool; }
78 gpointer getItem() const { return _item; }
79 gpointer getItem2() const { return _item2; }
80 MotionEvent const &getEvent() const { return *_event; }
81 Origin getOrigin() const { return _origin; }
82
83private:
85 gpointer _item;
86 gpointer _item2;
87 std::unique_ptr<MotionEvent> _event;
89};
90
105 : public sigc::trackable
106 , boost::noncopyable
107{
108public:
109 ToolBase(SPDesktop *desktop, std::string &&prefs_path, std::string &&cursor_filename, bool uses_snap = true);
110 virtual ~ToolBase();
111
112 virtual void set(Preferences::Entry const &val);
113 virtual bool root_handler(CanvasEvent const &event);
114 virtual bool item_handler(SPItem *item, CanvasEvent const &event);
115 virtual void menu_popup(CanvasEvent const &event, SPObject *obj = nullptr);
116 virtual bool can_undo(bool redo = false) { return false; }
117 virtual bool is_ready() const { return true; }
118 virtual void switching_away(std::string const &new_tool) {}
119
120 std::string const &getPrefsPath() const { return _prefs_path; };
121 void enableSelectionCue(bool enable = true);
122
124
125 SPDesktop *getDesktop() const { return _desktop; }
126 SPGroup *currentLayer() const;
127
128 // name of the last active tool (if known) before *this* one, or empty string
129 void set_last_active_tool(Glib::ustring last_tool);
130 const Glib::ustring& get_last_active_tool() const;
131
132private:
133 std::unique_ptr<Preferences::PreferencesObserver> pref_observer;
134 std::string _prefs_path;
135
136 void set_on_buttons(CanvasEvent const &event);
137 bool are_buttons_1_and_3_on() const;
138 bool are_buttons_1_and_3_on(CanvasEvent const &event);
139
140protected:
141 Glib::RefPtr<Gdk::Cursor> _cursor;
142 std::string _cursor_filename = "select.svg";
143 std::string _cursor_default = "select.svg";
144
146 bool dragging = false;
147 int tolerance = 0;
148 bool within_tolerance = false;
149 bool _button1on = false;
150 bool _button2on = false;
151 bool _button3on = false;
154
156 void saveDragOrigin(Geom::Point const &pos);
157 bool checkDragMoved(Geom::Point const &pos);
158
159 // Commonly used CanvasItemCatchall grab/ungrab.
160 void grabCanvasEvents(EventMask mask =
165 void ungrabCanvasEvents();
166
167private:
169 {
171 PANNING_SPACE_BUTTON1 = 1, // TODO is this mode relevant?
174 PANNING_SPACE = 4
175 };
177
178 bool rotating = false;
180
181public:
182 bool start_root_handler(CanvasEvent const &event);
183 bool tool_root_handler(CanvasEvent const &event);
184 bool start_item_handler(SPItem *item, CanvasEvent const &event);
185 bool virtual_item_handler(SPItem *item, CanvasEvent const &event);
186
188 bool is_panning() const { return panning != 0; }
189
192
193 std::unique_ptr<MessageContext> message_context;
194 SelCue *_selcue = nullptr;
195
196 GrDrag *_grdrag = nullptr;
197
199
200 void snap_delay_handler(gpointer item, gpointer item2, MotionEvent const &event, DelayedSnapEvent::Origin origin);
203 bool _uses_snap = false;
204
205 void set_cursor(std::string filename);
206 void use_cursor(Glib::RefPtr<Gdk::Cursor> cursor);
207 Glib::RefPtr<Gdk::Cursor> get_cursor(Gtk::Widget &widget,
208 std::string const &filename) const;
209 void use_tool_cursor();
210
211 void enableGrDrag(bool enable = true);
212 bool deleteSelectedDrag(bool just_one);
213 bool hasGradientDrag() const;
214 GrDrag *get_drag() { return _grdrag; }
215
216protected:
218 void set_high_motion_precision(bool high_precision = true);
219
220 SPDesktop *_desktop = nullptr;
221
222 // Tool based shortcuts
228private:
230 bool _keyboardMove(KeyEvent const &event, Geom::Point const &dir);
231
232 std::optional<DelayedSnapEvent> _dse;
234 sigc::scoped_connection _dse_timeout_conn;
236 Glib::ustring _last_active_tool;
237};
238
239void sp_event_context_read(ToolBase *tool, char const *key);
240
241// Gobbling is no longer possible in GTK4. The following stubs
242// are left in place in case an alternative solution is required.
243inline int gobble_key_events(unsigned keyval, unsigned mask) { return 0; }
244inline void gobble_motion_events(unsigned mask) {}
245
246void sp_event_show_modifier_tip(MessageContext *message_context, KeyEvent const &event,
247 char const *ctrl_tip, char const *shift_tip, char const *alt_tip);
248
250// Prefer one of the non-_impl versions below. This is public for shortcuts.cpp.
251unsigned get_latin_keyval_impl(unsigned event_keyval, unsigned event_keycode,
252 GdkModifierType event_state, unsigned event_group,
253 unsigned *consumed_modifiers);
254unsigned get_latin_keyval(GtkEventControllerKey const *controller,
255 unsigned keyval, unsigned keycode, GdkModifierType state,
256 unsigned *consumed_modifiers = nullptr);
257unsigned get_latin_keyval(Gtk::EventControllerKey const &controller,
258 unsigned keyval, unsigned keycode, Gdk::ModifierType state,
259 unsigned *consumed_modifiers = nullptr);
260unsigned get_latin_keyval(KeyEvent const &event, unsigned *consumed_modifiers = nullptr);
261
262SPItem *sp_event_context_find_item(SPDesktop *desktop, Geom::Point const &p, bool select_under, bool into_groups);
264
266
267} // namespace Tools
268} // namespace UI
269} // namespace Inkscape
270
271#endif // INKSCAPE_UI_TOOLS_TOOL_BASE_H
272
273/*
274 Local Variables:
275 mode:c++
276 c-file-style:"stroustrup"
277 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
278 indent-tabs-mode:nil
279 fill-column:99
280 End:
281*/
282// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
Cartesian point / 2D vector and related operations.
Point origin
Definition aa.cpp:227
Action Accel A simple tracker for accelerator keys associated to an action.
void redo(SPDocument *document)
Two-dimensional point with integer coordinates.
Definition int-point.h:57
Two-dimensional point that doubles as a vector.
Definition point.h:66
This is the root class of the gradient dragging machinery.
A mask representing a subset of EventTypes.
Definition enums.h:38
A convenience class for working with MessageStacks.
Data type representing a typeless value of a preference.
std::unique_ptr< MotionEvent > _event
Definition tool-base.h:87
MotionEvent const & getEvent() const
Definition tool-base.h:80
Base class for Event processors.
Definition tool-base.h:107
bool is_space_panning() const
True if we're panning with the space bar.
Definition tool-base.h:191
bool sp_event_context_knot_mouseover() const
Returns true if we're hovering above a knot (needed because we don't want to pre-snap in that case).
Glib::RefPtr< Gdk::Cursor > get_cursor(Gtk::Widget &widget, std::string const &filename) const
Returns the Gdk Cursor for the given filename.
void ungrabCanvasEvents()
Ungrab events from the Canvas Catchall.
void set_cursor(std::string filename)
Sets the current cursor to the given filename.
void grabCanvasEvents(EventMask mask=EventType::KEY_PRESS|EventType::BUTTON_RELEASE|EventType::MOTION|EventType::BUTTON_PRESS)
Grab events from the Canvas Catchall.
std::optional< DelayedSnapEvent > _dse
Definition tool-base.h:232
void use_tool_cursor()
Uses the saved cursor, based on the saved filename.
virtual bool is_ready() const
Definition tool-base.h:117
bool is_panning() const
True if we're panning with any method (space bar, middle-mouse, right-mouse+Ctrl)
Definition tool-base.h:188
void _schedule_delayed_snap_event()
Internal function used to set process_delayed_snap_event() to occur a given delay in the future from ...
SPGroup * currentLayer() const
Glib::ustring _last_active_tool
Definition tool-base.h:236
bool within_tolerance
are we still within tolerance of origin
Definition tool-base.h:148
std::string const & getPrefsPath() const
Definition tool-base.h:120
SPDesktop * getDesktop() const
Definition tool-base.h:125
bool start_item_handler(SPItem *item, CanvasEvent const &event)
Starts handling item snapping and pass to virtual_item_handler afterwards.
bool dragging
are we dragging?
Definition tool-base.h:146
std::unique_ptr< MessageContext > message_context
Definition tool-base.h:193
sigc::scoped_connection _dse_timeout_conn
Definition tool-base.h:234
void setup_for_drag_start(ButtonPressEvent const &ev)
void set_last_active_tool(Glib::ustring last_tool)
void _filterEventForSnapping(SPItem *item, CanvasEvent const &event, DelayedSnapEvent::Origin origin)
Common code between root and item handlers related to delayed snap events.
void snap_delay_handler(gpointer item, gpointer item2, MotionEvent const &event, DelayedSnapEvent::Origin origin)
Analyses the current event, calculates the mouse speed, turns snapping off (temporarily) if the mouse...
SPItem * item_to_select
the item where mouse_press occurred, to be selected if this is a click not drag
Definition tool-base.h:152
Glib::RefPtr< Gdk::Cursor > _cursor
Definition tool-base.h:141
virtual bool can_undo(bool redo=false)
Definition tool-base.h:116
virtual void switching_away(std::string const &new_tool)
Definition tool-base.h:118
bool virtual_item_handler(SPItem *item, CanvasEvent const &event)
void use_cursor(Glib::RefPtr< Gdk::Cursor > cursor)
Set the cursor to this specific one, don't remember it.
virtual void menu_popup(CanvasEvent const &event, SPObject *obj=nullptr)
Create popup menu and tell Gtk to show it.
void set_high_motion_precision(bool high_precision=true)
Enable (or disable) high precision for motion events.
Geom::IntPoint xyp
where drag started
Definition tool-base.h:145
bool hasGradientDrag() const
Return true if there is a gradient drag.
std::unique_ptr< Preferences::PreferencesObserver > pref_observer
Definition tool-base.h:133
Util::ActionAccel _acc_undo
Definition tool-base.h:223
virtual bool root_handler(CanvasEvent const &event)
void process_delayed_snap_event()
When the delayed snap event timer expires, this method will be called and will re-inject the last mot...
void saveDragOrigin(Geom::Point const &pos)
bool checkDragMoved(Geom::Point const &pos)
Analyse the current position and return true once it has moved farther than tolerance from the drag o...
const Glib::ustring & get_last_active_tool() const
bool tool_root_handler(CanvasEvent const &event)
Calls the right tool's event handler, depending on the selected tool and state.
virtual bool item_handler(SPItem *item, CanvasEvent const &event)
Handles item specific events.
void enableGrDrag(bool enable=true)
bool start_root_handler(CanvasEvent const &event)
Handles snapping events for all tools and then passes to tool_root_handler.
Util::ActionAccel _acc_redo
Definition tool-base.h:224
void enableSelectionCue(bool enable=true)
Enables/disables the ToolBase's SelCue.
void set_on_buttons(CanvasEvent const &event)
This function allows to handle global tool events if _pre function is not fully overridden.
Util::ActionAccel _acc_quick_preview
Definition tool-base.h:225
Util::ActionAccel _acc_quick_pan
Definition tool-base.h:227
void discard_delayed_snap_event()
If a delayed snap event has been scheduled, this function will cancel it.
bool _keyboardMove(KeyEvent const &event, Geom::Point const &dir)
Moves the selected points along the supplied unit vector according to the modifier state of the suppl...
Util::ActionAccel _acc_quick_zoom
Definition tool-base.h:226
MessageContext * defaultMessageContext() const
Definition tool-base.h:123
bool deleteSelectedDrag(bool just_one)
Delete a selected GrDrag point.
The ActionAccel class stores the keyboard shortcuts for a given action and automatically keeps track ...
To do: update description of desktop.
Definition desktop.h:149
Base class for visual SVG elements.
Definition sp-item.h:109
SPObject is an abstract base class of all of the document nodes at the SVG document level.
Definition sp-object.h:160
static Glib::ustring const prefs_path
SPItem * item
Definition desktop.h:50
void init_latin_keys_group()
Initialize Latin keys group handling.
SPItem * sp_event_context_over_item(SPDesktop *desktop, SPItem *item, Geom::Point const &p)
Returns item if it is under point p in desktop, at any depth; otherwise returns NULL.
void sp_event_show_modifier_tip(MessageContext *message_context, KeyEvent const &event, char const *ctrl_tip, char const *shift_tip, char const *alt_tip)
Show tool context specific modifier tip.
SPItem * sp_event_context_find_item(SPDesktop *desktop, Geom::Point const &p, bool select_under, bool into_groups)
Returns item at point p in desktop.
void gobble_motion_events(unsigned mask)
Definition tool-base.h:244
unsigned get_latin_keyval(GtkEventControllerKey const *const controller, unsigned const keyval, unsigned const keycode, GdkModifierType const state, unsigned *consumed_modifiers)
Return the keyval corresponding to the event controller key in Latin group.
void sp_toggle_dropper(SPDesktop *dt)
Toggles current tool between active tool and dropper tool.
int gobble_key_events(unsigned keyval, unsigned mask)
Definition tool-base.h:243
void sp_event_context_read(ToolBase *tool, char const *key)
Calls virtual set() function of ToolBase.
unsigned get_latin_keyval_impl(unsigned const event_keyval, unsigned const event_keycode, GdkModifierType const event_state, unsigned const event_group, unsigned *consumed_modifiers)
Helper class to stream background task notifications as a series of messages.
static cairo_user_data_key_t key
Singleton class to access the preferences file in a convenient way.
A mouse button (left/right/middle) is pressed.
Abstract base class for events.
A key has been pressed.
Movement of the mouse pointer.
SPDesktop * desktop