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