]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
Fix bug #6315: counters in insets that don't produce output have ghost values.
[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 "Text.h"
24 #include "TextMetrics.h"
25
26 #include "frontends/FontMetrics.h"
27 #include "frontends/Painter.h"
28
29 #include "support/debug.h"
30 #include "support/docstring.h"
31 #include "support/docstream.h"
32 #include "support/gettext.h"
33
34 using namespace std;
35
36
37 namespace lyx {
38
39         InsetNewpage::InsetNewpage() : Inset(0)
40 {}
41
42
43 InsetNewpage::InsetNewpage(InsetNewpageParams const & params)
44         : Inset(0), params_(params)
45 {}
46
47
48 void InsetNewpageParams::write(ostream & os) const
49 {
50         string command;
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         // Cache the inset dimension. 
107         setDimCache(mi, dim);
108 }
109
110
111 void InsetNewpage::draw(PainterInfo & pi, int x, int y) const
112 {
113         using frontend::Painter;
114
115         FontInfo font;
116         font.setColor(ColorName());
117         font.decSize();
118
119         Dimension const dim = dimension(*pi.base.bv);
120
121         int w = 0;
122         int a = 0;
123         int d = 0;
124         theFontMetrics(font).rectText(insetLabel(), w, a, d);
125
126         int const text_start = int(x + (dim.wid - w) / 2);
127         int const text_end = text_start + w;
128
129         pi.pain.rectText(text_start, y + d, insetLabel(), font,
130                 Color_none, Color_none);
131
132         pi.pain.line(x, y, text_start, y,
133                    ColorName(), Painter::line_onoffdash);
134         pi.pain.line(text_end, y, int(x + dim.wid), y,
135                    ColorName(), Painter::line_onoffdash);
136 }
137
138
139 void InsetNewpage::doDispatch(Cursor & cur, FuncRequest & cmd)
140 {
141         switch (cmd.action()) {
142
143         case LFUN_INSET_MODIFY: {
144                 InsetNewpageParams params;
145                 cur.recordUndo();
146                 string2params(to_utf8(cmd.argument()), params);
147                 params_.kind = params.kind;
148                 break;
149         }
150
151         default:
152                 Inset::doDispatch(cur, cmd);
153                 break;
154         }
155 }
156
157
158 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
159         FuncStatus & status) const
160 {
161         switch (cmd.action()) {
162         // we handle these
163         case LFUN_INSET_MODIFY:
164                 if (cmd.getArg(0) == "newpage") {
165                         InsetNewpageParams params;
166                         string2params(to_utf8(cmd.argument()), params);
167                         status.setOnOff(params_.kind == params.kind);
168                 } 
169                 status.setEnabled(true);
170                 return true;
171         default:
172                 return Inset::getStatus(cur, cmd, status);
173         }
174 }
175
176
177 docstring InsetNewpage::insetLabel() const
178 {
179         switch (params_.kind) {
180                 case InsetNewpageParams::NEWPAGE:
181                         return _("New Page");
182                         break;
183                 case InsetNewpageParams::PAGEBREAK:
184                         return _("Page Break");
185                         break;
186                 case InsetNewpageParams::CLEARPAGE:
187                         return _("Clear Page");
188                         break;
189                 case InsetNewpageParams::CLEARDOUBLEPAGE:
190                         return _("Clear Double Page");
191                         break;
192                 default:
193                         return _("New Page");
194                         break;
195         }
196 }
197
198
199 ColorCode InsetNewpage::ColorName() const
200 {
201         switch (params_.kind) {
202                 case InsetNewpageParams::PAGEBREAK:
203                         return Color_pagebreak;
204                         break;
205                 case InsetNewpageParams::NEWPAGE:
206                 case InsetNewpageParams::CLEARPAGE:
207                 case InsetNewpageParams::CLEARDOUBLEPAGE:
208                         return Color_newpage;
209                         break;
210         }
211         // not really useful, but to avoids gcc complaints
212         return Color_newpage;
213 }
214
215
216 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
217 {
218         switch (params_.kind) {
219                 case InsetNewpageParams::NEWPAGE:
220                         os << "\\newpage{}";
221                         break;
222                 case InsetNewpageParams::PAGEBREAK:
223                         os << "\\pagebreak{}";
224                         break;
225                 case InsetNewpageParams::CLEARPAGE:
226                         os << "\\clearpage{}";
227                         break;
228                 case InsetNewpageParams::CLEARDOUBLEPAGE:
229                         os << "\\cleardoublepage{}";
230                         break;
231                 default:
232                         os << "\\newpage{}";
233                         break;
234         }
235         return 0;
236 }
237
238
239 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
240 {
241         os << '\n';
242         return PLAINTEXT_NEWLINE;
243 }
244
245
246 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
247 {
248         os << '\n';
249         return 0;
250 }
251
252
253 docstring InsetNewpage::xhtml(XHTMLStream & xs, OutputParams const &) const
254 {
255         xs << html::CompTag("br");
256         return docstring();
257 }
258
259
260 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
261 {
262         return from_ascii("context-newpage");
263 }
264
265
266 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
267 {
268         params = InsetNewpageParams();
269         if (in.empty())
270                 return;
271
272         istringstream data(in);
273         Lexer lex;
274         lex.setStream(data);
275
276         string name;
277         lex >> name;
278         if (!lex || name != "newpage") {
279                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
280                 return;
281         }
282
283         params.read(lex);
284 }
285
286
287 string InsetNewpage::params2string(InsetNewpageParams const & params)
288 {
289         ostringstream data;
290         data << "newpage" << ' ';
291         params.write(data);
292         return data.str();
293 }
294
295
296 } // namespace lyx