]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Oops -- cut off wrong part of label
[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 void InsetCharStyle::init()
52 {}
53
54
55 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
56         : InsetCollapsable(bp, Collapsed)
57 {
58         params_.name = s;
59         setUndefined();
60         init();
61 }
62
63
64 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
65                                 CharStyles::iterator cs)
66         : InsetCollapsable(bp, Collapsed)
67 {
68         params_.name = cs->name;
69         setDefined(cs);
70         init();
71 }
72
73
74 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
75         : InsetCollapsable(in), params_(in.params_)
76 {
77         init();
78 }
79
80
81 auto_ptr<Inset> InsetCharStyle::doClone() const
82 {
83         return auto_ptr<Inset>(new InsetCharStyle(*this));
84 }
85
86
87 bool InsetCharStyle::undefined() const
88 {
89         return layout_.latexname.empty();
90 }
91
92
93 void InsetCharStyle::setUndefined()
94 {
95         layout_.latextype.clear();
96         layout_.latexname.clear();
97         layout_.latexparam.clear();
98         layout_.font = Font(Font::ALL_INHERIT);
99         layout_.labelfont = Font(Font::ALL_INHERIT);
100         layout_.labelfont.setColor(Color::error);
101 }
102
103
104 void InsetCharStyle::setDefined(CharStyles::iterator cs)
105 {
106         layout_ = *cs;
107 }
108
109
110 docstring const InsetCharStyle::editMessage() const
111 {
112         return _("Opened CharStyle Inset");
113 }
114
115
116 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
117 {
118         params_.write(os);
119         InsetCollapsable::write(buf, os);
120 }
121
122
123 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
124 {
125         params_.read(lex);
126         InsetCollapsable::read(buf, lex);
127 }
128
129
130 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         Font tmpfont = mi.base.font;
133         getDrawFont(mi.base.font);
134         mi.base.font.reduce(Font(Font::ALL_SANE));
135         mi.base.font.realize(tmpfont);
136         bool changed = InsetCollapsable::metrics(mi, dim);
137         mi.base.font = tmpfont;
138         if (status() == Open) {
139                 // FIXME UNICODE
140                 docstring s(from_utf8(params_.name));
141                 // Chop off prefix:
142                 if (s.find(':') != string::npos)
143                         s = s.substr(s.find(':'));
144                 if (undefined())
145                         s = _("Undef: ") + s;
146                 layout_.labelstring = s;
147         }
148         return changed;
149 }
150
151
152 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
153 {
154         setPosCache(pi, x, y);
155
156         Font tmpfont = pi.base.font;
157         getDrawFont(pi.base.font);
158         // I don't understand why the above .reduce and .realize aren't
159         //needed, or even wanted, here. It just works. -- MV 10.04.2005
160         InsetCollapsable::draw(pi, x, y);
161         pi.base.font = tmpfont;
162
163         // the name of the charstyle. Can be toggled.
164         if (status() == Open) {
165                 // FIXME UNICODE
166                 docstring s(from_utf8(params_.name));
167                 // Chop off prefix:
168                 if (s.find(':') != string::npos)
169                         s = s.substr(s.find(':'));
170                 if (undefined())
171                         s = _("Undef: ") + s;
172                 layout_.labelstring = s;
173         }
174 }
175
176
177 void InsetCharStyle::getDrawFont(Font & font) const
178 {
179         font = layout_.font;
180 }
181
182
183 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
184 {
185         InsetCollapsable::doDispatch(cur, cmd);
186 }
187
188
189 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
190         FuncStatus & status) const
191 {
192         switch (cmd.action) {
193                 // paragraph breaks not allowed in charstyle insets
194                 case LFUN_BREAK_PARAGRAPH:
195                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
196                 case LFUN_BREAK_PARAGRAPH_SKIP:
197                         status.enabled(false);
198                         return true;
199
200                 default:
201                         return InsetCollapsable::getStatus(cur, cmd, status);
202                 }
203 }
204
205
206 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
207                               OutputParams const & runparams) const
208 {
209         return InsetText::plaintext(buf, os, runparams);
210 }
211
212
213 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
214                             OutputParams const & runparams) const
215 {
216         ParagraphList::const_iterator beg = paragraphs().begin();
217         ParagraphList::const_iterator par = paragraphs().begin();
218         ParagraphList::const_iterator end = paragraphs().end();
219
220         if (!undefined())
221                 // FIXME UNICODE
222                 sgml::openTag(os, layout_.latexname,
223                               par->getID(buf, runparams) + layout_.latexparam);
224
225         for (; par != end; ++par) {
226                 par->simpleDocBookOnePar(buf, os, runparams,
227                                          outerFont(std::distance(beg, par),
228                                                    paragraphs()));
229         }
230
231         if (!undefined())
232                 sgml::closeTag(os, layout_.latexname);
233
234         return 0;
235 }
236
237
238 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
239 {
240         os << paragraphs().begin()->asString(buf, true);
241 }
242
243
244 void InsetCharStyleParams::write(ostream & os) const
245 {
246         os << "CharStyle " << name << "\n";
247 }
248
249
250 void InsetCharStyleParams::read(Lexer & lex)
251 {
252         while (lex.isOK()) {
253                 lex.next();
254                 string token = lex.getString();
255
256                 if (token == "CharStyle") {
257                         lex.next();
258                         name = lex.getString();
259                 }
260
261                 // This is handled in Collapsable
262                 else if (token == "status") {
263                         lex.pushToken(token);
264                         break;
265                 }
266         }
267 }
268
269
270 } // namespace lyx