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 "node-context.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 "text-context.h"
47 #include "sp-text.h"
48 #include "sp-flowtext.h"
49 #include "gradient-context.h"
50 #include "zoom-context.h"
51 #include "dropper-context.h"
52 #include "connector-context.h"
53 #include "flood-context.h"
54 #include "sp-offset.h"
55 #include "message-context.h"
57 #include "tools-switch.h"
59 static char const *const tool_names[] = {
60 NULL,
61 "tools.select",
62 "tools.nodes",
63 "tools.tweak",
64 "tools.shapes.rect",
65 "tools.shapes.3dbox",
66 "tools.shapes.arc",
67 "tools.shapes.star",
68 "tools.shapes.spiral",
69 "tools.freehand.pencil",
70 "tools.freehand.pen",
71 "tools.calligraphic",
72 "tools.text",
73 "tools.gradient",
74 "tools.zoom",
75 "tools.dropper",
76 "tools.connector",
77 "tools.paintbucket",
78 "tools.eraser",
79 NULL
80 };
82 static char const *const tool_ids[] = {
83 NULL,
84 "select",
85 "nodes",
86 "tweak",
87 "rect",
88 "3dbox",
89 "arc",
90 "star",
91 "spiral",
92 "pencil",
93 "pen",
94 "calligraphic",
95 "text",
96 "gradient",
97 "zoom",
98 "dropper",
99 "connector",
100 "paintbucket",
101 "eraser",
102 NULL
103 };
105 static int
106 tools_id2num(char const *id)
107 {
108 int i = 1;
109 while (tool_ids[i]) {
110 if (strcmp(tool_ids[i], id) == 0)
111 return i;
112 else i++;
113 }
114 g_assert( 0 == TOOLS_INVALID );
115 return 0; //nothing found
116 }
118 int
119 tools_isactive(SPDesktop *dt, unsigned num)
120 {
121 g_assert( num < G_N_ELEMENTS(tool_ids) );
122 if (SP_IS_EVENT_CONTEXT(dt->event_context))
123 return (!strcmp(dt->event_context->prefs_repr->attribute("id"), tool_ids[num]));
124 else return FALSE;
125 }
127 int
128 tools_active(SPDesktop *dt)
129 {
130 return (tools_id2num(dt->event_context->prefs_repr->attribute("id")));
131 }
133 void
134 tools_switch(SPDesktop *dt, int num)
135 {
136 if (dt) {
137 dt->_tool_changed.emit(num);
138 }
140 switch (num) {
141 case TOOLS_SELECT:
142 dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
143 /* fixme: This is really ugly hack. We should bind and unbind class methods */
144 dt->activate_guides(true);
145 inkscape_eventcontext_set(sp_desktop_event_context(dt));
146 break;
147 case TOOLS_NODES:
148 dt->set_event_context(SP_TYPE_NODE_CONTEXT, tool_names[num]);
149 dt->activate_guides(true);
150 inkscape_eventcontext_set(sp_desktop_event_context(dt));
151 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To edit a path, <b>click</b>, <b>Shift+click</b>, or <b>drag around</b> nodes to select them, then <b>drag</b> nodes and handles. <b>Click</b> on an object to select."));
152 break;
153 case TOOLS_TWEAK:
154 dt->set_event_context(SP_TYPE_TWEAK_CONTEXT, tool_names[num]);
155 dt->activate_guides(true);
156 inkscape_eventcontext_set(sp_desktop_event_context(dt));
157 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To tweak a path by pushing, select it and drag over it."));
158 break;
159 case TOOLS_SHAPES_RECT:
160 dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
161 dt->activate_guides(false);
162 inkscape_eventcontext_set(sp_desktop_event_context(dt));
163 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."));
164 break;
165 case TOOLS_SHAPES_3DBOX:
166 dt->set_event_context(SP_TYPE_BOX3D_CONTEXT, tool_names[num]);
167 dt->activate_guides(false);
168 inkscape_eventcontext_set(sp_desktop_event_context(dt));
169 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)."));
170 break;
171 case TOOLS_SHAPES_ARC:
172 dt->set_event_context(SP_TYPE_ARC_CONTEXT, tool_names[num]);
173 dt->activate_guides(false);
174 inkscape_eventcontext_set(sp_desktop_event_context(dt));
175 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."));
176 break;
177 case TOOLS_SHAPES_STAR:
178 dt->set_event_context(SP_TYPE_STAR_CONTEXT, tool_names[num]);
179 dt->activate_guides(false);
180 inkscape_eventcontext_set(sp_desktop_event_context(dt));
181 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."));
182 break;
183 case TOOLS_SHAPES_SPIRAL:
184 dt->set_event_context(SP_TYPE_SPIRAL_CONTEXT, tool_names[num]);
185 dt->activate_guides(false);
186 inkscape_eventcontext_set(sp_desktop_event_context(dt));
187 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."));
188 break;
189 case TOOLS_FREEHAND_PENCIL:
190 dt->set_event_context(SP_TYPE_PENCIL_CONTEXT, tool_names[num]);
191 dt->activate_guides(false);
192 inkscape_eventcontext_set(sp_desktop_event_context(dt));
193 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to create a freehand line. Start drawing with <b>Shift</b> to append to selected path. <b>Ctrl+click</b> to create single dots."));
194 break;
195 case TOOLS_FREEHAND_PEN:
196 dt->set_event_context(SP_TYPE_PEN_CONTEXT, tool_names[num]);
197 dt->activate_guides(false);
198 inkscape_eventcontext_set(sp_desktop_event_context(dt));
199 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."));
200 break;
201 case TOOLS_CALLIGRAPHIC:
202 dt->set_event_context(SP_TYPE_DYNA_DRAW_CONTEXT, tool_names[num]);
203 dt->activate_guides(false);
204 inkscape_eventcontext_set(sp_desktop_event_context(dt));
205 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to draw a calligraphic stroke; with <b>Ctrl</b> to track a guide, with <b>Alt</b> to thin/thicken. <b>Arrow keys</b> adjust width (left/right) and angle (up/down)."));
206 break;
207 case TOOLS_TEXT:
208 dt->set_event_context(SP_TYPE_TEXT_CONTEXT, tool_names[num]);
209 dt->activate_guides(false);
210 inkscape_eventcontext_set(sp_desktop_event_context(dt));
211 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
212 break;
213 case TOOLS_GRADIENT:
214 dt->set_event_context(SP_TYPE_GRADIENT_CONTEXT, tool_names[num]);
215 dt->activate_guides(false);
216 inkscape_eventcontext_set(sp_desktop_event_context(dt));
217 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."));
218 break;
219 case TOOLS_ZOOM:
220 dt->set_event_context(SP_TYPE_ZOOM_CONTEXT, tool_names[num]);
221 dt->activate_guides(false);
222 inkscape_eventcontext_set(sp_desktop_event_context(dt));
223 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."));
224 break;
225 case TOOLS_DROPPER:
226 dt->set_event_context(SP_TYPE_DROPPER_CONTEXT, tool_names[num]);
227 dt->activate_guides(false);
228 inkscape_eventcontext_set(sp_desktop_event_context(dt));
229 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"));
230 break;
231 case TOOLS_CONNECTOR:
232 dt->set_event_context(SP_TYPE_CONNECTOR_CONTEXT, tool_names[num]);
233 dt->activate_guides(false);
234 inkscape_eventcontext_set(sp_desktop_event_context(dt));
235 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click and drag</b> between shapes to create a connector."));
236 break;
237 case TOOLS_PAINTBUCKET:
238 dt->set_event_context(SP_TYPE_FLOOD_CONTEXT, tool_names[num]);
239 dt->activate_guides(false);
240 inkscape_eventcontext_set(sp_desktop_event_context(dt));
241 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."));
242 break;
243 case TOOLS_ERASER:
244 dt->set_event_context(SP_TYPE_ERASER_CONTEXT, tool_names[num]);
245 dt->activate_guides(false);
246 inkscape_eventcontext_set(sp_desktop_event_context(dt));
247 dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to erase."));
248 break;
249 }
250 }
252 void
253 tools_switch_current(int num)
254 {
255 SPDesktop *dt = SP_ACTIVE_DESKTOP;
256 if (dt) tools_switch(dt, num);
257 }
259 void tools_switch_by_item(SPDesktop *dt, SPItem *item)
260 {
261 if (SP_IS_RECT(item)) {
262 tools_switch(dt, TOOLS_SHAPES_RECT);
263 } else if (SP_IS_BOX3D(item)) {
264 tools_switch(dt, TOOLS_SHAPES_3DBOX);
265 } else if (SP_IS_GENERICELLIPSE(item)) {
266 tools_switch(dt, TOOLS_SHAPES_ARC);
267 } else if (SP_IS_STAR(item)) {
268 tools_switch(dt, TOOLS_SHAPES_STAR);
269 } else if (SP_IS_SPIRAL(item)) {
270 tools_switch(dt, TOOLS_SHAPES_SPIRAL);
271 } else if (SP_IS_PATH(item)) {
272 if (cc_item_is_connector(item)) {
273 tools_switch(dt, TOOLS_CONNECTOR);
274 }
275 else {
276 tools_switch(dt, TOOLS_NODES);
277 }
278 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
279 tools_switch(dt, TOOLS_TEXT);
280 } else if (SP_IS_OFFSET(item)) {
281 tools_switch(dt, TOOLS_NODES);
282 }
283 }
285 /*
286 Local Variables:
287 mode:c++
288 c-file-style:"stroustrup"
289 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
290 indent-tabs-mode:nil
291 fill-column:99
292 End:
293 */
294 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :