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();
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();
199 int InsetText::width(Painter & pain, LyXFont const &) const
202 text->init(current_view);
203 computeTextRows(pain);
207 // return insetWidth;
211 void InsetText::draw(Painter & pain, LyXFont const & f,
212 int baseline, float & x) const
215 UpdatableInset::draw(pain, f, baseline, x);
218 text->init(current_view);
219 computeTextRows(pain);
222 if ((baseline != top_baseline) || (top_x != int(x))) {
223 top_baseline = baseline;
225 computeBaselines(baseline);
227 if (the_locking_inset && (text->cursor.pos() == inset_pos)) {
229 inset_x = text->cursor.x() - top_x + drawTextXOffset;
230 inset_y = text->cursor.y() + drawTextYOffset;
232 if (drawLockedFrame && locked) {
233 pain.rectangle(int(x), baseline - ascent(pain, f), insetWidth,
234 ascent(pain,f) + descent(pain, f), frame_color);
236 x += TEXT_TO_INSET_OFFSET; // place for border
239 Row * row = text->GetRowNearY(y);
240 y += baseline - row->ascent_of_text();
243 text->GetVisibleRow(current_view, y, top_x, row, y);
248 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
249 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
250 rows[r].baseline, x);
251 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
254 x += insetWidth - TEXT_TO_INSET_OFFSET;
258 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
259 int row, int baseline, float x) const
265 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
266 s_start = selection_end_cursor.pos();
267 s_end = selection_start_cursor.pos();
269 s_start = selection_start_cursor.pos();
270 s_end = selection_end_cursor.pos();
272 if ((s_start > endpos) || (s_end < startpos))
276 int ssel_x = esel_x = int(x);
279 for(; p < endpos; ++p) {
282 if ((p >= s_start) && (p <= s_end))
284 char ch = par->GetChar(p);
285 font = GetDrawFont(current_view->buffer(), par, p);
286 if (IsFloatChar(ch)) {
288 } else if (ch == LyXParagraph::META_INSET) {
289 Inset const * tmpinset = par->GetInset(p);
290 x += tmpinset->width(pain, font);
292 x += lyxfont::width(ch, font);
297 if ((p >= s_start) && (p <= s_end))
299 if (ssel_x < esel_x) {
300 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
301 int(esel_x - ssel_x),
302 rows[row].asc + rows[row].desc,
308 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
309 int baseline, float x) const
311 Assert(endpos <= par->Last());
313 for(int p = startpos; p < endpos; ++p) {
314 char ch = par->GetChar(p);
315 LyXFont font = GetDrawFont(current_view->buffer(), par, p);
316 if (IsFloatChar(ch)) {
318 } else if (par->IsNewline(p)) {
319 // Draw end-of-line marker
320 int wid = lyxfont::width('n', font);
321 int asc = lyxfont::maxAscent(font);
325 xp[0] = int(x + wid * 0.375);
326 yp[0] = int(y - 0.875 * asc * 0.75);
329 yp[1] = int(y - 0.500 * asc * 0.75);
331 xp[2] = int(x + wid * 0.375);
332 yp[2] = int(y - 0.125 * asc * 0.75);
334 pain.lines(xp, yp, 3, LColor::eolmarker);
337 yp[0] = int(y - 0.500 * asc * 0.75);
339 xp[1] = int(x + wid);
340 yp[1] = int(y - 0.500 * asc * 0.75);
342 xp[2] = int(x + wid);
343 yp[2] = int(y - asc * 0.75);
345 pain.lines(xp, yp, 3, LColor::eolmarker);
347 } else if (ch == LyXParagraph::META_INSET) {
348 Inset * tmpinset = par->GetInset(p);
350 tmpinset->draw(pain, font, baseline, x);
352 pain.text(int(x), baseline, ch, font);
353 x += lyxfont::width(ch, font);
359 char const * InsetText::EditMessage() const
361 return _("Opened Text Inset");
365 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
367 par->SetInsetOwner(this);
368 UpdatableInset::Edit(bv, x, y, button);
370 if (!bv->lockInset(this)) {
371 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
375 the_locking_inset = 0;
376 inset_pos = inset_x = inset_y = 0;
377 setPos(bv->painter(), x, y);
378 checkAndActivateInset(bv, x, y, button);
379 selection_start_cursor = selection_end_cursor = cursor;
380 current_font = real_current_font =GetFont(bv->buffer(), par, cursor.pos());
381 bv->text->FinishUndo();
382 UpdateLocal(bv, true);
386 void InsetText::InsetUnlock(BufferView * bv)
388 if (the_locking_inset) {
389 the_locking_inset->InsetUnlock(bv);
390 the_locking_inset = 0;
393 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
395 selection_start_cursor = selection_end_cursor = cursor;
396 no_selection = false;
398 UpdateLocal(bv, true);
402 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
404 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
407 if (inset == par->GetInset(text->cursor.pos())) {
408 lyxerr[Debug::INSETS] << "OK" << endl;
409 the_locking_inset = inset;
410 resetPos(bv->painter());
411 inset_x = text->cursor.x() - top_x + drawTextXOffset;
412 inset_y = text->cursor.y() + drawTextYOffset;
413 inset_pos = text->cursor.pos();
415 } else if (the_locking_inset && (the_locking_inset == inset)) {
416 if (text->cursor.pos() == inset_pos) {
417 lyxerr[Debug::INSETS] << "OK" << endl;
418 resetPos(bv->painter());
419 inset_x = text->cursor.x() - top_x + drawTextXOffset;
420 inset_y = text->cursor.y() + drawTextYOffset;
422 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
424 } else if (the_locking_inset) {
425 lyxerr[Debug::INSETS] << "MAYBE" << endl;
426 return the_locking_inset->LockInsetInInset(bv, inset);
428 lyxerr[Debug::INSETS] << "NOT OK" << endl;
433 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
436 if (!the_locking_inset)
438 if (the_locking_inset == inset) {
439 the_locking_inset->InsetUnlock(bv);
440 the_locking_inset = 0;
442 moveRight(bv, false);
445 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
449 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
451 if (!the_locking_inset)
453 if (the_locking_inset != inset)
454 return the_locking_inset->UpdateInsetInInset(bv, inset);
455 lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
457 UpdateLocal(bv, true);
458 if (text->cursor.pos() == inset_pos) {
459 inset_x = text->cursor.x() - top_x + drawTextXOffset;
460 inset_y = text->cursor.y() + drawTextYOffset;
466 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
468 if (hasSelection()) {
469 selection_start_cursor = selection_end_cursor = cursor;
470 UpdateLocal(bv, false);
472 no_selection = false;
473 setPos(bv->painter(), x, y);
475 if (the_locking_inset) {
476 UpdatableInset * inset = 0;
477 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET)
478 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
479 if (the_locking_inset == inset) {
480 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
483 // otherwise unlock the_locking_inset and lock the new inset
484 the_locking_inset->InsetUnlock(bv);
485 inset_x = cursor.x() - top_x + drawTextXOffset;
486 inset_y = cursor.y() + drawTextYOffset;
487 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
488 inset->Edit(bv, x - inset_x, y - inset_y, button);
489 UpdateLocal(bv, true);
492 // otherwise only unlock the_locking_inset
493 the_locking_inset->InsetUnlock(bv);
494 the_locking_inset = 0;
496 if (bv->the_locking_inset) {
497 if ((par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) &&
498 par->GetInset(cursor.pos()) &&
499 (par->GetInset(cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE)) {
500 UpdatableInset * inset =
501 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
502 inset_x = cursor.x() - top_x + drawTextXOffset;
503 inset_y = cursor.y() + drawTextYOffset;
504 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
505 inset->Edit(bv, x - inset_x, y - inset_y, 0);
506 UpdateLocal(bv, true);
509 selection_start_cursor = selection_end_cursor = cursor;
513 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
515 UpdatableInset * inset = 0;
517 if (the_locking_inset) {
518 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
520 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
521 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
522 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
523 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
525 inset_x = cursor.x() - top_x + drawTextXOffset;
526 inset_y = cursor.y() + drawTextYOffset;
527 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
528 inset->Edit(bv, x - inset_x, y - inset_y, button);
532 no_selection = false;
536 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
538 if (the_locking_inset) {
539 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
544 LyXCursor old = selection_end_cursor;
546 setPos(bv->painter(), x, y);
547 selection_end_cursor = cursor;
548 if (old != selection_end_cursor)
549 UpdateLocal(bv, false);
552 no_selection = false;
556 void InsetText::InsetKeyPress(XKeyEvent * xke)
558 if (the_locking_inset) {
559 the_locking_inset->InsetKeyPress(xke);
565 UpdatableInset::RESULT
566 InsetText::LocalDispatch(BufferView * bv,
567 int action, string const & arg)
569 no_selection = false;
570 UpdatableInset::RESULT
571 result= UpdatableInset::LocalDispatch(bv, action, arg);
572 if (result != UNDISPATCHED) {
573 resetPos(bv->painter());
578 if ((action < 0) && arg.empty())
581 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
582 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
584 if (the_locking_inset) {
585 result = the_locking_inset->LocalDispatch(bv, action, arg);
586 if (result == DISPATCHED_NOUPDATE)
588 else if (result == DISPATCHED) {
589 the_locking_inset->ToggleInsetCursor(bv);
590 UpdateLocal(bv, false);
591 the_locking_inset->ToggleInsetCursor(bv);
593 } else if (result == FINISHED) {
597 cursor.pos(inset_pos + 1);
598 resetPos(bv->painter());
604 the_locking_inset = 0;
612 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
613 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
614 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
615 cutSelection(bv->buffer());
616 cursor = selection_start_cursor;
617 par->InsertChar(cursor.pos(), arg[0]);
618 SetCharFont(bv->buffer(), cursor.pos(), current_font);
619 cursor.pos(cursor.pos() + 1);
620 selection_start_cursor = selection_end_cursor = cursor;
621 UpdateLocal(bv, true);
623 // --- Cursor Movements ---------------------------------------------
625 bv->text->FinishUndo();
626 moveRight(bv, false);
627 selection_end_cursor = cursor;
628 UpdateLocal(bv, false);
631 bv->text->FinishUndo();
632 result = moveRight(bv);
633 if (hasSelection()) {
634 selection_start_cursor = selection_end_cursor = cursor;
635 UpdateLocal(bv, false);
637 selection_start_cursor = selection_end_cursor = cursor;
641 bv->text->FinishUndo();
643 selection_end_cursor = cursor;
644 UpdateLocal(bv, false);
647 bv->text->FinishUndo();
648 result= moveLeft(bv);
649 if (hasSelection()) {
650 selection_start_cursor = selection_end_cursor = cursor;
651 UpdateLocal(bv, false);
653 selection_start_cursor = selection_end_cursor = cursor;
657 bv->text->FinishUndo();
659 selection_end_cursor = cursor;
660 UpdateLocal(bv, false);
663 bv->text->FinishUndo();
664 result = moveDown(bv);
665 if (hasSelection()) {
666 selection_start_cursor = selection_end_cursor = cursor;
667 UpdateLocal(bv, false);
669 selection_start_cursor = selection_end_cursor = cursor;
673 bv->text->FinishUndo();
675 selection_end_cursor = cursor;
676 UpdateLocal(bv, false);
679 bv->text->FinishUndo();
681 if (hasSelection()) {
682 selection_start_cursor = selection_end_cursor = cursor;
683 UpdateLocal(bv, false);
685 selection_start_cursor = selection_end_cursor = cursor;
690 if (hasSelection()) {
691 selection_start_cursor = selection_end_cursor = cursor;
692 UpdateLocal(bv, false);
699 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
700 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
701 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
703 if (hasSelection()) {
704 LyXParagraph::size_type i = selection_start_cursor.pos();
705 for (; i < selection_end_cursor.pos(); ++i) {
706 par->Erase(selection_start_cursor.pos());
710 if (ret) { // we need update
711 selection_start_cursor = selection_end_cursor = cursor;
712 UpdateLocal(bv, true);
713 } else if (hasSelection()) {
714 selection_start_cursor = selection_end_cursor = cursor;
715 UpdateLocal(bv, false);
718 resetPos(bv->painter());
721 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
722 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
723 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
725 if (cutSelection(bv->buffer())) {
727 cursor = selection_end_cursor = selection_start_cursor;
728 UpdateLocal(bv, true);
729 } else if (hasSelection()) {
730 selection_start_cursor = selection_end_cursor = cursor;
731 UpdateLocal(bv, false);
733 resetPos(bv->painter());
736 bv->text->FinishUndo();
737 if (copySelection(bv->buffer())) {
739 selection_start_cursor = selection_end_cursor = cursor;
740 UpdateLocal(bv, true);
741 } else if (hasSelection()) {
742 selection_start_cursor = selection_end_cursor = cursor;
743 UpdateLocal(bv, false);
748 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
749 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
750 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
751 if (pasteSelection(bv->buffer())) {
752 selection_start_cursor = selection_end_cursor = cursor;
753 UpdateLocal(bv, true);
756 resetPos(bv->painter());
759 bv->text->FinishUndo();
760 for(; cursor.pos() > rows[actrow].pos;) {
761 cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
762 cursor.pos(cursor.pos() - 1);
765 cursor.x(cursor.x() - SingleWidth(bv->painter(), par, cursor.pos()));
766 if (hasSelection()) {
767 selection_start_cursor = selection_end_cursor = cursor;
768 UpdateLocal(bv, false);
770 selection_start_cursor = selection_end_cursor = cursor;
772 resetPos(bv->painter());
776 bv->text->FinishUndo();
777 int checkpos = (int)rows[actrow + 1].pos;
778 if ((actrow + 2) < (int)rows.size())
780 for(; cursor.pos() < checkpos;) {
781 cursor.x(cursor.x() + SingleWidth(bv->painter(), par, cursor.pos()));
782 cursor.pos(cursor.pos() + 1);
784 if (hasSelection()) {
785 selection_start_cursor = selection_end_cursor = cursor;
786 UpdateLocal(bv, false);
788 selection_start_cursor = selection_end_cursor = cursor;
791 resetPos(bv->painter());
793 case LFUN_BREAKPARAGRAPH:
797 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
798 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
799 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
800 par->InsertChar(cursor.pos(),LyXParagraph::META_NEWLINE);
801 SetCharFont(bv->buffer(), cursor.pos(),current_font);
802 UpdateLocal(bv, true);
803 cursor.pos(cursor.pos() + 1);
804 selection_start_cursor = selection_end_cursor = cursor;
805 resetPos(bv->painter());
809 static LyXTextClass::size_type cur_layout = par->layout;
811 // Derive layout number from given argument (string)
812 // and current buffer's textclass (number). */
813 LyXTextClassList::ClassList::size_type tclass =
814 bv->buffer()->params.textclass;
815 std::pair <bool, LyXTextClass::size_type> layout =
816 textclasslist.NumberOfLayout(tclass, arg);
818 // If the entry is obsolete, use the new one instead.
820 string obs = textclasslist.Style(tclass,layout.second).
823 layout = textclasslist.NumberOfLayout(tclass, obs);
826 // see if we found the layout number:
828 string msg = string(N_("Layout ")) + arg + N_(" not known");
830 bv->owner()->getMiniBuffer()->Set(msg);
834 if (cur_layout != layout.second) {
835 cur_layout = layout.second;
836 text->SetLayout(bv, layout.second);
837 bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
838 UpdateLocal(bv, true);
843 result = UNDISPATCHED;
846 if (result != FINISHED) {
849 bv->unlockInset(this);
854 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
857 buf->latexParagraphs(os, par, 0, texrow);
858 return texrow.rows();
862 void InsetText::Validate(LaTeXFeatures & features) const
864 par->validate(features);
868 // Returns the width of a character at a certain spot
869 int InsetText::SingleWidth(Painter & pain, LyXParagraph * p, int pos) const
871 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
872 char c = p->GetChar(pos);
874 if (IsPrintable(c)) {
875 return lyxfont::width(c, font);
876 } else if (c == LyXParagraph::META_INSET) {
877 Inset const * tmpinset = p->GetInset(pos);
879 return tmpinset->width(pain, font);
882 } else if (IsSeparatorChar(c))
884 else if (IsNewlineChar(c))
886 return lyxfont::width(c, font);
890 // Returns the width of a character at a certain spot
891 void InsetText::SingleHeight(Painter & pain, LyXParagraph * p,int pos,
892 int & asc, int & desc) const
894 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
895 char c = p->GetChar(pos);
898 if (c == LyXParagraph::META_INSET) {
899 Inset const * tmpinset=p->GetInset(pos);
901 asc = tmpinset->ascent(pain, font);
902 desc = tmpinset->descent(pain, font);
905 asc = lyxfont::maxAscent(font);
906 desc = lyxfont::maxDescent(font);
912 // Gets the fully instantiated font at a given position in a paragraph
913 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
914 // The difference is that this one is used for displaying, and thus we
915 // are allowed to make cosmetic improvements. For instance make footnotes
917 // If position is -1, we get the layout font of the paragraph.
918 // If position is -2, we get the font of the manual label of the paragraph.
919 LyXFont InsetText::GetFont(Buffer const * buf, LyXParagraph * p, int pos) const
921 char par_depth = p->GetDepth();
923 LyXLayout const & layout =
924 textclasslist.Style(buf->params.textclass, p->GetLayout());
926 // We specialize the 95% common case:
927 if (p->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
930 if (layout.labeltype == LABEL_MANUAL
931 && pos < BeginningOfMainBody(buf, p)) {
933 return p->GetFontSettings(buf->params,
934 pos).realize(layout.reslabelfont);
936 return p->GetFontSettings(buf->params,
937 pos).realize(layout.resfont);
940 // process layoutfont for pos == -1 and labelfont for pos < -1
942 return layout.resfont;
944 return layout.reslabelfont;
947 // The uncommon case need not be optimized as much
949 LyXFont layoutfont, tmpfont;
953 if (pos < BeginningOfMainBody(buf, p)) {
955 layoutfont = layout.labelfont;
958 layoutfont = layout.font;
960 tmpfont = p->GetFontSettings(buf->params, pos);
961 tmpfont.realize(layoutfont);
964 // process layoutfont for pos == -1 and labelfont for pos < -1
966 tmpfont = layout.font;
968 tmpfont = layout.labelfont;
971 // Resolve against environment font information
972 //if (par->GetDepth()){ // already in while condition
973 while (p && par_depth && !tmpfont.resolved()) {
974 p = p->DepthHook(par_depth - 1);
976 tmpfont.realize(textclasslist.Style(buf->params.textclass,
977 p->GetLayout()).font);
978 par_depth = p->GetDepth();
981 tmpfont.realize((textclasslist.TextClass(buf->params.textclass).
987 // the font for drawing may be different from the real font
988 LyXFont InsetText::GetDrawFont(Buffer const * buf, LyXParagraph * p, int pos) const
990 return GetFont(buf, p, pos);
994 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
996 if (textclasslist.Style(buf->params.textclass,
997 p->GetLayout()).labeltype != LABEL_MANUAL)
1000 return p->BeginningOfMainBody();
1004 void InsetText::GetCursorPos(int & x, int & y) const
1011 int InsetText::InsetInInsetY()
1013 if (!the_locking_inset)
1016 return (inset_y + the_locking_inset->InsetInInsetY());
1020 void InsetText::ToggleInsetCursor(BufferView * bv)
1022 if (the_locking_inset) {
1023 the_locking_inset->ToggleInsetCursor(bv);
1027 LyXFont font = GetDrawFont(bv->buffer(), par, text->cursor.pos());
1029 int asc = lyxfont::maxAscent(font);
1030 int desc = lyxfont::maxDescent(font);
1032 asc = text->cursor.row()->ascent_of_text();
1033 desc = text->cursor.row()->height() - asc;
1035 bv->hideLockedInsetCursor();
1037 bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(),
1039 cursor_visible = !cursor_visible;
1043 void InsetText::ShowInsetCursor(BufferView * bv)
1045 if (the_locking_inset) {
1046 the_locking_inset->ShowInsetCursor(bv);
1049 if (!cursor_visible) {
1050 LyXFont font = GetDrawFont(bv->buffer(), par, cursor.pos());
1052 int asc = lyxfont::maxAscent(font);
1053 int desc = lyxfont::maxDescent(font);
1054 asc = text->cursor.row()->ascent_of_text();
1055 desc = text->cursor.row()->height() - asc;
1056 bv->fitLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1057 bv->showLockedInsetCursor(text->cursor.x(), text->cursor.y(), asc, desc);
1058 cursor_visible = true;
1063 void InsetText::HideInsetCursor(BufferView * bv)
1065 if (cursor_visible) {
1066 bv->hideLockedInsetCursor();
1067 cursor_visible = false;
1069 if (the_locking_inset)
1070 the_locking_inset->HideInsetCursor(bv);
1074 void InsetText::setPos(Painter & pain, int x, int y) const
1076 x -= drawTextXOffset;
1077 y -= drawTextYOffset;
1078 // search right X-pos x==0 -> top_x
1081 cursor.y(top_baseline);
1083 for(unsigned int i = 1;
1084 (long(cursor.y() + rows[i - 1].desc) < y)
1085 && (i < rows.size() - 1); ++i) {
1086 cursor.y(rows[i].baseline);
1087 cursor.pos(rows[i].pos);
1090 cursor.y(cursor.y() - top_baseline);
1091 cursor.x(top_x + 2); // 2 = frame width
1095 int sw = swh = SingleWidth(pain, par, cursor.pos());
1096 if (par->GetChar(cursor.pos()) != LyXParagraph::META_INSET)
1098 int checkpos = rows[actrow + 1].pos;
1099 if ((actrow + 2) < (int)rows.size())
1101 while ((cursor.pos() < checkpos) && ((cursor.x() + swh) < x)) {
1102 cursor.x(cursor.x() + sw);
1103 cursor.pos(cursor.pos() + 1);
1104 sw = swh = SingleWidth(pain, par, cursor.pos());
1105 if (par->GetChar(cursor.pos())!=LyXParagraph::META_INSET)
1111 void InsetText::resetPos(Painter & pain) const
1118 int old_pos = cursor.pos();
1120 cursor.y(top_baseline);
1122 for(unsigned int i = 0;
1123 i < (rows.size() - 1) && rows[i].pos <= cursor.pos();
1125 cursor.y(rows[i].baseline);
1128 cursor.y(cursor.y() - top_baseline);
1129 setPos(pain, 0, cursor.y());
1130 cursor.x(top_x + 2); // 2 = frame width
1131 while(cursor.pos() < old_pos) {
1132 cursor.x(cursor.x() + SingleWidth(pain, par,cursor.pos()));
1133 cursor.pos(cursor.pos() + 1);
1138 UpdatableInset::RESULT
1139 InsetText::moveRight(BufferView * bv, bool activate_inset)
1141 if (cursor.pos() >= par->Last())
1143 if (activate_inset && checkAndActivateInset(bv)) {
1146 cursor.pos(cursor.pos() + 1);
1147 resetPos(bv->painter());
1148 real_current_font = current_font =GetFont(bv->buffer(), par, cursor.pos());
1149 return DISPATCHED_NOUPDATE;
1153 UpdatableInset::RESULT
1154 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1156 if (cursor.pos() <= 0)
1158 cursor.pos(cursor.pos() - 1);
1159 resetPos(bv->painter());
1161 if (checkAndActivateInset(bv, -1, -1))
1163 return DISPATCHED_NOUPDATE;
1167 UpdatableInset::RESULT
1168 InsetText::moveUp(BufferView * bv)
1172 cursor.y(rows[actrow - 1].baseline - top_baseline);
1173 if (cursor.x_fix() < 0)
1174 cursor.x_fix(cursor.x());
1175 setPos(bv->painter(),
1176 cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1177 return DISPATCHED_NOUPDATE;
1181 UpdatableInset::RESULT
1182 InsetText::moveDown(BufferView * bv)
1184 if (actrow >= int(rows.size() - 2))
1186 cursor.y(rows[actrow + 1].baseline - top_baseline);
1187 if (cursor.x_fix() < 0)
1188 cursor.x_fix(cursor.x());
1189 setPos(bv->painter(),
1190 cursor.x_fix() - top_x + drawTextXOffset, cursor.y());
1191 return DISPATCHED_NOUPDATE;
1195 bool InsetText::Delete()
1197 if ((par->GetChar(cursor.pos())==LyXParagraph::META_INSET) &&
1198 !par->GetInset(cursor.pos())->Deletable()) {
1201 par->Erase(cursor.pos());
1206 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1208 if (the_locking_inset) {
1209 if (the_locking_inset->InsertInsetAllowed(inset))
1210 return the_locking_inset->InsertInset(bv, inset);
1213 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1214 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1215 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1216 if (inset->Editable() == Inset::IS_EDITABLE) {
1217 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1218 i->setOwner(static_cast<UpdatableInset *>(this));
1220 par->InsertChar(cursor.pos(), LyXParagraph::META_INSET);
1221 par->InsertInset(cursor.pos(), inset);
1222 if (hasSelection()) {
1223 selection_start_cursor = selection_end_cursor = cursor;
1225 selection_start_cursor = selection_end_cursor = cursor;
1227 UpdateLocal(bv, true);
1228 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1233 UpdatableInset * InsetText::GetLockingInset()
1235 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1239 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1243 if (the_locking_inset)
1244 return the_locking_inset->GetFirstLockingInsetOfType(c);
1249 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1251 // if there is no selection just set the current_font
1252 if (!hasSelection()) {
1253 // Determine basis font
1255 if (cursor.pos() < BeginningOfMainBody(bv->buffer(), par))
1256 layoutfont = GetFont(bv->buffer(), par, -2);
1258 layoutfont = GetFont(bv->buffer(), par, -1);
1260 // Update current font
1261 real_current_font.update(font, bv->buffer()->params.language_info,
1264 // Reduce to implicit settings
1265 current_font = real_current_font;
1266 current_font.reduce(layoutfont);
1267 // And resolve it completely
1268 real_current_font.realize(layoutfont);
1273 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1274 s_start = selection_end_cursor.pos();
1275 s_end = selection_start_cursor.pos();
1277 s_start = selection_start_cursor.pos();
1278 s_end = selection_end_cursor.pos();
1281 while(s_start < s_end) {
1282 newfont = GetFont(bv->buffer(), par,s_start);
1283 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1284 SetCharFont(bv->buffer(), s_start, newfont);
1287 UpdateLocal(bv, true);
1291 void InsetText::SetCharFont(Buffer const * buf, int pos, LyXFont const & f) //
1293 /* let the insets convert their font */
1296 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1297 if (par->GetInset(pos))
1298 font = par->GetInset(pos)->ConvertFont(font);
1300 LyXLayout const & layout =
1301 textclasslist.Style(buf->params.textclass,par->GetLayout());
1303 // Get concrete layout font to reduce against
1306 if (pos < BeginningOfMainBody(buf, par))
1307 layoutfont = layout.labelfont;
1309 layoutfont = layout.font;
1312 layoutfont.realize((textclasslist.TextClass(buf->params.textclass).
1315 // Now, reduce font against full layout font
1316 font.reduce(layoutfont);
1318 par->SetFont(pos, font);
1322 void InsetText::computeTextRows(Painter & pain) const
1336 int width = wordAscent = wordDescent = 0;
1337 insetWidth = maxAscent = maxDescent = 0;
1342 rows.push_back(row);
1343 if (!autoBreakRows || (getMaxTextWidth(pain, this) < 0)) {
1344 for(p = 0; p < par->Last(); ++p) {
1345 insetWidth += SingleWidth(pain, par, p);
1346 SingleHeight(pain, par, p, asc, desc);
1347 maxAscent = max(maxAscent, asc);
1348 maxDescent = max(maxDescent, desc);
1350 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1351 rows[0].asc = maxAscent;
1352 rows[0].desc = maxDescent;
1353 // alocate a dummy row for the endpos
1354 row.pos = par->Last();
1355 rows.push_back(row);
1359 bool is_first_word_in_row = true;
1360 int cw, lastWordWidth = 0;
1361 int maxWidth = getMaxTextWidth(pain, this);
1362 // if we auto break rows than the insetwidth should be always the max
1363 // width as the display is stable it may get larger if we have a really
1364 // large word below and we draw it!!!
1365 insetWidth = maxWidth;
1367 for(p = 0; p < par->Last(); ++p) {
1368 if (par->IsNewline(p)) {
1369 rows.back().asc = wordAscent;
1370 rows.back().desc = wordDescent;
1372 rows.push_back(row);
1374 width = lastWordWidth = 0;
1375 oasc = odesc = wordAscent = wordDescent = 0;
1376 is_first_word_in_row = true;
1379 cw = SingleWidth(pain, par, p);
1381 if (par->GetChar(p) == LyXParagraph::META_INSET)
1382 inset = par->GetInset(p);
1383 if (inset && inset->display()) {
1384 inset->setOwner(const_cast<InsetText *>(this));
1385 if (cw > insetWidth)
1387 if (!is_first_word_in_row || (p != nwp)) {
1388 oasc = max(oasc, wordAscent);
1389 odesc = max(odesc, wordDescent);
1390 rows.back().asc = oasc;
1391 rows.back().desc = odesc;
1393 rows.push_back(row);
1395 SingleHeight(pain, par, p, asc, desc);
1396 rows.back().asc = asc;
1397 rows.back().desc = desc;
1398 row.pos = nwp = p + 1;
1399 rows.push_back(row);
1400 width = lastWordWidth = 0;
1401 oasc = odesc = wordAscent = wordDescent = 0;
1402 is_first_word_in_row = true;
1405 SingleHeight(pain, par, p, asc, desc);
1407 lastWordWidth += cw;
1408 if (width > maxWidth) {
1409 if (is_first_word_in_row) {
1410 if (!(width-cw)) { // only this character in word
1411 rows.back().asc = asc;
1412 rows.back().desc = desc;
1414 rows.push_back(row);
1420 lastWordWidth = width = 0;
1422 rows.back().asc = wordAscent;
1423 rows.back().desc = wordDescent;
1425 rows.push_back(row);
1430 lastWordWidth = width = cw;
1434 rows.back().asc = oasc;
1435 rows.back().desc = odesc;
1437 rows.push_back(row);
1439 odesc = wordDescent;
1440 width = lastWordWidth;
1441 wordAscent = max(wordAscent, asc);
1442 wordDescent = max(wordDescent, desc);
1443 is_first_word_in_row = true;
1446 wordAscent = max(wordAscent, asc);
1447 wordDescent = max(wordDescent, desc);
1449 if (par->IsSeparator(p) || inset) {
1451 inset->setOwner(const_cast<InsetText *>(this));
1455 oasc = max(oasc, wordAscent);
1456 odesc = max(odesc, wordDescent);
1457 wordAscent = wordDescent = lastWordWidth = 0;
1459 is_first_word_in_row = false;
1462 // if we have some data in the paragraph we have ascent/descent
1464 // assign last row data
1465 rows.back().asc = max(oasc, wordAscent);
1466 rows.back().desc = max(odesc, wordDescent);
1468 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1469 // alocate a dummy row for the endpos
1470 row.pos = par->Last();
1471 rows.push_back(row);
1472 // calculate maxAscent/Descent
1473 maxAscent = rows[0].asc;
1474 maxDescent = rows[0].desc;
1475 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1476 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1481 void InsetText::computeBaselines(int baseline) const
1483 rows[0].baseline = baseline;
1484 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1485 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1486 rows[i].asc + interline_space;
1491 void InsetText::UpdateLocal(BufferView * bv, bool flag)
1494 text->FullRebreak(bv);
1495 computeTextRows(bv->painter());
1496 computeBaselines(top_baseline);
1498 bv->updateInset(this, flag);
1500 resetPos(bv->painter());
1501 bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
1505 bool InsetText::cutSelection(Buffer const * buf)
1507 if (!hasSelection())
1512 LyXParagraph * endpar = par;
1514 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1515 start = selection_end_cursor.pos();
1516 end = selection_start_cursor.pos();
1518 start = selection_start_cursor.pos();
1519 end = selection_end_cursor.pos();
1522 return cap.cutSelection(par, &endpar, start, end, buf->params.textclass);
1526 bool InsetText::copySelection(Buffer const * buf)
1528 if (!hasSelection())
1534 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1535 start = selection_end_cursor.pos();
1536 end = selection_start_cursor.pos();
1538 start = selection_start_cursor.pos();
1539 end = selection_end_cursor.pos();
1541 return cap.copySelection(par, par, start, end, buf->params.textclass);
1545 bool InsetText::pasteSelection(Buffer const * buf)
1549 if (cap.nrOfParagraphs() > 1) {
1550 WriteAlert(_("Impossible operation"),
1551 _("Cannot include more than one paragraph!"),
1555 LyXParagraph * endpar;
1556 LyXParagraph * actpar = par;
1558 int pos = cursor.pos();
1559 bool ret = cap.pasteSelection(&actpar, &endpar, pos,buf->params.textclass);
1565 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1568 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
1569 UpdatableInset * inset =
1570 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
1571 LyXFont font = GetFont(bv->buffer(), par, cursor.pos());
1573 x = inset->width(bv->painter(), font);
1575 y = inset->descent(bv->painter(), font);
1576 inset_x = cursor.x() - top_x + drawTextXOffset;
1577 inset_y = cursor.y() + drawTextYOffset;
1578 inset->Edit(bv, x - inset_x, y - inset_y, button);
1579 if (!the_locking_inset)
1581 UpdateLocal(bv, true);
1588 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset) const
1590 // int w=getMaxWidth(pain, inset);
1592 return getMaxWidth(pain, inset) - 4; // 2+2 width of eventual border
1595 void InsetText::SetParagraphData(LyXParagraph *p)
1600 par->SetInsetOwner(this);
1604 void InsetText::SetAutoBreakRows(bool flag)
1606 if (flag != autoBreakRows) {
1607 autoBreakRows = flag;
1612 void InsetText::SetDrawLockedFrame(bool flag)
1614 if (flag != drawLockedFrame) {
1615 drawLockedFrame = flag;
1620 void InsetText::SetFrameColor(LColor::color col)
1622 if (frame_color != col) {