]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNomencl.cpp
We cannot access theFontMetrics in command line mode.
[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 "DispatchResult.h"
20 #include "Font.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetIterator.h"
24 #include "InsetList.h"
25 #include "LaTeXFeatures.h"
26 #include "Length.h"
27 #include "LyX.h"
28 #include "MetricsInfo.h"
29 #include "sgml.h"
30
31 #include "frontends/FontMetrics.h"
32
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lstrings.h"
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42
43 /////////////////////////////////////////////////////////////////////
44 //
45 // InsetNomencl
46 //
47 /////////////////////////////////////////////////////////////////////
48
49 InsetNomencl::InsetNomencl(InsetCommandParams const & p)
50         : InsetCommand(p, "nomenclature"),
51           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
52 {}
53
54
55 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
56 {
57         static ParamInfo param_info_;
58         if (param_info_.empty()) {
59                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
60                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
61                 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
62         }
63         return param_info_;
64 }
65
66
67 docstring InsetNomencl::screenLabel() const
68 {
69         size_t const maxLabelChars = 25;
70
71         docstring label = _("Nom: ") + getParam("symbol");
72         if (label.size() > maxLabelChars) {
73                 label.erase(maxLabelChars - 3);
74                 label += "...";
75         }
76         return label;
77 }
78
79
80 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
81 {
82         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
83         tip += _("Description: ") + "\t"
84                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
85         if (!getParam("prefix").empty())
86                 tip += "\n" + _("Sorting: ") + getParam("prefix");
87         return tip;
88 }
89
90
91
92 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
93 {
94         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
95            << sgml::escapeString(getParam("symbol"))
96            << "</glossterm>";
97         return 0;
98 }
99
100
101 int InsetNomencl::docbookGlossary(odocstream & os) const
102 {
103         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
104            << "<glossterm>"
105            << sgml::escapeString(getParam("symbol"))
106            << "</glossterm>\n"
107            << "<glossdef><para>"
108            << sgml::escapeString(getParam("description"))
109            << "</para></glossdef>\n"
110            <<"</glossentry>\n";
111         return 4;
112 }
113
114
115 void InsetNomencl::validate(LaTeXFeatures & features) const
116 {
117         features.require("nomencl");
118 }
119
120
121 /////////////////////////////////////////////////////////////////////
122 //
123 // InsetPrintNomencl
124 //
125 /////////////////////////////////////////////////////////////////////
126
127 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
128         : InsetCommand(p, "nomencl_print")
129 {}
130
131
132 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
133 {
134         // The symbol width is set via nomencl's \nomlabelwidth in 
135         // InsetPrintNomencl::latex and not as optional parameter of
136         // \printnomenclature
137         static ParamInfo param_info_;
138         if (param_info_.empty()) {
139                 // how is the width set?
140                 // values: none|auto|custom
141                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
142                 // custom width
143                 param_info_.add("width", ParamInfo::LYX_INTERNAL);
144         }
145         return param_info_;
146 }
147
148
149 docstring InsetPrintNomencl::screenLabel() const
150 {
151         return _("Nomenclature");
152 }
153
154
155 void InsetPrintNomencl::doDispatch(Cursor & cur, FuncRequest & cmd)
156 {
157         switch (cmd.action) {
158
159         case LFUN_INSET_MODIFY: {
160                 InsetCommandParams p(NOMENCL_PRINT_CODE);
161                 // FIXME UNICODE
162                 InsetCommand::string2params("nomencl_print",
163                         to_utf8(cmd.argument()), p);
164                 if (p.getCmdName().empty()) {
165                         cur.noUpdate();
166                         break;
167                 }
168                 setParams(p);
169                 break;
170         }
171
172         default:
173                 InsetCommand::doDispatch(cur, cmd);
174                 break;
175         }
176 }
177
178
179 bool InsetPrintNomencl::getStatus(Cursor & cur, FuncRequest const & cmd,
180         FuncStatus & status) const
181 {
182         switch (cmd.action) {
183
184         case LFUN_INSET_DIALOG_UPDATE:
185         case LFUN_INSET_MODIFY:
186                 status.setEnabled(true);
187                 return true;
188
189         default:
190                 return InsetCommand::getStatus(cur, cmd, status);
191         }
192 }
193
194
195
196 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
197 {
198         os << "<glossary>\n";
199         int newlines = 2;
200         InsetIterator it = inset_iterator_begin(buffer().inset());
201         while (it) {
202                 if (it->lyxCode() == NOMENCL_CODE) {
203                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
204                         ++it;
205                 } else if (!it->producesOutput()) {
206                         // Ignore contents of insets that are not in output
207                         size_t const depth = it.depth();
208                         ++it;
209                         while (it.depth() > depth)
210                                 ++it;
211                 } else {
212                         ++it;
213                 }
214         }
215         os << "</glossary>\n";
216         return newlines;
217 }
218
219
220 namespace {
221 docstring nomenclWidest(Buffer const & buffer)
222 {
223         // nomenclWidest() determines and returns the widest used
224         // nomenclature symbol in the document
225
226         int w = 0;
227         docstring symb;
228         InsetNomencl const * nomencl = 0;
229         ParagraphList::const_iterator it = buffer.paragraphs().begin();
230         ParagraphList::const_iterator end = buffer.paragraphs().end();
231
232         for (; it != end; ++it) {
233                 if (it->insetList().empty())
234                         continue;
235                 InsetList::const_iterator iit = it->insetList().begin();
236                 InsetList::const_iterator eend = it->insetList().end();
237                 for (; iit != eend; ++iit) {
238                         Inset * inset = iit->inset;
239                         if (inset->lyxCode() != NOMENCL_CODE)
240                                 continue;
241                         nomencl = static_cast<InsetNomencl const *>(inset);
242                         docstring const symbol =
243                                 nomencl->getParam("symbol");
244                         // This is only an approximation,
245                         // but the best we can get.
246                         int const wx = use_gui ?
247                                 theFontMetrics(Font()).width(symbol) :
248                                 symbol.size();
249                         if (wx > w) {
250                                 w = wx;
251                                 symb = symbol;
252                         }
253                 }
254         }
255         // return the widest (or an empty) string
256         return symb;
257 }
258 }
259
260
261 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
262 {
263         int lines = 0;
264         if (getParam("set_width") == "auto") {
265                 docstring widest = nomenclWidest(buffer());
266                 // Set the label width via nomencl's command \nomlabelwidth.
267                 // This must be output before the command
268                 // \printnomenclature
269                 if (!widest.empty()) {
270                         // assure that the width is never below the
271                         // value predefined in nomencl.cfg
272                         os << "\\newlength{\\symbwidth}\n";
273                         os << "\\settowidth{\\symbwidth}{"
274                            << widest
275                            << "}\n";
276                         os << "\\ifthenelse{%\n";
277                         os << "\\lengthtest{\\symbwidth < \\nomlabelwidth}}\n";
278                         os << " {\\setlength{\\symbwidth}{\\nomlabelwidth}}\n";
279                         os << " {}\n";
280                         lines += 6;
281                 }
282         } else if (getParam("set_width") == "custom") {
283                 // custom length as optional arg of \printnomenclature
284                 string const width =
285                         Length(to_ascii(getParam("width"))).asLatexString();
286                 os << '\\'
287                    << from_ascii(getCmdName())
288                    << '['
289                    << from_ascii(width)
290                    << "]{}";
291                 return lines;
292         }
293         // output the command \printnomenclature
294         os << getCommand();
295         return lines;
296 }
297
298
299 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
300 {
301         features.require("nomencl");
302         // needed for InsetPrintNomencl::latex
303         features.require("ifthen");
304 }
305
306
307 InsetCode InsetPrintNomencl::lyxCode() const
308 {
309         return NOMENCL_PRINT_CODE;
310 }
311
312
313 docstring InsetPrintNomencl::contextMenu(BufferView const &, int, int) const
314 {
315         return from_ascii("context-nomenclprint");
316 }
317
318
319 } // namespace lyx