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