]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
3 more percents...
[lyx.git] / src / insets / insettext.C
1 /**
2  * \file insettext.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Jürgen Vigna
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "insettext.h"
14 #include "insetnewline.h"
15
16 #include "buffer.h"
17 #include "bufferparams.h"
18 #include "BufferView.h"
19 #include "CutAndPaste.h"
20 #include "cursor.h"
21 #include "debug.h"
22 #include "dispatchresult.h"
23 #include "errorlist.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "intl.h"
27 #include "LColor.h"
28 #include "lyxfind.h"
29 #include "lyxlex.h"
30 #include "lyxrc.h"
31 #include "lyxtext.h"
32 #include "metricsinfo.h"
33 #include "output_docbook.h"
34 #include "output_latex.h"
35 #include "output_linuxdoc.h"
36 #include "output_plaintext.h"
37 #include "paragraph.h"
38 #include "paragraph_funcs.h"
39 #include "ParagraphParameters.h"
40 #include "rowpainter.h"
41 #include "lyxrow.h"
42 #include "sgml.h"
43 #include "texrow.h"
44 #include "undo.h"
45
46 #include "frontends/Alert.h"
47 #include "frontends/font_metrics.h"
48 #include "frontends/Painter.h"
49
50 #include "support/lyxalgo.h" // lyx::count
51
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
54
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60
61 using boost::bind;
62 using boost::ref;
63
64 using std::endl;
65 using std::for_each;
66 using std::max;
67 using std::string;
68 using std::auto_ptr;
69 using std::ostream;
70 using std::vector;
71
72
73 int InsetText::border_ = 2;
74
75
76 InsetText::InsetText(BufferParams const & bp)
77         : drawFrame_(false), frame_color_(LColor::insetframe), text_(0)
78 {
79         paragraphs().push_back(Paragraph());
80         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
81         if (bp.tracking_changes)
82                 paragraphs().back().trackChanges();
83         init();
84 }
85
86
87 InsetText::InsetText(InsetText const & in)
88         : InsetOld(in), text_(in.text_.bv_owner)
89 {
90         text_.autoBreakRows_ = in.text_.autoBreakRows_;
91         drawFrame_ = in.drawFrame_;
92         frame_color_ = in.frame_color_;
93         text_.paragraphs() = in.text_.paragraphs();
94         init();
95 }
96
97
98 InsetText::InsetText()
99         : text_(0)
100 {}
101
102
103 void InsetText::init()
104 {
105         for_each(paragraphs().begin(), paragraphs().end(),
106                  bind(&Paragraph::setInsetOwner, _1, this));
107         old_pit = -1;
108 }
109
110
111 void InsetText::clear(bool just_mark_erased)
112 {
113         ParagraphList & pars = paragraphs();
114         if (just_mark_erased) {
115                 for_each(pars.begin(), pars.end(),
116                          bind(&Paragraph::markErased, _1));
117                 return;
118         }
119
120         // This is a gross hack...
121         LyXLayout_ptr old_layout = pars.begin()->layout();
122
123         pars.clear();
124         pars.push_back(Paragraph());
125         pars.begin()->setInsetOwner(this);
126         pars.begin()->layout(old_layout);
127 }
128
129
130 auto_ptr<InsetBase> InsetText::doClone() const
131 {
132         return auto_ptr<InsetBase>(new InsetText(*this));
133 }
134
135
136 void InsetText::write(Buffer const & buf, ostream & os) const
137 {
138         os << "Text\n";
139         text_.write(buf, os);
140 }
141
142
143 void InsetText::read(Buffer const & buf, LyXLex & lex)
144 {
145         clear(false);
146
147 #ifdef WITH_WARNINGS
148 #warning John, look here. Doesnt make much sense.
149 #endif
150         if (buf.params().tracking_changes)
151                 paragraphs().begin()->trackChanges();
152
153         // delete the initial paragraph
154         Paragraph oldpar = *paragraphs().begin();
155         paragraphs().clear();
156         bool res = text_.read(buf, lex);
157         init();
158
159         if (!res) {
160                 lex.printError("Missing \\end_inset at this point. "
161                                            "Read: `$$Token'");
162         }
163
164         // sanity check
165         // ensure we have at least one paragraph.
166         if (paragraphs().empty())
167                 paragraphs().push_back(oldpar);
168 }
169
170
171 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
172 {
173         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
174         setViewCache(mi.base.bv);
175         mi.base.textwidth -= 2 * border_;
176         font_ = mi.base.font;
177         text_.font_ = mi.base.font;
178         text_.metrics(mi, dim);
179         dim.asc += border_;
180         dim.des += border_;
181         dim.wid += 2 * border_;
182         mi.base.textwidth += 2 * border_;
183         dim_ = dim;
184 }
185
186
187 void InsetText::draw(PainterInfo & pi, int x, int y) const
188 {
189         BOOST_ASSERT(!text_.paragraphs().front().rows().empty());
190         // update our idea of where we are
191         setPosCache(pi, x, y);
192
193         text_.draw(pi, x + border_, y);
194
195         if (drawFrame_) {
196                 int const w = text_.width() + 2 * border_;
197                 int const a = text_.ascent() + border_;
198                 int const h = a + text_.descent() + border_;
199                 int const ww = pi.base.bv->workWidth();
200                 if (w > ww - 40)  {
201                         pi.pain.line(0, y - a, ww, y - a, frameColor());
202                         pi.pain.line(0, y - a + h, ww, y - a + h, frameColor());
203                 } else {
204                         pi.pain.rectangle(x, y - a, w, h, frameColor());
205                 }
206         }
207 }
208
209
210 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
211 {
212         if (backgroundColor() != LColor::background) {
213                 // repaint the background if needed
214                 int const w = text_.width() + 2 * border_;
215                 int const a = text_.ascent() + border_;
216                 int const h = a + text_.descent() + border_;
217                 pi.pain.fillRectangle(x, y - a, w, h, backgroundColor());
218         }
219         text_.drawSelection(pi, x, y);
220 }
221
222
223 string const InsetText::editMessage() const
224 {
225         return _("Opened Text Inset");
226 }
227
228
229 void InsetText::edit(LCursor & cur, bool left)
230 {
231         //lyxerr << "InsetText: edit left/right" << endl;
232         old_pit = -1;
233         setViewCache(&cur.bv());
234         int const pit = left ? 0 : paragraphs().size() - 1;
235         int const pos = left ? 0 : paragraphs().back().size();
236         text_.setCursor(cur.top(), pit, pos);
237         cur.clearSelection();
238         finishUndo();
239 }
240
241
242 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
243 {
244         old_pit = -1;
245         return text_.editXY(cur, x, y);
246 }
247
248
249 void InsetText::doDispatch(LCursor & cur, FuncRequest & cmd)
250 {
251         lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
252     << " [ cmd.action = " << cmd.action << ']' << endl;
253         setViewCache(&cur.bv());
254         text_.dispatch(cur, cmd);
255 }
256
257
258 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
259         FuncStatus & status) const
260 {
261         return text_.getStatus(cur, cmd, status);
262 }
263
264
265 int InsetText::latex(Buffer const & buf, ostream & os,
266                      OutputParams const & runparams) const
267 {
268         TexRow texrow;
269         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
270         return texrow.rows();
271 }
272
273
274 int InsetText::plaintext(Buffer const & buf, ostream & os,
275                      OutputParams const & runparams) const
276 {
277         ParagraphList::const_iterator beg = paragraphs().begin();
278         ParagraphList::const_iterator end = paragraphs().end();
279         ParagraphList::const_iterator it = beg;
280         bool ref_printed = false;
281         for (; it != end; ++it)
282                 asciiParagraph(buf, *it, os, runparams, ref_printed);
283
284         // FIXME: Give the total numbers of lines
285         return 1;
286 }
287
288
289 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
290                         OutputParams const & runparams) const
291 {
292         linuxdocParagraphs(buf, paragraphs(), os, runparams);
293         return 0;
294 }
295
296
297 int InsetText::docbook(Buffer const & buf, ostream & os,
298                        OutputParams const & runparams) const
299 {
300         docbookParagraphs(paragraphs(), buf, os, runparams);
301         return 0;
302 }
303
304
305 void InsetText::validate(LaTeXFeatures & features) const
306 {
307         for_each(paragraphs().begin(), paragraphs().end(),
308                  bind(&Paragraph::validate, _1, ref(features)));
309 }
310
311
312 void InsetText::cursorPos
313         (CursorSlice const & sl, bool boundary, int & x, int & y) const
314 {
315         x = text_.cursorX(sl, boundary) + border_;
316         y = text_.cursorY(sl, boundary);
317 }
318
319
320 bool InsetText::showInsetDialog(BufferView *) const
321 {
322         return false;
323 }
324
325
326 void InsetText::getLabelList(Buffer const & buffer,
327                              std::vector<string> & list) const
328 {
329         ParagraphList::const_iterator pit = paragraphs().begin();
330         ParagraphList::const_iterator pend = paragraphs().end();
331         for (; pit != pend; ++pit) {
332                 InsetList::const_iterator beg = pit->insetlist.begin();
333                 InsetList::const_iterator end = pit->insetlist.end();
334                 for (; beg != end; ++beg)
335                         beg->inset->getLabelList(buffer, list);
336         }
337 }
338
339
340 void InsetText::markNew(bool track_changes)
341 {
342         ParagraphList::iterator pit = paragraphs().begin();
343         ParagraphList::iterator end = paragraphs().end();
344         for (; pit != end; ++pit) {
345                 if (track_changes)
346                         pit->trackChanges();
347                 else // no-op when not tracking
348                         pit->cleanChanges();
349         }
350 }
351
352
353 void InsetText::setText(string const & data, LyXFont const & font)
354 {
355         clear(false);
356         Paragraph & first = paragraphs().front();
357         for (unsigned int i = 0; i < data.length(); ++i)
358                 first.insertChar(i, data[i], font);
359 }
360
361
362 void InsetText::setAutoBreakRows(bool flag)
363 {
364         if (flag == text_.autoBreakRows_)
365                 return;
366
367         text_.autoBreakRows_ = flag;
368         if (flag)
369                 return;
370
371         // remove previously existing newlines
372         ParagraphList::iterator it = paragraphs().begin();
373         ParagraphList::iterator end = paragraphs().end();
374         for (; it != end; ++it)
375                 for (int i = 0; i < it->size(); ++i)
376                         if (it->isNewline(i))
377                                 it->erase(i);
378 }
379
380
381 void InsetText::setDrawFrame(bool flag)
382 {
383         drawFrame_ = flag;
384 }
385
386
387 LColor_color InsetText::frameColor() const
388 {
389         return LColor::color(frame_color_);
390 }
391
392
393 void InsetText::setFrameColor(LColor_color col)
394 {
395         frame_color_ = col;
396 }
397
398
399 void InsetText::setViewCache(BufferView const * bv) const
400 {
401         if (bv && bv != text_.bv_owner) {
402                 //lyxerr << "setting view cache from "
403                 //      << text_.bv_owner << " to " << bv << "\n";
404                 text_.bv_owner = const_cast<BufferView *>(bv);
405         }
406 }
407
408
409 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
410 {
411 #ifdef WITH_WARNINGS
412 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
413 // And it probably does. You have to take a look at this John. (Lgb)
414 #warning John, have a look here. (Lgb)
415 #endif
416         ParagraphList & pl = paragraphs();
417
418         ParagraphList::iterator pit = plist.begin();
419         ParagraphList::iterator ins = pl.insert(pl.end(), *pit);
420         ++pit;
421         mergeParagraph(buffer->params(), pl, ins - pl.begin() - 1);
422
423         for_each(pit, plist.end(),
424                  bind(&ParagraphList::push_back, ref(pl), _1));
425 }
426
427
428 void InsetText::addPreview(PreviewLoader & loader) const
429 {
430         ParagraphList::const_iterator pit = paragraphs().begin();
431         ParagraphList::const_iterator pend = paragraphs().end();
432
433         for (; pit != pend; ++pit) {
434                 InsetList::const_iterator it  = pit->insetlist.begin();
435                 InsetList::const_iterator end = pit->insetlist.end();
436                 for (; it != end; ++it)
437                         it->inset->addPreview(loader);
438         }
439 }
440
441
442 ParagraphList const & InsetText::paragraphs() const
443 {
444         return text_.paragraphs();
445 }
446
447
448 ParagraphList & InsetText::paragraphs()
449 {
450         return text_.paragraphs();
451 }