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 "inkscape-private.h"
19 #include "desktop.h"
20 #include "desktop-handles.h"
21 #include <glibmm/i18n.h>
23 #include <xml/repr.h>
25 #include "select-context.h"
26 #include "node-context.h"
27 #include "tweak-context.h"
28 #include "sp-path.h"
29 #include "rect-context.h"
30 #include "sp-rect.h"
31 #include "box3d-context.h"
32 #include "box3d.h"
33 #include "arc-context.h"
34 #include "sp-ellipse.h"
35 #include "star-context.h"
36 #include "sp-star.h"
37 #include "spiral-context.h"
38 #include "sp-spiral.h"
39 #include "dyna-draw-context.h"
40 #include "pen-context.h"
41 #include "pencil-context.h"
42 #include "text-context.h"
43 #include "sp-text.h"
44 #include "sp-flowtext.h"
45 #include "gradient-context.h"
46 #include "zoom-context.h"
47 #include "dropper-context.h"
48 #include "connector-context.h"
49 #include "flood-context.h"
50 #include "sp-offset.h"
51 #include "message-context.h"
53 #include "tools-switch.h"
55 static char const *const tool_names[] = {
56 NULL,
57 "tools.select",
58 "tools.nodes",
59 "tools.tweak",
60 "tools.shapes.rect",
61 "tools.shapes.3dbox",
62 "tools.shapes.arc",
63 "tools.shapes.star",
64 "tools.shapes.spiral",
65 "tools.freehand.pencil",
66 "tools.freehand.pen",
67 "tools.calligraphic",
68 "tools.text",
69 "tools.gradient",
70 "tools.zoom",
71 "tools.dropper",
72 "tools.connector",
73 "tools.paintbucket",
74 NULL
75 };
77 static char const *const tool_ids[] = {
78 NULL,
79 "select",
80 "nodes",
81 "tweak",
82 "rect",
83 "3dbox",
84 "arc",
85 "star",
86 "spiral",
87 "pencil",
88 "pen",
89 "calligraphic",
90 "text",
91 "gradient",
92 "zoom",
93 "dropper",
94 "connector",
95 "paintbucket",
96 NULL
97 };
99 static int
100 tools_id2num(char const *id)
101 {
102 int i = 1;
103 while (tool_ids[i]) {
104 if (strcmp(tool_ids[i], id) == 0)
105 return i;
106 else i++;
107 }
108 g_assert( 0 == TOOLS_INVALID );
109 return 0; //nothing found
110 }
112 int
113 tools_isactive(SPDesktop *dt, unsigned num)
114 {
115 g_assert( num < G_N_ELEMENTS(tool_ids) );
116 if (SP_IS_EVENT_CONTEXT(dt->event_context))
117 return (!strcmp(dt->event_context->prefs_repr->attribute("id"), tool_ids[num]));
118 else return FALSE;
119 }
121 int
122 tools_active(SPDesktop *dt)
123 {
124 return (tools_id2num(dt->event_context->prefs_repr->attribute("id")));
125 }
127 void
128 tools_switch(SPDesktop *dt, int num)
129 {
130 if (dt) {
131 dt->_tool_changed.emit(num);
132 }
134 switch (num) {
135 case TOOLS_SELECT:
136 dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
137 /* fixme: This is really ugly hack. We should bind and unbind class methods */
138 dt->activate_guides(true);
139 inkscape_eventcontext_set(sp_desktop_event_context(dt));
140 break;
141 case TOOLS_NODES:
142 dt->set_event_context(SP_TYPE_NODE_CONTEXT, tool_names[num]);
143 dt->activate_guides(true);
144 inkscape_eventcontext_set(sp_desktop_event_context(dt));
145 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."));
146 break;
147 case TOOLS_TWEAK:
148 dt->set_event_context(SP_TYPE_TWEAK_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 tweak a path by pushing, select it and drag over it."));
152 break;
153 case TOOLS_SHAPES_RECT:
154 dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
155 dt->activate_guides(false);
156 inkscape_eventcontext_set(sp_desktop_event_context(dt));
157 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."));
158 break;
159 case TOOLS_SHAPES_3DBOX:
160 dt->set_event_context(SP_TYPE_3DBOX_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 3D box. <b>Drag controls</b> to resize in perspective. <b>Click</b> to select (with <b>Ctrl+Alt</b> for single faces)."));
164 break;
165 case TOOLS_SHAPES_ARC:
166 dt->set_event_context(SP_TYPE_ARC_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 an ellipse. <b>Drag controls</b> to make an arc or segment. <b>Click</b> to select."));
170 break;
171 case TOOLS_SHAPES_STAR:
172 dt->set_event_context(SP_TYPE_STAR_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 a star. <b>Drag controls</b> to edit the star shape. <b>Click</b> to select."));
176 break;
177 case TOOLS_SHAPES_SPIRAL:
178 dt->set_event_context(SP_TYPE_SPIRAL_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 spiral. <b>Drag controls</b> to edit the spiral shape. <b>Click</b> to select."));
182 break;
183 case TOOLS_FREEHAND_PENCIL:
184 dt->set_event_context(SP_TYPE_PENCIL_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 freehand line. Start drawing with <b>Shift</b> to append to selected path."));
188 break;
189 case TOOLS_FREEHAND_PEN:
190 dt->set_event_context(SP_TYPE_PEN_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>Click</b> or <b>click and drag</b> to start a path; with <b>Shift</b> to append to selected path."));
194 break;
195 case TOOLS_CALLIGRAPHIC:
196 dt->set_event_context(SP_TYPE_DYNA_DRAW_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>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)."));
200 break;
201 case TOOLS_TEXT:
202 dt->set_event_context(SP_TYPE_TEXT_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>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
206 break;
207 case TOOLS_GRADIENT:
208 dt->set_event_context(SP_TYPE_GRADIENT_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>Drag</b> or <b>double click</b> to create a gradient on selected objects, <b>drag handles</b> to adjust gradients."));
212 break;
213 case TOOLS_ZOOM:
214 dt->set_event_context(SP_TYPE_ZOOM_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>Click</b> or <b>drag around an area</b> to zoom in, <b>Shift+click</b> to zoom out."));
218 break;
219 case TOOLS_DROPPER:
220 dt->set_event_context(SP_TYPE_DROPPER_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> 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"));
224 break;
225 case TOOLS_CONNECTOR:
226 dt->set_event_context(SP_TYPE_CONNECTOR_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 and drag</b> between shapes to create a connector."));
230 break;
231 case TOOLS_PAINTBUCKET:
232 dt->set_event_context(SP_TYPE_FLOOD_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</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."));
236 break;
237 }
238 }
240 void
241 tools_switch_current(int num)
242 {
243 SPDesktop *dt = SP_ACTIVE_DESKTOP;
244 if (dt) tools_switch(dt, num);
245 }
247 void tools_switch_by_item(SPDesktop *dt, SPItem *item)
248 {
249 if (SP_IS_RECT(item)) {
250 tools_switch(dt, TOOLS_SHAPES_RECT);
251 } else if (SP_IS_GENERICELLIPSE(item)) {
252 tools_switch(dt, TOOLS_SHAPES_ARC);
253 } else if (SP_IS_STAR(item)) {
254 tools_switch(dt, TOOLS_SHAPES_STAR);
255 } else if (SP_IS_SPIRAL(item)) {
256 tools_switch(dt, TOOLS_SHAPES_SPIRAL);
257 } else if (SP_IS_PATH(item)) {
258 if (cc_item_is_connector(item)) {
259 tools_switch(dt, TOOLS_CONNECTOR);
260 }
261 else {
262 tools_switch(dt, TOOLS_NODES);
263 }
264 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
265 tools_switch(dt, TOOLS_TEXT);
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 :