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