]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
Fix InsetLine metrics and drawing.
[lyx.git] / src / insets / InsetRef.cpp
1 /**
2  * \file InsetRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author José Matos
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10 #include <config.h>
11
12 #include "InsetRef.h"
13
14 #include "Buffer.h"
15 #include "Cursor.h"
16 #include "DispatchResult.h"
17 #include "InsetLabel.h"
18 #include "LaTeXFeatures.h"
19 #include "LyX.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "ParIterator.h"
23 #include "sgml.h"
24 #include "TocBackend.h"
25
26 #include "support/docstream.h"
27 #include "support/gettext.h"
28 #include "support/lstrings.h"
29
30 using namespace lyx::support;
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetRef::InsetRef(Buffer * buf, InsetCommandParams const & p)
37         : InsetCommand(buf, p, "ref"), isLatex(buf->isLatex())
38 {}
39
40
41 InsetRef::InsetRef(InsetRef const & ir)
42         : InsetCommand(ir), isLatex(ir.isLatex)
43 {}
44
45
46 bool InsetRef::isCompatibleCommand(string const & s) {
47         //FIXME This is likely not the best way to handle this.
48         //But this stuff is hardcoded elsewhere already.
49         return s == "ref" 
50                 || s == "pageref"
51                 || s == "vref" 
52                 || s == "vpageref"
53                 || s == "prettyref"
54                 || s == "eqref"
55                 || s == "nameref";
56 }
57
58
59 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
60 {
61         static ParamInfo param_info_;
62         if (param_info_.empty()) {
63                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
64                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED,
65                                 ParamInfo::HANDLING_ESCAPE);
66         }
67         return param_info_;
68 }
69
70
71 int InsetRef::latex(odocstream & os, OutputParams const & runparams) const
72 {
73         // We don't want to output p_["name"], since that is only used 
74         // in docbook. So we construct new params, without it, and use that.
75         InsetCommandParams p(REF_CODE, getCmdName());
76         p["reference"] = getParam("reference");
77         os << p.getCommand(runparams);
78         return 0;
79 }
80
81
82 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
83 {
84         docstring const str = getParam("reference");
85         os << '[' << str << ']';
86         return 2 + str.size();
87 }
88
89
90 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
91 {
92         docstring const & name = getParam("name");
93         if (name.empty()) {
94                 if (runparams.flavor == OutputParams::XML) {
95                         os << "<xref linkend=\""
96                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
97                            << "\" />";
98                 } else {
99                         os << "<xref linkend=\""
100                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
101                            << "\">";
102                 }
103         } else {
104                 os << "<link linkend=\""
105                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
106                    << "\">"
107                    << getParam("name")
108                    << "</link>";
109         }
110
111         return 0;
112 }
113
114
115 docstring InsetRef::xhtml(XHTMLStream & xs, OutputParams const &) const
116 {
117         docstring const & ref = getParam("reference");
118         InsetLabel const * il = buffer().insetLabel(ref);
119         string const & cmd = params().getCmdName();
120         docstring display_string;
121
122         if (il && !il->counterValue().empty()) {
123                 // Try to construct a label from the InsetLabel we reference.
124                 docstring const & value = il->counterValue();
125                 if (cmd == "ref")
126                         display_string = value;
127                 else if (cmd == "vref")
128                         // normally, would be "ref on page #", but we have no pages
129                         display_string = value;
130                 else if (cmd == "pageref" || cmd == "vpageref")
131                         // normally would be "on page #", but we have no pages
132                         display_string = _("elsewhere");
133                 else if (cmd == "eqref")
134                         display_string = bformat(from_ascii("equation (%1$s)"), value);
135                 else if (cmd == "prettyref" 
136                          // we don't really have the ability to handle these 
137                          // properly in XHTML output
138                          || cmd == "nameref")
139                         display_string = il->prettyCounter();
140         } else 
141                         display_string = ref;
142
143         // FIXME What we'd really like to do is to be able to output some
144         // appropriate sort of text here. But to do that, we need to associate
145         // some sort of counter with the label, and we don't have that yet.
146         string const attr = "href=\"#" + html::cleanAttr(to_utf8(ref)) + "\"";
147         xs << html::StartTag("a", attr);
148         xs << display_string;
149         xs << html::EndTag("a");
150         return docstring();
151 }
152
153
154 void InsetRef::tocString(odocstream & os) const
155 {
156         plaintext(os, OutputParams(0));
157 }
158
159
160 void InsetRef::updateBuffer(ParIterator const & it, UpdateType)
161 {
162         docstring const & ref = getParam("reference");
163         // register this inset into the buffer reference cache.
164         buffer().references(ref).push_back(make_pair(this, it));
165
166         docstring label;
167         for (int i = 0; !types[i].latex_name.empty(); ++i) {
168                 if (getCmdName() == types[i].latex_name) {
169                         label = _(types[i].short_gui_name);
170                         break;
171                 }
172         }
173         label += ref;
174
175         if (!isLatex && !getParam("name").empty()) {
176                 label += "||";
177                 label += getParam("name");
178         }
179         
180         screen_label_ = label;
181         bool shortened = false;
182         unsigned int const maxLabelChars = 24;
183         if (screen_label_.size() > maxLabelChars) {
184                 screen_label_.erase(maxLabelChars - 3);
185                 screen_label_ += "...";
186                 shortened = true;
187         }
188         if (shortened)
189                 tooltip_ = label;
190         else 
191                 tooltip_ = from_ascii("");
192 }
193
194
195 void InsetRef::addToToc(DocIterator const & cpit)
196 {
197         docstring const & label = getParam("reference");
198         if (buffer().insetLabel(label))
199                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
200                 return;
201
202         // It seems that this reference does not point to any valid label.
203         screen_label_ = _("BROKEN: ") + screen_label_;
204         Toc & toc = buffer().tocBackend().toc("label");
205         toc.push_back(TocItem(cpit, 0, screen_label_));
206 }
207
208
209 void InsetRef::validate(LaTeXFeatures & features) const
210 {
211         string const cmd = getCmdName();
212         if (cmd == "vref" || cmd == "vpageref")
213                 features.require("varioref");
214         else if (cmd == "prettyref")
215                 features.require("prettyref");
216         else if (cmd == "eqref")
217                 features.require("amsmath");
218         else if (cmd == "nameref")
219                 features.require("nameref");
220 }
221
222
223 InsetRef::type_info InsetRef::types[] = {
224         { "ref",       N_("Standard"),              N_("Ref: ")},
225         { "eqref",     N_("Equation"),              N_("EqRef: ")},
226         { "pageref",   N_("Page Number"),           N_("Page: ")},
227         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
228         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
229         { "prettyref", N_("PrettyRef"),             N_("FrmtRef: ")},
230         { "nameref",   N_("Reference to Name"),     N_("NameRef:")},
231         { "", "", "" }
232 };
233
234
235 int InsetRef::getType(string const & name)
236 {
237         for (int i = 0; !types[i].latex_name.empty(); ++i)
238                 if (name == types[i].latex_name)
239                         return i;
240         return 0;
241 }
242
243
244 string const & InsetRef::getName(int type)
245 {
246         return types[type].latex_name;
247 }
248
249
250 } // namespace lyx