]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
3683905a1b5315dedba67ea1293870d81235a1e9
[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 "FuncRequest.h"
18 #include "LaTeXFeatures.h"
19 #include "LyXFunc.h"
20 #include "OutputParams.h"
21 #include "ParIterator.h"
22 #include "sgml.h"
23 #include "TocBackend.h"
24
25 #include "support/docstream.h"
26 #include "support/gettext.h"
27 #include "support/lstrings.h"
28 #include "support/FileName.h"
29
30 using namespace lyx::support;
31 using namespace std;
32
33 namespace lyx {
34
35
36 InsetRef::InsetRef(Buffer const & buf, InsetCommandParams const & p)
37         : InsetCommand(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 }
56
57
58 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
59 {
60         static ParamInfo param_info_;
61         if (param_info_.empty()) {
62                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
63                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
64         }
65         return param_info_;
66 }
67
68
69 docstring InsetRef::screenLabel() const
70 {
71         return screen_label_;
72 }
73
74
75 int InsetRef::latex(odocstream & os, OutputParams const &) const
76 {
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, getCmdName());
80         p["reference"] = getParam("reference");
81         os << escape(p.getCommand());
82         return 0;
83 }
84
85
86 int InsetRef::plaintext(odocstream & os, OutputParams const &) const
87 {
88         docstring const str = getParam("reference");
89         os << '[' << str << ']';
90         return 2 + str.size();
91 }
92
93
94 int InsetRef::docbook(odocstream & os, OutputParams const & runparams) const
95 {
96         docstring const & name = getParam("name");
97         if (name.empty()) {
98                 if (runparams.flavor == OutputParams::XML) {
99                         os << "<xref linkend=\""
100                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
101                            << "\" />";
102                 } else {
103                         os << "<xref linkend=\""
104                            << sgml::cleanID(buffer(), runparams, getParam("reference"))
105                            << "\">";
106                 }
107         } else {
108                 os << "<link linkend=\""
109                    << sgml::cleanID(buffer(), runparams, getParam("reference"))
110                    << "\">"
111                    << getParam("name")
112                    << "</link>";
113         }
114
115         return 0;
116 }
117
118
119 void InsetRef::tocString(odocstream & os) const
120 {
121         plaintext(os, OutputParams(0));
122 }
123
124
125 void InsetRef::updateLabels(ParIterator const & it)
126 {
127         docstring const & label = getParam("reference");
128         // register this inset into the buffer reference cache.
129         buffer().references(label).push_back(make_pair(this, it));
130
131         for (int i = 0; !types[i].latex_name.empty(); ++i) {
132                 if (getCmdName() == types[i].latex_name) {
133                         screen_label_ = _(types[i].short_gui_name);
134                         break;
135                 }
136         }
137         screen_label_ += getParam("reference");
138
139         if (!isLatex && !getParam("name").empty()) {
140                 screen_label_ += "||";
141                 screen_label_ += getParam("name");
142         }
143 }
144
145
146 /** \note
147  ** If inset belongs to an Embedded WorkArea's Buffer, ref is likely
148  ** to point to some documentBuffer's label.  Don't know how to
149  ** retrieve the corresponding documentBuffer (so as to check if the
150  ** label exists or not), so the "BROKEN:" indication is disabled in
151  ** Embedded WorkArea(s).
152  **/
153 void InsetRef::addToToc(DocIterator const & cpit)
154 {
155         docstring const & label = getParam("reference");
156         if (buffer().fileName().extension() == "internal")
157                 return;
158
159         if (buffer().insetLabel(label))
160                 // This InsetRef has already been taken care of in InsetLabel::addToToc().
161                 return;
162
163         // It seems that this reference does not point to any valid label.
164         screen_label_ = _("BROKEN: ") + screen_label_;
165         Toc & toc = buffer().tocBackend().toc("label");
166         toc.push_back(TocItem(cpit, 0, screen_label_));
167 }
168
169
170 void InsetRef::validate(LaTeXFeatures & features) const
171 {
172         if (getCmdName() == "vref" || getCmdName() == "vpageref")
173                 features.require("varioref");
174         else if (getCmdName() == "prettyref")
175                 features.require("prettyref");
176         else if (getCmdName() == "eqref")
177                 features.require("amsmath");
178 }
179
180
181 InsetRef::type_info InsetRef::types[] = {
182         { "ref",       N_("Standard"),              N_("Ref: ")},
183         { "eqref",     N_("Equation"),              N_("EqRef: ")},
184         { "pageref",   N_("Page Number"),           N_("Page: ")},
185         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
186         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
187         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
188         { "", "", "" }
189 };
190
191
192 int InsetRef::getType(string const & name)
193 {
194         for (int i = 0; !types[i].latex_name.empty(); ++i)
195                 if (name == types[i].latex_name)
196                         return i;
197         return 0;
198 }
199
200
201 string const & InsetRef::getName(int type)
202 {
203         return types[type].latex_name;
204 }
205
206
207 } // namespace lyx