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