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