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"
16 #include "FuncRequest.h"
17 #include "FuncStatus.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "TextMetrics.h"
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
37 InsetNewpage::InsetNewpage()
41 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
46 void InsetNewpageParams::write(ostream & os) const
50 case InsetNewpageParams::NEWPAGE:
53 case InsetNewpageParams::PAGEBREAK:
56 case InsetNewpageParams::CLEARPAGE:
59 case InsetNewpageParams::CLEARDOUBLEPAGE:
60 os << "cleardoublepage";
66 void InsetNewpageParams::read(Lexer & lex)
68 lex.setContext("InsetNewpageParams::read");
72 if (token == "newpage")
73 kind = InsetNewpageParams::NEWPAGE;
74 else if (token == "pagebreak")
75 kind = InsetNewpageParams::PAGEBREAK;
76 else if (token == "clearpage")
77 kind = InsetNewpageParams::CLEARPAGE;
78 else if (token == "cleardoublepage")
79 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
81 lex.printError("Unknown kind");
87 void InsetNewpage::write(ostream & os) const
94 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;
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.enabled(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::NEWPAGE:
202 return Color_newpage;
204 case InsetNewpageParams::PAGEBREAK:
205 return Color_pagebreak;
207 case InsetNewpageParams::CLEARPAGE:
208 return Color_newpage;
210 case InsetNewpageParams::CLEARDOUBLEPAGE:
211 return Color_newpage;
214 return Color_newpage;
220 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
222 switch (params_.kind) {
223 case InsetNewpageParams::NEWPAGE:
226 case InsetNewpageParams::PAGEBREAK:
227 os << "\\pagebreak{}";
229 case InsetNewpageParams::CLEARPAGE:
230 os << "\\clearpage{}";
232 case InsetNewpageParams::CLEARDOUBLEPAGE:
233 os << "\\cleardoublepage{}";
243 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
246 return PLAINTEXT_NEWLINE;
250 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
257 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
259 return from_ascii("context-newpage");
263 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
265 params = InsetNewpageParams();
269 istringstream data(in);
275 if (!lex || name != "newpage") {
276 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
284 string InsetNewpage::params2string(InsetNewpageParams const & params)
287 data << "newpage" << ' ';