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