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