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