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