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