]> git.lyx.org Git - lyx.git/blob - src/insets/InsetERT.cpp
Cocoa based Qt-4.6 needs to paint every character separately to match metrics computa...
[lyx.git] / src / insets / InsetERT.cpp
1 /**
2  * \file InsetERT.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  * \author Lars Gullik Bjønnes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetERT.h"
15
16 #include "Buffer.h"
17 #include "BufferParams.h"
18 #include "BufferView.h"
19 #include "Cursor.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Language.h"
24 #include "Layout.h"
25 #include "Lexer.h"
26 #include "LyXAction.h"
27 #include "OutputParams.h"
28 #include "ParagraphParameters.h"
29 #include "Paragraph.h"
30 #include "TextClass.h"
31
32 #include "support/gettext.h"
33 #include "support/lstrings.h"
34
35 #include <sstream>
36
37 using namespace std;
38 using namespace lyx::support;
39
40 namespace lyx {
41
42 InsetERT::InsetERT(Buffer * buf, CollapseStatus status)
43         : InsetCollapsable(buf)
44 {
45         status_ = status;
46 }
47
48
49 void InsetERT::write(ostream & os) const
50 {
51         os << "ERT" << "\n";
52         InsetCollapsable::write(os);
53 }
54
55
56 int InsetERT::plaintext(odocstream & os, OutputParams const & rp) const
57 {
58         if (!rp.inIndexEntry)
59                 // do not output TeX code
60                 return 0;
61
62         ParagraphList::const_iterator par = paragraphs().begin();
63         ParagraphList::const_iterator end = paragraphs().end();
64
65         while (par != end) {
66                 pos_type siz = par->size();
67                 for (pos_type i = 0; i < siz; ++i) {
68                         char_type const c = par->getChar(i);
69                         // output the active characters
70                         switch (c) {
71                         case '|':
72                         case '!':
73                         case '@':
74                                 os.put(c);
75                                 break;
76                         default:
77                                 break;
78                         }
79                 }
80                 ++par;
81         }
82         return 0;
83 }
84
85
86 int InsetERT::docbook(odocstream & os, OutputParams const &) const
87 {
88         // FIXME can we do the same thing here as for LaTeX?
89         ParagraphList::const_iterator par = paragraphs().begin();
90         ParagraphList::const_iterator end = paragraphs().end();
91
92         int lines = 0;
93         while (par != end) {
94                 pos_type siz = par->size();
95                 for (pos_type i = 0; i < siz; ++i)
96                         os.put(par->getChar(i));
97                 ++par;
98                 if (par != end) {
99                         os << "\n";
100                         ++lines;
101                 }
102         }
103
104         return lines;
105 }
106
107
108 void InsetERT::doDispatch(Cursor & cur, FuncRequest & cmd)
109 {
110         switch (cmd.action()) {
111         case LFUN_INSET_MODIFY:
112                 if (cmd.getArg(0) == "ert") {
113                         cur.recordUndoInset(ATOMIC_UNDO, this);
114                         setStatus(cur, string2params(to_utf8(cmd.argument())));
115                         break;
116                 }
117                 //fall-through
118         default:
119                 InsetCollapsable::doDispatch(cur, cmd);
120                 break;
121         }
122
123 }
124
125
126 bool InsetERT::getStatus(Cursor & cur, FuncRequest const & cmd,
127         FuncStatus & status) const
128 {
129         switch (cmd.action()) {
130         case LFUN_INSET_MODIFY:
131                 if (cmd.getArg(0) == "ert") {
132                         status.setEnabled(true);
133                         return true;
134                 }
135                 //fall through
136
137         default:
138                 return InsetCollapsable::getStatus(cur, cmd, status);
139         }
140 }
141
142
143 docstring const InsetERT::buttonLabel(BufferView const & bv) const
144 {
145         if (decoration() == InsetLayout::CLASSIC)
146                 return isOpen(bv) ? _("ERT") : getNewLabel(_("ERT"));
147         else
148                 return getNewLabel(_("ERT"));
149 }
150
151
152 InsetCollapsable::CollapseStatus InsetERT::string2params(string const & in)
153 {
154         if (in.empty())
155                 return Collapsed;
156         istringstream data(in);
157         Lexer lex;
158         lex.setStream(data);
159         lex.setContext("InsetERT::string2params");
160         lex >> "ert";
161         int s;
162         lex >> s;
163         return static_cast<CollapseStatus>(s);
164 }
165
166
167 string InsetERT::params2string(CollapseStatus status)
168 {
169         ostringstream data;
170         data << "ert" << ' ' << status;
171         return data.str();
172 }
173
174
175 docstring InsetERT::xhtml(XHTMLStream &, OutputParams const &) const
176 {
177         return docstring();
178 }
179
180 } // namespace lyx