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