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"
19 #include "MetricsInfo.h"
20 #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() : Inset(0)
41 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
42 : Inset(0), params_(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");
85 void InsetNewpage::write(ostream & os) const
92 void InsetNewpage::read(Lexer & lex)
99 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
101 dim.asc = defaultRowHeight();
102 dim.des = defaultRowHeight();
103 dim.wid = mi.base.textwidth;
104 // Cache the inset dimension.
105 setDimCache(mi, dim);
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;
143 string2params(to_utf8(cmd.argument()), params);
144 params_.kind = params.kind;
149 Inset::doDispatch(cur, cmd);
155 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
156 FuncStatus & status) const
158 switch (cmd.action) {
160 case LFUN_INSET_MODIFY:
161 if (cmd.getArg(0) == "newpage") {
162 InsetNewpageParams params;
163 string2params(to_utf8(cmd.argument()), params);
164 status.setOnOff(params_.kind == params.kind);
166 status.setEnabled(true);
169 return Inset::getStatus(cur, cmd, status);
174 docstring InsetNewpage::insetLabel() const
176 switch (params_.kind) {
177 case InsetNewpageParams::NEWPAGE:
178 return _("New Page");
180 case InsetNewpageParams::PAGEBREAK:
181 return _("Page Break");
183 case InsetNewpageParams::CLEARPAGE:
184 return _("Clear Page");
186 case InsetNewpageParams::CLEARDOUBLEPAGE:
187 return _("Clear Double Page");
190 return _("New Page");
196 ColorCode InsetNewpage::ColorName() const
198 switch (params_.kind) {
199 case InsetNewpageParams::PAGEBREAK:
200 return Color_pagebreak;
202 case InsetNewpageParams::NEWPAGE:
203 case InsetNewpageParams::CLEARPAGE:
204 case InsetNewpageParams::CLEARDOUBLEPAGE:
205 return Color_newpage;
208 // not really useful, but to avoids gcc complaints
209 return Color_newpage;
213 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
215 switch (params_.kind) {
216 case InsetNewpageParams::NEWPAGE:
219 case InsetNewpageParams::PAGEBREAK:
220 os << "\\pagebreak{}";
222 case InsetNewpageParams::CLEARPAGE:
223 os << "\\clearpage{}";
225 case InsetNewpageParams::CLEARDOUBLEPAGE:
226 os << "\\cleardoublepage{}";
236 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
239 return PLAINTEXT_NEWLINE;
243 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
250 docstring InsetNewpage::xhtml(XHTMLStream &, OutputParams const &) const
252 return from_ascii("<br />\n");
256 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
258 return from_ascii("context-newpage");
262 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
264 params = InsetNewpageParams();
268 istringstream data(in);
274 if (!lex || name != "newpage") {
275 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
283 string InsetNewpage::params2string(InsetNewpageParams const & params)
286 data << "newpage" << ' ';