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