]> git.lyx.org Git - lyx.git/blob - src/insets/insetref.C
fc639e95475586e4e5a3005a43c107045d7ce1e5
[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
19 extern BufferView * current_view;
20
21
22 InsetRef::InsetRef(string const & cmd, Buffer * bf)
23         : master(bf)
24 {
25         scanCommand(cmd);
26         if (getCmdName() == "ref")
27                 flag = InsetRef::REF;
28         else
29                 flag = InsetRef::PAGE_REF;
30 }
31
32
33 InsetRef::InsetRef(InsetCommand const & inscmd, Buffer * bf)
34         : master(bf)
35 {
36         setCmdName(inscmd.getCmdName());
37         setContents(inscmd.getContents());
38         setOptions(inscmd.getOptions());
39         if (getCmdName() == "ref")
40                 flag = InsetRef::REF;
41         else
42                 flag = InsetRef::PAGE_REF;
43 }
44
45
46 InsetRef::~InsetRef()
47 {
48 }
49
50
51 void InsetRef::Edit(int, int)
52 {
53         current_view->owner()->getLyXFunc()
54                 ->Dispatch(LFUN_REFGOTO, getContents().c_str());
55 }
56
57
58 string InsetRef::getScreenLabel() const
59 {
60         string temp;
61         if (flag == InsetRef::PAGE_REF)
62                 temp += _("Page: ");
63         else 
64                 temp += _("Ref: ");
65         temp += getContents();
66         if(!current_view->buffer()->isLatex()
67            && !getOptions().empty()) {
68                 temp += "||";
69                 temp += getOptions();
70         }
71         return temp;
72 }
73
74
75 int InsetRef::Latex(FILE * file, signed char /*fragile*/)
76 {
77         if(getOptions().empty())
78                 fprintf(file, "%s", escape(getCommand()).c_str());
79         else {
80                 string ns;
81                 InsetCommand clone = InsetCommand(getCmdName(), getContents(), ns);
82                 fprintf(file, "%s", escape(clone.getCommand()).c_str());
83         }
84         return 0;
85 }
86
87
88 int InsetRef::Latex(string & file, signed char /*fragile*/)
89 {
90         if(getOptions().empty())
91                 file += escape(getCommand());
92         else {
93                 string ns;
94                 InsetCommand clone= InsetCommand(getCmdName(), getContents(), ns);
95                 file += escape(clone.getCommand());
96         }
97         return 0;
98 }
99
100
101 int InsetRef::Linuxdoc(string & file)
102 {
103         file += "<ref id=\"" + getContents()
104                 + "\" name=\""+ getOptions() +"\" >" ;
105
106         return 0;
107 }
108
109
110 int InsetRef::DocBook(string & file)
111 {
112         file += "<link linkend=\"" + getContents()
113                 + "\">"+ getOptions() +"</link>" ;
114
115         return 0;
116 }
117
118
119 // This function escapes 8-bit characters and other problematic characters
120 // It's exactly the same code as in insetlabel.C.
121 string InsetRef::escape(string const & lab) const {
122         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
123                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
124         string enc;
125         for (string::size_type i = 0; i < lab.length(); ++i) {
126                 unsigned char c= lab[i];
127                 if (c >= 128 || c == '=' || c == '%') {
128                         enc += '=';
129                         enc += hexdigit[c>>4];
130                         enc += hexdigit[c & 15];
131                 } else {
132                         enc += c;
133                 }
134         }
135         return enc;
136 }