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