]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
more border tweaks
[lyx.git] / src / insets / InsetNewpage.cpp
1 /**
2  * \file InsetNewpage.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
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 "InsetNewpage.h"
15
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "Text.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "TextMetrics.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31
32 using namespace std;
33
34
35 namespace lyx {
36
37 InsetNewpage::InsetNewpage()
38 {}
39
40
41 InsetNewpage::InsetNewpage(InsetNewpageParams par)
42 {
43         params_.kind = par.kind;
44 }
45
46 void InsetNewpageParams::write(ostream & os) const
47 {
48         string command;
49         switch (kind) {
50         case InsetNewpageParams::NEWPAGE:
51                 os << "newpage";
52                 break;
53         case InsetNewpageParams::PAGEBREAK:
54                 os <<  "pagebreak";
55                 break;
56         case InsetNewpageParams::CLEARPAGE:
57                 os <<  "clearpage";
58                 break;
59         case InsetNewpageParams::CLEARDOUBLEPAGE:
60                 os <<  "cleardoublepage";
61                 break;
62         }
63 }
64
65
66 void InsetNewpageParams::read(Lexer & lex)
67 {
68         lex.next();
69         string const command = lex.getString();
70
71         if (command == "newpage")
72                 kind = InsetNewpageParams::NEWPAGE;
73         else if (command == "pagebreak")
74                 kind = InsetNewpageParams::PAGEBREAK;
75         else if (command == "clearpage")
76                 kind = InsetNewpageParams::CLEARPAGE;
77         else if (command == "cleardoublepage")
78                 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
79         else
80                 lex.printError("InsetNewpage: Unknown kind: `$$Token'");
81
82         string token;
83         lex >> token;
84         if (!lex)
85                 return;
86         if (token != "\\end_inset")
87                 lex.printError("Missing \\end_inset at this point. "
88                                "Read: `$$Token'");
89 }
90
91
92 void InsetNewpage::write(ostream & os) const
93 {
94         os << "Newpage ";
95         params_.write(os);
96 }
97
98
99 void InsetNewpage::read(Lexer & lex)
100 {
101         params_.read(lex);
102 }
103
104
105 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         dim.asc = defaultRowHeight();
108         dim.des = defaultRowHeight();
109         dim.wid = mi.base.textwidth;
110         // Cache the inset dimension. 
111         setDimCache(mi, dim);
112 }
113
114
115 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
116 {
117         using frontend::Painter;
118
119         FontInfo font;
120         font.setColor(ColorName());
121         font.decSize();
122
123         Dimension const dim = dimension(*pi.base.bv);
124
125         int w = 0;
126         int a = 0;
127         int d = 0;
128         theFontMetrics(font).rectText(insetLabel(), w, a, d);
129
130         int const text_start = int(x + (dim.wid - w) / 2);
131         int const text_end = text_start + w;
132
133         pi.pain.rectText(text_start, y + d, insetLabel(), font,
134                 Color_none, Color_none);
135
136         pi.pain.line(x, y, text_start, y,
137                    ColorName(), Painter::line_onoffdash);
138         pi.pain.line(text_end, y, int(x + dim.wid), y,
139                    ColorName(), Painter::line_onoffdash);
140 }
141
142
143 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
144 {
145         switch (cmd.action) {
146
147         case LFUN_INSET_MODIFY: {
148                 InsetNewpageParams params;
149                 InsetNewpageMailer::string2params(to_utf8(cmd.argument()), params);
150                 params_.kind = params.kind;
151                 break;
152         }
153
154         default:
155                 Inset::doDispatch(cur, cmd);
156                 break;
157         }
158 }
159
160
161 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
162         FuncStatus & status) const
163 {
164         switch (cmd.action) {
165         // we handle these
166         case LFUN_INSET_MODIFY:
167                 if (cmd.getArg(0) == "newpage") {
168                         InsetNewpageParams params;
169                         InsetNewpageMailer::string2params(to_utf8(cmd.argument()), params);
170                         status.setOnOff(params_.kind == params.kind);
171                 } else
172                         status.enabled(true);
173                 return true;
174         default:
175                 return Inset::getStatus(cur, cmd, status);
176         }
177 }
178
179
180 docstring InsetNewpage::insetLabel() const
181 {
182         switch (params_.kind) {
183                 case InsetNewpageParams::NEWPAGE:
184                         return _("New Page");
185                         break;
186                 case InsetNewpageParams::PAGEBREAK:
187                         return _("Page Break");
188                         break;
189                 case InsetNewpageParams::CLEARPAGE:
190                         return _("Clear Page");
191                         break;
192                 case InsetNewpageParams::CLEARDOUBLEPAGE:
193                         return _("Clear Double Page");
194                         break;
195                 default:
196                         return _("New Page");
197                         break;
198         }
199 }
200
201
202 ColorCode InsetNewpage::ColorName() const
203 {
204         switch (params_.kind) {
205                 case InsetNewpageParams::NEWPAGE:
206                         return Color_newpage;
207                         break;
208                 case InsetNewpageParams::PAGEBREAK:
209                         return Color_pagebreak;
210                         break;
211                 case InsetNewpageParams::CLEARPAGE:
212                         return Color_newpage;
213                         break;
214                 case InsetNewpageParams::CLEARDOUBLEPAGE:
215                         return Color_newpage;
216                         break;
217                 default:
218                         return Color_newpage;
219                         break;
220         }
221 }
222
223
224 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
225 {
226         switch (params_.kind) {
227                 case InsetNewpageParams::NEWPAGE:
228                         os << "\\newpage{}";
229                         break;
230                 case InsetNewpageParams::PAGEBREAK:
231                         os << "\\pagebreak{}";
232                         break;
233                 case InsetNewpageParams::CLEARPAGE:
234                         os << "\\clearpage{}";
235                         break;
236                 case InsetNewpageParams::CLEARDOUBLEPAGE:
237                         os << "\\cleardoublepage{}";
238                         break;
239                 default:
240                         os << "\\newpage{}";
241                         break;
242         }
243         return 0;
244 }
245
246
247 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
248 {
249         os << '\n';
250         return PLAINTEXT_NEWLINE;
251 }
252
253
254 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
255 {
256         os << '\n';
257         return 0;
258 }
259
260
261 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
262 {
263         return from_ascii("context-newpage");
264 }
265
266
267 string const InsetNewpageMailer::name_ = "newpage";
268
269
270 InsetNewpageMailer::InsetNewpageMailer(InsetNewpage & inset)
271         : inset_(inset)
272 {}
273
274
275 string const InsetNewpageMailer::inset2string(Buffer const &) const
276 {
277         return params2string(inset_.params());
278 }
279
280
281 void InsetNewpageMailer::string2params(string const & in, InsetNewpageParams & params)
282 {
283         params = InsetNewpageParams();
284         if (in.empty())
285                 return;
286
287         istringstream data(in);
288         Lexer lex(0,0);
289         lex.setStream(data);
290
291         string name;
292         lex >> name;
293         if (!lex || name != name_)
294                 return print_mailer_error("InsetNewpageMailer", in, 1, name_);
295
296         params.read(lex);
297 }
298
299
300 string const InsetNewpageMailer::params2string(InsetNewpageParams const & params)
301 {
302         ostringstream data;
303         data << name_ << ' ';
304         params.write(data);
305         return data.str();
306 }
307
308
309 } // namespace lyx