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