]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Improve handling of top and bottom margin
[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_docbook.h"
23 #include "output_xhtml.h"
24 #include "texstream.h"
25
26 #include "frontends/Application.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
29
30 #include "support/debug.h"
31 #include "support/docstream.h"
32 #include "support/docstring.h"
33
34 using namespace std;
35 using namespace lyx::frontend;
36
37 namespace lyx {
38
39 InsetSeparator::InsetSeparator() : Inset(0)
40 {}
41
42
43 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
44         : Inset(0), params_(params)
45 {}
46
47
48 void InsetSeparatorParams::write(ostream & os) const
49 {
50         switch (kind) {
51         case InsetSeparatorParams::PLAIN:
52                 os <<  "plain";
53                 break;
54         case InsetSeparatorParams::PARBREAK:
55                 os <<  "parbreak";
56                 break;
57         case InsetSeparatorParams::LATEXPAR:
58                 os <<  "latexpar";
59                 break;
60         }
61 }
62
63
64 void InsetSeparatorParams::read(Lexer & lex)
65 {
66         string token;
67         lex.setContext("InsetSeparatorParams::read");
68         lex >> token;
69         if (token == "plain")
70                 kind = InsetSeparatorParams::PLAIN;
71         else if (token == "parbreak")
72                 kind = InsetSeparatorParams::PARBREAK;
73         else if (token == "latexpar")
74                 kind = InsetSeparatorParams::LATEXPAR;
75         else
76                 lex.printError("Unknown kind: `$$Token'");
77 }
78
79
80 void InsetSeparator::write(ostream & os) const
81 {
82         os << "Separator ";
83         params_.write(os);
84 }
85
86
87 void InsetSeparator::read(Lexer & lex)
88 {
89         params_.read(lex);
90         lex >> "\\end_inset";
91 }
92
93
94 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
95 {
96         switch (cmd.action()) {
97
98         case LFUN_INSET_MODIFY: {
99                 InsetSeparatorParams params;
100                 cur.recordUndo();
101                 string2params(to_utf8(cmd.argument()), params);
102                 params_.kind = params.kind;
103                 break;
104         }
105
106         default:
107                 Inset::doDispatch(cur, cmd);
108                 break;
109         }
110 }
111
112
113 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
114         FuncStatus & status) const
115 {
116         switch (cmd.action()) {
117         // we handle these
118         case LFUN_INSET_MODIFY: {
119                 if (cmd.getArg(0) != "separator")
120                         break;
121                 InsetSeparatorParams params;
122                 string2params(to_utf8(cmd.argument()), params);
123                 status.setOnOff(params_.kind == params.kind);
124                 status.setEnabled(true);
125                 return true;
126         }
127         default:
128                 return Inset::getStatus(cur, cmd, status);
129         }
130         return false;
131 }
132
133
134 ColorCode InsetSeparator::ColorName() const
135 {
136         return Color_latex;
137 }
138
139
140 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
141 {
142         // Do nothing if a paragraph break was just output
143         if (!os.afterParbreak()) {
144                 switch (params_.kind) {
145                         case InsetSeparatorParams::PLAIN:
146                                 os << breakln << "%\n";
147                                 break;
148                         case InsetSeparatorParams::PARBREAK:
149                         case InsetSeparatorParams::LATEXPAR:
150                                 if (runparams.inDeletedInset)
151                                         os << breakln << "}\n\n{";
152                                 else
153                                         os << breakln << "\n";
154                                 break;
155                         default:
156                                 os << breakln << "%\n";
157                                 break;
158                 }
159         }
160 }
161
162
163 int InsetSeparator::plaintext(odocstringstream & os,
164         OutputParams const &, size_t) const
165 {
166         os << '\n';
167         return PLAINTEXT_NEWLINE;
168 }
169
170
171 void InsetSeparator::docbook(XMLStream & xs, OutputParams const &) const
172 {
173         xs << xml::CR();
174 }
175
176
177 docstring InsetSeparator::xhtml(XMLStream & xs, OutputParams const &) const
178 {
179         xs << xml::CR() << xml::CompTag("br") << xml::CR();
180         return docstring();
181 }
182
183
184 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
185 {
186         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
187         dim.asc = fm.maxAscent();
188         dim.des = fm.maxDescent();
189         dim.wid = fm.width('n');
190         if (params_.kind != InsetSeparatorParams::LATEXPAR)
191                 dim.wid *= 8;
192 }
193
194
195 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
196 {
197         FontInfo font;
198         font.setColor(ColorName());
199
200         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
201         int const wid = fm.width('n');
202         int const asc = fm.maxAscent();
203
204         int xp[7];
205         int yp[7];
206
207         if (params_.kind != InsetSeparatorParams::LATEXPAR) {
208                 yp[0] = int(y - 0.500 * asc * 0.75);
209                 yp[1] = yp[0];
210
211                 xp[0] = int(x);
212                 xp[1] = int(x + wid * 8);
213
214                 pi.pain.lines(xp, yp, 2, ColorName());
215
216                 if (params_.kind == InsetSeparatorParams::PARBREAK) {
217                         yp[0] += int(0.25 * asc * 0.75);
218                         yp[1] = yp[0];
219                         pi.pain.lines(xp, yp, 2, ColorName());
220                 }
221         } else {
222                 yp[0] = int(y - 0.500 * asc * 0.5);
223                 yp[1] = int(y - 0.250 * asc * 0.5);
224                 yp[2] = int(y);
225
226                 if (pi.ltr_pos) {
227                         xp[0] = int(x + 1 + wid * 0.375);
228                         xp[1] = int(x + 1);
229                 } else {
230                         xp[0] = int(x - 1 + wid * 0.625);
231                         xp[1] = int(x - 1 + wid);
232                 }
233                 xp[2] = xp[0];
234
235                 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
236
237                 yp[0] = yp[1];
238                 yp[2] = int(y - 0.850 * asc * 0.5);
239                 yp[3] = int(y - 1.250 * asc * 0.5);
240                 yp[4] = yp[3];
241                 yp[5] = yp[2];
242                 yp[6] = yp[5];
243
244                 xp[0] = xp[1];
245                 if (pi.ltr_pos) {
246                         xp[1] = int(x + 1 + wid * 0.50);
247                         xp[2] = int(x + wid);
248                         xp[3] = xp[2];
249                         xp[4] = int(x + wid * 0.75);
250                 } else {
251                         xp[1] = int(x + wid * 0.50);
252                         xp[2] = int(x);
253                         xp[3] = xp[2];
254                         xp[4] = int(x + wid * 0.25);
255                 }
256                 xp[5] = xp[4];
257                 xp[6] = xp[2];
258
259                 int c1x[7];
260                 int c1y[7];
261                 int c2x[7];
262                 int c2y[7];
263
264                 for (int i = 1; i < 7; ++i) {
265                         c1x[i] = xp[i - 1];
266                         c1y[i] = yp[i - 1];
267                         c2x[i] = xp[i];
268                         c2y[i] = yp[i];
269                 }
270
271                 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
272
273                 c1x[2] = xp[2];
274                 c2y[2] = int(y - 0.500 * asc * 0.5);
275                 c1x[5] += d;
276                 c2x[5] += d;
277
278                 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
279         }
280 }
281
282
283 string InsetSeparator::contextMenuName() const
284 {
285         if (params_.kind == InsetSeparatorParams::LATEXPAR)
286                 return string();
287
288         return "context-separator";
289 }
290
291
292 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
293 {
294         params = InsetSeparatorParams();
295         if (in.empty())
296                 return;
297         istringstream data(in);
298         Lexer lex;
299         lex.setStream(data);
300         lex.setContext("InsetSeparator::string2params");
301         lex >> "separator";
302         params.read(lex);
303 }
304
305
306 string InsetSeparator::params2string(InsetSeparatorParams const & params)
307 {
308         ostringstream data;
309         data << "separator" << ' ';
310         params.write(data);
311         return data.str();
312 }
313
314
315 } // namespace lyx