]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
Fix Bug 3947: BibTeX allows parentheses in the key (even if the whole entry is delimi...
[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 "gettext.h"
19 #include "LaTeXFeatures.h"
20 #include "LyXFunc.h"
21 #include "OutputParams.h"
22 #include "sgml.h"
23
24 #include "support/lstrings.h"
25
26
27 namespace lyx {
28
29 using support::escape;
30
31 using std::string;
32 using std::ostream;
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 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
46 {
47         switch (cmd.action) {
48         case LFUN_MOUSE_RELEASE:
49                 // Eventually trigger dialog with button 3 not 1
50                 if (cmd.button() == mouse_button::button3)
51                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
52                                                   getParam("reference")));
53                 else
54                         InsetCommand::doDispatch(cur, cmd);
55                 break;
56
57         default:
58                 InsetCommand::doDispatch(cur, cmd);
59         }
60 }
61
62
63 docstring const InsetRef::getScreenLabel(Buffer const &) const
64 {
65         docstring temp;
66         for (int i = 0; !types[i].latex_name.empty(); ++i) {
67                 if (getCmdName() == types[i].latex_name) {
68                         temp = _(types[i].short_gui_name);
69                         break;
70                 }
71         }
72         temp += getParam("reference");
73
74         if (!isLatex && !getParam("name").empty()) {
75                 temp += "||";
76                 temp += getParam("name");
77         }
78         return temp;
79 }
80
81
82 int InsetRef::latex(Buffer const &, odocstream & os,
83                     OutputParams const &) const
84 {
85         // Don't output p_["name"], this is only used in docbook
86         InsetCommandParams p(getCmdName());
87         p["reference"] = getParam("reference");
88         os << escape(p.getCommand());
89         return 0;
90 }
91
92
93 int InsetRef::plaintext(Buffer const &, odocstream & os,
94                         OutputParams const &) const
95 {
96         docstring const str = getParam("reference");
97         os << '[' << str << ']';
98         return 2 + str.size();
99 }
100
101
102 int InsetRef::docbook(Buffer const & buf, odocstream & os,
103                       OutputParams const & runparams) const
104 {
105         docstring const & name = getParam("name");
106         if (name.empty()) {
107                 if (runparams.flavor == OutputParams::XML) {
108                         os << "<xref linkend=\""
109                            << sgml::cleanID(buf, runparams, getParam("reference"))
110                            << "\" />";
111                 } else {
112                         os << "<xref linkend=\""
113                            << sgml::cleanID(buf, runparams, getParam("reference"))
114                            << "\">";
115                 }
116         } else {
117                 os << "<link linkend=\""
118                    << sgml::cleanID(buf, runparams, getParam("reference"))
119                    << "\">"
120                    << getParam("name")
121                    << "</link>";
122         }
123
124         return 0;
125 }
126
127
128 int InsetRef::textString(Buffer const & buf, odocstream & os,
129                        OutputParams const & op) const
130 {
131         return plaintext(buf, os, op);
132 }
133
134
135 void InsetRef::validate(LaTeXFeatures & features) const
136 {
137         if (getCmdName() == "vref" || getCmdName() == "vpageref")
138                 features.require("varioref");
139         else if (getCmdName() == "prettyref")
140                 features.require("prettyref");
141         else if (getCmdName() == "eqref")
142                 features.require("amsmath");
143 }
144
145
146 InsetRef::type_info InsetRef::types[] = {
147         { "ref",       N_("Standard"),              N_("Ref: ")},
148         { "eqref",     N_("Equation"),              N_("EqRef: ")},
149         { "pageref",   N_("Page Number"),           N_("Page: ")},
150         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
151         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
152         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
153         { "", "", "" }
154 };
155
156
157 int InsetRef::getType(string const & name)
158 {
159         for (int i = 0; !types[i].latex_name.empty(); ++i)
160                 if (name == types[i].latex_name)
161                         return i;
162         return 0;
163 }
164
165
166 string const & InsetRef::getName(int type)
167 {
168         return types[type].latex_name;
169 }
170
171
172 } // namespace lyx