]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
reenable hide signal
[lyx.git] / src / insets / insetref.C
1 #include <config.h>
2
3 #include <cstdlib>
4
5 #ifdef __GNUG__
6 #pragma implementation
7 #endif
8
9 #include FORMS_H_LOCATION 
10 #include "insetref.h"
11 #include "buffer.h"
12 #include "debug.h"
13 #include "lyx_gui_misc.h" // CancelCloseBoxCB
14 #include "LyXView.h"
15 #include "lyxfunc.h"
16 #include "commandtags.h"
17 #include "gettext.h"
18 #include "LaTeXFeatures.h"
19
20 using std::ostream;
21 using std::endl;
22
23 extern BufferView * current_view;
24
25
26 InsetRef::InsetRef(string const & cmd, Buffer * bf)
27         : master(bf)
28 {
29         scanCommand(cmd);
30         GenerateFlag();
31 }
32
33
34 InsetRef::InsetRef(InsetCommand const & inscmd, Buffer * bf)
35         : master(bf)
36 {
37         setCmdName(inscmd.getCmdName());
38         setContents(inscmd.getContents());
39         setOptions(inscmd.getOptions());
40         GenerateFlag();
41 }
42
43
44 void InsetRef::GenerateFlag()
45 {
46         if (getCmdName() == "ref")
47                 flag = REF;
48         else if (getCmdName() == "pageref")
49                 flag = PAGE_REF;
50         else if (getCmdName() == "vref")
51                 flag = VREF;
52         else if (getCmdName() == "vpageref")
53                 flag = VPAGE_REF;
54         else if (getCmdName() == "prettyref")
55                 flag = PRETTY_REF;
56         else {
57                 lyxerr << "ERROR (InsetRef::GenerateFlag): Unknown command name "
58                        << getCmdName() << endl;
59                 flag = REF;
60         }
61 }
62
63
64 void InsetRef::Toggle() {
65         static string const cmd_names[REF_LAST+1] 
66                 = {"ref", "pageref", "vref", "vpageref", "prettyref"};
67         
68         if (flag == REF_LAST)
69                 flag = REF_FIRST;
70         else
71                 flag = static_cast<Ref_Flags>(flag + 1);
72         setCmdName(cmd_names[flag]);
73 }
74
75
76 void InsetRef::Edit(BufferView * bv, int, int, unsigned int)
77 {
78         bv->owner()->getLyXFunc()->
79                 Dispatch(LFUN_REFGOTO, getContents().c_str());
80 }
81
82
83 string InsetRef::getScreenLabel() const
84 {
85         static char const * labels[REF_LAST+1]
86                 = { N_("Ref: "), N_("Page: "), N_("TextRef: "), N_("TextPage: "),
87                     N_("PrettyRef: ")};
88         string temp = _(labels[flag]) + getContents();
89         if(!current_view->buffer()->isLatex()
90            && !getOptions().empty()) {
91                 temp += "||";
92                 temp += getOptions();
93         }
94         return temp;
95 }
96
97
98 int InsetRef::Latex(Buffer const *, ostream & os,
99                     bool /*fragile*/, bool /*fs*/) const
100 {
101         if(getOptions().empty())
102                 os << escape(getCommand());
103         else {
104                 string ns;
105                 InsetCommand clone(getCmdName(),
106                                    getContents(), ns);
107                 os << escape(clone.getCommand());
108         }
109         return 0;
110 }
111
112
113 int InsetRef::Ascii(Buffer const *, ostream & os) const
114 {
115         os << "[" << getContents() << "]";
116         return 0;
117 }
118
119
120 int InsetRef::Linuxdoc(Buffer const *, ostream & os) const
121 {
122         os << "<ref id=\"" << getContents()
123            << "\" name=\"" << getOptions() << "\" >";
124         return 0;
125 }
126
127
128 int InsetRef::DocBook(Buffer const *, ostream & os) const
129 {
130         os << "<link linkend=\"" << getContents()
131            << "\">" << getOptions() << "</link>";
132         return 0;
133 }
134
135
136 // This function escapes 8-bit characters and other problematic characters
137 // It's exactly the same code as in insetlabel.C.
138 string InsetRef::escape(string const & lab) const
139 {
140         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
141                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
142         string enc;
143         for (string::size_type i = 0; i < lab.length(); ++i) {
144                 unsigned char c= lab[i];
145                 if (c >= 128 || c == '=' || c == '%') {
146                         enc += '=';
147                         enc += hexdigit[c>>4];
148                         enc += hexdigit[c & 15];
149                 } else {
150                         enc += c;
151                 }
152         }
153         return enc;
154 }
155
156 void InsetRef::Validate(LaTeXFeatures & features) const
157 {
158         switch (flag) {
159         case VREF:
160         case VPAGE_REF: 
161                 features.varioref = true;
162                 break;
163         case PRETTY_REF:
164                 features.prettyref = true;
165                 break;
166         case REF:
167         case PAGE_REF:
168                 break;
169         }
170 }