]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCharStyle.cpp
Cleanup 2: moving more stuff charstyle -> collapsable
[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 "LaTeXFeatures.h"
26 #include "Color.h"
27 #include "Lexer.h"
28 #include "Text.h"
29 #include "MetricsInfo.h"
30 #include "Paragraph.h"
31 #include "paragraph_funcs.h"
32 #include "sgml.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include "support/convert.h"
38
39 #include <sstream>
40
41
42 namespace lyx {
43
44 using std::string;
45 using std::auto_ptr;
46 using std::istringstream;
47 using std::max;
48 using std::ostream;
49 using std::ostringstream;
50
51
52 void InsetCharStyle::init()
53 {}
54
55
56 InsetCharStyle::InsetCharStyle(BufferParams const & bp, string const s)
57         : InsetCollapsable(bp, Collapsed)
58 {
59         params_.name = s;
60         setUndefined();
61         init();
62 }
63
64
65 InsetCharStyle::InsetCharStyle(BufferParams const & bp,
66                                 CharStyles::iterator cs)
67         : InsetCollapsable(bp, Collapsed)
68 {
69         params_.name = cs->name;
70         setDefined(cs);
71         init();
72 }
73
74
75 InsetCharStyle::InsetCharStyle(InsetCharStyle const & in)
76         : InsetCollapsable(in), params_(in.params_)
77 {
78         init();
79 }
80
81
82 auto_ptr<Inset> InsetCharStyle::doClone() const
83 {
84         return auto_ptr<Inset>(new InsetCharStyle(*this));
85 }
86
87
88 bool InsetCharStyle::undefined() const
89 {
90         return layout_.latexname.empty();
91 }
92
93
94 void InsetCharStyle::setUndefined()
95 {
96         layout_.latextype.clear();
97         layout_.latexname.clear();
98         layout_.latexparam.clear();
99         layout_.font = Font(Font::ALL_INHERIT);
100         layout_.labelfont = Font(Font::ALL_INHERIT);
101         layout_.labelfont.setColor(Color::error);
102 }
103
104
105 void InsetCharStyle::setDefined(CharStyles::iterator cs)
106 {
107         layout_ = *cs;
108 }
109
110
111 docstring const InsetCharStyle::editMessage() const
112 {
113         return _("Opened CharStyle Inset");
114 }
115
116
117 void InsetCharStyle::write(Buffer const & buf, ostream & os) const
118 {
119         params_.write(os);
120         InsetCollapsable::write(buf, os);
121 }
122
123
124 void InsetCharStyle::read(Buffer const & buf, Lexer & lex)
125 {
126         params_.read(lex);
127         InsetCollapsable::read(buf, lex);
128 }
129
130
131 bool InsetCharStyle::metrics(MetricsInfo & mi, Dimension & dim) const
132 {
133         Font tmpfont = mi.base.font;
134         getDrawFont(mi.base.font);
135         mi.base.font.reduce(Font(Font::ALL_SANE));
136         mi.base.font.realize(tmpfont);
137         bool changed = InsetCollapsable::metrics(mi, dim);
138         mi.base.font = tmpfont;
139         if (status() == Open) {
140                 // FIXME UNICODE
141                 docstring s(from_utf8(params_.name));
142                 if (undefined())
143                         s = _("Undef: ") + s;
144                 // Chop off prefix:
145                 if (s.find(':') != string::npos)
146                         s = s.substr(s.find(':'));
147                 layout_.labelstring = s;
148         }
149         return changed;
150 }
151
152
153 void InsetCharStyle::draw(PainterInfo & pi, int x, int y) const
154 {
155         setPosCache(pi, x, y);
156
157         Font tmpfont = pi.base.font;
158         getDrawFont(pi.base.font);
159         // I don't understand why the above .reduce and .realize aren't
160         //needed, or even wanted, here. It just works. -- MV 10.04.2005
161         InsetCollapsable::draw(pi, x, y);
162         pi.base.font = tmpfont;
163
164         // the name of the charstyle. Can be toggled.
165         if (status() == Open) {
166                 // FIXME UNICODE
167                 docstring s(from_utf8(params_.name));
168                 if (undefined())
169                         s = _("Undef: ") + s;
170                 // Chop off prefix:
171                 if (s.find(':') != string::npos)
172                         s = s.substr(s.find(':'));
173                 layout_.labelstring = s;
174         }
175 }
176
177
178 void InsetCharStyle::getDrawFont(Font & font) const
179 {
180         font = layout_.font;
181 }
182
183
184 void InsetCharStyle::doDispatch(Cursor & cur, FuncRequest & cmd)
185 {
186         switch (cmd.action) {
187
188         case LFUN_MOUSE_RELEASE:
189                         if (cmd.button() == mouse_button::button3)
190                                 if (internalStatus() == Open)
191                                         setStatus(cur, Collapsed);
192                                 else
193                                         setStatus(cur, Open);
194                         else
195                                 InsetCollapsable::doDispatch(cur, cmd);
196                         break;
197
198         case LFUN_INSET_TOGGLE:
199                 if (cmd.argument() == "open")
200                         setStatus(cur, Open);
201                 else if (cmd.argument() == "close")
202                         setStatus(cur, Collapsed);
203                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
204                         if (internalStatus() == Open)
205                                 setStatus(cur, Collapsed);
206                         else
207                                 setStatus(cur, Open);
208                 else // if assign or anything else
209                         cur.undispatched();
210                 cur.dispatched();
211                 break;
212
213         default:
214                 InsetCollapsable::doDispatch(cur, cmd);
215                 break;
216         }
217 }
218
219
220 bool InsetCharStyle::getStatus(Cursor & cur, FuncRequest const & cmd,
221         FuncStatus & status) const
222 {
223         switch (cmd.action) {
224                 // paragraph breaks not allowed in charstyle insets
225                 case LFUN_BREAK_PARAGRAPH:
226                 case LFUN_BREAK_PARAGRAPH_KEEP_LAYOUT:
227                 case LFUN_BREAK_PARAGRAPH_SKIP:
228                         status.enabled(false);
229                         return true;
230
231                 default:
232                         return InsetCollapsable::getStatus(cur, cmd, status);
233                 }
234 }
235
236
237 int InsetCharStyle::latex(Buffer const & buf, odocstream & os,
238                           OutputParams const & runparams) const
239 {
240         if (!undefined()) {
241                 // FIXME UNICODE
242                 os << '\\' << from_utf8(layout_.latexname);
243                 if (!layout_.latexparam.empty())
244                         os << from_utf8(layout_.latexparam);
245                 os << '{';
246         }
247         int i = InsetText::latex(buf, os, runparams);
248         if (!undefined())
249                 os << "}";
250         return i;
251 }
252
253
254 int InsetCharStyle::plaintext(Buffer const & buf, odocstream & os,
255                               OutputParams const & runparams) const
256 {
257         return InsetText::plaintext(buf, os, runparams);
258 }
259
260
261 int InsetCharStyle::docbook(Buffer const & buf, odocstream & os,
262                             OutputParams const & runparams) const
263 {
264         ParagraphList::const_iterator beg = paragraphs().begin();
265         ParagraphList::const_iterator par = paragraphs().begin();
266         ParagraphList::const_iterator end = paragraphs().end();
267
268         if (!undefined())
269                 // FIXME UNICODE
270                 sgml::openTag(os, layout_.latexname,
271                               par->getID(buf, runparams) + layout_.latexparam);
272
273         for (; par != end; ++par) {
274                 par->simpleDocBookOnePar(buf, os, runparams,
275                                          outerFont(std::distance(beg, par),
276                                                    paragraphs()));
277         }
278
279         if (!undefined())
280                 sgml::closeTag(os, layout_.latexname);
281
282         return 0;
283 }
284
285
286 void InsetCharStyle::textString(Buffer const & buf, odocstream & os) const
287 {
288         os << paragraphs().begin()->asString(buf, true);
289 }
290
291
292 void InsetCharStyle::validate(LaTeXFeatures & features) const
293 {
294         // Force inclusion of preamble snippet in layout file
295         features.require(params_.name);
296         InsetText::validate(features);
297 }
298
299
300 void InsetCharStyleParams::write(ostream & os) const
301 {
302         os << "CharStyle " << name << "\n";
303 }
304
305
306 void InsetCharStyleParams::read(Lexer & lex)
307 {
308         while (lex.isOK()) {
309                 lex.next();
310                 string token = lex.getString();
311
312                 if (token == "CharStyle") {
313                         lex.next();
314                         name = lex.getString();
315                 }
316
317                 // This is handled in Collapsable
318                 else if (token == "status") {
319                         lex.pushToken(token);
320                         break;
321                 }
322         }
323 }
324
325
326 } // namespace lyx