]> git.lyx.org Git - features.git/blob - src/insets/InsetNewline.cpp
use simpler parser interface
[features.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         lex >> "\\end_inset";
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 }
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                 } else {
119                         status.enabled(true);
120                 }
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                 default:
138                         return Color_eolmarker;
139                         break;
140         }
141 }
142
143
144 int InsetNewline::latex(odocstream & os, OutputParams const &) const
145 {
146         switch (params_.kind) {
147                 case InsetNewlineParams::NEWLINE:
148                         os << "\\\\\n";
149                         break;
150                 case InsetNewlineParams::LINEBREAK:
151                         os << "\\linebreak{}\n";
152                         break;
153                 default:
154                         os << "\\\\\n";
155                         break;
156         }
157         return 0;
158 }
159
160
161 int InsetNewline::plaintext(odocstream & os, OutputParams const &) const
162 {
163         os << '\n';
164         return PLAINTEXT_NEWLINE;
165 }
166
167
168 int InsetNewline::docbook(odocstream & os, OutputParams const &) const
169 {
170         os << '\n';
171         return 0;
172 }
173
174
175 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
176 {
177         FontInfo font;
178         font.setColor(ColorName());
179
180         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
181         int const wid = fm.width('n');
182         int const asc = fm.maxAscent();
183
184         int xp[3];
185         int yp[3];
186
187         yp[0] = int(y - 0.875 * asc * 0.75);
188         yp[1] = int(y - 0.500 * asc * 0.75);
189         yp[2] = int(y - 0.125 * asc * 0.75);
190
191         if (pi.ltr_pos) {
192                 xp[0] = int(x + wid * 0.375);
193                 xp[1] = int(x);
194                 xp[2] = int(x + wid * 0.375);
195         } else {
196                 xp[0] = int(x + wid * 0.625);
197                 xp[1] = int(x + wid);
198                 xp[2] = int(x + wid * 0.625);
199         }
200
201         pi.pain.lines(xp, yp, 3, ColorName());
202
203         yp[0] = int(y - 0.500 * asc * 0.75);
204         yp[1] = int(y - 0.500 * asc * 0.75);
205         yp[2] = int(y - asc * 0.75);
206
207         if (pi.ltr_pos) {
208                 xp[0] = int(x);
209                 xp[1] = int(x + wid);
210                 xp[2] = int(x + wid);
211         } else {
212                 xp[0] = int(x + wid);
213                 xp[1] = int(x);
214                 xp[2] = int(x);
215         }
216
217         pi.pain.lines(xp, yp, 3, ColorName());
218
219         if (params_.kind == InsetNewlineParams::LINEBREAK) {
220
221                 yp[2] = int(y - 0.500 * asc * 0.75);
222
223                 if (pi.ltr_pos) {
224                         xp[0] = int(x + 1.3 * wid);
225                         xp[1] = int(x + 2 * wid);
226                         xp[2] = int(x + 2 * wid);
227                 } else {
228                         xp[0] = int(x - 0.3 * wid);
229                         xp[1] = int(x - wid);
230                         xp[2] = int(x - wid);
231                 }
232                 pi.pain.lines(xp, yp, 3, ColorName());
233
234                 yp[0] = int(y - 0.875 * asc * 0.75);
235                 yp[1] = int(y - 0.500 * asc * 0.75);
236                 yp[2] = int(y - 0.125 * asc * 0.75);
237         
238                 if (pi.ltr_pos) {
239                         xp[0] = int(x + 2 * wid * 0.813);
240                         xp[1] = int(x + 2 * wid);
241                         xp[2] = int(x + 2 * wid * 0.813);
242                 } else {
243                         xp[0] = int(x - wid * 0.625);
244                         xp[1] = int(x - wid);
245                         xp[2] = int(x - wid * 0.625);
246                 }
247                 pi.pain.lines(xp, yp, 3, ColorName());
248         }
249 }
250
251
252 docstring InsetNewline::contextMenu(BufferView const &, int, int) const
253 {
254         return from_ascii("context-newline");
255 }
256
257
258 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
259 {
260         params = InsetNewlineParams();
261         if (in.empty())
262                 return;
263         istringstream data(in);
264         Lexer lex;
265         lex.setStream(data);
266         lex.setContext("InsetNewline::string2params");
267         lex >> "newline";
268         params.read(lex);
269 }
270
271
272 string InsetNewline::params2string(InsetNewlineParams const & params)
273 {
274         ostringstream data;
275         data << "newline" << ' ';
276         params.write(data);
277         return data.str();
278 }
279
280
281 } // namespace lyx