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