]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
Get rid of Qt resources
[lyx.git] / src / insets / InsetNewpage.cpp
1 /**
2  * \file InsetNewpage.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  * \author Jürgen Spitzmüller
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "InsetNewpage.h"
15
16 #include "Cursor.h"
17 #include "FuncRequest.h"
18 #include "FuncStatus.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "output_xhtml.h"
23 #include "texstream.h"
24 #include "Text.h"
25 #include "TextMetrics.h"
26
27 #include "frontends/FontMetrics.h"
28 #include "frontends/Painter.h"
29
30 #include "support/debug.h"
31 #include "support/docstring.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34
35 using namespace std;
36
37
38 namespace lyx {
39
40         InsetNewpage::InsetNewpage() : Inset(0)
41 {}
42
43
44 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
45         : Inset(0), params_(params)
46 {}
47
48
49 void InsetNewpageParams::write(ostream & os) const
50 {
51         switch (kind) {
52         case InsetNewpageParams::NEWPAGE:
53                 os << "newpage";
54                 break;
55         case InsetNewpageParams::PAGEBREAK:
56                 os <<  "pagebreak";
57                 break;
58         case InsetNewpageParams::CLEARPAGE:
59                 os <<  "clearpage";
60                 break;
61         case InsetNewpageParams::CLEARDOUBLEPAGE:
62                 os <<  "cleardoublepage";
63                 break;
64         }
65 }
66
67
68 void InsetNewpageParams::read(Lexer & lex)
69 {
70         lex.setContext("InsetNewpageParams::read");
71         string token;
72         lex >> token;
73
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;
82         else
83                 lex.printError("Unknown kind");
84 }
85
86
87 void InsetNewpage::write(ostream & os) const
88 {
89         os << "Newpage ";
90         params_.write(os);
91 }
92
93
94 void InsetNewpage::read(Lexer & lex)
95 {
96         params_.read(lex);
97         lex >> "\\end_inset";
98 }
99
100
101 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
102 {
103         dim.asc = defaultRowHeight();
104         dim.des = defaultRowHeight();
105         dim.wid = mi.base.textwidth;
106 }
107
108
109 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
110 {
111         using frontend::Painter;
112
113         FontInfo font;
114         font.setColor(ColorName());
115         font.decSize();
116
117         Dimension const dim = dimension(*pi.base.bv);
118
119         int w = 0;
120         int a = 0;
121         int d = 0;
122         theFontMetrics(font).rectText(insetLabel(), w, a, d);
123
124         int const text_start = int(x + (dim.wid - w) / 2);
125         int const text_end = text_start + w;
126
127         pi.pain.rectText(text_start, y + d, insetLabel(), font,
128                 Color_none, Color_none);
129
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);
134 }
135
136
137 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
138 {
139         switch (cmd.action()) {
140
141         case LFUN_INSET_MODIFY: {
142                 InsetNewpageParams params;
143                 cur.recordUndo();
144                 string2params(to_utf8(cmd.argument()), params);
145                 params_.kind = params.kind;
146                 break;
147         }
148
149         default:
150                 Inset::doDispatch(cur, cmd);
151                 break;
152         }
153 }
154
155
156 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
157         FuncStatus & status) const
158 {
159         switch (cmd.action()) {
160         // we handle these
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);
166                 }
167                 status.setEnabled(true);
168                 return true;
169         default:
170                 return Inset::getStatus(cur, cmd, status);
171         }
172 }
173
174
175 docstring InsetNewpage::insetLabel() const
176 {
177         switch (params_.kind) {
178                 case InsetNewpageParams::NEWPAGE:
179                         return _("New Page");
180                         break;
181                 case InsetNewpageParams::PAGEBREAK:
182                         return _("Page Break");
183                         break;
184                 case InsetNewpageParams::CLEARPAGE:
185                         return _("Clear Page");
186                         break;
187                 case InsetNewpageParams::CLEARDOUBLEPAGE:
188                         return _("Clear Double Page");
189                         break;
190                 default:
191                         return _("New Page");
192                         break;
193         }
194 }
195
196
197 ColorCode InsetNewpage::ColorName() const
198 {
199         switch (params_.kind) {
200                 case InsetNewpageParams::PAGEBREAK:
201                         return Color_pagebreak;
202                         break;
203                 case InsetNewpageParams::NEWPAGE:
204                 case InsetNewpageParams::CLEARPAGE:
205                 case InsetNewpageParams::CLEARDOUBLEPAGE:
206                         return Color_newpage;
207                         break;
208         }
209         // not really useful, but to avoids gcc complaints
210         return Color_newpage;
211 }
212
213
214 void InsetNewpage::latex(otexstream & os, OutputParams const & runparams) const
215 {
216         if (runparams.inDeletedInset) {
217                 os << "\\mbox{}\\\\\\makebox[\\columnwidth]{\\dotfill\\ "
218                    << insetLabel() << "\\ \\dotfill}";
219         } else {
220                 switch (params_.kind) {
221                 case InsetNewpageParams::NEWPAGE:
222                         os << "\\newpage" << termcmd;
223                         break;
224                 case InsetNewpageParams::PAGEBREAK:
225                         if (runparams.moving_arg)
226                                 os << "\\protect";
227                         os << "\\pagebreak" << termcmd;
228                         break;
229                 case InsetNewpageParams::CLEARPAGE:
230                         os << "\\clearpage" << termcmd;
231                         break;
232                 case InsetNewpageParams::CLEARDOUBLEPAGE:
233                         os << "\\cleardoublepage" << termcmd;
234                         break;
235                 default:
236                         os << "\\newpage" << termcmd;
237                         break;
238                 }
239         }
240 }
241
242
243 int InsetNewpage::plaintext(odocstringstream & os,
244         OutputParams const &, size_t) const
245 {
246         os << '\n';
247         return PLAINTEXT_NEWLINE;
248 }
249
250
251 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
252 {
253         os << '\n';
254         return 0;
255 }
256
257
258 docstring InsetNewpage::xhtml(XHTMLStream & xs, OutputParams const &) const
259 {
260         xs << html::CompTag("br");
261         return docstring();
262 }
263
264
265 string InsetNewpage::contextMenuName() const
266 {
267         return "context-newpage";
268 }
269
270
271 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
272 {
273         params = InsetNewpageParams();
274         if (in.empty())
275                 return;
276
277         istringstream data(in);
278         Lexer lex;
279         lex.setStream(data);
280
281         string name;
282         lex >> name;
283         if (!lex || name != "newpage") {
284                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
285                 return;
286         }
287
288         params.read(lex);
289 }
290
291
292 string InsetNewpage::params2string(InsetNewpageParams const & params)
293 {
294         ostringstream data;
295         data << "newpage" << ' ';
296         params.write(data);
297         return data.str();
298 }
299
300
301 } // namespace lyx