]> git.lyx.org Git - lyx.git/blob - src/insets/insettext.C
Michael Schmitt's patch to remove unused arguments
[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
261         text_.xo_ = x;
262         text_.yo_ = y - text_.firstRow()->ascent_of_text() + bv->top_y();
263         
264         paintTextInset(*bv, text_, x, y);
265
266         if (drawFrame_ == ALWAYS || drawFrame_ == LOCKED)
267                 drawFrame(pain, start_x);
268 }
269
270
271 void InsetText::drawFrame(Painter & pain, int x) const
272 {
273         int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
274         int const frame_x = x + ttoD2;
275         int const frame_y = yo_ - dim_.asc + ttoD2;
276         int const frame_w = dim_.wid - TEXT_TO_INSET_OFFSET;
277         int const frame_h = dim_.asc + dim_.des - TEXT_TO_INSET_OFFSET;
278         pain.rectangle(frame_x, frame_y, frame_w, frame_h, frameColor());
279 }
280
281
282 void InsetText::updateLocal(BufferView * bv, bool /*mark_dirty*/)
283 {
284         if (!bv)
285                 return;
286
287         if (!autoBreakRows_ && paragraphs.size() > 1)
288                 collapseParagraphs(bv);
289
290         if (!text_.selection.set())
291                 text_.selection.cursor = text_.cursor;
292
293         bv->owner()->view_state_changed();
294         bv->owner()->updateMenubar();
295         bv->owner()->updateToolbar();
296         if (old_par != text_.cursor.par()) {
297                 bv->owner()->setLayout(cpar()->layout()->name());
298                 old_par = text_.cursor.par();
299         }
300 }
301
302
303 string const InsetText::editMessage() const
304 {
305         return _("Opened Text Inset");
306 }
307
308
309 void InsetText::sanitizeEmptyText(BufferView * bv)
310 {
311         if (paragraphs.size() == 1
312                         && paragraphs.begin()->empty() 
313                         && bv->getParentLanguage(this) != text_.current_font.language()) {
314                 LyXFont font(LyXFont::ALL_IGNORE);
315                 font.setLanguage(bv->getParentLanguage(this));
316                 setFont(bv, font, false);
317         }
318 }
319
320
321 extern LCursor theTempCursor;
322
323
324 void InsetText::edit(BufferView * bv, bool left)
325 {
326         lyxerr << "InsetText: edit left/right" << endl;
327         setViewCache(bv);
328
329         old_par = -1;
330
331         if (left)
332                 text_.setCursorIntern(0, 0);
333         else
334                 text_.setCursor(paragraphs.size() - 1, paragraphs.back().size());
335
336         sanitizeEmptyText(bv);
337         updateLocal(bv, false);
338         bv->updateParagraphDialog();
339 }
340
341
342 void InsetText::edit(BufferView * bv, int x, int y)
343 {
344         lyxerr << "InsetText::edit xy" << endl;
345         old_par = -1;
346         sanitizeEmptyText(bv);
347         text_.setCursorFromCoordinates(x - text_.xo_, y + bv->top_y()
348                                        - text_.yo_);
349         text_.clearSelection();
350         finishUndo();
351
352         updateLocal(bv, false);
353         bv->updateParagraphDialog();
354 }
355
356
357 DispatchResult InsetText::priv_dispatch(FuncRequest const & cmd,
358         idx_type &, pos_type &)
359 {
360         lyxerr << "InsetText::priv_dispatch (begin), act: "
361                << cmd.action << " " << endl;
362         
363         BufferView * bv = cmd.view();
364         setViewCache(bv);
365
366         DispatchResult result;
367         result.dispatched(true);
368
369         bool was_empty = paragraphs.begin()->empty() && paragraphs.size() == 1;
370
371         switch (cmd.action) {
372         case LFUN_MOUSE_PRESS:
373                 bv->cursor() = theTempCursor;
374                 // fall through
375         default:
376                 result = text_.dispatch(cmd);
377                 break;
378         }
379
380         // If the action has deleted all text in the inset, we need
381         // to change the language to the language of the surronding
382         // text.
383         if (!was_empty && paragraphs.begin()->empty() &&
384             paragraphs.size() == 1) {
385                 LyXFont font(LyXFont::ALL_IGNORE);
386                 font.setLanguage(bv->getParentLanguage(this));
387                 setFont(bv, font, false);
388         }
389
390         lyxerr << "InsetText::priv_dispatch (end)" << endl;
391         return result;
392 }
393
394
395 int InsetText::latex(Buffer const & buf, ostream & os,
396                      OutputParams const & runparams) const
397 {
398         TexRow texrow;
399         latexParagraphs(buf, paragraphs, os, texrow, runparams);
400         return texrow.rows();
401 }
402
403
404 int InsetText::plaintext(Buffer const & buf, ostream & os,
405                      OutputParams const & runparams) const
406 {
407         ParagraphList::const_iterator beg = paragraphs.begin();
408         ParagraphList::const_iterator end = paragraphs.end();
409         ParagraphList::const_iterator it = beg;
410         for (; it != end; ++it)
411                 asciiParagraph(buf, *it, os, runparams, it == beg);
412
413         //FIXME: Give the total numbers of lines
414         return 0;
415 }
416
417
418 int InsetText::linuxdoc(Buffer const & buf, ostream & os,
419                         OutputParams const & runparams) const
420 {
421         linuxdocParagraphs(buf, paragraphs, os, runparams);
422         return 0;
423 }
424
425
426 int InsetText::docbook(Buffer const & buf, ostream & os,
427                        OutputParams const & runparams) const
428 {
429         docbookParagraphs(buf, paragraphs, os, runparams);
430         return 0;
431 }
432
433
434 void InsetText::validate(LaTeXFeatures & features) const
435 {
436         for_each(paragraphs.begin(), paragraphs.end(),
437                  boost::bind(&Paragraph::validate, _1, boost::ref(features)));
438 }
439
440
441 void InsetText::getCursorPos(int & x, int & y) const
442 {
443         x = text_.cursor.x() + TEXT_TO_INSET_OFFSET;
444         y = text_.cursor.y() - dim_.asc + TEXT_TO_INSET_OFFSET;
445 }
446
447
448 int InsetText::insetInInsetY() const
449 {
450         return 0;
451 }
452
453
454 bool InsetText::insertInset(BufferView * bv, InsetOld * inset)
455 {
456         inset->setOwner(this);
457         text_.insertInset(inset);
458         updateLocal(bv, true);
459         return true;
460 }
461
462
463 bool InsetText::insetAllowed(InsetOld::Code code) const
464 {
465         // in_insetAllowed is a really gross hack,
466         // to allow us to call the owner's insetAllowed
467         // without stack overflow, which can happen
468         // when the owner uses InsetCollapsable::insetAllowed()
469         bool ret = true;
470         if (in_insetAllowed)
471                 return ret;
472         in_insetAllowed = true;
473         if (owner())
474                 ret = owner()->insetAllowed(code);
475         in_insetAllowed = false;
476         return ret;
477 }
478
479
480 bool InsetText::showInsetDialog(BufferView *) const
481 {
482         return false;
483 }
484
485
486 void InsetText::getLabelList(Buffer const & buffer,
487                              std::vector<string> & list) const
488 {
489         ParagraphList::const_iterator pit = paragraphs.begin();
490         ParagraphList::const_iterator pend = paragraphs.end();
491         for (; pit != pend; ++pit) {
492                 InsetList::const_iterator beg = pit->insetlist.begin();
493                 InsetList::const_iterator end = pit->insetlist.end();
494                 for (; beg != end; ++beg)
495                         beg->inset->getLabelList(buffer, list);
496         }
497 }
498
499
500 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
501                         bool selectall)
502 {
503         if ((paragraphs.size() == 1 && paragraphs.begin()->empty())
504             || cpar()->empty()) {
505                 text_.setFont(font, toggleall);
506                 return;
507         }
508
509         if (text_.selection.set())
510                 text_.recUndo(text_.cursor.par());
511
512         if (selectall) {
513                 text_.cursorTop();
514                 text_.selection.cursor = text_.cursor;
515                 text_.cursorBottom();
516                 text_.setSelection();
517         }
518
519         text_.toggleFree(font, toggleall);
520
521         if (selectall)
522                 text_.clearSelection();
523
524         updateLocal(bv, true);
525 }
526
527
528 void InsetText::markNew(bool track_changes)
529 {
530         ParagraphList::iterator pit = paragraphs.begin();
531         ParagraphList::iterator end = paragraphs.end();
532         for (; pit != end; ++pit) {
533                 if (track_changes) {
534                         pit->trackChanges();
535                 } else {
536                         // no-op when not tracking
537                         pit->cleanChanges();
538                 }
539         }
540 }
541
542
543 void InsetText::setText(string const & data, LyXFont const & font)
544 {
545         clear(false);
546         for (unsigned int i = 0; i < data.length(); ++i)
547                 paragraphs.begin()->insertChar(i, data[i], font);
548 }
549
550
551 void InsetText::setAutoBreakRows(bool flag)
552 {
553         if (flag != autoBreakRows_) {
554                 autoBreakRows_ = flag;
555                 if (!flag)
556                         removeNewlines();
557         }
558 }
559
560
561 void InsetText::setDrawFrame(DrawFrame how)
562 {
563         drawFrame_ = how;
564 }
565
566
567 LColor_color InsetText::frameColor() const
568 {
569         return LColor::color(frame_color_);
570 }
571
572
573 void InsetText::setFrameColor(LColor_color col)
574 {
575         frame_color_ = col;
576 }
577
578
579 pos_type InsetText::cpos() const
580 {
581         return text_.cursor.pos();
582 }
583
584
585 ParagraphList::iterator InsetText::cpar() const
586 {
587         return text_.cursorPar();
588 }
589
590
591 RowList::iterator InsetText::crow() const
592 {
593         return cpar()->getRow(cpos());
594 }
595
596
597 void InsetText::setViewCache(BufferView const * bv) const
598 {
599         if (bv) {
600                 if (bv != text_.bv_owner) {
601                         //lyxerr << "setting view cache from "
602                         //      << text_.bv_owner << " to " << bv << "\n";
603                         text_.init(const_cast<BufferView *>(bv));
604                 }
605                 text_.bv_owner = const_cast<BufferView *>(bv);
606         }
607 }
608
609
610 void InsetText::removeNewlines()
611 {
612         ParagraphList::iterator it = paragraphs.begin();
613         ParagraphList::iterator end = paragraphs.end();
614         for (; it != end; ++it)
615                 for (int i = 0; i < it->size(); ++i)
616                         if (it->isNewline(i))
617                                 it->erase(i);
618 }
619
620
621 int InsetText::scroll(bool /*recursive*/) const
622 {
623         return UpdatableInset::scroll(false);
624 }
625
626
627 void InsetText::clearSelection(BufferView *)
628 {
629         text_.clearSelection();
630 }
631
632
633 void InsetText::clearInset(BufferView * bv, int start_x, int baseline) const
634 {
635         Painter & pain = bv->painter();
636         int w = dim_.wid;
637         int h = dim_.asc + dim_.des;
638         int ty = baseline - dim_.asc;
639
640         if (ty < 0) {
641                 h += ty;
642                 ty = 0;
643         }
644         if (ty + h > pain.paperHeight())
645                 h = pain.paperHeight();
646         if (xo_ + w > pain.paperWidth())
647                 w = pain.paperWidth();
648         pain.fillRectangle(start_x + 1, ty + 1, w - 3, h - 1, backgroundColor());
649 }
650
651
652 ParagraphList * InsetText::getParagraphs(int i) const
653 {
654         return (i == 0) ? const_cast<ParagraphList*>(&paragraphs) : 0;
655 }
656
657
658 LyXText * InsetText::getText(int i) const
659 {
660         return (i == 0) ? const_cast<LyXText*>(&text_) : 0;
661 }
662
663
664 bool InsetText::checkInsertChar(LyXFont & font)
665 {
666         return owner() ? owner()->checkInsertChar(font) : true;
667 }
668
669
670 void InsetText::collapseParagraphs(BufferView * bv)
671 {
672         while (paragraphs.size() > 1) {
673                 ParagraphList::iterator const first = paragraphs.begin();
674                 ParagraphList::iterator second = first;
675                 ++second;
676                 size_t const first_par_size = first->size();
677
678                 if (!first->empty() &&
679                     !second->empty() &&
680                     !first->isSeparator(first_par_size - 1)) {
681                         first->insertChar(first_par_size, ' ');
682                 }
683
684 #warning probably broken
685                 if (text_.selection.set()) {
686                         if (text_.selection.start.par() == 1) {
687                                 text_.selection.start.par(1);
688                                 text_.selection.start.pos(text_.selection.start.pos() + first_par_size);
689                         }
690                         if (text_.selection.end.par() == 2) {
691                                 text_.selection.end.par(1);
692                                 text_.selection.end.pos(text_.selection.end.pos() + first_par_size);
693                         }
694                 }
695
696                 mergeParagraph(bv->buffer()->params(), paragraphs, first);
697         }
698 }
699
700
701 void InsetText::getDrawFont(LyXFont & font) const
702 {
703         if (owner())
704                 owner()->getDrawFont(font);
705 }
706
707
708 void InsetText::appendParagraphs(Buffer * buffer, ParagraphList & plist)
709 {
710 #warning FIXME Check if Changes stuff needs changing here. (Lgb)
711 // And it probably does. You have to take a look at this John. (Lgb)
712 #warning John, have a look here. (Lgb)
713         ParagraphList::iterator pit = plist.begin();
714         ParagraphList::iterator ins = paragraphs.insert(paragraphs.end(), *pit);
715         ++pit;
716         mergeParagraph(buffer->params(), paragraphs, boost::prior(ins));
717
718         ParagraphList::iterator pend = plist.end();
719         for (; pit != pend; ++pit)
720                 paragraphs.push_back(*pit);
721 }
722
723
724 void InsetText::addPreview(PreviewLoader & loader) const
725 {
726         ParagraphList::const_iterator pit = paragraphs.begin();
727         ParagraphList::const_iterator pend = paragraphs.end();
728
729         for (; pit != pend; ++pit) {
730                 InsetList::const_iterator it  = pit->insetlist.begin();
731                 InsetList::const_iterator end = pit->insetlist.end();
732                 for (; it != end; ++it)
733                         it->inset->addPreview(loader);
734         }
735 }