]> git.lyx.org Git - features.git/blob - src/insets/InsetSeparator.cpp
Prettify the output of InsetSeparator.
[features.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
34 namespace lyx {
35
36 InsetSeparator::InsetSeparator() : Inset(0)
37 {}
38
39
40 InsetSeparator::InsetSeparator(InsetSeparatorParams const & params)
41         : Inset(0), params_(params)
42 {}
43
44
45 void InsetSeparatorParams::write(ostream & os) const
46 {
47         string command;
48         switch (kind) {
49         case InsetSeparatorParams::PLAIN:
50                 os <<  "plain";
51                 break;
52         case InsetSeparatorParams::PARBREAK:
53                 os <<  "parbreak";
54                 break;
55         }
56 }
57
58
59 void InsetSeparatorParams::read(Lexer & lex)
60 {
61         string token;
62         lex.setContext("InsetSeparatorParams::read");
63         lex >> token;   
64         if (token == "plain")
65                 kind = InsetSeparatorParams::PLAIN;
66         else if (token == "parbreak")
67                 kind = InsetSeparatorParams::PARBREAK;
68         else
69                 lex.printError("Unknown kind: `$$Token'");
70 }
71
72
73 void InsetSeparator::write(ostream & os) const
74 {
75         os << "Separator ";
76         params_.write(os);
77 }
78
79
80 void InsetSeparator::read(Lexer & lex)
81 {
82         params_.read(lex);
83         lex >> "\\end_inset";
84 }
85
86
87 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
88 {
89         switch (cmd.action()) {
90
91         case LFUN_INSET_MODIFY: {
92                 InsetSeparatorParams params;
93                 cur.recordUndo();
94                 string2params(to_utf8(cmd.argument()), params);
95                 params_.kind = params.kind;
96                 break;
97         }
98
99         default:
100                 Inset::doDispatch(cur, cmd);
101                 break;
102         }
103 }
104
105
106 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
107         FuncStatus & status) const
108 {
109         switch (cmd.action()) {
110         // we handle these
111         case LFUN_INSET_MODIFY:
112                 if (cmd.getArg(0) == "plain") {
113                         InsetSeparatorParams params;
114                         string2params(to_utf8(cmd.argument()), params);
115                         status.setOnOff(params_.kind == params.kind);
116                 }
117                 status.setEnabled(true);
118                 return true;
119         default:
120                 return Inset::getStatus(cur, cmd, status);
121         }
122 }
123
124
125 ColorCode InsetSeparator::ColorName() const
126 {
127         switch (params_.kind) {
128                 case InsetSeparatorParams::PLAIN:
129                         return Color_latex;
130                         break;
131                 case InsetSeparatorParams::PARBREAK:
132                         return Color_pagebreak;
133                         break;
134         }
135         // not really useful, but to avoids gcc complaints
136         return Color_latex;
137 }
138
139
140 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
141 {
142         // Do nothing if a paragraph break was just output
143         if (!os.afterParbreak()) {
144                 switch (params_.kind) {
145                         case InsetSeparatorParams::PLAIN:
146                                 os << breakln << "%\n";
147                                 break;
148                         case InsetSeparatorParams::PARBREAK:
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('m');
187         if (params_.kind == InsetSeparatorParams::PLAIN)
188                 dim.wid *= 5;
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('m');
199         int const asc = fm.maxAscent();
200
201         int xp[3];
202         int yp[3];
203
204         if (params_.kind == InsetSeparatorParams::PLAIN) {
205                 yp[0] = int(y - 0.500 * asc * 0.75);
206                 yp[1] = int(y - 0.500 * asc * 0.75);
207
208                 xp[0] = int(x);
209                 xp[1] = int(x + wid * 5);
210
211                 pi.pain.lines(xp, yp, 2, ColorName());
212         } else {
213                 yp[0] = int(y - 0.875 * asc * 0.75);
214                 yp[1] = int(y - 0.500 * asc * 0.75);
215                 yp[2] = int(y - 0.125 * asc * 0.75);
216
217                 if (pi.ltr_pos) {
218                         xp[0] = int(x + wid * 0.375);
219                         xp[1] = int(x);
220                         xp[2] = int(x + wid * 0.375);
221                 } else {
222                         xp[0] = int(x + wid * 0.625);
223                         xp[1] = int(x + wid);
224                         xp[2] = int(x + wid * 0.625);
225                 }
226
227                 pi.pain.lines(xp, yp, 3, ColorName());
228
229                 yp[0] = int(y - 0.500 * asc * 0.75);
230                 yp[1] = int(y - 0.500 * asc * 0.75);
231                 yp[2] = int(y - asc * 0.75);
232
233                 if (pi.ltr_pos) {
234                         xp[0] = int(x);
235                         xp[1] = int(x + wid);
236                         xp[2] = int(x + wid);
237                 } else {
238                         xp[0] = int(x + wid);
239                         xp[1] = int(x);
240                         xp[2] = int(x);
241                 }
242
243                 pi.pain.lines(xp, yp, 3, ColorName());
244         }
245 }
246
247
248 string InsetSeparator::contextMenuName() const
249 {
250         return "context-separator";
251 }
252
253
254 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
255 {
256         params = InsetSeparatorParams();
257         if (in.empty())
258                 return;
259         istringstream data(in);
260         Lexer lex;
261         lex.setStream(data);
262         lex.setContext("InsetSeparator::string2params");
263         lex >> "separator";
264         params.read(lex);
265 }
266
267
268 string InsetSeparator::params2string(InsetSeparatorParams const & params)
269 {
270         ostringstream data;
271         data << "separator" << ' ';
272         params.write(data);
273         return data.str();
274 }
275
276
277 } // namespace lyx