1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1995 Matthias Ettrich
7 * Copyright 1995-2000 The LyX Team.
9 * ====================================================== */
14 #pragma implementation
17 #include "insetlabel.h"
18 #include "support/LOstream.h"
19 #include "lyx_gui_misc.h" //askForText
20 #include "support/lstrings.h" //frontStrip, strip
28 /* Label. Used to insert a label automatically */
31 InsetLabel::InsetLabel(InsetCommandParams const & p)
36 vector<string> const InsetLabel::getLabelList() const
38 return vector<string>(1,getContents());
42 void InsetLabel::Edit(BufferView * bv, int, int, unsigned int)
44 if (bv->buffer()->isReadonly()) {
45 WarnReadonly(bv->buffer()->fileName());
49 pair<bool, string> result = askForText(_("Enter label:"), getContents());
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->ChangeRefsIfUnique(getContents(),
57 setContents(new_contents);
58 bv->text->RedoParagraph(bv);
61 bv->fitCursor(getLyXText(bv));
63 bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
69 int InsetLabel::Latex(Buffer const *, ostream & os,
70 bool /*fragile*/, bool /*fs*/) const
72 os << escape(getCommand());
76 int InsetLabel::Ascii(Buffer const *, ostream & os, int) const
78 os << "<" << getContents() << ">";
83 int InsetLabel::Linuxdoc(Buffer const *, ostream & os) const
85 os << "<label id=\"" << getContents() << "\" >";
90 int InsetLabel::DocBook(Buffer const *, ostream & os) const
92 os << "<anchor id=\"" << getContents() << "\" ></anchor>";
97 // This function escapes 8-bit characters and other problematic characters
98 // It's exactly the same code as in insetref.C.
99 string const InsetLabel::escape(string const & lab) const {
100 char hexdigit[16] = { '0', '1', '2', '3', '4', '5', '6', '7',
101 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
103 for (string::size_type i= 0; i < lab.length(); ++i) {
104 unsigned char c = lab[i];
105 if (c >= 128 || c == '=' || c == '%') {
107 enc += hexdigit[c >> 4];
108 enc += hexdigit[c & 15];