2 * \file InsetNomencl.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjønnes
9 * \author Jürgen Spitzmüller
11 * Full author contact details are available in file CREDITS.
15 #include "InsetNomencl.h"
16 #include "InsetNote.h"
20 #include "DispatchResult.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetIterator.h"
26 #include "InsetList.h"
28 #include "LaTeXFeatures.h"
31 #include "OutputParams.h"
32 #include "output_xhtml.h"
34 #include "texstream.h"
35 #include "TocBackend.h"
37 #include "frontends/FontMetrics.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lstrings.h"
45 using namespace lyx::support;
50 /////////////////////////////////////////////////////////////////////
54 /////////////////////////////////////////////////////////////////////
56 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
57 : InsetCommand(buf, p),
58 nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
62 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
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);
76 docstring InsetNomencl::screenLabel() const
78 size_t const maxLabelChars = 25;
79 docstring label = _("Nom: ") + getParam("symbol");
80 support::truncateWithEllipsis(label, maxLabelChars);
85 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
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");
97 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
99 os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
100 << sgml::escapeString(getParam("symbol"))
106 docstring InsetNomencl::xhtml(XHTMLStream &, OutputParams const &) const
112 int InsetNomencl::docbookGlossary(odocstream & os) const
114 os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
116 << sgml::escapeString(getParam("symbol"))
118 << "<glossdef><para>"
119 << sgml::escapeString(getParam("description"))
120 << "</para></glossdef>\n"
126 void InsetNomencl::validate(LaTeXFeatures & features) const
128 features.require("nomencl");
132 void InsetNomencl::addToToc(DocIterator const & cpit, bool output_active,
135 docstring const str = getParam("symbol");
136 buffer().tocBackend().toc("nomencl")->push_back(TocItem(cpit, 0, str, output_active));
140 /////////////////////////////////////////////////////////////////////
144 /////////////////////////////////////////////////////////////////////
146 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
147 : InsetCommand(buf, p)
151 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
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);
162 param_info_.add("width", ParamInfo::LYX_INTERNAL);
168 docstring InsetPrintNomencl::screenLabel() const
170 return _("Nomenclature");
174 struct NomenclEntry {
175 NomenclEntry() : par(0) {}
176 NomenclEntry(docstring s, docstring d, Paragraph const * p)
177 : symbol(s), desc(d), par(p)
182 Paragraph const * par;
186 typedef map<docstring, NomenclEntry > EntryMap;
189 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const & op) const
191 shared_ptr<Toc const> toc = buffer().tocBackend().toc("nomencl");
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());
202 InsetCommand const * ic = inset->asInsetCommand();
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;
213 entries[sortas] = NomenclEntry(symbol, desc, &par);
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;
225 InsetLayout const & il = getLayout();
226 string const & tag = il.htmltag();
227 docstring toclabel = translateIfPossible(from_ascii("Nomenclature"),
228 op.local_font->language()->lang());
230 xs << html::StartTag("div", "class='nomencl'")
231 << html::StartTag(tag, "class='nomencl'")
235 << html::StartTag("dl")
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'")
247 << html::EndTag("dt")
249 << html::StartTag("dd")
251 << html::EndTag("dd")
255 xs << html::EndTag("dl")
257 << html::EndTag("div")
264 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
266 switch (cmd.action()) {
268 case LFUN_INSET_MODIFY: {
269 InsetCommandParams p(NOMENCL_PRINT_CODE);
271 InsetCommand::string2params(to_utf8(cmd.argument()), p);
272 if (p.getCmdName().empty()) {
273 cur.noScreenUpdate();
283 InsetCommand::doDispatch(cur, cmd);
289 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
290 FuncStatus & status) const
292 switch (cmd.action()) {
294 case LFUN_INSET_DIALOG_UPDATE:
295 case LFUN_INSET_MODIFY:
296 status.setEnabled(true);
300 return InsetCommand::getStatus(cur, cmd, status);
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
309 os << "<glossary>\n";
311 InsetIterator it = inset_iterator_begin(buffer().inset());
313 if (it->lyxCode() == NOMENCL_CODE) {
314 newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
316 } else if (!it->producesOutput()) {
317 // Ignore contents of insets that are not in output
318 size_t const depth = it.depth();
320 while (it.depth() > depth)
326 os << "</glossary>\n";
332 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
334 // nomenclWidest() determines and returns the widest used
335 // nomenclature symbol in the document
339 InsetNomencl const * nomencl = 0;
340 ParagraphList::const_iterator it = buffer.paragraphs().begin();
341 ParagraphList::const_iterator end = buffer.paragraphs().end();
343 for (; it != end; ++it) {
344 if (it->insetList().empty())
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)
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) :
366 // return the widest (or an empty) string
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 '"
376 << "' in nomencl widest string!");
377 return latex_symb.first;
382 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
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}{"
394 } else if (getParam("set_width") == "custom") {
395 // custom length as optional arg of \printnomenclature
397 Length(to_ascii(getParam("width"))).asLatexString();
399 << from_ascii(getCmdName())
405 // output the command \printnomenclature
406 os << getCommand(runparams);
410 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
412 features.require("nomencl");
416 InsetCode InsetPrintNomencl::lyxCode() const
418 return NOMENCL_PRINT_CODE;
422 string InsetPrintNomencl::contextMenuName() const
424 return "context-nomenclprint";