]> git.lyx.org Git - lyx.git/blob - src/insets/InsetNewpage.cpp
- Simplify prefs, graphics and external display options which are now true or false.
[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 "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.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         lex >> "\\end_inset";
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 }
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::NEWPAGE:
201                         return Color_newpage;
202                         break;
203                 case InsetNewpageParams::PAGEBREAK:
204                         return Color_pagebreak;
205                         break;
206                 case InsetNewpageParams::CLEARPAGE:
207                         return Color_newpage;
208                         break;
209                 case InsetNewpageParams::CLEARDOUBLEPAGE:
210                         return Color_newpage;
211                         break;
212                 default:
213                         return Color_newpage;
214                         break;
215         }
216 }
217
218
219 int InsetNewpage::latex(odocstream & os, OutputParams const &) const
220 {
221         switch (params_.kind) {
222                 case InsetNewpageParams::NEWPAGE:
223                         os << "\\newpage{}";
224                         break;
225                 case InsetNewpageParams::PAGEBREAK:
226                         os << "\\pagebreak{}";
227                         break;
228                 case InsetNewpageParams::CLEARPAGE:
229                         os << "\\clearpage{}";
230                         break;
231                 case InsetNewpageParams::CLEARDOUBLEPAGE:
232                         os << "\\cleardoublepage{}";
233                         break;
234                 default:
235                         os << "\\newpage{}";
236                         break;
237         }
238         return 0;
239 }
240
241
242 int InsetNewpage::plaintext(odocstream & os, OutputParams const &) const
243 {
244         os << '\n';
245         return PLAINTEXT_NEWLINE;
246 }
247
248
249 int InsetNewpage::docbook(odocstream & os, OutputParams const &) const
250 {
251         os << '\n';
252         return 0;
253 }
254
255
256 docstring InsetNewpage::contextMenu(BufferView const &, int, int) const
257 {
258         return from_ascii("context-newpage");
259 }
260
261
262 void InsetNewpage::string2params(string const & in, InsetNewpageParams & params)
263 {
264         params = InsetNewpageParams();
265         if (in.empty())
266                 return;
267
268         istringstream data(in);
269         Lexer lex;
270         lex.setStream(data);
271
272         string name;
273         lex >> name;
274         if (!lex || name != "newpage") {
275                 LYXERR0("Expected arg 2 to be \"wrap\" in " << in);
276                 return;
277         }
278
279         params.read(lex);
280 }
281
282
283 string InsetNewpage::params2string(InsetNewpageParams const & params)
284 {
285         ostringstream data;
286         data << "newpage" << ' ';
287         params.write(data);
288         return data.str();
289 }
290
291
292 } // namespace lyx