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