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"
24 #include "TextMetrics.h"
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
39 InsetNewpage::InsetNewpage() : Inset(0)
43 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
44 : Inset(0), params_(params)
48 void InsetNewpageParams::write(ostream & os) const
51 case InsetNewpageParams::NEWPAGE:
54 case InsetNewpageParams::PAGEBREAK:
57 case InsetNewpageParams::CLEARPAGE:
60 case InsetNewpageParams::CLEARDOUBLEPAGE:
61 os << "cleardoublepage";
67 void InsetNewpageParams::read(Lexer & lex)
69 lex.setContext("InsetNewpageParams::read");
73 if (token == "newpage")
74 kind = InsetNewpageParams::NEWPAGE;
75 else if (token == "pagebreak")
76 kind = InsetNewpageParams::PAGEBREAK;
77 else if (token == "clearpage")
78 kind = InsetNewpageParams::CLEARPAGE;
79 else if (token == "cleardoublepage")
80 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
82 lex.printError("Unknown kind");
86 void InsetNewpage::write(ostream & os) const
93 void InsetNewpage::read(Lexer & lex)
100 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
102 dim.asc = defaultRowHeight();
103 dim.des = defaultRowHeight();
104 dim.wid = mi.base.textwidth;
105 // Cache the inset dimension.
106 setDimCache(mi, dim);
110 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
112 using frontend::Painter;
115 font.setColor(ColorName());
118 Dimension const dim = dimension(*pi.base.bv);
123 theFontMetrics(font).rectText(insetLabel(), w, a, d);
125 int const text_start = int(x + (dim.wid - w) / 2);
126 int const text_end = text_start + w;
128 pi.pain.rectText(text_start, y + d, insetLabel(), font,
129 Color_none, Color_none);
131 pi.pain.line(x, y, text_start, y,
132 ColorName(), Painter::line_onoffdash);
133 pi.pain.line(text_end, y, int(x + dim.wid), y,
134 ColorName(), Painter::line_onoffdash);
138 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
140 switch (cmd.action()) {
142 case LFUN_INSET_MODIFY: {
143 InsetNewpageParams params;
145 string2params(to_utf8(cmd.argument()), params);
146 params_.kind = params.kind;
151 Inset::doDispatch(cur, cmd);
157 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
158 FuncStatus & status) const
160 switch (cmd.action()) {
162 case LFUN_INSET_MODIFY:
163 if (cmd.getArg(0) == "newpage") {
164 InsetNewpageParams params;
165 string2params(to_utf8(cmd.argument()), params);
166 status.setOnOff(params_.kind == params.kind);
168 status.setEnabled(true);
171 return Inset::getStatus(cur, cmd, status);
176 docstring InsetNewpage::insetLabel() const
178 switch (params_.kind) {
179 case InsetNewpageParams::NEWPAGE:
180 return _("New Page");
182 case InsetNewpageParams::PAGEBREAK:
183 return _("Page Break");
185 case InsetNewpageParams::CLEARPAGE:
186 return _("Clear Page");
188 case InsetNewpageParams::CLEARDOUBLEPAGE:
189 return _("Clear Double Page");
192 return _("New Page");
198 ColorCode InsetNewpage::ColorName() const
200 switch (params_.kind) {
201 case InsetNewpageParams::PAGEBREAK:
202 return Color_pagebreak;
204 case InsetNewpageParams::NEWPAGE:
205 case InsetNewpageParams::CLEARPAGE:
206 case InsetNewpageParams::CLEARDOUBLEPAGE:
207 return Color_newpage;
210 // not really useful, but to avoids gcc complaints
211 return Color_newpage;
215 void InsetNewpage::latex(otexstream & os, OutputParams const &) const
217 switch (params_.kind) {
218 case InsetNewpageParams::NEWPAGE:
221 case InsetNewpageParams::PAGEBREAK:
222 os << "\\pagebreak{}";
224 case InsetNewpageParams::CLEARPAGE:
225 os << "\\clearpage{}";
227 case InsetNewpageParams::CLEARDOUBLEPAGE:
228 os << "\\cleardoublepage{}";
237 int InsetNewpage::plaintext(odocstringstream & os,
238 OutputParams const &, size_t) const
241 return PLAINTEXT_NEWLINE;
245 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
252 docstring InsetNewpage::xhtml(XHTMLStream & xs, OutputParams const &) const
254 xs << html::CompTag("br");
259 string InsetNewpage::contextMenuName() const
261 return "context-newpage";
265 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
267 params = InsetNewpageParams();
271 istringstream data(in);
277 if (!lex || name != "newpage") {
278 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
286 string InsetNewpage::params2string(InsetNewpageParams const & params)
289 data << "newpage" << ' ';