2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998 The LyX Team.
9 * ======================================================
23 #pragma implementation
26 #include "insettext.h"
31 #include "commandtags.h"
34 #include "BufferView.h"
35 #include "support/textutils.h"
37 #include "insetlatexaccent.h"
38 #include "insetquotes.h"
39 #include "mathed/formulamacro.h"
41 #include "insetinfo.h"
42 #include "insetinclude.h"
44 #include "insetcommand.h"
45 #include "insetindex.h"
46 #include "insetlabel.h"
48 //#include "insettabular.h"
50 #include "insetspecialchar.h"
51 #include "LaTeXFeatures.h"
53 #include "lyx_gui_misc.h"
54 #include "support/LAssert.h"
56 #include "lyxcursor.h"
58 extern unsigned char getCurrentTextClass(Buffer *);
60 InsetText::InsetText(Buffer * buf)
62 par = new LyXParagraph();
68 InsetText::InsetText(InsetText const & ins, Buffer * buf)
75 void InsetText::init(Buffer * buf, LyXParagraph *p)
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 InsetText::~InsetText()
102 Inset * InsetText::Clone() const
104 InsetText * t = new InsetText(*this, buffer);
109 void InsetText::Write(ostream & os) const
112 WriteParagraphData(os);
116 void InsetText::WriteParagraphData(ostream & os) const
118 par->writeFile(os, buffer->params, 0, 0);
122 void InsetText::Read(LyXLex & lex)
124 string token, tmptok;
126 LyXParagraph * return_par = 0;
127 char depth = 0; // signed or unsigned?
128 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
129 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
130 LyXFont font(LyXFont::ALL_INHERIT);
133 par = new LyXParagraph;
137 token = lex.GetString();
140 if (token == "\\end_inset")
142 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
146 // the_end read this should NEVER happen
147 lex.printError("\\the_end read in inset! Error in document!");
151 if (token != "\\end_inset") {
152 lex.printError("Missing \\end_inset at this point. "
159 int InsetText::ascent(Painter & pain, LyXFont const & font) const
162 computeTextRows(pain, xpos);
167 return font.maxAscent();
171 int InsetText::descent(Painter & pain, LyXFont const & font) const
174 computeTextRows(pain, xpos);
179 return font.maxDescent();
183 int InsetText::width(Painter & pain, LyXFont const &) const
186 computeTextRows(pain, xpos);
193 void InsetText::draw(Painter & pain, LyXFont const & f,
194 int baseline, float & x) const
197 computeTextRows(pain, x);
198 UpdatableInset::draw(pain, f, baseline, x);
201 top_baseline = baseline;
202 computeBaselines(baseline);
203 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
204 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
205 rows[r].baseline, x);
206 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
212 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
213 int row, int baseline, float x) const
219 if (selection_start > selection_end) {
220 s_start = selection_end;
221 s_end = selection_start;
223 s_start = selection_start;
224 s_end = selection_end;
226 if ((s_start > endpos) || (s_end < startpos))
230 int ssel_x = esel_x = int(x);
233 for(; p < endpos; ++p) {
236 if ((p >= s_start) && (p <= s_end))
238 char ch = par->GetChar(p);
239 font = GetFont(par,p);
240 if (IsFloatChar(ch)) {
242 } else if (ch == LyXParagraph::META_INSET) {
243 Inset const * tmpinset = par->GetInset(p);
244 x += tmpinset->width(pain, font);
246 x += pain.width(ch,font);
251 if ((p >= s_start) && (p <= s_end))
253 if (ssel_x < esel_x) {
254 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
255 int(esel_x - ssel_x),
256 rows[row].asc + rows[row].desc,
262 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
263 int baseline, float x) const
265 Assert(endpos <= par->Last());
267 for(int p = startpos; p < endpos; ++p) {
268 char ch = par->GetChar(p);
269 LyXFont font = GetFont(par,p);
270 if (IsFloatChar(ch)) {
272 } else if (par->IsNewline(p)) {
273 // Draw end-of-line marker
274 int wid = font.width('n');
275 int asc = font.maxAscent();
279 xp[0] = int(x + wid * 0.375);
280 yp[0] = int(y - 0.875 * asc * 0.75);
283 yp[1] = int(y - 0.500 * asc * 0.75);
285 xp[2] = int(x + wid * 0.375);
286 yp[2] = int(y - 0.125 * asc * 0.75);
288 pain.lines(xp, yp, 3, LColor::eolmarker);
291 yp[0] = int(y - 0.500 * asc * 0.75);
293 xp[1] = int(x + wid);
294 yp[1] = int(y - 0.500 * asc * 0.75);
296 xp[2] = int(x + wid);
297 yp[2] = int(y - asc * 0.75);
299 pain.lines(xp, yp, 3, LColor::eolmarker);
301 } else if (ch == LyXParagraph::META_INSET) {
302 Inset * tmpinset = par->GetInset(p);
304 tmpinset->draw(pain, font, baseline, x);
306 pain.text(int(x), baseline, ch, font);
307 x += pain.width(ch,font);
313 char const * InsetText::EditMessage() const
315 return _("Opened Text Inset");
319 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
321 UpdatableInset::Edit(bv, x, y, button);
324 the_locking_inset = 0;
325 inset_pos = inset_x = inset_y = 0;
328 selection_start = selection_end = actpos;
329 current_font = real_current_font = GetFont(par, actpos);
330 bv->text->FinishUndo();
334 void InsetText::InsetUnlock(BufferView * bv)
336 if (the_locking_inset)
337 the_locking_inset->InsetUnlock(bv);
339 if (hasSelection()) {
340 selection_start = selection_end = actpos;
341 UpdateLocal(bv, false);
343 the_locking_inset = 0;
344 no_selection = false;
348 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
350 if (!the_locking_inset)
352 if (the_locking_inset == inset) {
353 the_locking_inset->InsetUnlock(bv);
354 the_locking_inset = 0;
356 moveRight(bv, false);
359 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
363 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
365 if (!the_locking_inset)
367 if (the_locking_inset != inset)
368 return the_locking_inset->UpdateInsetInInset(bv, inset);
370 inset->draw(bv->getPainter(), real_current_font, inset_y, x);
371 UpdateLocal(bv, true);
376 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
378 if (the_locking_inset) {
379 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
382 no_selection = false;
386 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
388 if (hasSelection()) {
389 selection_start = selection_end = actpos;
390 UpdateLocal(bv, false);
392 no_selection = false;
393 if (the_locking_inset) {
394 setPos(bv, x, y, false);
397 if (par->GetChar(actpos) == LyXParagraph::META_INSET)
398 inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
399 if (the_locking_inset == inset) {
400 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
403 // otherwise unlock the_locking_inset and lock the new inset
407 the_locking_inset->InsetUnlock(bv);
408 the_locking_inset = inset;
409 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
412 // otherwise only unlock the_locking_inset
413 the_locking_inset->InsetUnlock(bv);
416 the_locking_inset = 0;
418 selection_start = selection_end = actpos;
419 if (!the_locking_inset)
424 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
426 if (the_locking_inset) {
427 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
432 int old = selection_end;
433 setPos(bv, x, y, false);
434 selection_end = actpos;
435 if (old != selection_end)
436 UpdateLocal(bv, false);
438 no_selection = false;
442 void InsetText::InsetKeyPress(XKeyEvent * xke)
444 if (the_locking_inset) {
445 the_locking_inset->InsetKeyPress(xke);
451 UpdatableInset::RESULT
452 InsetText::LocalDispatch(BufferView * bv,
453 int action, string const & arg)
455 no_selection = false;
456 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
461 UpdatableInset::RESULT
464 if ((action < 0) && arg.empty())
467 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
468 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
470 if (the_locking_inset) {
471 result = the_locking_inset->LocalDispatch(bv, action, arg);
472 if (result == DISPATCHED) {
473 the_locking_inset->ToggleInsetCursor(bv);
474 UpdateLocal(bv, false);
475 the_locking_inset->ToggleInsetCursor(bv);
477 } else if (result == FINISHED) {
478 if ((action == LFUN_RIGHT) || (action == -1)) {
479 actpos = inset_pos + 1;
482 the_locking_inset = 0;
490 bv->text->SetUndo(Undo::INSERT,
491 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
492 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
494 actpos = selection_start;
495 par->InsertChar(actpos,arg[0]);
496 par->SetFont(actpos,real_current_font);
498 selection_start = selection_end = actpos;
499 UpdateLocal(bv, true);
501 // --- Cursor Movements ---------------------------------------------
503 bv->text->FinishUndo();
504 moveRight(bv, false);
505 selection_end = actpos;
506 UpdateLocal(bv, false);
509 bv->text->FinishUndo();
510 result= DISPATCH_RESULT(moveRight(bv));
511 if (hasSelection()) {
512 selection_start = selection_end = actpos;
513 UpdateLocal(bv, false);
515 selection_start = selection_end = actpos;
519 bv->text->FinishUndo();
521 selection_end = actpos;
522 UpdateLocal(bv, false);
525 bv->text->FinishUndo();
526 result= DISPATCH_RESULT(moveLeft(bv));
527 if (hasSelection()) {
528 selection_start = selection_end = actpos;
529 UpdateLocal(bv, false);
531 selection_start = selection_end = actpos;
535 bv->text->FinishUndo();
537 selection_end = actpos;
538 UpdateLocal(bv, false);
541 bv->text->FinishUndo();
542 result= DISPATCH_RESULT(moveDown(bv));
543 if (hasSelection()) {
544 selection_start = selection_end = actpos;
545 UpdateLocal(bv, false);
547 selection_start = selection_end = actpos;
551 bv->text->FinishUndo();
553 selection_end = actpos;
554 UpdateLocal(bv, false);
557 bv->text->FinishUndo();
558 result= DISPATCH_RESULT(moveUp(bv));
559 if (hasSelection()) {
560 selection_start = selection_end = actpos;
561 UpdateLocal(bv, false);
563 selection_start = selection_end = actpos;
567 if (!actpos) { // || par->IsNewline(actpos-1)) {
568 if (hasSelection()) {
569 selection_start = selection_end = actpos;
570 UpdateLocal(bv, false);
577 bv->text->SetUndo(Undo::DELETE,
578 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
579 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
581 ret = cutSelection();
584 if (ret) { // we need update
585 selection_start = selection_end = actpos;
586 UpdateLocal(bv, true);
587 } else if (hasSelection()) {
588 selection_start = selection_end = actpos;
589 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);
596 if (cutSelection()) { // we need update
597 actpos = selection_end = selection_start;
598 UpdateLocal(bv, true);
599 } else if (hasSelection()) {
600 selection_start = selection_end = actpos;
601 UpdateLocal(bv, false);
605 bv->text->FinishUndo();
606 if (copySelection()) { // we need update
607 selection_start = selection_end = actpos;
608 UpdateLocal(bv, true);
609 } else if (hasSelection()) {
610 selection_start = selection_end = actpos;
611 UpdateLocal(bv, false);
615 bv->text->SetUndo(Undo::INSERT,
616 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
617 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
618 if (pasteSelection()) {
619 selection_start = selection_end = actpos;
620 UpdateLocal(bv, true);
624 bv->text->FinishUndo();
625 for(; actpos > rows[actrow].pos; --actpos)
626 cx -= SingleWidth(bv->getPainter(), par, actpos);
627 cx -= SingleWidth(bv->getPainter(), par, actpos);
628 if (hasSelection()) {
629 selection_start = selection_end = actpos;
630 UpdateLocal(bv, false);
632 selection_start = selection_end = actpos;
637 bv->text->FinishUndo();
638 int checkpos = (int)rows[actrow + 1].pos;
639 if ((actrow + 2) < (int)rows.size())
641 for(; actpos < checkpos; ++actpos)
642 cx += SingleWidth(bv->getPainter(), par, actpos);
643 if (hasSelection()) {
644 selection_start = selection_end = actpos;
645 UpdateLocal(bv, false);
647 selection_start = selection_end = actpos;
651 case LFUN_MATH_MODE: // Open or create a math inset
652 bv->text->SetUndo(Undo::INSERT,
653 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
654 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
655 InsertInset(bv, new InsetFormula);
656 if (hasSelection()) {
657 selection_start = selection_end = actpos;
658 UpdateLocal(bv, false);
660 selection_start = selection_end = actpos;
664 bv->text->SetUndo(Undo::INSERT,
665 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
666 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
667 par->InsertChar(actpos,LyXParagraph::META_NEWLINE);
668 par->SetFont(actpos,real_current_font);
669 UpdateLocal(bv, true);
671 selection_start = selection_end = actpos;
675 result = UNDISPATCHED;
678 if (result != FINISHED) {
679 if (!the_locking_inset)
682 bv->unlockInset(this);
687 int InsetText::Latex(ostream & os, signed char /*fragile*/, bool) const
690 int ret = par->SimpleTeXOnePar(os, texrow);
695 void InsetText::Validate(LaTeXFeatures & features) const
697 par->validate(features);
701 // Returns the width of a character at a certain spot
702 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
704 LyXFont font = GetFont(par, pos);
705 char c = par->GetChar(pos);
707 if (IsPrintable(c)) {
708 return font.width(c);
709 } else if (c == LyXParagraph::META_INSET) {
710 Inset const * tmpinset = par->GetInset(pos);
712 return tmpinset->width(pain, font);
715 } else if (IsSeparatorChar(c))
717 else if (IsNewlineChar(c))
719 return font.width(c);
723 // Returns the width of a character at a certain spot
724 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
725 int & asc, int & desc) const
727 LyXFont font = GetFont(par, pos);
728 char c = par->GetChar(pos);
731 if (c == LyXParagraph::META_INSET) {
732 Inset const * tmpinset=par->GetInset(pos);
734 asc = tmpinset->ascent(pain, font);
735 desc = tmpinset->descent(pain, font);
738 asc = font.maxAscent();
739 desc = font.maxDescent();
745 // Gets the fully instantiated font at a given position in a paragraph
746 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
747 // The difference is that this one is used for displaying, and thus we
748 // are allowed to make cosmetic improvements. For instance make footnotes
750 // If position is -1, we get the layout font of the paragraph.
751 // If position is -2, we get the font of the manual label of the paragraph.
752 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
754 char par_depth = par->GetDepth();
756 LyXLayout const & layout =
757 textclasslist.Style(buffer->params.textclass, par->GetLayout());
759 // We specialize the 95% common case:
760 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
763 if (layout.labeltype == LABEL_MANUAL
764 && pos < BeginningOfMainBody(par)) {
766 return par->GetFontSettings(pos).realize(layout.reslabelfont);
768 return par->GetFontSettings(pos).realize(layout.resfont);
771 // process layoutfont for pos == -1 and labelfont for pos < -1
773 return layout.resfont;
775 return layout.reslabelfont;
778 // The uncommon case need not be optimized as much
780 LyXFont layoutfont, tmpfont;
784 if (pos < BeginningOfMainBody(par)) {
786 layoutfont = layout.labelfont;
789 layoutfont = layout.font;
791 tmpfont = par->GetFontSettings(pos);
792 tmpfont.realize(layoutfont);
795 // process layoutfont for pos == -1 and labelfont for pos < -1
797 tmpfont = layout.font;
799 tmpfont = layout.labelfont;
802 // Resolve against environment font information
803 //if (par->GetDepth()){ // already in while condition
804 while (par && par_depth && !tmpfont.resolved()) {
805 par = par->DepthHook(par_depth - 1);
807 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
808 par->GetLayout()).font);
809 par_depth = par->GetDepth();
812 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
818 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
820 if (textclasslist.Style(buffer->params.textclass,
821 par->GetLayout()).labeltype != LABEL_MANUAL)
824 return par->BeginningOfMainBody();
828 void InsetText::GetCursorPos(int & x, int & y) const
835 int InsetText::InsetInInsetY()
837 if (!the_locking_inset)
841 return (y + the_locking_inset->InsetInInsetY());
845 void InsetText::ToggleInsetCursor(BufferView * bv)
847 if (the_locking_inset) {
848 the_locking_inset->ToggleInsetCursor(bv);
852 LyXFont font = GetFont(par, actpos);
854 int asc = font.maxAscent();
855 int desc = font.maxDescent();
858 bv->hideLockedInsetCursor();
860 bv->showLockedInsetCursor(cx, cy, asc, desc);
861 cursor_visible = !cursor_visible;
865 void InsetText::ShowInsetCursor(BufferView * bv)
867 if (!cursor_visible) {
868 LyXFont font = GetFont(par, actpos);
870 int asc = font.maxAscent();
871 int desc = font.maxDescent();
872 bv->fitLockedInsetCursor(cx, cy, asc, desc);
873 bv->showLockedInsetCursor(cx, cy, asc, desc);
874 cursor_visible = true;
879 void InsetText::HideInsetCursor(BufferView * bv)
882 ToggleInsetCursor(bv);
886 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
891 // search right X-pos x==0 -> top_x
895 for(unsigned int i = 1;
896 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
897 cy = rows[i].baseline;
898 actpos = rows[i].pos;
909 sw = swh = SingleWidth(bv->getPainter(), par,actpos);
910 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
912 checkpos = rows[actrow + 1].pos;
913 if ((actrow+2) < (int)rows.size())
915 while ((actpos < checkpos) && ((cx + swh) < x)) {
918 sw = swh = SingleWidth(bv->getPainter(), par,actpos);
919 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
922 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
924 static_cast<UpdatableInset*>(par->GetInset(actpos));
925 inset_x = cx - top_x;
928 the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
933 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
935 if (actpos >= par->Last())
937 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
939 static_cast<UpdatableInset*>(par->GetInset(actpos));
940 inset_x = cx - top_x;
943 the_locking_inset->Edit(bv, 0, 0, 0);
952 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
957 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
959 static_cast<UpdatableInset*>(par->GetInset(actpos));
961 inset_x = cx - top_x;
964 the_locking_inset->Edit(bv, the_locking_inset->
965 width(bv->getPainter(), GetFont(par,actpos)),
974 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
978 cy = rows[actrow - 1].baseline - top_baseline;
979 setPos(bv, cx - top_x, cy, activate_inset);
984 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
986 if (actrow >= int(rows.size() - 2))
988 cy = rows[actrow + 1].baseline - top_baseline;
989 setPos(bv, cx - top_x, cy, activate_inset);
994 void InsetText::resetPos(BufferView * bv)
999 int old_pos = actpos;
1003 for(unsigned int i = 0; (i < (rows.size()-1)) && (rows[i].pos <= actpos);
1005 cy = rows[i].baseline;
1009 setPos(bv, 0, cy, false);
1011 while(actpos < old_pos) {
1012 cx += SingleWidth(bv->getPainter(), par,actpos);
1018 bool InsetText::Delete()
1020 /* some insets are undeletable here */
1021 if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
1022 /* force complete redo when erasing display insets */
1023 /* this is a cruel method but save..... Matthias */
1024 if (par->GetInset(actpos)->Deletable() &&
1025 par->GetInset(actpos)->display()) {
1036 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1038 if (inset->Editable() == Inset::IS_EDITABLE) {
1039 UpdatableInset *i = (UpdatableInset *)inset;
1040 i->setOwner((UpdatableInset *)this);
1042 par->InsertChar(actpos, LyXParagraph::META_INSET);
1043 par->InsertInset(actpos, inset);
1044 UpdateLocal(bv, true);
1045 the_locking_inset = static_cast<UpdatableInset*>(inset);
1046 inset_x = cx - top_x;
1049 inset->Edit(bv, 0, 0, 0);
1054 UpdatableInset * InsetText::GetLockingInset()
1056 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1060 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1062 // if there is no selection just set the current_font
1063 if (!hasSelection()) {
1064 // Determine basis font
1066 if (actpos < BeginningOfMainBody(par))
1067 layoutfont = GetFont(par, -2);
1069 layoutfont = GetFont(par, -1);
1071 // Update current font
1072 real_current_font.update(font, bv->buffer()->params.language_info,
1075 // Reduce to implicit settings
1076 current_font = real_current_font;
1077 current_font.reduce(layoutfont);
1078 // And resolve it completely
1079 real_current_font.realize(layoutfont);
1084 if (selection_start > selection_end) {
1085 s_start = selection_end;
1086 s_end = selection_start;
1088 s_start = selection_start;
1089 s_end = selection_end;
1092 while(s_start < s_end) {
1093 newfont = GetFont(par,s_start);
1094 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1095 SetCharFont(s_start, newfont);
1098 UpdateLocal(bv, true);
1102 void InsetText::SetCharFont(int pos, LyXFont const & f)
1104 /* let the insets convert their font */
1107 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1108 if (par->GetInset(pos))
1109 font = par->GetInset(pos)->ConvertFont(font);
1111 LyXLayout const & layout =
1112 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1114 // Get concrete layout font to reduce against
1117 if (pos < BeginningOfMainBody(par))
1118 layoutfont = layout.labelfont;
1120 layoutfont = layout.font;
1123 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1126 // Now, reduce font against full layout font
1127 font.reduce(layoutfont);
1129 par->SetFont(pos, font);
1133 void InsetText::computeTextRows(Painter & pain, float x) const
1148 int width = wordAscent = wordDescent = 0;
1149 insetWidth = maxAscent = maxDescent = 0;
1154 rows.push_back(row);
1155 if (!autoBreakRows) {
1156 for(p = 0; p < par->Last(); ++p) {
1157 insetWidth += SingleWidth(pain, par, p);
1158 SingleHeight(pain, par, p, asc, desc);
1159 maxAscent = max(maxAscent, asc);
1160 maxDescent = max(maxDescent, desc);
1162 rows[0].asc = maxAscent;
1163 rows[0].desc = maxDescent;
1164 // alocate a dummy row for the endpos
1165 row.pos = par->Last();
1166 rows.push_back(row);
1170 bool is_first_word_in_row = true;
1172 int cw, lastWordWidth = 0;
1174 maxWidth = getMaxWidth(pain) - widthOffset;
1175 for(p = 0; p < par->Last(); ++p) {
1176 cw = SingleWidth(pain, par, p);
1178 lastWordWidth += cw;
1179 SingleHeight(pain, par, p, asc, desc);
1180 wordAscent = max(wordAscent, asc);
1181 wordDescent = max(wordDescent, desc);
1182 if (par->IsNewline(p)) {
1183 rows.back().asc = wordAscent;
1184 rows.back().desc = wordDescent;
1186 rows.push_back(row);
1187 SingleHeight(pain, par, p, oasc, odesc);
1188 width = lastWordWidth = 0;
1189 is_first_word_in_row = true;
1190 wordAscent = wordDescent = 0;
1193 Inset const * inset = 0;
1194 if (((p + 1) < par->Last()) &&
1195 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1196 inset = par->GetInset(p + 1);
1197 if (inset && inset->display()) {
1198 if (!is_first_word_in_row && (width >= (maxWidth - x))) {
1199 // we have to split also the row above
1200 rows.back().asc = oasc;
1201 rows.back().desc = odesc;
1203 rows.push_back(row);
1205 odesc = wordDescent;
1206 insetWidth = max(insetWidth, owidth);
1207 width = lastWordWidth;
1210 oasc = max(oasc, wordAscent);
1211 odesc = max(odesc, wordDescent);
1213 rows.back().asc = oasc;
1214 rows.back().desc = odesc;
1216 rows.push_back(row);
1217 SingleHeight(pain, par, p, asc, desc);
1218 rows.back().asc = asc;
1219 rows.back().desc = desc;
1220 row.pos = nwp = p + 1;
1221 rows.push_back(row);
1222 oasc = odesc = width = lastWordWidth = 0;
1223 is_first_word_in_row = true;
1224 wordAscent = wordDescent = 0;
1226 } else if (par->IsSeparator(p)) {
1227 if (width >= maxWidth - x) {
1228 if (is_first_word_in_row) {
1229 rows.back().asc = wordAscent;
1230 rows.back().desc = wordDescent;
1232 rows.push_back(row);
1233 oasc = odesc = width = 0;
1235 rows.back().asc = oasc;
1236 rows.back().desc = odesc;
1238 rows.push_back(row);
1240 odesc = wordDescent;
1241 insetWidth = max(insetWidth, owidth);
1242 width = lastWordWidth;
1244 wordAscent = wordDescent = lastWordWidth = 0;
1249 oasc = max(oasc, wordAscent);
1250 odesc = max(odesc, wordDescent);
1251 wordAscent = wordDescent = lastWordWidth = 0;
1253 is_first_word_in_row = false;
1256 // if we have some data in the paragraph we have ascent/descent
1258 if (width >= (maxWidth - x)) {
1260 rows.back().asc = oasc;
1261 rows.back().desc = odesc;
1262 // assign and allocate lower row
1264 rows.push_back(row);
1265 rows.back().asc = wordAscent;
1266 rows.back().desc = wordDescent;
1267 width -= lastWordWidth;
1269 // assign last row data
1270 rows.back().asc = max(oasc, wordAscent);
1271 rows.back().desc = max(odesc, wordDescent);
1274 insetWidth = max(insetWidth, width);
1275 // alocate a dummy row for the endpos
1276 row.pos = par->Last();
1277 rows.push_back(row);
1278 // calculate maxAscent/Descent
1279 maxAscent = rows[0].asc;
1280 maxDescent = rows[0].desc;
1281 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1282 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1285 if (the_locking_inset) {
1286 computeBaselines(top_baseline);
1289 inset_x = cx - top_x;
1296 void InsetText::computeBaselines(int baseline) const
1298 rows[0].baseline = baseline;
1299 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1300 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1301 rows[i].asc + interline_space;
1305 void InsetText::UpdateLocal(BufferView *bv, bool flag)
1307 HideInsetCursor(bv);
1309 computeTextRows(bv->painter(), xpos);
1310 computeBaselines(top_baseline);
1313 bv->updateInset(this, flag);
1314 ShowInsetCursor(bv);
1317 // this is for the simple cut and paste mechanism
1318 // this then should be a global stuff so that cut'n'paste can work in and
1319 // and outside text-insets
1320 static LyXParagraph * simple_cut_buffer = 0;
1321 // static char simple_cut_buffer_textclass = 0;
1323 // for now here this should be in another Cut&Paste Class!
1325 static void DeleteSimpleCutBuffer()
1327 if (!simple_cut_buffer)
1329 LyXParagraph * tmppar;
1331 while (simple_cut_buffer) {
1332 tmppar = simple_cut_buffer;
1333 simple_cut_buffer = simple_cut_buffer->next;
1336 simple_cut_buffer = 0;
1339 bool InsetText::cutSelection()
1341 if (!hasSelection())
1343 DeleteSimpleCutBuffer();
1345 // only within one paragraph
1346 simple_cut_buffer = new LyXParagraph;
1347 LyXParagraph::size_type i = selection_start;
1348 for (; i < selection_end; ++i) {
1349 par->CopyIntoMinibuffer(selection_start);
1350 par->Erase(selection_start);
1351 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1356 bool InsetText::copySelection()
1358 if (!hasSelection())
1360 DeleteSimpleCutBuffer();
1362 // only within one paragraph
1363 simple_cut_buffer = new LyXParagraph;
1364 LyXParagraph::size_type i = selection_start;
1365 for (; i < selection_end; ++i) {
1366 par->CopyIntoMinibuffer(i);
1367 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1372 bool InsetText::pasteSelection()
1374 if (!simple_cut_buffer)
1377 LyXParagraph * tmppar = simple_cut_buffer->Clone();
1379 while (simple_cut_buffer->size()) {
1380 simple_cut_buffer->CutIntoMinibuffer(0);
1381 simple_cut_buffer->Erase(0);
1382 par->InsertFromMinibuffer(actpos);
1385 delete simple_cut_buffer;
1386 simple_cut_buffer = tmppar;