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