]> git.lyx.org Git - lyx.git/blob - src/insets/InsetRef.cpp
'using namespace lyx::support' instead of 'using support::xxx'
[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 "support/gettext.h"
19 #include "LaTeXFeatures.h"
20 #include "LyXFunc.h"
21 #include "OutputParams.h"
22 #include "sgml.h"
23
24 #include "support/docstream.h"
25 #include "support/lstrings.h"
26
27 using namespace std;
28 using namespace lyx::support;
29
30 namespace lyx {
31
32
33 InsetRef::InsetRef(InsetCommandParams const & p, Buffer const & buf)
34         : InsetCommand(p, "ref"), isLatex(buf.isLatex())
35 {}
36
37
38 InsetRef::InsetRef(InsetRef const & ir)
39         : InsetCommand(ir), isLatex(ir.isLatex)
40 {}
41
42
43 bool InsetRef::isCompatibleCommand(std::string const & s) {
44         //FIXME This is likely not the best way to handle this.
45         //But this stuff is hardcoded elsewhere already.
46         return s == "ref" 
47                 || s == "pageref"
48                 || s == "vref" 
49                 || s == "vpageref"
50                 || s == "prettyref"
51                 || s == "eqref";
52 }
53
54
55 CommandInfo const * InsetRef::findInfo(std::string const & /* cmdName */)
56 {
57         static const char * const paramnames[] = {"name", "reference", ""};
58         static const bool isoptional[] = {true, false};
59         static const CommandInfo info = {2, paramnames, isoptional};
60         return &info;
61 }
62
63
64 void InsetRef::doDispatch(Cursor & cur, FuncRequest & cmd)
65 {
66         switch (cmd.action) {
67         case LFUN_MOUSE_RELEASE:
68                 // Eventually trigger dialog with button 3 not 1
69                 if (cmd.button() == mouse_button::button3)
70                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO,
71                                                   getParam("reference")));
72                 else
73                         InsetCommand::doDispatch(cur, cmd);
74                 break;
75
76         default:
77                 InsetCommand::doDispatch(cur, cmd);
78         }
79 }
80
81
82 docstring const InsetRef::getScreenLabel(Buffer const &) const
83 {
84         docstring temp;
85         for (int i = 0; !types[i].latex_name.empty(); ++i) {
86                 if (getCmdName() == types[i].latex_name) {
87                         temp = _(types[i].short_gui_name);
88                         break;
89                 }
90         }
91         temp += getParam("reference");
92
93         if (!isLatex && !getParam("name").empty()) {
94                 temp += "||";
95                 temp += getParam("name");
96         }
97         return temp;
98 }
99
100
101 int InsetRef::latex(Buffer const &, odocstream & os,
102                     OutputParams const &) const
103 {
104         // We don't want to output p_["name"], since that is only used 
105         // in docbook. So we construct new params, without it, and use that.
106         InsetCommandParams p(REF_CODE, getCmdName());
107         p["reference"] = getParam("reference");
108         os << escape(p.getCommand());
109         return 0;
110 }
111
112
113 int InsetRef::plaintext(Buffer const &, odocstream & os,
114                         OutputParams const &) const
115 {
116         docstring const str = getParam("reference");
117         os << '[' << str << ']';
118         return 2 + str.size();
119 }
120
121
122 int InsetRef::docbook(Buffer const & buf, odocstream & os,
123                       OutputParams const & runparams) const
124 {
125         docstring const & name = getParam("name");
126         if (name.empty()) {
127                 if (runparams.flavor == OutputParams::XML) {
128                         os << "<xref linkend=\""
129                            << sgml::cleanID(buf, runparams, getParam("reference"))
130                            << "\" />";
131                 } else {
132                         os << "<xref linkend=\""
133                            << sgml::cleanID(buf, runparams, getParam("reference"))
134                            << "\">";
135                 }
136         } else {
137                 os << "<link linkend=\""
138                    << sgml::cleanID(buf, runparams, getParam("reference"))
139                    << "\">"
140                    << getParam("name")
141                    << "</link>";
142         }
143
144         return 0;
145 }
146
147
148 int InsetRef::textString(Buffer const & buf, odocstream & os,
149                        OutputParams const & op) const
150 {
151         return plaintext(buf, os, op);
152 }
153
154
155 void InsetRef::validate(LaTeXFeatures & features) const
156 {
157         if (getCmdName() == "vref" || getCmdName() == "vpageref")
158                 features.require("varioref");
159         else if (getCmdName() == "prettyref")
160                 features.require("prettyref");
161         else if (getCmdName() == "eqref")
162                 features.require("amsmath");
163 }
164
165
166 InsetRef::type_info InsetRef::types[] = {
167         { "ref",       N_("Standard"),              N_("Ref: ")},
168         { "eqref",     N_("Equation"),              N_("EqRef: ")},
169         { "pageref",   N_("Page Number"),           N_("Page: ")},
170         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
171         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
172         { "prettyref", N_("PrettyRef"),             N_("FormatRef: ")},
173         { "", "", "" }
174 };
175
176
177 int InsetRef::getType(string const & name)
178 {
179         for (int i = 0; !types[i].latex_name.empty(); ++i)
180                 if (name == types[i].latex_name)
181                         return i;
182         return 0;
183 }
184
185
186 string const & InsetRef::getName(int type)
187 {
188         return types[type].latex_name;
189 }
190
191
192 } // namespace lyx