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