Code

da03ab7bf2b5be53bc197648c98583256d5b6873
[inkscape.git] / src / tools-switch.cpp
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 "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 char const *const tool_ids[] = {
85     NULL,
86     "select",
87     "nodes",
88     "tweak",
89     "rect",
90     "3dbox",
91     "arc",
92     "star",
93     "spiral",
94     "pencil",
95     "pen",
96     "calligraphic",
97     "text",
98     "gradient",
99     "zoom",
100     "dropper",
101     "connector",
102     "paintbucket",
103     "eraser",
104     "lpetool",
105     NULL
106 };
108 static int
109 tools_id2num(char const *id)
111     int i = 1;
112     while (tool_ids[i]) {
113         if (strcmp(tool_ids[i], id) == 0)
114             return i;
115         else i++;
116     }
117     g_assert( 0 == TOOLS_INVALID );
118     return 0; //nothing found
121 int
122 tools_isactive(SPDesktop *dt, unsigned num)
124     g_assert( num < G_N_ELEMENTS(tool_ids) );
125     if (SP_IS_EVENT_CONTEXT(dt->event_context))
126         return (!strcmp(dt->event_context->prefs_repr->attribute("id"), tool_ids[num]));
127     else return FALSE;
130 int
131 tools_active(SPDesktop *dt)
133     return (tools_id2num(dt->event_context->prefs_repr->attribute("id")));
136 void
137 tools_switch(SPDesktop *dt, int num)
139     if (dt) {
140         dt->_tool_changed.emit(num);
141     }
143     switch (num) {
144         case TOOLS_SELECT:
145             dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
146             /* fixme: This is really ugly hack. We should bind and unbind class methods */
147             dt->activate_guides(true);
148             inkscape_eventcontext_set(sp_desktop_event_context(dt));
149             break;
150         case TOOLS_NODES:
151             dt->set_event_context(SP_TYPE_NODE_CONTEXT, tool_names[num]);
152             dt->activate_guides(true);
153             inkscape_eventcontext_set(sp_desktop_event_context(dt));
154             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."));
155             break;
156         case TOOLS_TWEAK:
157             dt->set_event_context(SP_TYPE_TWEAK_CONTEXT, tool_names[num]);
158             dt->activate_guides(true);
159             inkscape_eventcontext_set(sp_desktop_event_context(dt));
160             dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("To tweak a path by pushing, select it and drag over it."));
161             break;
162         case TOOLS_SHAPES_RECT:
163             dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
164             dt->activate_guides(false);
165             inkscape_eventcontext_set(sp_desktop_event_context(dt));
166             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."));
167             break;
168         case TOOLS_SHAPES_3DBOX:
169             dt->set_event_context(SP_TYPE_BOX3D_CONTEXT, tool_names[num]);
170             dt->activate_guides(false);
171             inkscape_eventcontext_set(sp_desktop_event_context(dt));
172             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)."));
173             break;
174         case TOOLS_SHAPES_ARC:
175             dt->set_event_context(SP_TYPE_ARC_CONTEXT, tool_names[num]);
176             dt->activate_guides(false);
177             inkscape_eventcontext_set(sp_desktop_event_context(dt));
178             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."));
179             break;
180         case TOOLS_SHAPES_STAR:
181             dt->set_event_context(SP_TYPE_STAR_CONTEXT, tool_names[num]);
182             dt->activate_guides(false);
183             inkscape_eventcontext_set(sp_desktop_event_context(dt));
184             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."));
185             break;
186         case TOOLS_SHAPES_SPIRAL:
187             dt->set_event_context(SP_TYPE_SPIRAL_CONTEXT, tool_names[num]);
188             dt->activate_guides(false);
189             inkscape_eventcontext_set(sp_desktop_event_context(dt));
190             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."));
191             break;
192         case TOOLS_FREEHAND_PENCIL:
193             dt->set_event_context(SP_TYPE_PENCIL_CONTEXT, tool_names[num]);
194             dt->activate_guides(false);
195             inkscape_eventcontext_set(sp_desktop_event_context(dt));
196             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."));
197             break;
198         case TOOLS_FREEHAND_PEN:
199             dt->set_event_context(SP_TYPE_PEN_CONTEXT, tool_names[num]);
200             dt->activate_guides(false);
201             inkscape_eventcontext_set(sp_desktop_event_context(dt));
202             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."));
203             break;
204         case TOOLS_CALLIGRAPHIC:
205             dt->set_event_context(SP_TYPE_DYNA_DRAW_CONTEXT, tool_names[num]);
206             dt->activate_guides(false);
207             inkscape_eventcontext_set(sp_desktop_event_context(dt));
208             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)."));
209             break;
210         case TOOLS_TEXT:
211             dt->set_event_context(SP_TYPE_TEXT_CONTEXT, tool_names[num]);
212             dt->activate_guides(false);
213             inkscape_eventcontext_set(sp_desktop_event_context(dt));
214             dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
215             break;
216         case TOOLS_GRADIENT:
217             dt->set_event_context(SP_TYPE_GRADIENT_CONTEXT, tool_names[num]);
218             dt->activate_guides(false);
219             inkscape_eventcontext_set(sp_desktop_event_context(dt));
220             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."));
221             break;
222         case TOOLS_ZOOM:
223             dt->set_event_context(SP_TYPE_ZOOM_CONTEXT, tool_names[num]);
224             dt->activate_guides(false);
225             inkscape_eventcontext_set(sp_desktop_event_context(dt));
226             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."));
227             break;
228         case TOOLS_DROPPER:
229             dt->set_event_context(SP_TYPE_DROPPER_CONTEXT, tool_names[num]);
230             dt->activate_guides(false);
231             inkscape_eventcontext_set(sp_desktop_event_context(dt));
232             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"));
233             break;
234         case TOOLS_CONNECTOR:
235             dt->set_event_context(SP_TYPE_CONNECTOR_CONTEXT, tool_names[num]);
236             dt->activate_guides(false);
237             inkscape_eventcontext_set(sp_desktop_event_context(dt));
238             dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Click and drag</b> between shapes to create a connector."));
239             break;
240         case TOOLS_PAINTBUCKET:
241             dt->set_event_context(SP_TYPE_FLOOD_CONTEXT, tool_names[num]);
242             dt->activate_guides(false);
243             inkscape_eventcontext_set(sp_desktop_event_context(dt));
244             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."));
245             break;
246         case TOOLS_ERASER:
247             dt->set_event_context(SP_TYPE_ERASER_CONTEXT, tool_names[num]);
248             dt->activate_guides(false);
249             inkscape_eventcontext_set(sp_desktop_event_context(dt));
250             dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("<b>Drag</b> to erase."));
251             break;
252         case TOOLS_LPETOOL:
253             dt->set_event_context(SP_TYPE_LPETOOL_CONTEXT, tool_names[num]);
254             dt->activate_guides(false);
255             inkscape_eventcontext_set(sp_desktop_event_context(dt));
256             dt->tipsMessageContext()->set(Inkscape::NORMAL_MESSAGE, _("Choose a subtool from the toolbar"));
257             break;
258     }
261 void tools_switch_by_item(SPDesktop *dt, SPItem *item)
263     if (SP_IS_RECT(item)) {
264         tools_switch(dt, TOOLS_SHAPES_RECT);
265     } else if (SP_IS_BOX3D(item)) {
266         tools_switch(dt, TOOLS_SHAPES_3DBOX);
267     } else if (SP_IS_GENERICELLIPSE(item)) {
268         tools_switch(dt, TOOLS_SHAPES_ARC);
269     } else if (SP_IS_STAR(item)) {
270         tools_switch(dt, TOOLS_SHAPES_STAR);
271     } else if (SP_IS_SPIRAL(item)) {
272         tools_switch(dt, TOOLS_SHAPES_SPIRAL);
273     } else if (SP_IS_PATH(item)) {
274         if (cc_item_is_connector(item)) {
275             tools_switch(dt, TOOLS_CONNECTOR);
276         }
277         else {
278             tools_switch(dt, TOOLS_NODES);
279         }
280     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))  {
281         tools_switch(dt, TOOLS_TEXT);
282     } else if (SP_IS_OFFSET(item))  {
283         tools_switch(dt, TOOLS_NODES);
284     }
287 /*
288   Local Variables:
289   mode:c++
290   c-file-style:"stroustrup"
291   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
292   indent-tabs-mode:nil
293   fill-column:99
294   End:
295 */
296 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :