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