2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
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();
71 InsetText::InsetText(InsetText const & ins, Buffer * buf)
75 autoBreakRows = ins.autoBreakRows;
78 void InsetText::init(Buffer * buf, InsetText const * ins)
80 the_locking_inset = 0;
82 cursor_visible = false;
84 selection_start = selection_end = 0;
88 maxAscent = maxDescent = insetWidth = 0;
89 drawTextXOffset = drawTextYOffset = 0;
90 autoBreakRows = false;
95 par = ins->par->Clone();
96 autoBreakRows = ins->autoBreakRows;
98 par->SetInsetOwner(this);
104 InsetText::~InsetText()
110 Inset * InsetText::Clone() const
112 InsetText * t = new InsetText(*this, buffer);
117 void InsetText::Write(ostream & os) const
120 WriteParagraphData(os);
124 void InsetText::WriteParagraphData(ostream & os) const
126 par->writeFile(os, buffer->params, 0, 0);
130 void InsetText::Read(LyXLex & lex)
132 string token, tmptok;
134 LyXParagraph * return_par = 0;
135 char depth = 0; // signed or unsigned?
136 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
137 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
138 LyXFont font(LyXFont::ALL_INHERIT);
141 par = new LyXParagraph;
142 par->SetInsetOwner(this);
146 token = lex.GetString();
149 if (token == "\\end_inset")
151 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
155 // the_end read this should NEVER happen
156 lex.printError("\\the_end read in inset! Error in document!");
160 if (token != "\\end_inset") {
161 lex.printError("Missing \\end_inset at this point. "
168 int InsetText::ascent(Painter & pain, LyXFont const & font) const
171 computeTextRows(pain, xpos);
177 return lyxfont::maxAscent(font);
181 int InsetText::descent(Painter & pain, LyXFont const & font) const
184 computeTextRows(pain, xpos);
190 return lyxfont::maxDescent(font);
194 int InsetText::width(Painter & pain, LyXFont const &) const
197 computeTextRows(pain, xpos);
205 void InsetText::draw(Painter & pain, LyXFont const & f,
206 int baseline, float & x) const
209 UpdatableInset::draw(pain, f, baseline, x);
211 if (init_inset || (baseline != top_baseline) || (top_x != int(x))) {
212 top_baseline = baseline;
213 if (init_inset || (top_x != int(x))) {
215 computeTextRows(pain, x);
218 computeBaselines(baseline);
220 if (the_locking_inset && (cursor.pos == inset_pos)) {
222 inset_x = cursor.x - top_x + drawTextXOffset;
223 inset_y = cursor.y + drawTextYOffset;
225 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
226 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
227 rows[r].baseline, x);
228 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
234 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
235 int row, int baseline, float x) const
241 if (selection_start > selection_end) {
242 s_start = selection_end;
243 s_end = selection_start;
245 s_start = selection_start;
246 s_end = selection_end;
248 if ((s_start > endpos) || (s_end < startpos))
252 int ssel_x = esel_x = int(x);
255 for(; p < endpos; ++p) {
258 if ((p >= s_start) && (p <= s_end))
260 char ch = par->GetChar(p);
261 font = GetDrawFont(par,p);
262 if (IsFloatChar(ch)) {
264 } else if (ch == LyXParagraph::META_INSET) {
265 Inset const * tmpinset = par->GetInset(p);
266 x += tmpinset->width(pain, font);
268 x += lyxfont::width(ch, font);
273 if ((p >= s_start) && (p <= s_end))
275 if (ssel_x < esel_x) {
276 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
277 int(esel_x - ssel_x),
278 rows[row].asc + rows[row].desc,
284 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
285 int baseline, float x) const
287 Assert(endpos <= par->Last());
289 for(int p = startpos; p < endpos; ++p) {
290 char ch = par->GetChar(p);
291 LyXFont font = GetDrawFont(par,p);
292 if (IsFloatChar(ch)) {
294 } else if (par->IsNewline(p)) {
295 // Draw end-of-line marker
296 int wid = lyxfont::width('n', font);
297 int asc = lyxfont::maxAscent(font);
301 xp[0] = int(x + wid * 0.375);
302 yp[0] = int(y - 0.875 * asc * 0.75);
305 yp[1] = int(y - 0.500 * asc * 0.75);
307 xp[2] = int(x + wid * 0.375);
308 yp[2] = int(y - 0.125 * asc * 0.75);
310 pain.lines(xp, yp, 3, LColor::eolmarker);
313 yp[0] = int(y - 0.500 * asc * 0.75);
315 xp[1] = int(x + wid);
316 yp[1] = int(y - 0.500 * asc * 0.75);
318 xp[2] = int(x + wid);
319 yp[2] = int(y - asc * 0.75);
321 pain.lines(xp, yp, 3, LColor::eolmarker);
323 } else if (ch == LyXParagraph::META_INSET) {
324 Inset * tmpinset = par->GetInset(p);
326 tmpinset->draw(pain, font, baseline, x);
328 pain.text(int(x), baseline, ch, font);
329 x += lyxfont::width(ch, font);
335 char const * InsetText::EditMessage() const
337 return _("Opened Text Inset");
341 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
343 par->SetInsetOwner(this);
344 UpdatableInset::Edit(bv, x, y, button);
346 if (!bv->lockInset(this)) {
347 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
350 the_locking_inset = 0;
351 selection_start = selection_end = inset_pos = inset_x = inset_y = 0;
352 // no_selection = true;
353 setPos(bv->painter(), x, y);
354 checkAndActivateInset(bv, x, y, button);
355 selection_start = selection_end = cursor.pos;
356 current_font = real_current_font = GetFont(par, cursor.pos);
357 bv->text->FinishUndo();
358 UpdateLocal(bv, true);
362 void InsetText::InsetUnlock(BufferView * bv)
364 if (the_locking_inset) {
365 the_locking_inset->InsetUnlock(bv);
366 the_locking_inset = 0;
368 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
370 if (hasSelection()) {
371 selection_start = selection_end = cursor.pos;
372 UpdateLocal(bv, false);
374 no_selection = false;
377 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
379 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
382 if (inset == par->GetInset(cursor.pos)) {
383 lyxerr[Debug::INSETS] << "OK" << endl;
384 the_locking_inset = inset;
385 resetPos(bv->painter());
386 inset_x = cursor.x - top_x + drawTextXOffset;
387 inset_y = cursor.y + drawTextYOffset;
388 inset_pos = cursor.pos;
390 } else if (the_locking_inset && (the_locking_inset == inset)) {
391 if (cursor.pos == inset_pos) {
392 lyxerr[Debug::INSETS] << "OK" << endl;
393 resetPos(bv->painter());
394 inset_x = cursor.x - top_x + drawTextXOffset;
395 inset_y = cursor.y + drawTextYOffset;
397 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
399 } else if (the_locking_inset) {
400 lyxerr[Debug::INSETS] << "MAYBE" << endl;
401 return the_locking_inset->LockInsetInInset(bv, inset);
403 lyxerr[Debug::INSETS] << "NOT OK" << endl;
407 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
410 if (!the_locking_inset)
412 if (the_locking_inset == inset) {
413 the_locking_inset->InsetUnlock(bv);
414 the_locking_inset = 0;
416 moveRight(bv, false);
419 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
423 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
425 if (!the_locking_inset)
427 if (the_locking_inset != inset)
428 return the_locking_inset->UpdateInsetInInset(bv, inset);
429 lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
431 UpdateLocal(bv, true);
432 if (cursor.pos == inset_pos) {
433 inset_x = cursor.x - top_x + drawTextXOffset;
434 inset_y = cursor.y + drawTextYOffset;
440 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
442 if (hasSelection()) {
443 selection_start = selection_end = cursor.pos;
444 UpdateLocal(bv, false);
446 no_selection = false;
447 setPos(bv->painter(), x, y);
449 if (the_locking_inset) {
452 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET)
453 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
454 if (the_locking_inset == inset) {
455 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
458 // otherwise unlock the_locking_inset and lock the new inset
459 the_locking_inset->InsetUnlock(bv);
460 inset_x = cursor.x - top_x + drawTextXOffset;
461 inset_y = cursor.y + drawTextYOffset;
462 inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
463 inset->Edit(bv, x - inset_x, y - inset_y, button);
464 UpdateLocal(bv, true);
467 // otherwise only unlock the_locking_inset
468 the_locking_inset->InsetUnlock(bv);
469 the_locking_inset = 0;
471 if (bv->the_locking_inset) {
472 if ((par->GetChar(cursor.pos) == LyXParagraph::META_INSET) &&
473 par->GetInset(cursor.pos) &&
474 (par->GetInset(cursor.pos)->Editable() == Inset::HIGHLY_EDITABLE)) {
475 UpdatableInset *inset =
476 static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
477 inset_x = cursor.x - top_x + drawTextXOffset;
478 inset_y = cursor.y + drawTextYOffset;
479 inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
480 inset->Edit(bv, x-inset_x, y-inset_y, 0);
481 UpdateLocal(bv, true);
484 selection_start = selection_end = cursor.pos;
488 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
490 UpdatableInset * inset = 0;
492 if (the_locking_inset) {
493 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
495 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
496 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
497 if (inset->Editable()==Inset::HIGHLY_EDITABLE) {
498 inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
500 inset_x = cursor.x - top_x + drawTextXOffset;
501 inset_y = cursor.y + drawTextYOffset;
502 inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
503 inset->Edit(bv, x-inset_x, y-inset_y, button);
507 no_selection = false;
511 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
513 if (the_locking_inset) {
514 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
519 int old = selection_end;
521 setPos(bv->painter(), x, y);
522 selection_end = cursor.pos;
523 if (old != selection_end)
524 UpdateLocal(bv, false);
527 no_selection = false;
531 void InsetText::InsetKeyPress(XKeyEvent * xke)
533 if (the_locking_inset) {
534 the_locking_inset->InsetKeyPress(xke);
540 UpdatableInset::RESULT
541 InsetText::LocalDispatch(BufferView * bv,
542 int action, string const & arg)
544 no_selection = false;
545 UpdatableInset::RESULT
546 result= UpdatableInset::LocalDispatch(bv, action, arg);
547 if (result != UNDISPATCHED) {
548 resetPos(bv->painter());
553 if ((action < 0) && arg.empty())
556 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
557 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
559 if (the_locking_inset) {
560 result = the_locking_inset->LocalDispatch(bv, action, arg);
561 if (result == DISPATCHED_NOUPDATE)
563 else if (result == DISPATCHED) {
564 the_locking_inset->ToggleInsetCursor(bv);
565 UpdateLocal(bv, false);
566 the_locking_inset->ToggleInsetCursor(bv);
568 } else if (result == FINISHED) {
572 cursor.pos = inset_pos + 1;
573 resetPos(bv->painter());
579 the_locking_inset = 0;
587 bv->text->SetUndo(Undo::INSERT,
588 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
589 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
591 cursor.pos = selection_start;
592 par->InsertChar(cursor.pos,arg[0]);
593 SetCharFont(cursor.pos,current_font);
595 selection_start = selection_end = cursor.pos;
596 UpdateLocal(bv, true);
598 // --- Cursor Movements ---------------------------------------------
600 bv->text->FinishUndo();
601 moveRight(bv, false);
602 selection_end = cursor.pos;
603 UpdateLocal(bv, false);
606 bv->text->FinishUndo();
607 result = moveRight(bv);
608 if (hasSelection()) {
609 selection_start = selection_end = cursor.pos;
610 UpdateLocal(bv, false);
612 selection_start = selection_end = cursor.pos;
616 bv->text->FinishUndo();
618 selection_end = cursor.pos;
619 UpdateLocal(bv, false);
622 bv->text->FinishUndo();
623 result= moveLeft(bv);
624 if (hasSelection()) {
625 selection_start = selection_end = cursor.pos;
626 UpdateLocal(bv, false);
628 selection_start = selection_end = cursor.pos;
632 bv->text->FinishUndo();
634 selection_end = cursor.pos;
635 UpdateLocal(bv, false);
638 bv->text->FinishUndo();
639 result = moveDown(bv);
640 if (hasSelection()) {
641 selection_start = selection_end = cursor.pos;
642 UpdateLocal(bv, false);
644 selection_start = selection_end = cursor.pos;
648 bv->text->FinishUndo();
650 selection_end = cursor.pos;
651 UpdateLocal(bv, false);
654 bv->text->FinishUndo();
656 if (hasSelection()) {
657 selection_start = selection_end = cursor.pos;
658 UpdateLocal(bv, false);
660 selection_start = selection_end = cursor.pos;
664 if (!cursor.pos) { // || par->IsNewline(cursor.pos-1)) {
665 if (hasSelection()) {
666 selection_start = selection_end = cursor.pos;
667 UpdateLocal(bv, false);
674 bv->text->SetUndo(Undo::DELETE,
675 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
676 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
678 if (hasSelection()) {
679 LyXParagraph::size_type i = selection_start;
680 for (; i < selection_end; ++i) {
681 par->Erase(selection_start);
685 if (ret) { // we need update
686 selection_start = selection_end = cursor.pos;
687 UpdateLocal(bv, true);
688 } else if (hasSelection()) {
689 selection_start = selection_end = cursor.pos;
690 UpdateLocal(bv, false);
693 resetPos(bv->painter());
696 bv->text->SetUndo(Undo::DELETE,
697 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
698 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
700 if (cutSelection()) {
702 cursor.pos = selection_end = selection_start;
703 UpdateLocal(bv, true);
704 } else if (hasSelection()) {
705 selection_start = selection_end = cursor.pos;
706 UpdateLocal(bv, false);
708 resetPos(bv->painter());
711 bv->text->FinishUndo();
712 if (copySelection()) {
714 selection_start = selection_end = cursor.pos;
715 UpdateLocal(bv, true);
716 } else if (hasSelection()) {
717 selection_start = selection_end = cursor.pos;
718 UpdateLocal(bv, false);
723 bv->text->SetUndo(Undo::INSERT,
724 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
725 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
726 if (pasteSelection()) {
727 selection_start = selection_end = cursor.pos;
728 UpdateLocal(bv, true);
731 resetPos(bv->painter());
734 bv->text->FinishUndo();
735 for(; cursor.pos > rows[actrow].pos; --cursor.pos)
736 cursor.x -= SingleWidth(bv->painter(), par, cursor.pos);
737 cursor.x -= SingleWidth(bv->painter(), par, cursor.pos);
738 if (hasSelection()) {
739 selection_start = selection_end = cursor.pos;
740 UpdateLocal(bv, false);
742 selection_start = selection_end = cursor.pos;
744 resetPos(bv->painter());
748 bv->text->FinishUndo();
749 int checkpos = (int)rows[actrow + 1].pos;
750 if ((actrow + 2) < (int)rows.size())
752 for(; cursor.pos < checkpos; ++cursor.pos)
753 cursor.x += SingleWidth(bv->painter(), par, cursor.pos);
754 if (hasSelection()) {
755 selection_start = selection_end = cursor.pos;
756 UpdateLocal(bv, false);
758 selection_start = selection_end = cursor.pos;
761 resetPos(bv->painter());
764 InsertInset(bv, new InsetFormula);
767 InsertInset(bv, new InsetERT(buffer));
769 case LFUN_BREAKPARAGRAPH:
773 bv->text->SetUndo(Undo::INSERT,
774 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
775 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
776 par->InsertChar(cursor.pos,LyXParagraph::META_NEWLINE);
777 SetCharFont(cursor.pos,current_font);
778 UpdateLocal(bv, true);
780 selection_start = selection_end = cursor.pos;
781 resetPos(bv->painter());
784 result = UNDISPATCHED;
787 if (result != FINISHED) {
790 bv->unlockInset(this);
795 int InsetText::Latex(ostream & os, bool /*fragile*/, bool) const
798 int ret = par->SimpleTeXOnePar(os, texrow);
803 void InsetText::Validate(LaTeXFeatures & features) const
805 par->validate(features);
809 // Returns the width of a character at a certain spot
810 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
812 LyXFont font = GetDrawFont(par, pos);
813 char c = par->GetChar(pos);
815 if (IsPrintable(c)) {
816 return lyxfont::width(c, font);
817 } else if (c == LyXParagraph::META_INSET) {
818 Inset const * tmpinset = par->GetInset(pos);
820 return tmpinset->width(pain, font);
823 } else if (IsSeparatorChar(c))
825 else if (IsNewlineChar(c))
827 return lyxfont::width(c, font);
831 // Returns the width of a character at a certain spot
832 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
833 int & asc, int & desc) const
835 LyXFont font = GetDrawFont(par, pos);
836 char c = par->GetChar(pos);
839 if (c == LyXParagraph::META_INSET) {
840 Inset const * tmpinset=par->GetInset(pos);
842 asc = tmpinset->ascent(pain, font);
843 desc = tmpinset->descent(pain, font);
846 asc = lyxfont::maxAscent(font);
847 desc = lyxfont::maxDescent(font);
853 // Gets the fully instantiated font at a given position in a paragraph
854 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
855 // The difference is that this one is used for displaying, and thus we
856 // are allowed to make cosmetic improvements. For instance make footnotes
858 // If position is -1, we get the layout font of the paragraph.
859 // If position is -2, we get the font of the manual label of the paragraph.
860 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
862 char par_depth = par->GetDepth();
864 LyXLayout const & layout =
865 textclasslist.Style(buffer->params.textclass, par->GetLayout());
867 // We specialize the 95% common case:
868 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
871 if (layout.labeltype == LABEL_MANUAL
872 && pos < BeginningOfMainBody(par)) {
874 return par->GetFontSettings(pos).realize(layout.reslabelfont);
876 return par->GetFontSettings(pos).realize(layout.resfont);
879 // process layoutfont for pos == -1 and labelfont for pos < -1
881 return layout.resfont;
883 return layout.reslabelfont;
886 // The uncommon case need not be optimized as much
888 LyXFont layoutfont, tmpfont;
892 if (pos < BeginningOfMainBody(par)) {
894 layoutfont = layout.labelfont;
897 layoutfont = layout.font;
899 tmpfont = par->GetFontSettings(pos);
900 tmpfont.realize(layoutfont);
903 // process layoutfont for pos == -1 and labelfont for pos < -1
905 tmpfont = layout.font;
907 tmpfont = layout.labelfont;
910 // Resolve against environment font information
911 //if (par->GetDepth()){ // already in while condition
912 while (par && par_depth && !tmpfont.resolved()) {
913 par = par->DepthHook(par_depth - 1);
915 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
916 par->GetLayout()).font);
917 par_depth = par->GetDepth();
920 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
925 // the font for drawing may be different from the real font
926 LyXFont InsetText::GetDrawFont(LyXParagraph * par, int pos) const
928 return GetFont(par, pos);
931 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
933 if (textclasslist.Style(buffer->params.textclass,
934 par->GetLayout()).labeltype != LABEL_MANUAL)
937 return par->BeginningOfMainBody();
941 void InsetText::GetCursorPos(int & x, int & y) const
948 int InsetText::InsetInInsetY()
950 if (!the_locking_inset)
953 return (inset_y + the_locking_inset->InsetInInsetY());
957 void InsetText::ToggleInsetCursor(BufferView * bv)
959 if (the_locking_inset) {
960 the_locking_inset->ToggleInsetCursor(bv);
964 LyXFont font = GetDrawFont(par, cursor.pos);
966 int asc = lyxfont::maxAscent(font);
967 int desc = lyxfont::maxDescent(font);
970 bv->hideLockedInsetCursor();
972 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
973 cursor_visible = !cursor_visible;
977 void InsetText::ShowInsetCursor(BufferView * bv)
979 if (the_locking_inset) {
980 the_locking_inset->ShowInsetCursor(bv);
983 if (!cursor_visible) {
984 LyXFont font = GetDrawFont(par, cursor.pos);
986 int asc = lyxfont::maxAscent(font);
987 int desc = lyxfont::maxDescent(font);
988 bv->fitLockedInsetCursor(cursor.x, cursor.y, asc, desc);
989 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
990 cursor_visible = true;
995 void InsetText::HideInsetCursor(BufferView * bv)
997 if (cursor_visible) {
998 bv->hideLockedInsetCursor();
999 cursor_visible = false;
1001 if (the_locking_inset)
1002 the_locking_inset->HideInsetCursor(bv);
1006 void InsetText::setPos(Painter & pain, int x, int y) const
1008 x -= drawTextXOffset;
1009 y -= drawTextYOffset;
1010 // search right X-pos x==0 -> top_x
1011 cursor.pos = actrow = 0;
1012 cursor.y = top_baseline;
1014 for(unsigned int i = 1;
1015 ((cursor.y + rows[i - 1].desc) < y) && (i < rows.size() - 1); ++i) {
1016 cursor.y = rows[i].baseline;
1017 cursor.pos = rows[i].pos;
1020 cursor.y -= top_baseline;
1025 int sw = swh = SingleWidth(pain, par,cursor.pos);
1026 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1028 int checkpos = rows[actrow + 1].pos;
1029 if ((actrow+2) < (int)rows.size())
1031 while ((cursor.pos < checkpos) && ((cursor.x + swh) < x)) {
1034 sw = swh = SingleWidth(pain, par,cursor.pos);
1035 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1041 void InsetText::resetPos(Painter & pain) const
1046 int old_pos = cursor.pos;
1048 cursor.y = top_baseline;
1050 for(unsigned int i = 0; (i < (rows.size()-1)) && (rows[i].pos <= cursor.pos);
1052 cursor.y = rows[i].baseline;
1055 cursor.y -= top_baseline;
1056 setPos(pain, 0, cursor.y);
1058 while(cursor.pos < old_pos) {
1059 cursor.x += SingleWidth(pain, par,cursor.pos);
1065 UpdatableInset::RESULT
1066 InsetText::moveRight(BufferView * bv, bool activate_inset)
1068 if (cursor.pos >= par->Last())
1070 if (activate_inset && checkAndActivateInset(bv)) {
1074 resetPos(bv->painter());
1075 real_current_font = current_font = GetFont(par, cursor.pos);
1076 return DISPATCHED_NOUPDATE;
1080 UpdatableInset::RESULT
1081 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1083 if (cursor.pos <= 0)
1086 resetPos(bv->painter());
1088 if (checkAndActivateInset(bv, -1, -1))
1090 return DISPATCHED_NOUPDATE;
1094 UpdatableInset::RESULT
1095 InsetText::moveUp(BufferView * bv)
1099 cursor.y = rows[actrow - 1].baseline - top_baseline;
1100 if (cursor.x_fix < 0)
1101 cursor.x_fix = cursor.x;
1102 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1103 return DISPATCHED_NOUPDATE;
1107 UpdatableInset::RESULT
1108 InsetText::moveDown(BufferView * bv)
1110 if (actrow >= int(rows.size() - 2))
1112 cursor.y = rows[actrow + 1].baseline - top_baseline;
1113 if (cursor.x_fix < 0)
1114 cursor.x_fix = cursor.x;
1115 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1116 return DISPATCHED_NOUPDATE;
1120 bool InsetText::Delete()
1122 if ((par->GetChar(cursor.pos)==LyXParagraph::META_INSET) &&
1123 !par->GetInset(cursor.pos)->Deletable()) {
1126 par->Erase(cursor.pos);
1131 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1133 bv->text->SetUndo(Undo::INSERT,
1134 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
1135 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
1136 if (inset->Editable() == Inset::IS_EDITABLE) {
1137 UpdatableInset *i = (UpdatableInset *)inset;
1138 i->setOwner((UpdatableInset *)this);
1140 par->InsertChar(cursor.pos, LyXParagraph::META_INSET);
1141 par->InsertInset(cursor.pos, inset);
1142 if (hasSelection()) {
1143 selection_start = selection_end = cursor.pos;
1145 selection_start = selection_end = cursor.pos;
1147 UpdateLocal(bv, true);
1148 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1153 UpdatableInset * InsetText::GetLockingInset()
1155 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1159 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1161 // if there is no selection just set the current_font
1162 if (!hasSelection()) {
1163 // Determine basis font
1165 if (cursor.pos < BeginningOfMainBody(par))
1166 layoutfont = GetFont(par, -2);
1168 layoutfont = GetFont(par, -1);
1170 // Update current font
1171 real_current_font.update(font, bv->buffer()->params.language_info,
1174 // Reduce to implicit settings
1175 current_font = real_current_font;
1176 current_font.reduce(layoutfont);
1177 // And resolve it completely
1178 real_current_font.realize(layoutfont);
1183 if (selection_start > selection_end) {
1184 s_start = selection_end;
1185 s_end = selection_start;
1187 s_start = selection_start;
1188 s_end = selection_end;
1191 while(s_start < s_end) {
1192 newfont = GetFont(par,s_start);
1193 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1194 SetCharFont(s_start, newfont);
1197 UpdateLocal(bv, true);
1201 void InsetText::SetCharFont(int pos, LyXFont const & f)
1203 /* let the insets convert their font */
1206 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1207 if (par->GetInset(pos))
1208 font = par->GetInset(pos)->ConvertFont(font);
1210 LyXLayout const & layout =
1211 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1213 // Get concrete layout font to reduce against
1216 if (pos < BeginningOfMainBody(par))
1217 layoutfont = layout.labelfont;
1219 layoutfont = layout.font;
1222 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1225 // Now, reduce font against full layout font
1226 font.reduce(layoutfont);
1228 par->SetFont(pos, font);
1232 void InsetText::computeTextRows(Painter & pain, float x) const
1247 int width = wordAscent = wordDescent = 0;
1248 insetWidth = maxAscent = maxDescent = 0;
1253 rows.push_back(row);
1254 if (!autoBreakRows) {
1255 for(p = 0; p < par->Last(); ++p) {
1256 insetWidth += SingleWidth(pain, par, p);
1257 SingleHeight(pain, par, p, asc, desc);
1258 maxAscent = max(maxAscent, asc);
1259 maxDescent = max(maxDescent, desc);
1261 rows[0].asc = maxAscent;
1262 rows[0].desc = maxDescent;
1263 // alocate a dummy row for the endpos
1264 row.pos = par->Last();
1265 rows.push_back(row);
1269 bool is_first_word_in_row = true;
1270 int cw, lastWordWidth = 0;
1271 int maxWidth = getMaxTextWidth(pain, this, x);
1273 for(p = 0; p < par->Last(); ++p) {
1274 cw = SingleWidth(pain, par, p);
1276 lastWordWidth += cw;
1277 SingleHeight(pain, par, p, asc, desc);
1278 wordAscent = max(wordAscent, asc);
1279 wordDescent = max(wordDescent, desc);
1280 if (par->IsNewline(p)) {
1281 if (!is_first_word_in_row && (width >= maxWidth)) {
1282 // we have to split also the row above
1283 rows.back().asc = oasc;
1284 rows.back().desc = odesc;
1286 rows.push_back(row);
1288 odesc = wordDescent;
1289 insetWidth = max(insetWidth, owidth);
1290 width = lastWordWidth;
1293 rows.back().asc = wordAscent;
1294 rows.back().desc = wordDescent;
1296 rows.push_back(row);
1297 SingleHeight(pain, par, p, oasc, odesc);
1298 insetWidth = max(insetWidth, owidth);
1300 is_first_word_in_row = true;
1301 wordAscent = wordDescent = lastWordWidth = 0;
1306 if (((p + 1) < par->Last()) &&
1307 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1308 inset = par->GetInset(p + 1);
1310 inset->setOwner(this);
1311 if (inset->display()) {
1312 if (!is_first_word_in_row && (width >= maxWidth)) {
1313 // we have to split also the row above
1314 rows.back().asc = oasc;
1315 rows.back().desc = odesc;
1317 rows.push_back(row);
1319 odesc = wordDescent;
1320 insetWidth = max(insetWidth, owidth);
1321 width = lastWordWidth;
1324 oasc = max(oasc, wordAscent);
1325 odesc = max(odesc, wordDescent);
1327 rows.back().asc = oasc;
1328 rows.back().desc = odesc;
1330 rows.push_back(row);
1331 SingleHeight(pain, par, p, asc, desc);
1332 rows.back().asc = asc;
1333 rows.back().desc = desc;
1334 row.pos = nwp = p + 1;
1335 rows.push_back(row);
1336 oasc = odesc = width = lastWordWidth = 0;
1337 is_first_word_in_row = true;
1338 wordAscent = wordDescent = 0;
1341 } else if (par->IsSeparator(p)) {
1342 if (width >= maxWidth) {
1343 if (is_first_word_in_row) {
1344 rows.back().asc = wordAscent;
1345 rows.back().desc = wordDescent;
1347 rows.push_back(row);
1348 oasc = odesc = width = 0;
1350 rows.back().asc = oasc;
1351 rows.back().desc = odesc;
1353 rows.push_back(row);
1355 odesc = wordDescent;
1356 insetWidth = max(insetWidth, owidth);
1357 width = lastWordWidth;
1359 wordAscent = wordDescent = lastWordWidth = 0;
1364 oasc = max(oasc, wordAscent);
1365 odesc = max(odesc, wordDescent);
1366 wordAscent = wordDescent = lastWordWidth = 0;
1368 is_first_word_in_row = false;
1371 // if we have some data in the paragraph we have ascent/descent
1373 if (width >= maxWidth) {
1375 rows.back().asc = oasc;
1376 rows.back().desc = odesc;
1377 // assign and allocate lower row
1379 rows.push_back(row);
1380 rows.back().asc = wordAscent;
1381 rows.back().desc = wordDescent;
1382 width -= lastWordWidth;
1384 // assign last row data
1385 rows.back().asc = max(oasc, wordAscent);
1386 rows.back().desc = max(odesc, wordDescent);
1389 insetWidth = max(insetWidth, width);
1390 // alocate a dummy row for the endpos
1391 row.pos = par->Last();
1392 rows.push_back(row);
1393 // calculate maxAscent/Descent
1394 maxAscent = rows[0].asc;
1395 maxDescent = rows[0].desc;
1396 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1397 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1402 void InsetText::computeBaselines(int baseline) const
1404 rows[0].baseline = baseline;
1405 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1406 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1407 rows[i].asc + interline_space;
1411 void InsetText::UpdateLocal(BufferView *bv, bool flag)
1414 computeTextRows(bv->painter(), xpos);
1415 computeBaselines(top_baseline);
1417 bv->updateInset(this, flag);
1419 resetPos(bv->painter());
1422 bool InsetText::cutSelection()
1424 if (!hasSelection())
1429 LyXParagraph *endpar = par;
1431 if (selection_start > selection_end) {
1432 start = selection_end;
1433 end = selection_start;
1435 start = selection_start;
1436 end = selection_end;
1439 return cap.cutSelection(par, &endpar, start, end,buffer->params.textclass);
1442 bool InsetText::copySelection()
1444 if (!hasSelection())
1450 if (selection_start > selection_end) {
1451 start = selection_end;
1452 end = selection_start;
1454 start = selection_start;
1455 end = selection_end;
1457 return cap.copySelection(par, par, start, end, buffer->params.textclass);
1460 bool InsetText::pasteSelection()
1464 if (cap.nrOfParagraphs() > 1) {
1465 WriteAlert(_("Impossible operation"),
1466 _("Cannot include more than one paragraph!"),
1470 LyXParagraph *endpar;
1471 LyXParagraph *actpar = par;
1473 return cap.pasteSelection(&actpar, &endpar, cursor.pos,
1474 buffer->params.textclass);
1477 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1480 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
1481 UpdatableInset * inset =
1482 static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
1483 LyXFont font = GetFont(par, cursor.pos);
1485 x = inset->width(bv->painter(), font);
1487 y = inset->descent(bv->painter(), font);
1488 inset_x = cursor.x - top_x + drawTextXOffset;
1489 inset_y = cursor.y + drawTextYOffset;
1490 inset->Edit(bv, x-inset_x, y-inset_y, button);
1491 if (!the_locking_inset)
1493 UpdateLocal(bv, true);
1499 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset,
1502 return getMaxWidth(pain, inset) - x;