]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Fix text frame drawing.
[lyx.git] / src / insets / InsetCharStyle.cpp
1 /**
2  * \file InsetCharStyle.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  * \author Martin Vermeer
8  * \author Jürgen Spitzmüller
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCharStyle.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "DispatchResult.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "Cursor.h"
24 #include "gettext.h"
25 #include "Color.h"
26 #include "Lexer.h"
27 #include "Text.h"
28 #include "MetricsInfo.h"
29 #include "Paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "sgml.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
36 #include "support/convert.h"
37
38 #include <sstream>
39
40
41 namespace lyx {
42
43 using std::string;
44 using std::ostream;
45
46
47 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
48         : InsetCollapsable(bp, Collapsed)
49 {
50         params_.name = s;
51 }
52
53
54 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
55                                 InsetLayout il)
56         : InsetCollapsable(bp, Collapsed)
57 {
58         params_.name = il.name;
59         setLayout(il);
60 }
61
62
63 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
64         : InsetCollapsable(in), params_(in.params_)
65 {}
66
67
68 Inset * InsetCharStyle::clone() const
69 {
70         return new InsetCharStyle(*this);
71 }
72
73
74 bool InsetCharStyle::undefined() const
75 {
76         return layout_.labelstring == from_utf8("UNDEFINED");
77 }
78
79
80 void InsetCharStyle::setLayout(InsetLayout il)
81 {
82         layout_ = il;
83 }
84
85
86 docstring const InsetCharStyle::editMessage() const
87 {
88         return _("Opened CharStyle Inset");
89 }
90
91
92 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
93 {
94         params_.write(os);
95         InsetCollapsable::write(buf, os);
96 }
97
98
99 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
100 {
101         params_.read(lex);
102         InsetCollapsable::read(buf, lex);
103 }
104
105
106 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
107 {
108         Font tmpfont = mi.base.font;
109         getDrawFont(mi.base.font);
110         mi.base.font.reduce(Font(Font::ALL_SANE));
111         mi.base.font.realize(tmpfont);
112         bool changed = InsetCollapsable::metrics(mi, dim);
113         mi.base.font = tmpfont;
114         return changed;
115 }
116
117
118 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
119 {
120         setPosCache(pi, x, y);
121
122         Font tmpfont = pi.base.font;
123         getDrawFont(pi.base.font);
124         // I don't understand why the above .reduce and .realize aren't
125         //needed, or even wanted, here. It just works. -- MV 10.04.2005
126         InsetCollapsable::draw(pi, x, y);
127         pi.base.font = tmpfont;
128 }
129
130
131 void InsetCharStyle::getDrawFont(Font & font) const
132 {
133         font = layout_.font;
134 }
135
136
137 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
138 {
139         InsetCollapsable::doDispatch(cur, cmd);
140 }
141
142
143 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
144         FuncStatus & status) const
145 {
146         switch (cmd.action) {
147                 // paragraph breaks not allowed in charstyle insets
148                 case LFUN_BREAK_PARAGRAPH:
149                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
150                 case LFUN_BREAK_PARAGRAPH_SKIP:
151                         status.enabled(false);
152                         return true;
153
154                 default:
155                         return InsetCollapsable::getStatus(cur, cmd, status);
156                 }
157 }
158
159
160 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
161                               OutputParams const & runparams) const
162 {
163         return InsetText::plaintext(buf, os, runparams);
164 }
165
166
167 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
168                             OutputParams const & runparams) const
169 {
170         ParagraphList::const_iterator beg = paragraphs().begin();
171         ParagraphList::const_iterator par = paragraphs().begin();
172         ParagraphList::const_iterator end = paragraphs().end();
173
174         if (!undefined())
175                 // FIXME UNICODE
176                 sgml::openTag(os, layout_.latexname,
177                               par->getID(buf, runparams) + layout_.latexparam);
178
179         for (; par != end; ++par) {
180                 par->simpleDocBookOnePar(buf, os, runparams,
181                                          outerFont(std::distance(beg, par),
182                                                    paragraphs()));
183         }
184
185         if (!undefined())
186                 sgml::closeTag(os, layout_.latexname);
187
188         return 0;
189 }
190
191
192 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
193 {
194         os << paragraphs().begin()->asString(buf, true);
195 }
196
197
198 void InsetCharStyleParams::write(ostream & os) const
199 {
200         os << "CharStyle " << name << "\n";
201 }
202
203
204 void InsetCharStyleParams::read(Lexer & lex)
205 {
206         while (lex.isOK()) {
207                 lex.next();
208                 string token = lex.getString();
209
210                 if (token == "CharStyle") {
211                         lex.next();
212                         name = lex.getString();
213                 }
214
215                 // This is handled in Collapsable
216                 else if (token == "status") {
217                         lex.pushToken(token);
218                         break;
219                 }
220         }
221 }
222
223
224 } // namespace lyx