Code

Get rid of the SP_DT_* macros which do nothing more than provide additional, confusin...
[inkscape.git] / src / tools-switch.cpp
1 /*
2  * Utility functions for switching tools (= contexts)
3  *
4  * Authors:
5  *   bulia byak <bulia@dr.com>
6  *
7  * Copyright (C) 2003 authors
8  *
9  * Released under GNU GPL, read the file 'COPYING' for more information
10  */
13 #ifdef HAVE_CONFIG_H
14 # include "config.h"
15 #endif
17 #include "inkscape-private.h"
18 #include "desktop.h"
19 #include "desktop-handles.h"
20 #include <glibmm/i18n.h>
22 #include <xml/repr.h>
24 #include "select-context.h"
25 #include "node-context.h"
26 #include "sp-path.h"
27 #include "rect-context.h"
28 #include "sp-rect.h"
29 #include "arc-context.h"
30 #include "sp-ellipse.h"
31 #include "star-context.h"
32 #include "sp-star.h"
33 #include "spiral-context.h"
34 #include "sp-spiral.h"
35 #include "dyna-draw-context.h"
36 #include "pen-context.h"
37 #include "pencil-context.h"
38 #include "text-context.h"
39 #include "sp-text.h"
40 #include "sp-flowtext.h"
41 #include "gradient-context.h"
42 #include "zoom-context.h"
43 #include "dropper-context.h"
44 #include "connector-context.h"
45 #include "message-context.h"
47 #include "tools-switch.h"
49 static char const *const tool_names[] = {
50     NULL,
51     "tools.select",
52     "tools.nodes",
53     "tools.shapes.rect",
54     "tools.shapes.arc",
55     "tools.shapes.star",
56     "tools.shapes.spiral",
57     "tools.freehand.pencil",
58     "tools.freehand.pen",
59     "tools.calligraphic",
60     "tools.text",
61     "tools.gradient",
62     "tools.zoom",
63     "tools.dropper",
64     "tools.connector",
65     NULL
66 };
68 static char const *const tool_ids[] = {
69     NULL,
70     "select",
71     "nodes",
72     "rect",
73     "arc",
74     "star",
75     "spiral",
76     "pencil",
77     "pen",
78     "calligraphic",
79     "text",
80     "gradient",
81     "zoom",
82     "dropper",
83     "connector",
84     NULL
85 };
87 static int
88 tools_id2num(char const *id)
89 {
90     int i = 1;
91     while (tool_ids[i]) {
92         if (strcmp(tool_ids[i], id) == 0)
93             return i;
94         else i++;
95     }
96     g_assert( 0 == TOOLS_INVALID );
97     return 0; //nothing found
98 }
100 int
101 tools_isactive(SPDesktop *dt, unsigned num)
103     g_assert( num < G_N_ELEMENTS(tool_ids) );
104     if (SP_IS_EVENT_CONTEXT(dt->event_context))
105         return (!strcmp(dt->event_context->prefs_repr->attribute("id"), tool_ids[num]));
106     else return FALSE;
109 int
110 tools_active(SPDesktop *dt)
112     return (tools_id2num(dt->event_context->prefs_repr->attribute("id")));
115 void
116 tools_switch(SPDesktop *dt, int num)
118     if (dt) {
119         dt->_tool_changed.emit(num);
120     }
122     switch (num) {
123         case TOOLS_SELECT:
124             dt->set_event_context(SP_TYPE_SELECT_CONTEXT, tool_names[num]);
125             /* fixme: This is really ugly hack. We should bind and unbind class methods */
126             dt->activate_guides(true);
127             inkscape_eventcontext_set(sp_desktop_event_context(dt));
128             break;
129         case TOOLS_NODES:
130             dt->set_event_context(SP_TYPE_NODE_CONTEXT, tool_names[num]);
131             dt->activate_guides(true);
132             inkscape_eventcontext_set(sp_desktop_event_context(dt));
133             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."));
134             break;
135         case TOOLS_SHAPES_RECT:
136             dt->set_event_context(SP_TYPE_RECT_CONTEXT, tool_names[num]);
137             dt->activate_guides(false);
138             inkscape_eventcontext_set(sp_desktop_event_context(dt));
139             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."));
140             break;
141         case TOOLS_SHAPES_ARC:
142             dt->set_event_context(SP_TYPE_ARC_CONTEXT, tool_names[num]);
143             dt->activate_guides(false);
144             inkscape_eventcontext_set(sp_desktop_event_context(dt));
145             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."));
146             break;
147         case TOOLS_SHAPES_STAR:
148             dt->set_event_context(SP_TYPE_STAR_CONTEXT, tool_names[num]);
149             dt->activate_guides(false);
150             inkscape_eventcontext_set(sp_desktop_event_context(dt));
151             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."));
152             break;
153         case TOOLS_SHAPES_SPIRAL:
154             dt->set_event_context(SP_TYPE_SPIRAL_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 spiral. <b>Drag controls</b> to edit the spiral shape. <b>Click</b> to select."));
158             break;
159         case TOOLS_FREEHAND_PENCIL:
160             dt->set_event_context(SP_TYPE_PENCIL_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 freehand line. Start drawing with <b>Shift</b> to append to selected path."));
164             break;
165         case TOOLS_FREEHAND_PEN:
166             dt->set_event_context(SP_TYPE_PEN_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>Click</b> or <b>click and drag</b> to start a path; with <b>Shift</b> to append to selected path."));
170             break;
171         case TOOLS_CALLIGRAPHIC:
172             dt->set_event_context(SP_TYPE_DYNA_DRAW_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 paint a calligraphic stroke. <b>Left</b>/<b>right</b> arrow keys adjust width, <b>up</b>/<b>down</b> adjust angle."));
176             break;
177         case TOOLS_TEXT:
178             dt->set_event_context(SP_TYPE_TEXT_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>Click</b> to select or create text, <b>drag</b> to create flowed text; then type."));
182             break;
183         case TOOLS_GRADIENT:
184             dt->set_event_context(SP_TYPE_GRADIENT_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> or <b>double click</b> to create a gradient on selected objects, <b>drag handles</b> to adjust gradients."));
188             break;
189         case TOOLS_ZOOM:
190             dt->set_event_context(SP_TYPE_ZOOM_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>drag around an area</b> to zoom in, <b>Shift+click</b> to zoom out."));
194             break;
195         case TOOLS_DROPPER:
196             dt->set_event_context(SP_TYPE_DROPPER_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> 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"));
200             break;
201         case TOOLS_CONNECTOR:
202             dt->set_event_context(SP_TYPE_CONNECTOR_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 and drag</b> between shapes to create a connector."));
206             break;
207     }
210 void
211 tools_switch_current(int num)
213     SPDesktop *dt = SP_ACTIVE_DESKTOP;
214     if (dt) tools_switch(dt, num);
217 void tools_switch_by_item(SPDesktop *dt, SPItem *item)
219     if (SP_IS_RECT(item)) {
220         tools_switch(dt, TOOLS_SHAPES_RECT);
221     } else if (SP_IS_GENERICELLIPSE(item)) {
222         tools_switch(dt, TOOLS_SHAPES_ARC);
223     } else if (SP_IS_STAR(item)) {
224         tools_switch(dt, TOOLS_SHAPES_STAR);
225     } else if (SP_IS_SPIRAL(item)) {
226         tools_switch(dt, TOOLS_SHAPES_SPIRAL);
227     } else if (SP_IS_PATH(item)) {
228         if (cc_item_is_connector(item)) {
229             tools_switch(dt, TOOLS_CONNECTOR);
230         }
231         else {
232             tools_switch(dt, TOOLS_NODES);
233         }
234     } else if (SP_IS_TEXT(item) || SP_IS_FLOWTEXT(item))  {
235         tools_switch(dt, TOOLS_TEXT);
236     }
239 /*
240   Local Variables:
241   mode:c++
242   c-file-style:"stroustrup"
243   c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
244   indent-tabs-mode:nil
245   fill-column:99
246   End:
247 */
248 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=99 :