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
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;
106 // Cache the inset dimension.
107 setDimCache(mi, dim);
111 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
113 using frontend::Painter;
116 font.setColor(ColorName());
119 Dimension const dim = dimension(*pi.base.bv);
124 theFontMetrics(font).rectText(insetLabel(), w, a, d);
126 int const text_start = int(x + (dim.wid - w) / 2);
127 int const text_end = text_start + w;
129 pi.pain.rectText(text_start, y + d, insetLabel(), font,
130 Color_none, Color_none);
132 pi.pain.line(x, y, text_start, y,
133 ColorName(), Painter::line_onoffdash);
134 pi.pain.line(text_end, y, int(x + dim.wid), y,
135 ColorName(), Painter::line_onoffdash);
139 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
141 switch (cmd.action()) {
143 case LFUN_INSET_MODIFY: {
144 InsetNewpageParams params;
146 string2params(to_utf8(cmd.argument()), params);
147 params_.kind = params.kind;
152 Inset::doDispatch(cur, cmd);
158 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
159 FuncStatus & status) const
161 switch (cmd.action()) {
163 case LFUN_INSET_MODIFY:
164 if (cmd.getArg(0) == "newpage") {
165 InsetNewpageParams params;
166 string2params(to_utf8(cmd.argument()), params);
167 status.setOnOff(params_.kind == params.kind);
169 status.setEnabled(true);
172 return Inset::getStatus(cur, cmd, status);
177 docstring InsetNewpage::insetLabel() const
179 switch (params_.kind) {
180 case InsetNewpageParams::NEWPAGE:
181 return _("New Page");
183 case InsetNewpageParams::PAGEBREAK:
184 return _("Page Break");
186 case InsetNewpageParams::CLEARPAGE:
187 return _("Clear Page");
189 case InsetNewpageParams::CLEARDOUBLEPAGE:
190 return _("Clear Double Page");
193 return _("New Page");
199 ColorCode InsetNewpage::ColorName() const
201 switch (params_.kind) {
202 case InsetNewpageParams::PAGEBREAK:
203 return Color_pagebreak;
205 case InsetNewpageParams::NEWPAGE:
206 case InsetNewpageParams::CLEARPAGE:
207 case InsetNewpageParams::CLEARDOUBLEPAGE:
208 return Color_newpage;
211 // not really useful, but to avoids gcc complaints
212 return Color_newpage;
216 void InsetNewpage::latex(otexstream & os, OutputParams const &) const
218 switch (params_.kind) {
219 case InsetNewpageParams::NEWPAGE:
222 case InsetNewpageParams::PAGEBREAK:
223 os << "\\pagebreak{}";
225 case InsetNewpageParams::CLEARPAGE:
226 os << "\\clearpage{}";
228 case InsetNewpageParams::CLEARDOUBLEPAGE:
229 os << "\\cleardoublepage{}";
238 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) 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 docstring InsetNewpage::contextMenuName() const
261 return from_ascii("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" << ' ';