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"
48 #include "trans_mgr.h"
56 extern unsigned char getCurrentTextClass(Buffer *);
57 #warning BAAAAAAAADDDDDDD current_view (but Lars wanted it :) !!!
58 extern BufferView * current_view;
61 InsetText::InsetText()
63 par = new LyXParagraph();
65 text = new LyXText(this);
69 InsetText::InsetText(InsetText const & ins) : UpdatableInset()
73 text = new LyXText(this);
74 autoBreakRows = ins.autoBreakRows;
78 InsetText & InsetText::operator=(InsetText const & it)
81 text = new LyXText(this);
82 autoBreakRows = it.autoBreakRows;
86 void InsetText::init(InsetText const * ins)
88 the_locking_inset = 0;
89 cursor_visible = false;
94 maxAscent = maxDescent = insetWidth = 0;
95 drawTextXOffset = drawTextYOffset = 0;
96 autoBreakRows = drawLockedFrame = false;
99 SetParagraphData(ins->par);
100 autoBreakRows = ins->autoBreakRows;
101 drawLockedFrame = ins->drawLockedFrame;
103 par->SetInsetOwner(this);
106 selection_start_cursor = selection_end_cursor = cursor;
107 frame_color = LColor::insetframe;
113 InsetText::~InsetText()
119 Inset * InsetText::Clone() const
121 InsetText * t = new InsetText(*this);
126 void InsetText::Write(Buffer const * buf, ostream & os) const
129 WriteParagraphData(buf, os);
133 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
135 par->writeFile(buf, os, buf->params, 0, 0);
139 void InsetText::Read(Buffer const * buf, LyXLex & lex)
141 string token, tmptok;
143 LyXParagraph * return_par = 0;
144 char depth = 0; // signed or unsigned?
145 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
146 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
147 LyXFont font(LyXFont::ALL_INHERIT);
150 par = new LyXParagraph;
151 par->SetInsetOwner(this);
155 token = lex.GetString();
158 if (token == "\\end_inset")
160 if (const_cast<Buffer*>(buf)->parseSingleLyXformat2Token(lex, par, return_par,
164 // the_end read this should NEVER happen
165 lex.printError("\\the_end read in inset! Error in document!");
172 if (token != "\\end_inset") {
173 lex.printError("Missing \\end_inset at this point. "
180 int InsetText::ascent(Painter &, LyXFont const &) const
183 Row * row = text->GetRowNearY(y_temp);
184 return row->ascent_of_text() + 2;
188 int InsetText::descent(Painter &, LyXFont const &) const
191 Row * row = text->GetRowNearY(y);
192 return text->height - row->ascent_of_text() + 2;
196 int InsetText::width(Painter & pain, LyXFont const &) const
198 return std::max(static_cast<long int>(getMaxTextWidth(pain, this)),
203 void InsetText::draw(Painter & pain, LyXFont const & f,
204 int baseline, float & x) const
207 UpdatableInset::draw(pain, f, baseline, x);
211 text->init(current_view);
212 computeTextRows(pain);
216 if ((baseline != top_baseline) || (top_x != int(x))) {
217 top_baseline = baseline;
219 computeBaselines(baseline);
221 if (the_locking_inset && (text->cursor.pos() == inset_pos)) {
223 inset_x = cx() - top_x + drawTextXOffset;
224 inset_y = cy() + drawTextYOffset;
226 if (drawLockedFrame && locked) {
227 pain.rectangle(int(x), baseline - ascent(pain, f), width(pain, f),
228 ascent(pain,f) + descent(pain, f), frame_color);
230 x += TEXT_TO_INSET_OFFSET; // place for border
233 Row * row = text->GetRowNearY(y);
234 y += baseline - row->ascent_of_text() + 1;
236 text->GetVisibleRow(current_view, y, x, row, y);
241 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
242 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
243 rows[r].baseline, x);
244 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
247 x += insetWidth - TEXT_TO_INSET_OFFSET;
251 void InsetText::update(BufferView * bv, LyXFont const &) const
257 // Dump all rowinformation:
259 Row * tmprow = text->GetRowNearY(y_dummy);
260 lyxerr << "Width = " << text->width << endl;
261 lyxerr << "Baseline Paragraph Pos Height Ascent Fill\n";
263 lyxerr << tmprow->baseline() << '\t'
264 << tmprow->par() << '\t'
265 << tmprow->pos() << '\t'
266 << tmprow->height() << '\t'
267 << tmprow->ascent_of_text() << '\t'
268 << tmprow->fill() << '\n';
269 tmprow = tmprow->next();
277 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
278 int row, int baseline, float x) const
284 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
285 s_start = selection_end_cursor.pos();
286 s_end = selection_start_cursor.pos();
288 s_start = selection_start_cursor.pos();
289 s_end = selection_end_cursor.pos();
291 if ((s_start > endpos) || (s_end < startpos))
295 int ssel_x = esel_x = int(x);
298 for(; p < endpos; ++p) {
301 if ((p >= s_start) && (p <= s_end))
303 char ch = par->GetChar(p);
304 font = GetDrawFont(current_view->buffer(), par, p);
305 if (IsFloatChar(ch)) {
307 } else if (ch == LyXParagraph::META_INSET) {
308 Inset const * tmpinset = par->GetInset(p);
309 x += tmpinset->width(pain, font);
311 x += lyxfont::width(ch, font);
316 if ((p >= s_start) && (p <= s_end))
318 if (ssel_x < esel_x) {
319 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
320 int(esel_x - ssel_x),
321 rows[row].asc + rows[row].desc,
327 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
328 int baseline, float x) const
330 Assert(endpos <= par->Last());
332 for(int p = startpos; p < endpos; ++p) {
333 char ch = par->GetChar(p);
334 LyXFont font = GetDrawFont(current_view->buffer(), par, p);
335 if (IsFloatChar(ch)) {
337 } else if (par->IsNewline(p)) {
338 // Draw end-of-line marker
339 int wid = lyxfont::width('n', font);
340 int asc = lyxfont::maxAscent(font);
344 xp[0] = int(x + wid * 0.375);
345 yp[0] = int(y - 0.875 * asc * 0.75);
348 yp[1] = int(y - 0.500 * asc * 0.75);
350 xp[2] = int(x + wid * 0.375);
351 yp[2] = int(y - 0.125 * asc * 0.75);
353 pain.lines(xp, yp, 3, LColor::eolmarker);
356 yp[0] = int(y - 0.500 * asc * 0.75);
358 xp[1] = int(x + wid);
359 yp[1] = int(y - 0.500 * asc * 0.75);
361 xp[2] = int(x + wid);
362 yp[2] = int(y - asc * 0.75);
364 pain.lines(xp, yp, 3, LColor::eolmarker);
366 } else if (ch == LyXParagraph::META_INSET) {
367 Inset * tmpinset = par->GetInset(p);
369 tmpinset->draw(pain, font, baseline, x);
371 pain.text(int(x), baseline, ch, font);
372 x += lyxfont::width(ch, font);
378 char const * InsetText::EditMessage() const
380 return _("Opened Text Inset");
384 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
386 par->SetInsetOwner(this);
387 UpdatableInset::Edit(bv, x, y, button);
389 if (!bv->lockInset(this)) {
390 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
394 the_locking_inset = 0;
395 inset_pos = inset_x = inset_y = 0;
396 setPos(bv->painter(), x, y);
397 checkAndActivateInset(bv, x, y, button);
398 selection_start_cursor = selection_end_cursor = cursor;
399 text->sel_cursor = text->cursor;
400 bv->text->FinishUndo();
401 UpdateLocal(bv, true, false);
405 void InsetText::InsetUnlock(BufferView * bv)
407 if (the_locking_inset) {
408 the_locking_inset->InsetUnlock(bv);
409 the_locking_inset = 0;
412 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
414 selection_start_cursor = selection_end_cursor = cursor;
415 no_selection = false;
417 UpdateLocal(bv, true, false);
421 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
423 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
426 if (inset == par->GetInset(text->cursor.pos())) {
427 lyxerr[Debug::INSETS] << "OK" << endl;
428 the_locking_inset = inset;
429 resetPos(bv->painter());
430 inset_x = cx() - top_x + drawTextXOffset;
431 inset_y = cy() + drawTextYOffset;
432 inset_pos = text->cursor.pos();
434 } else if (the_locking_inset && (the_locking_inset == inset)) {
435 if (text->cursor.pos() == inset_pos) {
436 lyxerr[Debug::INSETS] << "OK" << endl;
437 resetPos(bv->painter());
438 inset_x = cx() - top_x + drawTextXOffset;
439 inset_y = cy() + drawTextYOffset;
441 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
443 } else if (the_locking_inset) {
444 lyxerr[Debug::INSETS] << "MAYBE" << endl;
445 return the_locking_inset->LockInsetInInset(bv, inset);
447 lyxerr[Debug::INSETS] << "NOT OK" << endl;
452 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
455 if (!the_locking_inset)
457 if (the_locking_inset == inset) {
458 the_locking_inset->InsetUnlock(bv);
459 the_locking_inset = 0;
461 moveRight(bv, false);
464 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
468 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
470 if (!the_locking_inset)
472 if (the_locking_inset != inset)
473 return the_locking_inset->UpdateInsetInInset(bv, inset);
474 lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
476 UpdateLocal(bv, true, false);
477 if (text->cursor.pos() == inset_pos) {
478 inset_x = cx() - top_x + drawTextXOffset;
479 inset_y = cy() + drawTextYOffset;
485 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
487 if (hasSelection()) {
488 selection_start_cursor = selection_end_cursor = cursor;
489 UpdateLocal(bv, false, false);
491 no_selection = false;
492 setPos(bv->painter(), x, y);
494 if (the_locking_inset) {
495 UpdatableInset * inset = 0;
496 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET)
497 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
498 if (the_locking_inset == inset) {
499 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
502 // otherwise unlock the_locking_inset and lock the new inset
503 the_locking_inset->InsetUnlock(bv);
504 inset_x = cursor.x() - top_x + drawTextXOffset;
505 inset_y = cursor.y() + drawTextYOffset;
506 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
507 inset->Edit(bv, x - inset_x, y - inset_y, button);
508 UpdateLocal(bv, true, false);
511 // otherwise only unlock the_locking_inset
512 the_locking_inset->InsetUnlock(bv);
513 the_locking_inset = 0;
515 if (bv->the_locking_inset) {
516 if ((par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) &&
517 par->GetInset(cursor.pos()) &&
518 (par->GetInset(cursor.pos())->Editable() == Inset::HIGHLY_EDITABLE)) {
519 UpdatableInset * inset =
520 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
521 inset_x = cursor.x() - top_x + drawTextXOffset;
522 inset_y = cursor.y() + drawTextYOffset;
523 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
524 inset->Edit(bv, x - inset_x, y - inset_y, 0);
525 UpdateLocal(bv, true, false);
528 selection_start_cursor = selection_end_cursor = cursor;
532 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
534 UpdatableInset * inset = 0;
536 if (the_locking_inset) {
537 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
539 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
540 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
541 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
542 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
544 inset_x = cursor.x() - top_x + drawTextXOffset;
545 inset_y = cursor.y() + drawTextYOffset;
546 inset->InsetButtonRelease(bv, x - inset_x, y - inset_y,button);
547 inset->Edit(bv, x - inset_x, y - inset_y, button);
551 no_selection = false;
555 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
557 if (the_locking_inset) {
558 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
563 LyXCursor old = selection_end_cursor;
565 setPos(bv->painter(), x, y);
566 selection_end_cursor = cursor;
567 if (old != selection_end_cursor)
568 UpdateLocal(bv, false, false);
571 no_selection = false;
575 void InsetText::InsetKeyPress(XKeyEvent * xke)
577 if (the_locking_inset) {
578 the_locking_inset->InsetKeyPress(xke);
584 UpdatableInset::RESULT
585 InsetText::LocalDispatch(BufferView * bv,
586 int action, string const & arg)
588 no_selection = false;
589 UpdatableInset::RESULT
590 result= UpdatableInset::LocalDispatch(bv, action, arg);
591 if (result != UNDISPATCHED) {
592 resetPos(bv->painter());
597 if ((action < 0) && arg.empty())
600 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
601 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
603 if (the_locking_inset) {
604 result = the_locking_inset->LocalDispatch(bv, action, arg);
605 if (result == DISPATCHED_NOUPDATE)
607 else if (result == DISPATCHED) {
608 the_locking_inset->ToggleInsetCursor(bv);
609 UpdateLocal(bv, false, false);
610 the_locking_inset->ToggleInsetCursor(bv);
612 } else if (result == FINISHED) {
616 cursor.pos(inset_pos + 1);
617 resetPos(bv->painter());
623 the_locking_inset = 0;
631 if (bv->buffer()->isReadonly()) {
633 // setErrorMessage(N_("Document is read only"));
637 /* Automatically delete the currently selected
638 * text and replace it with what is being
639 * typed in now. Depends on lyxrc settings
640 * "auto_region_delete", which defaults to
643 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
644 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
645 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
646 if (lyxrc.auto_region_delete) {
647 if (text->selection){
648 text->CutSelection(bv, false);
651 text->ClearSelection();
652 for (string::size_type i = 0; i < arg.length(); ++i) {
653 bv->owner()->getIntl()->getTrans()->TranslateAndInsert(arg[i], text);
656 UpdateLocal(bv, true, true);
658 // --- Cursor Movements ---------------------------------------------
660 bv->text->FinishUndo();
661 moveRight(bv, false);
662 text->SetSelection();
663 UpdateLocal(bv, false, false);
666 bv->text->FinishUndo();
667 result = moveRight(bv);
668 text->sel_cursor = text->cursor;
669 UpdateLocal(bv, false, false);
672 bv->text->FinishUndo();
674 text->SetSelection();
675 UpdateLocal(bv, false, false);
678 bv->text->FinishUndo();
679 result= moveLeft(bv);
680 text->sel_cursor = text->cursor;
681 UpdateLocal(bv, false, false);
684 bv->text->FinishUndo();
686 selection_end_cursor = cursor;
687 UpdateLocal(bv, false, false);
690 bv->text->FinishUndo();
691 result = moveDown(bv);
692 text->sel_cursor = text->cursor;
693 UpdateLocal(bv, false, false);
696 bv->text->FinishUndo();
698 selection_end_cursor = cursor;
699 UpdateLocal(bv, false, false);
702 bv->text->FinishUndo();
704 text->sel_cursor = text->cursor;
705 UpdateLocal(bv, false, false);
708 bv->text->FinishUndo();
709 text->CursorHome(bv);
710 text->sel_cursor = text->cursor;
711 UpdateLocal(bv, false, false);
715 text->sel_cursor = text->cursor;
716 UpdateLocal(bv, false, false);
719 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
720 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
721 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
723 UpdateLocal(bv, true, true);
726 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
727 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
728 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
730 UpdateLocal(bv, true, true);
733 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
734 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
735 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
736 text->CutSelection(bv);
737 UpdateLocal(bv, true, true);
740 bv->text->FinishUndo();
741 text->CopySelection(bv);
742 UpdateLocal(bv, true, false);
745 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
746 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
747 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
748 text->PasteSelection(bv);
749 UpdateLocal(bv, true, true);
751 case LFUN_BREAKPARAGRAPH:
754 text->BreakParagraph(bv, 0);
755 UpdateLocal(bv, true, true);
760 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
761 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
762 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
763 text->InsertChar(bv, LyXParagraph::META_NEWLINE);
764 UpdateLocal(bv, true, true);
768 static LyXTextClass::size_type cur_layout = par->layout;
770 // Derive layout number from given argument (string)
771 // and current buffer's textclass (number). */
772 LyXTextClassList::ClassList::size_type tclass =
773 bv->buffer()->params.textclass;
774 std::pair <bool, LyXTextClass::size_type> layout =
775 textclasslist.NumberOfLayout(tclass, arg);
777 // If the entry is obsolete, use the new one instead.
779 string obs = textclasslist.Style(tclass,layout.second).
782 layout = textclasslist.NumberOfLayout(tclass, obs);
785 // see if we found the layout number:
787 string msg = string(N_("Layout ")) + arg + N_(" not known");
789 bv->owner()->getMiniBuffer()->Set(msg);
793 if (cur_layout != layout.second) {
794 cur_layout = layout.second;
795 text->SetLayout(bv, layout.second);
796 bv->owner()->getToolbar()->combox->select(cursor.par()->GetLayout()+1);
797 UpdateLocal(bv, true, true);
802 result = UNDISPATCHED;
805 if (result != FINISHED) {
808 bv->unlockInset(this);
813 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
816 buf->latexParagraphs(os, par, 0, texrow);
817 return texrow.rows();
821 void InsetText::Validate(LaTeXFeatures & features) const
823 par->validate(features);
827 // Returns the width of a character at a certain spot
828 int InsetText::SingleWidth(Painter & pain, LyXParagraph * p, int pos) const
830 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
831 char c = p->GetChar(pos);
833 if (IsPrintable(c)) {
834 return lyxfont::width(c, font);
835 } else if (c == LyXParagraph::META_INSET) {
836 Inset const * tmpinset = p->GetInset(pos);
838 return tmpinset->width(pain, font);
841 } else if (IsSeparatorChar(c))
843 else if (IsNewlineChar(c))
845 return lyxfont::width(c, font);
849 // Returns the width of a character at a certain spot
850 void InsetText::SingleHeight(Painter & pain, LyXParagraph * p,int pos,
851 int & asc, int & desc) const
853 LyXFont font = GetDrawFont(current_view->buffer(), p, pos);
854 char c = p->GetChar(pos);
857 if (c == LyXParagraph::META_INSET) {
858 Inset const * tmpinset=p->GetInset(pos);
860 asc = tmpinset->ascent(pain, font);
861 desc = tmpinset->descent(pain, font);
864 asc = lyxfont::maxAscent(font);
865 desc = lyxfont::maxDescent(font);
871 // Gets the fully instantiated font at a given position in a paragraph
872 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
873 // The difference is that this one is used for displaying, and thus we
874 // are allowed to make cosmetic improvements. For instance make footnotes
876 // If position is -1, we get the layout font of the paragraph.
877 // If position is -2, we get the font of the manual label of the paragraph.
878 LyXFont InsetText::GetFont(Buffer const * buf, LyXParagraph * p, int pos) const
880 char par_depth = p->GetDepth();
882 LyXLayout const & layout =
883 textclasslist.Style(buf->params.textclass, p->GetLayout());
885 // We specialize the 95% common case:
886 if (p->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
889 if (layout.labeltype == LABEL_MANUAL
890 && pos < BeginningOfMainBody(buf, p)) {
892 return p->GetFontSettings(buf->params,
893 pos).realize(layout.reslabelfont);
895 return p->GetFontSettings(buf->params,
896 pos).realize(layout.resfont);
899 // process layoutfont for pos == -1 and labelfont for pos < -1
901 return layout.resfont;
903 return layout.reslabelfont;
906 // The uncommon case need not be optimized as much
908 LyXFont layoutfont, tmpfont;
912 if (pos < BeginningOfMainBody(buf, p)) {
914 layoutfont = layout.labelfont;
917 layoutfont = layout.font;
919 tmpfont = p->GetFontSettings(buf->params, pos);
920 tmpfont.realize(layoutfont);
923 // process layoutfont for pos == -1 and labelfont for pos < -1
925 tmpfont = layout.font;
927 tmpfont = layout.labelfont;
930 // Resolve against environment font information
931 //if (par->GetDepth()){ // already in while condition
932 while (p && par_depth && !tmpfont.resolved()) {
933 p = p->DepthHook(par_depth - 1);
935 tmpfont.realize(textclasslist.Style(buf->params.textclass,
936 p->GetLayout()).font);
937 par_depth = p->GetDepth();
940 tmpfont.realize((textclasslist.TextClass(buf->params.textclass).
946 // the font for drawing may be different from the real font
947 LyXFont InsetText::GetDrawFont(Buffer const * buf, LyXParagraph * p, int pos) const
949 return text->GetFont(buf, p, pos);
953 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
955 if (textclasslist.Style(buf->params.textclass,
956 p->GetLayout()).labeltype != LABEL_MANUAL)
959 return p->BeginningOfMainBody();
963 void InsetText::GetCursorPos(int & x, int & y) const
970 int InsetText::InsetInInsetY()
972 if (!the_locking_inset)
975 return (inset_y + the_locking_inset->InsetInInsetY());
979 void InsetText::ToggleInsetCursor(BufferView * bv)
981 if (the_locking_inset) {
982 the_locking_inset->ToggleInsetCursor(bv);
986 LyXFont font = GetDrawFont(bv->buffer(), cpar(), cpos());
988 int asc = lyxfont::maxAscent(font);
989 int desc = lyxfont::maxDescent(font);
992 bv->hideLockedInsetCursor();
994 bv->showLockedInsetCursor(cx(), cy(),
996 cursor_visible = !cursor_visible;
1000 void InsetText::ShowInsetCursor(BufferView * bv)
1002 if (the_locking_inset) {
1003 the_locking_inset->ShowInsetCursor(bv);
1006 if (!cursor_visible) {
1007 LyXFont font = GetDrawFont(bv->buffer(), cpar(), cpos());
1009 int asc = lyxfont::maxAscent(font);
1010 int desc = lyxfont::maxDescent(font);
1012 bv->fitLockedInsetCursor(cx(), cy(), asc, desc);
1013 bv->showLockedInsetCursor(cx(), cy(), asc, desc);
1014 cursor_visible = true;
1019 void InsetText::HideInsetCursor(BufferView * bv)
1021 if (cursor_visible) {
1022 bv->hideLockedInsetCursor();
1023 cursor_visible = false;
1025 if (the_locking_inset)
1026 the_locking_inset->HideInsetCursor(bv);
1030 void InsetText::setPos(Painter & pain, int x, int y) const
1032 x -= drawTextXOffset;
1033 y -= drawTextYOffset;
1034 // search right X-pos x==0 -> top_x
1037 cursor.y(top_baseline);
1039 for(unsigned int i = 1;
1040 (long(cursor.y() + rows[i - 1].desc) < y)
1041 && (i < rows.size() - 1); ++i) {
1042 cursor.y(rows[i].baseline);
1043 cursor.pos(rows[i].pos);
1046 cursor.y(cursor.y() - top_baseline);
1047 cursor.x(top_x + 2); // 2 = frame width
1051 int sw = swh = SingleWidth(pain, par, cursor.pos());
1052 if (par->GetChar(cursor.pos()) != LyXParagraph::META_INSET)
1054 int checkpos = rows[actrow + 1].pos;
1055 if ((actrow + 2) < (int)rows.size())
1057 while ((cursor.pos() < checkpos) && ((cursor.x() + swh) < x)) {
1058 cursor.x(cursor.x() + sw);
1059 cursor.pos(cursor.pos() + 1);
1060 sw = swh = SingleWidth(pain, par, cursor.pos());
1061 if (par->GetChar(cursor.pos())!=LyXParagraph::META_INSET)
1067 void InsetText::resetPos(Painter & pain) const
1074 int old_pos = cursor.pos();
1076 cursor.y(top_baseline);
1078 for(unsigned int i = 0;
1079 i < (rows.size() - 1) && rows[i].pos <= cursor.pos();
1081 cursor.y(rows[i].baseline);
1084 cursor.y(cursor.y() - top_baseline);
1085 setPos(pain, 0, cursor.y());
1086 cursor.x(top_x + 2); // 2 = frame width
1087 while(cursor.pos() < old_pos) {
1088 cursor.x(cursor.x() + SingleWidth(pain, par,cursor.pos()));
1089 cursor.pos(cursor.pos() + 1);
1094 UpdatableInset::RESULT
1095 InsetText::moveRight(BufferView * bv, bool activate_inset)
1097 if (!cpar()->next && (cpos() >= cpar()->Last()))
1099 if (activate_inset && checkAndActivateInset(bv)) {
1102 text->CursorRight(bv);
1103 // real_current_font = current_font = GetFont(bv->buffer(), cpar(), cpos());
1104 return DISPATCHED_NOUPDATE;
1108 UpdatableInset::RESULT
1109 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1111 if (!cpar()->previous && (cpos() <= 0))
1113 text->CursorLeft(bv);
1115 if (checkAndActivateInset(bv, -1, -1))
1117 return DISPATCHED_NOUPDATE;
1121 UpdatableInset::RESULT
1122 InsetText::moveUp(BufferView * bv)
1124 if (!crow()->previous())
1127 return DISPATCHED_NOUPDATE;
1131 UpdatableInset::RESULT
1132 InsetText::moveDown(BufferView * bv)
1134 if (!crow()->next())
1136 text->CursorDown(bv);
1137 return DISPATCHED_NOUPDATE;
1141 bool InsetText::Delete()
1143 if ((par->GetChar(cursor.pos())==LyXParagraph::META_INSET) &&
1144 !par->GetInset(cursor.pos())->Deletable()) {
1147 par->Erase(cursor.pos());
1152 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1154 if (the_locking_inset) {
1155 if (the_locking_inset->InsertInsetAllowed(inset))
1156 return the_locking_inset->InsertInset(bv, inset);
1159 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1160 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous,
1161 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next);
1162 if (inset->Editable() == Inset::IS_EDITABLE) {
1163 UpdatableInset * i = static_cast<UpdatableInset *>(inset);
1164 i->setOwner(static_cast<UpdatableInset *>(this));
1166 par->InsertChar(cursor.pos(), LyXParagraph::META_INSET);
1167 par->InsertInset(cursor.pos(), inset);
1168 if (hasSelection()) {
1169 selection_start_cursor = selection_end_cursor = cursor;
1171 selection_start_cursor = selection_end_cursor = cursor;
1173 UpdateLocal(bv, true, true);
1174 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1179 UpdatableInset * InsetText::GetLockingInset()
1181 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1185 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1189 if (the_locking_inset)
1190 return the_locking_inset->GetFirstLockingInsetOfType(c);
1195 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1197 text->SetFont(bv, font, toggleall);
1201 void InsetText::computeTextRows(Painter & pain) const
1215 int width = wordAscent = wordDescent = 0;
1216 insetWidth = maxAscent = maxDescent = 0;
1221 rows.push_back(row);
1222 if (!autoBreakRows || (getMaxTextWidth(pain, this) < 0)) {
1223 for(p = 0; p < par->Last(); ++p) {
1224 insetWidth += SingleWidth(pain, par, p);
1225 SingleHeight(pain, par, p, asc, desc);
1226 maxAscent = max(maxAscent, asc);
1227 maxDescent = max(maxDescent, desc);
1229 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1230 rows[0].asc = maxAscent;
1231 rows[0].desc = maxDescent;
1232 // alocate a dummy row for the endpos
1233 row.pos = par->Last();
1234 rows.push_back(row);
1238 bool is_first_word_in_row = true;
1239 int cw, lastWordWidth = 0;
1240 int maxWidth = getMaxTextWidth(pain, this);
1241 // if we auto break rows than the insetwidth should be always the max
1242 // width as the display is stable it may get larger if we have a really
1243 // large word below and we draw it!!!
1244 insetWidth = maxWidth;
1246 for(p = 0; p < par->Last(); ++p) {
1247 if (par->IsNewline(p)) {
1248 rows.back().asc = wordAscent;
1249 rows.back().desc = wordDescent;
1251 rows.push_back(row);
1253 width = lastWordWidth = 0;
1254 oasc = odesc = wordAscent = wordDescent = 0;
1255 is_first_word_in_row = true;
1258 cw = SingleWidth(pain, par, p);
1260 if (par->GetChar(p) == LyXParagraph::META_INSET)
1261 inset = par->GetInset(p);
1262 if (inset && inset->display()) {
1263 inset->setOwner(const_cast<InsetText *>(this));
1264 if (cw > insetWidth)
1266 if (!is_first_word_in_row || (p != nwp)) {
1267 oasc = max(oasc, wordAscent);
1268 odesc = max(odesc, wordDescent);
1269 rows.back().asc = oasc;
1270 rows.back().desc = odesc;
1272 rows.push_back(row);
1274 SingleHeight(pain, par, p, asc, desc);
1275 rows.back().asc = asc;
1276 rows.back().desc = desc;
1277 row.pos = nwp = p + 1;
1278 rows.push_back(row);
1279 width = lastWordWidth = 0;
1280 oasc = odesc = wordAscent = wordDescent = 0;
1281 is_first_word_in_row = true;
1284 SingleHeight(pain, par, p, asc, desc);
1286 lastWordWidth += cw;
1287 if (width > maxWidth) {
1288 if (is_first_word_in_row) {
1289 if (!(width-cw)) { // only this character in word
1290 rows.back().asc = asc;
1291 rows.back().desc = desc;
1293 rows.push_back(row);
1299 lastWordWidth = width = 0;
1301 rows.back().asc = wordAscent;
1302 rows.back().desc = wordDescent;
1304 rows.push_back(row);
1309 lastWordWidth = width = cw;
1313 rows.back().asc = oasc;
1314 rows.back().desc = odesc;
1316 rows.push_back(row);
1318 odesc = wordDescent;
1319 width = lastWordWidth;
1320 wordAscent = max(wordAscent, asc);
1321 wordDescent = max(wordDescent, desc);
1322 is_first_word_in_row = true;
1325 wordAscent = max(wordAscent, asc);
1326 wordDescent = max(wordDescent, desc);
1328 if (par->IsSeparator(p) || inset) {
1330 inset->setOwner(const_cast<InsetText *>(this));
1334 oasc = max(oasc, wordAscent);
1335 odesc = max(odesc, wordDescent);
1336 wordAscent = wordDescent = lastWordWidth = 0;
1338 is_first_word_in_row = false;
1341 // if we have some data in the paragraph we have ascent/descent
1343 // assign last row data
1344 rows.back().asc = max(oasc, wordAscent);
1345 rows.back().desc = max(odesc, wordDescent);
1347 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1348 // alocate a dummy row for the endpos
1349 row.pos = par->Last();
1350 rows.push_back(row);
1351 // calculate maxAscent/Descent
1352 maxAscent = rows[0].asc;
1353 maxDescent = rows[0].desc;
1354 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1355 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1360 void InsetText::computeBaselines(int baseline) const
1362 rows[0].baseline = baseline;
1363 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1364 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1365 rows[i].asc + interline_space;
1370 void InsetText::UpdateLocal(BufferView * bv, bool what, bool mark_dirty)
1373 text->FullRebreak(bv);
1375 bv->updateInset(this, mark_dirty);
1377 // resetPos(bv->painter());
1378 if (old_par != cpar()) {
1379 bv->owner()->getToolbar()->combox->select(cpar()->GetLayout()+1);
1385 bool InsetText::cutSelection(Buffer const * buf)
1387 if (!hasSelection())
1392 LyXParagraph * endpar = par;
1394 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1395 start = selection_end_cursor.pos();
1396 end = selection_start_cursor.pos();
1398 start = selection_start_cursor.pos();
1399 end = selection_end_cursor.pos();
1402 return cap.cutSelection(par, &endpar, start, end, buf->params.textclass);
1406 bool InsetText::copySelection(Buffer const * buf)
1408 if (!hasSelection())
1414 if (selection_start_cursor.pos() > selection_end_cursor.pos()) {
1415 start = selection_end_cursor.pos();
1416 end = selection_start_cursor.pos();
1418 start = selection_start_cursor.pos();
1419 end = selection_end_cursor.pos();
1421 return cap.copySelection(par, par, start, end, buf->params.textclass);
1425 bool InsetText::pasteSelection(Buffer const * buf)
1429 if (cap.nrOfParagraphs() > 1) {
1430 WriteAlert(_("Impossible operation"),
1431 _("Cannot include more than one paragraph!"),
1435 LyXParagraph * endpar;
1436 LyXParagraph * actpar = par;
1438 int pos = cursor.pos();
1439 bool ret = cap.pasteSelection(&actpar, &endpar, pos,buf->params.textclass);
1445 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1448 if (par->GetChar(cursor.pos()) == LyXParagraph::META_INSET) {
1449 UpdatableInset * inset =
1450 static_cast<UpdatableInset*>(par->GetInset(cursor.pos()));
1451 LyXFont font = text->GetFont(bv->buffer(), cpar(), cpos());
1453 x = inset->width(bv->painter(), font);
1455 y = inset->descent(bv->painter(), font);
1456 inset_x = cursor.x() - top_x + drawTextXOffset;
1457 inset_y = cursor.y() + drawTextYOffset;
1458 inset->Edit(bv, x - inset_x, y - inset_y, button);
1459 if (!the_locking_inset)
1461 UpdateLocal(bv, true, false);
1468 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset) const
1470 // int w=getMaxWidth(pain, inset);
1472 return getMaxWidth(pain, inset) - 4; // 2+2 width of eventual border
1475 void InsetText::SetParagraphData(LyXParagraph *p)
1480 par->SetInsetOwner(this);
1484 void InsetText::SetAutoBreakRows(bool flag)
1486 if (flag != autoBreakRows) {
1487 autoBreakRows = flag;
1492 void InsetText::SetDrawLockedFrame(bool flag)
1494 if (flag != drawLockedFrame) {
1495 drawLockedFrame = flag;
1500 void InsetText::SetFrameColor(LColor::color col)
1502 if (frame_color != col) {
1508 int InsetText::cx() const
1510 return text->cursor.x() + top_x + 1;
1513 int InsetText::cy() const
1515 long int y_dummy = 0;
1516 Row * tmprow = text->GetRowNearY(y_dummy);
1517 return text->cursor.y() - tmprow->baseline();
1520 int InsetText::cpos() const
1522 return text->cursor.pos();
1525 LyXParagraph * InsetText::cpar() const
1527 return text->cursor.par();
1530 Row * InsetText::crow() const
1532 return text->cursor.row();