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