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();
67 InsetText::InsetText(InsetText const & ins, Buffer * buf)
73 void InsetText::init(Buffer * buf, LyXParagraph *p)
78 par = new LyXParagraph(p);
79 for(int pos = 0; pos < p->Last(); ++pos) {
80 par->InsertChar(pos, p->GetChar(pos));
81 par->SetFont(pos, p->GetFontSettings(pos));
82 if ((p->GetChar(pos) == LyXParagraph::META_INSET) &&
84 par->InsertInset(pos, p->GetInset(pos)->Clone());
88 the_locking_inset = 0;
90 cursor_visible = false;
91 maxWidth = old_x = -1;
92 actpos = selection_start = selection_end = 0;
96 maxAscent = maxDescent = insetWidth = widthOffset = 0;
97 autoBreakRows = false;
102 InsetText::~InsetText()
108 Inset * InsetText::Clone() const
110 InsetText * t = new InsetText(*this, buffer);
115 void InsetText::Write(ostream & os) const
118 WriteParagraphData(os);
122 void InsetText::WriteParagraphData(ostream & os) const
124 par->writeFile(os, buffer->params, 0, 0);
128 void InsetText::Read(LyXLex & lex)
130 string token, tmptok;
132 LyXParagraph * return_par = 0;
133 char depth = 0; // signed or unsigned?
134 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
135 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
136 LyXFont font(LyXFont::ALL_INHERIT);
139 par = new LyXParagraph;
143 token = lex.GetString();
146 if (token == "\\end_inset")
148 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
152 // the_end read this should NEVER happen
153 lex.printError("\\the_end read in inset! Error in document!");
157 if (token != "\\end_inset") {
158 lex.printError("Missing \\end_inset at this point. "
165 int InsetText::ascent(Painter & pain, LyXFont const & font) const
168 computeTextRows(pain, xpos);
173 return font.maxAscent();
177 int InsetText::descent(Painter & pain, LyXFont const & font) const
180 computeTextRows(pain, xpos);
185 return font.maxDescent();
189 int InsetText::width(Painter & pain, LyXFont const &) const
192 computeTextRows(pain, xpos);
199 void InsetText::draw(Painter & pain, LyXFont const & f,
200 int baseline, float & x) const
203 computeTextRows(pain, x);
204 UpdatableInset::draw(pain, f, baseline, x);
207 top_baseline = baseline;
208 computeBaselines(baseline);
209 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
210 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
211 rows[r].baseline, x);
212 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
218 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
219 int row, int baseline, float x) const
225 if (selection_start > selection_end) {
226 s_start = selection_end;
227 s_end = selection_start;
229 s_start = selection_start;
230 s_end = selection_end;
232 if ((s_start > endpos) || (s_end < startpos))
236 int ssel_x = esel_x = int(x);
239 for(; p < endpos; ++p) {
242 if ((p >= s_start) && (p <= s_end))
244 char ch = par->GetChar(p);
245 font = GetFont(par,p);
246 if (IsFloatChar(ch)) {
248 } else if (ch == LyXParagraph::META_INSET) {
249 Inset const * tmpinset = par->GetInset(p);
250 x += tmpinset->width(pain, font);
252 x += pain.width(ch,font);
257 if ((p >= s_start) && (p <= s_end))
259 if (ssel_x < esel_x) {
260 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
261 int(esel_x - ssel_x),
262 rows[row].asc + rows[row].desc,
268 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
269 int baseline, float x) const
271 Assert(endpos <= par->Last());
273 for(int p = startpos; p < endpos; ++p) {
274 char ch = par->GetChar(p);
275 LyXFont font = GetFont(par,p);
276 if (IsFloatChar(ch)) {
278 } else if (par->IsNewline(p)) {
279 // Draw end-of-line marker
280 int wid = font.width('n');
281 int asc = font.maxAscent();
285 xp[0] = int(x + wid * 0.375);
286 yp[0] = int(y - 0.875 * asc * 0.75);
289 yp[1] = int(y - 0.500 * asc * 0.75);
291 xp[2] = int(x + wid * 0.375);
292 yp[2] = int(y - 0.125 * asc * 0.75);
294 pain.lines(xp, yp, 3, LColor::eolmarker);
297 yp[0] = int(y - 0.500 * asc * 0.75);
299 xp[1] = int(x + wid);
300 yp[1] = int(y - 0.500 * asc * 0.75);
302 xp[2] = int(x + wid);
303 yp[2] = int(y - asc * 0.75);
305 pain.lines(xp, yp, 3, LColor::eolmarker);
307 } else if (ch == LyXParagraph::META_INSET) {
308 Inset * tmpinset = par->GetInset(p);
310 tmpinset->draw(pain, font, baseline, x);
312 pain.text(int(x), baseline, ch, font);
313 x += pain.width(ch,font);
319 char const * InsetText::EditMessage() const
321 return _("Opened Text Inset");
325 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
327 UpdatableInset::Edit(bv, x, y, button);
330 the_locking_inset = 0;
331 inset_pos = inset_x = inset_y = 0;
334 selection_start = selection_end = actpos;
335 current_font = real_current_font = GetFont(par, actpos);
336 bv->text->FinishUndo();
340 void InsetText::InsetUnlock(BufferView * bv)
342 if (the_locking_inset)
343 the_locking_inset->InsetUnlock(bv);
345 if (hasSelection()) {
346 selection_start = selection_end = actpos;
347 UpdateLocal(bv, false);
349 the_locking_inset = 0;
350 no_selection = false;
354 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
356 if (!the_locking_inset)
358 if (the_locking_inset == inset) {
359 the_locking_inset->InsetUnlock(bv);
360 the_locking_inset = 0;
362 moveRight(bv, false);
365 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
369 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
371 if (!the_locking_inset)
373 if (the_locking_inset != inset)
374 return the_locking_inset->UpdateInsetInInset(bv, inset);
376 inset->draw(bv->getPainter(), real_current_font, inset_y, x);
377 UpdateLocal(bv, true);
382 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
384 if (the_locking_inset) {
385 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
388 no_selection = false;
392 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
394 if (hasSelection()) {
395 selection_start = selection_end = actpos;
396 UpdateLocal(bv, false);
398 no_selection = false;
399 if (the_locking_inset) {
400 setPos(bv, x, y, false);
403 if (par->GetChar(actpos) == LyXParagraph::META_INSET)
404 inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
405 if (the_locking_inset == inset) {
406 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
409 // otherwise unlock the_locking_inset and lock the new inset
413 the_locking_inset->InsetUnlock(bv);
414 the_locking_inset = inset;
415 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
418 // otherwise only unlock the_locking_inset
419 the_locking_inset->InsetUnlock(bv);
422 the_locking_inset = 0;
424 selection_start = selection_end = actpos;
425 if (!the_locking_inset)
430 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
432 if (the_locking_inset) {
433 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
438 int old = selection_end;
439 setPos(bv, x, y, false);
440 selection_end = actpos;
441 if (old != selection_end)
442 UpdateLocal(bv, false);
444 no_selection = false;
448 void InsetText::InsetKeyPress(XKeyEvent * xke)
450 if (the_locking_inset) {
451 the_locking_inset->InsetKeyPress(xke);
457 UpdatableInset::RESULT
458 InsetText::LocalDispatch(BufferView * bv,
459 int action, string const & arg)
461 no_selection = false;
462 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
467 UpdatableInset::RESULT
470 if ((action < 0) && arg.empty())
473 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
474 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
476 if (the_locking_inset) {
477 result = the_locking_inset->LocalDispatch(bv, action, arg);
478 if (result == DISPATCHED) {
479 the_locking_inset->ToggleInsetCursor(bv);
480 UpdateLocal(bv, false);
481 the_locking_inset->ToggleInsetCursor(bv);
483 } else if (result == FINISHED) {
484 if ((action == LFUN_RIGHT) || (action == -1)) {
485 actpos = inset_pos + 1;
488 the_locking_inset = 0;
496 bv->text->SetUndo(Undo::INSERT,
497 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
498 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
500 actpos = selection_start;
501 par->InsertChar(actpos,arg[0]);
502 par->SetFont(actpos,real_current_font);
504 selection_start = selection_end = actpos;
505 UpdateLocal(bv, true);
507 // --- Cursor Movements ---------------------------------------------
509 bv->text->FinishUndo();
510 moveRight(bv, false);
511 selection_end = actpos;
512 UpdateLocal(bv, false);
515 bv->text->FinishUndo();
516 result= DISPATCH_RESULT(moveRight(bv));
517 if (hasSelection()) {
518 selection_start = selection_end = actpos;
519 UpdateLocal(bv, false);
521 selection_start = selection_end = actpos;
525 bv->text->FinishUndo();
527 selection_end = actpos;
528 UpdateLocal(bv, false);
531 bv->text->FinishUndo();
532 result= DISPATCH_RESULT(moveLeft(bv));
533 if (hasSelection()) {
534 selection_start = selection_end = actpos;
535 UpdateLocal(bv, false);
537 selection_start = selection_end = actpos;
541 bv->text->FinishUndo();
543 selection_end = actpos;
544 UpdateLocal(bv, false);
547 bv->text->FinishUndo();
548 result= DISPATCH_RESULT(moveDown(bv));
549 if (hasSelection()) {
550 selection_start = selection_end = actpos;
551 UpdateLocal(bv, false);
553 selection_start = selection_end = actpos;
557 bv->text->FinishUndo();
559 selection_end = actpos;
560 UpdateLocal(bv, false);
563 bv->text->FinishUndo();
564 result= DISPATCH_RESULT(moveUp(bv));
565 if (hasSelection()) {
566 selection_start = selection_end = actpos;
567 UpdateLocal(bv, false);
569 selection_start = selection_end = actpos;
573 if (!actpos) { // || par->IsNewline(actpos-1)) {
574 if (hasSelection()) {
575 selection_start = selection_end = actpos;
576 UpdateLocal(bv, false);
583 bv->text->SetUndo(Undo::DELETE,
584 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
585 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
587 ret = cutSelection();
590 if (ret) { // we need update
591 selection_start = selection_end = actpos;
592 UpdateLocal(bv, true);
593 } else if (hasSelection()) {
594 selection_start = selection_end = actpos;
595 UpdateLocal(bv, false);
599 bv->text->SetUndo(Undo::DELETE,
600 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
601 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
602 if (cutSelection()) { // we need update
603 actpos = selection_end = selection_start;
604 UpdateLocal(bv, true);
605 } else if (hasSelection()) {
606 selection_start = selection_end = actpos;
607 UpdateLocal(bv, false);
611 bv->text->FinishUndo();
612 if (copySelection()) { // we need update
613 selection_start = selection_end = actpos;
614 UpdateLocal(bv, true);
615 } else if (hasSelection()) {
616 selection_start = selection_end = actpos;
617 UpdateLocal(bv, false);
621 bv->text->SetUndo(Undo::INSERT,
622 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
623 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
624 if (pasteSelection()) {
625 selection_start = selection_end = actpos;
626 UpdateLocal(bv, true);
630 bv->text->FinishUndo();
631 for(; actpos > rows[actrow].pos; --actpos)
632 cx -= SingleWidth(bv->getPainter(), par, actpos);
633 cx -= SingleWidth(bv->getPainter(), par, actpos);
634 if (hasSelection()) {
635 selection_start = selection_end = actpos;
636 UpdateLocal(bv, false);
638 selection_start = selection_end = actpos;
643 bv->text->FinishUndo();
644 int checkpos = (int)rows[actrow + 1].pos;
645 if ((actrow + 2) < (int)rows.size())
647 for(; actpos < checkpos; ++actpos)
648 cx += SingleWidth(bv->getPainter(), par, actpos);
649 if (hasSelection()) {
650 selection_start = selection_end = actpos;
651 UpdateLocal(bv, false);
653 selection_start = selection_end = actpos;
657 case LFUN_MATH_MODE: // Open or create a math inset
658 bv->text->SetUndo(Undo::INSERT,
659 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
660 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
661 InsertInset(bv, new InsetFormula);
662 if (hasSelection()) {
663 selection_start = selection_end = actpos;
664 UpdateLocal(bv, false);
666 selection_start = selection_end = actpos;
670 bv->text->SetUndo(Undo::INSERT,
671 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
672 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
673 par->InsertChar(actpos,LyXParagraph::META_NEWLINE);
674 par->SetFont(actpos,real_current_font);
675 UpdateLocal(bv, true);
677 selection_start = selection_end = actpos;
681 result = UNDISPATCHED;
684 if (result != FINISHED) {
685 if (!the_locking_inset)
688 bv->unlockInset(this);
693 int InsetText::Latex(ostream & os, signed char /*fragile*/, bool) const
696 int ret = par->SimpleTeXOnePar(os, texrow);
701 void InsetText::Validate(LaTeXFeatures & features) const
703 par->validate(features);
707 // Returns the width of a character at a certain spot
708 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
710 LyXFont font = GetFont(par, pos);
711 char c = par->GetChar(pos);
713 if (IsPrintable(c)) {
714 return font.width(c);
715 } else if (c == LyXParagraph::META_INSET) {
716 Inset const * tmpinset = par->GetInset(pos);
718 return tmpinset->width(pain, font);
721 } else if (IsSeparatorChar(c))
723 else if (IsNewlineChar(c))
725 return font.width(c);
729 // Returns the width of a character at a certain spot
730 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
731 int & asc, int & desc) const
733 LyXFont font = GetFont(par, pos);
734 char c = par->GetChar(pos);
737 if (c == LyXParagraph::META_INSET) {
738 Inset const * tmpinset=par->GetInset(pos);
740 asc = tmpinset->ascent(pain, font);
741 desc = tmpinset->descent(pain, font);
744 asc = font.maxAscent();
745 desc = font.maxDescent();
751 // Gets the fully instantiated font at a given position in a paragraph
752 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
753 // The difference is that this one is used for displaying, and thus we
754 // are allowed to make cosmetic improvements. For instance make footnotes
756 // If position is -1, we get the layout font of the paragraph.
757 // If position is -2, we get the font of the manual label of the paragraph.
758 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
760 char par_depth = par->GetDepth();
762 LyXLayout const & layout =
763 textclasslist.Style(buffer->params.textclass, par->GetLayout());
765 // We specialize the 95% common case:
766 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
769 if (layout.labeltype == LABEL_MANUAL
770 && pos < BeginningOfMainBody(par)) {
772 return par->GetFontSettings(pos).realize(layout.reslabelfont);
774 return par->GetFontSettings(pos).realize(layout.resfont);
777 // process layoutfont for pos == -1 and labelfont for pos < -1
779 return layout.resfont;
781 return layout.reslabelfont;
784 // The uncommon case need not be optimized as much
786 LyXFont layoutfont, tmpfont;
790 if (pos < BeginningOfMainBody(par)) {
792 layoutfont = layout.labelfont;
795 layoutfont = layout.font;
797 tmpfont = par->GetFontSettings(pos);
798 tmpfont.realize(layoutfont);
801 // process layoutfont for pos == -1 and labelfont for pos < -1
803 tmpfont = layout.font;
805 tmpfont = layout.labelfont;
808 // Resolve against environment font information
809 //if (par->GetDepth()){ // already in while condition
810 while (par && par_depth && !tmpfont.resolved()) {
811 par = par->DepthHook(par_depth - 1);
813 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
814 par->GetLayout()).font);
815 par_depth = par->GetDepth();
818 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
824 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
826 if (textclasslist.Style(buffer->params.textclass,
827 par->GetLayout()).labeltype != LABEL_MANUAL)
830 return par->BeginningOfMainBody();
834 void InsetText::GetCursorPos(int & x, int & y) const
841 int InsetText::InsetInInsetY()
843 if (!the_locking_inset)
847 return (y + the_locking_inset->InsetInInsetY());
851 void InsetText::ToggleInsetCursor(BufferView * bv)
853 if (the_locking_inset) {
854 the_locking_inset->ToggleInsetCursor(bv);
858 LyXFont font = GetFont(par, actpos);
860 int asc = font.maxAscent();
861 int desc = font.maxDescent();
864 bv->hideLockedInsetCursor();
866 bv->showLockedInsetCursor(cx, cy, asc, desc);
867 cursor_visible = !cursor_visible;
871 void InsetText::ShowInsetCursor(BufferView * bv)
873 if (!cursor_visible) {
874 LyXFont font = GetFont(par, actpos);
876 int asc = font.maxAscent();
877 int desc = font.maxDescent();
878 bv->fitLockedInsetCursor(cx, cy, asc, desc);
879 bv->showLockedInsetCursor(cx, cy, asc, desc);
880 cursor_visible = true;
885 void InsetText::HideInsetCursor(BufferView * bv)
888 ToggleInsetCursor(bv);
892 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
897 // search right X-pos x==0 -> top_x
901 for(unsigned int i = 1;
902 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
903 cy = rows[i].baseline;
904 actpos = rows[i].pos;
915 sw = swh = SingleWidth(bv->getPainter(), par,actpos);
916 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
918 checkpos = rows[actrow + 1].pos;
919 if ((actrow+2) < (int)rows.size())
921 while ((actpos < checkpos) && ((cx + swh) < x)) {
924 sw = swh = SingleWidth(bv->getPainter(), par,actpos);
925 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
928 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
930 static_cast<UpdatableInset*>(par->GetInset(actpos));
931 inset_x = cx - top_x;
934 the_locking_inset->Edit(bv, ox - inset_x, oy - inset_y, 0);
939 bool InsetText::moveRight(BufferView * bv, bool activate_inset)
941 if (actpos >= par->Last())
943 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
945 static_cast<UpdatableInset*>(par->GetInset(actpos));
946 inset_x = cx - top_x;
949 the_locking_inset->Edit(bv, 0, 0, 0);
958 bool InsetText::moveLeft(BufferView * bv, bool activate_inset)
963 if (activate_inset && par->GetChar(actpos)==LyXParagraph::META_INSET) {
965 static_cast<UpdatableInset*>(par->GetInset(actpos));
967 inset_x = cx - top_x;
970 the_locking_inset->Edit(bv, the_locking_inset->
971 width(bv->getPainter(), GetFont(par,actpos)),
980 bool InsetText::moveUp(BufferView * bv, bool activate_inset)
984 cy = rows[actrow - 1].baseline - top_baseline;
985 setPos(bv, cx - top_x, cy, activate_inset);
990 bool InsetText::moveDown(BufferView * bv, bool activate_inset)
992 if (actrow >= int(rows.size() - 2))
994 cy = rows[actrow + 1].baseline - top_baseline;
995 setPos(bv, cx - top_x, cy, activate_inset);
1000 void InsetText::resetPos(BufferView * bv)
1005 int old_pos = actpos;
1009 for(unsigned int i = 0; (i < (rows.size()-1)) && (rows[i].pos <= actpos);
1011 cy = rows[i].baseline;
1015 setPos(bv, 0, cy, false);
1017 while(actpos < old_pos) {
1018 cx += SingleWidth(bv->getPainter(), par,actpos);
1024 bool InsetText::Delete()
1026 /* some insets are undeletable here */
1027 if (par->GetChar(actpos)==LyXParagraph::META_INSET) {
1028 /* force complete redo when erasing display insets */
1029 /* this is a cruel method but save..... Matthias */
1030 if (par->GetInset(actpos)->Deletable() &&
1031 par->GetInset(actpos)->display()) {
1042 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1044 if (inset->Editable() == Inset::IS_EDITABLE) {
1045 UpdatableInset *i = (UpdatableInset *)inset;
1046 i->setOwner((UpdatableInset *)this);
1048 par->InsertChar(actpos, LyXParagraph::META_INSET);
1049 par->InsertInset(actpos, inset);
1050 UpdateLocal(bv, true);
1051 the_locking_inset = static_cast<UpdatableInset*>(inset);
1052 inset_x = cx - top_x;
1055 inset->Edit(bv, 0, 0, 0);
1060 UpdatableInset * InsetText::GetLockingInset()
1062 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1066 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1068 // if there is no selection just set the current_font
1069 if (!hasSelection()) {
1070 // Determine basis font
1072 if (actpos < BeginningOfMainBody(par))
1073 layoutfont = GetFont(par, -2);
1075 layoutfont = GetFont(par, -1);
1077 // Update current font
1078 real_current_font.update(font, bv->buffer()->params.language_info,
1081 // Reduce to implicit settings
1082 current_font = real_current_font;
1083 current_font.reduce(layoutfont);
1084 // And resolve it completely
1085 real_current_font.realize(layoutfont);
1090 if (selection_start > selection_end) {
1091 s_start = selection_end;
1092 s_end = selection_start;
1094 s_start = selection_start;
1095 s_end = selection_end;
1098 while(s_start < s_end) {
1099 newfont = GetFont(par,s_start);
1100 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1101 SetCharFont(s_start, newfont);
1104 UpdateLocal(bv, true);
1108 void InsetText::SetCharFont(int pos, LyXFont const & f)
1110 /* let the insets convert their font */
1113 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1114 if (par->GetInset(pos))
1115 font = par->GetInset(pos)->ConvertFont(font);
1117 LyXLayout const & layout =
1118 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1120 // Get concrete layout font to reduce against
1123 if (pos < BeginningOfMainBody(par))
1124 layoutfont = layout.labelfont;
1126 layoutfont = layout.font;
1129 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1132 // Now, reduce font against full layout font
1133 font.reduce(layoutfont);
1135 par->SetFont(pos, font);
1139 void InsetText::computeTextRows(Painter & pain, float x) const
1154 int width = wordAscent = wordDescent = 0;
1155 insetWidth = maxAscent = maxDescent = 0;
1160 rows.push_back(row);
1161 if (!autoBreakRows) {
1162 for(p = 0; p < par->Last(); ++p) {
1163 insetWidth += SingleWidth(pain, par, p);
1164 SingleHeight(pain, par, p, asc, desc);
1165 maxAscent = max(maxAscent, asc);
1166 maxDescent = max(maxDescent, desc);
1168 rows[0].asc = maxAscent;
1169 rows[0].desc = maxDescent;
1170 // alocate a dummy row for the endpos
1171 row.pos = par->Last();
1172 rows.push_back(row);
1176 bool is_first_word_in_row = true;
1178 int cw, lastWordWidth = 0;
1180 maxWidth = getMaxWidth(pain) - widthOffset;
1181 for(p = 0; p < par->Last(); ++p) {
1182 cw = SingleWidth(pain, par, p);
1184 lastWordWidth += cw;
1185 SingleHeight(pain, par, p, asc, desc);
1186 wordAscent = max(wordAscent, asc);
1187 wordDescent = max(wordDescent, desc);
1188 if (par->IsNewline(p)) {
1189 rows.back().asc = wordAscent;
1190 rows.back().desc = wordDescent;
1192 rows.push_back(row);
1193 SingleHeight(pain, par, p, oasc, odesc);
1194 width = lastWordWidth = 0;
1195 is_first_word_in_row = true;
1196 wordAscent = wordDescent = 0;
1199 Inset const * inset = 0;
1200 if (((p + 1) < par->Last()) &&
1201 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1202 inset = par->GetInset(p + 1);
1203 if (inset && inset->display()) {
1204 if (!is_first_word_in_row && (width >= (maxWidth - x))) {
1205 // we have to split also the row above
1206 rows.back().asc = oasc;
1207 rows.back().desc = odesc;
1209 rows.push_back(row);
1211 odesc = wordDescent;
1212 insetWidth = max(insetWidth, owidth);
1213 width = lastWordWidth;
1216 oasc = max(oasc, wordAscent);
1217 odesc = max(odesc, wordDescent);
1219 rows.back().asc = oasc;
1220 rows.back().desc = odesc;
1222 rows.push_back(row);
1223 SingleHeight(pain, par, p, asc, desc);
1224 rows.back().asc = asc;
1225 rows.back().desc = desc;
1226 row.pos = nwp = p + 1;
1227 rows.push_back(row);
1228 oasc = odesc = width = lastWordWidth = 0;
1229 is_first_word_in_row = true;
1230 wordAscent = wordDescent = 0;
1232 } else if (par->IsSeparator(p)) {
1233 if (width >= maxWidth - x) {
1234 if (is_first_word_in_row) {
1235 rows.back().asc = wordAscent;
1236 rows.back().desc = wordDescent;
1238 rows.push_back(row);
1239 oasc = odesc = width = 0;
1241 rows.back().asc = oasc;
1242 rows.back().desc = odesc;
1244 rows.push_back(row);
1246 odesc = wordDescent;
1247 insetWidth = max(insetWidth, owidth);
1248 width = lastWordWidth;
1250 wordAscent = wordDescent = lastWordWidth = 0;
1255 oasc = max(oasc, wordAscent);
1256 odesc = max(odesc, wordDescent);
1257 wordAscent = wordDescent = lastWordWidth = 0;
1259 is_first_word_in_row = false;
1262 // if we have some data in the paragraph we have ascent/descent
1264 if (width >= (maxWidth - x)) {
1266 rows.back().asc = oasc;
1267 rows.back().desc = odesc;
1268 // assign and allocate lower row
1270 rows.push_back(row);
1271 rows.back().asc = wordAscent;
1272 rows.back().desc = wordDescent;
1273 width -= lastWordWidth;
1275 // assign last row data
1276 rows.back().asc = max(oasc, wordAscent);
1277 rows.back().desc = max(odesc, wordDescent);
1280 insetWidth = max(insetWidth, width);
1281 // alocate a dummy row for the endpos
1282 row.pos = par->Last();
1283 rows.push_back(row);
1284 // calculate maxAscent/Descent
1285 maxAscent = rows[0].asc;
1286 maxDescent = rows[0].desc;
1287 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1288 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1291 if (the_locking_inset) {
1292 computeBaselines(top_baseline);
1295 inset_x = cx - top_x;
1302 void InsetText::computeBaselines(int baseline) const
1304 rows[0].baseline = baseline;
1305 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1306 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1307 rows[i].asc + interline_space;
1311 void InsetText::UpdateLocal(BufferView *bv, bool flag)
1313 HideInsetCursor(bv);
1315 computeTextRows(bv->painter(), xpos);
1316 computeBaselines(top_baseline);
1319 bv->updateInset(this, flag);
1320 ShowInsetCursor(bv);
1323 // this is for the simple cut and paste mechanism
1324 // this then should be a global stuff so that cut'n'paste can work in and
1325 // and outside text-insets
1326 static LyXParagraph * simple_cut_buffer = 0;
1327 // static char simple_cut_buffer_textclass = 0;
1329 // for now here this should be in another Cut&Paste Class!
1331 static void DeleteSimpleCutBuffer()
1333 if (!simple_cut_buffer)
1335 LyXParagraph * tmppar;
1337 while (simple_cut_buffer) {
1338 tmppar = simple_cut_buffer;
1339 simple_cut_buffer = simple_cut_buffer->next;
1342 simple_cut_buffer = 0;
1345 bool InsetText::cutSelection()
1347 if (!hasSelection())
1349 DeleteSimpleCutBuffer();
1351 // only within one paragraph
1352 simple_cut_buffer = new LyXParagraph;
1353 LyXParagraph::size_type i = selection_start;
1354 for (; i < selection_end; ++i) {
1355 par->CopyIntoMinibuffer(selection_start);
1356 par->Erase(selection_start);
1357 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1362 bool InsetText::copySelection()
1364 if (!hasSelection())
1366 DeleteSimpleCutBuffer();
1368 // only within one paragraph
1369 simple_cut_buffer = new LyXParagraph;
1370 LyXParagraph::size_type i = selection_start;
1371 for (; i < selection_end; ++i) {
1372 par->CopyIntoMinibuffer(i);
1373 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1378 bool InsetText::pasteSelection()
1380 if (!simple_cut_buffer)
1383 LyXParagraph * tmppar = simple_cut_buffer->Clone();
1385 while (simple_cut_buffer->size()) {
1386 simple_cut_buffer->CutIntoMinibuffer(0);
1387 simple_cut_buffer->Erase(0);
1388 par->InsertFromMinibuffer(actpos);
1391 delete simple_cut_buffer;
1392 simple_cut_buffer = tmppar;