]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Fix trailing whitespace in cpp files.
[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 #include "texstream.h"
24
25 #include "frontends/Application.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/docstring.h"
32
33 using namespace std;
34 using namespace lyx::frontend;
35
36 namespace lyx {
37
38 InsetSeparator::InsetSeparator() : Inset(0)
39 {}
40
41
42 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
43         : Inset(0), params_(params)
44 {}
45
46
47 void InsetSeparatorParams::write(ostream & os) const
48 {
49         switch (kind) {
50         case InsetSeparatorParams::PLAIN:
51                 os <<  "plain";
52                 break;
53         case InsetSeparatorParams::PARBREAK:
54                 os <<  "parbreak";
55                 break;
56         case InsetSeparatorParams::LATEXPAR:
57                 os <<  "latexpar";
58                 break;
59         }
60 }
61
62
63 void InsetSeparatorParams::read(Lexer & lex)
64 {
65         string token;
66         lex.setContext("InsetSeparatorParams::read");
67         lex >> token;
68         if (token == "plain")
69                 kind = InsetSeparatorParams::PLAIN;
70         else if (token == "parbreak")
71                 kind = InsetSeparatorParams::PARBREAK;
72         else if (token == "latexpar")
73                 kind = InsetSeparatorParams::LATEXPAR;
74         else
75                 lex.printError("Unknown kind: `$$Token'");
76 }
77
78
79 void InsetSeparator::write(ostream & os) const
80 {
81         os << "Separator ";
82         params_.write(os);
83 }
84
85
86 void InsetSeparator::read(Lexer & lex)
87 {
88         params_.read(lex);
89         lex >> "\\end_inset";
90 }
91
92
93 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
94 {
95         switch (cmd.action()) {
96
97         case LFUN_INSET_MODIFY: {
98                 InsetSeparatorParams params;
99                 cur.recordUndo();
100                 string2params(to_utf8(cmd.argument()), params);
101                 params_.kind = params.kind;
102                 break;
103         }
104
105         default:
106                 Inset::doDispatch(cur, cmd);
107                 break;
108         }
109 }
110
111
112 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
113         FuncStatus & status) const
114 {
115         switch (cmd.action()) {
116         // we handle these
117         case LFUN_INSET_MODIFY: {
118                 if (cmd.getArg(0) != "separator")
119                         break;
120                 InsetSeparatorParams params;
121                 string2params(to_utf8(cmd.argument()), params);
122                 status.setOnOff(params_.kind == params.kind);
123                 status.setEnabled(true);
124                 return true;
125         }
126         default:
127                 return Inset::getStatus(cur, cmd, status);
128         }
129         return false;
130 }
131
132
133 ColorCode InsetSeparator::ColorName() const
134 {
135         return Color_latex;
136 }
137
138
139 void InsetSeparator::latex(otexstream & os, OutputParams const & runparams) const
140 {
141         // Do nothing if a paragraph break was just output
142         if (!os.afterParbreak()) {
143                 switch (params_.kind) {
144                         case InsetSeparatorParams::PLAIN:
145                                 os << breakln << "%\n";
146                                 break;
147                         case InsetSeparatorParams::PARBREAK:
148                         case InsetSeparatorParams::LATEXPAR:
149                                 if (runparams.inDeletedInset)
150                                         os << breakln << "}\n\n{";
151                                 else
152                                         os << breakln << "\n";
153                                 break;
154                         default:
155                                 os << breakln << "%\n";
156                                 break;
157                 }
158         }
159 }
160
161
162 int InsetSeparator::plaintext(odocstringstream & os,
163         OutputParams const &, size_t) const
164 {
165         os << '\n';
166         return PLAINTEXT_NEWLINE;
167 }
168
169
170 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
171 {
172         os << '\n';
173         return 0;
174 }
175
176
177 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
178 {
179         xs << html::CR() << html::CompTag("br") << html::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