]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
the stuff from the sneak preview:
[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/LyXView.h"
49 #include "frontends/Painter.h"
50
51 #include "support/lyxalgo.h" // lyx::count
52
53 #include <boost/bind.hpp>
54
55 using lyx::pos_type;
56
57 using lyx::graphics::PreviewLoader;
58
59 using lyx::support::isStrUnsignedInt;
60 using lyx::support::strToUnsignedInt;
61
62 using std::endl;
63 using std::for_each;
64 using std::max;
65 using std::string;
66 using std::auto_ptr;
67 using std::ostream;
68 using std::vector;
69
70
71 InsetText::InsetText(BufferParams const & bp)
72         : autoBreakRows_(false), drawFrame_(NEVER),
73           frame_color_(LColor::insetframe), text_(0)
74 {
75         paragraphs().push_back(Paragraph());
76         paragraphs().back().layout(bp.getLyXTextClass().defaultLayout());
77         if (bp.tracking_changes)
78                 paragraphs().back().trackChanges();
79         init();
80 }
81
82
83 InsetText::InsetText(InsetText const & in)
84         : UpdatableInset(in), text_(in.text_.bv_owner)
85 {
86         // this is ugly...
87         operator=(in);
88 }
89
90
91 void InsetText::operator=(InsetText const & in)
92 {
93         UpdatableInset::operator=(in);
94         autoBreakRows_ = in.autoBreakRows_;
95         drawFrame_ = in.drawFrame_;
96         frame_color_ = in.frame_color_;
97         text_ = LyXText(in.text_.bv_owner);
98         text_.paragraphs() = in.text_.paragraphs();
99         init();
100 }
101
102
103 void InsetText::init()
104 {
105         ParagraphList::iterator pit = paragraphs().begin();
106         ParagraphList::iterator end = paragraphs().end();
107         for (; pit != end; ++pit)
108                 pit->setInsetOwner(this);
109         old_par = -1;
110 }
111
112
113 void InsetText::clear(bool just_mark_erased)
114 {
115         ParagraphList & pars = paragraphs();
116         if (just_mark_erased) {
117                 ParagraphList::iterator it = pars.begin();
118                 ParagraphList::iterator end = pars.end();
119                 for (; it != end; ++it)
120                         it->markErased();
121                 return;
122         }
123
124         // This is a gross hack...
125         LyXLayout_ptr old_layout = pars.begin()->layout();
126
127         pars.clear();
128         pars.push_back(Paragraph());
129         pars.begin()->setInsetOwner(this);
130         pars.begin()->layout(old_layout);
131 }
132
133
134 auto_ptr<InsetBase> InsetText::clone() const
135 {
136         return auto_ptr<InsetBase>(new InsetText(*this));
137 }
138
139
140 void InsetText::write(Buffer const & buf, ostream & os) const
141 {
142         os << "Text\n";
143         text_.write(buf, os);
144 }
145
146
147 void InsetText::read(Buffer const & buf, LyXLex & lex)
148 {
149         clear(false);
150
151 #warning John, look here. Doesnt make much sense.
152         if (buf.params().tracking_changes)
153                 paragraphs().begin()->trackChanges();
154
155         // delete the initial paragraph
156         Paragraph oldpar = *paragraphs().begin();
157         paragraphs().clear();
158         bool res = text_.read(buf, lex);
159         init();
160
161         if (!res) {
162                 lex.printError("Missing \\end_inset at this point. "
163                                            "Read: `$$Token'");
164         }
165
166         // sanity check
167         // ensure we have at least one par.
168         if (paragraphs().empty())
169                 paragraphs().push_back(oldpar);
170 }
171
172
173 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
174 {
175         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
176         setViewCache(mi.base.bv);
177         mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
178         text_.metrics(mi, dim);
179         dim.asc += TEXT_TO_INSET_OFFSET;
180         dim.des += TEXT_TO_INSET_OFFSET;
181         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
182         mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
183         dim_ = dim;
184         font_ = mi.base.font;
185         text_.font_ = mi.base.font;
186 }
187
188
189 void InsetText::draw(PainterInfo & pi, int x, int y) const
190 {
191         BOOST_ASSERT(!text_.paragraphs().begin()->rows.empty());
192         // update our idea of where we are
193         setPosCache(pi, x, y);
194
195         // repaint the background if needed
196         x += TEXT_TO_INSET_OFFSET;
197         if (backgroundColor() != LColor::background)
198                 clearInset(pi.pain, x, y);
199
200         BufferView * bv = pi.base.bv;
201         bv->hideCursor();
202
203         x += scroll();
204         y += bv->top_y() - text_.ascent();
205
206         text_.draw(pi, x, y);
207
208         if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
209                 drawFrame(pi.pain, xo_, yo_ - bv->top_y());
210 }
211
212
213 void InsetText::drawSelection(PainterInfo & pi, int x, int y) const
214 {
215         text_.drawSelection(pi, x, y);
216 }
217
218
219 void InsetText::drawFrame(Painter & pain, int x, int y) const
220 {
221         int const frame_x = x + TEXT_TO_INSET_OFFSET / 2;
222         int const frame_y = y - dim_.asc + TEXT_TO_INSET_OFFSET / 2;
223         int const frame_w = text_.width();
224         int const frame_h = text_.height();
225         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
226 }
227
228
229 void InsetText::updateLocal(LCursor & cur)
230 {
231         if (!autoBreakRows_ && paragraphs().size() > 1) {
232                 // collapseParagraphs
233                 while (paragraphs().size() > 1) {
234                         ParagraphList::iterator const first = paragraphs().begin();
235                         ParagraphList::iterator second = first;
236                         ++second;
237                         size_t const first_par_size = first->size();
238
239                         if (!first->empty() &&
240                                         !second->empty() &&
241                                         !first->isSeparator(first_par_size - 1)) {
242                                 first->insertChar(first_par_size, ' ');
243                         }
244
245                         cur.clearSelection();
246                         mergeParagraph(cur.bv().buffer()->params(), paragraphs(), 0);
247                 }
248         }
249
250         if (!cur.selection())
251                 cur.resetAnchor();
252
253         LyXView * lv = cur.bv().owner();
254         lv->view_state_changed();
255         lv->updateMenubar();
256         lv->updateToolbar();
257         if (old_par != cur.par()) {
258                 lv->setLayout(text_.getPar(cur.par()).layout()->name());
259                 old_par = cur.par();
260         }
261 }
262
263
264 string const InsetText::editMessage() const
265 {
266         return _("Opened Text Inset");
267 }
268
269
270 void InsetText::sanitizeEmptyText(BufferView & bv)
271 {
272         if (paragraphs().size() == 1
273             && paragraphs().begin()->empty()
274             && bv.getParentLanguage(this) != text_.current_font.language()) {
275                 LyXFont font(LyXFont::ALL_IGNORE);
276                 font.setLanguage(bv.getParentLanguage(this));
277                 text_.setFont(bv.cursor(), font, false);
278         }
279 }
280
281
282 void InsetText::edit(LCursor & cur, bool left)
283 {
284         //lyxerr << "InsetText: edit left/right" << endl;
285         old_par = -1;
286         setViewCache(&cur.bv());
287         int const par = left ? 0 : paragraphs().size() - 1;
288         int const pos = left ? 0 : paragraphs().back().size();
289         text_.setCursor(cur.top(), par, pos);
290         cur.clearSelection();
291         finishUndo();
292         sanitizeEmptyText(cur.bv());
293         updateLocal(cur);
294 }
295
296
297 InsetBase * InsetText::editXY(LCursor & cur, int x, int y)
298 {
299         lyxerr << "InsetText::edit xy" << endl;
300         old_par = -1;
301         return text_.editXY(cur, x, y);
302         //sanitizeEmptyText(cur.bv());
303         //updateLocal(cur);
304 }
305
306
307 void InsetText::priv_dispatch(LCursor & cur, FuncRequest & cmd)
308 {
309         //lyxerr << "InsetText::priv_dispatch: " << cmd.action << " " << endl;
310         setViewCache(&cur.bv());
311
312         bool was_empty = paragraphs().begin()->empty() && paragraphs().size() == 1;
313         text_.dispatch(cur, cmd);
314
315         // If the action has deleted all text in the inset, we need
316         // to change the language to the language of the surronding
317         // text.
318         // Why this cleverness? (Andre')
319         if (!was_empty && paragraphs().begin()->empty() &&
320             paragraphs().size() == 1) {
321                 LyXFont font(LyXFont::ALL_IGNORE);
322                 font.setLanguage(cur.bv().getParentLanguage(this));
323                 text_.setFont(cur, font, false);
324         }
325 }
326
327
328 bool InsetText::getStatus(LCursor & cur, FuncRequest const & cmd,
329         FuncStatus & status) const
330 {
331         return text_.getStatus(cur, cmd, status);
332 }
333
334
335 int InsetText::latex(Buffer const & buf, ostream & os,
336                      OutputParams const & runparams) const
337 {
338         TexRow texrow;
339         latexParagraphs(buf, paragraphs(), os, texrow, runparams);
340         return texrow.rows();
341 }
342
343
344 int InsetText::plaintext(Buffer const & buf, ostream & os,
345                      OutputParams const & runparams) const
346 {
347         ParagraphList::const_iterator beg = paragraphs().begin();
348         ParagraphList::const_iterator end = paragraphs().end();
349         ParagraphList::const_iterator it = beg;
350         for (; it != end; ++it)
351                 asciiParagraph(buf, *it, os, runparams, it == beg);
352
353         //FIXME: Give the total numbers of lines
354         return 0;
355 }
356
357
358 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
359                         OutputParams const & runparams) const
360 {
361         linuxdocParagraphs(buf, paragraphs(), os, runparams);
362         return 0;
363 }
364
365
366 int InsetText::docbook(Buffer const & buf, ostream & os,
367                        OutputParams const & runparams) const
368 {
369         docbookParagraphs(buf, paragraphs(), os, runparams);
370         return 0;
371 }
372
373
374 void InsetText::validate(LaTeXFeatures & features) const
375 {
376         for_each(paragraphs().begin(), paragraphs().end(),
377                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
378 }
379
380
381 void InsetText::getCursorPos(CursorSlice const & cur, int & x, int & y) const
382 {
383         x = text_.cursorX(cur);
384         y = text_.cursorY(cur);
385 }
386
387
388 bool InsetText::showInsetDialog(BufferView *) const
389 {
390         return false;
391 }
392
393
394 void InsetText::getLabelList(Buffer const & buffer,
395                              std::vector<string> & list) const
396 {
397         ParagraphList::const_iterator pit = paragraphs().begin();
398         ParagraphList::const_iterator pend = paragraphs().end();
399         for (; pit != pend; ++pit) {
400                 InsetList::const_iterator beg = pit->insetlist.begin();
401                 InsetList::const_iterator end = pit->insetlist.end();
402                 for (; beg != end; ++beg)
403                         beg->inset->getLabelList(buffer, list);
404         }
405 }
406
407
408 void InsetText::markNew(bool track_changes)
409 {
410         ParagraphList::iterator pit = paragraphs().begin();
411         ParagraphList::iterator end = paragraphs().end();
412         for (; pit != end; ++pit) {
413                 if (track_changes) {
414                         pit->trackChanges();
415                 } else {
416                         // no-op when not tracking
417                         pit->cleanChanges();
418                 }
419         }
420 }
421
422
423 void InsetText::setText(string const & data, LyXFont const & font)
424 {
425         clear(false);
426         for (unsigned int i = 0; i < data.length(); ++i)
427                 paragraphs().begin()->insertChar(i, data[i], font);
428 }
429
430
431 void InsetText::setAutoBreakRows(bool flag)
432 {
433         if (flag != autoBreakRows_) {
434                 autoBreakRows_ = flag;
435                 if (!flag)
436                         removeNewlines();
437         }
438 }
439
440
441 void InsetText::setDrawFrame(DrawFrame how)
442 {
443         drawFrame_ = how;
444 }
445
446
447 LColor_color InsetText::frameColor() const
448 {
449         return LColor::color(frame_color_);
450 }
451
452
453 void InsetText::setFrameColor(LColor_color col)
454 {
455         frame_color_ = col;
456 }
457
458
459 void InsetText::setViewCache(BufferView const * bv) const
460 {
461         if (bv && bv != text_.bv_owner) {
462                 //lyxerr << "setting view cache from "
463                 //      << text_.bv_owner << " to " << bv << "\n";
464                 text_.bv_owner = const_cast<BufferView *>(bv);
465         }
466 }
467
468
469 void InsetText::removeNewlines()
470 {
471         ParagraphList::iterator it = paragraphs().begin();
472         ParagraphList::iterator end = paragraphs().end();
473         for (; it != end; ++it)
474                 for (int i = 0; i < it->size(); ++i)
475                         if (it->isNewline(i))
476                                 it->erase(i);
477 }
478
479
480 void InsetText::clearInset(Painter & pain, int x, int y) const
481 {
482         int w = dim_.wid;
483         int h = dim_.asc + dim_.des;
484         int ty = y - dim_.asc;
485
486         if (ty < 0) {
487                 h += ty;
488                 ty = 0;
489         }
490         if (ty + h > pain.paperHeight())
491                 h = pain.paperHeight();
492         if (xo_ + w > pain.paperWidth())
493                 w = pain.paperWidth();
494         pain.fillRectangle(x + 1, ty + 1, w - 3, h - 1, backgroundColor());
495 }
496
497
498 LyXText * InsetText::getText(int i) const
499 {
500         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
501 }
502
503
504 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
505 {
506 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
507 // And it probably does. You have to take a look at this John. (Lgb)
508 #warning John, have a look here. (Lgb)
509         ParagraphList::iterator pit = plist.begin();
510         ParagraphList::iterator ins = paragraphs().insert(paragraphs().end(), *pit);
511         ++pit;
512         mergeParagraph(buffer->params(), paragraphs(),
513                 ins - paragraphs().begin() - 1);
514
515         ParagraphList::iterator pend = plist.end();
516         for (; pit != pend; ++pit)
517                 paragraphs().push_back(*pit);
518 }
519
520
521 void InsetText::addPreview(PreviewLoader & loader) const
522 {
523         ParagraphList::const_iterator pit = paragraphs().begin();
524         ParagraphList::const_iterator pend = paragraphs().end();
525
526         for (; pit != pend; ++pit) {
527                 InsetList::const_iterator it  = pit->insetlist.begin();
528                 InsetList::const_iterator end = pit->insetlist.end();
529                 for (; it != end; ++it)
530                         it->inset->addPreview(loader);
531         }
532 }
533
534
535 ParagraphList & InsetText::paragraphs() const
536 {
537         return const_cast<ParagraphList &>(text_.paragraphs());
538 }