]> git.lyx.org Git - features.git/blob - src/insets/InsetNomencl.cpp
generalize special handling of command inset parameters on LaTeX output (escaping...
[features.git] / src / insets / InsetNomencl.cpp
1 /**
2  * \file InsetNomencl.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Lars Gullik Bjønnes
7  * \author O. U. Baran
8  * \author Uwe Stöhr
9  * \author Jürgen Spitzmüller
10  *
11  * Full author contact details are available in file CREDITS.
12  */
13 #include <config.h>
14
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
17
18 #include "Buffer.h"
19 #include "DispatchResult.h"
20 #include "Font.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetIterator.h"
24 #include "InsetList.h"
25 #include "LaTeXFeatures.h"
26 #include "Length.h"
27 #include "LyX.h"
28 #include "MetricsInfo.h"
29 #include "output_xhtml.h"
30 #include "sgml.h"
31
32 #include "frontends/FontMetrics.h"
33
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lstrings.h"
37
38 using namespace std;
39 using namespace lyx::support;
40
41 namespace lyx {
42
43
44 /////////////////////////////////////////////////////////////////////
45 //
46 // InsetNomencl
47 //
48 /////////////////////////////////////////////////////////////////////
49
50 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
51         : InsetCommand(buf, p, "nomenclature"),
52           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
53 {}
54
55
56 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
57 {
58         static ParamInfo param_info_;
59         if (param_info_.empty()) {
60                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
61                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
62                 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
63         }
64         return param_info_;
65 }
66
67
68 docstring InsetNomencl::screenLabel() const
69 {
70         size_t const maxLabelChars = 25;
71
72         docstring label = _("Nom: ") + getParam("symbol");
73         if (label.size() > maxLabelChars) {
74                 label.erase(maxLabelChars - 3);
75                 label += "...";
76         }
77         return label;
78 }
79
80
81 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
82 {
83         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
84         tip += _("Description: ") + "\t"
85                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
86         if (!getParam("prefix").empty())
87                 tip += "\n" + _("Sorting: ") + getParam("prefix");
88         return tip;
89 }
90
91
92
93 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
94 {
95         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
96            << sgml::escapeString(getParam("symbol"))
97            << "</glossterm>";
98         return 0;
99 }
100
101
102 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
103 {
104         return docstring();
105 }
106
107
108 int InsetNomencl::docbookGlossary(odocstream & os) const
109 {
110         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
111            << "<glossterm>"
112            << sgml::escapeString(getParam("symbol"))
113            << "</glossterm>\n"
114            << "<glossdef><para>"
115            << sgml::escapeString(getParam("description"))
116            << "</para></glossdef>\n"
117            <<"</glossentry>\n";
118         return 4;
119 }
120
121
122 void InsetNomencl::validate(LaTeXFeatures & features) const
123 {
124         features.require("nomencl");
125 }
126
127
128 /////////////////////////////////////////////////////////////////////
129 //
130 // InsetPrintNomencl
131 //
132 /////////////////////////////////////////////////////////////////////
133
134 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
135         : InsetCommand(buf, p, "nomencl_print")
136 {}
137
138
139 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
140 {
141         // The symbol width is set via nomencl's \nomlabelwidth in 
142         // InsetPrintNomencl::latex and not as optional parameter of
143         // \printnomenclature
144         static ParamInfo param_info_;
145         if (param_info_.empty()) {
146                 // how is the width set?
147                 // values: none|auto|custom
148                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
149                 // custom width
150                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
151         }
152         return param_info_;
153 }
154
155
156 docstring InsetPrintNomencl::screenLabel() const
157 {
158         return _("Nomenclature");
159 }
160
161
162 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
163 {
164         switch (cmd.action) {
165
166         case LFUN_INSET_MODIFY: {
167                 InsetCommandParams p(NOMENCL_PRINT_CODE);
168                 // FIXME UNICODE
169                 InsetCommand::string2params("nomencl_print",
170                         to_utf8(cmd.argument()), p);
171                 if (p.getCmdName().empty()) {
172                         cur.noUpdate();
173                         break;
174                 }
175                 setParams(p);
176                 break;
177         }
178
179         default:
180                 InsetCommand::doDispatch(cur, cmd);
181                 break;
182         }
183 }
184
185
186 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
187         FuncStatus & status) const
188 {
189         switch (cmd.action) {
190
191         case LFUN_INSET_DIALOG_UPDATE:
192         case LFUN_INSET_MODIFY:
193                 status.setEnabled(true);
194                 return true;
195
196         default:
197                 return InsetCommand::getStatus(cur, cmd, status);
198         }
199 }
200
201
202 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
203 {
204         return docstring();
205 }
206
207
208 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
209 {
210         os << "<glossary>\n";
211         int newlines = 2;
212         InsetIterator it = inset_iterator_begin(buffer().inset());
213         while (it) {
214                 if (it->lyxCode() == NOMENCL_CODE) {
215                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
216                         ++it;
217                 } else if (!it->producesOutput()) {
218                         // Ignore contents of insets that are not in output
219                         size_t const depth = it.depth();
220                         ++it;
221                         while (it.depth() > depth)
222                                 ++it;
223                 } else {
224                         ++it;
225                 }
226         }
227         os << "</glossary>\n";
228         return newlines;
229 }
230
231
232 namespace {
233 docstring nomenclWidest(Buffer const & buffer)
234 {
235         // nomenclWidest() determines and returns the widest used
236         // nomenclature symbol in the document
237
238         int w = 0;
239         docstring symb;
240         InsetNomencl const * nomencl = 0;
241         ParagraphList::const_iterator it = buffer.paragraphs().begin();
242         ParagraphList::const_iterator end = buffer.paragraphs().end();
243
244         for (; it != end; ++it) {
245                 if (it->insetList().empty())
246                         continue;
247                 InsetList::const_iterator iit = it->insetList().begin();
248                 InsetList::const_iterator eend = it->insetList().end();
249                 for (; iit != eend; ++iit) {
250                         Inset * inset = iit->inset;
251                         if (inset->lyxCode() != NOMENCL_CODE)
252                                 continue;
253                         nomencl = static_cast<InsetNomencl const *>(inset);
254                         docstring const symbol =
255                                 nomencl->getParam("symbol");
256                         // This is only an approximation,
257                         // but the best we can get.
258                         int const wx = use_gui ?
259                                 theFontMetrics(Font()).width(symbol) :
260                                 symbol.size();
261                         if (wx > w) {
262                                 w = wx;
263                                 symb = symbol;
264                         }
265                 }
266         }
267         // return the widest (or an empty) string
268         return symb;
269 }
270 }
271
272
273 int InsetPrintNomencl::latex(odocstream & os, OutputParams const & runparams_in) const
274 {
275         int lines = 0;
276         if (getParam("set_width") == "auto") {
277                 docstring widest = nomenclWidest(buffer());
278                 // Set the label width via nomencl's command \nomlabelwidth.
279                 // This must be output before the command \printnomenclature
280                 if (!widest.empty()) {
281                         os << "\\settowidth{\\nomlabelwidth}{"
282                            << widest
283                            << "}\n";
284                         ++lines;
285                 }
286         } else if (getParam("set_width") == "custom") {
287                 // custom length as optional arg of \printnomenclature
288                 string const width =
289                         Length(to_ascii(getParam("width"))).asLatexString();
290                 os << '\\'
291                    << from_ascii(getCmdName())
292                    << '['
293                    << from_ascii(width)
294                    << "]{}";
295                 return lines;
296         }
297         // output the command \printnomenclature
298         OutputParams runparams = runparams_in;
299         os << getCommand(runparams);
300         return lines;
301 }
302
303
304 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
305 {
306         features.require("nomencl");
307 }
308
309
310 InsetCode InsetPrintNomencl::lyxCode() const
311 {
312         return NOMENCL_PRINT_CODE;
313 }
314
315
316 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
317 {
318         return from_ascii("context-nomenclprint");
319 }
320
321
322 } // namespace lyx