]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewline.cpp
Remove profiling.py
[lyx.git] / src / insets / InsetNewline.cpp
1 /**
2  * \file InsetNewline.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetNewline.h"
15
16 #include "Cursor.h"
17 #include "Dimension.h"
18 #include "FuncRequest.h"
19 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "output_docbook.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 #include "support/Lexer.h"
33
34 using namespace std;
35
36 namespace lyx {
37
38 using support::Lexer;
39
40 InsetNewline::InsetNewline() : Inset(nullptr)
41 {}
42
43
44 int InsetNewline::rowFlags() const
45 {
46         if (params_.kind == InsetNewlineParams::LINEBREAK)
47                 return AlwaysBreakAfter;
48         else
49             return AlwaysBreakAfter | Flush;
50 }
51
52
53 void InsetNewlineParams::write(ostream & os) const
54 {
55         switch (kind) {
56         case InsetNewlineParams::NEWLINE:
57                 os << "newline";
58                 break;
59         case InsetNewlineParams::LINEBREAK:
60                 os <<  "linebreak";
61                 break;
62         }
63 }
64
65
66 void InsetNewlineParams::read(Lexer & lex)
67 {
68         string token;
69         lex.setContext("InsetNewlineParams::read");
70         lex >> token;
71         if (token == "newline")
72                 kind = InsetNewlineParams::NEWLINE;
73         else if (token == "linebreak")
74                 kind = InsetNewlineParams::LINEBREAK;
75         else
76                 lex.printError("Unknown kind: `$$Token'");
77 }
78
79
80 void InsetNewline::write(ostream & os) const
81 {
82         os << "Newline ";
83         params_.write(os);
84 }
85
86
87 void InsetNewline::read(Lexer & lex)
88 {
89         params_.read(lex);
90         lex >> "\\end_inset";
91 }
92
93
94 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
95 {
96         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
97         dim.asc = fm.maxAscent();
98         dim.des = fm.maxDescent();
99         dim.wid = fm.width('n');
100 }
101
102
103 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
104 {
105         switch (cmd.action()) {
106
107         case LFUN_INSET_MODIFY: {
108                 InsetNewlineParams params;
109                 cur.recordUndo();
110                 string2params(to_utf8(cmd.argument()), params);
111                 params_.kind = params.kind;
112                 break;
113         }
114
115         default:
116                 Inset::doDispatch(cur, cmd);
117                 break;
118         }
119 }
120
121
122 bool InsetNewline::getStatus(Cursor & cur, FuncRequest const & cmd,
123         FuncStatus & status) const
124 {
125         switch (cmd.action()) {
126         // we handle these
127         case LFUN_INSET_MODIFY:
128                 if (cmd.getArg(0) == "newline") {
129                         InsetNewlineParams params;
130                         string2params(to_utf8(cmd.argument()), params);
131                         status.setOnOff(params_.kind == params.kind);
132                 }
133                 status.setEnabled(true);
134                 return true;
135         default:
136                 return Inset::getStatus(cur, cmd, status);
137         }
138 }
139
140
141 ColorCode InsetNewline::ColorName() const
142 {
143         switch (params_.kind) {
144                 case InsetNewlineParams::NEWLINE:
145                         return Color_eolmarker;
146                         break;
147                 case InsetNewlineParams::LINEBREAK:
148                         return Color_pagebreak;
149                         break;
150         }
151         // not really useful, but to avoids gcc complaints
152         return Color_eolmarker;
153 }
154
155
156 void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
157 {
158         switch (params_.kind) {
159                 case InsetNewlineParams::NEWLINE:
160                         if (!rp.newlinecmd.empty())
161                                 os << "\\" << rp.newlinecmd << "\n";
162                         else if (rp.inTableCell == OutputParams::PLAIN)
163                                 os << "\\newline\n";
164                         else
165                                 os << "\\\\\n";
166                         break;
167                 case InsetNewlineParams::LINEBREAK:
168                         os << "\\linebreak{}\n";
169                         break;
170                 default:
171                         os << "\\\\\n";
172                         break;
173         }
174 }
175
176
177 int InsetNewline::plaintext(odocstringstream & os,
178         OutputParams const &, size_t) const
179 {
180         os << '\n';
181         return PLAINTEXT_NEWLINE;
182 }
183
184
185 void InsetNewline::docbook(XMLStream &, OutputParams const &) const
186 {
187         // New lines are handled by Paragraph::simpleDocBookOnePar.
188 }
189
190
191 docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
192 {
193         xs << xml::CR() << xml::CompTag("br") << xml::CR();
194         return docstring();
195 }
196
197
198 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
199 {
200         FontInfo font;
201         font.setColor(ColorName());
202
203         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
204         int const wid = fm.width('n');
205         int const asc = fm.maxAscent();
206
207         int xp[3];
208         int yp[3];
209
210         yp[0] = int(y - 0.875 * asc * 0.75);
211         yp[1] = int(y - 0.500 * asc * 0.75);
212         yp[2] = int(y - 0.125 * asc * 0.75);
213
214         if (pi.ltr_pos) {
215                 xp[0] = int(x + wid * 0.375);
216                 xp[1] = int(x);
217                 xp[2] = int(x + wid * 0.375);
218         } else {
219                 xp[0] = int(x + wid * 0.625);
220                 xp[1] = int(x + wid);
221                 xp[2] = int(x + wid * 0.625);
222         }
223
224         pi.pain.lines(xp, yp, 3, ColorName());
225
226         yp[0] = int(y - 0.500 * asc * 0.75);
227         yp[1] = int(y - 0.500 * asc * 0.75);
228         yp[2] = int(y - asc * 0.75);
229
230         if (pi.ltr_pos) {
231                 xp[0] = int(x);
232                 xp[1] = int(x + wid);
233                 xp[2] = int(x + wid);
234         } else {
235                 xp[0] = int(x + wid);
236                 xp[1] = int(x);
237                 xp[2] = int(x);
238         }
239
240         pi.pain.lines(xp, yp, 3, ColorName());
241
242         if (params_.kind == InsetNewlineParams::LINEBREAK) {
243
244                 yp[2] = int(y - 0.500 * asc * 0.75);
245
246                 if (pi.ltr_pos) {
247                         xp[0] = int(x + 1.3 * wid);
248                         xp[1] = int(x + 2 * wid);
249                         xp[2] = int(x + 2 * wid);
250                 } else {
251                         xp[0] = int(x - 0.3 * wid);
252                         xp[1] = int(x - wid);
253                         xp[2] = int(x - wid);
254                 }
255                 pi.pain.lines(xp, yp, 3, ColorName());
256
257                 yp[0] = int(y - 0.875 * asc * 0.75);
258                 yp[1] = int(y - 0.500 * asc * 0.75);
259                 yp[2] = int(y - 0.125 * asc * 0.75);
260
261                 if (pi.ltr_pos) {
262                         xp[0] = int(x + 2 * wid * 0.813);
263                         xp[1] = int(x + 2 * wid);
264                         xp[2] = int(x + 2 * wid * 0.813);
265                 } else {
266                         xp[0] = int(x - wid * 0.625);
267                         xp[1] = int(x - wid);
268                         xp[2] = int(x - wid * 0.625);
269                 }
270                 pi.pain.lines(xp, yp, 3, ColorName());
271         }
272 }
273
274
275 string InsetNewline::contextMenuName() const
276 {
277         return "context-newline";
278 }
279
280
281 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
282 {
283         params = InsetNewlineParams();
284         if (in.empty())
285                 return;
286         istringstream data(in);
287         Lexer lex;
288         lex.setStream(data);
289         lex.setContext("InsetNewline::string2params");
290         lex >> "newline";
291         params.read(lex);
292 }
293
294
295 string InsetNewline::params2string(InsetNewlineParams const & params)
296 {
297         ostringstream data;
298         data << "newline" << ' ';
299         params.write(data);
300         return data.str();
301 }
302
303
304 } // namespace lyx