]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
A couple of changing resulting from gcc 3.4 debug mode testing.
[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 bv_funcs::replaceSelection;
56
57 using lyx::pos_type;
58
59 using lyx::graphics::PreviewLoader;
60
61 using lyx::support::isStrUnsignedInt;
62 using lyx::support::strToUnsignedInt;
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 InsetText::InsetText(BufferParams const & bp)
74         : UpdatableInset(),
75           paragraphs(1),
76           autoBreakRows_(false),
77           drawFrame_(NEVER),
78           frame_color_(LColor::insetframe),
79           text_(0, this, true, paragraphs)
80 {
81         textwidth_ = 0; // broken
82         paragraphs.begin()->layout(bp.getLyXTextClass().defaultLayout());
83         if (bp.tracking_changes)
84                 paragraphs.begin()->trackChanges();
85         init();
86 }
87
88
89 InsetText::InsetText(InsetText const & in)
90         : UpdatableInset(in),
91           text_(in.text_.bv_owner, this, true, paragraphs)
92 {
93         // this is ugly...
94         operator=(in);
95 }
96
97
98 void InsetText::operator=(InsetText const & in)
99 {
100         UpdatableInset::operator=(in);
101         paragraphs = in.paragraphs;
102         autoBreakRows_ = in.autoBreakRows_;
103         drawFrame_ = in.drawFrame_;
104         frame_color_ = in.frame_color_;
105         textwidth_ = in.textwidth_;
106         text_ = LyXText(in.text_.bv_owner, this, true, paragraphs);
107         init();
108 }
109
110
111 void InsetText::init()
112 {
113         ParagraphList::iterator pit = paragraphs.begin();
114         ParagraphList::iterator end = paragraphs.end();
115         for (; pit != end; ++pit)
116                 pit->setInsetOwner(this);
117         text_.paragraphs_ = &paragraphs;
118         old_par = -1;
119         in_insetAllowed = false;
120 }
121
122
123 void InsetText::clear(bool just_mark_erased)
124 {
125         if (just_mark_erased) {
126                 ParagraphList::iterator it = paragraphs.begin();
127                 ParagraphList::iterator end = paragraphs.end();
128                 for (; it != end; ++it)
129                         it->markErased();
130                 return;
131         }
132
133         // This is a gross hack...
134         LyXLayout_ptr old_layout = paragraphs.begin()->layout();
135
136         paragraphs.clear();
137         paragraphs.push_back(Paragraph());
138         paragraphs.begin()->setInsetOwner(this);
139         paragraphs.begin()->layout(old_layout);
140 }
141
142
143 auto_ptr<InsetBase> InsetText::clone() const
144 {
145         return auto_ptr<InsetBase>(new InsetText(*this));
146 }
147
148
149 void InsetText::write(Buffer const & buf, ostream & os) const
150 {
151         os << "Text\n";
152         writeParagraphData(buf, os);
153 }
154
155
156 void InsetText::writeParagraphData(Buffer const & buf, ostream & os) const
157 {
158         ParagraphList::const_iterator it = paragraphs.begin();
159         ParagraphList::const_iterator end = paragraphs.end();
160         Paragraph::depth_type dth = 0;
161         for (; it != end; ++it) {
162                 it->write(buf, os, buf.params(), dth);
163         }
164 }
165
166
167 void InsetText::read(Buffer const & buf, LyXLex & lex)
168 {
169         string token;
170         Paragraph::depth_type depth = 0;
171
172         clear(false);
173
174 #warning John, look here. Doesnt make much sense.
175         if (buf.params().tracking_changes)
176                 paragraphs.begin()->trackChanges();
177
178         // delete the initial paragraph
179         Paragraph oldpar = *paragraphs.begin();
180         paragraphs.clear();
181         ParagraphList::iterator pit = paragraphs.begin();
182
183         while (lex.isOK()) {
184                 lex.nextToken();
185                 token = lex.getString();
186                 if (token.empty())
187                         continue;
188                 if (token == "\\end_inset") {
189                         break;
190                 }
191
192                 if (token == "\\end_document") {
193                         lex.printError("\\end_document read in inset! Error in document!");
194                         return;
195                 }
196
197                 // FIXME: ugly.
198                 const_cast<Buffer&>(buf).readParagraph(lex, token, paragraphs, pit, depth);
199         }
200
201         pit = paragraphs.begin();
202         ParagraphList::iterator const end = paragraphs.end();
203         for (; pit != end; ++pit)
204                 pit->setInsetOwner(this);
205
206         if (token != "\\end_inset") {
207                 lex.printError("Missing \\end_inset at this point. "
208                                            "Read: `$$Token'");
209         }
210
211         // sanity check
212         // ensure we have at least one par.
213         if (paragraphs.empty())
214                 paragraphs.push_back(oldpar);
215 }
216
217
218 void InsetText::metrics(MetricsInfo & mi, Dimension & dim) const
219 {
220         //lyxerr << "InsetText::metrics: width: " << mi.base.textwidth << endl;
221         textwidth_ = max(40, mi.base.textwidth - 30);
222         BufferView * bv = mi.base.bv;
223         setViewCache(bv);
224         text_.metrics(mi, dim);
225         dim.asc += TEXT_TO_INSET_OFFSET;
226         dim.des += TEXT_TO_INSET_OFFSET;
227         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
228         dim_ = dim;
229 }
230
231
232 int InsetText::textWidth() const
233 {
234         return textwidth_;
235 }
236
237
238 void InsetText::draw(PainterInfo & pi, int x, int y) const
239 {
240         // update our idea of where we are. Clearly, we should
241         // not have to know this information.
242         xo_ = x;
243         yo_ = y;
244
245         int const start_x = x;
246
247         BufferView * bv = pi.base.bv;
248         Painter & pain = pi.pain;
249
250         // repaint the background if needed
251         if (backgroundColor() != LColor::background)
252                 clearInset(bv, start_x + TEXT_TO_INSET_OFFSET, y);
253
254         bv->hideCursor();
255
256         if (!owner())
257                 x += scroll();
258
259         x += TEXT_TO_INSET_OFFSET;
260         y -= text_.firstRow()->ascent_of_text();
261
262         text_.xo_ = x;
263         text_.yo_ = y + bv->top_y();
264
265         paintTextInset(*bv, text_, x, y);
266
267         if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
268                 drawFrame(pain, start_x);
269 }
270
271
272 void InsetText::drawFrame(Painter & pain, int x) const
273 {
274         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
275         int const frame_x = x + ttoD2;
276         int const frame_y = yo_ - dim_.asc + ttoD2;
277         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
278         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
279         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
280 }
281
282
283 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
284 {
285         if (!bv)
286                 return;
287
288         if (!autoBreakRows_ && paragraphs.size() > 1)
289                 collapseParagraphs(bv);
290
291         if (!text_.selection.set())
292                 text_.selection.cursor = text_.cursor;
293
294         bv->owner()->view_state_changed();
295         bv->owner()->updateMenubar();
296         bv->owner()->updateToolbar();
297         if (old_par != text_.cursor.par()) {
298                 bv->owner()->setLayout(cpar()->layout()->name());
299                 old_par = text_.cursor.par();
300         }
301 }
302
303
304 string const InsetText::editMessage() const
305 {
306         return _("Opened Text Inset");
307 }
308
309
310 void InsetText::sanitizeEmptyText(BufferView * bv)
311 {
312         if (paragraphs.size() == 1
313                         && paragraphs.begin()->empty()
314                         && bv->getParentLanguage(this) != text_.current_font.language()) {
315                 LyXFont font(LyXFont::ALL_IGNORE);
316                 font.setLanguage(bv->getParentLanguage(this));
317                 setFont(bv, font, false);
318         }
319 }
320
321
322 extern LCursor theTempCursor;
323
324
325 void InsetText::edit(BufferView * bv, bool left)
326 {
327         lyxerr << "InsetText: edit left/right" << endl;
328         setViewCache(bv);
329
330         old_par = -1;
331
332         if (left)
333                 text_.setCursorIntern(0, 0);
334         else
335                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
336
337         sanitizeEmptyText(bv);
338         updateLocal(bv, false);
339         bv->updateParagraphDialog();
340 }
341
342
343 void InsetText::edit(BufferView * bv, int x, int y)
344 {
345         lyxerr << "InsetText::edit xy" << endl;
346         old_par = -1;
347         sanitizeEmptyText(bv);
348         text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
349                                        - text_.yo_);
350         text_.clearSelection();
351         finishUndo();
352
353         updateLocal(bv, false);
354         bv->updateParagraphDialog();
355 }
356
357
358 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
359         idx_type &, pos_type &)
360 {
361         lyxerr << "InsetText::priv_dispatch (begin), act: "
362                << cmd.action << " " << endl;
363
364         BufferView * bv = cmd.view();
365         setViewCache(bv);
366
367         DispatchResult result;
368         result.dispatched(true);
369
370         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
371
372         switch (cmd.action) {
373         case LFUN_MOUSE_PRESS:
374                 bv->cursor() = theTempCursor;
375                 // fall through
376         default:
377                 result = text_.dispatch(cmd);
378                 break;
379         }
380
381         // If the action has deleted all text in the inset, we need
382         // to change the language to the language of the surronding
383         // text.
384         if (!was_empty && paragraphs.begin()->empty() &&
385             paragraphs.size() == 1) {
386                 LyXFont font(LyXFont::ALL_IGNORE);
387                 font.setLanguage(bv->getParentLanguage(this));
388                 setFont(bv, font, false);
389         }
390
391         lyxerr << "InsetText::priv_dispatch (end)" << endl;
392         return result;
393 }
394
395
396 int InsetText::latex(Buffer const & buf, ostream & os,
397                      OutputParams const & runparams) const
398 {
399         TexRow texrow;
400         latexParagraphs(buf, paragraphs, os, texrow, runparams);
401         return texrow.rows();
402 }
403
404
405 int InsetText::plaintext(Buffer const & buf, ostream & os,
406                      OutputParams const & runparams) const
407 {
408         ParagraphList::const_iterator beg = paragraphs.begin();
409         ParagraphList::const_iterator end = paragraphs.end();
410         ParagraphList::const_iterator it = beg;
411         for (; it != end; ++it)
412                 asciiParagraph(buf, *it, os, runparams, it == beg);
413
414         //FIXME: Give the total numbers of lines
415         return 0;
416 }
417
418
419 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
420                         OutputParams const & runparams) const
421 {
422         linuxdocParagraphs(buf, paragraphs, os, runparams);
423         return 0;
424 }
425
426
427 int InsetText::docbook(Buffer const & buf, ostream & os,
428                        OutputParams const & runparams) const
429 {
430         docbookParagraphs(buf, paragraphs, os, runparams);
431         return 0;
432 }
433
434
435 void InsetText::validate(LaTeXFeatures & features) const
436 {
437         for_each(paragraphs.begin(), paragraphs.end(),
438                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
439 }
440
441
442 void InsetText::getCursorPos(int & x, int & y) const
443 {
444         x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
445         y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
446 }
447
448
449 int InsetText::insetInInsetY() const
450 {
451         return 0;
452 }
453
454
455 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
456 {
457         inset->setOwner(this);
458         text_.insertInset(inset);
459         updateLocal(bv, true);
460         return true;
461 }
462
463
464 bool InsetText::insetAllowed(InsetOld::Code code) const
465 {
466         // in_insetAllowed is a really gross hack,
467         // to allow us to call the owner's insetAllowed
468         // without stack overflow, which can happen
469         // when the owner uses InsetCollapsable::insetAllowed()
470         bool ret = true;
471         if (in_insetAllowed)
472                 return ret;
473         in_insetAllowed = true;
474         if (owner())
475                 ret = owner()->insetAllowed(code);
476         in_insetAllowed = false;
477         return ret;
478 }
479
480
481 bool InsetText::showInsetDialog(BufferView *) const
482 {
483         return false;
484 }
485
486
487 void InsetText::getLabelList(Buffer const & buffer,
488                              std::vector<string> & list) const
489 {
490         ParagraphList::const_iterator pit = paragraphs.begin();
491         ParagraphList::const_iterator pend = paragraphs.end();
492         for (; pit != pend; ++pit) {
493                 InsetList::const_iterator beg = pit->insetlist.begin();
494                 InsetList::const_iterator end = pit->insetlist.end();
495                 for (; beg != end; ++beg)
496                         beg->inset->getLabelList(buffer, list);
497         }
498 }
499
500
501 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
502                         bool selectall)
503 {
504         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
505             || cpar()->empty()) {
506                 text_.setFont(font, toggleall);
507                 return;
508         }
509
510         if (text_.selection.set())
511                 text_.recUndo(text_.cursor.par());
512
513         if (selectall) {
514                 text_.cursorTop();
515                 text_.selection.cursor = text_.cursor;
516                 text_.cursorBottom();
517                 text_.setSelection();
518         }
519
520         text_.toggleFree(font, toggleall);
521
522         if (selectall)
523                 text_.clearSelection();
524
525         updateLocal(bv, true);
526 }
527
528
529 void InsetText::markNew(bool track_changes)
530 {
531         ParagraphList::iterator pit = paragraphs.begin();
532         ParagraphList::iterator end = paragraphs.end();
533         for (; pit != end; ++pit) {
534                 if (track_changes) {
535                         pit->trackChanges();
536                 } else {
537                         // no-op when not tracking
538                         pit->cleanChanges();
539                 }
540         }
541 }
542
543
544 void InsetText::setText(string const & data, LyXFont const & font)
545 {
546         clear(false);
547         for (unsigned int i = 0; i < data.length(); ++i)
548                 paragraphs.begin()->insertChar(i, data[i], font);
549 }
550
551
552 void InsetText::setAutoBreakRows(bool flag)
553 {
554         if (flag != autoBreakRows_) {
555                 autoBreakRows_ = flag;
556                 if (!flag)
557                         removeNewlines();
558         }
559 }
560
561
562 void InsetText::setDrawFrame(DrawFrame how)
563 {
564         drawFrame_ = how;
565 }
566
567
568 LColor_color InsetText::frameColor() const
569 {
570         return LColor::color(frame_color_);
571 }
572
573
574 void InsetText::setFrameColor(LColor_color col)
575 {
576         frame_color_ = col;
577 }
578
579
580 pos_type InsetText::cpos() const
581 {
582         return text_.cursor.pos();
583 }
584
585
586 ParagraphList::iterator InsetText::cpar() const
587 {
588         return text_.cursorPar();
589 }
590
591
592 RowList::iterator InsetText::crow() const
593 {
594         return cpar()->getRow(cpos());
595 }
596
597
598 void InsetText::setViewCache(BufferView const * bv) const
599 {
600         if (bv) {
601                 if (bv != text_.bv_owner) {
602                         //lyxerr << "setting view cache from "
603                         //      << text_.bv_owner << " to " << bv << "\n";
604                         text_.init(const_cast<BufferView *>(bv));
605                 }
606                 text_.bv_owner = const_cast<BufferView *>(bv);
607         }
608 }
609
610
611 void InsetText::removeNewlines()
612 {
613         ParagraphList::iterator it = paragraphs.begin();
614         ParagraphList::iterator end = paragraphs.end();
615         for (; it != end; ++it)
616                 for (int i = 0; i < it->size(); ++i)
617                         if (it->isNewline(i))
618                                 it->erase(i);
619 }
620
621
622 int InsetText::scroll(bool /*recursive*/) const
623 {
624         return UpdatableInset::scroll(false);
625 }
626
627
628 void InsetText::clearSelection(BufferView *)
629 {
630         text_.clearSelection();
631 }
632
633
634 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
635 {
636         Painter & pain = bv->painter();
637         int w = dim_.wid;
638         int h = dim_.asc + dim_.des;
639         int ty = baseline - dim_.asc;
640
641         if (ty < 0) {
642                 h += ty;
643                 ty = 0;
644         }
645         if (ty + h > pain.paperHeight())
646                 h = pain.paperHeight();
647         if (xo_ + w > pain.paperWidth())
648                 w = pain.paperWidth();
649         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
650 }
651
652
653 ParagraphList * InsetText::getParagraphs(int i) const
654 {
655         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
656 }
657
658
659 LyXText * InsetText::getText(int i) const
660 {
661         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
662 }
663
664
665 bool InsetText::checkInsertChar(LyXFont & font)
666 {
667         return owner() ? owner()->checkInsertChar(font) : true;
668 }
669
670
671 void InsetText::collapseParagraphs(BufferView * bv)
672 {
673         while (paragraphs.size() > 1) {
674                 ParagraphList::iterator const first = paragraphs.begin();
675                 ParagraphList::iterator second = first;
676                 ++second;
677                 size_t const first_par_size = first->size();
678
679                 if (!first->empty() &&
680                     !second->empty() &&
681                     !first->isSeparator(first_par_size - 1)) {
682                         first->insertChar(first_par_size, ' ');
683                 }
684
685 #warning probably broken
686                 if (text_.selection.set()) {
687                         if (text_.selection.start.par() == 1) {
688                                 text_.selection.start.par(1);
689                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
690                         }
691                         if (text_.selection.end.par() == 2) {
692                                 text_.selection.end.par(1);
693                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
694                         }
695                 }
696
697                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
698         }
699 }
700
701
702 void InsetText::getDrawFont(LyXFont & font) const
703 {
704         if (owner())
705                 owner()->getDrawFont(font);
706 }
707
708
709 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
710 {
711 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
712 // And it probably does. You have to take a look at this John. (Lgb)
713 #warning John, have a look here. (Lgb)
714         ParagraphList::iterator pit = plist.begin();
715         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
716         ++pit;
717         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
718
719         ParagraphList::iterator pend = plist.end();
720         for (; pit != pend; ++pit)
721                 paragraphs.push_back(*pit);
722 }
723
724
725 void InsetText::addPreview(PreviewLoader & loader) const
726 {
727         ParagraphList::const_iterator pit = paragraphs.begin();
728         ParagraphList::const_iterator pend = paragraphs.end();
729
730         for (; pit != pend; ++pit) {
731                 InsetList::const_iterator it  = pit->insetlist.begin();
732                 InsetList::const_iterator end = pit->insetlist.end();
733                 for (; it != end; ++it)
734                         it->inset->addPreview(loader);
735         }
736 }