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