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