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