]> git.lyx.org Git - lyx.git/blob - src/ToolbarBackend.C
8d91fca37647ff2857e27d577960b9d440763f4c
[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_arg = lex.getString();
93                                 lyxerr[Debug::PARSER]
94                                         << "ToolbarBackend::read TO_ADD func: `"
95                                         << func_arg << '\'' << endl;
96                                 // Split func_arg in function and arg.
97                                 string::size_type sp = func_arg.find(' ');
98                                 if (sp != string::npos) {
99
100                                         string const func =
101                                                 func_arg.substr(0, sp);
102                                         string const arg =
103                                              func_arg.substr(sp + 1,
104                                                              string::npos);
105
106                                         kb_action const tf =
107                                                 lyxaction.LookupFunc(func);
108
109                                         add(tb, FuncRequest(tf, arg), tooltip);
110                                 } else {
111                                         kb_action const tf = lyxaction.LookupFunc(func_arg);
112                                         add(tb, FuncRequest(tf), tooltip);
113
114                                 }
115
116                         }
117                         break;
118
119                 case TO_MINIBUFFER:
120                         add(tb, FuncRequest(kb_action(MINIBUFFER)));
121                         break;
122
123                 case TO_SEPARATOR:
124                         add(tb, FuncRequest(kb_action(SEPARATOR)));
125                         break;
126
127                 case TO_LAYOUTS:
128                         add(tb, FuncRequest(kb_action(LAYOUTS)));
129                         break;
130
131                 case TO_ENDTOOLBAR:
132                         quit = true;
133                         break;
134                 default:
135                         lex.printError("ToolbarBackend::read: "
136                                        "Unknown toolbar tag: `$$Token'");
137                         break;
138                 }
139         }
140
141         toolbars.push_back(tb);
142
143         lex.popTable();
144 }
145
146
147 void ToolbarBackend::readToolbars(LyXLex & lex)
148 {
149         //consistency check
150         if (compare_ascii_no_case(lex.getString(), "toolbars")) {
151                 lyxerr << "ToolbarBackend::read: ERROR wrong token:`"
152                        << lex.getString() << '\'' << endl;
153         }
154
155         lex.next(true);
156
157         while (lex.isOK()) {
158                 string name = lex.getString();
159                 lex.next(true);
160
161                 if (!compare_ascii_no_case(name, "end"))
162                         return;
163
164                 Toolbars::iterator tcit = toolbars.begin();
165                 Toolbars::iterator tend = toolbars.end();
166                 for (; tcit != tend; ++tcit) {
167                         if (tcit->name == name)
168                                 break;
169                 }
170
171                 if (tcit == tend) {
172                         lyxerr << "ToolbarBackend: undefined toolbar "
173                                 << name << endl;
174                         return;
175                 }
176
177                 tcit->flags = static_cast<Flags>(0);
178                 string flagstr = lex.getString();
179                 lex.next(true);
180                 vector<string> flags = getVectorFromString(flagstr);
181
182                 vector<string>::const_iterator cit = flags.begin();
183                 vector<string>::const_iterator end = flags.end();
184
185                 for (; cit != end; ++cit) {
186                         int flag = 0;
187                         if (!compare_ascii_no_case(*cit, "off"))
188                                 flag = OFF;
189                         else if (!compare_ascii_no_case(*cit, "on"))
190                                 flag = ON;
191                         else if (!compare_ascii_no_case(*cit, "math"))
192                                 flag = MATH;
193                         else if (!compare_ascii_no_case(*cit, "table"))
194                                 flag = TABLE;
195                         else if (!compare_ascii_no_case(*cit, "top"))
196                                 flag = TOP;
197                         else if (!compare_ascii_no_case(*cit, "bottom"))
198                                 flag = BOTTOM;
199                         else if (!compare_ascii_no_case(*cit, "left"))
200                                 flag = LEFT;
201                         else if (!compare_ascii_no_case(*cit, "right"))
202                                 flag = RIGHT;
203                         else {
204                                 lyxerr << "ToolbarBackend::read: unrecognised token:`"
205                                        << *cit << '\'' << endl;
206                         }
207                         tcit->flags = static_cast<Flags>(tcit->flags | flag);
208                 }
209
210                 usedtoolbars.push_back(*tcit);
211         }
212 }
213
214
215 void ToolbarBackend::add(Toolbar & tb,
216                          FuncRequest const & func, string const & tooltip)
217 {
218         tb.items.push_back(make_pair(func, tooltip));
219 }
220
221
222 string const ToolbarBackend::getIcon(FuncRequest const & f)
223 {
224         string fullname;
225
226         if (f.action == LFUN_INSERT_MATH) {
227                 if (!f.argument.empty())
228                         fullname = find_xpm(f.argument.substr(1));
229         } else if (f.action == LFUN_MATH_DELIM) {
230                 fullname = find_xpm(f.argument);
231         } else {
232                 string const name = lyxaction.getActionName(f.action);
233                 string xpm_name(name);
234
235                 if (!f.argument.empty())
236                         xpm_name = subst(name + ' ' + f.argument, ' ', '_');
237
238                 fullname = LibFileSearch("images", xpm_name, "xpm");
239
240                 if (fullname.empty()) {
241                         // try without the argument
242                         fullname = LibFileSearch("images", name, "xpm");
243                 }
244         }
245
246         if (!fullname.empty()) {
247                 lyxerr[Debug::GUI] << "Full icon name is `"
248                                    << fullname << '\'' << endl;
249                 return fullname;
250         }
251
252         lyxerr[Debug::GUI] << "Cannot find icon for command \""
253                            << lyxaction.getActionName(f.action)
254                            << '(' << f.argument << ")\"" << endl;
255         return LibFileSearch("images", "unknown", "xpm");
256 }