]> git.lyx.org Git - lyx.git/blob - src/ToolbarBackend.C
a1f2ffa637870d2af7bd8dca0cbf089ccafa4c12
[lyx.git] / src / ToolbarBackend.C
1 /**
2  * \file ToolbarBackend.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jean-Marc Lasgouttes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "ToolbarBackend.h"
15 #include "funcrequest.h"
16 #include "LyXAction.h"
17 #include "lyxlex.h"
18 #include "debug.h"
19 #include "gettext.h"
20
21 #include "support/lstrings.h"
22 #include "support/filetools.h"
23
24 #include "frontends/controllers/ControlMath.h"
25
26 using lyx::docstring;
27 using lyx::support::compare_ascii_no_case;
28 using lyx::support::getVectorFromString;
29 using lyx::support::libFileSearch;
30 using lyx::support::subst;
31
32 using std::endl;
33 using std::make_pair;
34 using std::string;
35 using std::vector;
36
37
38 ToolbarBackend toolbarbackend;
39
40 namespace {
41
42 enum tooltags {
43         TO_ADD = 1,
44         TO_ENDTOOLBAR,
45         TO_SEPARATOR,
46         TO_LAYOUTS,
47         TO_MINIBUFFER,
48         TO_LAST
49 };
50
51 struct keyword_item toolTags[TO_LAST - 1] = {
52         { "end", TO_ENDTOOLBAR },
53         { "item", TO_ADD },
54         { "layouts", TO_LAYOUTS },
55         { "minibuffer", TO_MINIBUFFER },
56         { "separator", TO_SEPARATOR }
57 };
58
59 } // end of anon namespace
60
61
62 ToolbarBackend::ToolbarBackend()
63 {
64 }
65
66
67 void ToolbarBackend::read(LyXLex & lex)
68 {
69         //consistency check
70         if (compare_ascii_no_case(lex.getString(), "toolbar")) {
71                 lyxerr << "ToolbarBackend::read: ERROR wrong token:`"
72                        << lex.getString() << '\'' << endl;
73         }
74
75         lex.next(true);
76
77         Toolbar tb;
78         tb.name = lex.getString();
79         lex.next(true);
80         if (!lex.isOK()) {
81                 lyxerr << "ToolbarBackend::read: Malformed toolbar "
82                         "description " <<  lex.getString() << endl;
83                 return;
84         }
85         tb.gui_name = lex.getString();
86
87         bool quit = false;
88
89         lex.pushTable(toolTags, TO_LAST - 1);
90
91         if (lyxerr.debugging(Debug::PARSER))
92                 lex.printTable(lyxerr);
93
94         while (lex.isOK() && !quit) {
95                 switch (lex.lex()) {
96                 case TO_ADD:
97                         if (lex.next(true)) {
98                                 docstring const tooltip = _(lex.getString());
99                                 lex.next(true);
100                                 string const func_arg = lex.getString();
101                                 lyxerr[Debug::PARSER]
102                                         << "ToolbarBackend::read TO_ADD func: `"
103                                         << func_arg << '\'' << endl;
104
105                                 FuncRequest func =
106                                         lyxaction.lookupFunc(func_arg);
107                                 add(tb, func, tooltip);
108                         }
109                         break;
110
111                 case TO_MINIBUFFER:
112                         add(tb, FuncRequest(kb_action(MINIBUFFER)));
113                         break;
114
115                 case TO_SEPARATOR:
116                         add(tb, FuncRequest(kb_action(SEPARATOR)));
117                         break;
118
119                 case TO_LAYOUTS:
120                         add(tb, FuncRequest(kb_action(LAYOUTS)));
121                         break;
122
123                 case TO_ENDTOOLBAR:
124                         quit = true;
125                         break;
126                 default:
127                         lex.printError("ToolbarBackend::read: "
128                                        "Unknown toolbar tag: `$$Token'");
129                         break;
130                 }
131         }
132
133         toolbars.push_back(tb);
134
135         lex.popTable();
136 }
137
138
139 void ToolbarBackend::readToolbars(LyXLex & lex)
140 {
141         //consistency check
142         if (compare_ascii_no_case(lex.getString(), "toolbars")) {
143                 lyxerr << "ToolbarBackend::read: ERROR wrong token:`"
144                        << lex.getString() << '\'' << endl;
145         }
146
147         lex.next(true);
148
149         while (lex.isOK()) {
150                 string name = lex.getString();
151                 lex.next(true);
152
153                 if (!compare_ascii_no_case(name, "end"))
154                         return;
155
156                 Toolbars::iterator tcit = toolbars.begin();
157                 Toolbars::iterator tend = toolbars.end();
158                 for (; tcit != tend; ++tcit) {
159                         if (tcit->name == name)
160                                 break;
161                 }
162
163                 if (tcit == tend) {
164                         lyxerr << "ToolbarBackend: undefined toolbar "
165                                 << name << endl;
166                         return;
167                 }
168
169                 tcit->flags = static_cast<Flags>(0);
170                 string flagstr = lex.getString();
171                 lex.next(true);
172                 vector<string> flags = getVectorFromString(flagstr);
173
174                 vector<string>::const_iterator cit = flags.begin();
175                 vector<string>::const_iterator end = flags.end();
176
177                 for (; cit != end; ++cit) {
178                         int flag = 0;
179                         if (!compare_ascii_no_case(*cit, "off"))
180                                 flag = OFF;
181                         else if (!compare_ascii_no_case(*cit, "on"))
182                                 flag = ON;
183                         else if (!compare_ascii_no_case(*cit, "math"))
184                                 flag = MATH;
185                         else if (!compare_ascii_no_case(*cit, "table"))
186                                 flag = TABLE;
187                         else if (!compare_ascii_no_case(*cit, "top"))
188                                 flag = TOP;
189                         else if (!compare_ascii_no_case(*cit, "bottom"))
190                                 flag = BOTTOM;
191                         else if (!compare_ascii_no_case(*cit, "left"))
192                                 flag = LEFT;
193                         else if (!compare_ascii_no_case(*cit, "right"))
194                                 flag = RIGHT;
195                         else {
196                                 lyxerr << "ToolbarBackend::read: unrecognised token:`"
197                                        << *cit << '\'' << endl;
198                         }
199                         tcit->flags = static_cast<Flags>(tcit->flags | flag);
200                 }
201
202                 usedtoolbars.push_back(*tcit);
203         }
204 }
205
206
207 void ToolbarBackend::add(Toolbar & tb,
208                          FuncRequest const & func, docstring const & tooltip)
209 {
210         tb.items.push_back(make_pair(func, tooltip));
211         tb.items.back().first.origin = FuncRequest::UI;
212 }
213
214
215 string const ToolbarBackend::getIcon(FuncRequest const & f)
216 {
217         using lyx::frontend::find_xpm;
218
219         string fullname;
220
221         switch (f.action) {
222         case LFUN_MATH_INSERT:
223                 if (!f.argument().empty())
224                         fullname = find_xpm(lyx::to_utf8(f.argument()).substr(1));
225                 break;
226         case LFUN_MATH_DELIM:
227         case LFUN_MATH_BIGDELIM:
228                 fullname = find_xpm(lyx::to_utf8(f.argument()));
229                 break;
230         default:
231                 string const name = lyxaction.getActionName(f.action);
232                 string xpm_name(name);
233
234                 if (!f.argument().empty())
235                         xpm_name = subst(name + ' ' + lyx::to_utf8(f.argument()), ' ', '_');
236
237                 fullname = libFileSearch("images", xpm_name, "xpm");
238
239                 if (fullname.empty()) {
240                         // try without the argument
241                         fullname = libFileSearch("images", name, "xpm");
242                 }
243         }
244
245         if (!fullname.empty()) {
246                 lyxerr[Debug::GUI] << "Full icon name is `"
247                                    << fullname << '\'' << endl;
248                 return fullname;
249         }
250
251         lyxerr[Debug::GUI] << "Cannot find icon for command \""
252                            << lyxaction.getActionName(f.action)
253                            << '(' << lyx::to_utf8(f.argument()) << ")\"" << endl;
254         return libFileSearch("images", "unknown", "xpm");
255 }