]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Finally enable single row repainting within insets! The inset width needs to stay...
[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::auto_ptr;
45 using std::istringstream;
46 using std::max;
47 using std::ostream;
48 using std::ostringstream;
49
50
51
52 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
53         : InsetCollapsable(bp, Collapsed)
54 {
55         params_.name = s;
56 }
57
58
59 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
60                                 InsetLayout il)
61         : InsetCollapsable(bp, Collapsed)
62 {
63         params_.name = il.name;
64         setLayout(il);
65 }
66
67
68 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
69         : InsetCollapsable(in), params_(in.params_)
70 {}
71
72
73 auto_ptr<Inset> InsetCharStyle::doClone() const
74 {
75         return auto_ptr<Inset>(new InsetCharStyle(*this));
76 }
77
78
79 bool InsetCharStyle::undefined() const
80 {
81         return layout_.labelstring == from_utf8("UNDEFINED");
82 }
83
84
85 void InsetCharStyle::setLayout(InsetLayout il)
86 {
87         layout_ = il;
88 }
89
90
91 docstring const InsetCharStyle::editMessage() const
92 {
93         return _("Opened CharStyle Inset");
94 }
95
96
97 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
98 {
99         params_.write(os);
100         InsetCollapsable::write(buf, os);
101 }
102
103
104 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
105 {
106         params_.read(lex);
107         InsetCollapsable::read(buf, lex);
108 }
109
110
111 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
112 {
113         Font tmpfont = mi.base.font;
114         getDrawFont(mi.base.font);
115         mi.base.font.reduce(Font(Font::ALL_SANE));
116         mi.base.font.realize(tmpfont);
117         bool changed = InsetCollapsable::metrics(mi, dim);
118         mi.base.font = tmpfont;
119         return changed;
120 }
121
122
123 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
124 {
125         setPosCache(pi, x, y);
126
127         Font tmpfont = pi.base.font;
128         getDrawFont(pi.base.font);
129         // I don't understand why the above .reduce and .realize aren't
130         //needed, or even wanted, here. It just works. -- MV 10.04.2005
131         InsetCollapsable::draw(pi, x, y);
132         pi.base.font = tmpfont;
133 }
134
135
136 void InsetCharStyle::getDrawFont(Font & font) const
137 {
138         font = layout_.font;
139 }
140
141
142 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
143 {
144         InsetCollapsable::doDispatch(cur, cmd);
145 }
146
147
148 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
149         FuncStatus & status) const
150 {
151         switch (cmd.action) {
152                 // paragraph breaks not allowed in charstyle insets
153                 case LFUN_BREAK_PARAGRAPH:
154                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
155                 case LFUN_BREAK_PARAGRAPH_SKIP:
156                         status.enabled(false);
157                         return true;
158
159                 default:
160                         return InsetCollapsable::getStatus(cur, cmd, status);
161                 }
162 }
163
164
165 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
166                               OutputParams const & runparams) const
167 {
168         return InsetText::plaintext(buf, os, runparams);
169 }
170
171
172 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
173                             OutputParams const & runparams) const
174 {
175         ParagraphList::const_iterator beg = paragraphs().begin();
176         ParagraphList::const_iterator par = paragraphs().begin();
177         ParagraphList::const_iterator end = paragraphs().end();
178
179         if (!undefined())
180                 // FIXME UNICODE
181                 sgml::openTag(os, layout_.latexname,
182                               par->getID(buf, runparams) + layout_.latexparam);
183
184         for (; par != end; ++par) {
185                 par->simpleDocBookOnePar(buf, os, runparams,
186                                          outerFont(std::distance(beg, par),
187                                                    paragraphs()));
188         }
189
190         if (!undefined())
191                 sgml::closeTag(os, layout_.latexname);
192
193         return 0;
194 }
195
196
197 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
198 {
199         os << paragraphs().begin()->asString(buf, true);
200 }
201
202
203 void InsetCharStyleParams::write(ostream & os) const
204 {
205         os << "CharStyle " << name << "\n";
206 }
207
208
209 void InsetCharStyleParams::read(Lexer & lex)
210 {
211         while (lex.isOK()) {
212                 lex.next();
213                 string token = lex.getString();
214
215                 if (token == "CharStyle") {
216                         lex.next();
217                         name = lex.getString();
218                 }
219
220                 // This is handled in Collapsable
221                 else if (token == "status") {
222                         lex.pushToken(token);
223                         break;
224                 }
225         }
226 }
227
228
229 } // namespace lyx