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"
61 extern unsigned char getCurrentTextClass(Buffer *);
63 InsetText::InsetText(Buffer * buf)
65 par = new LyXParagraph();
71 InsetText::InsetText(InsetText const & ins, Buffer * buf)
78 void InsetText::init(Buffer * buf, LyXParagraph *p)
85 the_locking_inset = 0;
87 cursor_visible = false;
88 maxWidth = old_x = -1;
89 actpos = selection_start = selection_end = 0;
93 maxAscent = maxDescent = insetWidth = 0;
94 autoBreakRows = false;
99 InsetText::~InsetText()
105 Inset * InsetText::Clone() const
107 InsetText * t = new InsetText(*this, buffer);
112 void InsetText::Write(ostream & os) const
115 WriteParagraphData(os);
119 void InsetText::WriteParagraphData(ostream & os) const
121 par->writeFile(os, buffer->params, 0, 0);
125 void InsetText::Read(LyXLex & lex)
127 string token, tmptok;
129 LyXParagraph * return_par = 0;
130 char depth = 0; // signed or unsigned?
131 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
132 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
133 LyXFont font(LyXFont::ALL_INHERIT);
136 par = new LyXParagraph;
140 token = lex.GetString();
143 if (token == "\\end_inset")
145 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
149 // the_end read this should NEVER happen
150 lex.printError("\\the_end read in inset! Error in document!");
154 if (token != "\\end_inset") {
155 lex.printError("Missing \\end_inset at this point. "
162 int InsetText::ascent(Painter & pain, LyXFont const & font) const
165 computeTextRows(pain, xpos);
170 return lyxfont::maxAscent(font);
174 int InsetText::descent(Painter & pain, LyXFont const & font) const
177 computeTextRows(pain, xpos);
182 return lyxfont::maxDescent(font);
186 int InsetText::width(Painter & pain, LyXFont const &) const
189 computeTextRows(pain, xpos);
196 void InsetText::draw(Painter & pain, LyXFont const & f,
197 int baseline, float & x) const
200 computeTextRows(pain, x);
201 UpdatableInset::draw(pain, f, baseline, x);
204 top_baseline = baseline;
205 computeBaselines(baseline);
206 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
207 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
208 rows[r].baseline, x);
209 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
215 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
216 int row, int baseline, float x) const
222 if (selection_start > selection_end) {
223 s_start = selection_end;
224 s_end = selection_start;
226 s_start = selection_start;
227 s_end = selection_end;
229 if ((s_start > endpos) || (s_end < startpos))
233 int ssel_x = esel_x = int(x);
236 for(; p < endpos; ++p) {
239 if ((p >= s_start) && (p <= s_end))
241 char ch = par->GetChar(p);
242 font = GetFont(par,p);
243 if (IsFloatChar(ch)) {
245 } else if (ch == LyXParagraph::META_INSET) {
246 Inset const * tmpinset = par->GetInset(p);
247 x += tmpinset->width(pain, font);
249 x += lyxfont::width(ch, font);
254 if ((p >= s_start) && (p <= s_end))
256 if (ssel_x < esel_x) {
257 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
258 int(esel_x - ssel_x),
259 rows[row].asc + rows[row].desc,
265 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
266 int baseline, float x) const
268 Assert(endpos <= par->Last());
270 for(int p = startpos; p < endpos; ++p) {
271 char ch = par->GetChar(p);
272 LyXFont font = GetFont(par,p);
273 if (IsFloatChar(ch)) {
275 } else if (par->IsNewline(p)) {
276 // Draw end-of-line marker
277 int wid = lyxfont::width('n', font);
278 int asc = lyxfont::maxAscent(font);
282 xp[0] = int(x + wid * 0.375);
283 yp[0] = int(y - 0.875 * asc * 0.75);
286 yp[1] = int(y - 0.500 * asc * 0.75);
288 xp[2] = int(x + wid * 0.375);
289 yp[2] = int(y - 0.125 * asc * 0.75);
291 pain.lines(xp, yp, 3, LColor::eolmarker);
294 yp[0] = int(y - 0.500 * asc * 0.75);
296 xp[1] = int(x + wid);
297 yp[1] = int(y - 0.500 * asc * 0.75);
299 xp[2] = int(x + wid);
300 yp[2] = int(y - asc * 0.75);
302 pain.lines(xp, yp, 3, LColor::eolmarker);
304 } else if (ch == LyXParagraph::META_INSET) {
305 Inset * tmpinset = par->GetInset(p);
307 tmpinset->draw(pain, font, baseline, x);
309 pain.text(int(x), baseline, ch, font);
310 x += lyxfont::width(ch, font);
316 char const * InsetText::EditMessage() const
318 return _("Opened Text Inset");
322 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
324 UpdatableInset::Edit(bv, x, y, button);
327 the_locking_inset = 0;
328 inset_pos = inset_x = inset_y = 0;
331 selection_start = selection_end = actpos;
332 current_font = real_current_font = GetFont(par, actpos);
333 bv->text->FinishUndo();
337 void InsetText::InsetUnlock(BufferView * bv)
339 if (the_locking_inset)
340 the_locking_inset->InsetUnlock(bv);
342 if (hasSelection()) {
343 selection_start = selection_end = actpos;
344 UpdateLocal(bv, false);
346 the_locking_inset = 0;
347 no_selection = false;
351 bool InsetText::UnlockInsetInInset(BufferView * bv, Inset * inset, bool lr)
353 if (!the_locking_inset)
355 if (the_locking_inset == inset) {
356 the_locking_inset->InsetUnlock(bv);
357 the_locking_inset = 0;
359 moveRight(bv, false);
362 return the_locking_inset->UnlockInsetInInset(bv, inset,lr);
366 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
368 if (!the_locking_inset)
370 if (the_locking_inset != inset)
371 return the_locking_inset->UpdateInsetInInset(bv, inset);
373 inset->draw(bv->painter(), real_current_font, inset_y, x);
374 UpdateLocal(bv, true);
379 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
381 if (the_locking_inset) {
382 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
385 no_selection = false;
389 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
391 if (hasSelection()) {
392 selection_start = selection_end = actpos;
393 UpdateLocal(bv, false);
395 no_selection = false;
396 if (the_locking_inset) {
397 setPos(bv, x, y, false);
400 if (par->GetChar(actpos) == LyXParagraph::META_INSET)
401 inset = static_cast<UpdatableInset*>(par->GetInset(actpos));
402 if (the_locking_inset == inset) {
403 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
406 // otherwise unlock the_locking_inset and lock the new inset
410 the_locking_inset->InsetUnlock(bv);
411 the_locking_inset = inset;
412 the_locking_inset->Edit(bv, x - inset_x, y - inset_y, button);
415 // otherwise only unlock the_locking_inset
416 the_locking_inset->InsetUnlock(bv);
419 the_locking_inset = 0;
421 selection_start = selection_end = actpos;
422 if (!the_locking_inset)
427 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int button)
429 if (the_locking_inset) {
430 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
435 int old = selection_end;
436 setPos(bv, x, y, false);
437 selection_end = actpos;
438 if (old != selection_end)
439 UpdateLocal(bv, false);
441 no_selection = false;
445 void InsetText::InsetKeyPress(XKeyEvent * xke)
447 if (the_locking_inset) {
448 the_locking_inset->InsetKeyPress(xke);
454 UpdatableInset::RESULT
455 InsetText::LocalDispatch(BufferView * bv,
456 int action, string const & arg)
458 no_selection = false;
459 if (UpdatableInset::LocalDispatch(bv, action, arg)) {
464 UpdatableInset::RESULT
467 if ((action < 0) && arg.empty())
470 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
471 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
473 if (the_locking_inset) {
474 result = the_locking_inset->LocalDispatch(bv, action, arg);
475 if (result == DISPATCHED) {
476 the_locking_inset->ToggleInsetCursor(bv);
477 UpdateLocal(bv, false);
478 the_locking_inset->ToggleInsetCursor(bv);
480 } else if (result == FINISHED) {
481 if ((action == LFUN_RIGHT) || (action == -1)) {
482 actpos = inset_pos + 1;
485 the_locking_inset = 0;
493 bv->text->SetUndo(Undo::INSERT,
494 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
495 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
497 actpos = selection_start;
498 par->InsertChar(actpos,arg[0]);
499 SetCharFont(actpos,current_font);
501 selection_start = selection_end = actpos;
502 UpdateLocal(bv, true);
504 // --- Cursor Movements ---------------------------------------------
506 bv->text->FinishUndo();
507 moveRight(bv, false);
508 selection_end = actpos;
509 UpdateLocal(bv, false);
512 bv->text->FinishUndo();
513 result= DISPATCH_RESULT(moveRight(bv));
514 if (hasSelection()) {
515 selection_start = selection_end = actpos;
516 UpdateLocal(bv, false);
518 selection_start = selection_end = actpos;
522 bv->text->FinishUndo();
524 selection_end = actpos;
525 UpdateLocal(bv, false);
528 bv->text->FinishUndo();
529 result= DISPATCH_RESULT(moveLeft(bv));
530 if (hasSelection()) {
531 selection_start = selection_end = actpos;
532 UpdateLocal(bv, false);
534 selection_start = selection_end = actpos;
538 bv->text->FinishUndo();
540 selection_end = actpos;
541 UpdateLocal(bv, false);
544 bv->text->FinishUndo();
545 result= DISPATCH_RESULT(moveDown(bv));
546 if (hasSelection()) {
547 selection_start = selection_end = actpos;
548 UpdateLocal(bv, false);
550 selection_start = selection_end = actpos;
554 bv->text->FinishUndo();
556 selection_end = actpos;
557 UpdateLocal(bv, false);
560 bv->text->FinishUndo();
561 result= DISPATCH_RESULT(moveUp(bv));
562 if (hasSelection()) {
563 selection_start = selection_end = actpos;
564 UpdateLocal(bv, false);
566 selection_start = selection_end = actpos;
570 if (!actpos) { // || par->IsNewline(actpos-1)) {
571 if (hasSelection()) {
572 selection_start = selection_end = actpos;
573 UpdateLocal(bv, false);
580 bv->text->SetUndo(Undo::DELETE,
581 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
582 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
584 ret = cutSelection();
587 if (ret) { // we need update
588 selection_start = selection_end = actpos;
589 UpdateLocal(bv, true);
590 } else if (hasSelection()) {
591 selection_start = selection_end = actpos;
592 UpdateLocal(bv, false);
596 bv->text->SetUndo(Undo::DELETE,
597 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
598 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
599 if (cutSelection()) { // we need update
600 actpos = selection_end = selection_start;
601 UpdateLocal(bv, true);
602 } else if (hasSelection()) {
603 selection_start = selection_end = actpos;
604 UpdateLocal(bv, false);
608 bv->text->FinishUndo();
609 if (copySelection()) { // we need update
610 selection_start = selection_end = actpos;
611 UpdateLocal(bv, true);
612 } else if (hasSelection()) {
613 selection_start = selection_end = actpos;
614 UpdateLocal(bv, false);
618 bv->text->SetUndo(Undo::INSERT,
619 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
620 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
621 if (pasteSelection()) {
622 selection_start = selection_end = actpos;
623 UpdateLocal(bv, true);
627 bv->text->FinishUndo();
628 for(; actpos > rows[actrow].pos; --actpos)
629 cx -= SingleWidth(bv->painter(), par, actpos);
630 cx -= SingleWidth(bv->painter(), par, actpos);
631 if (hasSelection()) {
632 selection_start = selection_end = actpos;
633 UpdateLocal(bv, false);
635 selection_start = selection_end = actpos;
640 bv->text->FinishUndo();
641 int checkpos = (int)rows[actrow + 1].pos;
642 if ((actrow + 2) < (int)rows.size())
644 for(; actpos < checkpos; ++actpos)
645 cx += SingleWidth(bv->painter(), par, actpos);
646 if (hasSelection()) {
647 selection_start = selection_end = actpos;
648 UpdateLocal(bv, false);
650 selection_start = selection_end = actpos;
654 case LFUN_MATH_MODE: // Open or create a math inset
655 bv->text->SetUndo(Undo::INSERT,
656 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
657 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
658 InsertInset(bv, new InsetFormula);
659 if (hasSelection()) {
660 selection_start = selection_end = actpos;
661 UpdateLocal(bv, false);
663 selection_start = selection_end = actpos;
667 bv->text->SetUndo(Undo::INSERT,
668 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
669 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
670 par->InsertChar(actpos,LyXParagraph::META_NEWLINE);
671 SetCharFont(actpos,current_font);
672 UpdateLocal(bv, true);
674 selection_start = selection_end = actpos;
678 result = UNDISPATCHED;
681 if (result != FINISHED) {
682 if (!the_locking_inset)
685 bv->unlockInset(this);
690 int InsetText::Latex(ostream & os, signed char /*fragile*/, bool) const
693 int ret = par->SimpleTeXOnePar(os, texrow);
698 void InsetText::Validate(LaTeXFeatures & features) const
700 par->validate(features);
704 // Returns the width of a character at a certain spot
705 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
707 LyXFont font = GetFont(par, pos);
708 char c = par->GetChar(pos);
710 if (IsPrintable(c)) {
711 return lyxfont::width(c, font);
712 } else if (c == LyXParagraph::META_INSET) {
713 Inset const * tmpinset = par->GetInset(pos);
715 return tmpinset->width(pain, font);
718 } else if (IsSeparatorChar(c))
720 else if (IsNewlineChar(c))
722 return lyxfont::width(c, font);
726 // Returns the width of a character at a certain spot
727 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
728 int & asc, int & desc) const
730 LyXFont font = GetFont(par, pos);
731 char c = par->GetChar(pos);
734 if (c == LyXParagraph::META_INSET) {
735 Inset const * tmpinset=par->GetInset(pos);
737 asc = tmpinset->ascent(pain, font);
738 desc = tmpinset->descent(pain, font);
741 asc = lyxfont::maxAscent(font);
742 desc = lyxfont::maxDescent(font);
748 // Gets the fully instantiated font at a given position in a paragraph
749 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
750 // The difference is that this one is used for displaying, and thus we
751 // are allowed to make cosmetic improvements. For instance make footnotes
753 // If position is -1, we get the layout font of the paragraph.
754 // If position is -2, we get the font of the manual label of the paragraph.
755 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
757 char par_depth = par->GetDepth();
759 LyXLayout const & layout =
760 textclasslist.Style(buffer->params.textclass, par->GetLayout());
762 // We specialize the 95% common case:
763 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
766 if (layout.labeltype == LABEL_MANUAL
767 && pos < BeginningOfMainBody(par)) {
769 return par->GetFontSettings(pos).realize(layout.reslabelfont);
771 return par->GetFontSettings(pos).realize(layout.resfont);
774 // process layoutfont for pos == -1 and labelfont for pos < -1
776 return layout.resfont;
778 return layout.reslabelfont;
781 // The uncommon case need not be optimized as much
783 LyXFont layoutfont, tmpfont;
787 if (pos < BeginningOfMainBody(par)) {
789 layoutfont = layout.labelfont;
792 layoutfont = layout.font;
794 tmpfont = par->GetFontSettings(pos);
795 tmpfont.realize(layoutfont);
798 // process layoutfont for pos == -1 and labelfont for pos < -1
800 tmpfont = layout.font;
802 tmpfont = layout.labelfont;
805 // Resolve against environment font information
806 //if (par->GetDepth()){ // already in while condition
807 while (par && par_depth && !tmpfont.resolved()) {
808 par = par->DepthHook(par_depth - 1);
810 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
811 par->GetLayout()).font);
812 par_depth = par->GetDepth();
815 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
821 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
823 if (textclasslist.Style(buffer->params.textclass,
824 par->GetLayout()).labeltype != LABEL_MANUAL)
827 return par->BeginningOfMainBody();
831 void InsetText::GetCursorPos(int & x, int & y) const
838 int InsetText::InsetInInsetY()
840 if (!the_locking_inset)
844 return (y + the_locking_inset->InsetInInsetY());
848 void InsetText::ToggleInsetCursor(BufferView * bv)
850 if (the_locking_inset) {
851 the_locking_inset->ToggleInsetCursor(bv);
855 LyXFont font = GetFont(par, actpos);
857 int asc = lyxfont::maxAscent(font);
858 int desc = lyxfont::maxDescent(font);
861 bv->hideLockedInsetCursor();
863 bv->showLockedInsetCursor(cx, cy, asc, desc);
864 cursor_visible = !cursor_visible;
868 void InsetText::ShowInsetCursor(BufferView * bv)
870 if (!cursor_visible) {
871 LyXFont font = GetFont(par, actpos);
873 int asc = lyxfont::maxAscent(font);
874 int desc = lyxfont::maxDescent(font);
875 bv->fitLockedInsetCursor(cx, cy, asc, desc);
876 bv->showLockedInsetCursor(cx, cy, asc, desc);
877 cursor_visible = true;
882 void InsetText::HideInsetCursor(BufferView * bv)
885 ToggleInsetCursor(bv);
889 void InsetText::setPos(BufferView * bv, int x, int y, bool activate_inset)
894 // search right X-pos x==0 -> top_x
898 for(unsigned int i = 1;
899 ((cy + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
900 cy = rows[i].baseline;
901 actpos = rows[i].pos;
909 int sw = swh = SingleWidth(bv->painter(), par,actpos);
910 if (par->GetChar(actpos)!=LyXParagraph::META_INSET)
912 int checkpos = rows[actrow + 1].pos;
913 if ((actrow+2) < (int)rows.size())
915 while ((actpos < checkpos) && ((cx + swh) < x)) {
918 sw = swh = SingleWidth(bv->painter(), 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->painter(), 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->painter(), 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);
1312 bv->updateInset(this, flag);
1315 ShowInsetCursor(bv);
1318 // this is for the simple cut and paste mechanism
1319 // this then should be a global stuff so that cut'n'paste can work in and
1320 // and outside text-insets
1321 static LyXParagraph * simple_cut_buffer = 0;
1322 // static char simple_cut_buffer_textclass = 0;
1324 // for now here this should be in another Cut&Paste Class!
1326 static void DeleteSimpleCutBuffer()
1328 if (!simple_cut_buffer)
1330 LyXParagraph * tmppar;
1332 while (simple_cut_buffer) {
1333 tmppar = simple_cut_buffer;
1334 simple_cut_buffer = simple_cut_buffer->next;
1337 simple_cut_buffer = 0;
1340 bool InsetText::cutSelection()
1342 if (!hasSelection())
1344 DeleteSimpleCutBuffer();
1346 // only within one paragraph
1347 simple_cut_buffer = new LyXParagraph;
1348 LyXParagraph::size_type i = selection_start;
1349 for (; i < selection_end; ++i) {
1350 par->CopyIntoMinibuffer(selection_start);
1351 par->Erase(selection_start);
1352 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1357 bool InsetText::copySelection()
1359 if (!hasSelection())
1361 DeleteSimpleCutBuffer();
1363 // only within one paragraph
1364 simple_cut_buffer = new LyXParagraph;
1365 LyXParagraph::size_type i = selection_start;
1366 for (; i < selection_end; ++i) {
1367 par->CopyIntoMinibuffer(i);
1368 simple_cut_buffer->InsertFromMinibuffer(simple_cut_buffer->Last());
1373 bool InsetText::pasteSelection()
1375 if (!simple_cut_buffer)
1378 LyXParagraph * tmppar = simple_cut_buffer->Clone();
1380 while (simple_cut_buffer->size()) {
1381 simple_cut_buffer->CutIntoMinibuffer(0);
1382 simple_cut_buffer->Erase(0);
1383 par->InsertFromMinibuffer(actpos);
1386 delete simple_cut_buffer;
1387 simple_cut_buffer = tmppar;