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