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