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 "sp-path.h"
32 #include "rect-context.h"
33 #include "sp-rect.h"
34 #include "box3d-context.h"
35 #include "box3d.h"
36 #include "arc-context.h"
37 #include "sp-ellipse.h"
38 #include "star-context.h"
39 #include "sp-star.h"
40 #include "spiral-context.h"
41 #include "sp-spiral.h"
42 #include "dyna-draw-context.h"
43 #include "eraser-context.h"
44 #include "pen-context.h"
45 #include "pencil-context.h"
46 #include "lpe-tool-context.h"
47 #include "text-context.h"
48 #include "sp-text.h"
49 #include "sp-flowtext.h"
50 #include "gradient-context.h"
51 #include "zoom-context.h"
52 #include "dropper-context.h"
53 #include "connector-context.h"
54 #include "flood-context.h"
55 #include "sp-offset.h"
56 #include "message-context.h"
58 #include "tools-switch.h"
60 static char const *const tool_names[] = {
61 NULL,
62 "/tools/select",
63 "/tools/nodes",
64 "/tools/tweak",
65 "/tools/shapes/rect",
66 "/tools/shapes/3dbox",
67 "/tools/shapes/arc",
68 "/tools/shapes/star",
69 "/tools/shapes/spiral",
70 "/tools/freehand/pencil",
71 "/tools/freehand/pen",
72 "/tools/calligraphic",
73 "/tools/text",
74 "/tools/gradient",
75 "/tools/zoom",
76 "/tools/dropper",
77 "/tools/connector",
78 "/tools/paintbucket",
79 "/tools/eraser",
80 "/tools/lpetool",
81 NULL
82 };
84 static int
85 tools_prefpath2num(char const *id)
86 {
87 int i = 1;
88 while (tool_names[i]) {
89 if (strcmp(tool_names[i], id) == 0)
90 return i;
91 else i++;
92 }
93 g_assert( 0 == TOOLS_INVALID );
94 return 0; //nothing found
95 }
97 int
98 tools_isactive(SPDesktop *dt, unsigned num)
99 {
100 g_assert( num < G_N_ELEMENTS(tool_names) );
101 if (SP_IS_EVENT_CONTEXT(dt->event_context))
102 return dt->event_context->pref_observer->observed_path == tool_names[num];
103 else return FALSE;
104 }
106 int
107 tools_active(SPDesktop *dt)
108 {
109 return tools_prefpath2num(dt->event_context->pref_observer->observed_path.data());
110 }
112 void
113 tools_switch(SPDesktop *dt, int num)
114 {
115 if (dt) {
116 dt->_tool_changed.emit(num);
117 }
119 switch (num) {
120 case TOOLS_SELECT:
121 dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
122 /* fixme: This is really ugly hack. We should bind and unbind class methods */
123 dt->activate_guides(true);
124 inkscape_eventcontext_set(sp_desktop_event_context(dt));
125 break;
126 case TOOLS_NODES:
127 dt->set_event_context(INK_TYPE_NODE_TOOL, tool_names[num]);
128 dt->activate_guides(true);
129 inkscape_eventcontext_set(sp_desktop_event_context(dt));
130 break;
131 case TOOLS_TWEAK:
132 dt->set_event_context(SP_TYPE_TWEAK_CONTEXT, tool_names[num]);
133 dt->activate_guides(true);
134 inkscape_eventcontext_set(sp_desktop_event_context(dt));
135 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To tweak a path by pushing, select it and drag over it."));
136 break;
137 case TOOLS_SHAPES_RECT:
138 dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
139 dt->activate_guides(false);
140 inkscape_eventcontext_set(sp_desktop_event_context(dt));
141 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."));
142 break;
143 case TOOLS_SHAPES_3DBOX:
144 dt->set_event_context(SP_TYPE_BOX3D_CONTEXT, tool_names[num]);
145 dt->activate_guides(false);
146 inkscape_eventcontext_set(sp_desktop_event_context(dt));
147 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)."));
148 break;
149 case TOOLS_SHAPES_ARC:
150 dt->set_event_context(SP_TYPE_ARC_CONTEXT, tool_names[num]);
151 dt->activate_guides(false);
152 inkscape_eventcontext_set(sp_desktop_event_context(dt));
153 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."));
154 break;
155 case TOOLS_SHAPES_STAR:
156 dt->set_event_context(SP_TYPE_STAR_CONTEXT, tool_names[num]);
157 dt->activate_guides(false);
158 inkscape_eventcontext_set(sp_desktop_event_context(dt));
159 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."));
160 break;
161 case TOOLS_SHAPES_SPIRAL:
162 dt->set_event_context(SP_TYPE_SPIRAL_CONTEXT, tool_names[num]);
163 dt->activate_guides(false);
164 inkscape_eventcontext_set(sp_desktop_event_context(dt));
165 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."));
166 break;
167 case TOOLS_FREEHAND_PENCIL:
168 dt->set_event_context(SP_TYPE_PENCIL_CONTEXT, tool_names[num]);
169 dt->activate_guides(false);
170 inkscape_eventcontext_set(sp_desktop_event_context(dt));
171 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."));
172 break;
173 case TOOLS_FREEHAND_PEN:
174 dt->set_event_context(SP_TYPE_PEN_CONTEXT, tool_names[num]);
175 dt->activate_guides(false);
176 inkscape_eventcontext_set(sp_desktop_event_context(dt));
177 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)."));
178 break;
179 case TOOLS_CALLIGRAPHIC:
180 dt->set_event_context(SP_TYPE_DYNA_DRAW_CONTEXT, tool_names[num]);
181 dt->activate_guides(false);
182 inkscape_eventcontext_set(sp_desktop_event_context(dt));
183 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)."));
184 break;
185 case TOOLS_TEXT:
186 dt->set_event_context(SP_TYPE_TEXT_CONTEXT, tool_names[num]);
187 dt->activate_guides(false);
188 inkscape_eventcontext_set(sp_desktop_event_context(dt));
189 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
190 break;
191 case TOOLS_GRADIENT:
192 dt->set_event_context(SP_TYPE_GRADIENT_CONTEXT, tool_names[num]);
193 dt->activate_guides(false);
194 inkscape_eventcontext_set(sp_desktop_event_context(dt));
195 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."));
196 break;
197 case TOOLS_ZOOM:
198 dt->set_event_context(SP_TYPE_ZOOM_CONTEXT, tool_names[num]);
199 dt->activate_guides(false);
200 inkscape_eventcontext_set(sp_desktop_event_context(dt));
201 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."));
202 break;
203 case TOOLS_DROPPER:
204 dt->set_event_context(SP_TYPE_DROPPER_CONTEXT, tool_names[num]);
205 dt->activate_guides(false);
206 inkscape_eventcontext_set(sp_desktop_event_context(dt));
207 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"));
208 break;
209 case TOOLS_CONNECTOR:
210 dt->set_event_context(SP_TYPE_CONNECTOR_CONTEXT, tool_names[num]);
211 dt->activate_guides(false);
212 inkscape_eventcontext_set(sp_desktop_event_context(dt));
213 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click and drag</b> between shapes to create a connector."));
214 break;
215 case TOOLS_PAINTBUCKET:
216 dt->set_event_context(SP_TYPE_FLOOD_CONTEXT, tool_names[num]);
217 dt->activate_guides(false);
218 inkscape_eventcontext_set(sp_desktop_event_context(dt));
219 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."));
220 break;
221 case TOOLS_ERASER:
222 dt->set_event_context(SP_TYPE_ERASER_CONTEXT, tool_names[num]);
223 dt->activate_guides(false);
224 inkscape_eventcontext_set(sp_desktop_event_context(dt));
225 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to erase."));
226 break;
227 case TOOLS_LPETOOL:
228 dt->set_event_context(SP_TYPE_LPETOOL_CONTEXT, tool_names[num]);
229 dt->activate_guides(false);
230 inkscape_eventcontext_set(sp_desktop_event_context(dt));
231 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Choose a subtool from the toolbar"));
232 break;
233 }
234 }
236 void tools_switch_by_item(SPDesktop *dt, SPItem *item, Geom::Point const p)
237 {
238 if (SP_IS_RECT(item)) {
239 tools_switch(dt, TOOLS_SHAPES_RECT);
240 } else if (SP_IS_BOX3D(item)) {
241 tools_switch(dt, TOOLS_SHAPES_3DBOX);
242 } else if (SP_IS_GENERICELLIPSE(item)) {
243 tools_switch(dt, TOOLS_SHAPES_ARC);
244 } else if (SP_IS_STAR(item)) {
245 tools_switch(dt, TOOLS_SHAPES_STAR);
246 } else if (SP_IS_SPIRAL(item)) {
247 tools_switch(dt, TOOLS_SHAPES_SPIRAL);
248 } else if (SP_IS_PATH(item)) {
249 if (cc_item_is_connector(item)) {
250 tools_switch(dt, TOOLS_CONNECTOR);
251 }
252 else {
253 tools_switch(dt, TOOLS_NODES);
254 }
255 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
256 tools_switch(dt, TOOLS_TEXT);
257 sp_text_context_place_cursor_at (SP_TEXT_CONTEXT(dt->event_context), SP_OBJECT(item), p);
258 } else if (SP_IS_OFFSET(item)) {
259 tools_switch(dt, TOOLS_NODES);
260 }
261 }
263 /*
264 Local Variables:
265 mode:c++
266 c-file-style:"stroustrup"
267 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
268 indent-tabs-mode:nil
269 fill-column:99
270 End:
271 */
272 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :