]> git.lyx.org Git - lyx.git/blob - src/insets/InsetSeparator.cpp
Added inset-select-all to emacs bindings
[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         string command;
49         switch (kind) {
50         case InsetSeparatorParams::PLAIN:
51                 os <<  "plain";
52                 break;
53         case InsetSeparatorParams::PARBREAK:
54                 os <<  "parbreak";
55                 break;
56         }
57 }
58
59
60 void InsetSeparatorParams::read(Lexer & lex)
61 {
62         string token;
63         lex.setContext("InsetSeparatorParams::read");
64         lex >> token;
65         if (token == "plain")
66                 kind = InsetSeparatorParams::PLAIN;
67         else if (token == "parbreak")
68                 kind = InsetSeparatorParams::PARBREAK;
69         else
70                 lex.printError("Unknown kind: `$$Token'");
71 }
72
73
74 void InsetSeparator::write(ostream & os) const
75 {
76         os << "Separator ";
77         params_.write(os);
78 }
79
80
81 void InsetSeparator::read(Lexer & lex)
82 {
83         params_.read(lex);
84         lex >> "\\end_inset";
85 }
86
87
88 void InsetSeparator::doDispatch(Cursor & cur, FuncRequest & cmd)
89 {
90         switch (cmd.action()) {
91
92         case LFUN_INSET_MODIFY: {
93                 InsetSeparatorParams params;
94                 cur.recordUndo();
95                 string2params(to_utf8(cmd.argument()), params);
96                 params_.kind = params.kind;
97                 break;
98         }
99
100         default:
101                 Inset::doDispatch(cur, cmd);
102                 break;
103         }
104 }
105
106
107 bool InsetSeparator::getStatus(Cursor & cur, FuncRequest const & cmd,
108         FuncStatus & status) const
109 {
110         switch (cmd.action()) {
111         // we handle these
112         case LFUN_INSET_MODIFY: {
113                 if (cmd.getArg(0) != "separator")
114                         break;
115                 InsetSeparatorParams params;
116                 string2params(to_utf8(cmd.argument()), params);
117                 status.setOnOff(params_.kind == params.kind);
118                 status.setEnabled(true);
119                 return true;
120         }
121         default:
122                 return Inset::getStatus(cur, cmd, status);
123         }
124         return false;
125 }
126
127
128 ColorCode InsetSeparator::ColorName() const
129 {
130         return Color_latex;
131 }
132
133
134 void InsetSeparator::latex(otexstream & os, OutputParams const &) const
135 {
136         // Do nothing if a paragraph break was just output
137         if (!os.afterParbreak()) {
138                 switch (params_.kind) {
139                         case InsetSeparatorParams::PLAIN:
140                                 os << breakln << "%\n";
141                                 break;
142                         case InsetSeparatorParams::PARBREAK:
143                                 os << breakln << "\n";
144                                 break;
145                         default:
146                                 os << breakln << "%\n";
147                                 break;
148                 }
149         }
150 }
151
152
153 int InsetSeparator::plaintext(odocstringstream & os,
154         OutputParams const &, size_t) const
155 {
156         os << '\n';
157         return PLAINTEXT_NEWLINE;
158 }
159
160
161 int InsetSeparator::docbook(odocstream & os, OutputParams const &) const
162 {
163         os << '\n';
164         return 0;
165 }
166
167
168 docstring InsetSeparator::xhtml(XHTMLStream & xs, OutputParams const &) const
169 {
170         xs << html::CR() << html::CompTag("br") << html::CR();
171         return docstring();
172 }
173
174
175 void InsetSeparator::metrics(MetricsInfo & mi, Dimension & dim) const
176 {
177         frontend::FontMetrics const & fm = theFontMetrics(mi.base.font);
178         dim.asc = fm.maxAscent();
179         dim.des = fm.maxDescent();
180         dim.wid = fm.width('m');
181         if (params_.kind == InsetSeparatorParams::PLAIN)
182                 dim.wid *= 5;
183 }
184
185
186 void InsetSeparator::draw(PainterInfo & pi, int x, int y) const
187 {
188         FontInfo font;
189         font.setColor(ColorName());
190
191         frontend::FontMetrics const & fm = theFontMetrics(pi.base.font);
192         int const wid = fm.width('m');
193         int const asc = fm.maxAscent();
194
195         int xp[3];
196         int yp[3];
197
198         if (params_.kind == InsetSeparatorParams::PLAIN) {
199                 yp[0] = int(y - 0.500 * asc * 0.75);
200                 yp[1] = int(y - 0.500 * asc * 0.75);
201
202                 xp[0] = int(x);
203                 xp[1] = int(x + wid * 5);
204
205                 pi.pain.lines(xp, yp, 2, ColorName());
206         } else {
207                 yp[0] = int(y - 0.875 * asc * 0.5);
208                 yp[1] = int(y - 0.500 * asc * 0.5);
209                 yp[2] = int(y - 0.125 * asc * 0.5);
210
211                 if (pi.ltr_pos) {
212                         xp[0] = int(x + wid * 0.375);
213                         xp[1] = int(x);
214                         xp[2] = int(x + wid * 0.375);
215                 } else {
216                         xp[0] = int(x + wid * 0.625);
217                         xp[1] = int(x + wid);
218                         xp[2] = int(x + wid * 0.625);
219                 }
220
221                 pi.pain.lines(xp, yp, 3, ColorName(), Painter::fill_oddeven);
222
223                 yp[0] = int(y - 0.500 * asc * 0.5);
224                 yp[1] = int(y - 0.500 * asc * 0.5);
225                 yp[2] = int(y - asc * 0.5);
226
227                 if (pi.ltr_pos) {
228                         xp[0] = int(x);
229                         xp[1] = int(x + wid);
230                         xp[2] = int(x + wid);
231                 } else {
232                         xp[0] = int(x + wid);
233                         xp[1] = int(x);
234                         xp[2] = int(x);
235                 }
236
237                 pi.pain.lines(xp, yp, 3, ColorName());
238         }
239 }
240
241
242 string InsetSeparator::contextMenuName() const
243 {
244         return "context-separator";
245 }
246
247
248 void InsetSeparator::string2params(string const & in, InsetSeparatorParams & params)
249 {
250         params = InsetSeparatorParams();
251         if (in.empty())
252                 return;
253         istringstream data(in);
254         Lexer lex;
255         lex.setStream(data);
256         lex.setContext("InsetSeparator::string2params");
257         lex >> "separator";
258         params.read(lex);
259 }
260
261
262 string InsetSeparator::params2string(InsetSeparatorParams const & params)
263 {
264         ostringstream data;
265         data << "separator" << ' ';
266         params.write(data);
267         return data.str();
268 }
269
270
271 } // namespace lyx