2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "lyxparagraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
40 #include "minibuffer.h"
43 #include "support/textutils.h"
44 #include "support/LAssert.h"
53 extern unsigned char getCurrentTextClass(Buffer *);
54 #warning BAAAAAAAADDDDDDD current_view (but Lars wanted it :) !!!
55 extern BufferView * current_view;
58 InsetText::InsetText()
60 par = new LyXParagraph();
62 text = new LyXText(this);
66 InsetText::InsetText(InsetText const & ins) : UpdatableInset()
70 text = new LyXText(this);
71 autoBreakRows = ins.autoBreakRows;
75 InsetText & InsetText::operator=(InsetText const & it)
78 text = new LyXText(this);
79 autoBreakRows = it.autoBreakRows;
83 void InsetText::init(InsetText const * ins)
85 the_locking_inset = 0;
86 cursor_visible = false;
91 maxAscent = maxDescent = insetWidth = 0;
92 drawTextXOffset = drawTextYOffset = 0;
93 autoBreakRows = drawLockedFrame = false;
96 SetParagraphData(ins->par);
97 autoBreakRows = ins->autoBreakRows;
98 drawLockedFrame = ins->drawLockedFrame;
100 par->SetInsetOwner(this);
103 selection_start_cursor = selection_end_cursor = cursor;
104 frame_color = LColor::insetframe;
109 InsetText::~InsetText()
115 Inset * InsetText::Clone() const
117 InsetText * t = new InsetText(*this);
122 void InsetText::Write(Buffer const * buf, ostream & os) const
125 WriteParagraphData(buf, os);
129 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
131 par->writeFile(buf, os, buf->params, 0, 0);
135 void InsetText::Read(Buffer const * buf, LyXLex & lex)
137 string token, tmptok;
139 LyXParagraph * return_par = 0;
140 char depth = 0; // signed or unsigned?
141 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
142 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
143 LyXFont font(LyXFont::ALL_INHERIT);
146 par = new LyXParagraph;
147 par->SetInsetOwner(this);
151 token = lex.GetString();
154 if (token == "\\end_inset")
156 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
160 // the_end read this should NEVER happen
161 lex.printError("\\the_end read in inset! Error in document!");
165 if (token != "\\end_inset") {
166 lex.printError("Missing \\end_inset at this point. "
173 int InsetText::ascent(Painter & pain, LyXFont const & font) const
176 text->init(current_view);
177 computeTextRows(pain);
181 Row * row = text->GetRowNearY(y_temp);
182 return row->ascent_of_text() + 2;
186 int InsetText::descent(Painter & pain, LyXFont const & font) const
189 text->init(current_view);
190 computeTextRows(pain);
194 Row * row = text->GetRowNearY(y);
195 return text->height - row->ascent_of_text() + 2;
199 int InsetText::width(Painter & pain, LyXFont const &) const
202 text->init(current_view);
203 computeTextRows(pain);
206 return std::max(static_cast<long int>(getMaxTextWidth(pain, this)),
208 // return insetWidth;
212 void InsetText::draw(Painter & pain, LyXFont const & f,
213 int baseline, float & x) const
216 UpdatableInset::draw(pain, f, baseline, x);
219 text->init(current_view);
220 computeTextRows(pain);
223 if ((baseline != top_baseline) || (top_x != int(x))) {
224 top_baseline = baseline;
226 computeBaselines(baseline);
228 if (the_locking_inset && (text->cursor.pos() == inset_pos)) {
230 inset_x = text->cursor.x() - top_x + drawTextXOffset;
231 inset_y = text->cursor.y() + drawTextYOffset;
233 if (drawLockedFrame && locked) {
234 pain.rectangle(int(x), baseline - ascent(pain, f), width(pain, f),
235 ascent(pain,f) + descent(pain, f), frame_color);
237 x += TEXT_TO_INSET_OFFSET; // place for border
239 // Dump all rowinformation:
241 Row * tmprow = text->GetRowNearY(y_dummy);
242 lyxerr << "Baseline Paragraph Pos Height Ascent Fill\n";
244 lyxerr << tmprow->baseline() << '\t'
245 << tmprow->par() << '\t'
246 << tmprow->pos() << '\t'
247 << tmprow->height() << '\t'
248 << tmprow->ascent_of_text() << '\t'
249 << tmprow->fill() << '\n';
250 tmprow = tmprow->next();
254 Row * row = text->GetRowNearY(y);
255 y += baseline - row->ascent_of_text() + 1;
258 text->GetVisibleRow(current_view, y, x, row, y);
263 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
264 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
265 rows[r].baseline, x);
266 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
269 x += insetWidth - TEXT_TO_INSET_OFFSET;
273 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
274 int row, int baseline, float x) const
280 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
281 s_start = selection_end_cursor.pos();
282 s_end = selection_start_cursor.pos();
284 s_start = selection_start_cursor.pos();
285 s_end = selection_end_cursor.pos();
287 if ((s_start > endpos) || (s_end < startpos))
291 int ssel_x = esel_x = int(x);
294 for(; p < endpos; ++p) {
297 if ((p >= s_start) && (p <= s_end))
299 char ch = par->GetChar(p);
300 font = GetDrawFont(current_view->buffer(), par, p);
301 if (IsFloatChar(ch)) {
303 } else if (ch == LyXParagraph::META_INSET) {
304 Inset const * tmpinset = par->GetInset(p);
305 x += tmpinset->width(pain, font);
307 x += lyxfont::width(ch, font);
312 if ((p >= s_start) && (p <= s_end))
314 if (ssel_x < esel_x) {
315 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
316 int(esel_x - ssel_x),
317 rows[row].asc + rows[row].desc,
323 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
324 int baseline, float x) const
326 Assert(endpos <= par->Last());
328 for(int p = startpos; p < endpos; ++p) {
329 char ch = par->GetChar(p);
330 LyXFont font = GetDrawFont(current_view->buffer(), par, p);
331 if (IsFloatChar(ch)) {
333 } else if (par->IsNewline(p)) {
334 // Draw end-of-line marker
335 int wid = lyxfont::width('n', font);
336 int asc = lyxfont::maxAscent(font);
340 xp[0] = int(x + wid * 0.375);
341 yp[0] = int(y - 0.875 * asc * 0.75);
344 yp[1] = int(y - 0.500 * asc * 0.75);
346 xp[2] = int(x + wid * 0.375);
347 yp[2] = int(y - 0.125 * asc * 0.75);
349 pain.lines(xp, yp, 3, LColor::eolmarker);
352 yp[0] = int(y - 0.500 * asc * 0.75);
354 xp[1] = int(x + wid);
355 yp[1] = int(y - 0.500 * asc * 0.75);
357 xp[2] = int(x + wid);
358 yp[2] = int(y - asc * 0.75);
360 pain.lines(xp, yp, 3, LColor::eolmarker);
362 } else if (ch == LyXParagraph::META_INSET) {
363 Inset * tmpinset = par->GetInset(p);
365 tmpinset->draw(pain, font, baseline, x);
367 pain.text(int(x), baseline, ch, font);
368 x += lyxfont::width(ch, font);
374 char const * InsetText::EditMessage() const
376 return _("Opened Text Inset");
380 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
382 par->SetInsetOwner(this);
383 UpdatableInset::Edit(bv, x, y, button);
385 if (!bv->lockInset(this)) {
386 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
390 the_locking_inset = 0;
391 inset_pos = inset_x = inset_y = 0;
392 setPos(bv->painter(), x, y);
393 checkAndActivateInset(bv, x, y, button);
394 selection_start_cursor = selection_end_cursor = cursor;
395 current_font = real_current_font =GetFont(bv->buffer(), par, cursor.pos());
396 bv->text->FinishUndo();
397 UpdateLocal(bv, true);
401 void InsetText::InsetUnlock(BufferView * bv)
403 if (the_locking_inset) {
404 the_locking_inset->InsetUnlock(bv);
405 the_locking_inset = 0;
408 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
410 selection_start_cursor = selection_end_cursor = cursor;
411 no_selection = false;
413 UpdateLocal(bv, true);
417 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
419 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
422 if (inset == par->GetInset(text->cursor.pos())) {
423 lyxerr[Debug::INSETS] << "OK" << endl;
424 the_locking_inset = inset;
425 resetPos(bv->painter());
426 inset_x = text->cursor.x() - top_x + drawTextXOffset;
427 inset_y = text->cursor.y() + drawTextYOffset;
428 inset_pos = text->cursor.pos();
430 } else if (the_locking_inset && (the_locking_inset == inset)) {
431 if (text->cursor.pos() == inset_pos) {
432 lyxerr[Debug::INSETS] << "OK" << endl;
433 resetPos(bv->painter());
434 inset_x = text->cursor.x() - top_x + drawTextXOffset;
435 inset_y = text->cursor.y() + drawTextYOffset;
437 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
439 } else if (the_locking_inset) {
440 lyxerr[Debug::INSETS] << "MAYBE" << endl;
441 return the_locking_inset->LockInsetInInset(bv, inset);
443 lyxerr[Debug::INSETS] << "NOT OK" << endl;
448 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
451 if (!the_locking_inset)
453 if (the_locking_inset == inset) {
454 the_locking_inset->InsetUnlock(bv);
455 the_locking_inset = 0;
457 moveRight(bv, false);
460 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
464 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
466 if (!the_locking_inset)
468 if (the_locking_inset != inset)
469 return the_locking_inset->UpdateInsetInInset(bv, inset);
470 lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
472 UpdateLocal(bv, true);
473 if (text->cursor.pos() == inset_pos) {
474 inset_x = text->cursor.x() - top_x + drawTextXOffset;
475 inset_y = text->cursor.y() + drawTextYOffset;
481 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
483 if (hasSelection()) {
484 selection_start_cursor = selection_end_cursor = cursor;
485 UpdateLocal(bv, false);
487 no_selection = false;
488 setPos(bv->painter(), x, y);
490 if (the_locking_inset) {
491 UpdatableInset * inset = 0;
492 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET)
493 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
494 if (the_locking_inset == inset) {
495 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
498 // otherwise unlock the_locking_inset and lock the new inset
499 the_locking_inset->InsetUnlock(bv);
500 inset_x = cursor.x() - top_x + drawTextXOffset;
501 inset_y = cursor.y() + drawTextYOffset;
502 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
503 inset->Edit(bv, x - inset_x, y - inset_y, button);
504 UpdateLocal(bv, true);
507 // otherwise only unlock the_locking_inset
508 the_locking_inset->InsetUnlock(bv);
509 the_locking_inset = 0;
511 if (bv->the_locking_inset) {
512 if ((par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) &&
513 par->GetInset(cursor.pos()) &&
514 (par->GetInset(cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE)) {
515 UpdatableInset * inset =
516 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
517 inset_x = cursor.x() - top_x + drawTextXOffset;
518 inset_y = cursor.y() + drawTextYOffset;
519 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
520 inset->Edit(bv, x - inset_x, y - inset_y, 0);
521 UpdateLocal(bv, true);
524 selection_start_cursor = selection_end_cursor = cursor;
528 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
530 UpdatableInset * inset = 0;
532 if (the_locking_inset) {
533 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
535 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
536 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
537 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
538 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
540 inset_x = cursor.x() - top_x + drawTextXOffset;
541 inset_y = cursor.y() + drawTextYOffset;
542 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
543 inset->Edit(bv, x - inset_x, y - inset_y, button);
547 no_selection = false;
551 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
553 if (the_locking_inset) {
554 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
559 LyXCursor old = selection_end_cursor;
561 setPos(bv->painter(), x, y);
562 selection_end_cursor = cursor;
563 if (old != selection_end_cursor)
564 UpdateLocal(bv, false);
567 no_selection = false;
571 void InsetText::InsetKeyPress(XKeyEvent * xke)
573 if (the_locking_inset) {
574 the_locking_inset->InsetKeyPress(xke);
580 UpdatableInset::RESULT
581 InsetText::LocalDispatch(BufferView * bv,
582 int action, string const & arg)
584 no_selection = false;
585 UpdatableInset::RESULT
586 result= UpdatableInset::LocalDispatch(bv, action, arg);
587 if (result != UNDISPATCHED) {
588 resetPos(bv->painter());
593 if ((action < 0) && arg.empty())
596 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
597 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
599 if (the_locking_inset) {
600 result = the_locking_inset->LocalDispatch(bv, action, arg);
601 if (result == DISPATCHED_NOUPDATE)
603 else if (result == DISPATCHED) {
604 the_locking_inset->ToggleInsetCursor(bv);
605 UpdateLocal(bv, false);
606 the_locking_inset->ToggleInsetCursor(bv);
608 } else if (result == FINISHED) {
612 cursor.pos(inset_pos + 1);
613 resetPos(bv->painter());
619 the_locking_inset = 0;
627 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
628 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
629 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
630 cutSelection(bv->buffer());
631 cursor = selection_start_cursor;
632 par->InsertChar(cursor.pos(), arg[0]);
633 SetCharFont(bv->buffer(), cursor.pos(), current_font);
634 cursor.pos(cursor.pos() + 1);
635 selection_start_cursor = selection_end_cursor = cursor;
636 UpdateLocal(bv, true);
638 // --- Cursor Movements ---------------------------------------------
640 bv->text->FinishUndo();
641 moveRight(bv, false);
642 selection_end_cursor = cursor;
643 UpdateLocal(bv, false);
646 bv->text->FinishUndo();
647 result = moveRight(bv);
648 if (hasSelection()) {
649 selection_start_cursor = selection_end_cursor = cursor;
650 UpdateLocal(bv, false);
652 selection_start_cursor = selection_end_cursor = cursor;
656 bv->text->FinishUndo();
658 selection_end_cursor = cursor;
659 UpdateLocal(bv, false);
662 bv->text->FinishUndo();
663 result= moveLeft(bv);
664 if (hasSelection()) {
665 selection_start_cursor = selection_end_cursor = cursor;
666 UpdateLocal(bv, false);
668 selection_start_cursor = selection_end_cursor = cursor;
672 bv->text->FinishUndo();
674 selection_end_cursor = cursor;
675 UpdateLocal(bv, false);
678 bv->text->FinishUndo();
679 result = moveDown(bv);
680 if (hasSelection()) {
681 selection_start_cursor = selection_end_cursor = cursor;
682 UpdateLocal(bv, false);
684 selection_start_cursor = selection_end_cursor = cursor;
688 bv->text->FinishUndo();
690 selection_end_cursor = cursor;
691 UpdateLocal(bv, false);
694 bv->text->FinishUndo();
696 if (hasSelection()) {
697 selection_start_cursor = selection_end_cursor = cursor;
698 UpdateLocal(bv, false);
700 selection_start_cursor = selection_end_cursor = cursor;
705 if (hasSelection()) {
706 selection_start_cursor = selection_end_cursor = cursor;
707 UpdateLocal(bv, false);
714 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
715 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
716 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
718 if (hasSelection()) {
719 LyXParagraph::size_type i = selection_start_cursor.pos();
720 for (; i < selection_end_cursor.pos(); ++i) {
721 par->Erase(selection_start_cursor.pos());
725 if (ret) { // we need update
726 selection_start_cursor = selection_end_cursor = cursor;
727 UpdateLocal(bv, true);
728 } else if (hasSelection()) {
729 selection_start_cursor = selection_end_cursor = cursor;
730 UpdateLocal(bv, false);
733 resetPos(bv->painter());
736 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
737 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
738 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
740 if (cutSelection(bv->buffer())) {
742 cursor = selection_end_cursor = selection_start_cursor;
743 UpdateLocal(bv, true);
744 } else if (hasSelection()) {
745 selection_start_cursor = selection_end_cursor = cursor;
746 UpdateLocal(bv, false);
748 resetPos(bv->painter());
751 bv->text->FinishUndo();
752 if (copySelection(bv->buffer())) {
754 selection_start_cursor = selection_end_cursor = cursor;
755 UpdateLocal(bv, true);
756 } else if (hasSelection()) {
757 selection_start_cursor = selection_end_cursor = cursor;
758 UpdateLocal(bv, false);
763 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
764 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
765 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
766 if (pasteSelection(bv->buffer())) {
767 selection_start_cursor = selection_end_cursor = cursor;
768 UpdateLocal(bv, true);
771 resetPos(bv->painter());
774 bv->text->FinishUndo();
775 for(; cursor.pos() > rows[actrow].pos;) {
776 cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
777 cursor.pos(cursor.pos() - 1);
780 cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
781 if (hasSelection()) {
782 selection_start_cursor = selection_end_cursor = cursor;
783 UpdateLocal(bv, false);
785 selection_start_cursor = selection_end_cursor = cursor;
787 resetPos(bv->painter());
791 bv->text->FinishUndo();
792 int checkpos = (int)rows[actrow + 1].pos;
793 if ((actrow + 2) < (int)rows.size())
795 for(; cursor.pos() < checkpos;) {
796 cursor.x(cursor.x() + SingleWidth(bv->painter(), par, cursor.pos()));
797 cursor.pos(cursor.pos() + 1);
799 if (hasSelection()) {
800 selection_start_cursor = selection_end_cursor = cursor;
801 UpdateLocal(bv, false);
803 selection_start_cursor = selection_end_cursor = cursor;
806 resetPos(bv->painter());
808 case LFUN_BREAKPARAGRAPH:
812 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
813 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
814 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
815 par->InsertChar(cursor.pos(),LyXParagraph::META_NEWLINE);
816 SetCharFont(bv->buffer(), cursor.pos(),current_font);
817 UpdateLocal(bv, true);
818 cursor.pos(cursor.pos() + 1);
819 selection_start_cursor = selection_end_cursor = cursor;
820 resetPos(bv->painter());
824 static LyXTextClass::size_type cur_layout = par->layout;
826 // Derive layout number from given argument (string)
827 // and current buffer's textclass (number). */
828 LyXTextClassList::ClassList::size_type tclass =
829 bv->buffer()->params.textclass;
830 std::pair <bool, LyXTextClass::size_type> layout =
831 textclasslist.NumberOfLayout(tclass, arg);
833 // If the entry is obsolete, use the new one instead.
835 string obs = textclasslist.Style(tclass,layout.second).
838 layout = textclasslist.NumberOfLayout(tclass, obs);
841 // see if we found the layout number:
843 string msg = string(N_("Layout ")) + arg + N_(" not known");
845 bv->owner()->getMiniBuffer()->Set(msg);
849 if (cur_layout != layout.second) {
850 cur_layout = layout.second;
851 text->SetLayout(bv, layout.second);
852 bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
853 UpdateLocal(bv, true);
858 result = UNDISPATCHED;
861 if (result != FINISHED) {
864 bv->unlockInset(this);
869 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
872 buf->latexParagraphs(os, par, 0, texrow);
873 return texrow.rows();
877 void InsetText::Validate(LaTeXFeatures & features) const
879 par->validate(features);
883 // Returns the width of a character at a certain spot
884 int InsetText::SingleWidth(Painter & pain, LyXParagraph * p, int pos) const
886 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
887 char c = p->GetChar(pos);
889 if (IsPrintable(c)) {
890 return lyxfont::width(c, font);
891 } else if (c == LyXParagraph::META_INSET) {
892 Inset const * tmpinset = p->GetInset(pos);
894 return tmpinset->width(pain, font);
897 } else if (IsSeparatorChar(c))
899 else if (IsNewlineChar(c))
901 return lyxfont::width(c, font);
905 // Returns the width of a character at a certain spot
906 void InsetText::SingleHeight(Painter & pain, LyXParagraph * p,int pos,
907 int & asc, int & desc) const
909 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
910 char c = p->GetChar(pos);
913 if (c == LyXParagraph::META_INSET) {
914 Inset const * tmpinset=p->GetInset(pos);
916 asc = tmpinset->ascent(pain, font);
917 desc = tmpinset->descent(pain, font);
920 asc = lyxfont::maxAscent(font);
921 desc = lyxfont::maxDescent(font);
927 // Gets the fully instantiated font at a given position in a paragraph
928 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
929 // The difference is that this one is used for displaying, and thus we
930 // are allowed to make cosmetic improvements. For instance make footnotes
932 // If position is -1, we get the layout font of the paragraph.
933 // If position is -2, we get the font of the manual label of the paragraph.
934 LyXFont InsetText::GetFont(Buffer const * buf, LyXParagraph * p, int pos) const
936 char par_depth = p->GetDepth();
938 LyXLayout const & layout =
939 textclasslist.Style(buf->params.textclass, p->GetLayout());
941 // We specialize the 95% common case:
942 if (p->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
945 if (layout.labeltype == LABEL_MANUAL
946 && pos < BeginningOfMainBody(buf, p)) {
948 return p->GetFontSettings(buf->params,
949 pos).realize(layout.reslabelfont);
951 return p->GetFontSettings(buf->params,
952 pos).realize(layout.resfont);
955 // process layoutfont for pos == -1 and labelfont for pos < -1
957 return layout.resfont;
959 return layout.reslabelfont;
962 // The uncommon case need not be optimized as much
964 LyXFont layoutfont, tmpfont;
968 if (pos < BeginningOfMainBody(buf, p)) {
970 layoutfont = layout.labelfont;
973 layoutfont = layout.font;
975 tmpfont = p->GetFontSettings(buf->params, pos);
976 tmpfont.realize(layoutfont);
979 // process layoutfont for pos == -1 and labelfont for pos < -1
981 tmpfont = layout.font;
983 tmpfont = layout.labelfont;
986 // Resolve against environment font information
987 //if (par->GetDepth()){ // already in while condition
988 while (p && par_depth && !tmpfont.resolved()) {
989 p = p->DepthHook(par_depth - 1);
991 tmpfont.realize(textclasslist.Style(buf->params.textclass,
992 p->GetLayout()).font);
993 par_depth = p->GetDepth();
996 tmpfont.realize((textclasslist.TextClass(buf->params.textclass).
1002 // the font for drawing may be different from the real font
1003 LyXFont InsetText::GetDrawFont(Buffer const * buf, LyXParagraph * p, int pos) const
1005 return GetFont(buf, p, pos);
1009 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1011 if (textclasslist.Style(buf->params.textclass,
1012 p->GetLayout()).labeltype != LABEL_MANUAL)
1015 return p->BeginningOfMainBody();
1019 void InsetText::GetCursorPos(int & x, int & y) const
1026 int InsetText::InsetInInsetY()
1028 if (!the_locking_inset)
1031 return (inset_y + the_locking_inset->InsetInInsetY());
1035 void InsetText::ToggleInsetCursor(BufferView * bv)
1037 if (the_locking_inset) {
1038 the_locking_inset->ToggleInsetCursor(bv);
1042 LyXFont font = GetDrawFont(bv->buffer(), par, text->cursor.pos());
1044 int asc = lyxfont::maxAscent(font);
1045 int desc = lyxfont::maxDescent(font);
1047 asc = text->cursor.row()->ascent_of_text();
1048 desc = text->cursor.row()->height() - asc;
1050 bv->hideLockedInsetCursor();
1052 bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(),
1054 cursor_visible = !cursor_visible;
1058 void InsetText::ShowInsetCursor(BufferView * bv)
1060 if (the_locking_inset) {
1061 the_locking_inset->ShowInsetCursor(bv);
1064 if (!cursor_visible) {
1065 LyXFont font = GetDrawFont(bv->buffer(), par, cursor.pos());
1067 int asc = lyxfont::maxAscent(font);
1068 int desc = lyxfont::maxDescent(font);
1069 asc = text->cursor.row()->ascent_of_text();
1070 desc = text->cursor.row()->height() - asc;
1071 bv->fitLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1072 bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1073 cursor_visible = true;
1078 void InsetText::HideInsetCursor(BufferView * bv)
1080 if (cursor_visible) {
1081 bv->hideLockedInsetCursor();
1082 cursor_visible = false;
1084 if (the_locking_inset)
1085 the_locking_inset->HideInsetCursor(bv);
1089 void InsetText::setPos(Painter & pain, int x, int y) const
1091 x -= drawTextXOffset;
1092 y -= drawTextYOffset;
1093 // search right X-pos x==0 -> top_x
1096 cursor.y(top_baseline);
1098 for(unsigned int i = 1;
1099 (long(cursor.y() + rows[i - 1].desc) < y)
1100 && (i < rows.size() - 1); ++i) {
1101 cursor.y(rows[i].baseline);
1102 cursor.pos(rows[i].pos);
1105 cursor.y(cursor.y() - top_baseline);
1106 cursor.x(top_x + 2); // 2 = frame width
1110 int sw = swh = SingleWidth(pain, par, cursor.pos());
1111 if (par->GetChar(cursor.pos()) != LyXParagraph::META_INSET)
1113 int checkpos = rows[actrow + 1].pos;
1114 if ((actrow + 2) < (int)rows.size())
1116 while ((cursor.pos() < checkpos) && ((cursor.x() + swh) < x)) {
1117 cursor.x(cursor.x() + sw);
1118 cursor.pos(cursor.pos() + 1);
1119 sw = swh = SingleWidth(pain, par, cursor.pos());
1120 if (par->GetChar(cursor.pos())!=LyXParagraph::META_INSET)
1126 void InsetText::resetPos(Painter & pain) const
1133 int old_pos = cursor.pos();
1135 cursor.y(top_baseline);
1137 for(unsigned int i = 0;
1138 i < (rows.size() - 1) && rows[i].pos <= cursor.pos();
1140 cursor.y(rows[i].baseline);
1143 cursor.y(cursor.y() - top_baseline);
1144 setPos(pain, 0, cursor.y());
1145 cursor.x(top_x + 2); // 2 = frame width
1146 while(cursor.pos() < old_pos) {
1147 cursor.x(cursor.x() + SingleWidth(pain, par,cursor.pos()));
1148 cursor.pos(cursor.pos() + 1);
1153 UpdatableInset::RESULT
1154 InsetText::moveRight(BufferView * bv, bool activate_inset)
1156 if (cursor.pos() >= par->Last())
1158 if (activate_inset && checkAndActivateInset(bv)) {
1161 cursor.pos(cursor.pos() + 1);
1162 resetPos(bv->painter());
1163 real_current_font = current_font =GetFont(bv->buffer(), par, cursor.pos());
1164 return DISPATCHED_NOUPDATE;
1168 UpdatableInset::RESULT
1169 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1171 if (cursor.pos() <= 0)
1173 cursor.pos(cursor.pos() - 1);
1174 resetPos(bv->painter());
1176 if (checkAndActivateInset(bv, -1, -1))
1178 return DISPATCHED_NOUPDATE;
1182 UpdatableInset::RESULT
1183 InsetText::moveUp(BufferView * bv)
1187 cursor.y(rows[actrow - 1].baseline - top_baseline);
1188 if (cursor.x_fix() < 0)
1189 cursor.x_fix(cursor.x());
1190 setPos(bv->painter(),
1191 cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1192 return DISPATCHED_NOUPDATE;
1196 UpdatableInset::RESULT
1197 InsetText::moveDown(BufferView * bv)
1199 if (actrow >= int(rows.size() - 2))
1201 cursor.y(rows[actrow + 1].baseline - top_baseline);
1202 if (cursor.x_fix() < 0)
1203 cursor.x_fix(cursor.x());
1204 setPos(bv->painter(),
1205 cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1206 return DISPATCHED_NOUPDATE;
1210 bool InsetText::Delete()
1212 if ((par->GetChar(cursor.pos())==LyXParagraph::META_INSET) &&
1213 !par->GetInset(cursor.pos())->Deletable()) {
1216 par->Erase(cursor.pos());
1221 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1223 if (the_locking_inset) {
1224 if (the_locking_inset->InsertInsetAllowed(inset))
1225 return the_locking_inset->InsertInset(bv, inset);
1228 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1229 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1230 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1231 if (inset->Editable() == Inset::IS_EDITABLE) {
1232 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1233 i->setOwner(static_cast<UpdatableInset *>(this));
1235 par->InsertChar(cursor.pos(), LyXParagraph::META_INSET);
1236 par->InsertInset(cursor.pos(), inset);
1237 if (hasSelection()) {
1238 selection_start_cursor = selection_end_cursor = cursor;
1240 selection_start_cursor = selection_end_cursor = cursor;
1242 UpdateLocal(bv, true);
1243 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1248 UpdatableInset * InsetText::GetLockingInset()
1250 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1254 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1258 if (the_locking_inset)
1259 return the_locking_inset->GetFirstLockingInsetOfType(c);
1264 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1266 // if there is no selection just set the current_font
1267 if (!hasSelection()) {
1268 // Determine basis font
1270 if (cursor.pos() < BeginningOfMainBody(bv->buffer(), par))
1271 layoutfont = GetFont(bv->buffer(), par, -2);
1273 layoutfont = GetFont(bv->buffer(), par, -1);
1275 // Update current font
1276 real_current_font.update(font, bv->buffer()->params.language_info,
1279 // Reduce to implicit settings
1280 current_font = real_current_font;
1281 current_font.reduce(layoutfont);
1282 // And resolve it completely
1283 real_current_font.realize(layoutfont);
1288 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1289 s_start = selection_end_cursor.pos();
1290 s_end = selection_start_cursor.pos();
1292 s_start = selection_start_cursor.pos();
1293 s_end = selection_end_cursor.pos();
1296 while(s_start < s_end) {
1297 newfont = GetFont(bv->buffer(), par,s_start);
1298 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1299 SetCharFont(bv->buffer(), s_start, newfont);
1302 UpdateLocal(bv, true);
1306 void InsetText::SetCharFont(Buffer const * buf, int pos, LyXFont const & f) //
1308 /* let the insets convert their font */
1311 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1312 if (par->GetInset(pos))
1313 font = par->GetInset(pos)->ConvertFont(font);
1315 LyXLayout const & layout =
1316 textclasslist.Style(buf->params.textclass,par->GetLayout());
1318 // Get concrete layout font to reduce against
1321 if (pos < BeginningOfMainBody(buf, par))
1322 layoutfont = layout.labelfont;
1324 layoutfont = layout.font;
1327 layoutfont.realize((textclasslist.TextClass(buf->params.textclass).
1330 // Now, reduce font against full layout font
1331 font.reduce(layoutfont);
1333 par->SetFont(pos, font);
1337 void InsetText::computeTextRows(Painter & pain) const
1351 int width = wordAscent = wordDescent = 0;
1352 insetWidth = maxAscent = maxDescent = 0;
1357 rows.push_back(row);
1358 if (!autoBreakRows || (getMaxTextWidth(pain, this) < 0)) {
1359 for(p = 0; p < par->Last(); ++p) {
1360 insetWidth += SingleWidth(pain, par, p);
1361 SingleHeight(pain, par, p, asc, desc);
1362 maxAscent = max(maxAscent, asc);
1363 maxDescent = max(maxDescent, desc);
1365 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1366 rows[0].asc = maxAscent;
1367 rows[0].desc = maxDescent;
1368 // alocate a dummy row for the endpos
1369 row.pos = par->Last();
1370 rows.push_back(row);
1374 bool is_first_word_in_row = true;
1375 int cw, lastWordWidth = 0;
1376 int maxWidth = getMaxTextWidth(pain, this);
1377 // if we auto break rows than the insetwidth should be always the max
1378 // width as the display is stable it may get larger if we have a really
1379 // large word below and we draw it!!!
1380 insetWidth = maxWidth;
1382 for(p = 0; p < par->Last(); ++p) {
1383 if (par->IsNewline(p)) {
1384 rows.back().asc = wordAscent;
1385 rows.back().desc = wordDescent;
1387 rows.push_back(row);
1389 width = lastWordWidth = 0;
1390 oasc = odesc = wordAscent = wordDescent = 0;
1391 is_first_word_in_row = true;
1394 cw = SingleWidth(pain, par, p);
1396 if (par->GetChar(p) == LyXParagraph::META_INSET)
1397 inset = par->GetInset(p);
1398 if (inset && inset->display()) {
1399 inset->setOwner(const_cast<InsetText *>(this));
1400 if (cw > insetWidth)
1402 if (!is_first_word_in_row || (p != nwp)) {
1403 oasc = max(oasc, wordAscent);
1404 odesc = max(odesc, wordDescent);
1405 rows.back().asc = oasc;
1406 rows.back().desc = odesc;
1408 rows.push_back(row);
1410 SingleHeight(pain, par, p, asc, desc);
1411 rows.back().asc = asc;
1412 rows.back().desc = desc;
1413 row.pos = nwp = p + 1;
1414 rows.push_back(row);
1415 width = lastWordWidth = 0;
1416 oasc = odesc = wordAscent = wordDescent = 0;
1417 is_first_word_in_row = true;
1420 SingleHeight(pain, par, p, asc, desc);
1422 lastWordWidth += cw;
1423 if (width > maxWidth) {
1424 if (is_first_word_in_row) {
1425 if (!(width-cw)) { // only this character in word
1426 rows.back().asc = asc;
1427 rows.back().desc = desc;
1429 rows.push_back(row);
1435 lastWordWidth = width = 0;
1437 rows.back().asc = wordAscent;
1438 rows.back().desc = wordDescent;
1440 rows.push_back(row);
1445 lastWordWidth = width = cw;
1449 rows.back().asc = oasc;
1450 rows.back().desc = odesc;
1452 rows.push_back(row);
1454 odesc = wordDescent;
1455 width = lastWordWidth;
1456 wordAscent = max(wordAscent, asc);
1457 wordDescent = max(wordDescent, desc);
1458 is_first_word_in_row = true;
1461 wordAscent = max(wordAscent, asc);
1462 wordDescent = max(wordDescent, desc);
1464 if (par->IsSeparator(p) || inset) {
1466 inset->setOwner(const_cast<InsetText *>(this));
1470 oasc = max(oasc, wordAscent);
1471 odesc = max(odesc, wordDescent);
1472 wordAscent = wordDescent = lastWordWidth = 0;
1474 is_first_word_in_row = false;
1477 // if we have some data in the paragraph we have ascent/descent
1479 // assign last row data
1480 rows.back().asc = max(oasc, wordAscent);
1481 rows.back().desc = max(odesc, wordDescent);
1483 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1484 // alocate a dummy row for the endpos
1485 row.pos = par->Last();
1486 rows.push_back(row);
1487 // calculate maxAscent/Descent
1488 maxAscent = rows[0].asc;
1489 maxDescent = rows[0].desc;
1490 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1491 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1496 void InsetText::computeBaselines(int baseline) const
1498 rows[0].baseline = baseline;
1499 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1500 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1501 rows[i].asc + interline_space;
1506 void InsetText::UpdateLocal(BufferView * bv, bool flag)
1509 text->FullRebreak(bv);
1510 computeTextRows(bv->painter());
1511 computeBaselines(top_baseline);
1513 bv->updateInset(this, flag);
1515 resetPos(bv->painter());
1516 bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
1520 bool InsetText::cutSelection(Buffer const * buf)
1522 if (!hasSelection())
1527 LyXParagraph * endpar = par;
1529 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1530 start = selection_end_cursor.pos();
1531 end = selection_start_cursor.pos();
1533 start = selection_start_cursor.pos();
1534 end = selection_end_cursor.pos();
1537 return cap.cutSelection(par, &endpar, start, end, buf->params.textclass);
1541 bool InsetText::copySelection(Buffer const * buf)
1543 if (!hasSelection())
1549 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1550 start = selection_end_cursor.pos();
1551 end = selection_start_cursor.pos();
1553 start = selection_start_cursor.pos();
1554 end = selection_end_cursor.pos();
1556 return cap.copySelection(par, par, start, end, buf->params.textclass);
1560 bool InsetText::pasteSelection(Buffer const * buf)
1564 if (cap.nrOfParagraphs() > 1) {
1565 WriteAlert(_("Impossible operation"),
1566 _("Cannot include more than one paragraph!"),
1570 LyXParagraph * endpar;
1571 LyXParagraph * actpar = par;
1573 int pos = cursor.pos();
1574 bool ret = cap.pasteSelection(&actpar, &endpar, pos,buf->params.textclass);
1580 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1583 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
1584 UpdatableInset * inset =
1585 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
1586 LyXFont font = GetFont(bv->buffer(), par, cursor.pos());
1588 x = inset->width(bv->painter(), font);
1590 y = inset->descent(bv->painter(), font);
1591 inset_x = cursor.x() - top_x + drawTextXOffset;
1592 inset_y = cursor.y() + drawTextYOffset;
1593 inset->Edit(bv, x - inset_x, y - inset_y, button);
1594 if (!the_locking_inset)
1596 UpdateLocal(bv, true);
1603 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset) const
1605 // int w=getMaxWidth(pain, inset);
1607 return getMaxWidth(pain, inset) - 4; // 2+2 width of eventual border
1610 void InsetText::SetParagraphData(LyXParagraph *p)
1615 par->SetInsetOwner(this);
1619 void InsetText::SetAutoBreakRows(bool flag)
1621 if (flag != autoBreakRows) {
1622 autoBreakRows = flag;
1627 void InsetText::SetDrawLockedFrame(bool flag)
1629 if (flag != drawLockedFrame) {
1630 drawLockedFrame = flag;
1635 void InsetText::SetFrameColor(LColor::color col)
1637 if (frame_color != col) {