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"
56 #include "minibuffer.h"
65 extern unsigned char getCurrentTextClass(Buffer *);
66 extern LyXTextClass::size_type current_layout;
69 InsetText::InsetText(Buffer * buf)
71 par = new LyXParagraph();
76 InsetText::InsetText(InsetText const & ins, Buffer * buf)
80 autoBreakRows = ins.autoBreakRows;
84 void InsetText::init(Buffer * buf, InsetText const * ins)
86 the_locking_inset = 0;
88 cursor_visible = false;
93 maxAscent = maxDescent = insetWidth = 0;
94 drawTextXOffset = drawTextYOffset = 0;
95 autoBreakRows = false;
98 SetParagraphData(ins->par);
99 autoBreakRows = ins->autoBreakRows;
101 par->SetInsetOwner(this);
104 selection_start_cursor = selection_end_cursor = cursor;
108 InsetText::~InsetText()
114 Inset * InsetText::Clone() const
116 InsetText * t = new InsetText(*this, buffer);
121 void InsetText::Write(ostream & os) const
124 WriteParagraphData(os);
128 void InsetText::WriteParagraphData(ostream & os) const
130 par->writeFile(os, buffer->params, 0, 0);
134 void InsetText::Read(LyXLex & lex)
136 string token, tmptok;
138 LyXParagraph * return_par = 0;
139 char depth = 0; // signed or unsigned?
140 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
141 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
142 LyXFont font(LyXFont::ALL_INHERIT);
145 par = new LyXParagraph;
146 par->SetInsetOwner(this);
150 token = lex.GetString();
153 if (token == "\\end_inset")
155 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
159 // the_end read this should NEVER happen
160 lex.printError("\\the_end read in inset! Error in document!");
164 if (token != "\\end_inset") {
165 lex.printError("Missing \\end_inset at this point. "
172 int InsetText::ascent(Painter & pain, LyXFont const & font) const
175 computeTextRows(pain, xpos);
181 return lyxfont::maxAscent(font);
185 int InsetText::descent(Painter & pain, LyXFont const & font) const
188 computeTextRows(pain, xpos);
194 return lyxfont::maxDescent(font);
198 int InsetText::width(Painter & pain, LyXFont const &) const
201 computeTextRows(pain, xpos);
209 void InsetText::draw(Painter & pain, LyXFont const & f,
210 int baseline, float & x) const
213 UpdatableInset::draw(pain, f, baseline, x);
215 if (init_inset || (baseline != top_baseline) || (top_x != int(x))) {
216 top_baseline = baseline;
217 if (init_inset || (top_x != int(x))) {
219 computeTextRows(pain, x);
222 computeBaselines(baseline);
224 if (the_locking_inset && (cursor.pos == inset_pos)) {
226 inset_x = cursor.x - top_x + drawTextXOffset;
227 inset_y = cursor.y + drawTextYOffset;
229 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
230 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
231 rows[r].baseline, x);
232 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
238 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
239 int row, int baseline, float x) const
245 if (selection_start_cursor.pos > selection_end_cursor.pos) {
246 s_start = selection_end_cursor.pos;
247 s_end = selection_start_cursor.pos;
249 s_start = selection_start_cursor.pos;
250 s_end = selection_end_cursor.pos;
252 if ((s_start > endpos) || (s_end < startpos))
256 int ssel_x = esel_x = int(x);
259 for(; p < endpos; ++p) {
262 if ((p >= s_start) && (p <= s_end))
264 char ch = par->GetChar(p);
265 font = GetDrawFont(par,p);
266 if (IsFloatChar(ch)) {
268 } else if (ch == LyXParagraph::META_INSET) {
269 Inset const * tmpinset = par->GetInset(p);
270 x += tmpinset->width(pain, font);
272 x += lyxfont::width(ch, font);
277 if ((p >= s_start) && (p <= s_end))
279 if (ssel_x < esel_x) {
280 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
281 int(esel_x - ssel_x),
282 rows[row].asc + rows[row].desc,
288 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
289 int baseline, float x) const
291 Assert(endpos <= par->Last());
293 for(int p = startpos; p < endpos; ++p) {
294 char ch = par->GetChar(p);
295 LyXFont font = GetDrawFont(par,p);
296 if (IsFloatChar(ch)) {
298 } else if (par->IsNewline(p)) {
299 // Draw end-of-line marker
300 int wid = lyxfont::width('n', font);
301 int asc = lyxfont::maxAscent(font);
305 xp[0] = int(x + wid * 0.375);
306 yp[0] = int(y - 0.875 * asc * 0.75);
309 yp[1] = int(y - 0.500 * asc * 0.75);
311 xp[2] = int(x + wid * 0.375);
312 yp[2] = int(y - 0.125 * asc * 0.75);
314 pain.lines(xp, yp, 3, LColor::eolmarker);
317 yp[0] = int(y - 0.500 * asc * 0.75);
319 xp[1] = int(x + wid);
320 yp[1] = int(y - 0.500 * asc * 0.75);
322 xp[2] = int(x + wid);
323 yp[2] = int(y - asc * 0.75);
325 pain.lines(xp, yp, 3, LColor::eolmarker);
327 } else if (ch == LyXParagraph::META_INSET) {
328 Inset * tmpinset = par->GetInset(p);
330 tmpinset->draw(pain, font, baseline, x);
332 pain.text(int(x), baseline, ch, font);
333 x += lyxfont::width(ch, font);
339 char const * InsetText::EditMessage() const
341 return _("Opened Text Inset");
345 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
347 par->SetInsetOwner(this);
348 UpdatableInset::Edit(bv, x, y, button);
350 if (!bv->lockInset(this)) {
351 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
354 the_locking_inset = 0;
355 inset_pos = inset_x = inset_y = 0;
356 setPos(bv->painter(), x, y);
357 checkAndActivateInset(bv, x, y, button);
358 selection_start_cursor = selection_end_cursor = cursor;
359 current_font = real_current_font = GetFont(par, cursor.pos);
360 bv->text->FinishUndo();
361 UpdateLocal(bv, true);
365 void InsetText::InsetUnlock(BufferView * bv)
367 if (the_locking_inset) {
368 the_locking_inset->InsetUnlock(bv);
369 the_locking_inset = 0;
372 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
374 if (hasSelection()) {
375 selection_start_cursor = selection_end_cursor = cursor;
376 UpdateLocal(bv, false);
378 no_selection = false;
382 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
384 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
387 if (inset == par->GetInset(cursor.pos)) {
388 lyxerr[Debug::INSETS] << "OK" << endl;
389 the_locking_inset = inset;
390 resetPos(bv->painter());
391 inset_x = cursor.x - top_x + drawTextXOffset;
392 inset_y = cursor.y + drawTextYOffset;
393 inset_pos = cursor.pos;
395 } else if (the_locking_inset && (the_locking_inset == inset)) {
396 if (cursor.pos == inset_pos) {
397 lyxerr[Debug::INSETS] << "OK" << endl;
398 resetPos(bv->painter());
399 inset_x = cursor.x - top_x + drawTextXOffset;
400 inset_y = cursor.y + drawTextYOffset;
402 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
404 } else if (the_locking_inset) {
405 lyxerr[Debug::INSETS] << "MAYBE" << endl;
406 return the_locking_inset->LockInsetInInset(bv, inset);
408 lyxerr[Debug::INSETS] << "NOT OK" << endl;
413 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
416 if (!the_locking_inset)
418 if (the_locking_inset == inset) {
419 the_locking_inset->InsetUnlock(bv);
420 the_locking_inset = 0;
422 moveRight(bv, false);
425 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
429 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
431 if (!the_locking_inset)
433 if (the_locking_inset != inset)
434 return the_locking_inset->UpdateInsetInInset(bv, inset);
435 lyxerr[Debug::INSETS] << "InsetText::UpdateInsetInInset(" << inset <<
437 UpdateLocal(bv, true);
438 if (cursor.pos == inset_pos) {
439 inset_x = cursor.x - top_x + drawTextXOffset;
440 inset_y = cursor.y + drawTextYOffset;
446 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
448 if (hasSelection()) {
449 selection_start_cursor = selection_end_cursor = cursor;
450 UpdateLocal(bv, false);
452 no_selection = false;
453 setPos(bv->painter(), x, y);
455 if (the_locking_inset) {
458 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET)
459 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
460 if (the_locking_inset == inset) {
461 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
464 // otherwise unlock the_locking_inset and lock the new inset
465 the_locking_inset->InsetUnlock(bv);
466 inset_x = cursor.x - top_x + drawTextXOffset;
467 inset_y = cursor.y + drawTextYOffset;
468 inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
469 inset->Edit(bv, x - inset_x, y - inset_y, button);
470 UpdateLocal(bv, true);
473 // otherwise only unlock the_locking_inset
474 the_locking_inset->InsetUnlock(bv);
475 the_locking_inset = 0;
477 if (bv->the_locking_inset) {
478 if ((par->GetChar(cursor.pos) == LyXParagraph::META_INSET) &&
479 par->GetInset(cursor.pos) &&
480 (par->GetInset(cursor.pos)->Editable() == Inset::HIGHLY_EDITABLE)) {
481 UpdatableInset *inset =
482 static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
483 inset_x = cursor.x - top_x + drawTextXOffset;
484 inset_y = cursor.y + drawTextYOffset;
485 inset->InsetButtonPress(bv, x-inset_x, y-inset_y, button);
486 inset->Edit(bv, x-inset_x, y-inset_y, 0);
487 UpdateLocal(bv, true);
490 selection_start_cursor = selection_end_cursor = cursor;
494 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
496 UpdatableInset * inset = 0;
498 if (the_locking_inset) {
499 the_locking_inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
501 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
502 inset = static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
503 if (inset->Editable()==Inset::HIGHLY_EDITABLE) {
504 inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
506 inset_x = cursor.x - top_x + drawTextXOffset;
507 inset_y = cursor.y + drawTextYOffset;
508 inset->InsetButtonRelease(bv, x-inset_x, y-inset_y,button);
509 inset->Edit(bv, x-inset_x, y-inset_y, button);
513 no_selection = false;
517 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
519 if (the_locking_inset) {
520 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
525 LyXCursor old = selection_end_cursor;
527 setPos(bv->painter(), x, y);
528 selection_end_cursor = cursor;
529 if (old != selection_end_cursor)
530 UpdateLocal(bv, false);
533 no_selection = false;
537 void InsetText::InsetKeyPress(XKeyEvent * xke)
539 if (the_locking_inset) {
540 the_locking_inset->InsetKeyPress(xke);
546 UpdatableInset::RESULT
547 InsetText::LocalDispatch(BufferView * bv,
548 int action, string const & arg)
550 no_selection = false;
551 UpdatableInset::RESULT
552 result= UpdatableInset::LocalDispatch(bv, action, arg);
553 if (result != UNDISPATCHED) {
554 resetPos(bv->painter());
559 if ((action < 0) && arg.empty())
562 if ((action != LFUN_DOWN) && (action != LFUN_UP) &&
563 (action != LFUN_DOWNSEL) && (action != LFUN_UPSEL))
565 if (the_locking_inset) {
566 result = the_locking_inset->LocalDispatch(bv, action, arg);
567 if (result == DISPATCHED_NOUPDATE)
569 else if (result == DISPATCHED) {
570 the_locking_inset->ToggleInsetCursor(bv);
571 UpdateLocal(bv, false);
572 the_locking_inset->ToggleInsetCursor(bv);
574 } else if (result == FINISHED) {
578 cursor.pos = inset_pos + 1;
579 resetPos(bv->painter());
585 the_locking_inset = 0;
593 bv->text->SetUndo(Undo::INSERT,
594 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
595 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
597 cursor = selection_start_cursor;
598 par->InsertChar(cursor.pos,arg[0]);
599 SetCharFont(cursor.pos,current_font);
601 selection_start_cursor = selection_end_cursor = cursor;
602 UpdateLocal(bv, true);
604 // --- Cursor Movements ---------------------------------------------
606 bv->text->FinishUndo();
607 moveRight(bv, false);
608 selection_end_cursor = cursor;
609 UpdateLocal(bv, false);
612 bv->text->FinishUndo();
613 result = moveRight(bv);
614 if (hasSelection()) {
615 selection_start_cursor = selection_end_cursor = cursor;
616 UpdateLocal(bv, false);
618 selection_start_cursor = selection_end_cursor = cursor;
622 bv->text->FinishUndo();
624 selection_end_cursor = cursor;
625 UpdateLocal(bv, false);
628 bv->text->FinishUndo();
629 result= moveLeft(bv);
630 if (hasSelection()) {
631 selection_start_cursor = selection_end_cursor = cursor;
632 UpdateLocal(bv, false);
634 selection_start_cursor = selection_end_cursor = cursor;
638 bv->text->FinishUndo();
640 selection_end_cursor = cursor;
641 UpdateLocal(bv, false);
644 bv->text->FinishUndo();
645 result = moveDown(bv);
646 if (hasSelection()) {
647 selection_start_cursor = selection_end_cursor = cursor;
648 UpdateLocal(bv, false);
650 selection_start_cursor = selection_end_cursor = cursor;
654 bv->text->FinishUndo();
656 selection_end_cursor = cursor;
657 UpdateLocal(bv, false);
660 bv->text->FinishUndo();
662 if (hasSelection()) {
663 selection_start_cursor = selection_end_cursor = cursor;
664 UpdateLocal(bv, false);
666 selection_start_cursor = selection_end_cursor = cursor;
671 if (hasSelection()) {
672 selection_start_cursor = selection_end_cursor = cursor;
673 UpdateLocal(bv, false);
680 bv->text->SetUndo(Undo::DELETE,
681 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
682 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
684 if (hasSelection()) {
685 LyXParagraph::size_type i = selection_start_cursor.pos;
686 for (; i < selection_end_cursor.pos; ++i) {
687 par->Erase(selection_start_cursor.pos);
691 if (ret) { // we need update
692 selection_start_cursor = selection_end_cursor = cursor;
693 UpdateLocal(bv, true);
694 } else if (hasSelection()) {
695 selection_start_cursor = selection_end_cursor = cursor;
696 UpdateLocal(bv, false);
699 resetPos(bv->painter());
702 bv->text->SetUndo(Undo::DELETE,
703 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
704 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
706 if (cutSelection()) {
708 cursor = selection_end_cursor = selection_start_cursor;
709 UpdateLocal(bv, true);
710 } else if (hasSelection()) {
711 selection_start_cursor = selection_end_cursor = cursor;
712 UpdateLocal(bv, false);
714 resetPos(bv->painter());
717 bv->text->FinishUndo();
718 if (copySelection()) {
720 selection_start_cursor = selection_end_cursor = cursor;
721 UpdateLocal(bv, true);
722 } else if (hasSelection()) {
723 selection_start_cursor = selection_end_cursor = cursor;
724 UpdateLocal(bv, false);
729 bv->text->SetUndo(Undo::INSERT,
730 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
731 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
732 if (pasteSelection()) {
733 selection_start_cursor = selection_end_cursor = cursor;
734 UpdateLocal(bv, true);
737 resetPos(bv->painter());
740 bv->text->FinishUndo();
741 for(; cursor.pos > rows[actrow].pos; --cursor.pos)
742 cursor.x -= SingleWidth(bv->painter(), par, cursor.pos);
743 cursor.x -= SingleWidth(bv->painter(), par, cursor.pos);
744 if (hasSelection()) {
745 selection_start_cursor = selection_end_cursor = cursor;
746 UpdateLocal(bv, false);
748 selection_start_cursor = selection_end_cursor = cursor;
750 resetPos(bv->painter());
754 bv->text->FinishUndo();
755 int checkpos = (int)rows[actrow + 1].pos;
756 if ((actrow + 2) < (int)rows.size())
758 for(; cursor.pos < checkpos; ++cursor.pos)
759 cursor.x += SingleWidth(bv->painter(), par, cursor.pos);
760 if (hasSelection()) {
761 selection_start_cursor = selection_end_cursor = cursor;
762 UpdateLocal(bv, false);
764 selection_start_cursor = selection_end_cursor = cursor;
767 resetPos(bv->painter());
770 InsertInset(bv, new InsetFormula);
773 InsertInset(bv, new InsetERT(buffer));
775 case LFUN_BREAKPARAGRAPH:
779 bv->text->SetUndo(Undo::INSERT,
780 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
781 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
782 par->InsertChar(cursor.pos,LyXParagraph::META_NEWLINE);
783 SetCharFont(cursor.pos,current_font);
784 UpdateLocal(bv, true);
786 selection_start_cursor = selection_end_cursor = cursor;
787 resetPos(bv->painter());
791 // Derive layout number from given argument (string)
792 // and current buffer's textclass (number). */
793 LyXTextClassList::ClassList::size_type tclass =
794 buffer->params.textclass;
795 std::pair <bool, LyXTextClass::size_type> layout =
796 textclasslist.NumberOfLayout(tclass, arg);
798 // If the entry is obsolete, use the new one instead.
800 string obs = textclasslist.Style(tclass,layout.second).
803 layout = textclasslist.NumberOfLayout(tclass, obs);
806 // see if we found the layout number:
808 string msg = string(N_("Layout ")) + arg + N_(" not known");
810 bv->owner()->getMiniBuffer()->Set(msg);
814 if (current_layout != layout.second) {
815 bv->text->SetLayout(cursor, selection_start_cursor,
816 selection_end_cursor, layout.second);
817 bv->owner()->getToolbar()->combox->select(cursor.par->GetLayout()+1);
818 UpdateLocal(bv, true);
823 result = UNDISPATCHED;
826 if (result != FINISHED) {
829 bv->unlockInset(this);
834 int InsetText::Latex(ostream & os, bool /*fragile*/, bool /*fp*/) const
837 buffer->latexParagraphs(os, par, 0, texrow);
838 return texrow.rows();
842 void InsetText::Validate(LaTeXFeatures & features) const
844 par->validate(features);
848 // Returns the width of a character at a certain spot
849 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
851 LyXFont font = GetDrawFont(par, pos);
852 char c = par->GetChar(pos);
854 if (IsPrintable(c)) {
855 return lyxfont::width(c, font);
856 } else if (c == LyXParagraph::META_INSET) {
857 Inset const * tmpinset = par->GetInset(pos);
859 return tmpinset->width(pain, font);
862 } else if (IsSeparatorChar(c))
864 else if (IsNewlineChar(c))
866 return lyxfont::width(c, font);
870 // Returns the width of a character at a certain spot
871 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
872 int & asc, int & desc) const
874 LyXFont font = GetDrawFont(par, pos);
875 char c = par->GetChar(pos);
878 if (c == LyXParagraph::META_INSET) {
879 Inset const * tmpinset=par->GetInset(pos);
881 asc = tmpinset->ascent(pain, font);
882 desc = tmpinset->descent(pain, font);
885 asc = lyxfont::maxAscent(font);
886 desc = lyxfont::maxDescent(font);
892 // Gets the fully instantiated font at a given position in a paragraph
893 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
894 // The difference is that this one is used for displaying, and thus we
895 // are allowed to make cosmetic improvements. For instance make footnotes
897 // If position is -1, we get the layout font of the paragraph.
898 // If position is -2, we get the font of the manual label of the paragraph.
899 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
901 char par_depth = par->GetDepth();
903 LyXLayout const & layout =
904 textclasslist.Style(buffer->params.textclass, par->GetLayout());
906 // We specialize the 95% common case:
907 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
910 if (layout.labeltype == LABEL_MANUAL
911 && pos < BeginningOfMainBody(par)) {
913 return par->GetFontSettings(pos).realize(layout.reslabelfont);
915 return par->GetFontSettings(pos).realize(layout.resfont);
918 // process layoutfont for pos == -1 and labelfont for pos < -1
920 return layout.resfont;
922 return layout.reslabelfont;
925 // The uncommon case need not be optimized as much
927 LyXFont layoutfont, tmpfont;
931 if (pos < BeginningOfMainBody(par)) {
933 layoutfont = layout.labelfont;
936 layoutfont = layout.font;
938 tmpfont = par->GetFontSettings(pos);
939 tmpfont.realize(layoutfont);
942 // process layoutfont for pos == -1 and labelfont for pos < -1
944 tmpfont = layout.font;
946 tmpfont = layout.labelfont;
949 // Resolve against environment font information
950 //if (par->GetDepth()){ // already in while condition
951 while (par && par_depth && !tmpfont.resolved()) {
952 par = par->DepthHook(par_depth - 1);
954 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
955 par->GetLayout()).font);
956 par_depth = par->GetDepth();
959 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
965 // the font for drawing may be different from the real font
966 LyXFont InsetText::GetDrawFont(LyXParagraph * par, int pos) const
968 return GetFont(par, pos);
972 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
974 if (textclasslist.Style(buffer->params.textclass,
975 par->GetLayout()).labeltype != LABEL_MANUAL)
978 return par->BeginningOfMainBody();
982 void InsetText::GetCursorPos(int & x, int & y) const
989 int InsetText::InsetInInsetY()
991 if (!the_locking_inset)
994 return (inset_y + the_locking_inset->InsetInInsetY());
998 void InsetText::ToggleInsetCursor(BufferView * bv)
1000 if (the_locking_inset) {
1001 the_locking_inset->ToggleInsetCursor(bv);
1005 LyXFont font = GetDrawFont(par, cursor.pos);
1007 int asc = lyxfont::maxAscent(font);
1008 int desc = lyxfont::maxDescent(font);
1011 bv->hideLockedInsetCursor();
1013 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1014 cursor_visible = !cursor_visible;
1018 void InsetText::ShowInsetCursor(BufferView * bv)
1020 if (the_locking_inset) {
1021 the_locking_inset->ShowInsetCursor(bv);
1024 if (!cursor_visible) {
1025 LyXFont font = GetDrawFont(par, cursor.pos);
1027 int asc = lyxfont::maxAscent(font);
1028 int desc = lyxfont::maxDescent(font);
1029 bv->fitLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1030 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1031 cursor_visible = true;
1036 void InsetText::HideInsetCursor(BufferView * bv)
1038 if (cursor_visible) {
1039 bv->hideLockedInsetCursor();
1040 cursor_visible = false;
1042 if (the_locking_inset)
1043 the_locking_inset->HideInsetCursor(bv);
1047 void InsetText::setPos(Painter & pain, int x, int y) const
1049 x -= drawTextXOffset;
1050 y -= drawTextYOffset;
1051 // search right X-pos x==0 -> top_x
1052 cursor.pos = actrow = 0;
1053 cursor.y = top_baseline;
1055 for(unsigned int i = 1;
1056 (long(cursor.y + rows[i - 1].desc) < y)
1057 && (i < rows.size() - 1); ++i) {
1058 cursor.y = rows[i].baseline;
1059 cursor.pos = rows[i].pos;
1062 cursor.y -= top_baseline;
1067 int sw = swh = SingleWidth(pain, par,cursor.pos);
1068 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1070 int checkpos = rows[actrow + 1].pos;
1071 if ((actrow+2) < (int)rows.size())
1073 while ((cursor.pos < checkpos) && ((cursor.x + swh) < x)) {
1076 sw = swh = SingleWidth(pain, par,cursor.pos);
1077 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1083 void InsetText::resetPos(Painter & pain) const
1090 int old_pos = cursor.pos;
1092 cursor.y = top_baseline;
1094 for(unsigned int i = 0;
1095 (i < (rows.size()-1)) && (rows[i].pos <= cursor.pos);
1097 cursor.y = rows[i].baseline;
1100 cursor.y -= top_baseline;
1101 setPos(pain, 0, cursor.y);
1103 while(cursor.pos < old_pos) {
1104 cursor.x += SingleWidth(pain, par,cursor.pos);
1110 UpdatableInset::RESULT
1111 InsetText::moveRight(BufferView * bv, bool activate_inset)
1113 if (cursor.pos >= par->Last())
1115 if (activate_inset && checkAndActivateInset(bv)) {
1119 resetPos(bv->painter());
1120 real_current_font = current_font = GetFont(par, cursor.pos);
1121 return DISPATCHED_NOUPDATE;
1125 UpdatableInset::RESULT
1126 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1128 if (cursor.pos <= 0)
1131 resetPos(bv->painter());
1133 if (checkAndActivateInset(bv, -1, -1))
1135 return DISPATCHED_NOUPDATE;
1139 UpdatableInset::RESULT
1140 InsetText::moveUp(BufferView * bv)
1144 cursor.y = rows[actrow - 1].baseline - top_baseline;
1145 if (cursor.x_fix < 0)
1146 cursor.x_fix = cursor.x;
1147 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1148 return DISPATCHED_NOUPDATE;
1152 UpdatableInset::RESULT
1153 InsetText::moveDown(BufferView * bv)
1155 if (actrow >= int(rows.size() - 2))
1157 cursor.y = rows[actrow + 1].baseline - top_baseline;
1158 if (cursor.x_fix < 0)
1159 cursor.x_fix = cursor.x;
1160 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1161 return DISPATCHED_NOUPDATE;
1165 bool InsetText::Delete()
1167 if ((par->GetChar(cursor.pos)==LyXParagraph::META_INSET) &&
1168 !par->GetInset(cursor.pos)->Deletable()) {
1171 par->Erase(cursor.pos);
1176 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1178 bv->text->SetUndo(Undo::INSERT,
1179 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
1180 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
1181 if (inset->Editable() == Inset::IS_EDITABLE) {
1182 UpdatableInset *i = (UpdatableInset *)inset;
1183 i->setOwner((UpdatableInset *)this);
1185 par->InsertChar(cursor.pos, LyXParagraph::META_INSET);
1186 par->InsertInset(cursor.pos, inset);
1187 if (hasSelection()) {
1188 selection_start_cursor = selection_end_cursor = cursor;
1190 selection_start_cursor = selection_end_cursor = cursor;
1192 UpdateLocal(bv, true);
1193 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1198 UpdatableInset * InsetText::GetLockingInset()
1200 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1204 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1206 // if there is no selection just set the current_font
1207 if (!hasSelection()) {
1208 // Determine basis font
1210 if (cursor.pos < BeginningOfMainBody(par))
1211 layoutfont = GetFont(par, -2);
1213 layoutfont = GetFont(par, -1);
1215 // Update current font
1216 real_current_font.update(font, bv->buffer()->params.language_info,
1219 // Reduce to implicit settings
1220 current_font = real_current_font;
1221 current_font.reduce(layoutfont);
1222 // And resolve it completely
1223 real_current_font.realize(layoutfont);
1228 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1229 s_start = selection_end_cursor.pos;
1230 s_end = selection_start_cursor.pos;
1232 s_start = selection_start_cursor.pos;
1233 s_end = selection_end_cursor.pos;
1236 while(s_start < s_end) {
1237 newfont = GetFont(par,s_start);
1238 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1239 SetCharFont(s_start, newfont);
1242 UpdateLocal(bv, true);
1246 void InsetText::SetCharFont(int pos, LyXFont const & f)
1248 /* let the insets convert their font */
1251 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1252 if (par->GetInset(pos))
1253 font = par->GetInset(pos)->ConvertFont(font);
1255 LyXLayout const & layout =
1256 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1258 // Get concrete layout font to reduce against
1261 if (pos < BeginningOfMainBody(par))
1262 layoutfont = layout.labelfont;
1264 layoutfont = layout.font;
1267 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1270 // Now, reduce font against full layout font
1271 font.reduce(layoutfont);
1273 par->SetFont(pos, font);
1277 void InsetText::computeTextRows(Painter & pain, float x) const
1292 int width = wordAscent = wordDescent = 0;
1293 insetWidth = maxAscent = maxDescent = 0;
1298 rows.push_back(row);
1299 if (!autoBreakRows) {
1300 for(p = 0; p < par->Last(); ++p) {
1301 insetWidth += SingleWidth(pain, par, p);
1302 SingleHeight(pain, par, p, asc, desc);
1303 maxAscent = max(maxAscent, asc);
1304 maxDescent = max(maxDescent, desc);
1306 rows[0].asc = maxAscent;
1307 rows[0].desc = maxDescent;
1308 // alocate a dummy row for the endpos
1309 row.pos = par->Last();
1310 rows.push_back(row);
1314 bool is_first_word_in_row = true;
1315 int cw, lastWordWidth = 0;
1316 int maxWidth = getMaxTextWidth(pain, this, x);
1318 for(p = 0; p < par->Last(); ++p) {
1319 cw = SingleWidth(pain, par, p);
1321 lastWordWidth += cw;
1322 SingleHeight(pain, par, p, asc, desc);
1323 wordAscent = max(wordAscent, asc);
1324 wordDescent = max(wordDescent, desc);
1325 if (par->IsNewline(p)) {
1326 if (!is_first_word_in_row && (width >= maxWidth)) {
1327 // we have to split also the row above
1328 rows.back().asc = oasc;
1329 rows.back().desc = odesc;
1331 rows.push_back(row);
1333 odesc = wordDescent;
1334 insetWidth = max(insetWidth, owidth);
1335 width = lastWordWidth;
1338 rows.back().asc = wordAscent;
1339 rows.back().desc = wordDescent;
1341 rows.push_back(row);
1342 SingleHeight(pain, par, p, oasc, odesc);
1343 insetWidth = max(insetWidth, owidth);
1345 is_first_word_in_row = true;
1346 wordAscent = wordDescent = lastWordWidth = 0;
1351 if (((p + 1) < par->Last()) &&
1352 (par->GetChar(p + 1)==LyXParagraph::META_INSET))
1353 inset = par->GetInset(p + 1);
1355 inset->setOwner(const_cast<InsetText*>(this)); // is this safe?
1356 if (inset->display()) {
1357 if (!is_first_word_in_row && (width >= maxWidth)) {
1358 // we have to split also the row above
1359 rows.back().asc = oasc;
1360 rows.back().desc = odesc;
1362 rows.push_back(row);
1364 odesc = wordDescent;
1365 insetWidth = max(insetWidth, owidth);
1366 width = lastWordWidth;
1369 oasc = max(oasc, wordAscent);
1370 odesc = max(odesc, wordDescent);
1372 rows.back().asc = oasc;
1373 rows.back().desc = odesc;
1375 rows.push_back(row);
1376 SingleHeight(pain, par, p, asc, desc);
1377 rows.back().asc = asc;
1378 rows.back().desc = desc;
1379 row.pos = nwp = p + 1;
1380 rows.push_back(row);
1381 oasc = odesc = width = lastWordWidth = 0;
1382 is_first_word_in_row = true;
1383 wordAscent = wordDescent = 0;
1386 } else if (par->IsSeparator(p)) {
1387 if (width >= maxWidth) {
1388 if (is_first_word_in_row) {
1389 rows.back().asc = wordAscent;
1390 rows.back().desc = wordDescent;
1392 rows.push_back(row);
1393 oasc = odesc = width = 0;
1395 rows.back().asc = oasc;
1396 rows.back().desc = odesc;
1398 rows.push_back(row);
1400 odesc = wordDescent;
1401 insetWidth = max(insetWidth, owidth);
1402 width = lastWordWidth;
1404 wordAscent = wordDescent = lastWordWidth = 0;
1409 oasc = max(oasc, wordAscent);
1410 odesc = max(odesc, wordDescent);
1411 wordAscent = wordDescent = lastWordWidth = 0;
1413 is_first_word_in_row = false;
1416 // if we have some data in the paragraph we have ascent/descent
1418 if (width >= maxWidth) {
1420 rows.back().asc = oasc;
1421 rows.back().desc = odesc;
1422 // assign and allocate lower row
1424 rows.push_back(row);
1425 rows.back().asc = wordAscent;
1426 rows.back().desc = wordDescent;
1427 width -= lastWordWidth;
1429 // assign last row data
1430 rows.back().asc = max(oasc, wordAscent);
1431 rows.back().desc = max(odesc, wordDescent);
1434 insetWidth = max(insetWidth, width);
1435 // alocate a dummy row for the endpos
1436 row.pos = par->Last();
1437 rows.push_back(row);
1438 // calculate maxAscent/Descent
1439 maxAscent = rows[0].asc;
1440 maxDescent = rows[0].desc;
1441 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1442 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1447 void InsetText::computeBaselines(int baseline) const
1449 rows[0].baseline = baseline;
1450 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1451 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1452 rows[i].asc + interline_space;
1457 void InsetText::UpdateLocal(BufferView * bv, bool flag)
1460 computeTextRows(bv->painter(), xpos);
1461 computeBaselines(top_baseline);
1463 bv->updateInset(this, flag);
1465 resetPos(bv->painter());
1466 bv->owner()->getToolbar()->combox->select(cursor.par->GetLayout()+1);
1470 bool InsetText::cutSelection()
1472 if (!hasSelection())
1477 LyXParagraph * endpar = par;
1479 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1480 start = selection_end_cursor.pos;
1481 end = selection_start_cursor.pos;
1483 start = selection_start_cursor.pos;
1484 end = selection_end_cursor.pos;
1487 return cap.cutSelection(par, &endpar, start, end,buffer->params.textclass);
1491 bool InsetText::copySelection()
1493 if (!hasSelection())
1499 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1500 start = selection_end_cursor.pos;
1501 end = selection_start_cursor.pos;
1503 start = selection_start_cursor.pos;
1504 end = selection_end_cursor.pos;
1506 return cap.copySelection(par, par, start, end, buffer->params.textclass);
1510 bool InsetText::pasteSelection()
1514 if (cap.nrOfParagraphs() > 1) {
1515 WriteAlert(_("Impossible operation"),
1516 _("Cannot include more than one paragraph!"),
1520 LyXParagraph *endpar;
1521 LyXParagraph *actpar = par;
1523 return cap.pasteSelection(&actpar, &endpar, cursor.pos,
1524 buffer->params.textclass);
1528 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1531 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
1532 UpdatableInset * inset =
1533 static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
1534 LyXFont font = GetFont(par, cursor.pos);
1536 x = inset->width(bv->painter(), font);
1538 y = inset->descent(bv->painter(), font);
1539 inset_x = cursor.x - top_x + drawTextXOffset;
1540 inset_y = cursor.y + drawTextYOffset;
1541 inset->Edit(bv, x-inset_x, y-inset_y, button);
1542 if (!the_locking_inset)
1544 UpdateLocal(bv, true);
1551 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset,
1554 return getMaxWidth(pain, inset) - x;
1557 void InsetText::SetParagraphData(LyXParagraph *p)
1562 par->SetInsetOwner(this);