]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Fix export of xfig external insets (bug #9244).
[lyx.git] / src / insets / InsetSeparator.cpp
1 /**
2  * \file InsetSeparator.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Enrico Forestieri
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetSeparator.h"
14
15 #include "Cursor.h"
16 #include "Dimension.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23
24 #include "frontends/Application.h"
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/debug.h"
29 #include "support/docstream.h"
30 #include "support/docstring.h"
31
32 using namespace std;
33
34 namespace lyx {
35
36 InsetSeparator::InsetSeparator() : Inset(0)
37 {}
38
39
40 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
41         : Inset(0), params_(params)
42 {}
43
44
45 void InsetSeparatorParams::write(ostream & os) const
46 {
47         string command;
48         switch (kind) {
49         case InsetSeparatorParams::PLAIN:
50                 os <<  "plain";
51                 break;
52         case InsetSeparatorParams::PARBREAK:
53                 os <<  "parbreak";
54                 break;
55         }
56 }
57
58
59 void InsetSeparatorParams::read(Lexer & lex)
60 {
61         string token;
62         lex.setContext("InsetSeparatorParams::read");
63         lex >> token;
64         if (token == "plain")
65                 kind = InsetSeparatorParams::PLAIN;
66         else if (token == "parbreak")
67                 kind = InsetSeparatorParams::PARBREAK;
68         else
69                 lex.printError("Unknown kind: `$$Token'");
70 }
71
72
73 void InsetSeparator::write(ostream & os) const
74 {
75         os << "Separator ";
76         params_.write(os);
77 }
78
79
80 void InsetSeparator::read(Lexer & lex)
81 {
82         params_.read(lex);
83         lex >> "\\end_inset";
84 }
85
86
87 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
88 {
89         switch (cmd.action()) {
90
91         case LFUN_INSET_MODIFY: {
92                 InsetSeparatorParams params;
93                 cur.recordUndo();
94                 string2params(to_utf8(cmd.argument()), params);
95                 params_.kind = params.kind;
96                 break;
97         }
98
99         default:
100                 Inset::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
107         FuncStatus & status) const
108 {
109         switch (cmd.action()) {
110         // we handle these
111         case LFUN_INSET_MODIFY: {
112                 if (cmd.getArg(0) != "separator")
113                         break;
114                 InsetSeparatorParams params;
115                 string2params(to_utf8(cmd.argument()), params);
116                 status.setOnOff(params_.kind == params.kind);
117                 status.setEnabled(true);
118                 return true;
119         }
120         default:
121                 return Inset::getStatus(cur, cmd, status);
122         }
123         return false;
124 }
125
126
127 ColorCode InsetSeparator::ColorName() const
128 {
129         switch (params_.kind) {
130                 case InsetSeparatorParams::PLAIN:
131                         return Color_latex;
132                         break;
133                 case InsetSeparatorParams::PARBREAK:
134                         return Color_pagebreak;
135                         break;
136         }
137         // not really useful, but to avoids gcc complaints
138         return Color_latex;
139 }
140
141
142 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
143 {
144         // Do nothing if a paragraph break was just output
145         if (!os.afterParbreak()) {
146                 switch (params_.kind) {
147                         case InsetSeparatorParams::PLAIN:
148                                 os << breakln << "%\n";
149                                 break;
150                         case InsetSeparatorParams::PARBREAK:
151                                 os << breakln << "\n";
152                                 break;
153                         default:
154                                 os << breakln << "%\n";
155                                 break;
156                 }
157         }
158 }
159
160
161 int InsetSeparator::plaintext(odocstringstream & os,
162         OutputParams const &, size_t) const
163 {
164         os << '\n';
165         return PLAINTEXT_NEWLINE;
166 }
167
168
169 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
170 {
171         os << '\n';
172         return 0;
173 }
174
175
176 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
177 {
178         xs << html::CR() << html::CompTag("br") << html::CR();
179         return docstring();
180 }
181
182
183 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
184 {
185         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
186         dim.asc = fm.maxAscent();
187         dim.des = fm.maxDescent();
188         dim.wid = fm.width('m');
189         if (params_.kind == InsetSeparatorParams::PLAIN)
190                 dim.wid *= 5;
191 }
192
193
194 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
195 {
196         FontInfo font;
197         font.setColor(ColorName());
198
199         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
200         int const wid = fm.width('m');
201         int const asc = fm.maxAscent();
202
203         int xp[3];
204         int yp[3];
205
206         if (params_.kind == InsetSeparatorParams::PLAIN) {
207                 yp[0] = int(y - 0.500 * asc * 0.75);
208                 yp[1] = int(y - 0.500 * asc * 0.75);
209
210                 xp[0] = int(x);
211                 xp[1] = int(x + wid * 5);
212
213                 pi.pain.lines(xp, yp, 2, ColorName());
214         } else {
215                 yp[0] = int(y - 0.875 * asc * 0.75);
216                 yp[1] = int(y - 0.500 * asc * 0.75);
217                 yp[2] = int(y - 0.125 * asc * 0.75);
218
219                 if (pi.ltr_pos) {
220                         xp[0] = int(x + wid * 0.375);
221                         xp[1] = int(x);
222                         xp[2] = int(x + wid * 0.375);
223                 } else {
224                         xp[0] = int(x + wid * 0.625);
225                         xp[1] = int(x + wid);
226                         xp[2] = int(x + wid * 0.625);
227                 }
228
229                 pi.pain.lines(xp, yp, 3, ColorName());
230
231                 yp[0] = int(y - 0.500 * asc * 0.75);
232                 yp[1] = int(y - 0.500 * asc * 0.75);
233                 yp[2] = int(y - asc * 0.75);
234
235                 if (pi.ltr_pos) {
236                         xp[0] = int(x);
237                         xp[1] = int(x + wid);
238                         xp[2] = int(x + wid);
239                 } else {
240                         xp[0] = int(x + wid);
241                         xp[1] = int(x);
242                         xp[2] = int(x);
243                 }
244
245                 pi.pain.lines(xp, yp, 3, ColorName());
246         }
247 }
248
249
250 string InsetSeparator::contextMenuName() const
251 {
252         return "context-separator";
253 }
254
255
256 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
257 {
258         params = InsetSeparatorParams();
259         if (in.empty())
260                 return;
261         istringstream data(in);
262         Lexer lex;
263         lex.setStream(data);
264         lex.setContext("InsetSeparator::string2params");
265         lex >> "separator";
266         params.read(lex);
267 }
268
269
270 string InsetSeparator::params2string(InsetSeparatorParams const & params)
271 {
272         ostringstream data;
273         data << "separator" << ' ';
274         params.write(data);
275         return data.str();
276 }
277
278
279 } // namespace lyx