]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
Allow LyX format to be written to View>Source window.
[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 "LaTeXFeatures.h"
28 #include "Length.h"
29 #include "LyX.h"
30 #include "OutputParams.h"
31 #include "output_xhtml.h"
32 #include "sgml.h"
33
34 #include "frontends/FontMetrics.h"
35
36 #include "support/docstream.h"
37 #include "support/gettext.h"
38 #include "support/lstrings.h"
39
40 using namespace std;
41 using namespace lyx::support;
42
43 namespace lyx {
44
45
46 /////////////////////////////////////////////////////////////////////
47 //
48 // InsetNomencl
49 //
50 /////////////////////////////////////////////////////////////////////
51
52 InsetNomencl::InsetNomencl(Buffer * buf, InsetCommandParams const & p)
53         : InsetCommand(buf, p),
54           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
55 {}
56
57
58 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
59 {
60         static ParamInfo param_info_;
61         if (param_info_.empty()) {
62                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
63                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED,
64                                 ParamInfo::HANDLING_LATEXIFY);
65                 param_info_.add("description", ParamInfo::LATEX_REQUIRED,
66                                 ParamInfo::HANDLING_LATEXIFY);
67         }
68         return param_info_;
69 }
70
71
72 docstring InsetNomencl::screenLabel() const
73 {
74         size_t const maxLabelChars = 25;
75
76         docstring label = _("Nom: ") + getParam("symbol");
77         if (label.size() > maxLabelChars) {
78                 label.erase(maxLabelChars - 3);
79                 label += "...";
80         }
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 /////////////////////////////////////////////////////////////////////
133 //
134 // InsetPrintNomencl
135 //
136 /////////////////////////////////////////////////////////////////////
137
138 InsetPrintNomencl::InsetPrintNomencl(Buffer * buf, InsetCommandParams const & p)
139         : InsetCommand(buf, p)
140 {}
141
142
143 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
144 {
145         // The symbol width is set via nomencl's \nomlabelwidth in 
146         // InsetPrintNomencl::latex and not as optional parameter of
147         // \printnomenclature
148         static ParamInfo param_info_;
149         if (param_info_.empty()) {
150                 // how is the width set?
151                 // values: none|auto|custom
152                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
153                 // custom width
154                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
155         }
156         return param_info_;
157 }
158
159
160 docstring InsetPrintNomencl::screenLabel() const
161 {
162         return _("Nomenclature");
163 }
164
165
166 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
167 {
168         switch (cmd.action()) {
169
170         case LFUN_INSET_MODIFY: {
171                 InsetCommandParams p(NOMENCL_PRINT_CODE);
172                 // FIXME UNICODE
173                 InsetCommand::string2params(to_utf8(cmd.argument()), p);
174                 if (p.getCmdName().empty()) {
175                         cur.noScreenUpdate();
176                         break;
177                 }
178
179                 cur.recordUndo();
180                 setParams(p);
181                 break;
182         }
183
184         default:
185                 InsetCommand::doDispatch(cur, cmd);
186                 break;
187         }
188 }
189
190
191 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
192         FuncStatus & status) const
193 {
194         switch (cmd.action()) {
195
196         case LFUN_INSET_DIALOG_UPDATE:
197         case LFUN_INSET_MODIFY:
198                 status.setEnabled(true);
199                 return true;
200
201         default:
202                 return InsetCommand::getStatus(cur, cmd, status);
203         }
204 }
205
206
207 docstring InsetPrintNomencl::xhtml(XHTMLStream &, OutputParams const &) const
208 {
209         return docstring();
210 }
211
212
213 // FIXME This should be changed to use the TOC. Perhaps
214 // that could be done when XHTML output is added.
215 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
216 {
217         os << "<glossary>\n";
218         int newlines = 2;
219         InsetIterator it = inset_iterator_begin(buffer().inset());
220         while (it) {
221                 if (it->lyxCode() == NOMENCL_CODE) {
222                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
223                         ++it;
224                 } else if (!it->producesOutput()) {
225                         // Ignore contents of insets that are not in output
226                         size_t const depth = it.depth();
227                         ++it;
228                         while (it.depth() > depth)
229                                 ++it;
230                 } else {
231                         ++it;
232                 }
233         }
234         os << "</glossary>\n";
235         return newlines;
236 }
237
238
239 namespace {
240 docstring nomenclWidest(Buffer const & buffer, OutputParams const & runparams)
241 {
242         // nomenclWidest() determines and returns the widest used
243         // nomenclature symbol in the document
244
245         int w = 0;
246         docstring symb;
247         InsetNomencl const * nomencl = 0;
248         ParagraphList::const_iterator it = buffer.paragraphs().begin();
249         ParagraphList::const_iterator end = buffer.paragraphs().end();
250
251         for (; it != end; ++it) {
252                 if (it->insetList().empty())
253                         continue;
254                 InsetList::const_iterator iit = it->insetList().begin();
255                 InsetList::const_iterator eend = it->insetList().end();
256                 for (; iit != eend; ++iit) {
257                         Inset * inset = iit->inset;
258                         if (inset->lyxCode() != NOMENCL_CODE)
259                                 continue;
260                         nomencl = static_cast<InsetNomencl const *>(inset);
261                         docstring const symbol =
262                                 nomencl->getParam("symbol");
263                         // This is only an approximation,
264                         // but the best we can get.
265                         int const wx = use_gui ?
266                                 theFontMetrics(Font()).width(symbol) :
267                                 symbol.size();
268                         if (wx > w) {
269                                 w = wx;
270                                 symb = symbol;
271                         }
272                 }
273         }
274         // return the widest (or an empty) string
275         if (symb.empty())
276                 return symb;
277
278         // we have to encode the string properly
279         docstring latex_symb;
280         for (size_t n = 0; n < symb.size(); ++n) {
281                 try {
282                         latex_symb += runparams.encoding->latexChar(symb[n]).first;
283                 } catch (EncodingException & /* e */) {
284                         if (runparams.dryrun) {
285                                 latex_symb += "<" + _("LyX Warning: ")
286                                            + _("uncodable character") + " '";
287                                 latex_symb += docstring(1, symb[n]);
288                                 latex_symb += "'>";
289                         }
290                 }
291         }
292         return latex_symb;
293 }
294 } // namespace anon
295
296
297 void InsetPrintNomencl::latex(otexstream & os, OutputParams const & runparams_in) const
298 {
299         OutputParams runparams = runparams_in;
300         if (getParam("set_width") == "auto") {
301                 docstring widest = nomenclWidest(buffer(), runparams);
302                 // Set the label width via nomencl's command \nomlabelwidth.
303                 // This must be output before the command \printnomenclature
304                 if (!widest.empty()) {
305                         os << "\\settowidth{\\nomlabelwidth}{"
306                            << widest
307                            << "}\n";
308                 }
309         } else if (getParam("set_width") == "custom") {
310                 // custom length as optional arg of \printnomenclature
311                 string const width =
312                         Length(to_ascii(getParam("width"))).asLatexString();
313                 os << '\\'
314                    << from_ascii(getCmdName())
315                    << '['
316                    << from_ascii(width)
317                    << "]{}";
318                 return;
319         }
320         // output the command \printnomenclature
321         os << getCommand(runparams);
322 }
323
324
325 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
326 {
327         features.require("nomencl");
328 }
329
330
331 InsetCode InsetPrintNomencl::lyxCode() const
332 {
333         return NOMENCL_PRINT_CODE;
334 }
335
336
337 string InsetPrintNomencl::contextMenuName() const
338 {
339         return "context-nomenclprint";
340 }
341
342
343 } // namespace lyx