]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Merge branch 'master' into biblatex2
[lyx.git] / src / insets / InsetSeparator.cpp
1 /**
2  * \file InsetSeparator.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Enrico Forestieri
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "InsetSeparator.h"
14
15 #include "Cursor.h"
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 #include "output_xhtml.h"
23 #include "texstream.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 using namespace lyx::frontend;
35
36 namespace lyx {
37
38 InsetSeparator::InsetSeparator() : Inset(0)
39 {}
40
41
42 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
43         : Inset(0), params_(params)
44 {}
45
46
47 void InsetSeparatorParams::write(ostream & os) const
48 {
49         switch (kind) {
50         case InsetSeparatorParams::PLAIN:
51                 os <<  "plain";
52                 break;
53         case InsetSeparatorParams::PARBREAK:
54                 os <<  "parbreak";
55                 break;
56         case InsetSeparatorParams::LATEXPAR:
57                 os <<  "latexpar";
58                 break;
59         }
60 }
61
62
63 void InsetSeparatorParams::read(Lexer & lex)
64 {
65         string token;
66         lex.setContext("InsetSeparatorParams::read");
67         lex >> token;
68         if (token == "plain")
69                 kind = InsetSeparatorParams::PLAIN;
70         else if (token == "parbreak")
71                 kind = InsetSeparatorParams::PARBREAK;
72         else if (token == "latexpar")
73                 kind = InsetSeparatorParams::LATEXPAR;
74         else
75                 lex.printError("Unknown kind: `$$Token'");
76 }
77
78
79 void InsetSeparator::write(ostream & os) const
80 {
81         os << "Separator ";
82         params_.write(os);
83 }
84
85
86 void InsetSeparator::read(Lexer & lex)
87 {
88         params_.read(lex);
89         lex >> "\\end_inset";
90 }
91
92
93 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
94 {
95         switch (cmd.action()) {
96
97         case LFUN_INSET_MODIFY: {
98                 InsetSeparatorParams params;
99                 cur.recordUndo();
100                 string2params(to_utf8(cmd.argument()), params);
101                 params_.kind = params.kind;
102                 break;
103         }
104
105         default:
106                 Inset::doDispatch(cur, cmd);
107                 break;
108         }
109 }
110
111
112 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
113         FuncStatus & status) const
114 {
115         switch (cmd.action()) {
116         // we handle these
117         case LFUN_INSET_MODIFY: {
118                 if (cmd.getArg(0) != "separator")
119                         break;
120                 InsetSeparatorParams params;
121                 string2params(to_utf8(cmd.argument()), params);
122                 status.setOnOff(params_.kind == params.kind);
123                 status.setEnabled(true);
124                 return true;
125         }
126         default:
127                 return Inset::getStatus(cur, cmd, status);
128         }
129         return false;
130 }
131
132
133 ColorCode InsetSeparator::ColorName() const
134 {
135         return Color_latex;
136 }
137
138
139 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
140 {
141         // Do nothing if a paragraph break was just output
142         if (!os.afterParbreak()) {
143                 switch (params_.kind) {
144                         case InsetSeparatorParams::PLAIN:
145                                 os << breakln << "%\n";
146                                 break;
147                         case InsetSeparatorParams::PARBREAK:
148                         case InsetSeparatorParams::LATEXPAR:
149                                 os << breakln << "\n";
150                                 break;
151                         default:
152                                 os << breakln << "%\n";
153                                 break;
154                 }
155         }
156 }
157
158
159 int InsetSeparator::plaintext(odocstringstream & os,
160         OutputParams const &, size_t) const
161 {
162         os << '\n';
163         return PLAINTEXT_NEWLINE;
164 }
165
166
167 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
168 {
169         os << '\n';
170         return 0;
171 }
172
173
174 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
175 {
176         xs << html::CR() << html::CompTag("br") << html::CR();
177         return docstring();
178 }
179
180
181 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
182 {
183         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
184         dim.asc = fm.maxAscent();
185         dim.des = fm.maxDescent();
186         dim.wid = fm.width('n');
187         if (params_.kind != InsetSeparatorParams::LATEXPAR)
188                 dim.wid *= 8;
189 }
190
191
192 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
193 {
194         FontInfo font;
195         font.setColor(ColorName());
196
197         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
198         int const wid = fm.width('n');
199         int const asc = fm.maxAscent();
200
201         int xp[7];
202         int yp[7];
203
204         if (params_.kind != InsetSeparatorParams::LATEXPAR) {
205                 yp[0] = int(y - 0.500 * asc * 0.75);
206                 yp[1] = yp[0];
207
208                 xp[0] = int(x);
209                 xp[1] = int(x + wid * 8);
210
211                 pi.pain.lines(xp, yp, 2, ColorName());
212
213                 if (params_.kind == InsetSeparatorParams::PARBREAK) {
214                         yp[0] += int(0.25 * asc * 0.75);
215                         yp[1] = yp[0];
216                         pi.pain.lines(xp, yp, 2, ColorName());
217                 }
218         } else {
219                 yp[0] = int(y - 0.500 * asc * 0.5);
220                 yp[1] = int(y - 0.250 * asc * 0.5);
221                 yp[2] = int(y);
222
223                 if (pi.ltr_pos) {
224                         xp[0] = int(x + 1 + wid * 0.375);
225                         xp[1] = int(x + 1);
226                 } else {
227                         xp[0] = int(x - 1 + wid * 0.625);
228                         xp[1] = int(x - 1 + wid);
229                 }
230                 xp[2] = xp[0];
231
232                 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
233
234                 yp[0] = yp[1];
235                 yp[2] = int(y - 0.850 * asc * 0.5);
236                 yp[3] = int(y - 1.250 * asc * 0.5);
237                 yp[4] = yp[3];
238                 yp[5] = yp[2];
239                 yp[6] = yp[5];
240
241                 xp[0] = xp[1];
242                 if (pi.ltr_pos) {
243                         xp[1] = int(x + 1 + wid * 0.50);
244                         xp[2] = int(x + wid);
245                         xp[3] = xp[2];
246                         xp[4] = int(x + wid * 0.75);
247                 } else {
248                         xp[1] = int(x + wid * 0.50);
249                         xp[2] = int(x);
250                         xp[3] = xp[2];
251                         xp[4] = int(x + wid * 0.25);
252                 }
253                 xp[5] = xp[4];
254                 xp[6] = xp[2];
255
256                 int c1x[7];
257                 int c1y[7];
258                 int c2x[7];
259                 int c2y[7];
260
261                 for (int i = 1; i < 7; ++i) {
262                         c1x[i] = xp[i - 1];
263                         c1y[i] = yp[i - 1];
264                         c2x[i] = xp[i];
265                         c2y[i] = yp[i];
266                 }
267
268                 int d = pi.ltr_pos ? yp[4] - yp[5] : yp[5] - yp[4];
269
270                 c1x[2] = xp[2];
271                 c2y[2] = int(y - 0.500 * asc * 0.5);
272                 c1x[5] += d;
273                 c2x[5] += d;
274
275                 pi.pain.path(xp, yp, c1x, c1y, c2x, c2y, 7, ColorName());
276         }
277 }
278
279
280 string InsetSeparator::contextMenuName() const
281 {
282         if (params_.kind == InsetSeparatorParams::LATEXPAR)
283                 return string();
284
285         return "context-separator";
286 }
287
288
289 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
290 {
291         params = InsetSeparatorParams();
292         if (in.empty())
293                 return;
294         istringstream data(in);
295         Lexer lex;
296         lex.setStream(data);
297         lex.setContext("InsetSeparator::string2params");
298         lex >> "separator";
299         params.read(lex);
300 }
301
302
303 string InsetSeparator::params2string(InsetSeparatorParams const & params)
304 {
305         ostringstream data;
306         data << "separator" << ' ';
307         params.write(data);
308         return data.str();
309 }
310
311
312 } // namespace lyx