]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewline.cpp
Fix bug #6315: counters in insets that don't produce output have ghost values.
[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
25 #include "frontends/Application.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstream.h"
31 #include "support/docstring.h"
32
33 using namespace std;
34
35 namespace lyx {
36
37 InsetNewline::InsetNewline() : Inset(0)
38 {}
39
40
41 void InsetNewlineParams::write(ostream & os) const
42 {
43         string command;
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 int InsetNewline::latex(odocstream & os, OutputParams const & rp) const
146 {
147         switch (params_.kind) {
148                 case InsetNewlineParams::NEWLINE:
149                         if (rp.inTableCell == OutputParams::PLAIN)
150                                 os << "\\newline\n";
151                         else
152                                 os << "\\\\\n";
153                         break;
154                 case InsetNewlineParams::LINEBREAK:
155                         os << "\\linebreak{}\n";
156                         break;
157                 default:
158                         os << "\\\\\n";
159                         break;
160         }
161         return 0;
162 }
163
164
165 int InsetNewline::plaintext(odocstream & os, OutputParams const &) const
166 {
167         os << '\n';
168         return PLAINTEXT_NEWLINE;
169 }
170
171
172 int InsetNewline::docbook(odocstream & os, OutputParams const &) const
173 {
174         os << '\n';
175         return 0;
176 }
177
178
179 docstring InsetNewline::xhtml(XHTMLStream & xs, OutputParams const &) const
180 {
181         xs << html::CompTag("br");
182         xs.cr();
183         return docstring();
184 }
185
186
187 void InsetNewline::draw(PainterInfo & pi, int x, int y) const
188 {
189         FontInfo font;
190         font.setColor(ColorName());
191
192         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
193         int const wid = fm.width('n');
194         int const asc = fm.maxAscent();
195
196         int xp[3];
197         int yp[3];
198
199         yp[0] = int(y - 0.875 * asc * 0.75);
200         yp[1] = int(y - 0.500 * asc * 0.75);
201         yp[2] = int(y - 0.125 * asc * 0.75);
202
203         if (pi.ltr_pos) {
204                 xp[0] = int(x + wid * 0.375);
205                 xp[1] = int(x);
206                 xp[2] = int(x + wid * 0.375);
207         } else {
208                 xp[0] = int(x + wid * 0.625);
209                 xp[1] = int(x + wid);
210                 xp[2] = int(x + wid * 0.625);
211         }
212
213         pi.pain.lines(xp, yp, 3, ColorName());
214
215         yp[0] = int(y - 0.500 * asc * 0.75);
216         yp[1] = int(y - 0.500 * asc * 0.75);
217         yp[2] = int(y - asc * 0.75);
218
219         if (pi.ltr_pos) {
220                 xp[0] = int(x);
221                 xp[1] = int(x + wid);
222                 xp[2] = int(x + wid);
223         } else {
224                 xp[0] = int(x + wid);
225                 xp[1] = int(x);
226                 xp[2] = int(x);
227         }
228
229         pi.pain.lines(xp, yp, 3, ColorName());
230
231         if (params_.kind == InsetNewlineParams::LINEBREAK) {
232
233                 yp[2] = int(y - 0.500 * asc * 0.75);
234
235                 if (pi.ltr_pos) {
236                         xp[0] = int(x + 1.3 * wid);
237                         xp[1] = int(x + 2 * wid);
238                         xp[2] = int(x + 2 * wid);
239                 } else {
240                         xp[0] = int(x - 0.3 * wid);
241                         xp[1] = int(x - wid);
242                         xp[2] = int(x - wid);
243                 }
244                 pi.pain.lines(xp, yp, 3, ColorName());
245
246                 yp[0] = int(y - 0.875 * asc * 0.75);
247                 yp[1] = int(y - 0.500 * asc * 0.75);
248                 yp[2] = int(y - 0.125 * asc * 0.75);
249         
250                 if (pi.ltr_pos) {
251                         xp[0] = int(x + 2 * wid * 0.813);
252                         xp[1] = int(x + 2 * wid);
253                         xp[2] = int(x + 2 * wid * 0.813);
254                 } else {
255                         xp[0] = int(x - wid * 0.625);
256                         xp[1] = int(x - wid);
257                         xp[2] = int(x - wid * 0.625);
258                 }
259                 pi.pain.lines(xp, yp, 3, ColorName());
260         }
261 }
262
263
264 docstring InsetNewline::contextMenu(BufferView const &, int, int) const
265 {
266         return from_ascii("context-newline");
267 }
268
269
270 void InsetNewline::string2params(string const & in, InsetNewlineParams & params)
271 {
272         params = InsetNewlineParams();
273         if (in.empty())
274                 return;
275         istringstream data(in);
276         Lexer lex;
277         lex.setStream(data);
278         lex.setContext("InsetNewline::string2params");
279         lex >> "newline";
280         params.read(lex);
281 }
282
283
284 string InsetNewline::params2string(InsetNewlineParams const & params)
285 {
286         ostringstream data;
287         data << "newline" << ' ';
288         params.write(data);
289         return data.str();
290 }
291
292
293 } // namespace lyx