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