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