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"
42 #include "support/textutils.h"
43 #include "support/LAssert.h"
47 #include "trans_mgr.h"
48 #include "lyxscreen.h"
57 extern unsigned char getCurrentTextClass(Buffer *);
58 extern bool math_insert_greek(BufferView *, char);
59 extern int greek_kb_flag;
61 InsetText::InsetText()
63 par = new LyXParagraph;
68 InsetText::InsetText(InsetText const & ins)
73 autoBreakRows = ins.autoBreakRows;
77 InsetText & InsetText::operator=(InsetText const & it)
80 autoBreakRows = it.autoBreakRows;
85 void InsetText::init(InsetText const * ins)
93 the_locking_inset = 0;
95 cursor_visible = false;
102 autoBreakRows = false;
106 SetParagraphData(ins->par);
107 autoBreakRows = ins->autoBreakRows;
108 drawFrame_ = ins->drawFrame_;
110 par->SetInsetOwner(this);
111 frame_color = LColor::insetframe;
114 last_drawn_width = -1;
115 frame_is_visible = false;
119 InsetText::~InsetText()
121 // delete all instances of LyXText before deleting the paragraps used
123 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
124 delete (*cit).second;
128 LyXParagraph * p = par->next_;
137 LyXParagraph * tmp = par->next();
145 void InsetText::clear()
147 // delete all instances of LyXText before deleting the paragraps used
149 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
150 delete (*cit).second;
154 LyXParagraph * p = par->next_;
163 LyXParagraph * tmp = par->next();
168 par = new LyXParagraph;
172 Inset * InsetText::Clone(Buffer const &) const
174 InsetText * t = new InsetText(*this);
179 void InsetText::Write(Buffer const * buf, ostream & os) const
182 WriteParagraphData(buf, os);
186 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
188 par->writeFile(buf, os, buf->params, 0, 0);
192 void InsetText::Read(Buffer const * buf, LyXLex & lex)
196 LyXParagraph * return_par = 0;
197 char depth = 0; // signed or unsigned?
199 LyXParagraph::footnote_flag footnoteflag = LyXParagraph::NO_FOOTNOTE;
200 LyXParagraph::footnote_kind footnotekind = LyXParagraph::FOOTNOTE;
202 LyXFont font(LyXFont::ALL_INHERIT);
204 // delete all instances of LyXText before deleting the paragraps used
206 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
207 delete (*cit).second;
212 LyXParagraph * p = par->next_;
221 LyXParagraph * tmp = par->next();
226 par = new LyXParagraph;
229 token = lex.GetString();
232 if (token == "\\end_inset")
234 if (const_cast<Buffer*>(buf)->
235 parseSingleLyXformat2Token(lex, par, return_par,
236 token, pos, depth, font
238 , footnoteflag, footnotekind
242 // the_end read this should NEVER happen
243 lex.printError("\\the_end read in inset! Error in document!");
251 return_par->SetInsetOwner(this);
253 return_par = return_par->next_;
255 return_par = return_par->next();
259 if (token != "\\end_inset") {
260 lex.printError("Missing \\end_inset at this point. "
267 int InsetText::ascent(BufferView * bv, LyXFont const &) const
270 Row * row = TEXT(bv)->GetRowNearY(y_temp);
271 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
276 int InsetText::descent(BufferView * bv, LyXFont const &) const
279 Row * row = TEXT(bv)->GetRowNearY(y_temp);
280 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
281 TEXT_TO_INSET_OFFSET;
286 int InsetText::width(BufferView * bv, LyXFont const &) const
288 insetWidth = max(textWidth(bv),
289 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
294 int InsetText::textWidth(BufferView * bv) const
296 int const w = getMaxWidth(bv, this);
301 void InsetText::draw(BufferView * bv, LyXFont const & f,
302 int baseline, float & x, bool cleared) const
307 Painter & pain = bv->painter();
309 // no draw is necessary !!!
310 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
312 top_baseline = baseline;
314 if (!cleared && (need_update & CLEAR_FRAME))
315 clearFrame(pain, cleared);
322 UpdatableInset::draw(bv, f, baseline, x, cleared);
325 x += (float)scroll();
327 // update insetWidth and insetHeight with dummy calls
329 (void)descent(bv, f);
332 // if top_x differs we have a rule down and we don't have to clear anything
333 if (!cleared && (top_x == int(x)) &&
334 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
335 (last_drawn_width!=insetWidth)))
338 int h = insetAscent + insetDescent;
339 int ty = baseline - insetAscent;
345 if ((ty + h) > pain.paperHeight())
346 h = pain.paperHeight();
347 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
348 w = pain.paperWidth();
349 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
353 if (!cleared && (need_update == NONE))
356 if (top_x != int(x)) {
359 bv->text->status = LyXText::CHANGED_IN_DRAW;
363 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
365 if (cleared || (last_drawn_width != insetWidth)) {
367 last_drawn_width = insetWidth;
370 top_baseline = baseline;
371 top_y = baseline - ascent(bv, f);
372 last_width = width(bv, f);
373 last_height = ascent(bv, f) + descent(bv, f);
375 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
376 inset_x = cx(bv) - top_x + drawTextXOffset;
377 inset_y = cy(bv) + drawTextYOffset;
379 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
380 drawFrame(pain, cleared);
385 x += TEXT_TO_INSET_OFFSET;
388 #warning Jürgen, why is this a block of its own? (Lgb)
389 #warning because you told me to define variables only in local context (Jug)!
393 Row * row = TEXT(bv)->GetRowNearY(y);
394 int y_offset = baseline - row->ascent_of_text();
395 int ph = pain.paperHeight();
398 while ((row != 0) && ((y+row->height()) <= 0)) {
400 first += row->height();
405 TEXT(bv)->first = first;
409 while ((row != 0) && (yf < ph)) {
410 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
416 } else if (!locked) {
417 if (need_update & CURSOR) {
418 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
419 TEXT(bv)->ClearSelection(bv);
420 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
422 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
425 if (need_update & SELECTION)
426 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
427 else if (need_update & CURSOR) {
428 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
429 TEXT(bv)->ClearSelection(bv);
430 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
432 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
436 TEXT(bv)->refresh_y = 0;
437 TEXT(bv)->status = LyXText::UNCHANGED;
438 if ((need_update != CURSOR_PAR) &&
439 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
440 drawFrame(pain, cleared);
441 else if (need_update & CLEAR_FRAME)
442 clearFrame(pain, cleared);
443 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
444 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
446 } else if (need_update != INIT)
451 void InsetText::drawFrame(Painter & pain, bool cleared) const
453 if (!frame_is_visible || cleared) {
454 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
455 insetWidth - 1, insetAscent + insetDescent - 1,
457 frame_is_visible = true;
462 void InsetText::clearFrame(Painter & pain, bool cleared) const
464 if (frame_is_visible) {
466 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
467 insetWidth - 1, insetAscent + insetDescent - 1,
470 frame_is_visible = false;
475 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
481 owner()->update(bv, font, true);
484 if (the_locking_inset) {
485 inset_x = cx(bv) - top_x + drawTextXOffset;
486 inset_y = cy(bv) + drawTextYOffset;
487 the_locking_inset->update(bv, font, reinit);
490 if (need_update == INIT) {
495 int oldw = insetWidth;
496 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
497 if (oldw != insetWidth) {
500 // update(bv, font, reinit);
503 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
506 TEXT(bv)->UpdateInset(bv, the_locking_inset);
509 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
513 Row * row = TEXT(bv)->GetRowNearY(y_temp);
514 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
515 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
516 TEXT_TO_INSET_OFFSET;
521 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
524 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
526 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
527 need_update |= CURSOR_PAR;
529 // this to not draw a selection when we redraw all of it!
530 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
531 TEXT(bv)->ClearSelection(bv);
535 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
537 TEXT(bv)->FullRebreak(bv);
538 SetUpdateStatus(bv, what);
539 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
541 bv->updateInset(this, mark_dirty);
542 bv->owner()->showState();
543 if (old_par != cpar(bv)) {
544 bv->owner()->setLayout(cpar(bv)->GetLayout());
550 string const InsetText::EditMessage() const
552 return _("Opened Text Inset");
556 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
558 // par->SetInsetOwner(this);
559 UpdatableInset::Edit(bv, x, y, button);
561 if (!bv->lockInset(this)) {
562 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
566 the_locking_inset = 0;
567 inset_pos = inset_x = inset_y = 0;
568 inset_boundary = false;
571 if (!checkAndActivateInset(bv, x, y, button))
572 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
574 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
575 bv->text->FinishUndo();
577 UpdateLocal(bv, CURSOR, false);
579 // If the inset is empty set the language of the current font to the
580 // language to the surronding text.
582 if (par->Last() == 0 && !par->next_)
584 if (par->size() == 0 && !par->next())
587 LyXFont font(LyXFont::ALL_IGNORE);
588 font.setLanguage(bv->getParentLanguage(this));
589 SetFont(bv, font, false);
594 void InsetText::InsetUnlock(BufferView * bv)
596 if (the_locking_inset) {
597 the_locking_inset->InsetUnlock(bv);
598 the_locking_inset = 0;
601 no_selection = false;
603 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
605 bv->owner()->setLayout(owner()->getLyXText(bv)
606 ->cursor.par()->GetLayout());
608 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
612 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
614 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
618 if (inset == cpar(bv)->GetInset(cpos(bv))) {
619 lyxerr[Debug::INSETS] << "OK" << endl;
620 the_locking_inset = inset;
621 inset_x = cx(bv) - top_x + drawTextXOffset;
622 inset_y = cy(bv) + drawTextYOffset;
623 inset_pos = cpos(bv);
624 inset_par = cpar(bv);
625 inset_boundary = cboundary(bv);
627 TEXT(bv)->ClearSelection(bv);
628 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
629 TEXT(bv)->UpdateInset(bv, the_locking_inset);
631 UpdateLocal(bv, CURSOR, false);
634 } else if (the_locking_inset && (the_locking_inset == inset)) {
635 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
636 lyxerr[Debug::INSETS] << "OK" << endl;
637 inset_x = cx(bv) - top_x + drawTextXOffset;
638 inset_y = cy(bv) + drawTextYOffset;
640 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
642 } else if (the_locking_inset) {
643 lyxerr[Debug::INSETS] << "MAYBE" << endl;
644 return the_locking_inset->LockInsetInInset(bv, inset);
646 lyxerr[Debug::INSETS] << "NOT OK" << endl;
651 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
654 if (!the_locking_inset)
656 if (the_locking_inset == inset) {
657 the_locking_inset->InsetUnlock(bv);
658 TEXT(bv)->UpdateInset(bv, inset);
659 the_locking_inset = 0;
661 moveRight(bv, false);
662 old_par = 0; // force layout setting
666 UpdateLocal(bv, CURSOR, false);
669 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
673 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
675 if (!the_locking_inset)
677 if (the_locking_inset != inset) {
678 TEXT(bv)->UpdateInset(bv, the_locking_inset);
679 SetUpdateStatus(bv, CURSOR_PAR);
680 return the_locking_inset->UpdateInsetInInset(bv, inset);
682 // UpdateLocal(bv, FULL, false);
683 if (TEXT(bv)->UpdateInset(bv, inset))
684 UpdateLocal(bv, CURSOR_PAR, false);
685 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
686 inset_x = cx(bv) - top_x + drawTextXOffset;
687 inset_y = cy(bv) + drawTextYOffset;
693 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
697 int tmp_x = x - drawTextXOffset;
698 int tmp_y = y + insetAscent - TEXT(bv)->first;
699 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
702 if (the_locking_inset) {
703 if (the_locking_inset == inset) {
704 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
705 no_selection = false;
708 // otherwise unlock the_locking_inset and lock the new inset
709 the_locking_inset->InsetUnlock(bv);
710 inset_x = cx(bv) - top_x + drawTextXOffset;
711 inset_y = cy(bv) + drawTextYOffset;
712 the_locking_inset = static_cast<UpdatableInset*>(inset);
713 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
714 inset->Edit(bv, x - inset_x, y - inset_y, button);
715 if (the_locking_inset)
716 UpdateLocal(bv, CURSOR, false);
717 no_selection = false;
720 // otherwise only unlock the_locking_inset
721 the_locking_inset->InsetUnlock(bv);
722 the_locking_inset = 0;
724 if (bv->theLockingInset()) {
725 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
726 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
727 inset_x = cx(bv) - top_x + drawTextXOffset;
728 inset_y = cy(bv) + drawTextYOffset;
729 inset_pos = cpos(bv);
730 inset_par = cpar(bv);
731 inset_boundary = cboundary(bv);
732 the_locking_inset = uinset;
733 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
735 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
736 if (the_locking_inset)
737 UpdateLocal(bv, CURSOR, false);
738 no_selection = false;
742 if (!inset && (button == 2)) {
743 bool paste_internally = false;
744 if ((button == 2) && TEXT(bv)->selection) {
745 LocalDispatch(bv, LFUN_COPY, "");
746 paste_internally = true;
748 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
750 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
751 UpdateLocal(bv, CURSOR, false);
752 bv->owner()->setLayout(cpar(bv)->GetLayout());
754 // Insert primary selection with middle mouse
755 // if there is a local selection in the current buffer,
758 if (paste_internally)
759 LocalDispatch(bv, LFUN_PASTE, "");
761 LocalDispatch(bv, LFUN_PASTESELECTION,
766 no_selection = false;
770 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
772 UpdatableInset * inset = 0;
774 if (the_locking_inset) {
775 the_locking_inset->InsetButtonRelease(bv,
776 x - inset_x, y - inset_y,
779 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
780 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
781 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
782 inset->InsetButtonRelease(bv,
784 y - inset_y, button);
786 inset_x = cx(bv) - top_x + drawTextXOffset;
787 inset_y = cy(bv) + drawTextYOffset;
788 inset->InsetButtonRelease(bv,
790 y - inset_y, button);
792 x - inset_x, y - inset_y, button);
794 UpdateLocal(bv, CURSOR_PAR, false);
797 no_selection = false;
801 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
805 if (the_locking_inset) {
806 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
811 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
813 TEXT(bv)->SetSelection(bv);
814 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
815 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
816 UpdateLocal(bv, SELECTION, false);
821 void InsetText::InsetKeyPress(XKeyEvent * xke)
823 if (the_locking_inset) {
824 the_locking_inset->InsetKeyPress(xke);
830 UpdatableInset::RESULT
831 InsetText::LocalDispatch(BufferView * bv,
832 kb_action action, string const & arg)
834 no_selection = false;
835 UpdatableInset::RESULT
836 result= UpdatableInset::LocalDispatch(bv, action, arg);
837 if (result != UNDISPATCHED) {
842 if ((action < 0) && arg.empty())
845 if (the_locking_inset) {
846 result = the_locking_inset->LocalDispatch(bv, action, arg);
847 if (result == DISPATCHED_NOUPDATE)
849 else if (result == DISPATCHED) {
850 UpdateLocal(bv, CURSOR_PAR, false);
852 } else if (result == FINISHED) {
853 bool dispatched = false;
855 case LFUN_UNKNOWN_ACTION:
856 case LFUN_BREAKPARAGRAPH:
858 moveRightIntern(bv, false, false);
861 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
862 moveRightIntern(bv, false, false);
866 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
867 moveRightIntern(bv, false, false);
873 the_locking_inset = 0;
881 case LFUN_UNKNOWN_ACTION:
882 if (bv->buffer()->isReadonly()) {
883 // setErrorMessage(N_("Document is read only"));
887 /* Automatically delete the currently selected
888 * text and replace it with what is being
889 * typed in now. Depends on lyxrc settings
890 * "auto_region_delete", which defaults to
893 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
895 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
896 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
898 bv->text->cursor.par()->previous(),
899 bv->text->cursor.par()->next()
903 if (lyxrc.auto_region_delete) {
904 if (TEXT(bv)->selection){
905 TEXT(bv)->CutSelection(bv, false);
908 TEXT(bv)->ClearSelection(bv);
909 for (string::size_type i = 0; i < arg.length(); ++i) {
911 if (!math_insert_greek(bv, arg[i])) {
912 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
913 } else if (!the_locking_inset) {
914 (void)moveRight(bv, false);
917 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
921 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
922 UpdateLocal(bv, CURSOR_PAR, true);
923 result=DISPATCHED_NOUPDATE;
925 // --- Cursor Movements -----------------------------------
927 bv->text->FinishUndo();
928 moveRight(bv, false, true);
929 TEXT(bv)->SetSelection(bv);
930 UpdateLocal(bv, SELECTION, false);
933 result = moveRight(bv);
934 bv->text->FinishUndo();
935 UpdateLocal(bv, CURSOR, false);
938 bv->text->FinishUndo();
939 moveLeft(bv, false, true);
940 TEXT(bv)->SetSelection(bv);
941 UpdateLocal(bv, SELECTION, false);
944 bv->text->FinishUndo();
945 result = moveLeft(bv);
946 UpdateLocal(bv, CURSOR, false);
949 bv->text->FinishUndo();
951 TEXT(bv)->SetSelection(bv);
952 UpdateLocal(bv, SELECTION, false);
955 bv->text->FinishUndo();
956 result = moveDown(bv);
957 UpdateLocal(bv, CURSOR, false);
960 bv->text->FinishUndo();
962 TEXT(bv)->SetSelection(bv);
963 UpdateLocal(bv, SELECTION, false);
966 bv->text->FinishUndo();
968 UpdateLocal(bv, CURSOR, false);
971 bv->text->FinishUndo();
972 TEXT(bv)->CursorHome(bv);
973 UpdateLocal(bv, CURSOR, false);
976 TEXT(bv)->CursorEnd(bv);
977 UpdateLocal(bv, CURSOR, false);
980 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
982 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
983 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
985 bv->text->cursor.par()->previous(),
986 bv->text->cursor.par()->next()
989 if (TEXT(bv)->selection)
990 TEXT(bv)->CutSelection(bv);
992 TEXT(bv)->Backspace(bv);
993 UpdateLocal(bv, CURSOR_PAR, true);
996 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
998 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
999 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1001 bv->text->cursor.par()->previous(),
1002 bv->text->cursor.par()->next()
1005 if (TEXT(bv)->selection)
1006 TEXT(bv)->CutSelection(bv);
1008 TEXT(bv)->Delete(bv);
1009 UpdateLocal(bv, CURSOR_PAR, true);
1012 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
1014 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1015 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1017 bv->text->cursor.par()->previous(),
1018 bv->text->cursor.par()->next()
1021 TEXT(bv)->CutSelection(bv);
1022 UpdateLocal(bv, CURSOR_PAR, true);
1025 bv->text->FinishUndo();
1026 TEXT(bv)->CopySelection(bv);
1027 UpdateLocal(bv, CURSOR_PAR, false);
1029 case LFUN_PASTESELECTION:
1031 string clip(bv->workarea()->getClipboard());
1035 if (arg == "paragraph") {
1036 TEXT(bv)->InsertStringB(bv, clip);
1038 TEXT(bv)->InsertStringA(bv, clip);
1040 UpdateLocal(bv, CURSOR_PAR, true);
1044 if (!autoBreakRows) {
1047 if (cap.nrOfParagraphs() > 1) {
1048 WriteAlert(_("Impossible operation"),
1049 _("Cannot include more than one paragraph!"),
1054 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1056 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1057 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1059 bv->text->cursor.par()->previous(),
1060 bv->text->cursor.par()->next()
1063 TEXT(bv)->PasteSelection(bv);
1064 UpdateLocal(bv, CURSOR_PAR, true);
1066 case LFUN_BREAKPARAGRAPH:
1069 TEXT(bv)->BreakParagraph(bv, 0);
1070 UpdateLocal(bv, FULL, true);
1072 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1075 TEXT(bv)->BreakParagraph(bv, 1);
1076 UpdateLocal(bv, FULL, true);
1078 case LFUN_BREAKLINE:
1081 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1083 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1084 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1086 bv->text->cursor.par()->previous(),
1087 bv->text->cursor.par()->next()
1090 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1091 UpdateLocal(bv, CURSOR_PAR, true);
1094 // do not set layouts on non breakable textinsets
1095 if (autoBreakRows) {
1096 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1098 // Derive layout number from given argument (string)
1099 // and current buffer's textclass (number). */
1100 LyXTextClassList::ClassList::size_type tclass =
1101 bv->buffer()->params.textclass;
1102 std::pair <bool, LyXTextClass::size_type> layout =
1103 textclasslist.NumberOfLayout(tclass, arg);
1105 // If the entry is obsolete, use the new one instead.
1107 string obs = textclasslist.Style(tclass,layout.second).
1110 layout = textclasslist.NumberOfLayout(tclass, obs);
1113 // see if we found the layout number:
1114 if (!layout.first) {
1115 string msg = string(N_("Layout ")) + arg + N_(" not known");
1117 bv->owner()->getMiniBuffer()->Set(msg);
1121 if (cur_layout != layout.second) {
1122 cur_layout = layout.second;
1123 TEXT(bv)->SetLayout(bv, layout.second);
1124 bv->owner()->setLayout(cpar(bv)->GetLayout());
1125 UpdateLocal(bv, CURSOR_PAR, true);
1128 // reset the layout box
1129 bv->owner()->setLayout(cpar(bv)->GetLayout());
1132 case LFUN_PARAGRAPH_SPACING:
1133 // This one is absolutely not working. When fiddling with this
1134 // it also seems to me that the paragraphs inside the insettext
1135 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1137 LyXParagraph * par = TEXT(bv)->cursor.par();
1138 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1139 float cur_value = 1.0;
1140 if (cur_spacing == Spacing::Other) {
1141 cur_value = par->params.spacing().getValue();
1144 std::istringstream istr(arg.c_str());
1147 Spacing::Space new_spacing = cur_spacing;
1148 float new_value = cur_value;
1150 lyxerr << "Missing argument to `paragraph-spacing'"
1152 } else if (tmp == "single") {
1153 new_spacing = Spacing::Single;
1154 } else if (tmp == "onehalf") {
1155 new_spacing = Spacing::Onehalf;
1156 } else if (tmp == "double") {
1157 new_spacing = Spacing::Double;
1158 } else if (tmp == "other") {
1159 new_spacing = Spacing::Other;
1162 lyxerr << "new_value = " << tmpval << endl;
1165 } else if (tmp == "default") {
1166 new_spacing = Spacing::Default;
1168 lyxerr << _("Unknown spacing argument: ")
1171 if (cur_spacing != new_spacing || cur_value != new_value) {
1172 par->params.spacing(Spacing(new_spacing, new_value));
1173 //TEXT(bv)->RedoParagraph(owner->view());
1174 UpdateLocal(bv, CURSOR_PAR, true);
1175 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1181 if (!bv->Dispatch(action, arg))
1182 result = UNDISPATCHED;
1186 /// If the action has deleted all text in the inset, we need to change the
1187 // language to the language to the surronding text.
1189 if (par->Last() == 0 && !par->next_)
1191 if (par->size() == 0 && !par->next())
1194 LyXFont font(LyXFont::ALL_IGNORE);
1195 font.setLanguage(bv->getParentLanguage(this));
1196 SetFont(bv, font, false);
1199 if (result != FINISHED) {
1200 ShowInsetCursor(bv);
1202 bv->unlockInset(this);
1207 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1210 buf->latexParagraphs(os, par, 0, texrow);
1211 return texrow.rows();
1215 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1217 LyXParagraph * p = par;
1218 unsigned int lines = 0;
1222 tmp = buf->asciiParagraph(p, linelen);
1223 lines += countChar(tmp, '\n');
1235 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1237 LyXParagraph * p = par;
1238 unsigned int lines = 0;
1243 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1255 void InsetText::Validate(LaTeXFeatures & features) const
1257 LyXParagraph * p = par;
1259 p->validate(features);
1269 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1271 if (textclasslist.Style(buf->params.textclass,
1272 p->GetLayout()).labeltype != LABEL_MANUAL)
1275 return p->BeginningOfMainBody();
1279 void InsetText::GetCursorPos(BufferView * bv,
1280 int & x, int & y) const
1287 unsigned int InsetText::InsetInInsetY()
1289 if (!the_locking_inset)
1292 return (inset_y + the_locking_inset->InsetInInsetY());
1296 void InsetText::ToggleInsetCursor(BufferView * bv)
1298 if (the_locking_inset) {
1299 the_locking_inset->ToggleInsetCursor(bv);
1303 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1305 int const asc = lyxfont::maxAscent(font);
1306 int const desc = lyxfont::maxDescent(font);
1308 if (isCursorVisible())
1309 bv->hideLockedInsetCursor();
1311 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1312 toggleCursorVisible();
1316 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1318 if (the_locking_inset) {
1319 the_locking_inset->ShowInsetCursor(bv);
1322 if (!isCursorVisible()) {
1323 LyXFont const font =
1324 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1326 int const asc = lyxfont::maxAscent(font);
1327 int const desc = lyxfont::maxDescent(font);
1329 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1331 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1332 setCursorVisible(true);
1337 void InsetText::HideInsetCursor(BufferView * bv)
1339 if (isCursorVisible()) {
1340 bv->hideLockedInsetCursor();
1341 setCursorVisible(false);
1343 if (the_locking_inset)
1344 the_locking_inset->HideInsetCursor(bv);
1348 UpdatableInset::RESULT
1349 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1351 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1352 return moveLeftIntern(bv, false, activate_inset, selecting);
1354 return moveRightIntern(bv, false, activate_inset, selecting);
1357 UpdatableInset::RESULT
1358 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1360 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1361 return moveRightIntern(bv, true, activate_inset, selecting);
1363 return moveLeftIntern(bv, true, activate_inset, selecting);
1367 UpdatableInset::RESULT
1368 InsetText::moveRightIntern(BufferView * bv, bool behind,
1369 bool activate_inset, bool selecting)
1372 if (!cpar(bv)->next_ && (cpos(bv) >= cpar(bv)->Last()))
1374 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1377 if (activate_inset && checkAndActivateInset(bv, behind))
1379 TEXT(bv)->CursorRight(bv);
1381 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1382 return DISPATCHED_NOUPDATE;
1386 UpdatableInset::RESULT
1387 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1388 bool activate_inset, bool selecting)
1391 if (!cpar(bv)->previous_ && (cpos(bv) <= 0))
1393 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1396 TEXT(bv)->CursorLeft(bv);
1398 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1399 if (activate_inset && checkAndActivateInset(bv, behind))
1401 return DISPATCHED_NOUPDATE;
1405 UpdatableInset::RESULT
1406 InsetText::moveUp(BufferView * bv)
1408 if (!crow(bv)->previous())
1410 TEXT(bv)->CursorUp(bv);
1411 return DISPATCHED_NOUPDATE;
1415 UpdatableInset::RESULT
1416 InsetText::moveDown(BufferView * bv)
1418 if (!crow(bv)->next())
1420 TEXT(bv)->CursorDown(bv);
1421 return DISPATCHED_NOUPDATE;
1425 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1427 if (the_locking_inset) {
1428 if (the_locking_inset->InsertInsetAllowed(inset))
1429 return the_locking_inset->InsertInset(bv, inset);
1432 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1434 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1435 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1437 bv->text->cursor.par()->previous(),
1438 bv->text->cursor.par()->next()
1441 inset->setOwner(this);
1442 HideInsetCursor(bv);
1443 TEXT(bv)->InsertInset(bv, inset);
1445 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1446 (cpar(bv)->GetInset(cpos(bv)) != inset))
1447 TEXT(bv)->CursorLeft(bv);
1449 bv->fitCursor(TEXT(bv));
1450 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1451 ShowInsetCursor(bv);
1456 UpdatableInset * InsetText::GetLockingInset()
1458 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1462 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1466 if (the_locking_inset)
1467 return the_locking_inset->GetFirstLockingInsetOfType(c);
1472 bool InsetText::ShowInsetDialog(BufferView * bv) const
1474 if (the_locking_inset)
1475 return the_locking_inset->ShowInsetDialog(bv);
1480 std::vector<string> const InsetText::getLabelList() const
1482 std::vector<string> label_list;
1484 LyXParagraph * tpar = par;
1486 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1487 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1488 for (; beg != end; ++beg) {
1489 std::vector<string> const l = (*beg)->getLabelList();
1490 label_list.insert(label_list.end(), l.begin(), l.end());
1492 tpar = tpar->next();
1498 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1500 if (TEXT(bv)->selection) {
1501 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1503 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->previous_,
1504 bv->text->cursor.par()->ParFromPos(bv->text->cursor.pos())->next_
1506 bv->text->cursor.par()->previous(),
1507 bv->text->cursor.par()->next()
1511 TEXT(bv)->SetFont(bv, font, toggleall);
1512 bv->fitCursor(TEXT(bv));
1513 UpdateLocal(bv, CURSOR_PAR, true);
1517 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1519 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1523 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1524 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1526 LyXFont const font =
1527 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1529 x = inset->width(bv, font);
1530 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1533 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1535 //inset_x = cx(bv) - top_x + drawTextXOffset;
1536 //inset_y = cy(bv) + drawTextYOffset;
1537 inset->Edit(bv, x, y, 0);
1538 if (!the_locking_inset)
1540 UpdateLocal(bv, CURSOR, false);
1547 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1552 dummyx = x = x - drawTextXOffset;
1553 dummyy = y + insetAscent;
1554 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1561 inset_x = cx(bv) - top_x + drawTextXOffset;
1562 inset_y = cy(bv) + drawTextYOffset;
1563 inset->Edit(bv, x - inset_x, y - inset_y, button);
1564 if (!the_locking_inset)
1566 UpdateLocal(bv, CURSOR, false);
1573 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1575 int w = UpdatableInset::getMaxWidth(bv, inset);
1580 w = w - top_x + owner()->x();
1583 w -= (2 * TEXT_TO_INSET_OFFSET);
1585 // return w - (2*TEXT_TO_INSET_OFFSET);
1589 void InsetText::SetParagraphData(LyXParagraph * p)
1591 // delete all instances of LyXText before deleting the paragraps used
1593 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1594 delete (*cit).second;
1610 par->SetInsetOwner(this);
1614 np->next(p->Clone());
1615 np->next_->previous(np);
1617 np->SetInsetOwner(this);
1621 LyXParagraph * tmp = par->next();
1627 par->SetInsetOwner(this);
1628 LyXParagraph * np = par;
1631 np->next(p->Clone());
1632 np->next()->previous(np);
1634 np->SetInsetOwner(this);
1641 void InsetText::SetText(string const & data)
1644 LyXFont font(LyXFont::ALL_SANE);
1645 for (unsigned int i=0; i < data.length(); ++i)
1646 par->InsertChar(i, data[i], font);
1650 void InsetText::SetAutoBreakRows(bool flag)
1652 if (flag != autoBreakRows) {
1653 autoBreakRows = flag;
1661 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1663 if (how != drawFrame_) {
1666 UpdateLocal(bv, DRAW_FRAME, false);
1671 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1673 if (frame_color != col) {
1676 UpdateLocal(bv, DRAW_FRAME, false);
1681 int InsetText::cx(BufferView * bv) const
1683 LyXText * text = TEXT(bv);
1684 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1685 if (the_locking_inset) {
1686 LyXFont font = text->GetFont(bv->buffer(),
1688 text->cursor.pos());
1689 if (font.isVisibleRightToLeft())
1690 x -= the_locking_inset->width(bv, font);
1696 int InsetText::cy(BufferView * bv) const
1699 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1703 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1705 return TEXT(bv)->cursor.pos();
1709 LyXParagraph * InsetText::cpar(BufferView * bv) const
1711 return TEXT(bv)->cursor.par();
1714 bool InsetText::cboundary(BufferView * bv) const
1716 return TEXT(bv)->cursor.boundary();
1720 Row * InsetText::crow(BufferView * bv) const
1722 return TEXT(bv)->cursor.row();
1726 LyXText * InsetText::getLyXText(BufferView const * lbv,
1727 bool const recursive) const
1729 // Super UGLY! (Lgb)
1730 BufferView * bv = const_cast<BufferView *>(lbv);
1732 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1733 if (recursive && the_locking_inset)
1734 return the_locking_inset->getLyXText(bv);
1737 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1740 if (the_locking_inset) {
1741 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1743 return the_locking_inset->getLyXText(bv);
1749 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1751 if ((cache.find(bv) == cache.end()) || !cache[bv])
1756 /// then remove all LyXText in text-insets
1757 LyXParagraph * p = par;
1759 for (; p; p = p->next_) {
1760 p->deleteInsetsLyXText(bv);
1763 for (; p; p = p->next()) {
1764 p->deleteInsetsLyXText(bv);
1771 void InsetText::resizeLyXText(BufferView * bv) const
1774 if (!par->next_ && !par->size()) // resize not neccessary!
1776 if (!par->next() && !par->size()) // resize not neccessary!
1779 if ((cache.find(bv) == cache.end()) || !cache[bv])
1782 LyXParagraph * lpar = 0;
1783 LyXParagraph * selstartpar = 0;
1784 LyXParagraph * selendpar = 0;
1785 LyXParagraph::size_type pos = 0;
1786 LyXParagraph::size_type selstartpos = 0;
1787 LyXParagraph::size_type selendpos = 0;
1788 bool boundary = false;
1789 bool selstartboundary = false;
1790 bool selendboundary = false;
1794 // ProhibitInput(bv);
1797 lpar = TEXT(bv)->cursor.par();
1798 pos = TEXT(bv)->cursor.pos();
1799 boundary = TEXT(bv)->cursor.boundary();
1800 selstartpar = TEXT(bv)->sel_start_cursor.par();
1801 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1802 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1803 selendpar = TEXT(bv)->sel_end_cursor.par();
1804 selendpos = TEXT(bv)->sel_end_cursor.pos();
1805 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1806 selection = TEXT(bv)->selection;
1807 mark_set = TEXT(bv)->mark_set;
1809 deleteLyXText(bv, (the_locking_inset == 0));
1812 TEXT(bv)->selection = true;
1813 /* at this point just to avoid the Delete-Empty-Paragraph
1814 * Mechanism when setting the cursor */
1815 TEXT(bv)->mark_set = mark_set;
1817 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1818 true, selstartboundary);
1819 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1820 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1821 true, selendboundary);
1822 TEXT(bv)->SetSelection(bv);
1823 TEXT(bv)->SetCursor(bv, lpar, pos);
1825 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1826 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1827 TEXT(bv)->selection = false;
1831 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1832 // this will scroll the screen such that the cursor becomes visible
1833 bv->updateScrollbar();
1835 if (the_locking_inset) {
1836 /// then resize all LyXText in text-insets
1837 inset_x = cx(bv) - top_x + drawTextXOffset;
1838 inset_y = cy(bv) + drawTextYOffset;
1840 for (LyXParagraph * p = par; p; p = p->next_) {
1841 p->resizeInsetsLyXText(bv);
1844 for (LyXParagraph * p = par; p; p = p->next()) {
1845 p->resizeInsetsLyXText(bv);
1853 void InsetText::removeNewlines()
1856 for (LyXParagraph * p = par; p; p = p->next_) {
1857 for (int i = 0; i < p->Last(); ++i) {
1859 for (LyXParagraph * p = par; p; p = p->next()) {
1860 for (int i = 0; i < p->size(); ++i) {
1862 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1868 bool InsetText::nodraw() const
1870 if (the_locking_inset)
1871 return the_locking_inset->nodraw();
1872 return UpdatableInset::nodraw();
1875 int InsetText::scroll(bool recursive) const
1877 int sx = UpdatableInset::scroll(false);
1879 if (recursive && the_locking_inset)
1880 sx += the_locking_inset->scroll(recursive);
1885 bool InsetText::doClearArea() const
1887 return !locked || (need_update & (FULL|INIT));