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 "sp-offset.h"
47 #include "message-context.h"
49 #include "tools-switch.h"
51 static char const *const tool_names[] = {
52 NULL,
53 "tools.select",
54 "tools.nodes",
55 "tools.shapes.rect",
56 "tools.shapes.arc",
57 "tools.shapes.star",
58 "tools.shapes.spiral",
59 "tools.freehand.pencil",
60 "tools.freehand.pen",
61 "tools.calligraphic",
62 "tools.text",
63 "tools.gradient",
64 "tools.zoom",
65 "tools.dropper",
66 "tools.connector",
67 NULL
68 };
70 static char const *const tool_ids[] = {
71 NULL,
72 "select",
73 "nodes",
74 "rect",
75 "arc",
76 "star",
77 "spiral",
78 "pencil",
79 "pen",
80 "calligraphic",
81 "text",
82 "gradient",
83 "zoom",
84 "dropper",
85 "connector",
86 NULL
87 };
89 static int
90 tools_id2num(char const *id)
91 {
92 int i = 1;
93 while (tool_ids[i]) {
94 if (strcmp(tool_ids[i], id) == 0)
95 return i;
96 else i++;
97 }
98 g_assert( 0 == TOOLS_INVALID );
99 return 0; //nothing found
100 }
102 int
103 tools_isactive(SPDesktop *dt, unsigned num)
104 {
105 g_assert( num < G_N_ELEMENTS(tool_ids) );
106 if (SP_IS_EVENT_CONTEXT(dt->event_context))
107 return (!strcmp(dt->event_context->prefs_repr->attribute("id"), tool_ids[num]));
108 else return FALSE;
109 }
111 int
112 tools_active(SPDesktop *dt)
113 {
114 return (tools_id2num(dt->event_context->prefs_repr->attribute("id")));
115 }
117 void
118 tools_switch(SPDesktop *dt, int num)
119 {
120 if (dt) {
121 dt->_tool_changed.emit(num);
122 }
124 switch (num) {
125 case TOOLS_SELECT:
126 dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
127 /* fixme: This is really ugly hack. We should bind and unbind class methods */
128 dt->activate_guides(true);
129 inkscape_eventcontext_set(sp_desktop_event_context(dt));
130 break;
131 case TOOLS_NODES:
132 dt->set_event_context(SP_TYPE_NODE_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 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."));
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_ARC:
144 dt->set_event_context(SP_TYPE_ARC_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 an ellipse. <b>Drag controls</b> to make an arc or segment. <b>Click</b> to select."));
148 break;
149 case TOOLS_SHAPES_STAR:
150 dt->set_event_context(SP_TYPE_STAR_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 a star. <b>Drag controls</b> to edit the star shape. <b>Click</b> to select."));
154 break;
155 case TOOLS_SHAPES_SPIRAL:
156 dt->set_event_context(SP_TYPE_SPIRAL_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 spiral. <b>Drag controls</b> to edit the spiral shape. <b>Click</b> to select."));
160 break;
161 case TOOLS_FREEHAND_PENCIL:
162 dt->set_event_context(SP_TYPE_PENCIL_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 freehand line. Start drawing with <b>Shift</b> to append to selected path."));
166 break;
167 case TOOLS_FREEHAND_PEN:
168 dt->set_event_context(SP_TYPE_PEN_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>Click</b> or <b>click and drag</b> to start a path; with <b>Shift</b> to append to selected path."));
172 break;
173 case TOOLS_CALLIGRAPHIC:
174 dt->set_event_context(SP_TYPE_DYNA_DRAW_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>Drag</b> to paint a calligraphic stroke. <b>Left</b>/<b>right</b> arrow keys adjust width, <b>up</b>/<b>down</b> adjust angle."));
178 break;
179 case TOOLS_TEXT:
180 dt->set_event_context(SP_TYPE_TEXT_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>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
184 break;
185 case TOOLS_GRADIENT:
186 dt->set_event_context(SP_TYPE_GRADIENT_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>Drag</b> or <b>double click</b> to create a gradient on selected objects, <b>drag handles</b> to adjust gradients."));
190 break;
191 case TOOLS_ZOOM:
192 dt->set_event_context(SP_TYPE_ZOOM_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>Click</b> or <b>drag around an area</b> to zoom in, <b>Shift+click</b> to zoom out."));
196 break;
197 case TOOLS_DROPPER:
198 dt->set_event_context(SP_TYPE_DROPPER_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> 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"));
202 break;
203 case TOOLS_CONNECTOR:
204 dt->set_event_context(SP_TYPE_CONNECTOR_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 and drag</b> between shapes to create a connector."));
208 break;
209 }
210 }
212 void
213 tools_switch_current(int num)
214 {
215 SPDesktop *dt = SP_ACTIVE_DESKTOP;
216 if (dt) tools_switch(dt, num);
217 }
219 void tools_switch_by_item(SPDesktop *dt, SPItem *item)
220 {
221 if (SP_IS_RECT(item)) {
222 tools_switch(dt, TOOLS_SHAPES_RECT);
223 } else if (SP_IS_GENERICELLIPSE(item)) {
224 tools_switch(dt, TOOLS_SHAPES_ARC);
225 } else if (SP_IS_STAR(item)) {
226 tools_switch(dt, TOOLS_SHAPES_STAR);
227 } else if (SP_IS_SPIRAL(item)) {
228 tools_switch(dt, TOOLS_SHAPES_SPIRAL);
229 } else if (SP_IS_PATH(item)) {
230 if (cc_item_is_connector(item)) {
231 tools_switch(dt, TOOLS_CONNECTOR);
232 }
233 else {
234 tools_switch(dt, TOOLS_NODES);
235 }
236 } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item)) {
237 tools_switch(dt, TOOLS_TEXT);
238 } else if (SP_IS_OFFSET(item)) {
239 tools_switch(dt, TOOLS_NODES);
240 }
241 }
243 /*
244 Local Variables:
245 mode:c++
246 c-file-style:"stroustrup"
247 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
248 indent-tabs-mode:nil
249 fill-column:99
250 End:
251 */
252 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :