1 /*
2 * Utility functions for switching tools (= contexts)
3 *
4 * Authors:
5 * bulia byak <buliabyak@users.sf.net>
6 * Josh Andler <scislac@users.sf.net>
7 *
8 * Copyright (C) 2003-2007 authors
9 *
10 * Released under GNU GPL, read the file 'COPYING' for more information
11 */
14 #ifdef HAVE_CONFIG_H
15 # include "config.h"
16 #endif
18 #include <cstring>
19 #include <string>
21 #include "inkscape-private.h"
22 #include "desktop.h"
23 #include "desktop-handles.h"
24 #include <glibmm/i18n.h>
26 #include <xml/repr.h>
28 #include "select-context.h"
29 #include "ui/tool/node-tool.h"
30 #include "tweak-context.h"
31 #include "spray-context.h"
32 #include "sp-path.h"
33 #include "rect-context.h"
34 #include "sp-rect.h"
35 #include "box3d-context.h"
36 #include "box3d.h"
37 #include "arc-context.h"
38 #include "sp-ellipse.h"
39 #include "star-context.h"
40 #include "sp-star.h"
41 #include "spiral-context.h"
42 #include "sp-spiral.h"
43 #include "dyna-draw-context.h"
44 #include "eraser-context.h"
45 #include "pen-context.h"
46 #include "pencil-context.h"
47 #include "lpe-tool-context.h"
48 #include "text-context.h"
49 #include "sp-text.h"
50 #include "sp-flowtext.h"
51 #include "gradient-context.h"
52 #include "zoom-context.h"
53 #include "dropper-context.h"
54 #include "connector-context.h"
55 #include "flood-context.h"
56 #include "sp-offset.h"
57 #include "message-context.h"
59 #include "tools-switch.h"
61 static char const *const tool_names[] = {
62 NULL,
63 "/tools/select",
64 "/tools/nodes",
65 "/tools/tweak",
66 "/tools/spray",
67 "/tools/shapes/rect",
68 "/tools/shapes/3dbox",
69 "/tools/shapes/arc",
70 "/tools/shapes/star",
71 "/tools/shapes/spiral",
72 "/tools/freehand/pencil",
73 "/tools/freehand/pen",
74 "/tools/calligraphic",
75 "/tools/text",
76 "/tools/gradient",
77 "/tools/zoom",
78 "/tools/dropper",
79 "/tools/connector",
80 "/tools/paintbucket",
81 "/tools/eraser",
82 "/tools/lpetool",
83 NULL
84 };
86 static int
87 tools_prefpath2num(char const *id)
88 {
89 int i = 1;
90 while (tool_names[i]) {
91 if (strcmp(tool_names[i], id) == 0)
92 return i;
93 else i++;
94 }
95 g_assert( 0 == TOOLS_INVALID );
96 return 0; //nothing found
97 }
99 int
100 tools_isactive(SPDesktop *dt, unsigned num)
101 {
102 g_assert( num < G_N_ELEMENTS(tool_names) );
103 if (SP_IS_EVENT_CONTEXT(dt->event_context))
104 return dt->event_context->pref_observer->observed_path == tool_names[num];
105 else return FALSE;
106 }
108 int
109 tools_active(SPDesktop *dt)
110 {
111 return tools_prefpath2num(dt->event_context->pref_observer->observed_path.data());
112 }
114 void
115 tools_switch(SPDesktop *dt, int num)
116 {
117 if (dt) {
118 dt->_tool_changed.emit(num);
119 }
121 switch (num) {
122 case TOOLS_SELECT:
123 dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
124 /* fixme: This is really ugly hack. We should bind and unbind class methods */
125 dt->activate_guides(true);
126 inkscape_eventcontext_set(sp_desktop_event_context(dt));
127 break;
128 case TOOLS_NODES:
129 dt->set_event_context(INK_TYPE_NODE_TOOL, tool_names[num]);
130 dt->activate_guides(true);
131 inkscape_eventcontext_set(sp_desktop_event_context(dt));
132 break;
133 case TOOLS_TWEAK:
134 dt->set_event_context(SP_TYPE_TWEAK_CONTEXT, tool_names[num]);
135 dt->activate_guides(true);
136 inkscape_eventcontext_set(sp_desktop_event_context(dt));
137 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To tweak a path by pushing, select it and drag over it."));
138 break;
139 case TOOLS_SPRAY:
140 dt->set_event_context(SP_TYPE_SPRAY_CONTEXT, tool_names[num]);
141 dt->activate_guides(true);
142 inkscape_eventcontext_set(sp_desktop_event_context(dt));
143 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To spray a path by pushing, select it and drag over it."));
144 break;
145 case TOOLS_SHAPES_RECT:
146 dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
147 dt->activate_guides(false);
148 inkscape_eventcontext_set(sp_desktop_event_context(dt));
149 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a rectangle. <b>Drag controls</b> to round corners and resize. <b>Click</b> to select."));
150 break;
151 case TOOLS_SHAPES_3DBOX:
152 dt->set_event_context(SP_TYPE_BOX3D_CONTEXT, tool_names[num]);
153 dt->activate_guides(false);
154 inkscape_eventcontext_set(sp_desktop_event_context(dt));
155 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a 3D box. <b>Drag controls</b> to resize in perspective. <b>Click</b> to select (with <b>Ctrl+Alt</b> for single faces)."));
156 break;
157 case TOOLS_SHAPES_ARC:
158 dt->set_event_context(SP_TYPE_ARC_CONTEXT, tool_names[num]);
159 dt->activate_guides(false);
160 inkscape_eventcontext_set(sp_desktop_event_context(dt));
161 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create an ellipse. <b>Drag controls</b> to make an arc or segment. <b>Click</b> to select."));
162 break;
163 case TOOLS_SHAPES_STAR:
164 dt->set_event_context(SP_TYPE_STAR_CONTEXT, tool_names[num]);
165 dt->activate_guides(false);
166 inkscape_eventcontext_set(sp_desktop_event_context(dt));
167 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a star. <b>Drag controls</b> to edit the star shape. <b>Click</b> to select."));
168 break;
169 case TOOLS_SHAPES_SPIRAL:
170 dt->set_event_context(SP_TYPE_SPIRAL_CONTEXT, tool_names[num]);
171 dt->activate_guides(false);
172 inkscape_eventcontext_set(sp_desktop_event_context(dt));
173 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a spiral. <b>Drag controls</b> to edit the spiral shape. <b>Click</b> to select."));
174 break;
175 case TOOLS_FREEHAND_PENCIL:
176 dt->set_event_context(SP_TYPE_PENCIL_CONTEXT, tool_names[num]);
177 dt->activate_guides(false);
178 inkscape_eventcontext_set(sp_desktop_event_context(dt));
179 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a freehand line. <b>Shift</b> appends to selected path, <b>Alt</b> activates sketch mode."));
180 break;
181 case TOOLS_FREEHAND_PEN:
182 dt->set_event_context(SP_TYPE_PEN_CONTEXT, tool_names[num]);
183 dt->activate_guides(false);
184 inkscape_eventcontext_set(sp_desktop_event_context(dt));
185 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>click and drag</b> to start a path; with <b>Shift</b> to append to selected path. <b>Ctrl+click</b> to create single dots (straight line modes only)."));
186 break;
187 case TOOLS_CALLIGRAPHIC:
188 dt->set_event_context(SP_TYPE_DYNA_DRAW_CONTEXT, tool_names[num]);
189 dt->activate_guides(false);
190 inkscape_eventcontext_set(sp_desktop_event_context(dt));
191 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to draw a calligraphic stroke; with <b>Ctrl</b> to track a guide path. <b>Arrow keys</b> adjust width (left/right) and angle (up/down)."));
192 break;
193 case TOOLS_TEXT:
194 dt->set_event_context(SP_TYPE_TEXT_CONTEXT, tool_names[num]);
195 dt->activate_guides(false);
196 inkscape_eventcontext_set(sp_desktop_event_context(dt));
197 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
198 break;
199 case TOOLS_GRADIENT:
200 dt->set_event_context(SP_TYPE_GRADIENT_CONTEXT, tool_names[num]);
201 dt->activate_guides(false);
202 inkscape_eventcontext_set(sp_desktop_event_context(dt));
203 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> or <b>double click</b> to create a gradient on selected objects, <b>drag handles</b> to adjust gradients."));
204 break;
205 case TOOLS_ZOOM:
206 dt->set_event_context(SP_TYPE_ZOOM_CONTEXT, tool_names[num]);
207 dt->activate_guides(false);
208 inkscape_eventcontext_set(sp_desktop_event_context(dt));
209 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> or <b>drag around an area</b> to zoom in, <b>Shift+click</b> to zoom out."));
210 break;
211 case TOOLS_DROPPER:
212 dt->set_event_context(SP_TYPE_DROPPER_CONTEXT, tool_names[num]);
213 dt->activate_guides(false);
214 inkscape_eventcontext_set(sp_desktop_event_context(dt));
215 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to set fill, <b>Shift+click</b> to set stroke; <b>drag</b> to average color in area; with <b>Alt</b> to pick inverse color; <b>Ctrl+C</b> to copy the color under mouse to clipboard"));
216 break;
217 case TOOLS_CONNECTOR:
218 dt->set_event_context(SP_TYPE_CONNECTOR_CONTEXT, tool_names[num]);
219 dt->activate_guides(false);
220 inkscape_eventcontext_set(sp_desktop_event_context(dt));
221 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click and drag</b> between shapes to create a connector."));
222 break;
223 case TOOLS_PAINTBUCKET:
224 dt->set_event_context(SP_TYPE_FLOOD_CONTEXT, tool_names[num]);
225 dt->activate_guides(false);
226 inkscape_eventcontext_set(sp_desktop_event_context(dt));
227 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to paint a bounded area, <b>Shift+click</b> to union the new fill with the current selection, <b>Ctrl+click</b> to change the clicked object's fill and stroke to the current setting."));
228 break;
229 case TOOLS_ERASER:
230 dt->set_event_context(SP_TYPE_ERASER_CONTEXT, tool_names[num]);
231 dt->activate_guides(false);
232 inkscape_eventcontext_set(sp_desktop_event_context(dt));
233 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to erase."));
234 break;
235 case TOOLS_LPETOOL:
236 dt->set_event_context(SP_TYPE_LPETOOL_CONTEXT, tool_names[num]);
237 dt->activate_guides(false);
238 inkscape_eventcontext_set(sp_desktop_event_context(dt));
239 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Choose a subtool from the toolbar"));
240 break;
241 }
242 }
244 void tools_switch_by_item(SPDesktop *dt, SPItem *item, Geom::Point const p)
245 {
246 if (SP_IS_RECT(item)) {
247 tools_switch(dt, TOOLS_SHAPES_RECT);
248 } else if (SP_IS_BOX3D(item)) {
249 tools_switch(dt, TOOLS_SHAPES_3DBOX);
250 } else if (SP_IS_GENERICELLIPSE(item)) {
251 tools_switch(dt, TOOLS_SHAPES_ARC);
252 } else if (SP_IS_STAR(item)) {
253 tools_switch(dt, TOOLS_SHAPES_STAR);
254 } else if (SP_IS_SPIRAL(item)) {
255 tools_switch(dt, TOOLS_SHAPES_SPIRAL);
256 } else if (SP_IS_PATH(item)) {
257 if (cc_item_is_connector(item)) {
258 tools_switch(dt, TOOLS_CONNECTOR);
259 }
260 else {
261 tools_switch(dt, TOOLS_NODES);
262 }
263 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
264 tools_switch(dt, TOOLS_TEXT);
265 sp_text_context_place_cursor_at (SP_TEXT_CONTEXT(dt->event_context), SP_OBJECT(item), p);
266 } else if (SP_IS_OFFSET(item)) {
267 tools_switch(dt, TOOLS_NODES);
268 }
269 }
271 /*
272 Local Variables:
273 mode:c++
274 c-file-style:"stroustrup"
275 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
276 indent-tabs-mode:nil
277 fill-column:99
278 End:
279 */
280 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :