]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
This commit cleans up everything related to singleton. The other important change...
[lyx.git] / src / insets / insetref.C
1 /**
2  * \file insetref.C
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 using lyx::support::escape;
28
29 using std::string;
30 using std::ostream;
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 void InsetRef::doDispatch(LCursor & cur, FuncRequest & cmd)
44 {
45         switch (cmd.action) {
46         case LFUN_MOUSE_PRESS:
47                 // Eventually trigger dialog with button 3 not 1
48                 if (cmd.button() == mouse_button::button3)
49                         lyx::dispatch(FuncRequest(LFUN_LABEL_GOTO, getContents()));
50                 else {
51                         InsetCommandMailer("ref", *this).showDialog(&cur.bv());
52                         cur.undispatched();
53                 }
54                 return;
55
56         case LFUN_MOUSE_RELEASE:
57                 return;
58
59         default:
60                 return InsetCommand::doDispatch(cur, cmd);
61         }
62 }
63
64
65 string const InsetRef::getScreenLabel(Buffer const &) const
66 {
67         string temp;
68         for (int i = 0; !types[i].latex_name.empty(); ++i) {
69                 if (getCmdName() == types[i].latex_name) {
70                         // FIXME UNIOCDE
71                         temp = lyx::to_utf8(_(types[i].short_gui_name));
72                         break;
73                 }
74         }
75         temp += getContents();
76
77         if (!isLatex && !getOptions().empty()) {
78                 temp += "||";
79                 temp += getOptions();
80         }
81         return temp;
82 }
83
84
85 int InsetRef::latex(Buffer const &, ostream & os,
86                     OutputParams const &) const
87 {
88         if (getOptions().empty())
89                 os << escape(getCommand());
90         else {
91                 InsetCommandParams p(getCmdName(), getContents(), "");
92                 os << escape(p.getCommand());
93         }
94         return 0;
95 }
96
97
98 int InsetRef::plaintext(Buffer const &, ostream & os,
99                     OutputParams const &) const
100 {
101         os << '[' << getContents() << ']';
102         return 0;
103 }
104
105
106 int InsetRef::docbook(Buffer const & buf, ostream & os,
107                       OutputParams const & runparams) const
108 {
109         if (getOptions().empty() && runparams.flavor == OutputParams::XML) {
110                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" />";
111         } else if (getOptions().empty()) {
112                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\">";
113         } else {
114                 os << "<link linkend=\"" << sgml::cleanID(buf, runparams, getContents())
115                    << "\">" << getOptions() << "</link>";
116         }
117
118         return 0;
119 }
120
121
122 int InsetRef::textString(Buffer const & buf, ostream & os,
123                        OutputParams const & op) const
124 {
125         return plaintext(buf, os, op);
126 }
127
128
129 void InsetRef::validate(LaTeXFeatures & features) const
130 {
131         if (getCmdName() == "vref" || getCmdName() == "vpageref")
132                 features.require("varioref");
133         else if (getCmdName() == "prettyref")
134                 features.require("prettyref");
135         else if (getCmdName() == "eqref")
136                 features.require("amsmath");
137 }
138
139
140 InsetRef::type_info InsetRef::types[] = {
141         { "ref",       N_("Standard"),              N_("Ref: ")},
142         { "eqref",     N_("Equation"),              N_("EqRef: ")},
143         { "pageref",   N_("Page Number"),           N_("Page: ")},
144         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
145         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
146         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
147         { "", "", "" }
148 };
149
150
151 int InsetRef::getType(string const & name)
152 {
153         for (int i = 0; !types[i].latex_name.empty(); ++i)
154                 if (name == types[i].latex_name)
155                         return i;
156         return 0;
157 }
158
159
160 string const & InsetRef::getName(int type)
161 {
162         return types[type].latex_name;
163 }