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