2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
32 #include "support/textutils.h"
34 #include "insetlatexaccent.h"
35 #include "insetquotes.h"
36 #include "mathed/formulamacro.h"
38 #include "insetinfo.h"
39 #include "insetinclude.h"
41 #include "insetcommand.h"
42 #include "insetindex.h"
43 #include "insetlabel.h"
45 //#include "insettabular.h"
47 #include "insetspecialchar.h"
48 #include "LaTeXFeatures.h"
50 #include "lyx_gui_misc.h"
51 #include "support/LAssert.h"
53 #include "lyxcursor.h"
54 #include "CutAndPaste.h"
62 extern unsigned char getCurrentTextClass(Buffer *);
64 InsetText::InsetText(Buffer * buf)
66 par = new LyXParagraph();
72 InsetText::InsetText(InsetText const & ins, Buffer * buf)
77 autoBreakRows = ins.autoBreakRows;
80 void InsetText::init(Buffer * buf, InsetText const * ins)
82 the_locking_inset = 0;
84 cursor_visible = false;
85 maxWidth = old_x = -1;
86 actpos = selection_start = selection_end = 0;
90 maxAscent = maxDescent = insetWidth = 0;
91 autoBreakRows = false;
96 par = ins->par->Clone();
97 autoBreakRows = ins->autoBreakRows;
99 par->SetInsetOwner(this);
103 InsetText::~InsetText()
109 Inset * InsetText::Clone() const
111 InsetText * t = new InsetText(*this, buffer);
116 void InsetText::Write(ostream & os) const
119 WriteParagraphData(os);
123 void InsetText::WriteParagraphData(ostream & os) const
125 par->writeFile(os, buffer->params, 0, 0);
129 void InsetText::Read(LyXLex & lex)
131 string token, tmptok;
133 LyXParagraph * return_par = 0;
134 char depth = 0; // signed or unsigned?
135 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
136 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
137 LyXFont font(LyXFont::ALL_INHERIT);
140 par = new LyXParagraph;
144 token = lex.GetString();
147 if (token == "\\end_inset")
149 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
153 // the_end read this should NEVER happen
154 lex.printError("\\the_end read in inset! Error in document!");
158 if (token != "\\end_inset") {
159 lex.printError("Missing \\end_inset at this point. "
166 int InsetText::ascent(Painter & pain, LyXFont const & font) const
169 computeTextRows(pain, xpos);
174 return lyxfont::maxAscent(font);
178 int InsetText::descent(Painter & pain, LyXFont const & font) const
181 computeTextRows(pain, xpos);
186 return lyxfont::maxDescent(font);
190 int InsetText::width(Painter & pain, LyXFont const &) const
193 computeTextRows(pain, xpos);
200 void InsetText::draw(Painter & pain, LyXFont const & f,
201 int baseline, float & x) const
204 UpdatableInset::draw(pain, f, baseline, x);
207 top_baseline = baseline;
208 computeTextRows(pain, x);
209 computeBaselines(baseline);
210 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
211 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
212 rows[r].baseline, x);
213 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
219 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
220 int row, int baseline, float x) const
226 if (selection_start > selection_end) {
227 s_start = selection_end;
228 s_end = selection_start;
230 s_start = selection_start;
231 s_end = selection_end;
233 if ((s_start > endpos) || (s_end < startpos))
237 int ssel_x = esel_x = int(x);
240 for(; p < endpos; ++p) {
243 if ((p >= s_start) && (p <= s_end))
245 char ch = par->GetChar(p);
246 font = GetDrawFont(par,p);
247 if (IsFloatChar(ch)) {
249 } else if (ch == LyXParagraph::META_INSET) {
250 Inset const * tmpinset = par->GetInset(p);
251 x += tmpinset->width(pain, font);
253 x += lyxfont::width(ch, font);
258 if ((p >= s_start) && (p <= s_end))
260 if (ssel_x < esel_x) {
261 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
262 int(esel_x - ssel_x),
263 rows[row].asc + rows[row].desc,
269 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
270 int baseline, float x) const
272 Assert(endpos <= par->Last());
274 for(int p = startpos; p < endpos; ++p) {
275 char ch = par->GetChar(p);
276 LyXFont font = GetDrawFont(par,p);
277 if (IsFloatChar(ch)) {
279 } else if (par->IsNewline(p)) {
280 // Draw end-of-line marker
281 int wid = lyxfont::width('n', font);
282 int asc = lyxfont::maxAscent(font);
286 xp[0] = int(x + wid * 0.375);
287 yp[0] = int(y - 0.875 * asc * 0.75);
290 yp[1] = int(y - 0.500 * asc * 0.75);
292 xp[2] = int(x + wid * 0.375);
293 yp[2] = int(y - 0.125 * asc * 0.75);
295 pain.lines(xp, yp, 3, LColor::eolmarker);
298 yp[0] = int(y - 0.500 * asc * 0.75);
300 xp[1] = int(x + wid);
301 yp[1] = int(y - 0.500 * asc * 0.75);
303 xp[2] = int(x + wid);
304 yp[2] = int(y - asc * 0.75);
306 pain.lines(xp, yp, 3, LColor::eolmarker);
308 } else if (ch == LyXParagraph::META_INSET) {
309 Inset * tmpinset = par->GetInset(p);
311 tmpinset->draw(pain, font, baseline, x);
313 pain.text(int(x), baseline, ch, font);
314 x += lyxfont::width(ch, font);
320 char const * InsetText::EditMessage() const
322 return _("Opened Text Inset");
326 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
328 par->SetInsetOwner(this);
329 UpdatableInset::Edit(bv, x, y, button);
332 the_locking_inset = 0;
333 inset_pos = inset_x = inset_y = 0;
336 selection_start = selection_end = actpos;
337 current_font = real_current_font = GetFont(par, actpos);
338 bv->text->FinishUndo();
342 void InsetText::InsetUnlock(BufferView * bv)
344 if (the_locking_inset)
345 the_locking_inset->InsetUnlock(bv);
347 if (hasSelection()) {
348 selection_start = selection_end = actpos;
349 UpdateLocal(bv, false);
351 the_locking_inset = 0;
352 no_selection = false;
356 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
358 if (!the_locking_inset)
360 if (the_locking_inset == inset) {
361 the_locking_inset->InsetUnlock(bv);
362 the_locking_inset = 0;
364 moveRight(bv, false);
367 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
371 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
373 if (!the_locking_inset)
375 if (the_locking_inset != inset)
376 return the_locking_inset->UpdateInsetInInset(bv, inset);
378 inset->draw(bv->painter(), real_current_font, inset_y, x);
379 UpdateLocal(bv, true);
384 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
386 if (the_locking_inset) {
387 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
390 no_selection = false;
394 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
396 if (hasSelection()) {
397 selection_start = selection_end = actpos;
398 UpdateLocal(bv, false);
400 no_selection = false;
401 if (the_locking_inset) {
402 setPos(bv, x, y, false);
405 if (par->GetChar(actpos) == LyXParagraph::META_INSET)
406 inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
407 if (the_locking_inset == inset) {
408 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
411 // otherwise unlock the_locking_inset and lock the new inset
415 the_locking_inset->InsetUnlock(bv);
416 the_locking_inset = inset;
417 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
420 // otherwise only unlock the_locking_inset
421 the_locking_inset->InsetUnlock(bv);
424 the_locking_inset = 0;
426 selection_start = selection_end = actpos;
427 if (!the_locking_inset)
432 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
434 if (the_locking_inset) {
435 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
440 int old = selection_end;
441 setPos(bv, x, y, false);
442 selection_end = actpos;
443 if (old != selection_end)
444 UpdateLocal(bv, false);
446 no_selection = false;
450 void InsetText::InsetKeyPress(XKeyEvent * xke)
452 if (the_locking_inset) {
453 the_locking_inset->InsetKeyPress(xke);
459 UpdatableInset::RESULT
460 InsetText::LocalDispatch(BufferView * bv,
461 int action, string const & arg)
463 no_selection = false;
464 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
469 UpdatableInset::RESULT
472 if ((action < 0) && arg.empty())
475 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
476 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
478 if (the_locking_inset) {
479 result = the_locking_inset->LocalDispatch(bv, action, arg);
480 if (result == DISPATCHED) {
481 the_locking_inset->ToggleInsetCursor(bv);
482 UpdateLocal(bv, false);
483 the_locking_inset->ToggleInsetCursor(bv);
485 } else if (result == FINISHED) {
486 if ((action == LFUN_RIGHT) || (action == -1)) {
487 actpos = inset_pos + 1;
490 the_locking_inset = 0;
498 bv->text->SetUndo(Undo::INSERT,
499 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
500 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
502 actpos = selection_start;
503 par->InsertChar(actpos,arg[0]);
504 SetCharFont(actpos,current_font);
506 selection_start = selection_end = actpos;
507 UpdateLocal(bv, true);
509 // --- Cursor Movements ---------------------------------------------
511 bv->text->FinishUndo();
512 moveRight(bv, false);
513 selection_end = actpos;
514 UpdateLocal(bv, false);
518 bv->text->FinishUndo();
519 result= DISPATCH_RESULT(moveRight(bv));
520 if (hasSelection()) {
521 selection_start = selection_end = actpos;
522 UpdateLocal(bv, false);
524 selection_start = selection_end = actpos;
529 bv->text->FinishUndo();
531 selection_end = actpos;
532 UpdateLocal(bv, false);
536 bv->text->FinishUndo();
537 result= DISPATCH_RESULT(moveLeft(bv));
538 if (hasSelection()) {
539 selection_start = selection_end = actpos;
540 UpdateLocal(bv, false);
542 selection_start = selection_end = actpos;
547 bv->text->FinishUndo();
549 selection_end = actpos;
550 UpdateLocal(bv, false);
554 bv->text->FinishUndo();
555 result= DISPATCH_RESULT(moveDown(bv));
556 if (hasSelection()) {
557 selection_start = selection_end = actpos;
558 UpdateLocal(bv, false);
560 selection_start = selection_end = actpos;
565 bv->text->FinishUndo();
567 selection_end = actpos;
568 UpdateLocal(bv, false);
572 bv->text->FinishUndo();
573 result= DISPATCH_RESULT(moveUp(bv));
574 if (hasSelection()) {
575 selection_start = selection_end = actpos;
576 UpdateLocal(bv, false);
578 selection_start = selection_end = actpos;
583 if (!actpos) { // || par->IsNewline(actpos-1)) {
584 if (hasSelection()) {
585 selection_start = selection_end = actpos;
586 UpdateLocal(bv, false);
593 bv->text->SetUndo(Undo::DELETE,
594 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
595 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
597 if (hasSelection()) {
598 LyXParagraph::size_type i = selection_start;
599 for (; i < selection_end; ++i) {
600 par->Erase(selection_start);
604 if (ret) { // we need update
605 selection_start = selection_end = actpos;
606 UpdateLocal(bv, true);
607 } else if (hasSelection()) {
608 selection_start = selection_end = actpos;
609 UpdateLocal(bv, false);
615 bv->text->SetUndo(Undo::DELETE,
616 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
617 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
619 if (cutSelection()) {
621 actpos = selection_end = selection_start;
622 UpdateLocal(bv, true);
623 } else if (hasSelection()) {
624 selection_start = selection_end = actpos;
625 UpdateLocal(bv, false);
630 bv->text->FinishUndo();
631 if (copySelection()) {
633 selection_start = selection_end = actpos;
634 UpdateLocal(bv, true);
635 } else if (hasSelection()) {
636 selection_start = selection_end = actpos;
637 UpdateLocal(bv, false);
642 bv->text->SetUndo(Undo::INSERT,
643 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
644 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
645 if (pasteSelection()) {
646 selection_start = selection_end = actpos;
647 UpdateLocal(bv, true);
653 bv->text->FinishUndo();
654 for(; actpos > rows[actrow].pos; --actpos)
655 cx -= SingleWidth(bv->painter(), par, actpos);
656 cx -= SingleWidth(bv->painter(), par, actpos);
657 if (hasSelection()) {
658 selection_start = selection_end = actpos;
659 UpdateLocal(bv, false);
661 selection_start = selection_end = actpos;
667 bv->text->FinishUndo();
668 int checkpos = (int)rows[actrow + 1].pos;
669 if ((actrow + 2) < (int)rows.size())
671 for(; actpos < checkpos; ++actpos)
672 cx += SingleWidth(bv->painter(), par, actpos);
673 if (hasSelection()) {
674 selection_start = selection_end = actpos;
675 UpdateLocal(bv, false);
677 selection_start = selection_end = actpos;
682 case LFUN_MATH_MODE: // Open or create a math inset
683 bv->text->SetUndo(Undo::INSERT,
684 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
685 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
686 InsertInset(bv, new InsetFormula);
687 if (hasSelection()) {
688 selection_start = selection_end = actpos;
689 UpdateLocal(bv, false);
691 selection_start = selection_end = actpos;
694 case LFUN_BREAKPARAGRAPH:
698 bv->text->SetUndo(Undo::INSERT,
699 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
700 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
701 par->InsertChar(actpos,LyXParagraph::META_NEWLINE);
702 SetCharFont(actpos,current_font);
703 UpdateLocal(bv, true);
705 selection_start = selection_end = actpos;
709 result = UNDISPATCHED;
712 if (result != FINISHED) {
713 if (!the_locking_inset)
716 bv->unlockInset(this);
721 int InsetText::Latex(ostream & os, signed char /*fragile*/, bool) const
724 int ret = par->SimpleTeXOnePar(os, texrow);
729 void InsetText::Validate(LaTeXFeatures & features) const
731 par->validate(features);
735 // Returns the width of a character at a certain spot
736 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
738 LyXFont font = GetDrawFont(par, pos);
739 char c = par->GetChar(pos);
741 if (IsPrintable(c)) {
742 return lyxfont::width(c, font);
743 } else if (c == LyXParagraph::META_INSET) {
744 Inset const * tmpinset = par->GetInset(pos);
746 return tmpinset->width(pain, font);
749 } else if (IsSeparatorChar(c))
751 else if (IsNewlineChar(c))
753 return lyxfont::width(c, font);
757 // Returns the width of a character at a certain spot
758 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
759 int & asc, int & desc) const
761 LyXFont font = GetDrawFont(par, pos);
762 char c = par->GetChar(pos);
765 if (c == LyXParagraph::META_INSET) {
766 Inset const * tmpinset=par->GetInset(pos);
768 asc = tmpinset->ascent(pain, font);
769 desc = tmpinset->descent(pain, font);
772 asc = lyxfont::maxAscent(font);
773 desc = lyxfont::maxDescent(font);
779 // Gets the fully instantiated font at a given position in a paragraph
780 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
781 // The difference is that this one is used for displaying, and thus we
782 // are allowed to make cosmetic improvements. For instance make footnotes
784 // If position is -1, we get the layout font of the paragraph.
785 // If position is -2, we get the font of the manual label of the paragraph.
786 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
788 char par_depth = par->GetDepth();
790 LyXLayout const & layout =
791 textclasslist.Style(buffer->params.textclass, par->GetLayout());
793 // We specialize the 95% common case:
794 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
797 if (layout.labeltype == LABEL_MANUAL
798 && pos < BeginningOfMainBody(par)) {
800 return par->GetFontSettings(pos).realize(layout.reslabelfont);
802 return par->GetFontSettings(pos).realize(layout.resfont);
805 // process layoutfont for pos == -1 and labelfont for pos < -1
807 return layout.resfont;
809 return layout.reslabelfont;
812 // The uncommon case need not be optimized as much
814 LyXFont layoutfont, tmpfont;
818 if (pos < BeginningOfMainBody(par)) {
820 layoutfont = layout.labelfont;
823 layoutfont = layout.font;
825 tmpfont = par->GetFontSettings(pos);
826 tmpfont.realize(layoutfont);
829 // process layoutfont for pos == -1 and labelfont for pos < -1
831 tmpfont = layout.font;
833 tmpfont = layout.labelfont;
836 // Resolve against environment font information
837 //if (par->GetDepth()){ // already in while condition
838 while (par && par_depth && !tmpfont.resolved()) {
839 par = par->DepthHook(par_depth - 1);
841 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
842 par->GetLayout()).font);
843 par_depth = par->GetDepth();
846 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
851 // the font for drawing may be different from the real font
852 LyXFont InsetText::GetDrawFont(LyXParagraph * par, int pos) const
854 return GetFont(par, pos);
857 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
859 if (textclasslist.Style(buffer->params.textclass,
860 par->GetLayout()).labeltype != LABEL_MANUAL)
863 return par->BeginningOfMainBody();
867 void InsetText::GetCursorPos(int & x, int & y) const
874 int InsetText::InsetInInsetY()
876 if (!the_locking_inset)
880 return (y + the_locking_inset->InsetInInsetY());
884 void InsetText::ToggleInsetCursor(BufferView * bv)
886 if (the_locking_inset) {
887 the_locking_inset->ToggleInsetCursor(bv);
891 LyXFont font = GetDrawFont(par, actpos);
893 int asc = lyxfont::maxAscent(font);
894 int desc = lyxfont::maxDescent(font);
897 bv->hideLockedInsetCursor();
899 bv->showLockedInsetCursor(cx, cy, asc, desc);
900 cursor_visible = !cursor_visible;
904 void InsetText::ShowInsetCursor(BufferView * bv)
906 if (!cursor_visible) {
907 LyXFont font = GetDrawFont(par, actpos);
909 int asc = lyxfont::maxAscent(font);
910 int desc = lyxfont::maxDescent(font);
911 bv->fitLockedInsetCursor(cx, cy, asc, desc);
912 bv->showLockedInsetCursor(cx, cy, asc, desc);
913 cursor_visible = true;
918 void InsetText::HideInsetCursor(BufferView * bv)
921 ToggleInsetCursor(bv);
925 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
930 // search right X-pos x==0 -> top_x
934 for(unsigned int i = 1;
935 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
936 cy = rows[i].baseline;
937 actpos = rows[i].pos;
945 int sw = swh = SingleWidth(bv->painter(), par,actpos);
946 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
948 int checkpos = rows[actrow + 1].pos;
949 if ((actrow+2) < (int)rows.size())
951 while ((actpos < checkpos) && ((cx + swh) < x)) {
954 sw = swh = SingleWidth(bv->painter(), par,actpos);
955 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
958 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
960 static_cast<UpdatableInset*>(par->GetInset(actpos));
961 inset_x = cx - top_x;
964 the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
969 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
971 if (actpos >= par->Last())
973 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
975 static_cast<UpdatableInset*>(par->GetInset(actpos));
976 inset_x = cx - top_x;
979 the_locking_inset->Edit(bv, 0, 0, 0);
988 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
993 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
995 static_cast<UpdatableInset*>(par->GetInset(actpos));
997 inset_x = cx - top_x;
1000 the_locking_inset->Edit(bv, the_locking_inset->
1001 width(bv->painter(), GetDrawFont(par,actpos)),
1010 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
1014 cy = rows[actrow - 1].baseline - top_baseline;
1015 setPos(bv, cx - top_x, cy, activate_inset);
1020 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
1022 if (actrow >= int(rows.size() - 2))
1024 cy = rows[actrow + 1].baseline - top_baseline;
1025 setPos(bv, cx - top_x, cy, activate_inset);
1030 void InsetText::resetPos(BufferView * bv, bool setfont)
1033 real_current_font = current_font = GetDrawFont(par, actpos);
1038 int old_pos = actpos;
1042 for(unsigned int i = 0; (i < (rows.size()-1)) && (rows[i].pos <= actpos);
1044 cy = rows[i].baseline;
1048 setPos(bv, 0, cy, false);
1050 while(actpos < old_pos) {
1051 cx += SingleWidth(bv->painter(), par,actpos);
1057 bool InsetText::Delete()
1059 /* some insets are undeletable here */
1060 if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
1061 /* force complete redo when erasing display insets */
1062 /* this is a cruel method but save..... Matthias */
1063 if (par->GetInset(actpos)->Deletable() &&
1064 par->GetInset(actpos)->display()) {
1075 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1077 if (inset->Editable() == Inset::IS_EDITABLE) {
1078 UpdatableInset *i = (UpdatableInset *)inset;
1079 i->setOwner((UpdatableInset *)this);
1081 par->InsertChar(actpos, LyXParagraph::META_INSET);
1082 par->InsertInset(actpos, inset);
1083 UpdateLocal(bv, true);
1084 the_locking_inset = static_cast<UpdatableInset*>(inset);
1085 inset_x = cx - top_x;
1088 inset->Edit(bv, 0, 0, 0);
1093 UpdatableInset * InsetText::GetLockingInset()
1095 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1099 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1101 // if there is no selection just set the current_font
1102 if (!hasSelection()) {
1103 // Determine basis font
1105 if (actpos < BeginningOfMainBody(par))
1106 layoutfont = GetFont(par, -2);
1108 layoutfont = GetFont(par, -1);
1110 // Update current font
1111 real_current_font.update(font, bv->buffer()->params.language_info,
1114 // Reduce to implicit settings
1115 current_font = real_current_font;
1116 current_font.reduce(layoutfont);
1117 // And resolve it completely
1118 real_current_font.realize(layoutfont);
1123 if (selection_start > selection_end) {
1124 s_start = selection_end;
1125 s_end = selection_start;
1127 s_start = selection_start;
1128 s_end = selection_end;
1131 while(s_start < s_end) {
1132 newfont = GetFont(par,s_start);
1133 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1134 SetCharFont(s_start, newfont);
1137 UpdateLocal(bv, true);
1141 void InsetText::SetCharFont(int pos, LyXFont const & f)
1143 /* let the insets convert their font */
1146 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1147 if (par->GetInset(pos))
1148 font = par->GetInset(pos)->ConvertFont(font);
1150 LyXLayout const & layout =
1151 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1153 // Get concrete layout font to reduce against
1156 if (pos < BeginningOfMainBody(par))
1157 layoutfont = layout.labelfont;
1159 layoutfont = layout.font;
1162 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1165 // Now, reduce font against full layout font
1166 font.reduce(layoutfont);
1168 par->SetFont(pos, font);
1172 void InsetText::computeTextRows(Painter & pain, float x) const
1187 int width = wordAscent = wordDescent = 0;
1188 insetWidth = maxAscent = maxDescent = 0;
1193 rows.push_back(row);
1194 if (!autoBreakRows) {
1195 for(p = 0; p < par->Last(); ++p) {
1196 insetWidth += SingleWidth(pain, par, p);
1197 SingleHeight(pain, par, p, asc, desc);
1198 maxAscent = max(maxAscent, asc);
1199 maxDescent = max(maxDescent, desc);
1201 rows[0].asc = maxAscent;
1202 rows[0].desc = maxDescent;
1203 // alocate a dummy row for the endpos
1204 row.pos = par->Last();
1205 rows.push_back(row);
1209 bool is_first_word_in_row = true;
1211 int cw, lastWordWidth = 0;
1213 maxWidth = getMaxWidth(pain) - widthOffset;
1214 for(p = 0; p < par->Last(); ++p) {
1215 cw = SingleWidth(pain, par, p);
1217 lastWordWidth += cw;
1218 SingleHeight(pain, par, p, asc, desc);
1219 wordAscent = max(wordAscent, asc);
1220 wordDescent = max(wordDescent, desc);
1221 if (par->IsNewline(p)) {
1222 rows.back().asc = wordAscent;
1223 rows.back().desc = wordDescent;
1225 rows.push_back(row);
1226 SingleHeight(pain, par, p, oasc, odesc);
1227 width = lastWordWidth = 0;
1228 is_first_word_in_row = true;
1229 wordAscent = wordDescent = 0;
1232 Inset const * inset = 0;
1233 if (((p + 1) < par->Last()) &&
1234 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1235 inset = par->GetInset(p + 1);
1236 if (inset && inset->display()) {
1237 if (!is_first_word_in_row && (width >= (maxWidth - x))) {
1238 // we have to split also the row above
1239 rows.back().asc = oasc;
1240 rows.back().desc = odesc;
1242 rows.push_back(row);
1244 odesc = wordDescent;
1245 insetWidth = max(insetWidth, owidth);
1246 width = lastWordWidth;
1249 oasc = max(oasc, wordAscent);
1250 odesc = max(odesc, wordDescent);
1252 rows.back().asc = oasc;
1253 rows.back().desc = odesc;
1255 rows.push_back(row);
1256 SingleHeight(pain, par, p, asc, desc);
1257 rows.back().asc = asc;
1258 rows.back().desc = desc;
1259 row.pos = nwp = p + 1;
1260 rows.push_back(row);
1261 oasc = odesc = width = lastWordWidth = 0;
1262 is_first_word_in_row = true;
1263 wordAscent = wordDescent = 0;
1265 } else if (par->IsSeparator(p)) {
1266 if (width >= maxWidth - x) {
1267 if (is_first_word_in_row) {
1268 rows.back().asc = wordAscent;
1269 rows.back().desc = wordDescent;
1271 rows.push_back(row);
1272 oasc = odesc = width = 0;
1274 rows.back().asc = oasc;
1275 rows.back().desc = odesc;
1277 rows.push_back(row);
1279 odesc = wordDescent;
1280 insetWidth = max(insetWidth, owidth);
1281 width = lastWordWidth;
1283 wordAscent = wordDescent = lastWordWidth = 0;
1288 oasc = max(oasc, wordAscent);
1289 odesc = max(odesc, wordDescent);
1290 wordAscent = wordDescent = lastWordWidth = 0;
1292 is_first_word_in_row = false;
1295 // if we have some data in the paragraph we have ascent/descent
1297 if (width >= (maxWidth - x)) {
1299 rows.back().asc = oasc;
1300 rows.back().desc = odesc;
1301 // assign and allocate lower row
1303 rows.push_back(row);
1304 rows.back().asc = wordAscent;
1305 rows.back().desc = wordDescent;
1306 width -= lastWordWidth;
1308 // assign last row data
1309 rows.back().asc = max(oasc, wordAscent);
1310 rows.back().desc = max(odesc, wordDescent);
1313 insetWidth = max(insetWidth, width);
1314 // alocate a dummy row for the endpos
1315 row.pos = par->Last();
1316 rows.push_back(row);
1317 // calculate maxAscent/Descent
1318 maxAscent = rows[0].asc;
1319 maxDescent = rows[0].desc;
1320 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1321 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1324 if (the_locking_inset) {
1325 computeBaselines(top_baseline);
1328 inset_x = cx - top_x;
1335 void InsetText::computeBaselines(int baseline) const
1337 rows[0].baseline = baseline;
1338 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1339 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1340 rows[i].asc + interline_space;
1344 void InsetText::UpdateLocal(BufferView *bv, bool flag)
1346 HideInsetCursor(bv);
1348 computeTextRows(bv->painter(), xpos);
1349 computeBaselines(top_baseline);
1351 bv->updateInset(this, flag);
1354 ShowInsetCursor(bv);
1357 bool InsetText::cutSelection()
1359 if (!hasSelection())
1364 LyXParagraph *endpar = par;
1366 return cap.cutSelection(par, &endpar, selection_start, selection_end,
1367 buffer->params.textclass);
1370 bool InsetText::copySelection()
1372 if (!hasSelection())
1377 return cap.copySelection(par, par, selection_start, selection_end,
1378 buffer->params.textclass);
1381 bool InsetText::pasteSelection()
1385 if (cap.nrOfParagraphs() > 1) {
1386 WriteAlert(_("Impossible operation"),
1387 _("Cannot include more than one paragraph!"),
1391 LyXParagraph *endpar;
1392 LyXParagraph *actpar = par;
1394 return cap.pasteSelection(&actpar, &endpar, actpos,
1395 buffer->params.textclass);