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"
24 #include "lyxparagraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
40 #include "minibuffer.h"
43 #include "support/textutils.h"
44 #include "support/LAssert.h"
52 extern unsigned char getCurrentTextClass(Buffer *);
53 extern LyXTextClass::size_type current_layout;
56 InsetText::InsetText(Buffer * buf)
58 par = new LyXParagraph();
63 InsetText::InsetText(InsetText const & ins, Buffer * buf)
67 autoBreakRows = ins.autoBreakRows;
71 void InsetText::init(Buffer * buf, InsetText const * ins)
73 the_locking_inset = 0;
75 cursor_visible = false;
80 maxAscent = maxDescent = insetWidth = 0;
81 drawTextXOffset = drawTextYOffset = 0;
82 autoBreakRows = drawLockedFrame = false;
85 SetParagraphData(ins->par);
86 autoBreakRows = ins->autoBreakRows;
88 par->SetInsetOwner(this);
91 selection_start_cursor = selection_end_cursor = cursor;
92 frame_color = LColor::insetframe;
97 InsetText::~InsetText()
103 Inset * InsetText::Clone() const
105 InsetText * t = new InsetText(*this, buffer);
110 void InsetText::Write(ostream & os) const
113 WriteParagraphData(os);
117 void InsetText::WriteParagraphData(ostream & os) const
119 par->writeFile(os, buffer->params, 0, 0);
123 void InsetText::Read(LyXLex & lex)
125 string token, tmptok;
127 LyXParagraph * return_par = 0;
128 char depth = 0; // signed or unsigned?
129 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
130 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
131 LyXFont font(LyXFont::ALL_INHERIT);
134 par = new LyXParagraph;
135 par->SetInsetOwner(this);
139 token = lex.GetString();
142 if (token == "\\end_inset")
144 if (buffer->parseSingleLyXformat2Token(lex, par, return_par,
148 // the_end read this should NEVER happen
149 lex.printError("\\the_end read in inset! Error in document!");
153 if (token != "\\end_inset") {
154 lex.printError("Missing \\end_inset at this point. "
161 int InsetText::ascent(Painter & pain, LyXFont const & font) const
164 computeTextRows(pain);
169 return maxAscent + 2;
170 return lyxfont::maxAscent(font);
174 int InsetText::descent(Painter & pain, LyXFont const & font) const
177 computeTextRows(pain);
182 return maxDescent + 2;
183 return lyxfont::maxDescent(font);
187 int InsetText::width(Painter & pain, LyXFont const &) const
190 computeTextRows(pain);
198 void InsetText::draw(Painter & pain, LyXFont const & f,
199 int baseline, float & x) const
202 UpdatableInset::draw(pain, f, baseline, x);
204 if (init_inset || (baseline != top_baseline) || (top_x != int(x))) {
205 top_baseline = baseline;
206 if (init_inset || (top_x != int(x))) {
208 computeTextRows(pain);
211 computeBaselines(baseline);
213 if (the_locking_inset && (cursor.pos == inset_pos)) {
215 inset_x = cursor.x - top_x + drawTextXOffset;
216 inset_y = cursor.y + drawTextYOffset;
218 if (drawLockedFrame && locked) {
219 pain.rectangle(int(x), baseline - ascent(pain, f), insetWidth,
220 ascent(pain,f) + descent(pain, f), frame_color);
222 x += TEXT_TO_INSET_OFFSET; // place for border
223 for(RowList::size_type r = 0; r < rows.size() - 1; ++r) {
224 drawRowSelection(pain, rows[r].pos, rows[r + 1].pos, r,
225 rows[r].baseline, x);
226 drawRowText(pain, rows[r].pos, rows[r + 1].pos, rows[r].baseline, x);
228 x += insetWidth - TEXT_TO_INSET_OFFSET;
232 void InsetText::drawRowSelection(Painter & pain, int startpos, int endpos,
233 int row, int baseline, float x) const
239 if (selection_start_cursor.pos > selection_end_cursor.pos) {
240 s_start = selection_end_cursor.pos;
241 s_end = selection_start_cursor.pos;
243 s_start = selection_start_cursor.pos;
244 s_end = selection_end_cursor.pos;
246 if ((s_start > endpos) || (s_end < startpos))
250 int ssel_x = esel_x = int(x);
253 for(; p < endpos; ++p) {
256 if ((p >= s_start) && (p <= s_end))
258 char ch = par->GetChar(p);
259 font = GetDrawFont(par,p);
260 if (IsFloatChar(ch)) {
262 } else if (ch == LyXParagraph::META_INSET) {
263 Inset const * tmpinset = par->GetInset(p);
264 x += tmpinset->width(pain, font);
266 x += lyxfont::width(ch, font);
271 if ((p >= s_start) && (p <= s_end))
273 if (ssel_x < esel_x) {
274 pain.fillRectangle(int(ssel_x), baseline-rows[row].asc,
275 int(esel_x - ssel_x),
276 rows[row].asc + rows[row].desc,
282 void InsetText::drawRowText(Painter & pain, int startpos, int endpos,
283 int baseline, float x) const
285 Assert(endpos <= par->Last());
287 for(int p = startpos; p < endpos; ++p) {
288 char ch = par->GetChar(p);
289 LyXFont font = GetDrawFont(par,p);
290 if (IsFloatChar(ch)) {
292 } else if (par->IsNewline(p)) {
293 // Draw end-of-line marker
294 int wid = lyxfont::width('n', font);
295 int asc = lyxfont::maxAscent(font);
299 xp[0] = int(x + wid * 0.375);
300 yp[0] = int(y - 0.875 * asc * 0.75);
303 yp[1] = int(y - 0.500 * asc * 0.75);
305 xp[2] = int(x + wid * 0.375);
306 yp[2] = int(y - 0.125 * asc * 0.75);
308 pain.lines(xp, yp, 3, LColor::eolmarker);
311 yp[0] = int(y - 0.500 * asc * 0.75);
313 xp[1] = int(x + wid);
314 yp[1] = int(y - 0.500 * asc * 0.75);
316 xp[2] = int(x + wid);
317 yp[2] = int(y - asc * 0.75);
319 pain.lines(xp, yp, 3, LColor::eolmarker);
321 } else if (ch == LyXParagraph::META_INSET) {
322 Inset * tmpinset = par->GetInset(p);
324 tmpinset->draw(pain, font, baseline, x);
326 pain.text(int(x), baseline, ch, font);
327 x += lyxfont::width(ch, font);
333 char const * InsetText::EditMessage() const
335 return _("Opened Text Inset");
339 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
341 par->SetInsetOwner(this);
342 UpdatableInset::Edit(bv, x, y, button);
344 if (!bv->lockInset(this)) {
345 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
349 the_locking_inset = 0;
350 inset_pos = inset_x = inset_y = 0;
351 setPos(bv->painter(), x, y);
352 checkAndActivateInset(bv, x, y, button);
353 selection_start_cursor = selection_end_cursor = cursor;
354 current_font = real_current_font = GetFont(par, cursor.pos);
355 bv->text->FinishUndo();
356 UpdateLocal(bv, true);
360 void InsetText::InsetUnlock(BufferView * bv)
362 if (the_locking_inset) {
363 the_locking_inset->InsetUnlock(bv);
364 the_locking_inset = 0;
367 lyxerr[Debug::INSETS] << "InsetText::InsetUnlock(" << this <<
369 selection_start_cursor = selection_end_cursor = cursor;
370 no_selection = false;
372 UpdateLocal(bv, true);
376 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
378 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset(" << inset << "): ";
381 if (inset == par->GetInset(cursor.pos)) {
382 lyxerr[Debug::INSETS] << "OK" << endl;
383 the_locking_inset = inset;
384 resetPos(bv->painter());
385 inset_x = cursor.x - top_x + drawTextXOffset;
386 inset_y = cursor.y + drawTextYOffset;
387 inset_pos = cursor.pos;
389 } else if (the_locking_inset && (the_locking_inset == inset)) {
390 if (cursor.pos == inset_pos) {
391 lyxerr[Debug::INSETS] << "OK" << endl;
392 resetPos(bv->painter());
393 inset_x = cursor.x - top_x + drawTextXOffset;
394 inset_y = cursor.y + drawTextYOffset;
396 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
398 } else if (the_locking_inset) {
399 lyxerr[Debug::INSETS] << "MAYBE" << endl;
400 return the_locking_inset->LockInsetInInset(bv, inset);
402 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_cursor = selection_end_cursor = cursor;
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_cursor = selection_end_cursor = cursor;
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 LyXCursor old = selection_end_cursor;
521 setPos(bv->painter(), x, y);
522 selection_end_cursor = cursor;
523 if (old != selection_end_cursor)
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 = selection_start_cursor;
592 par->InsertChar(cursor.pos,arg[0]);
593 SetCharFont(cursor.pos,current_font);
595 selection_start_cursor = selection_end_cursor = cursor;
596 UpdateLocal(bv, true);
598 // --- Cursor Movements ---------------------------------------------
600 bv->text->FinishUndo();
601 moveRight(bv, false);
602 selection_end_cursor = cursor;
603 UpdateLocal(bv, false);
606 bv->text->FinishUndo();
607 result = moveRight(bv);
608 if (hasSelection()) {
609 selection_start_cursor = selection_end_cursor = cursor;
610 UpdateLocal(bv, false);
612 selection_start_cursor = selection_end_cursor = cursor;
616 bv->text->FinishUndo();
618 selection_end_cursor = cursor;
619 UpdateLocal(bv, false);
622 bv->text->FinishUndo();
623 result= moveLeft(bv);
624 if (hasSelection()) {
625 selection_start_cursor = selection_end_cursor = cursor;
626 UpdateLocal(bv, false);
628 selection_start_cursor = selection_end_cursor = cursor;
632 bv->text->FinishUndo();
634 selection_end_cursor = cursor;
635 UpdateLocal(bv, false);
638 bv->text->FinishUndo();
639 result = moveDown(bv);
640 if (hasSelection()) {
641 selection_start_cursor = selection_end_cursor = cursor;
642 UpdateLocal(bv, false);
644 selection_start_cursor = selection_end_cursor = cursor;
648 bv->text->FinishUndo();
650 selection_end_cursor = cursor;
651 UpdateLocal(bv, false);
654 bv->text->FinishUndo();
656 if (hasSelection()) {
657 selection_start_cursor = selection_end_cursor = cursor;
658 UpdateLocal(bv, false);
660 selection_start_cursor = selection_end_cursor = cursor;
665 if (hasSelection()) {
666 selection_start_cursor = selection_end_cursor = cursor;
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_cursor.pos;
680 for (; i < selection_end_cursor.pos; ++i) {
681 par->Erase(selection_start_cursor.pos);
685 if (ret) { // we need update
686 selection_start_cursor = selection_end_cursor = cursor;
687 UpdateLocal(bv, true);
688 } else if (hasSelection()) {
689 selection_start_cursor = selection_end_cursor = cursor;
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 = selection_end_cursor = selection_start_cursor;
703 UpdateLocal(bv, true);
704 } else if (hasSelection()) {
705 selection_start_cursor = selection_end_cursor = cursor;
706 UpdateLocal(bv, false);
708 resetPos(bv->painter());
711 bv->text->FinishUndo();
712 if (copySelection()) {
714 selection_start_cursor = selection_end_cursor = cursor;
715 UpdateLocal(bv, true);
716 } else if (hasSelection()) {
717 selection_start_cursor = selection_end_cursor = cursor;
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_cursor = selection_end_cursor = cursor;
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_cursor = selection_end_cursor = cursor;
740 UpdateLocal(bv, false);
742 selection_start_cursor = selection_end_cursor = cursor;
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_cursor = selection_end_cursor = cursor;
756 UpdateLocal(bv, false);
758 selection_start_cursor = selection_end_cursor = cursor;
761 resetPos(bv->painter());
763 case LFUN_BREAKPARAGRAPH:
767 bv->text->SetUndo(Undo::INSERT,
768 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
769 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
770 par->InsertChar(cursor.pos,LyXParagraph::META_NEWLINE);
771 SetCharFont(cursor.pos,current_font);
772 UpdateLocal(bv, true);
774 selection_start_cursor = selection_end_cursor = cursor;
775 resetPos(bv->painter());
779 // Derive layout number from given argument (string)
780 // and current buffer's textclass (number). */
781 LyXTextClassList::ClassList::size_type tclass =
782 buffer->params.textclass;
783 std::pair <bool, LyXTextClass::size_type> layout =
784 textclasslist.NumberOfLayout(tclass, arg);
786 // If the entry is obsolete, use the new one instead.
788 string obs = textclasslist.Style(tclass,layout.second).
791 layout = textclasslist.NumberOfLayout(tclass, obs);
794 // see if we found the layout number:
796 string msg = string(N_("Layout ")) + arg + N_(" not known");
798 bv->owner()->getMiniBuffer()->Set(msg);
802 if (current_layout != layout.second) {
803 bv->text->SetLayout(cursor, selection_start_cursor,
804 selection_end_cursor, layout.second);
805 bv->owner()->getToolbar()->combox->select(cursor.par->GetLayout()+1);
806 UpdateLocal(bv, true);
811 result = UNDISPATCHED;
814 if (result != FINISHED) {
817 bv->unlockInset(this);
822 int InsetText::Latex(ostream & os, bool /*fragile*/, bool /*fp*/) const
825 buffer->latexParagraphs(os, par, 0, texrow);
826 return texrow.rows();
830 void InsetText::Validate(LaTeXFeatures & features) const
832 par->validate(features);
836 // Returns the width of a character at a certain spot
837 int InsetText::SingleWidth(Painter & pain, LyXParagraph * par, int pos) const
839 LyXFont font = GetDrawFont(par, pos);
840 char c = par->GetChar(pos);
842 if (IsPrintable(c)) {
843 return lyxfont::width(c, font);
844 } else if (c == LyXParagraph::META_INSET) {
845 Inset const * tmpinset = par->GetInset(pos);
847 return tmpinset->width(pain, font);
850 } else if (IsSeparatorChar(c))
852 else if (IsNewlineChar(c))
854 return lyxfont::width(c, font);
858 // Returns the width of a character at a certain spot
859 void InsetText::SingleHeight(Painter & pain, LyXParagraph * par,int pos,
860 int & asc, int & desc) const
862 LyXFont font = GetDrawFont(par, pos);
863 char c = par->GetChar(pos);
866 if (c == LyXParagraph::META_INSET) {
867 Inset const * tmpinset=par->GetInset(pos);
869 asc = tmpinset->ascent(pain, font);
870 desc = tmpinset->descent(pain, font);
873 asc = lyxfont::maxAscent(font);
874 desc = lyxfont::maxDescent(font);
880 // Gets the fully instantiated font at a given position in a paragraph
881 // Basically the same routine as LyXParagraph::getFont() in paragraph.C.
882 // The difference is that this one is used for displaying, and thus we
883 // are allowed to make cosmetic improvements. For instance make footnotes
885 // If position is -1, we get the layout font of the paragraph.
886 // If position is -2, we get the font of the manual label of the paragraph.
887 LyXFont InsetText::GetFont(LyXParagraph * par, int pos) const
889 char par_depth = par->GetDepth();
891 LyXLayout const & layout =
892 textclasslist.Style(buffer->params.textclass, par->GetLayout());
894 // We specialize the 95% common case:
895 if (par->footnoteflag == LyXParagraph::NO_FOOTNOTE && !par_depth) {
898 if (layout.labeltype == LABEL_MANUAL
899 && pos < BeginningOfMainBody(par)) {
901 return par->GetFontSettings(pos).realize(layout.reslabelfont);
903 return par->GetFontSettings(pos).realize(layout.resfont);
906 // process layoutfont for pos == -1 and labelfont for pos < -1
908 return layout.resfont;
910 return layout.reslabelfont;
913 // The uncommon case need not be optimized as much
915 LyXFont layoutfont, tmpfont;
919 if (pos < BeginningOfMainBody(par)) {
921 layoutfont = layout.labelfont;
924 layoutfont = layout.font;
926 tmpfont = par->GetFontSettings(pos);
927 tmpfont.realize(layoutfont);
930 // process layoutfont for pos == -1 and labelfont for pos < -1
932 tmpfont = layout.font;
934 tmpfont = layout.labelfont;
937 // Resolve against environment font information
938 //if (par->GetDepth()){ // already in while condition
939 while (par && par_depth && !tmpfont.resolved()) {
940 par = par->DepthHook(par_depth - 1);
942 tmpfont.realize(textclasslist.Style(buffer->params.textclass,
943 par->GetLayout()).font);
944 par_depth = par->GetDepth();
947 tmpfont.realize((textclasslist.TextClass(buffer->params.textclass).
953 // the font for drawing may be different from the real font
954 LyXFont InsetText::GetDrawFont(LyXParagraph * par, int pos) const
956 return GetFont(par, pos);
960 int InsetText::BeginningOfMainBody(LyXParagraph * par) const
962 if (textclasslist.Style(buffer->params.textclass,
963 par->GetLayout()).labeltype != LABEL_MANUAL)
966 return par->BeginningOfMainBody();
970 void InsetText::GetCursorPos(int & x, int & y) const
977 int InsetText::InsetInInsetY()
979 if (!the_locking_inset)
982 return (inset_y + the_locking_inset->InsetInInsetY());
986 void InsetText::ToggleInsetCursor(BufferView * bv)
988 if (the_locking_inset) {
989 the_locking_inset->ToggleInsetCursor(bv);
993 LyXFont font = GetDrawFont(par, cursor.pos);
995 int asc = lyxfont::maxAscent(font);
996 int desc = lyxfont::maxDescent(font);
999 bv->hideLockedInsetCursor();
1001 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1002 cursor_visible = !cursor_visible;
1006 void InsetText::ShowInsetCursor(BufferView * bv)
1008 if (the_locking_inset) {
1009 the_locking_inset->ShowInsetCursor(bv);
1012 if (!cursor_visible) {
1013 LyXFont font = GetDrawFont(par, cursor.pos);
1015 int asc = lyxfont::maxAscent(font);
1016 int desc = lyxfont::maxDescent(font);
1017 bv->fitLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1018 bv->showLockedInsetCursor(cursor.x, cursor.y, asc, desc);
1019 cursor_visible = true;
1024 void InsetText::HideInsetCursor(BufferView * bv)
1026 if (cursor_visible) {
1027 bv->hideLockedInsetCursor();
1028 cursor_visible = false;
1030 if (the_locking_inset)
1031 the_locking_inset->HideInsetCursor(bv);
1035 void InsetText::setPos(Painter & pain, int x, int y) const
1037 x -= drawTextXOffset;
1038 y -= drawTextYOffset;
1039 // search right X-pos x==0 -> top_x
1040 cursor.pos = actrow = 0;
1041 cursor.y = top_baseline;
1043 for(unsigned int i = 1;
1044 (long(cursor.y + rows[i - 1].desc) < y)
1045 && (i < rows.size() - 1); ++i) {
1046 cursor.y = rows[i].baseline;
1047 cursor.pos = rows[i].pos;
1050 cursor.y -= top_baseline;
1051 cursor.x = top_x + 2; // 2 = frame width
1055 int sw = swh = SingleWidth(pain, par,cursor.pos);
1056 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1058 int checkpos = rows[actrow + 1].pos;
1059 if ((actrow+2) < (int)rows.size())
1061 while ((cursor.pos < checkpos) && ((cursor.x + swh) < x)) {
1064 sw = swh = SingleWidth(pain, par,cursor.pos);
1065 if (par->GetChar(cursor.pos)!=LyXParagraph::META_INSET)
1071 void InsetText::resetPos(Painter & pain) const
1078 int old_pos = cursor.pos;
1080 cursor.y = top_baseline;
1082 for(unsigned int i = 0;
1083 (i < (rows.size()-1)) && (rows[i].pos <= cursor.pos);
1085 cursor.y = rows[i].baseline;
1088 cursor.y -= top_baseline;
1089 setPos(pain, 0, cursor.y);
1090 cursor.x = top_x + 2; // 2 = frame width
1091 while(cursor.pos < old_pos) {
1092 cursor.x += SingleWidth(pain, par,cursor.pos);
1098 UpdatableInset::RESULT
1099 InsetText::moveRight(BufferView * bv, bool activate_inset)
1101 if (cursor.pos >= par->Last())
1103 if (activate_inset && checkAndActivateInset(bv)) {
1107 resetPos(bv->painter());
1108 real_current_font = current_font = GetFont(par, cursor.pos);
1109 return DISPATCHED_NOUPDATE;
1113 UpdatableInset::RESULT
1114 InsetText::moveLeft(BufferView * bv, bool activate_inset)
1116 if (cursor.pos <= 0)
1119 resetPos(bv->painter());
1121 if (checkAndActivateInset(bv, -1, -1))
1123 return DISPATCHED_NOUPDATE;
1127 UpdatableInset::RESULT
1128 InsetText::moveUp(BufferView * bv)
1132 cursor.y = rows[actrow - 1].baseline - top_baseline;
1133 if (cursor.x_fix < 0)
1134 cursor.x_fix = cursor.x;
1135 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1136 return DISPATCHED_NOUPDATE;
1140 UpdatableInset::RESULT
1141 InsetText::moveDown(BufferView * bv)
1143 if (actrow >= int(rows.size() - 2))
1145 cursor.y = rows[actrow + 1].baseline - top_baseline;
1146 if (cursor.x_fix < 0)
1147 cursor.x_fix = cursor.x;
1148 setPos(bv->painter(), cursor.x_fix-top_x+drawTextXOffset, cursor.y);
1149 return DISPATCHED_NOUPDATE;
1153 bool InsetText::Delete()
1155 if ((par->GetChar(cursor.pos)==LyXParagraph::META_INSET) &&
1156 !par->GetInset(cursor.pos)->Deletable()) {
1159 par->Erase(cursor.pos);
1164 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1166 if (the_locking_inset) {
1167 if (the_locking_inset->InsertInsetAllowed(inset))
1168 return the_locking_inset->InsertInset(bv, inset);
1171 bv->text->SetUndo(Undo::INSERT,
1172 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->previous,
1173 bv->text->cursor.par->ParFromPos(bv->text->cursor.pos)->next);
1174 if (inset->Editable() == Inset::IS_EDITABLE) {
1175 UpdatableInset *i = (UpdatableInset *)inset;
1176 i->setOwner((UpdatableInset *)this);
1178 par->InsertChar(cursor.pos, LyXParagraph::META_INSET);
1179 par->InsertInset(cursor.pos, inset);
1180 if (hasSelection()) {
1181 selection_start_cursor = selection_end_cursor = cursor;
1183 selection_start_cursor = selection_end_cursor = cursor;
1185 UpdateLocal(bv, true);
1186 static_cast<UpdatableInset*>(inset)->Edit(bv, 0, 0, 0);
1191 UpdatableInset * InsetText::GetLockingInset()
1193 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1197 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1201 if (the_locking_inset)
1202 return the_locking_inset->GetFirstLockingInsetOfType(c);
1207 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1209 // if there is no selection just set the current_font
1210 if (!hasSelection()) {
1211 // Determine basis font
1213 if (cursor.pos < BeginningOfMainBody(par))
1214 layoutfont = GetFont(par, -2);
1216 layoutfont = GetFont(par, -1);
1218 // Update current font
1219 real_current_font.update(font, bv->buffer()->params.language_info,
1222 // Reduce to implicit settings
1223 current_font = real_current_font;
1224 current_font.reduce(layoutfont);
1225 // And resolve it completely
1226 real_current_font.realize(layoutfont);
1231 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1232 s_start = selection_end_cursor.pos;
1233 s_end = selection_start_cursor.pos;
1235 s_start = selection_start_cursor.pos;
1236 s_end = selection_end_cursor.pos;
1239 while(s_start < s_end) {
1240 newfont = GetFont(par,s_start);
1241 newfont.update(font, bv->buffer()->params.language_info, toggleall);
1242 SetCharFont(s_start, newfont);
1245 UpdateLocal(bv, true);
1249 void InsetText::SetCharFont(int pos, LyXFont const & f)
1251 /* let the insets convert their font */
1254 if (par->GetChar(pos) == LyXParagraph::META_INSET) {
1255 if (par->GetInset(pos))
1256 font = par->GetInset(pos)->ConvertFont(font);
1258 LyXLayout const & layout =
1259 textclasslist.Style(buffer->params.textclass,par->GetLayout());
1261 // Get concrete layout font to reduce against
1264 if (pos < BeginningOfMainBody(par))
1265 layoutfont = layout.labelfont;
1267 layoutfont = layout.font;
1270 layoutfont.realize((textclasslist.TextClass(buffer->params.textclass).
1273 // Now, reduce font against full layout font
1274 font.reduce(layoutfont);
1276 par->SetFont(pos, font);
1280 void InsetText::computeTextRows(Painter & pain) const
1294 int width = wordAscent = wordDescent = 0;
1295 insetWidth = maxAscent = maxDescent = 0;
1300 rows.push_back(row);
1301 if (!autoBreakRows) {
1302 for(p = 0; p < par->Last(); ++p) {
1303 insetWidth += SingleWidth(pain, par, p);
1304 SingleHeight(pain, par, p, asc, desc);
1305 maxAscent = max(maxAscent, asc);
1306 maxDescent = max(maxDescent, desc);
1308 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1309 rows[0].asc = maxAscent;
1310 rows[0].desc = maxDescent;
1311 // alocate a dummy row for the endpos
1312 row.pos = par->Last();
1313 rows.push_back(row);
1317 bool is_first_word_in_row = true;
1318 int cw, lastWordWidth = 0;
1319 int maxWidth = getMaxTextWidth(pain, this);
1320 // if we auto break rows than the insetwidth should be always the max
1321 // width as the display is stable it may get larger if we have a really
1322 // large word below and we draw it!!!
1323 insetWidth = maxWidth;
1325 for(p = 0; p < par->Last(); ++p) {
1326 if (par->IsNewline(p)) {
1327 rows.back().asc = wordAscent;
1328 rows.back().desc = wordDescent;
1330 rows.push_back(row);
1332 width = lastWordWidth = 0;
1333 oasc = odesc = wordAscent = wordDescent = 0;
1334 is_first_word_in_row = true;
1337 cw = SingleWidth(pain, par, p);
1339 if (par->GetChar(p) == LyXParagraph::META_INSET)
1340 inset = par->GetInset(p);
1341 if (inset && inset->display()) {
1342 inset->setOwner(const_cast<InsetText *>(this));
1345 if (!is_first_word_in_row || (p != nwp)) {
1346 oasc = max(oasc, wordAscent);
1347 odesc = max(odesc, wordDescent);
1348 rows.back().asc = oasc;
1349 rows.back().desc = odesc;
1351 rows.push_back(row);
1353 SingleHeight(pain, par, p, asc, desc);
1354 rows.back().asc = asc;
1355 rows.back().desc = desc;
1356 row.pos = nwp = p + 1;
1357 rows.push_back(row);
1358 width = lastWordWidth = 0;
1359 oasc = odesc = wordAscent = wordDescent = 0;
1360 is_first_word_in_row = true;
1363 SingleHeight(pain, par, p, asc, desc);
1365 lastWordWidth += cw;
1366 if (width > maxWidth) {
1367 if (is_first_word_in_row) {
1368 if (!(width-cw)) { // only this character in word
1369 rows.back().asc = asc;
1370 rows.back().desc = desc;
1372 rows.push_back(row);
1378 lastWordWidth = width = 0;
1380 rows.back().asc = wordAscent;
1381 rows.back().desc = wordDescent;
1383 rows.push_back(row);
1388 lastWordWidth = width = cw;
1392 rows.back().asc = oasc;
1393 rows.back().desc = odesc;
1395 rows.push_back(row);
1397 odesc = wordDescent;
1398 width = lastWordWidth;
1399 wordAscent = max(wordAscent, asc);
1400 wordDescent = max(wordDescent, desc);
1401 is_first_word_in_row = true;
1404 wordAscent = max(wordAscent, asc);
1405 wordDescent = max(wordDescent, desc);
1407 if (par->IsSeparator(p) || inset) {
1409 inset->setOwner(const_cast<InsetText *>(this));
1413 oasc = max(oasc, wordAscent);
1414 odesc = max(odesc, wordDescent);
1415 wordAscent = wordDescent = lastWordWidth = 0;
1417 is_first_word_in_row = false;
1420 // if we have some data in the paragraph we have ascent/descent
1422 // assign last row data
1423 rows.back().asc = max(oasc, wordAscent);
1424 rows.back().desc = max(odesc, wordDescent);
1426 insetWidth += (2 * TEXT_TO_INSET_OFFSET);
1427 // alocate a dummy row for the endpos
1428 row.pos = par->Last();
1429 rows.push_back(row);
1430 // calculate maxAscent/Descent
1431 maxAscent = rows[0].asc;
1432 maxDescent = rows[0].desc;
1433 for (RowList::size_type i = 1; i < rows.size() - 1; ++i) {
1434 maxDescent += rows[i].asc + rows[i].desc + interline_space;
1439 void InsetText::computeBaselines(int baseline) const
1441 rows[0].baseline = baseline;
1442 for (unsigned int i = 1; i < rows.size() - 1; i++) {
1443 rows[i].baseline = rows[i - 1].baseline + rows[i - 1].desc +
1444 rows[i].asc + interline_space;
1449 void InsetText::UpdateLocal(BufferView * bv, bool flag)
1452 computeTextRows(bv->painter());
1453 computeBaselines(top_baseline);
1455 bv->updateInset(this, flag);
1457 resetPos(bv->painter());
1458 bv->owner()->getToolbar()->combox->select(cursor.par->GetLayout()+1);
1462 bool InsetText::cutSelection()
1464 if (!hasSelection())
1469 LyXParagraph * endpar = par;
1471 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1472 start = selection_end_cursor.pos;
1473 end = selection_start_cursor.pos;
1475 start = selection_start_cursor.pos;
1476 end = selection_end_cursor.pos;
1479 return cap.cutSelection(par, &endpar, start, end,buffer->params.textclass);
1483 bool InsetText::copySelection()
1485 if (!hasSelection())
1491 if (selection_start_cursor.pos > selection_end_cursor.pos) {
1492 start = selection_end_cursor.pos;
1493 end = selection_start_cursor.pos;
1495 start = selection_start_cursor.pos;
1496 end = selection_end_cursor.pos;
1498 return cap.copySelection(par, par, start, end, buffer->params.textclass);
1502 bool InsetText::pasteSelection()
1506 if (cap.nrOfParagraphs() > 1) {
1507 WriteAlert(_("Impossible operation"),
1508 _("Cannot include more than one paragraph!"),
1512 LyXParagraph *endpar;
1513 LyXParagraph *actpar = par;
1515 return cap.pasteSelection(&actpar, &endpar, cursor.pos,
1516 buffer->params.textclass);
1520 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1523 if (par->GetChar(cursor.pos) == LyXParagraph::META_INSET) {
1524 UpdatableInset * inset =
1525 static_cast<UpdatableInset*>(par->GetInset(cursor.pos));
1526 LyXFont font = GetFont(par, cursor.pos);
1528 x = inset->width(bv->painter(), font);
1530 y = inset->descent(bv->painter(), font);
1531 inset_x = cursor.x - top_x + drawTextXOffset;
1532 inset_y = cursor.y + drawTextYOffset;
1533 inset->Edit(bv, x-inset_x, y-inset_y, button);
1534 if (!the_locking_inset)
1536 UpdateLocal(bv, true);
1543 int InsetText::getMaxTextWidth(Painter & pain, UpdatableInset const * inset) const
1545 // int w=getMaxWidth(pain, inset);
1547 return getMaxWidth(pain, inset) - 4; // 2+2 width of eventual border
1550 void InsetText::SetParagraphData(LyXParagraph *p)
1555 par->SetInsetOwner(this);
1559 void InsetText::SetAutoBreakRows(bool flag)
1561 if (flag != autoBreakRows) {
1562 autoBreakRows = flag;
1567 void InsetText::SetDrawLockedFrame(bool flag)
1569 if (flag != drawLockedFrame) {
1570 drawLockedFrame = flag;
1575 void InsetText::SetFrameColor(LColor::color col)
1577 if (frame_color != col) {