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