]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Amend 7e121117bb3203
[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 "Cursor.h"
20 #include "DispatchResult.h"
21 #include "Font.h"
22 #include "Encoding.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Length.h"
30 #include "LyX.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
33 #include "sgml.h"
34 #include "texstream.h"
35 #include "TocBackend.h"
36
37 #include "frontends/FontMetrics.h"
38
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
43
44 using namespace std;
45 using namespace lyx::support;
46
47 namespace lyx {
48
49
50 /////////////////////////////////////////////////////////////////////
51 //
52 // InsetNomencl
53 //
54 /////////////////////////////////////////////////////////////////////
55
56 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
57         : InsetCommand(buf, p),
58           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
59 {}
60
61
62 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
63 {
64         static ParamInfo param_info_;
65         if (param_info_.empty()) {
66                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
67                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
68                                 ParamInfo::HANDLING_LATEXIFY);
69                 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
70                                 ParamInfo::HANDLING_LATEXIFY);
71                 param_info_.add("literal", ParamInfo::LYX_INTERNAL);
72         }
73         return param_info_;
74 }
75
76
77 docstring InsetNomencl::screenLabel() const
78 {
79         size_t const maxLabelChars = 25;
80         docstring label = _("Nom: ") + getParam("symbol");
81         support::truncateWithEllipsis(label, maxLabelChars);
82         return label;
83 }
84
85
86 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
87 {
88         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
89         tip += _("Description: ") + "\t"
90                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
91         if (!getParam("prefix").empty())
92                 tip += "\n" + _("Sorting: ") + getParam("prefix");
93         return tip;
94 }
95
96
97 int InsetNomencl::plaintext(odocstringstream & os,
98         OutputParams const &, size_t) const
99 {
100         docstring s = "[" + getParam("symbol") + ": " + getParam("description") + "]";
101         os << s;
102         return s.size();
103 }
104
105
106 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
107 {
108         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
109            << sgml::escapeString(getParam("symbol"))
110            << "</glossterm>";
111         return 0;
112 }
113
114
115 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
116 {
117         return docstring();
118 }
119
120
121 int InsetNomencl::docbookGlossary(odocstream & os) const
122 {
123         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
124            << "<glossterm>"
125            << sgml::escapeString(getParam("symbol"))
126            << "</glossterm>\n"
127            << "<glossdef><para>"
128            << sgml::escapeString(getParam("description"))
129            << "</para></glossdef>\n"
130            <<"</glossentry>\n";
131         return 4;
132 }
133
134
135 void InsetNomencl::validate(LaTeXFeatures & features) const
136 {
137         features.require("nomencl");
138         InsetCommand::validate(features);
139 }
140
141
142 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
143                                                         UpdateType, TocBackend & backend) const
144 {
145         docstring const str = getParam("symbol");
146         TocBuilder & b = backend.builder("nomencl");
147         b.pushItem(cpit, str, output_active);
148         b.pop();
149 }
150
151
152 /////////////////////////////////////////////////////////////////////
153 //
154 // InsetPrintNomencl
155 //
156 /////////////////////////////////////////////////////////////////////
157
158 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
159         : InsetCommand(buf, p)
160 {}
161
162
163 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
164 {
165         // The symbol width is set via nomencl's \nomlabelwidth in
166         // InsetPrintNomencl::latex and not as optional parameter of
167         // \printnomenclature
168         static ParamInfo param_info_;
169         if (param_info_.empty()) {
170                 // how is the width set?
171                 // values: none|auto|custom
172                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
173                 // custom width
174                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
175         }
176         return param_info_;
177 }
178
179
180 docstring InsetPrintNomencl::screenLabel() const
181 {
182         return _("Nomenclature");
183 }
184
185
186 struct NomenclEntry {
187         NomenclEntry() : par(0) {}
188         NomenclEntry(docstring s, docstring d, Paragraph const * p)
189           : symbol(s), desc(d), par(p)
190         {}
191
192         docstring symbol;
193         docstring desc;
194         Paragraph const * par;
195 };
196
197
198 typedef map<docstring, NomenclEntry > EntryMap;
199
200
201 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
202 {
203         shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
204
205         EntryMap entries;
206         Toc::const_iterator it = toc->begin();
207         Toc::const_iterator const en = toc->end();
208         for (; it != en; ++it) {
209                 DocIterator dit = it->dit();
210                 Paragraph const & par = dit.innerParagraph();
211                 Inset const * inset = par.getInset(dit.top().pos());
212                 if (!inset)
213                         return docstring();
214                 InsetCommand const * ic = inset->asInsetCommand();
215                 if (!ic)
216                         return docstring();
217
218                 // FIXME We need a link to the paragraph here, so we
219                 // need some kind of struct.
220                 docstring const symbol = ic->getParam("symbol");
221                 docstring const desc = ic->getParam("description");
222                 docstring const prefix = ic->getParam("prefix");
223                 docstring const sortas = prefix.empty() ? symbol : prefix;
224
225                 entries[sortas] = NomenclEntry(symbol, desc, &par);
226         }
227
228         if (entries.empty())
229                 return docstring();
230
231         // we'll use our own stream, because we are going to defer everything.
232         // that's how we deal with the fact that we're probably inside a standard
233         // paragraph, and we don't want to be.
234         odocstringstream ods;
235         XHTMLStream xs(ods);
236
237         InsetLayout const & il = getLayout();
238         string const & tag = il.htmltag();
239         docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
240                 op.local_font->language()->lang());
241
242         xs << html::StartTag("div", "class='nomencl'")
243            << html::StartTag(tag, "class='nomencl'")
244                  << toclabel
245                  << html::EndTag(tag)
246            << html::CR()
247            << html::StartTag("dl")
248            << html::CR();
249
250         EntryMap::const_iterator eit = entries.begin();
251         EntryMap::const_iterator const een = entries.end();
252         for (; eit != een; ++eit) {
253                 NomenclEntry const & ne = eit->second;
254                 string const parid = ne.par->magicLabel();
255                 xs << html::StartTag("dt")
256                    << html::StartTag("a", "href='#" + parid + "' class='nomencl'")
257                    << ne.symbol
258                    << html::EndTag("a")
259                    << html::EndTag("dt")
260                    << html::CR()
261                    << html::StartTag("dd")
262                    << ne.desc
263                    << html::EndTag("dd")
264                    << html::CR();
265         }
266
267         xs << html::EndTag("dl")
268            << html::CR()
269            << html::EndTag("div")
270            << html::CR();
271
272         return ods.str();
273 }
274
275
276 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
277 {
278         switch (cmd.action()) {
279
280         case LFUN_INSET_MODIFY: {
281                 InsetCommandParams p(NOMENCL_PRINT_CODE);
282                 // FIXME UNICODE
283                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
284                 if (p.getCmdName().empty()) {
285                         cur.noScreenUpdate();
286                         break;
287                 }
288
289                 cur.recordUndo();
290                 setParams(p);
291                 break;
292         }
293
294         default:
295                 InsetCommand::doDispatch(cur, cmd);
296                 break;
297         }
298 }
299
300
301 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
302         FuncStatus & status) const
303 {
304         switch (cmd.action()) {
305
306         case LFUN_INSET_DIALOG_UPDATE:
307         case LFUN_INSET_MODIFY:
308                 status.setEnabled(true);
309                 return true;
310
311         default:
312                 return InsetCommand::getStatus(cur, cmd, status);
313         }
314 }
315
316
317 // FIXME This should be changed to use the TOC. Perhaps
318 // that could be done when XHTML output is added.
319 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
320 {
321         os << "<glossary>\n";
322         int newlines = 2;
323         InsetIterator it = inset_iterator_begin(buffer().inset());
324         while (it) {
325                 if (it->lyxCode() == NOMENCL_CODE) {
326                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
327                         ++it;
328                 } else if (!it->producesOutput()) {
329                         // Ignore contents of insets that are not in output
330                         size_t const depth = it.depth();
331                         ++it;
332                         while (it.depth() > depth)
333                                 ++it;
334                 } else {
335                         ++it;
336                 }
337         }
338         os << "</glossary>\n";
339         return newlines;
340 }
341
342
343 namespace {
344 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
345 {
346         // nomenclWidest() determines and returns the widest used
347         // nomenclature symbol in the document
348
349         int w = 0;
350         docstring symb;
351         InsetNomencl const * nomencl = 0;
352         ParagraphList::const_iterator it = buffer.paragraphs().begin();
353         ParagraphList::const_iterator end = buffer.paragraphs().end();
354
355         for (; it != end; ++it) {
356                 if (it->insetList().empty())
357                         continue;
358                 InsetList::const_iterator iit = it->insetList().begin();
359                 InsetList::const_iterator eend = it->insetList().end();
360                 for (; iit != eend; ++iit) {
361                         Inset * inset = iit->inset;
362                         if (inset->lyxCode() != NOMENCL_CODE)
363                                 continue;
364                         nomencl = static_cast<InsetNomencl const *>(inset);
365                         docstring const symbol =
366                                 nomencl->getParam("symbol");
367                         // This is only an approximation,
368                         // but the best we can get.
369                         int const wx = use_gui ?
370                                 theFontMetrics(Font()).width(symbol) :
371                                 symbol.size();
372                         if (wx > w) {
373                                 w = wx;
374                                 symb = symbol;
375                         }
376                 }
377         }
378         // return the widest (or an empty) string
379         if (symb.empty())
380                 return symb;
381
382         // we have to encode the string properly
383         pair<docstring, docstring> latex_symb =
384                 runparams.encoding->latexString(symb, runparams.dryrun);
385         if (!latex_symb.second.empty())
386                 LYXERR0("Omitting uncodable characters '"
387                         << latex_symb.second
388                         << "' in nomencl widest string!");
389         return latex_symb.first;
390 }
391 } // namespace
392
393
394 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
395 {
396         OutputParams runparams = runparams_in;
397         if (getParam("set_width") == "auto") {
398                 docstring widest = nomenclWidest(buffer(), runparams);
399                 // Set the label width via nomencl's command \nomlabelwidth.
400                 // This must be output before the command \printnomenclature
401                 if (!widest.empty()) {
402                         os << "\\settowidth{\\nomlabelwidth}{"
403                            << widest
404                            << "}\n";
405                 }
406         } else if (getParam("set_width") == "custom") {
407                 // custom length as optional arg of \printnomenclature
408                 string const width =
409                         Length(to_ascii(getParam("width"))).asLatexString();
410                 os << '\\'
411                    << from_ascii(getCmdName())
412                    << '['
413                    << from_ascii(width)
414                    << "]"
415                    << termcmd;
416                 return;
417         }
418         // output the command \printnomenclature
419         os << getCommand(runparams);
420 }
421
422
423 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
424 {
425         features.require("nomencl");
426 }
427
428
429 InsetCode InsetPrintNomencl::lyxCode() const
430 {
431         return NOMENCL_PRINT_CODE;
432 }
433
434
435 string InsetPrintNomencl::contextMenuName() const
436 {
437         return "context-nomenclprint";
438 }
439
440
441 } // namespace lyx