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