]> git.lyx.org Git - lyx.git/blob - src/mathed/ref_inset.C
Add locaDispatch methods to various inset classes. refactor the LFUN_XYZ_APPLY
[lyx.git] / src / mathed / ref_inset.C
1
2 #include <config.h>
3
4 #include "ref_inset.h"
5 #include "funcrequest.h"
6 #include "formulabase.h"
7 #include "BufferView.h"
8 #include "frontends/LyXView.h"
9 #include "frontends/Painter.h"
10 #include "frontends/Dialogs.h"
11 #include "lyxfunc.h"
12 #include "gettext.h"
13 #include "LaTeXFeatures.h"
14 #include "debug.h"
15 #include "math_mathmlstream.h"
16 #include "Lsstream.h"
17 #include "math_parser.h"
18 #include "support/lstrings.h"
19
20
21 RefInset::RefInset()
22         : CommandInset("ref")
23 {}
24
25
26 RefInset::RefInset(string const & data)
27         : CommandInset(data)
28 {}
29
30
31 MathInset * RefInset::clone() const
32 {
33         return new RefInset(*this);
34 }
35
36
37 void RefInset::infoize(std::ostream & os) const
38 {
39         os << "Ref: " << cell(0);
40 }
41
42
43 dispatch_result
44 RefInset::dispatch(FuncRequest const & cmd, idx_type & idx, pos_type & pos)
45 {
46         switch (cmd.action) {
47                 case LFUN_MOUSE_RELEASE:
48                         if (cmd.button() == mouse_button::button3) {
49                                 lyxerr << "trying to goto ref" << cell(0) << "\n";
50                                 cmd.view()->dispatch(FuncRequest(LFUN_REF_GOTO, asString(cell(0))));
51                                 return DISPATCHED;
52                         }
53                         if (cmd.button() == mouse_button::button1) {
54                                 // Eventually trigger dialog with button 3
55                                 // not 1
56                                 ostringstream data;
57                                 WriteStream wsdata(data);
58                                 write(wsdata);
59                                 wsdata << "\n\\end_inset\n\n";
60
61                                 cmd.view()->owner()->getDialogs().
62                                         show("ref", data.str(), this);
63                                 return DISPATCHED;
64                         }
65                         break;
66                 case LFUN_MOUSE_PRESS:
67                 case LFUN_MOUSE_MOTION:
68                         // eat other mouse commands
69                         return DISPATCHED;
70                 default:
71                         return CommandInset::dispatch(cmd, idx, pos);
72         }
73         // not our business
74         return UNDISPATCHED;
75 }
76
77
78 string RefInset::screenLabel() const
79 {
80         string str;
81         for (int i = 0; !types[i].latex_name.empty(); ++i)
82                 if (name_ == types[i].latex_name) {
83                         str = _(types[i].short_gui_name);
84                         break;
85                 }
86         str += asString(cell(0));
87
88         //if (/* !isLatex && */ !cell(0).empty()) {
89         //      str += "||";
90         //      str += asString(cell(1));
91         //}
92         return str;
93 }
94
95
96 void RefInset::validate(LaTeXFeatures & features) const
97 {
98         if (name_ == "vref" || name_ == "vpageref")
99                 features.require("varioref");
100         else if (name_ == "prettyref")
101                 features.require("prettyref");
102 }
103
104
105 int RefInset::ascii(std::ostream & os, int) const
106 {
107         os << '[' << asString(cell(0)) << ']';
108         return 0;
109 }
110
111
112 int RefInset::linuxdoc(std::ostream & os) const
113 {
114         os << "<ref id=\"" << asString(cell(0))
115            << "\" name=\"" << asString(cell(1)) << "\" >";
116         return 0;
117 }
118
119
120 int RefInset::docbook(std::ostream & os, bool) const
121 {
122         if (cell(1).empty()) {
123                 os << "<xref linkend=\"" << asString(cell(0)) << "\">";
124         } else {
125                 os << "<link linkend=\"" << asString(cell(0))
126                    << "\">" << asString(cell(1)) << "</link>";
127         }
128
129         return 0;
130 }
131
132
133 dispatch_result RefInset::localDispatch(FuncRequest const & cmd)
134 {
135         if (cmd.action != LFUN_REF_APPLY)
136                 return UNDISPATCHED;
137
138         MathArray ar;
139         if (!string2RefInset(cmd.argument, ar))
140                 return UNDISPATCHED;
141
142         *this = *ar[0].nucleus()->asRefInset();
143 //      if (cmd.view())
144 //                 // This does not compile because updateInset expects
145 //                 // an Inset* and 'this' isn't.
146 //              cmd.view()->updateInset(this, true);
147         return DISPATCHED;
148 }
149
150
151 bool string2RefInset(string const & str, MathArray & ar)
152 {
153         // str comes with a head "LatexCommand " and a
154         // tail "\nend_inset\n\n". Strip them off.
155         string trimmed;
156         string body = split(str, trimmed, ' ');
157         split(body, trimmed, '\n');
158
159         mathed_parse_cell(ar, trimmed);
160         if (ar.size() != 1)
161                 return false;
162
163         return ar[0].nucleus()->asRefInset();
164 }
165
166
167 RefInset::ref_type_info RefInset::types[] = {
168         { "ref",        N_("Standard"),                 N_("Ref: ")},
169         { "pageref",    N_("Page Number"),              N_("Page: ")},
170         { "vpageref",   N_("Textual Page Number"),      N_("TextPage: ")},
171         { "vref",       N_("Standard+Textual Page"),    N_("Ref+Text: ")},
172         { "prettyref",  N_("PrettyRef"),                N_("PrettyRef: ")},
173         { "", "", "" }
174 };