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