]> git.lyx.org Git - lyx.git/blob - src/insets/insetlabel.C
prepare for 1.1.6pre2
[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(InsetCommandParams const & p)
32         : InsetCommand(p)
33 {}
34
35
36 vector<string> const InsetLabel::getLabelList() const
37 {
38         return vector<string>(1,getContents());
39 }
40
41
42 void InsetLabel::Edit(BufferView * bv, int, int, unsigned int)
43 {
44         if (bv->buffer()->isReadonly()) {
45                 WarnReadonly(bv->buffer()->fileName());
46                 return;
47         }
48
49         pair<bool, string> result = askForText(_("Enter label:"), getContents());
50         if (result.first) {
51                 string new_contents = frontStrip(strip(result.second));
52                 if (!new_contents.empty() &&
53                     getContents() != new_contents) {
54                         bv->buffer()->markDirty();
55                         bool flag = bv->ChangeRefs(getContents(),new_contents);
56                         setContents( new_contents );
57                         bv->text->RedoParagraph(bv);
58                         if (flag) {
59                                 bv->redraw();
60                                 bv->fitCursor(getLyXText(bv));
61                         } else
62                                 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
63                 }
64         }
65 }
66
67
68 int InsetLabel::Latex(Buffer const *, ostream & os,
69                       bool /*fragile*/, bool /*fs*/) const
70 {
71         os << escape(getCommand());
72         return 0;
73 }
74
75 int InsetLabel::Ascii(Buffer const *, ostream & os, int) const
76 {
77         os << "<" << getContents()  << ">";
78         return 0;
79 }
80
81
82 int InsetLabel::Linuxdoc(Buffer const *, ostream & os) const
83 {
84         os << "<label id=\"" << getContents() << "\" >";
85         return 0;
86 }
87
88
89 int InsetLabel::DocBook(Buffer const *, ostream & os) const
90 {
91         os << "<anchor id=\"" << getContents() << "\" ></anchor>";
92         return 0;
93 }
94
95
96 // This function escapes 8-bit characters and other problematic characters
97 // It's exactly the same code as in insetref.C.
98 string const InsetLabel::escape(string const & lab) const {
99         char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
100                               '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
101         string enc;
102         for (string::size_type i= 0; i < lab.length(); ++i) {
103                 unsigned char c = lab[i];
104                 if (c >= 128 || c == '=' || c == '%') {
105                         enc += '=';
106                         enc += hexdigit[c >> 4];
107                         enc += hexdigit[c & 15];
108                 } else {
109                         enc += c;
110                 }
111         }
112         return enc;
113 }