2 * \file InsetNewpage.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Jürgen Spitzmüller
9 * Full author contact details are available in file CREDITS.
14 #include "InsetNewpage.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "texstream.h"
25 #include "TextMetrics.h"
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
30 #include "support/debug.h"
31 #include "support/docstring.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
40 InsetNewpage::InsetNewpage() : Inset(0)
44 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
45 : Inset(0), params_(params)
49 void InsetNewpageParams::write(ostream & os) const
52 case InsetNewpageParams::NEWPAGE:
55 case InsetNewpageParams::PAGEBREAK:
58 case InsetNewpageParams::CLEARPAGE:
61 case InsetNewpageParams::CLEARDOUBLEPAGE:
62 os << "cleardoublepage";
68 void InsetNewpageParams::read(Lexer & lex)
70 lex.setContext("InsetNewpageParams::read");
74 if (token == "newpage")
75 kind = InsetNewpageParams::NEWPAGE;
76 else if (token == "pagebreak")
77 kind = InsetNewpageParams::PAGEBREAK;
78 else if (token == "clearpage")
79 kind = InsetNewpageParams::CLEARPAGE;
80 else if (token == "cleardoublepage")
81 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
83 lex.printError("Unknown kind");
87 void InsetNewpage::write(ostream & os) const
94 void InsetNewpage::read(Lexer & lex)
101 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
103 dim.asc = defaultRowHeight();
104 dim.des = defaultRowHeight();
105 dim.wid = mi.base.textwidth;
109 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
111 using frontend::Painter;
114 font.setColor(ColorName());
117 Dimension const dim = dimension(*pi.base.bv);
122 theFontMetrics(font).rectText(insetLabel(), w, a, d);
124 int const text_start = int(x + (dim.wid - w) / 2);
125 int const text_end = text_start + w;
127 pi.pain.rectText(text_start, y + d, insetLabel(), font,
128 Color_none, Color_none);
130 pi.pain.line(x, y, text_start, y,
131 ColorName(), Painter::line_onoffdash);
132 pi.pain.line(text_end, y, int(x + dim.wid), y,
133 ColorName(), Painter::line_onoffdash);
137 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
139 switch (cmd.action()) {
141 case LFUN_INSET_MODIFY: {
142 InsetNewpageParams params;
144 string2params(to_utf8(cmd.argument()), params);
145 params_.kind = params.kind;
150 Inset::doDispatch(cur, cmd);
156 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
157 FuncStatus & status) const
159 switch (cmd.action()) {
161 case LFUN_INSET_MODIFY:
162 if (cmd.getArg(0) == "newpage") {
163 InsetNewpageParams params;
164 string2params(to_utf8(cmd.argument()), params);
165 status.setOnOff(params_.kind == params.kind);
167 status.setEnabled(true);
170 return Inset::getStatus(cur, cmd, status);
175 docstring InsetNewpage::insetLabel() const
177 switch (params_.kind) {
178 case InsetNewpageParams::NEWPAGE:
179 return _("New Page");
181 case InsetNewpageParams::PAGEBREAK:
182 return _("Page Break");
184 case InsetNewpageParams::CLEARPAGE:
185 return _("Clear Page");
187 case InsetNewpageParams::CLEARDOUBLEPAGE:
188 return _("Clear Double Page");
191 return _("New Page");
197 ColorCode InsetNewpage::ColorName() const
199 switch (params_.kind) {
200 case InsetNewpageParams::PAGEBREAK:
201 return Color_pagebreak;
203 case InsetNewpageParams::NEWPAGE:
204 case InsetNewpageParams::CLEARPAGE:
205 case InsetNewpageParams::CLEARDOUBLEPAGE:
206 return Color_newpage;
209 // not really useful, but to avoids gcc complaints
210 return Color_newpage;
214 void InsetNewpage::latex(otexstream & os, OutputParams const & runparams) const
216 switch (params_.kind) {
217 case InsetNewpageParams::NEWPAGE:
220 case InsetNewpageParams::PAGEBREAK:
221 if (runparams.moving_arg)
223 os << "\\pagebreak{}";
225 case InsetNewpageParams::CLEARPAGE:
226 os << "\\clearpage{}";
228 case InsetNewpageParams::CLEARDOUBLEPAGE:
229 os << "\\cleardoublepage{}";
238 int InsetNewpage::plaintext(odocstringstream & os,
239 OutputParams const &, size_t) const
242 return PLAINTEXT_NEWLINE;
246 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
253 docstring InsetNewpage::xhtml(XHTMLStream & xs, OutputParams const &) const
255 xs << html::CompTag("br");
260 string InsetNewpage::contextMenuName() const
262 return "context-newpage";
266 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
268 params = InsetNewpageParams();
272 istringstream data(in);
278 if (!lex || name != "newpage") {
279 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
287 string InsetNewpage::params2string(InsetNewpageParams const & params)
290 data << "newpage" << ' ';