]> git.lyx.org Git - features.git/blob - src/insets/InsetNomencl.cpp
* InsetNomencl.cpp (nomenclWidest):
[features.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 "InsetIterator.h"
23 #include "InsetList.h"
24 #include "LaTeXFeatures.h"
25 #include "MetricsInfo.h"
26 #include "sgml.h"
27
28 #include "frontends/FontMetrics.h"
29
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32 #include "support/lstrings.h"
33
34 using namespace std;
35 using namespace lyx::support;
36
37 namespace lyx {
38
39
40 /////////////////////////////////////////////////////////////////////
41 //
42 // InsetNomencl
43 //
44 /////////////////////////////////////////////////////////////////////
45
46 InsetNomencl::InsetNomencl(InsetCommandParams const & p)
47         : InsetCommand(p, "nomenclature"),
48           nomenclature_entry_id(sgml::uniqueID(from_ascii("nomen")))
49 {}
50
51
52 ParamInfo const & InsetNomencl::findInfo(string const & /* cmdName */)
53 {
54         static ParamInfo param_info_;
55         if (param_info_.empty()) {
56                 param_info_.add("prefix", ParamInfo::LATEX_OPTIONAL);
57                 param_info_.add("symbol", ParamInfo::LATEX_REQUIRED);
58                 param_info_.add("description", ParamInfo::LATEX_REQUIRED);
59         }
60         return param_info_;
61 }
62
63
64 docstring InsetNomencl::screenLabel() const
65 {
66         size_t const maxLabelChars = 25;
67
68         docstring label = _("Nom: ") + getParam("symbol");
69         if (label.size() > maxLabelChars) {
70                 label.erase(maxLabelChars - 3);
71                 label += "...";
72         }
73         return label;
74 }
75
76
77 docstring InsetNomencl::toolTip(BufferView const & /*bv*/, int /*x*/, int /*y*/) const
78 {
79         docstring tip = _("Nomenclature Symbol: ") + getParam("symbol") + "\n";
80         tip += _("Description: ") + "\t"
81                 + subst(getParam("description"), from_ascii("\\\\"), from_ascii("\n\t"));
82         if (!getParam("prefix").empty())
83                 tip += "\n" + _("Sorting: ") + getParam("prefix");
84         return tip;
85 }
86
87
88
89 int InsetNomencl::docbook(odocstream & os, OutputParams const &) const
90 {
91         os << "<glossterm linkend=\"" << nomenclature_entry_id << "\">"
92            << sgml::escapeString(getParam("symbol"))
93            << "</glossterm>";
94         return 0;
95 }
96
97
98 int InsetNomencl::docbookGlossary(odocstream & os) const
99 {
100         os << "<glossentry id=\"" << nomenclature_entry_id << "\">\n"
101            << "<glossterm>"
102            << sgml::escapeString(getParam("symbol"))
103            << "</glossterm>\n"
104            << "<glossdef><para>"
105            << sgml::escapeString(getParam("description"))
106            << "</para></glossdef>\n"
107            <<"</glossentry>\n";
108         return 4;
109 }
110
111
112 void InsetNomencl::validate(LaTeXFeatures & features) const
113 {
114         features.require("nomencl");
115 }
116
117
118 /////////////////////////////////////////////////////////////////////
119 //
120 // InsetPrintNomencl
121 //
122 /////////////////////////////////////////////////////////////////////
123
124 InsetPrintNomencl::InsetPrintNomencl(InsetCommandParams const & p)
125         : InsetCommand(p, "printnomenclature")
126 {}
127
128
129 ParamInfo const & InsetPrintNomencl::findInfo(string const & /* cmdName */)
130 {
131         // The symbol width is set via nomencl's \nomlabelwidth in 
132         // InsetPrintNomencl::latex and not as optional parameter of
133         // \printnomenclature
134         static ParamInfo param_info_;
135         if (param_info_.empty()) {
136                 // how is the width set?
137                 // values: none|auto
138                 param_info_.add("set_width", ParamInfo::LYX_INTERNAL);
139         }
140         return param_info_;
141 }
142
143
144 docstring InsetPrintNomencl::screenLabel() const
145 {
146         return _("Nomenclature");
147 }
148
149
150 int InsetPrintNomencl::docbook(odocstream & os, OutputParams const &) const
151 {
152         os << "<glossary>\n";
153         int newlines = 2;
154         InsetIterator it = inset_iterator_begin(buffer().inset());
155         while (it) {
156                 if (it->lyxCode() == NOMENCL_CODE) {
157                         newlines += static_cast<InsetNomencl const &>(*it).docbookGlossary(os);
158                         ++it;
159                 } else if (!it->producesOutput()) {
160                         // Ignore contents of insets that are not in output
161                         size_t const depth = it.depth();
162                         ++it;
163                         while (it.depth() > depth)
164                                 ++it;
165                 } else {
166                         ++it;
167                 }
168         }
169         os << "</glossary>\n";
170         return newlines;
171 }
172
173
174 namespace {
175 docstring nomenclWidest(Buffer const & buffer)
176 {
177         // nomenclWidest() determines and returns the widest used
178         // nomenclature symbol in the document
179
180         int w = 0;
181         docstring symb;
182         InsetNomencl const * nomencl = 0;
183         ParagraphList::const_iterator it = buffer.paragraphs().begin();
184         ParagraphList::const_iterator end = buffer.paragraphs().end();
185
186         for (; it != end; ++it) {
187                 if (it->insetList().empty())
188                         continue;
189                 InsetList::const_iterator iit = it->insetList().begin();
190                 InsetList::const_iterator eend = it->insetList().end();
191                 for (; iit != eend; ++iit) {
192                         Inset * inset = iit->inset;
193                         if (inset->lyxCode() != NOMENCL_CODE)
194                                 continue;
195                         nomencl = static_cast<InsetNomencl const *>(inset);
196                         docstring const symbol =
197                                 nomencl->getParam("symbol");
198                         // This is only an approximation,
199                         // but the best we can get.
200                         int const wx =
201                                 theFontMetrics(Font()).width(symbol);
202                         if (wx > w) {
203                                 w = wx;
204                                 symb = symbol;
205                         }
206                 }
207         }
208         // return the widest (or an empty) string
209         return symb;
210 }
211 }
212
213
214 int InsetPrintNomencl::latex(odocstream & os, OutputParams const &) const
215 {
216         int lines = 0;
217         if (getParam("set_width") == "auto") {
218                 docstring widest = nomenclWidest(buffer());
219                 // set the label width via nomencl's command
220                 // \nomlabelwidth. This must be output before the command
221                 // \printnomenclature
222                 if (!widest.empty()) {
223                         // assure that the width is never below the
224                         // predefined value of 1 cm
225                         // FIXME: Why this?
226                         os << "\\settowidth{\\nomlabelwidth}{"
227                            << widest
228                            << "}\n";
229                         os << "\\ifthenelse{%\n";
230                         os << "\\lengthtest{\\nomlabelwidth < 1cm}}\n";
231                         os << " {\\setlength{\\nomlabelwidth}{1cm}}\n";
232                         os << " {}\n";
233                         lines += 5;
234                 }
235         }
236         // output the command \printnomenclature
237         os << getCommand();
238         return lines;
239 }
240
241
242 void InsetPrintNomencl::validate(LaTeXFeatures & features) const
243 {
244         features.require("nomencl");
245         // needed for InsetPrintNomencl::latex
246         features.require("ifthen");
247 }
248
249
250 InsetCode InsetPrintNomencl::lyxCode() const
251 {
252         return NOMENCL_PRINT_CODE;
253 }
254
255
256 } // namespace lyx