]> git.lyx.org Git - lyx.git/blob - src/ToolbarBackend.C
263efeb1981581417661146c9c4bf5117bb8729d
[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 using std::endl;
26
27 ToolbarBackend toolbarbackend;
28
29 namespace {
30
31 enum _tooltags {
32         TO_ADD = 1,
33         TO_ENDTOOLBAR,
34         TO_SEPARATOR,
35         TO_LAYOUTS,
36         TO_NEWLINE,
37         TO_LAST
38 };
39
40
41 struct keyword_item toolTags[TO_LAST - 1] = {
42         { "end", TO_ENDTOOLBAR },
43         { "item", TO_ADD },
44         { "layouts", TO_LAYOUTS },
45         { "newline", TO_NEWLINE },
46         { "separator", TO_SEPARATOR }
47 };
48
49 } // end of anon namespace
50
51
52 ToolbarBackend::ToolbarBackend()
53 {
54 }
55
56
57 void ToolbarBackend::add(int action, string const & tooltip)
58 {
59         items.push_back(make_pair(action, tooltip));
60 }
61
62
63 void ToolbarBackend::read(LyXLex & lex)
64 {
65         //consistency check
66         if (compare_ascii_no_case(lex.getString(), "toolbar")) {
67                 lyxerr << "ToolbarBackend::read: ERROR wrong token:`"
68                        << lex.getString() << '\'' << endl;
69         }
70
71         bool quit = false;
72
73         lex.pushTable(toolTags, TO_LAST - 1);
74
75         if (lyxerr.debugging(Debug::PARSER))
76                 lex.printTable(lyxerr);
77
78         while (lex.isOK() && !quit) {
79                 switch (lex.lex()) {
80                 case TO_ADD:
81                         if (lex.next(true)) {
82                                 string const tooltip = _(lex.getString());
83                                 lex.next(true);
84                                 string const func = lex.getString();
85                                 lyxerr[Debug::PARSER]
86                                         << "ToolbarBackend::read TO_ADD func: `"
87                                         << func << '\'' << endl;
88                                 add(func, tooltip);
89                         }
90                         break;
91
92                 case TO_SEPARATOR:
93                         add(SEPARATOR);
94                         break;
95
96                 case TO_LAYOUTS:
97                         add(LAYOUTS);
98                         break;
99
100                 case TO_NEWLINE:
101                         add(NEWLINE);
102                         break;
103
104                 case TO_ENDTOOLBAR:
105                         quit = true;
106                         break;
107                 default:
108                         lex.printError("ToolbarBackend::read: "
109                                        "Unknown toolbar tag: `$$Token'");
110                         break;
111                 }
112         }
113         lex.popTable();
114 }
115
116
117 void ToolbarBackend::add(string const & func, string const & tooltip)
118 {
119         int const tf = lyxaction.LookupFunc(func);
120
121         if (tf == -1) {
122                 lyxerr << "ToolbarBackend::add: no LyX command called `"
123                        << func << "' exists!" << endl;
124         } else {
125                 add(tf, tooltip);
126         }
127 }
128
129
130 string const ToolbarBackend::getIcon(int action)
131 {
132         string fullname;
133         FuncRequest f = lyxaction.retrieveActionArg(action);
134
135         if (f.action == LFUN_INSERT_MATH && !f.argument.empty()) {
136                 fullname = find_xpm(f.argument.substr(1));
137         } else {
138                 string const name = lyxaction.getActionName(f.action);
139                 string xpm_name(name);
140
141                 if (!f.argument.empty())
142                         xpm_name = subst(name + ' ' + f.argument, ' ', '_');
143
144                 fullname = LibFileSearch("images", xpm_name, "xpm");
145
146                 if (fullname.empty()) {
147                         // try without the argument
148                         fullname = LibFileSearch("images", name, "xpm");
149                 }
150         }
151
152         if (!fullname.empty()) {
153                 lyxerr[Debug::GUI] << "Full icon name is `"
154                                    << fullname << '\'' << endl;
155                 return fullname;
156         }
157
158         return LibFileSearch("images", "unknown", "xpm");
159 }