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