]> git.lyx.org Git - features.git/blob - src/insets/InsetNewpage.cpp
e7b9b0523a2ac251187ef7da76a9a5a23b16de43
[features.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 "Text.h"
19 #include "Lexer.h"
20 #include "MetricsInfo.h"
21 #include "OutputParams.h"
22 #include "TextMetrics.h"
23
24 #include "frontends/FontMetrics.h"
25 #include "frontends/Painter.h"
26
27 #include "support/debug.h"
28 #include "support/docstring.h"
29 #include "support/docstream.h"
30 #include "support/gettext.h"
31
32 using namespace std;
33
34
35 namespace lyx {
36
37 InsetNewpage::InsetNewpage()
38 {}
39
40
41 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
42         : params_(params)
43 {}
44
45
46 void InsetNewpageParams::write(ostream & os) const
47 {
48         string command;
49         switch (kind) {
50         case InsetNewpageParams::NEWPAGE:
51                 os << "newpage";
52                 break;
53         case InsetNewpageParams::PAGEBREAK:
54                 os <<  "pagebreak";
55                 break;
56         case InsetNewpageParams::CLEARPAGE:
57                 os <<  "clearpage";
58                 break;
59         case InsetNewpageParams::CLEARDOUBLEPAGE:
60                 os <<  "cleardoublepage";
61                 break;
62         }
63 }
64
65
66 void InsetNewpageParams::read(Lexer & lex)
67 {
68         lex.next();
69         string const command = lex.getString();
70
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;
79         else
80                 lex.printError("InsetNewpage: Unknown kind: `$$Token'");
81
82         string token;
83         lex >> token;
84         if (!lex)
85                 return;
86         if (token != "\\end_inset")
87                 lex.printError("Missing \\end_inset at this point. "
88                                "Read: `$$Token'");
89 }
90
91
92 void InsetNewpage::write(ostream & os) const
93 {
94         os << "Newpage ";
95         params_.write(os);
96 }
97
98
99 void InsetNewpage::read(Lexer & lex)
100 {
101         params_.read(lex);
102 }
103
104
105 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
106 {
107         dim.asc = defaultRowHeight();
108         dim.des = defaultRowHeight();
109         dim.wid = mi.base.textwidth;
110         // Cache the inset dimension. 
111         setDimCache(mi, dim);
112 }
113
114
115 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
116 {
117         using frontend::Painter;
118
119         FontInfo font;
120         font.setColor(ColorName());
121         font.decSize();
122
123         Dimension const dim = dimension(*pi.base.bv);
124
125         int w = 0;
126         int a = 0;
127         int d = 0;
128         theFontMetrics(font).rectText(insetLabel(), w, a, d);
129
130         int const text_start = int(x + (dim.wid - w) / 2);
131         int const text_end = text_start + w;
132
133         pi.pain.rectText(text_start, y + d, insetLabel(), font,
134                 Color_none, Color_none);
135
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);
140 }
141
142
143 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
144 {
145         switch (cmd.action) {
146
147         case LFUN_INSET_MODIFY: {
148                 InsetNewpageParams params;
149                 string2params(to_utf8(cmd.argument()), params);
150                 params_.kind = params.kind;
151                 break;
152         }
153
154         default:
155                 Inset::doDispatch(cur, cmd);
156                 break;
157         }
158 }
159
160
161 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
162         FuncStatus & status) const
163 {
164         switch (cmd.action) {
165         // we handle these
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);
171                 } else {
172                         status.enabled(true);
173                 }
174                 return true;
175         default:
176                 return Inset::getStatus(cur, cmd, status);
177         }
178 }
179
180
181 docstring InsetNewpage::insetLabel() const
182 {
183         switch (params_.kind) {
184                 case InsetNewpageParams::NEWPAGE:
185                         return _("New Page");
186                         break;
187                 case InsetNewpageParams::PAGEBREAK:
188                         return _("Page Break");
189                         break;
190                 case InsetNewpageParams::CLEARPAGE:
191                         return _("Clear Page");
192                         break;
193                 case InsetNewpageParams::CLEARDOUBLEPAGE:
194                         return _("Clear Double Page");
195                         break;
196                 default:
197                         return _("New Page");
198                         break;
199         }
200 }
201
202
203 ColorCode InsetNewpage::ColorName() const
204 {
205         switch (params_.kind) {
206                 case InsetNewpageParams::NEWPAGE:
207                         return Color_newpage;
208                         break;
209                 case InsetNewpageParams::PAGEBREAK:
210                         return Color_pagebreak;
211                         break;
212                 case InsetNewpageParams::CLEARPAGE:
213                         return Color_newpage;
214                         break;
215                 case InsetNewpageParams::CLEARDOUBLEPAGE:
216                         return Color_newpage;
217                         break;
218                 default:
219                         return Color_newpage;
220                         break;
221         }
222 }
223
224
225 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
226 {
227         switch (params_.kind) {
228                 case InsetNewpageParams::NEWPAGE:
229                         os << "\\newpage{}";
230                         break;
231                 case InsetNewpageParams::PAGEBREAK:
232                         os << "\\pagebreak{}";
233                         break;
234                 case InsetNewpageParams::CLEARPAGE:
235                         os << "\\clearpage{}";
236                         break;
237                 case InsetNewpageParams::CLEARDOUBLEPAGE:
238                         os << "\\cleardoublepage{}";
239                         break;
240                 default:
241                         os << "\\newpage{}";
242                         break;
243         }
244         return 0;
245 }
246
247
248 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
249 {
250         os << '\n';
251         return PLAINTEXT_NEWLINE;
252 }
253
254
255 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
256 {
257         os << '\n';
258         return 0;
259 }
260
261
262 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
263 {
264         return from_ascii("context-newpage");
265 }
266
267
268 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
269 {
270         params = InsetNewpageParams();
271         if (in.empty())
272                 return;
273
274         istringstream data(in);
275         Lexer lex(0,0);
276         lex.setStream(data);
277
278         string name;
279         lex >> name;
280         if (!lex || name != "newpage") {
281                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
282                 return;
283         }
284
285         params.read(lex);
286 }
287
288
289 string InsetNewpage::params2string(InsetNewpageParams const & params)
290 {
291         ostringstream data;
292         data << "newpage" << ' ';
293         params.write(data);
294         return data.str();
295 }
296
297
298 } // namespace lyx