]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
Remove all BufferParam arguments in InsetXXX methods (since insets know about their...
[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 "Text.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.setContext("InsetNewpageParams::read");
69         string token;
70         lex >> token;
71
72         if (token == "newpage")
73                 kind = InsetNewpageParams::NEWPAGE;
74         else if (token == "pagebreak")
75                 kind = InsetNewpageParams::PAGEBREAK;
76         else if (token == "clearpage")
77                 kind = InsetNewpageParams::CLEARPAGE;
78         else if (token == "cleardoublepage")
79                 kind = InsetNewpageParams::CLEARDOUBLEPAGE;
80         else
81                 lex.printError("Unknown kind");
82 }
83
84
85 void InsetNewpage::write(ostream & os) const
86 {
87         os << "Newpage ";
88         params_.write(os);
89 }
90
91
92 void InsetNewpage::read(Lexer & lex)
93 {
94         params_.read(lex);
95         lex >> "\\end_inset";
96 }
97
98
99 void InsetNewpage::metrics(MetricsInfo & mi, Dimension & dim) const
100 {
101         dim.asc = defaultRowHeight();
102         dim.des = defaultRowHeight();
103         dim.wid = mi.base.textwidth;
104         // Cache the inset dimension. 
105         setDimCache(mi, dim);
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                 string2params(to_utf8(cmd.argument()), params);
144                 params_.kind = params.kind;
145                 break;
146         }
147
148         default:
149                 Inset::doDispatch(cur, cmd);
150                 break;
151         }
152 }
153
154
155 bool InsetNewpage::getStatus(Cursor & cur, FuncRequest const & cmd,
156         FuncStatus & status) const
157 {
158         switch (cmd.action) {
159         // we handle these
160         case LFUN_INSET_MODIFY:
161                 if (cmd.getArg(0) == "newpage") {
162                         InsetNewpageParams params;
163                         string2params(to_utf8(cmd.argument()), params);
164                         status.setOnOff(params_.kind == params.kind);
165                 } 
166                 status.setEnabled(true);
167                 return true;
168         default:
169                 return Inset::getStatus(cur, cmd, status);
170         }
171 }
172
173
174 docstring InsetNewpage::insetLabel() const
175 {
176         switch (params_.kind) {
177                 case InsetNewpageParams::NEWPAGE:
178                         return _("New Page");
179                         break;
180                 case InsetNewpageParams::PAGEBREAK:
181                         return _("Page Break");
182                         break;
183                 case InsetNewpageParams::CLEARPAGE:
184                         return _("Clear Page");
185                         break;
186                 case InsetNewpageParams::CLEARDOUBLEPAGE:
187                         return _("Clear Double Page");
188                         break;
189                 default:
190                         return _("New Page");
191                         break;
192         }
193 }
194
195
196 ColorCode InsetNewpage::ColorName() const
197 {
198         switch (params_.kind) {
199                 case InsetNewpageParams::PAGEBREAK:
200                         return Color_pagebreak;
201                         break;
202                 case InsetNewpageParams::NEWPAGE:
203                 case InsetNewpageParams::CLEARPAGE:
204                 case InsetNewpageParams::CLEARDOUBLEPAGE:
205                         return Color_newpage;
206                         break;
207         }
208         // not really useful, but to avoids gcc complaints
209         return Color_newpage;
210 }
211
212
213 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
214 {
215         switch (params_.kind) {
216                 case InsetNewpageParams::NEWPAGE:
217                         os << "\\newpage{}";
218                         break;
219                 case InsetNewpageParams::PAGEBREAK:
220                         os << "\\pagebreak{}";
221                         break;
222                 case InsetNewpageParams::CLEARPAGE:
223                         os << "\\clearpage{}";
224                         break;
225                 case InsetNewpageParams::CLEARDOUBLEPAGE:
226                         os << "\\cleardoublepage{}";
227                         break;
228                 default:
229                         os << "\\newpage{}";
230                         break;
231         }
232         return 0;
233 }
234
235
236 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
237 {
238         os << '\n';
239         return PLAINTEXT_NEWLINE;
240 }
241
242
243 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
244 {
245         os << '\n';
246         return 0;
247 }
248
249
250 docstring InsetNewpage::xhtml(odocstream & os, OutputParams const &) const
251 {
252         os << "<br />\n";
253         return docstring();
254 }
255
256
257 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
258 {
259         return from_ascii("context-newpage");
260 }
261
262
263 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
264 {
265         params = InsetNewpageParams();
266         if (in.empty())
267                 return;
268
269         istringstream data(in);
270         Lexer lex;
271         lex.setStream(data);
272
273         string name;
274         lex >> name;
275         if (!lex || name != "newpage") {
276                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
277                 return;
278         }
279
280         params.read(lex);
281 }
282
283
284 string InsetNewpage::params2string(InsetNewpageParams const & params)
285 {
286         ostringstream data;
287         data << "newpage" << ' ';
288         params.write(data);
289         return data.str();
290 }
291
292
293 } // namespace lyx