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)
69 string const command = lex.getString();
71 if (command == "newpage")
72 kind = InsetNewpageParams::NEWPAGE;
73 else if (command == "pagebreak")
74 kind = InsetNewpageParams::PAGEBREAK;
75 else if (command == "clearpage")
76 kind = InsetNewpageParams::CLEARPAGE;
77 else if (command == "cleardoublepage")
78 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
80 lex.printError("InsetNewpage: Unknown kind: `$$Token'");
86 if (token != "\\end_inset")
87 lex.printError("Missing \\end_inset at this point. "
92 void InsetNewpage::write(ostream & os) const
99 void InsetNewpage::read(Lexer & lex)
105 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
107 dim.asc = defaultRowHeight();
108 dim.des = defaultRowHeight();
109 dim.wid = mi.base.textwidth;
110 // Cache the inset dimension.
111 setDimCache(mi, dim);
115 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
117 using frontend::Painter;
120 font.setColor(ColorName());
123 Dimension const dim = dimension(*pi.base.bv);
128 theFontMetrics(font).rectText(insetLabel(), w, a, d);
130 int const text_start = int(x + (dim.wid - w) / 2);
131 int const text_end = text_start + w;
133 pi.pain.rectText(text_start, y + d, insetLabel(), font,
134 Color_none, Color_none);
136 pi.pain.line(x, y, text_start, y,
137 ColorName(), Painter::line_onoffdash);
138 pi.pain.line(text_end, y, int(x + dim.wid), y,
139 ColorName(), Painter::line_onoffdash);
143 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
145 switch (cmd.action) {
147 case LFUN_INSET_MODIFY: {
148 InsetNewpageParams params;
149 string2params(to_utf8(cmd.argument()), params);
150 params_.kind = params.kind;
155 Inset::doDispatch(cur, cmd);
161 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
162 FuncStatus & status) const
164 switch (cmd.action) {
166 case LFUN_INSET_MODIFY:
167 if (cmd.getArg(0) == "newpage") {
168 InsetNewpageParams params;
169 string2params(to_utf8(cmd.argument()), params);
170 status.setOnOff(params_.kind == params.kind);
172 status.enabled(true);
176 return Inset::getStatus(cur, cmd, status);
181 docstring InsetNewpage::insetLabel() const
183 switch (params_.kind) {
184 case InsetNewpageParams::NEWPAGE:
185 return _("New Page");
187 case InsetNewpageParams::PAGEBREAK:
188 return _("Page Break");
190 case InsetNewpageParams::CLEARPAGE:
191 return _("Clear Page");
193 case InsetNewpageParams::CLEARDOUBLEPAGE:
194 return _("Clear Double Page");
197 return _("New Page");
203 ColorCode InsetNewpage::ColorName() const
205 switch (params_.kind) {
206 case InsetNewpageParams::NEWPAGE:
207 return Color_newpage;
209 case InsetNewpageParams::PAGEBREAK:
210 return Color_pagebreak;
212 case InsetNewpageParams::CLEARPAGE:
213 return Color_newpage;
215 case InsetNewpageParams::CLEARDOUBLEPAGE:
216 return Color_newpage;
219 return Color_newpage;
225 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
227 switch (params_.kind) {
228 case InsetNewpageParams::NEWPAGE:
231 case InsetNewpageParams::PAGEBREAK:
232 os << "\\pagebreak{}";
234 case InsetNewpageParams::CLEARPAGE:
235 os << "\\clearpage{}";
237 case InsetNewpageParams::CLEARDOUBLEPAGE:
238 os << "\\cleardoublepage{}";
248 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
251 return PLAINTEXT_NEWLINE;
255 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
262 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
264 return from_ascii("context-newpage");
268 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
270 params = InsetNewpageParams();
274 istringstream data(in);
280 if (!lex || name != "newpage") {
281 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
289 string InsetNewpage::params2string(InsetNewpageParams const & params)
292 data << "newpage" << ' ';