]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
doubly stupid bug fix.
[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
29 using namespace std;
30 using namespace lyx::support;
31
32 namespace lyx {
33
34
35 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
36         : InsetCommand(p, "ref"), isLatex(buf.isLatex())
37 {}
38
39
40 InsetRef::InsetRef(InsetRef const & ir)
41         : InsetCommand(ir), isLatex(ir.isLatex)
42 {}
43
44
45 bool InsetRef::isCompatibleCommand(string const & s) {
46         //FIXME This is likely not the best way to handle this.
47         //But this stuff is hardcoded elsewhere already.
48         return s == "ref" 
49                 || s == "pageref"
50                 || s == "vref" 
51                 || s == "vpageref"
52                 || s == "prettyref"
53                 || s == "eqref";
54 }
55
56
57 ParamInfo const & InsetRef::findInfo(string const & /* cmdName */)
58 {
59         static ParamInfo param_info_;
60         if (param_info_.empty()) {
61                 param_info_.add("name", ParamInfo::LATEX_OPTIONAL);
62                 param_info_.add("reference", ParamInfo::LATEX_REQUIRED);
63         }
64         return param_info_;
65 }
66
67
68 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
69 {
70         switch (cmd.action) {
71         case LFUN_MOUSE_RELEASE:
72                 // Eventually trigger dialog with button 3 not 1
73                 if (cmd.button() == mouse_button::button3)
74                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
75                                                   getParam("reference")));
76                 else
77                         InsetCommand::doDispatch(cur, cmd);
78                 break;
79
80         default:
81                 InsetCommand::doDispatch(cur, cmd);
82         }
83 }
84
85
86 docstring const InsetRef::getScreenLabel(Buffer const &) const
87 {
88         docstring temp;
89         for (int i = 0; !types[i].latex_name.empty(); ++i) {
90                 if (getCmdName() == types[i].latex_name) {
91                         temp = _(types[i].short_gui_name);
92                         break;
93                 }
94         }
95         temp += getParam("reference");
96
97         if (!isLatex && !getParam("name").empty()) {
98                 temp += "||";
99                 temp += getParam("name");
100         }
101         return temp;
102 }
103
104
105 int InsetRef::latex(Buffer const &, odocstream & os,
106                     OutputParams const &) const
107 {
108         // We don't want to output p_["name"], since that is only used 
109         // in docbook. So we construct new params, without it, and use that.
110         InsetCommandParams p(REF_CODE, getCmdName());
111         p["reference"] = getParam("reference");
112         os << escape(p.getCommand());
113         return 0;
114 }
115
116
117 int InsetRef::plaintext(Buffer const &, odocstream & os,
118                         OutputParams const &) const
119 {
120         docstring const str = getParam("reference");
121         os << '[' << str << ']';
122         return 2 + str.size();
123 }
124
125
126 int InsetRef::docbook(Buffer const & buf, odocstream & os,
127                       OutputParams const & runparams) const
128 {
129         docstring const & name = getParam("name");
130         if (name.empty()) {
131                 if (runparams.flavor == OutputParams::XML) {
132                         os << "<xref linkend=\""
133                            << sgml::cleanID(buf, runparams, getParam("reference"))
134                            << "\" />";
135                 } else {
136                         os << "<xref linkend=\""
137                            << sgml::cleanID(buf, runparams, getParam("reference"))
138                            << "\">";
139                 }
140         } else {
141                 os << "<link linkend=\""
142                    << sgml::cleanID(buf, runparams, getParam("reference"))
143                    << "\">"
144                    << getParam("name")
145                    << "</link>";
146         }
147
148         return 0;
149 }
150
151
152 void InsetRef::textString(Buffer const & buf, odocstream & os) const
153 {
154         plaintext(buf, os, OutputParams(0));
155 }
156
157
158 void InsetRef::addToToc(Buffer const & buf,
159         ParConstIterator const & cpit) const
160 {
161         docstring const & label = getParam("reference");
162         Toc & toc = buf.tocBackend().toc("label");
163         Toc::iterator it = toc.begin();
164         Toc::iterator end = toc.end();
165         for (; it != end; ++it) {
166                 if (it->str() == label)
167                         break;
168         }
169
170         docstring const reflabel = getScreenLabel(buf);
171         if (it == end) {
172                 // This label has not been parsed yet so we just add it temporarily.
173                 // InsetLabel::addTocToc() will fix that later.
174                 toc.push_back(TocItem(cpit, 0, label));
175                 toc.push_back(TocItem(cpit, 1, reflabel));
176                 return;
177         }
178
179         // The Toc item for this label already exists so let's add
180         // this inset to this node.
181         ++it;
182         while (it != end && it->str() == reflabel)
183                 ++it;
184         toc.insert(it, TocItem(cpit, 1, reflabel));
185 }
186
187
188 void InsetRef::validate(LaTeXFeatures & features) const
189 {
190         if (getCmdName() == "vref" || getCmdName() == "vpageref")
191                 features.require("varioref");
192         else if (getCmdName() == "prettyref")
193                 features.require("prettyref");
194         else if (getCmdName() == "eqref")
195                 features.require("amsmath");
196 }
197
198
199 InsetRef::type_info InsetRef::types[] = {
200         { "ref",       N_("Standard"),              N_("Ref: ")},
201         { "eqref",     N_("Equation"),              N_("EqRef: ")},
202         { "pageref",   N_("Page Number"),           N_("Page: ")},
203         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
204         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
205         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
206         { "", "", "" }
207 };
208
209
210 int InsetRef::getType(string const & name)
211 {
212         for (int i = 0; !types[i].latex_name.empty(); ++i)
213                 if (name == types[i].latex_name)
214                         return i;
215         return 0;
216 }
217
218
219 string const & InsetRef::getName(int type)
220 {
221         return types[type].latex_name;
222 }
223
224
225 } // namespace lyx