]> git.lyx.org Git - lyx.git/blob - src/mathed/ref_inset.C
Fix event loop to no longer eat CPU
[lyx.git] / src / mathed / ref_inset.C
1 /**
2  * \file ref_inset.C
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 "ref_inset.h"
14
15 #include "BufferView.h"
16 #include "LaTeXFeatures.h"
17 #include "buffer.h"
18 #include "cursor.h"
19 #include "debug.h"
20 #include "funcrequest.h"
21 #include "FuncStatus.h"
22 #include "gettext.h"
23 #include "math_data.h"
24 #include "math_factory.h"
25 #include "math_support.h"
26 #include "outputparams.h"
27 #include "sgml.h"
28
29 #include "frontends/LyXView.h"
30 #include "frontends/Dialogs.h"
31
32 using std::string;
33 using std::auto_ptr;
34 using std::endl;
35
36
37
38 RefInset::RefInset()
39         : CommandInset("ref")
40 {}
41
42
43 RefInset::RefInset(string const & data)
44         : CommandInset(data)
45 {}
46
47
48 auto_ptr<InsetBase> RefInset::doClone() const
49 {
50         return auto_ptr<InsetBase>(new RefInset(*this));
51 }
52
53
54 void RefInset::infoize(std::ostream & os) const
55 {
56         os << "Ref: " << cell(0);
57 }
58
59
60 void RefInset::doDispatch(LCursor & cur, FuncRequest & cmd)
61 {
62         switch (cmd.action) {
63         case LFUN_INSET_MODIFY:
64                 if (cmd.getArg(0) == "ref") {
65                         MathArray ar;
66                         if (createMathInset_fromDialogStr(cmd.argument, ar)) {
67                                 *this = *ar[0].nucleus()->asRefInset();
68                                 break;
69                         }
70                 }
71                 cur.undispatched();
72                 break;
73
74         case LFUN_INSET_DIALOG_UPDATE: {
75                 string const data = createDialogStr("ref");
76                 if (cur.bv().owner()->getDialogs().visible("ref"))
77                         cur.bv().owner()->getDialogs().update("ref", data);
78                 break;
79         }
80
81         case LFUN_INSET_DIALOG_SHOW:
82         case LFUN_MOUSE_RELEASE:
83                 if (cmd.button() == mouse_button::button3) {
84                         lyxerr << "trying to goto ref '" << asString(cell(0)) << "'" << endl;
85                         cur.bv().dispatch(FuncRequest(LFUN_LABEL_GOTO, asString(cell(0))));
86                         break;
87                 }
88                 if (cmd.button() == mouse_button::button1) {
89                         // Eventually trigger dialog with button 3, not 1
90                         string const data = createDialogStr("ref");
91                         cur.bv().owner()->getDialogs().show("ref", data, this);
92                         break;
93                 }
94                 cur.undispatched();
95                 break;
96
97         case LFUN_MOUSE_PRESS:
98         case LFUN_MOUSE_MOTION:
99                 // eat other mouse commands
100                 break;
101
102         default:
103                 CommandInset::doDispatch(cur, cmd);
104                 break;
105         }
106 }
107
108
109 bool RefInset::getStatus(LCursor & cur, FuncRequest const & cmd,
110                          FuncStatus & status) const
111 {
112         switch (cmd.action) {
113         // we handle these
114         case LFUN_INSET_MODIFY:
115         case LFUN_INSET_DIALOG_UPDATE:
116         case LFUN_INSET_DIALOG_SHOW:
117         case LFUN_MOUSE_RELEASE:
118         case LFUN_MOUSE_PRESS:
119         case LFUN_MOUSE_MOTION:
120                 status.enabled(true);
121                 return true;
122         default:
123                 return CommandInset::getStatus(cur, cmd, status);
124         }
125 }
126
127
128 string const RefInset::screenLabel() const
129 {
130         string str;
131         for (int i = 0; !types[i].latex_name.empty(); ++i)
132                 if (commandname() == types[i].latex_name) {
133                         str = _(types[i].short_gui_name);
134                         break;
135                 }
136         str += asString(cell(0));
137
138         //if (/* !isLatex && */ !cell(0).empty()) {
139         //      str += "||";
140         //      str += asString(cell(1));
141         //}
142         return str;
143 }
144
145
146 void RefInset::validate(LaTeXFeatures & features) const
147 {
148         if (commandname() == "vref" || commandname() == "vpageref")
149                 features.require("varioref");
150         else if (commandname() == "prettyref")
151                 features.require("prettyref");
152 }
153
154
155 int RefInset::plaintext(std::ostream & os, OutputParams const &) const
156 {
157         os << '[' << asString(cell(0)) << ']';
158         return 0;
159 }
160
161
162 int RefInset::linuxdoc(std::ostream & os, OutputParams const &) const
163 {
164         os << "<ref id=\"" << asString(cell(0))
165            << "\" name=\"" << asString(cell(1)) << "\">";
166         return 0;
167 }
168
169
170 int RefInset::docbook(Buffer const & buf, std::ostream & os, OutputParams const & runparams) const
171 {
172         if (cell(1).empty()) {
173                 os << "<xref linkend=\"" << sgml::cleanID(buf, runparams, asString(cell(0)));
174                 if (runparams.flavor == OutputParams::XML)
175                         os << "\"/>";
176                 else
177                         os << "\">";
178         } else {
179                 os << "<link linkend=\"" << sgml::cleanID(buf, runparams, asString(cell(0)))
180                    << "\">" << asString(cell(1)) << "</link>";
181         }
182
183         return 0;
184 }
185
186
187 RefInset::ref_type_info RefInset::types[] = {
188         { "ref",       N_("Standard"),              N_("Ref: ")},
189         { "eqref",     N_("Equation"),              N_("EqRef: ")},
190         { "pageref",   N_("Page Number"),           N_("Page: ")},
191         { "vpageref",  N_("Textual Page Number"),   N_("TextPage: ")},
192         { "vref",      N_("Standard+Textual Page"), N_("Ref+Text: ")},
193         { "prettyref", N_("PrettyRef"),             N_("PrettyRef: ")},
194         { "", "", "" }
195 };
196