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