]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewline.cpp
Simplify code to generate only one paragraph at a time.
[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 "Lexer.h"
21 #include "MetricsInfo.h"
22 #include "OutputParams.h"
23 #include "output_docbook.h"
24 #include "output_xhtml.h"
25 #include "texstream.h"
26
27 #include "frontends/Application.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
30
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/docstring.h"
34
35 using namespace std;
36
37 namespace lyx {
38
39 InsetNewline::InsetNewline() : Inset(0)
40 {}
41
42
43 void InsetNewlineParams::write(ostream & os) const
44 {
45         switch (kind) {
46         case InsetNewlineParams::NEWLINE:
47                 os << "newline";
48                 break;
49         case InsetNewlineParams::LINEBREAK:
50                 os <<  "linebreak";
51                 break;
52         }
53 }
54
55
56 void InsetNewlineParams::read(Lexer & lex)
57 {
58         string token;
59         lex.setContext("InsetNewlineParams::read");
60         lex >> token;
61         if (token == "newline")
62                 kind = InsetNewlineParams::NEWLINE;
63         else if (token == "linebreak")
64                 kind = InsetNewlineParams::LINEBREAK;
65         else
66                 lex.printError("Unknown kind: `$$Token'");
67 }
68
69
70 void InsetNewline::write(ostream & os) const
71 {
72         os << "Newline ";
73         params_.write(os);
74 }
75
76
77 void InsetNewline::read(Lexer & lex)
78 {
79         params_.read(lex);
80         lex >> "\\end_inset";
81 }
82
83
84 void InsetNewline::metrics(MetricsInfo & mi, Dimension & dim) const
85 {
86         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
87         dim.asc = fm.maxAscent();
88         dim.des = fm.maxDescent();
89         dim.wid = fm.width('n');
90 }
91
92
93 void InsetNewline::doDispatch(Cursor & cur, FuncRequest & cmd)
94 {
95         switch (cmd.action()) {
96
97         case LFUN_INSET_MODIFY: {
98                 InsetNewlineParams 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 InsetNewline::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) == "newline") {
119                         InsetNewlineParams params;
120                         string2params(to_utf8(cmd.argument()), params);
121                         status.setOnOff(params_.kind == params.kind);
122                 }
123                 status.setEnabled(true);
124                 return true;
125         default:
126                 return Inset::getStatus(cur, cmd, status);
127         }
128 }
129
130
131 ColorCode InsetNewline::ColorName() const
132 {
133         switch (params_.kind) {
134                 case InsetNewlineParams::NEWLINE:
135                         return Color_eolmarker;
136                         break;
137                 case InsetNewlineParams::LINEBREAK:
138                         return Color_pagebreak;
139                         break;
140         }
141         // not really useful, but to avoids gcc complaints
142         return Color_eolmarker;
143 }
144
145
146 void InsetNewline::latex(otexstream & os, OutputParams const & rp) const
147 {
148         switch (params_.kind) {
149                 case InsetNewlineParams::NEWLINE:
150                         if (!rp.newlinecmd.empty())
151                                 os << "\\" << rp.newlinecmd << "\n";
152                         else if (rp.inTableCell == OutputParams::PLAIN)
153                                 os << "\\newline\n";
154                         else
155                                 os << "\\\\\n";
156                         break;
157                 case InsetNewlineParams::LINEBREAK:
158                         os << "\\linebreak{}\n";
159                         break;
160                 default:
161                         os << "\\\\\n";
162                         break;
163         }
164 }
165
166
167 int InsetNewline::plaintext(odocstringstream & os,
168         OutputParams const &, size_t) const
169 {
170         os << '\n';
171         return PLAINTEXT_NEWLINE;
172 }
173
174
175 void InsetNewline::docbook(XMLStream & xs, OutputParams const & runparams) const
176 {
177         if (runparams.docbook_in_par) {
178                 xs.closeFontTags();
179
180                 // TODO: what if within a list item, and docbookiteminnertag is not para? This would require information
181                 // about the paragraph's layout... Good for now, though, this should not happen in DocBook, only maybe
182                 // extensions.
183                 xs << XMLStream::ESCAPE_NONE << from_utf8("<!-- Is para open? " + string((xs.isTagOpen(xml::StartTag("para"))) ? "yes" : "no") +" -->");
184                 xs << XMLStream::ESCAPE_NONE << from_utf8("</para>\n<para");
185                 // TODO: that's a hack...
186 //              xs << xml::EndTag("para");
187 //              xs << xml::CR();
188 //              xs << xml::StartTag("para");
189         }
190         // Outside a paragraph, no need to handle new lines.
191 }
192
193
194 docstring InsetNewline::xhtml(XMLStream & xs, OutputParams const &) const
195 {
196         xs << xml::CR() << xml::CompTag("br") << xml::CR();
197         return docstring();
198 }
199
200
201 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
202 {
203         FontInfo font;
204         font.setColor(ColorName());
205
206         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
207         int const wid = fm.width('n');
208         int const asc = fm.maxAscent();
209
210         int xp[3];
211         int yp[3];
212
213         yp[0] = int(y - 0.875 * asc * 0.75);
214         yp[1] = int(y - 0.500 * asc * 0.75);
215         yp[2] = int(y - 0.125 * asc * 0.75);
216
217         if (pi.ltr_pos) {
218                 xp[0] = int(x + wid * 0.375);
219                 xp[1] = int(x);
220                 xp[2] = int(x + wid * 0.375);
221         } else {
222                 xp[0] = int(x + wid * 0.625);
223                 xp[1] = int(x + wid);
224                 xp[2] = int(x + wid * 0.625);
225         }
226
227         pi.pain.lines(xp, yp, 3, ColorName());
228
229         yp[0] = int(y - 0.500 * asc * 0.75);
230         yp[1] = int(y - 0.500 * asc * 0.75);
231         yp[2] = int(y - asc * 0.75);
232
233         if (pi.ltr_pos) {
234                 xp[0] = int(x);
235                 xp[1] = int(x + wid);
236                 xp[2] = int(x + wid);
237         } else {
238                 xp[0] = int(x + wid);
239                 xp[1] = int(x);
240                 xp[2] = int(x);
241         }
242
243         pi.pain.lines(xp, yp, 3, ColorName());
244
245         if (params_.kind == InsetNewlineParams::LINEBREAK) {
246
247                 yp[2] = int(y - 0.500 * asc * 0.75);
248
249                 if (pi.ltr_pos) {
250                         xp[0] = int(x + 1.3 * wid);
251                         xp[1] = int(x + 2 * wid);
252                         xp[2] = int(x + 2 * wid);
253                 } else {
254                         xp[0] = int(x - 0.3 * wid);
255                         xp[1] = int(x - wid);
256                         xp[2] = int(x - wid);
257                 }
258                 pi.pain.lines(xp, yp, 3, ColorName());
259
260                 yp[0] = int(y - 0.875 * asc * 0.75);
261                 yp[1] = int(y - 0.500 * asc * 0.75);
262                 yp[2] = int(y - 0.125 * asc * 0.75);
263
264                 if (pi.ltr_pos) {
265                         xp[0] = int(x + 2 * wid * 0.813);
266                         xp[1] = int(x + 2 * wid);
267                         xp[2] = int(x + 2 * wid * 0.813);
268                 } else {
269                         xp[0] = int(x - wid * 0.625);
270                         xp[1] = int(x - wid);
271                         xp[2] = int(x - wid * 0.625);
272                 }
273                 pi.pain.lines(xp, yp, 3, ColorName());
274         }
275 }
276
277
278 string InsetNewline::contextMenuName() const
279 {
280         return "context-newline";
281 }
282
283
284 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
285 {
286         params = InsetNewlineParams();
287         if (in.empty())
288                 return;
289         istringstream data(in);
290         Lexer lex;
291         lex.setStream(data);
292         lex.setContext("InsetNewline::string2params");
293         lex >> "newline";
294         params.read(lex);
295 }
296
297
298 string InsetNewline::params2string(InsetNewlineParams const & params)
299 {
300         ostringstream data;
301         data << "newline" << ' ';
302         params.write(data);
303         return data.str();
304 }
305
306
307 } // namespace lyx