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