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