]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
The speed patch: redraw only rows that have changed
[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 "BufferView.h"
17 #include "dispatchresult.h"
18 #include "funcrequest.h"
19 #include "gettext.h"
20 #include "LaTeXFeatures.h"
21 #include "outputparams.h"
22 #include "sgml.h"
23
24 #include "frontends/LyXView.h"
25
26 #include "support/lstrings.h"
27
28
29 using lyx::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(LCursor & 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                         cur.bv().owner()->dispatch(FuncRequest(LFUN_LABEL_GOTO, getContents()));
52                 else
53                         InsetCommandMailer("ref", *this).showDialog(&cur.bv());
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                         temp = _(types[i].short_gui_name);
71                         break;
72                 }
73         }
74         temp += getContents();
75
76         if (!isLatex && !getOptions().empty()) {
77                 temp += "||";
78                 temp += getOptions();
79         }
80         return temp;
81 }
82
83
84 int InsetRef::latex(Buffer const &, ostream & os,
85                     OutputParams const &) const
86 {
87         if (getOptions().empty())
88                 os << escape(getCommand());
89         else {
90                 InsetCommandParams p(getCmdName(), getContents(), "");
91                 os << escape(p.getCommand());
92         }
93         return 0;
94 }
95
96
97 int InsetRef::plaintext(Buffer const &, ostream & os,
98                     OutputParams const &) const
99 {
100         os << '[' << getContents() << ']';
101         return 0;
102 }
103
104
105 int InsetRef::linuxdoc(Buffer const & buf, ostream & os,
106                        OutputParams const & runparams) const
107 {
108         os << "<ref id=\"" << sgml::cleanID(buf, runparams, getContents())
109            << "\" name=\"" << getOptions() << "\" >";
110         return 0;
111 }
112
113
114 int InsetRef::docbook(Buffer const & buf, ostream & os,
115                       OutputParams const & runparams) const
116 {
117         if (getOptions().empty() && runparams.flavor == OutputParams::XML) {
118                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\" />";
119         } else if (getOptions().empty()) {
120                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, getContents()) << "\">";
121         } else {
122                 os << "<link linkend=\"" << sgml::cleanID(buf, runparams, getContents())
123                    << "\">" << getOptions() << "</link>";
124         }
125
126         return 0;
127 }
128
129
130 int InsetRef::textString(Buffer const & buf, ostream & os,
131                        OutputParams const & op) const
132 {
133         return plaintext(buf, os, op);
134 }
135
136
137 void InsetRef::validate(LaTeXFeatures & features) const
138 {
139         if (getCmdName() == "vref" || getCmdName() == "vpageref")
140                 features.require("varioref");
141         else if (getCmdName() == "prettyref")
142                 features.require("prettyref");
143         else if (getCmdName() == "eqref")
144                 features.require("amsmath");
145 }
146
147
148 InsetRef::type_info InsetRef::types[] = {
149         { "ref",       N_("Standard"),              N_("Ref: ")},
150         { "eqref",     N_("Equation"),              N_("EqRef: ")},
151         { "pageref",   N_("Page Number"),           N_("Page: ")},
152         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
153         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
154         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
155         { "", "", "" }
156 };
157
158
159 int InsetRef::getType(string const & name)
160 {
161         for (int i = 0; !types[i].latex_name.empty(); ++i)
162                 if (name == types[i].latex_name)
163                         return i;
164         return 0;
165 }
166
167
168 string const & InsetRef::getName(int type)
169 {
170         return types[type].latex_name;
171 }