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