]> git.lyx.org Git - lyx.git/blob - src/mathed/InsetMathRef.cpp
aa93b220db2519e3c9873d38e7ed4b4354a0884e
[lyx.git] / src / mathed / InsetMathRef.cpp
1 /**
2  * \file InsetMathRef.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetMathRef.h"
14
15 #include "BufferView.h"
16 #include "LaTeXFeatures.h"
17 #include "Buffer.h"
18 #include "Cursor.h"
19 #include "FuncRequest.h"
20 #include "FuncStatus.h"
21 #include "MathData.h"
22 #include "MathFactory.h"
23 #include "MathSupport.h"
24 #include "OutputParams.h"
25 #include "sgml.h"
26
27 #include "insets/InsetCommand.h"
28
29 #include "support/debug.h"
30 #include "support/gettext.h"
31
32 using namespace std;
33
34 namespace lyx {
35
36 InsetMathRef::InsetMathRef()
37         : CommandInset(from_ascii("ref"))
38 {}
39
40
41 InsetMathRef::InsetMathRef(docstring const & data)
42         : CommandInset(data)
43 {}
44
45
46 Inset * InsetMathRef::clone() const
47 {
48         return new InsetMathRef(*this);
49 }
50
51
52 void InsetMathRef::infoize(odocstream & os) const
53 {
54         os << "Ref: " << cell(0);
55 }
56
57
58 void InsetMathRef::doDispatch(Cursor & cur, FuncRequest & cmd)
59 {
60         switch (cmd.action) {
61         case LFUN_INSET_MODIFY:
62                 if (cmd.getArg(0) == "ref") {
63                         MathData ar;
64                         if (createInsetMath_fromDialogStr(cmd.argument(), ar)) {
65                                 *this = *ar[0].nucleus()->asRefInset();
66                                 break;
67                         }
68                 }
69                 cur.undispatched();
70                 break;
71
72         case LFUN_INSET_DIALOG_UPDATE: {
73                 string const data = createDialogStr("ref");
74                 cur.bv().updateDialog("ref", data);
75                 break;
76         }
77
78         case LFUN_MOUSE_RELEASE:
79                 if (cmd.button() == mouse_button::button3) {
80                         LYXERR0("trying to goto ref '" << to_utf8(asString(cell(0))) << "'");
81                         cur.bv().dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
82                         break;
83                 }
84                 if (cmd.button() == mouse_button::button1) {
85                         // Eventually trigger dialog with button 3, not 1
86                         string const data = createDialogStr("ref");
87                         cur.bv().showDialog("ref", data, this);
88                         break;
89                 }
90                 cur.undispatched();
91                 break;
92
93         case LFUN_MOUSE_PRESS:
94         case LFUN_MOUSE_MOTION:
95                 // eat other mouse commands
96                 break;
97
98         default:
99                 CommandInset::doDispatch(cur, cmd);
100                 break;
101         }
102 }
103
104
105 bool InsetMathRef::getStatus(Cursor & cur, FuncRequest const & cmd,
106                          FuncStatus & status) const
107 {
108         switch (cmd.action) {
109         // we handle these
110         case LFUN_INSET_MODIFY:
111         case LFUN_INSET_DIALOG_UPDATE:
112         case LFUN_MOUSE_RELEASE:
113         case LFUN_MOUSE_PRESS:
114         case LFUN_MOUSE_MOTION:
115                 status.enabled(true);
116                 return true;
117         default:
118                 return CommandInset::getStatus(cur, cmd, status);
119         }
120 }
121
122
123 docstring const InsetMathRef::screenLabel() const
124 {
125         docstring str;
126         for (int i = 0; !types[i].latex_name.empty(); ++i) {
127                 if (commandname() == types[i].latex_name) {
128                         str = _(to_utf8(types[i].short_gui_name));
129                         break;
130                 }
131         }
132         str += asString(cell(0));
133
134         //if (/* !isLatex && */ !cell(0).empty()) {
135         //      str += "||";
136         //      str += asString(cell(1));
137         //}
138         return str;
139 }
140
141
142 void InsetMathRef::validate(LaTeXFeatures & features) const
143 {
144         if (commandname() == "vref" || commandname() == "vpageref")
145                 features.require("varioref");
146         else if (commandname() == "prettyref")
147                 features.require("prettyref");
148 }
149
150
151 int InsetMathRef::docbook(odocstream & os, OutputParams const & runparams) const
152 {
153         if (cell(1).empty()) {
154                 os << "<xref linkend=\""
155                    << sgml::cleanID(buffer(), runparams, asString(cell(0)));
156                 if (runparams.flavor == OutputParams::XML)
157                         os << "\"/>";
158                 else
159                         os << "\">";
160         } else {
161                 os << "<link linkend=\""
162                    << sgml::cleanID(buffer(), runparams, asString(cell(0)))
163                    << "\">"
164                    << asString(cell(1))
165                    << "</link>";
166         }
167
168         return 0;
169 }
170
171
172 string const InsetMathRef::createDialogStr(string const & name) const
173 {
174         InsetCommandParams icp(REF_CODE, to_ascii(commandname()));
175         icp["reference"] = asString(cell(0));
176         if (!cell(1).empty())
177                 icp["name"] = asString(cell(1));
178         return InsetCommand::params2string(name, icp);
179 }
180
181
182 InsetMathRef::ref_type_info InsetMathRef::types[] = {
183         { from_ascii("ref"),       from_ascii(N_("Standard[[mathref]]")),              from_ascii(N_("Ref: "))},
184         { from_ascii("eqref"),     from_ascii(N_("Equation")),              from_ascii(N_("EqRef: "))},
185         { from_ascii("pageref"),   from_ascii(N_("Page Number")),           from_ascii(N_("Page: "))},
186         { from_ascii("vpageref"),  from_ascii(N_("Textual Page Number")),   from_ascii(N_("TextPage: "))},
187         { from_ascii("vref"),      from_ascii(N_("Standard+Textual Page")), from_ascii(N_("Ref+Text: "))},
188         { from_ascii("prettyref"), from_ascii(N_("PrettyRef")),             from_ascii(N_("FormatRef: "))},
189         { from_ascii(""), from_ascii(""), from_ascii("") }
190 };
191
192
193 } // namespace lyx