]> git.lyx.org Git - features.git/blob - src/insets/InsetNomencl.cpp
Remove InsetCommand::mailer_name_.
[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 "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),
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)
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(to_utf8(cmd.argument()), p);
173                 if (p.getCmdName().empty()) {
174                         cur.noScreenUpdate();
175                         break;
176                 }
177                 setParams(p);
178                 break;
179         }
180
181         default:
182                 InsetCommand::doDispatch(cur, cmd);
183                 break;
184         }
185 }
186
187
188 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
189         FuncStatus & status) const
190 {
191         switch (cmd.action()) {
192
193         case LFUN_INSET_DIALOG_UPDATE:
194         case LFUN_INSET_MODIFY:
195                 status.setEnabled(true);
196                 return true;
197
198         default:
199                 return InsetCommand::getStatus(cur, cmd, status);
200         }
201 }
202
203
204 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
205 {
206         return docstring();
207 }
208
209
210 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
211 {
212         os << "<glossary>\n";
213         int newlines = 2;
214         InsetIterator it = inset_iterator_begin(buffer().inset());
215         while (it) {
216                 if (it->lyxCode() == NOMENCL_CODE) {
217                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
218                         ++it;
219                 } else if (!it->producesOutput()) {
220                         // Ignore contents of insets that are not in output
221                         size_t const depth = it.depth();
222                         ++it;
223                         while (it.depth() > depth)
224                                 ++it;
225                 } else {
226                         ++it;
227                 }
228         }
229         os << "</glossary>\n";
230         return newlines;
231 }
232
233
234 namespace {
235 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
236 {
237         // nomenclWidest() determines and returns the widest used
238         // nomenclature symbol in the document
239
240         int w = 0;
241         docstring symb;
242         InsetNomencl const * nomencl = 0;
243         ParagraphList::const_iterator it = buffer.paragraphs().begin();
244         ParagraphList::const_iterator end = buffer.paragraphs().end();
245
246         for (; it != end; ++it) {
247                 if (it->insetList().empty())
248                         continue;
249                 InsetList::const_iterator iit = it->insetList().begin();
250                 InsetList::const_iterator eend = it->insetList().end();
251                 for (; iit != eend; ++iit) {
252                         Inset * inset = iit->inset;
253                         if (inset->lyxCode() != NOMENCL_CODE)
254                                 continue;
255                         nomencl = static_cast<InsetNomencl const *>(inset);
256                         docstring const symbol =
257                                 nomencl->getParam("symbol");
258                         // This is only an approximation,
259                         // but the best we can get.
260                         int const wx = use_gui ?
261                                 theFontMetrics(Font()).width(symbol) :
262                                 symbol.size();
263                         if (wx > w) {
264                                 w = wx;
265                                 symb = symbol;
266                         }
267                 }
268         }
269         // return the widest (or an empty) string
270         if (symb.empty())
271                 return symb;
272
273         // we have to encode the string properly
274         docstring latex_symb;
275         for (size_t n = 0; n < symb.size(); ++n) {
276                 try {
277                         latex_symb += runparams.encoding->latexChar(symb[n]);
278                 } catch (EncodingException & /* e */) {
279                         if (runparams.dryrun) {
280                                 latex_symb += "<" + _("LyX Warning: ")
281                                            + _("uncodable character") + " '";
282                                 latex_symb += docstring(1, symb[n]);
283                                 latex_symb += "'>";
284                         }
285                 }
286         }
287         return latex_symb;
288 }
289 } // namespace anon
290
291
292 int InsetPrintNomencl::latex(odocstream & os, OutputParams const & runparams_in) const
293 {
294         OutputParams runparams = runparams_in;
295         int lines = 0;
296         if (getParam("set_width") == "auto") {
297                 docstring widest = nomenclWidest(buffer(), runparams);
298                 // Set the label width via nomencl's command \nomlabelwidth.
299                 // This must be output before the command \printnomenclature
300                 if (!widest.empty()) {
301                         os << "\\settowidth{\\nomlabelwidth}{"
302                            << widest
303                            << "}\n";
304                         ++lines;
305                 }
306         } else if (getParam("set_width") == "custom") {
307                 // custom length as optional arg of \printnomenclature
308                 string const width =
309                         Length(to_ascii(getParam("width"))).asLatexString();
310                 os << '\\'
311                    << from_ascii(getCmdName())
312                    << '['
313                    << from_ascii(width)
314                    << "]{}";
315                 return lines;
316         }
317         // output the command \printnomenclature
318         os << getCommand(runparams);
319         return lines;
320 }
321
322
323 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
324 {
325         features.require("nomencl");
326 }
327
328
329 InsetCode InsetPrintNomencl::lyxCode() const
330 {
331         return NOMENCL_PRINT_CODE;
332 }
333
334
335 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
336 {
337         return from_ascii("context-nomenclprint");
338 }
339
340
341 } // namespace lyx