]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
46ad3bdbd5ba8ac634e7b443fe2f90a863c147de
[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 "FuncRequest.h"
17 #include "FuncStatus.h"
18 #include "Lexer.h"
19 #include "MetricsInfo.h"
20 #include "OutputParams.h"
21 #include "output_xhtml.h"
22 #include "Text.h"
23 #include "TextMetrics.h"
24
25 #include "frontends/FontMetrics.h"
26 #include "frontends/Painter.h"
27
28 #include "support/debug.h"
29 #include "support/docstring.h"
30 #include "support/docstream.h"
31 #include "support/gettext.h"
32
33 using namespace std;
34
35
36 namespace lyx {
37
38         InsetNewpage::InsetNewpage() : Inset(0)
39 {}
40
41
42 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
43         : Inset(0), params_(params)
44 {}
45
46
47 void InsetNewpageParams::write(ostream & os) const
48 {
49         string command;
50         switch (kind) {
51         case InsetNewpageParams::NEWPAGE:
52                 os << "newpage";
53                 break;
54         case InsetNewpageParams::PAGEBREAK:
55                 os <<  "pagebreak";
56                 break;
57         case InsetNewpageParams::CLEARPAGE:
58                 os <<  "clearpage";
59                 break;
60         case InsetNewpageParams::CLEARDOUBLEPAGE:
61                 os <<  "cleardoublepage";
62                 break;
63         }
64 }
65
66
67 void InsetNewpageParams::read(Lexer & lex)
68 {
69         lex.setContext("InsetNewpageParams::read");
70         string token;
71         lex >> token;
72
73         if (token == "newpage")
74                 kind = InsetNewpageParams::NEWPAGE;
75         else if (token == "pagebreak")
76                 kind = InsetNewpageParams::PAGEBREAK;
77         else if (token == "clearpage")
78                 kind = InsetNewpageParams::CLEARPAGE;
79         else if (token == "cleardoublepage")
80                 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
81         else
82                 lex.printError("Unknown kind");
83 }
84
85
86 void InsetNewpage::write(ostream & os) const
87 {
88         os << "Newpage ";
89         params_.write(os);
90 }
91
92
93 void InsetNewpage::read(Lexer & lex)
94 {
95         params_.read(lex);
96         lex >> "\\end_inset";
97 }
98
99
100 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
101 {
102         dim.asc = defaultRowHeight();
103         dim.des = defaultRowHeight();
104         dim.wid = mi.base.textwidth;
105         // Cache the inset dimension. 
106         setDimCache(mi, dim);
107 }
108
109
110 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
111 {
112         using frontend::Painter;
113
114         FontInfo font;
115         font.setColor(ColorName());
116         font.decSize();
117
118         Dimension const dim = dimension(*pi.base.bv);
119
120         int w = 0;
121         int a = 0;
122         int d = 0;
123         theFontMetrics(font).rectText(insetLabel(), w, a, d);
124
125         int const text_start = int(x + (dim.wid - w) / 2);
126         int const text_end = text_start + w;
127
128         pi.pain.rectText(text_start, y + d, insetLabel(), font,
129                 Color_none, Color_none);
130
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);
135 }
136
137
138 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
139 {
140         switch (cmd.action_) {
141
142         case LFUN_INSET_MODIFY: {
143                 InsetNewpageParams params;
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 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
215 {
216         switch (params_.kind) {
217                 case InsetNewpageParams::NEWPAGE:
218                         os << "\\newpage{}";
219                         break;
220                 case InsetNewpageParams::PAGEBREAK:
221                         os << "\\pagebreak{}";
222                         break;
223                 case InsetNewpageParams::CLEARPAGE:
224                         os << "\\clearpage{}";
225                         break;
226                 case InsetNewpageParams::CLEARDOUBLEPAGE:
227                         os << "\\cleardoublepage{}";
228                         break;
229                 default:
230                         os << "\\newpage{}";
231                         break;
232         }
233         return 0;
234 }
235
236
237 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
238 {
239         os << '\n';
240         return PLAINTEXT_NEWLINE;
241 }
242
243
244 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
245 {
246         os << '\n';
247         return 0;
248 }
249
250
251 docstring InsetNewpage::xhtml(XHTMLStream & xs, OutputParams const &) const
252 {
253         xs << html::CompTag("br");
254         return docstring();
255 }
256
257
258 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
259 {
260         return from_ascii("context-newpage");
261 }
262
263
264 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
265 {
266         params = InsetNewpageParams();
267         if (in.empty())
268                 return;
269
270         istringstream data(in);
271         Lexer lex;
272         lex.setStream(data);
273
274         string name;
275         lex >> name;
276         if (!lex || name != "newpage") {
277                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
278                 return;
279         }
280
281         params.read(lex);
282 }
283
284
285 string InsetNewpage::params2string(InsetNewpageParams const & params)
286 {
287         ostringstream data;
288         data << "newpage" << ' ';
289         params.write(data);
290         return data.str();
291 }
292
293
294 } // namespace lyx