]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
32e167cedad9c5cedbf824e56af4600db9180a59
[lyx.git] / src / insets / insetlabel.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1995 Matthias Ettrich
7  *          Copyright 1995-2000 The LyX Team.
8  *
9  * ====================================================== */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetlabel.h"
18 #include "support/LOstream.h"
19 #include "lyx_gui_misc.h"     //askForText
20 #include "support/lstrings.h" //frontStrip, strip
21 #include "lyxtext.h"
22 #include "buffer.h"
23
24 using std::ostream;
25 using std::vector;
26 using std::pair;
27
28 /* Label. Used to insert a label automatically */
29
30
31 InsetLabel::InsetLabel(string const & cmd)
32 {
33         scanCommand(cmd);
34 }
35
36
37 Inset * InsetLabel::Clone() const
38 {
39         return new InsetLabel(getCommand());
40 }
41
42
43 vector<string> InsetLabel::getLabelList() const
44 {
45         return vector<string>(1,contents);
46 }
47
48
49 void InsetLabel::Edit(BufferView * bv, int, int, unsigned int)
50 {
51         if(bv->buffer()->isReadonly()) {
52                 WarnReadonly(bv->buffer()->fileName());
53                 return;
54         }
55
56         pair<bool, string> result = askForText(_("Enter label:"),
57                                                contents);
58         if (result.first) {
59                 string new_contents = frontStrip(strip(result.second));
60                 if (!new_contents.empty() &&
61                     contents != new_contents) {
62                         bool flag = bv->ChangeRefs(contents,new_contents);
63                         contents = new_contents;
64                         bv->text->RedoParagraph();
65                         if (flag) {
66                                 bv->redraw();
67                                 bv->fitCursor();
68                                 //bv->updateScrollbar();
69                         } else
70                                 bv->update(1);
71                 }
72         }
73 }
74
75 int InsetLabel::Latex(ostream & os,
76                       bool /*fragile*/, bool /*fs*/) const
77 {
78         os << escape(getCommand());
79         return 0;
80 }
81
82 int InsetLabel::Ascii(ostream & os) const
83 {
84         os << "<" << getContents()  << ">";
85         return 0;
86 }
87
88
89 int InsetLabel::Linuxdoc(ostream & os) const
90 {
91         os << "<label id=\"" << getContents() << "\" >";
92         return 0;
93 }
94
95
96 int InsetLabel::DocBook(ostream & os) const
97 {
98         os << "<anchor id=\"" << getContents() << "\" >";
99         return 0;
100 }
101
102
103 // This function escapes 8-bit characters and other problematic characters
104 // It's exactly the same code as in insetref.C.
105 string InsetLabel::escape(string const & lab) const {
106         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
107                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
108         string enc;
109         for (string::size_type i= 0; i < lab.length(); ++i) {
110                 unsigned char c = lab[i];
111                 if (c >= 128 || c == '=' || c == '%') {
112                         enc += '=';
113                         enc += hexdigit[c >> 4];
114                         enc += hexdigit[c & 15];
115                 } else {
116                         enc += c;
117                 }
118         }
119         return enc;
120 }