]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
* src/paragraph_funcs.cpp (breakParagraph): change parameter 'flag' to
[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         // We don't want to output p_["name"], since that is only used 
86         // in docbook. So we construct new params, without it, and use that.
87         InsetCommandParams p("ref", getCmdName());
88         p["reference"] = getParam("reference");
89         os << escape(p.getCommand());
90         return 0;
91 }
92
93
94 int InsetRef::plaintext(Buffer const &, odocstream & os,
95                         OutputParams const &) const
96 {
97         docstring const str = getParam("reference");
98         os << '[' << str << ']';
99         return 2 + str.size();
100 }
101
102
103 int InsetRef::docbook(Buffer const & buf, odocstream & os,
104                       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(buf, runparams, getParam("reference"))
111                            << "\" />";
112                 } else {
113                         os << "<xref linkend=\""
114                            << sgml::cleanID(buf, runparams, getParam("reference"))
115                            << "\">";
116                 }
117         } else {
118                 os << "<link linkend=\""
119                    << sgml::cleanID(buf, runparams, getParam("reference"))
120                    << "\">"
121                    << getParam("name")
122                    << "</link>";
123         }
124
125         return 0;
126 }
127
128
129 int InsetRef::textString(Buffer const & buf, odocstream & os,
130                        OutputParams const & op) const
131 {
132         return plaintext(buf, os, op);
133 }
134
135
136 void InsetRef::validate(LaTeXFeatures & features) const
137 {
138         if (getCmdName() == "vref" || getCmdName() == "vpageref")
139                 features.require("varioref");
140         else if (getCmdName() == "prettyref")
141                 features.require("prettyref");
142         else if (getCmdName() == "eqref")
143                 features.require("amsmath");
144 }
145
146
147 InsetRef::type_info InsetRef::types[] = {
148         { "ref",       N_("Standard"),              N_("Ref: ")},
149         { "eqref",     N_("Equation"),              N_("EqRef: ")},
150         { "pageref",   N_("Page Number"),           N_("Page: ")},
151         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
152         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
153         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
154         { "", "", "" }
155 };
156
157
158 int InsetRef::getType(string const & name)
159 {
160         for (int i = 0; !types[i].latex_name.empty(); ++i)
161                 if (name == types[i].latex_name)
162                         return i;
163         return 0;
164 }
165
166
167 string const & InsetRef::getName(int type)
168 {
169         return types[type].latex_name;
170 }
171
172
173 } // namespace lyx