1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettext.h"
23 #include "paragraph.h"
27 #include "commandtags.h"
30 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "lyx_gui_misc.h"
36 #include "lyxcursor.h"
37 #include "CutAndPaste.h"
40 #include "support/textutils.h"
41 #include "support/LAssert.h"
45 #include "trans_mgr.h"
46 #include "lyxscreen.h"
50 #include "ParagraphParameters.h"
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
62 InsetText::InsetText()
69 InsetText::InsetText(InsetText const & ins)
74 autoBreakRows = ins.autoBreakRows;
78 InsetText & InsetText::operator=(InsetText const & it)
81 autoBreakRows = it.autoBreakRows;
86 void InsetText::init(InsetText const * ins)
94 the_locking_inset = 0;
100 autoBreakRows = false;
104 SetParagraphData(ins->par);
105 autoBreakRows = ins->autoBreakRows;
106 drawFrame_ = ins->drawFrame_;
108 par->setInsetOwner(this);
109 frame_color = LColor::insetframe;
112 last_drawn_width = -1;
113 frame_is_visible = false;
117 InsetText::~InsetText()
119 // delete all instances of LyXText before deleting the paragraps used
121 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
122 delete (*cit).second;
127 Paragraph * tmp = par->next();
134 void InsetText::clear()
136 // delete all instances of LyXText before deleting the paragraps used
138 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
139 delete (*cit).second;
144 Paragraph * tmp = par->next();
152 Inset * InsetText::Clone(Buffer const &) const
154 InsetText * t = new InsetText(*this);
159 void InsetText::Write(Buffer const * buf, ostream & os) const
162 WriteParagraphData(buf, os);
166 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
168 par->writeFile(buf, os, buf->params, 0);
172 void InsetText::Read(Buffer const * buf, LyXLex & lex)
176 Paragraph * return_par = 0;
177 Paragraph::depth_type depth = 0;
178 LyXFont font(LyXFont::ALL_INHERIT);
184 token = lex.GetString();
187 if (token == "\\end_inset")
189 if (const_cast<Buffer*>(buf)->
190 parseSingleLyXformat2Token(lex, par, return_par,
191 token, pos, depth, font)) {
192 // the_end read this should NEVER happen
193 lex.printError("\\the_end read in inset! Error in document!");
201 return_par->setInsetOwner(this);
202 return_par = return_par->next();
205 if (token != "\\end_inset") {
206 lex.printError("Missing \\end_inset at this point. "
213 int InsetText::ascent(BufferView * bv, LyXFont const &) const
216 Row * row = TEXT(bv)->getRowNearY(y_temp);
217 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222 int InsetText::descent(BufferView * bv, LyXFont const &) const
225 Row * row = TEXT(bv)->getRowNearY(y_temp);
226 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
227 TEXT_TO_INSET_OFFSET;
232 int InsetText::width(BufferView * bv, LyXFont const &) const
234 insetWidth = max(textWidth(bv),
235 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
240 int InsetText::textWidth(BufferView * bv) const
242 int const w = getMaxWidth(bv, this);
247 void InsetText::draw(BufferView * bv, LyXFont const & f,
248 int baseline, float & x, bool cleared) const
253 Painter & pain = bv->painter();
255 // no draw is necessary !!!
256 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
258 top_baseline = baseline;
260 if (!cleared && (need_update & CLEAR_FRAME))
261 clearFrame(pain, cleared);
263 frame_is_visible = false;
270 UpdatableInset::draw(bv, f, baseline, x, cleared);
273 x += static_cast<float>(scroll());
276 // update insetWidth and insetHeight with dummy calls
278 (void)descent(bv, f);
282 // if top_x differs we have a rule down and we don't have to clear anything
283 if (!cleared && (top_x == int(x)) &&
284 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
285 (last_drawn_width!=insetWidth)))
287 clearInset(pain, baseline, cleared);
290 frame_is_visible = false;
292 if (!cleared && (need_update == NONE))
295 if (top_x != int(x)) {
296 if ((getMaxWidth(bv, this) > 0) &&
297 (TEXT(bv)->width != old_max_width))
301 old_max_width = TEXT(bv)->width;
302 bv->text->status = LyXText::CHANGED_IN_DRAW;
306 clearInset(pain, baseline, cleared);
312 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
314 if (cleared || (last_drawn_width != insetWidth)) {
316 last_drawn_width = insetWidth;
319 top_baseline = baseline;
320 top_y = baseline - ascent(bv, f);
321 last_width = width(bv, f);
322 last_height = ascent(bv, f) + descent(bv, f);
324 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
325 inset_x = cx(bv) - top_x + drawTextXOffset;
326 inset_y = cy(bv) + drawTextYOffset;
328 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection.set()) {
329 drawFrame(pain, cleared);
330 x += last_width; // was width(bv, f);
334 x += TEXT_TO_INSET_OFFSET;
337 Row * row = TEXT(bv)->getRowNearY(y);
338 int y_offset = baseline - row->ascent_of_text();
339 int ph = pain.paperHeight();
342 while ((row != 0) && ((y+row->height()) <= 0)) {
344 first += row->height();
349 TEXT(bv)->first = first;
353 while ((row != 0) && (yf < ph)) {
354 TEXT(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
360 } else if (!locked) {
361 if (need_update & CURSOR) {
362 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
363 TEXT(bv)->clearSelection(bv);
364 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
366 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
369 if (need_update & SELECTION)
370 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
371 else if (need_update & CURSOR) {
372 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
373 TEXT(bv)->clearSelection(bv);
374 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
376 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
380 TEXT(bv)->refresh_y = 0;
381 TEXT(bv)->status = LyXText::UNCHANGED;
382 if ((need_update != CURSOR_PAR) &&
383 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
384 drawFrame(pain, cleared);
385 else if (need_update & CLEAR_FRAME)
386 clearFrame(pain, cleared);
387 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
388 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
390 } else if (need_update != INIT)
395 void InsetText::drawFrame(Painter & pain, bool cleared) const
397 if (!frame_is_visible || cleared) {
398 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
399 insetWidth - 1, insetAscent + insetDescent - 1,
401 frame_is_visible = true;
406 void InsetText::clearFrame(Painter & pain, bool cleared) const
408 if (frame_is_visible) {
410 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
411 insetWidth - 1, insetAscent + insetDescent - 1,
414 frame_is_visible = false;
419 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
422 int ww = TEXT(bv)->width;
423 TEXT(bv)->BreakParagraph(bv);
424 if (ww != TEXT(bv)->width)
431 owner()->update(bv, font, true);
434 if (the_locking_inset) {
435 inset_x = cx(bv) - top_x + drawTextXOffset;
436 inset_y = cy(bv) + drawTextYOffset;
437 the_locking_inset->update(bv, font, reinit);
440 if (need_update == INIT) {
445 int oldw = insetWidth;
446 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
447 if (oldw != insetWidth) {
450 // update(bv, font, reinit);
453 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
456 TEXT(bv)->updateInset(bv, the_locking_inset);
459 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
463 Row * row = TEXT(bv)->GetRowNearY(y_temp);
464 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
465 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
466 TEXT_TO_INSET_OFFSET;
471 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
474 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
476 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
477 need_update |= CURSOR_PAR;
479 // this to not draw a selection when we redraw all of it!
480 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
481 TEXT(bv)->clearSelection(bv);
485 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
487 TEXT(bv)->fullRebreak(bv);
488 SetUpdateStatus(bv, what);
489 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
490 TEXT(bv)->selection.set())
491 bv->updateInset(this, mark_dirty);
492 bv->owner()->showState();
493 if (old_par != cpar(bv)) {
494 bv->owner()->setLayout(cpar(bv)->getLayout());
500 string const InsetText::EditMessage() const
502 return _("Opened Text Inset");
506 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
508 // par->SetInsetOwner(this);
509 UpdatableInset::Edit(bv, x, y, button);
511 if (!bv->lockInset(this)) {
512 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
516 the_locking_inset = 0;
517 inset_pos = inset_x = inset_y = 0;
518 inset_boundary = false;
521 int tmp_y = (y < 0)?0:y;
522 if (!checkAndActivateInset(bv, x, tmp_y, button))
523 TEXT(bv)->setCursorFromCoordinates(bv, x - drawTextXOffset,
525 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
526 bv->text->finishUndo();
528 UpdateLocal(bv, CURSOR, false);
530 // If the inset is empty set the language of the current font to the
531 // language to the surronding text (if different).
532 if (par->size() == 0 && !par->next() &&
533 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
535 LyXFont font(LyXFont::ALL_IGNORE);
536 font.setLanguage(bv->getParentLanguage(this));
537 SetFont(bv, font, false);
542 void InsetText::InsetUnlock(BufferView * bv)
544 if (the_locking_inset) {
545 the_locking_inset->InsetUnlock(bv);
546 the_locking_inset = 0;
549 no_selection = false;
551 int code = CURSOR|CLEAR_FRAME;
552 if (TEXT(bv)->selection.set()) {
553 TEXT(bv)->clearSelection(bv);
556 bv->owner()->setLayout(owner()->getLyXText(bv)
557 ->cursor.par()->getLayout());
559 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
560 UpdateLocal(bv, code, false);
564 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
566 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
570 if (inset == cpar(bv)->getInset(cpos(bv))) {
571 lyxerr[Debug::INSETS] << "OK" << endl;
572 the_locking_inset = inset;
573 inset_x = cx(bv) - top_x + drawTextXOffset;
574 inset_y = cy(bv) + drawTextYOffset;
575 inset_pos = cpos(bv);
576 inset_par = cpar(bv);
577 inset_boundary = cboundary(bv);
579 TEXT(bv)->clearSelection(bv);
580 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
581 TEXT(bv)->UpdateInset(bv, the_locking_inset);
583 UpdateLocal(bv, CURSOR, false);
586 } else if (the_locking_inset && (the_locking_inset == inset)) {
587 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
588 lyxerr[Debug::INSETS] << "OK" << endl;
589 inset_x = cx(bv) - top_x + drawTextXOffset;
590 inset_y = cy(bv) + drawTextYOffset;
592 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
594 } else if (the_locking_inset) {
595 lyxerr[Debug::INSETS] << "MAYBE" << endl;
596 return the_locking_inset->LockInsetInInset(bv, inset);
598 lyxerr[Debug::INSETS] << "NOT OK" << endl;
603 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
606 if (!the_locking_inset)
608 if (the_locking_inset == inset) {
609 the_locking_inset->InsetUnlock(bv);
610 TEXT(bv)->updateInset(bv, inset);
611 the_locking_inset = 0;
613 moveRight(bv, false);
614 old_par = 0; // force layout setting
618 UpdateLocal(bv, CURSOR, false);
621 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
625 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
627 if (!the_locking_inset)
629 if (the_locking_inset != inset) {
630 TEXT(bv)->updateInset(bv, the_locking_inset);
631 SetUpdateStatus(bv, CURSOR_PAR);
632 return the_locking_inset->UpdateInsetInInset(bv, inset);
634 // UpdateLocal(bv, FULL, false);
635 if (TEXT(bv)->updateInset(bv, inset))
636 UpdateLocal(bv, CURSOR_PAR, false);
637 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
638 inset_x = cx(bv) - top_x + drawTextXOffset;
639 inset_y = cy(bv) + drawTextYOffset;
645 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
649 int tmp_x = x - drawTextXOffset;
650 int tmp_y = y + insetAscent - TEXT(bv)->first;
651 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
654 if (the_locking_inset) {
655 if (the_locking_inset == inset) {
656 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
657 no_selection = false;
660 // otherwise unlock the_locking_inset and lock the new inset
661 the_locking_inset->InsetUnlock(bv);
662 inset_x = cx(bv) - top_x + drawTextXOffset;
663 inset_y = cy(bv) + drawTextYOffset;
664 the_locking_inset = static_cast<UpdatableInset*>(inset);
665 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
666 inset->Edit(bv, x - inset_x, y - inset_y, button);
667 if (the_locking_inset)
668 UpdateLocal(bv, CURSOR, false);
669 no_selection = false;
672 // otherwise only unlock the_locking_inset
673 the_locking_inset->InsetUnlock(bv);
674 the_locking_inset = 0;
676 if (bv->theLockingInset()) {
677 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
678 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
679 inset_x = cx(bv) - top_x + drawTextXOffset;
680 inset_y = cy(bv) + drawTextYOffset;
681 inset_pos = cpos(bv);
682 inset_par = cpar(bv);
683 inset_boundary = cboundary(bv);
684 the_locking_inset = uinset;
685 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
687 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
688 if (the_locking_inset)
689 UpdateLocal(bv, CURSOR, false);
690 no_selection = false;
694 if (!inset) { // && (button == 2)) {
695 bool paste_internally = false;
696 if ((button == 2) && TEXT(bv)->selection.set()) {
697 LocalDispatch(bv, LFUN_COPY, "");
698 paste_internally = true;
700 TEXT(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
702 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
703 UpdateLocal(bv, CURSOR, false);
704 bv->owner()->setLayout(cpar(bv)->getLayout());
706 // Insert primary selection with middle mouse
707 // if there is a local selection in the current buffer,
710 if (paste_internally)
711 LocalDispatch(bv, LFUN_PASTE, "");
713 LocalDispatch(bv, LFUN_PASTESELECTION,
718 no_selection = false;
722 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
724 UpdatableInset * inset = 0;
726 if (the_locking_inset) {
727 the_locking_inset->InsetButtonRelease(bv,
728 x - inset_x, y - inset_y,
731 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
732 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
733 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
734 inset->InsetButtonRelease(bv,
736 y - inset_y, button);
738 inset_x = cx(bv) - top_x + drawTextXOffset;
739 inset_y = cy(bv) + drawTextYOffset;
740 inset->InsetButtonRelease(bv,
742 y - inset_y, button);
744 x - inset_x, y - inset_y, button);
746 UpdateLocal(bv, CURSOR_PAR, false);
749 no_selection = false;
753 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
757 if (the_locking_inset) {
758 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
763 TEXT(bv)->setCursorFromCoordinates(bv, x - drawTextXOffset,
765 TEXT(bv)->setSelection(bv);
766 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
767 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
768 UpdateLocal(bv, SELECTION, false);
773 void InsetText::InsetKeyPress(XKeyEvent * xke)
775 if (the_locking_inset) {
776 the_locking_inset->InsetKeyPress(xke);
782 UpdatableInset::RESULT
783 InsetText::LocalDispatch(BufferView * bv,
784 kb_action action, string const & arg)
786 no_selection = false;
787 UpdatableInset::RESULT
788 result= UpdatableInset::LocalDispatch(bv, action, arg);
789 if (result != UNDISPATCHED) {
794 if ((action < 0) && arg.empty())
797 if (the_locking_inset) {
798 result = the_locking_inset->LocalDispatch(bv, action, arg);
799 if (result == DISPATCHED_NOUPDATE)
801 else if (result == DISPATCHED) {
802 UpdateLocal(bv, CURSOR_PAR, false);
804 } else if (result == FINISHED) {
805 bool dispatched = false;
807 case LFUN_UNKNOWN_ACTION:
808 case LFUN_BREAKPARAGRAPH:
810 moveRightIntern(bv, false, false);
813 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
814 moveRightIntern(bv, false, false);
818 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
819 moveRightIntern(bv, false, false);
825 the_locking_inset = 0;
833 case LFUN_SELFINSERT:
834 if (bv->buffer()->isReadonly()) {
835 // setErrorMessage(N_("Document is read only"));
839 /* Automatically delete the currently selected
840 * text and replace it with what is being
841 * typed in now. Depends on lyxrc settings
842 * "auto_region_delete", which defaults to
845 bv->text->setUndo(bv->buffer(), Undo::INSERT,
846 bv->text->cursor.par()->previous(),
847 bv->text->cursor.par()->next());
849 if (lyxrc.auto_region_delete) {
850 if (TEXT(bv)->selection.set()) {
851 TEXT(bv)->cutSelection(bv, false);
854 TEXT(bv)->clearSelection(bv);
855 for (string::size_type i = 0; i < arg.length(); ++i) {
857 if (!math_insert_greek(bv, arg[i])) {
858 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
859 } else if (!the_locking_inset) {
860 (void)moveRight(bv, false);
863 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
867 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
868 UpdateLocal(bv, CURSOR_PAR, true);
869 result=DISPATCHED_NOUPDATE;
871 // --- Cursor Movements -----------------------------------
873 bv->text->finishUndo();
874 moveRight(bv, false, true);
875 TEXT(bv)->setSelection(bv);
876 UpdateLocal(bv, SELECTION, false);
879 result = moveRight(bv);
880 bv->text->finishUndo();
881 UpdateLocal(bv, CURSOR, false);
884 bv->text->finishUndo();
885 moveLeft(bv, false, true);
886 TEXT(bv)->setSelection(bv);
887 UpdateLocal(bv, SELECTION, false);
890 bv->text->finishUndo();
891 result = moveLeft(bv);
892 UpdateLocal(bv, CURSOR, false);
895 bv->text->finishUndo();
897 TEXT(bv)->setSelection(bv);
898 UpdateLocal(bv, SELECTION, false);
901 bv->text->finishUndo();
902 result = moveDown(bv);
903 UpdateLocal(bv, CURSOR, false);
906 bv->text->finishUndo();
908 TEXT(bv)->setSelection(bv);
909 UpdateLocal(bv, SELECTION, false);
912 bv->text->finishUndo();
914 UpdateLocal(bv, CURSOR, false);
917 bv->text->finishUndo();
918 TEXT(bv)->cursorHome(bv);
919 UpdateLocal(bv, CURSOR, false);
922 TEXT(bv)->cursorEnd(bv);
923 UpdateLocal(bv, CURSOR, false);
926 bv->text->setUndo(bv->buffer(), Undo::DELETE,
927 bv->text->cursor.par()->previous(),
928 bv->text->cursor.par()->next());
929 if (TEXT(bv)->selection.set())
930 TEXT(bv)->cutSelection(bv);
932 TEXT(bv)->backspace(bv);
933 UpdateLocal(bv, CURSOR_PAR, true);
936 bv->text->setUndo(bv->buffer(), Undo::DELETE,
937 bv->text->cursor.par()->previous(),
938 bv->text->cursor.par()->next());
939 if (TEXT(bv)->selection.set())
940 TEXT(bv)->cutSelection(bv);
942 TEXT(bv)->Delete(bv);
943 UpdateLocal(bv, CURSOR_PAR, true);
946 bv->text->setUndo(bv->buffer(), Undo::DELETE,
947 bv->text->cursor.par()->previous(),
948 bv->text->cursor.par()->next());
949 TEXT(bv)->cutSelection(bv);
950 UpdateLocal(bv, CURSOR_PAR, true);
953 bv->text->finishUndo();
954 TEXT(bv)->copySelection(bv);
955 UpdateLocal(bv, CURSOR_PAR, false);
957 case LFUN_PASTESELECTION:
959 string const clip(bv->getClipboard());
963 if (arg == "paragraph") {
964 TEXT(bv)->insertStringAsParagraphs(bv, clip);
966 TEXT(bv)->insertStringAsLines(bv, clip);
968 UpdateLocal(bv, CURSOR_PAR, true);
972 if (!autoBreakRows) {
975 if (cap.nrOfParagraphs() > 1) {
976 WriteAlert(_("Impossible operation"),
977 _("Cannot include more than one paragraph!"),
982 bv->text->setUndo(bv->buffer(), Undo::INSERT,
983 bv->text->cursor.par()->previous(),
984 bv->text->cursor.par()->next());
985 TEXT(bv)->pasteSelection(bv);
986 UpdateLocal(bv, CURSOR_PAR, true);
988 case LFUN_BREAKPARAGRAPH:
991 TEXT(bv)->breakParagraph(bv, 0);
992 UpdateLocal(bv, FULL, true);
994 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
997 TEXT(bv)->breakParagraph(bv, 1);
998 UpdateLocal(bv, FULL, true);
1000 case LFUN_BREAKLINE:
1003 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1004 bv->text->cursor.par()->previous(),
1005 bv->text->cursor.par()->next());
1006 TEXT(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1007 UpdateLocal(bv, CURSOR_PAR, true);
1010 // do not set layouts on non breakable textinsets
1011 if (autoBreakRows) {
1012 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1014 // Derive layout number from given argument (string)
1015 // and current buffer's textclass (number). */
1016 LyXTextClassList::ClassList::size_type tclass =
1017 bv->buffer()->params.textclass;
1018 std::pair <bool, LyXTextClass::size_type> layout =
1019 textclasslist.NumberOfLayout(tclass, arg);
1021 // If the entry is obsolete, use the new one instead.
1023 string obs = textclasslist.Style(tclass,layout.second).
1026 layout = textclasslist.NumberOfLayout(tclass, obs);
1029 // see if we found the layout number:
1030 if (!layout.first) {
1031 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1032 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1036 if (cur_layout != layout.second) {
1037 cur_layout = layout.second;
1038 TEXT(bv)->setLayout(bv, layout.second);
1039 bv->owner()->setLayout(cpar(bv)->getLayout());
1040 UpdateLocal(bv, CURSOR_PAR, true);
1043 // reset the layout box
1044 bv->owner()->setLayout(cpar(bv)->getLayout());
1047 case LFUN_PARAGRAPH_SPACING:
1048 // This one is absolutely not working. When fiddling with this
1049 // it also seems to me that the paragraphs inside the insettext
1050 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1052 Paragraph * par = TEXT(bv)->cursor.par();
1053 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1054 float cur_value = 1.0;
1055 if (cur_spacing == Spacing::Other) {
1056 cur_value = par->params().spacing().getValue();
1059 std::istringstream istr(arg.c_str());
1062 Spacing::Space new_spacing = cur_spacing;
1063 float new_value = cur_value;
1065 lyxerr << "Missing argument to `paragraph-spacing'"
1067 } else if (tmp == "single") {
1068 new_spacing = Spacing::Single;
1069 } else if (tmp == "onehalf") {
1070 new_spacing = Spacing::Onehalf;
1071 } else if (tmp == "double") {
1072 new_spacing = Spacing::Double;
1073 } else if (tmp == "other") {
1074 new_spacing = Spacing::Other;
1077 lyxerr << "new_value = " << tmpval << endl;
1080 } else if (tmp == "default") {
1081 new_spacing = Spacing::Default;
1083 lyxerr << _("Unknown spacing argument: ")
1086 if (cur_spacing != new_spacing || cur_value != new_value) {
1087 par->params().spacing(Spacing(new_spacing, new_value));
1088 //TEXT(bv)->RedoParagraph(owner->view());
1089 UpdateLocal(bv, CURSOR_PAR, true);
1090 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1096 if (!bv->Dispatch(action, arg))
1097 result = UNDISPATCHED;
1101 /// If the action has deleted all text in the inset, we need to change the
1102 // language to the language to the surronding text.
1103 if (par->size() == 0 && !par->next()) {
1104 LyXFont font(LyXFont::ALL_IGNORE);
1105 font.setLanguage(bv->getParentLanguage(this));
1106 SetFont(bv, font, false);
1109 if (result != FINISHED) {
1110 ShowInsetCursor(bv);
1112 bv->unlockInset(this);
1117 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1120 buf->latexParagraphs(os, par, 0, texrow);
1121 return texrow.rows();
1125 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1127 Paragraph * p = par;
1128 unsigned int lines = 0;
1131 string const tmp = buf->asciiParagraph(p, linelen);
1132 lines += countChar(tmp, '\n');
1140 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1142 Paragraph * p = par;
1143 unsigned int lines = 0;
1148 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1156 void InsetText::Validate(LaTeXFeatures & features) const
1158 Paragraph * p = par;
1160 p->validate(features);
1166 int InsetText::BeginningOfMainBody(Buffer const * buf, Paragraph * p) const
1168 if (textclasslist.Style(buf->params.textclass,
1169 p->getLayout()).labeltype != LABEL_MANUAL)
1172 return p->beginningOfMainBody();
1176 void InsetText::GetCursorPos(BufferView * bv,
1177 int & x, int & y) const
1184 unsigned int InsetText::InsetInInsetY()
1186 if (!the_locking_inset)
1189 return (inset_y + the_locking_inset->InsetInInsetY());
1193 void InsetText::ToggleInsetCursor(BufferView * bv)
1195 if (the_locking_inset) {
1196 the_locking_inset->ToggleInsetCursor(bv);
1200 LyXFont const font(TEXT(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1202 int const asc = lyxfont::maxAscent(font);
1203 int const desc = lyxfont::maxDescent(font);
1205 if (isCursorVisible())
1206 bv->hideLockedInsetCursor();
1208 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1209 toggleCursorVisible();
1213 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1215 if (the_locking_inset) {
1216 the_locking_inset->ShowInsetCursor(bv);
1219 if (!isCursorVisible()) {
1220 LyXFont const font =
1221 TEXT(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1223 int const asc = lyxfont::maxAscent(font);
1224 int const desc = lyxfont::maxDescent(font);
1226 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1228 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1229 setCursorVisible(true);
1234 void InsetText::HideInsetCursor(BufferView * bv)
1236 if (isCursorVisible()) {
1237 bv->hideLockedInsetCursor();
1238 setCursorVisible(false);
1240 if (the_locking_inset)
1241 the_locking_inset->HideInsetCursor(bv);
1245 UpdatableInset::RESULT
1246 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1248 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1249 return moveLeftIntern(bv, false, activate_inset, selecting);
1251 return moveRightIntern(bv, false, activate_inset, selecting);
1255 UpdatableInset::RESULT
1256 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1258 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1259 return moveRightIntern(bv, true, activate_inset, selecting);
1261 return moveLeftIntern(bv, true, activate_inset, selecting);
1265 UpdatableInset::RESULT
1266 InsetText::moveRightIntern(BufferView * bv, bool behind,
1267 bool activate_inset, bool selecting)
1269 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1271 if (activate_inset && checkAndActivateInset(bv, behind))
1273 TEXT(bv)->cursorRight(bv);
1275 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1276 return DISPATCHED_NOUPDATE;
1280 UpdatableInset::RESULT
1281 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1282 bool activate_inset, bool selecting)
1284 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1286 TEXT(bv)->cursorLeft(bv);
1288 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1289 if (activate_inset && checkAndActivateInset(bv, behind))
1291 return DISPATCHED_NOUPDATE;
1295 UpdatableInset::RESULT
1296 InsetText::moveUp(BufferView * bv)
1298 if (!crow(bv)->previous())
1300 TEXT(bv)->cursorUp(bv);
1301 return DISPATCHED_NOUPDATE;
1305 UpdatableInset::RESULT
1306 InsetText::moveDown(BufferView * bv)
1308 if (!crow(bv)->next())
1310 TEXT(bv)->cursorDown(bv);
1311 return DISPATCHED_NOUPDATE;
1315 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1317 if (the_locking_inset) {
1318 if (the_locking_inset->InsertInsetAllowed(inset))
1319 return the_locking_inset->InsertInset(bv, inset);
1322 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1323 bv->text->cursor.par()->previous(),
1324 bv->text->cursor.par()->next());
1325 inset->setOwner(this);
1326 HideInsetCursor(bv);
1327 TEXT(bv)->insertInset(bv, inset);
1329 if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1330 (cpar(bv)->GetInset(cpos(bv)) != inset))
1331 TEXT(bv)->CursorLeft(bv);
1333 bv->fitCursor(TEXT(bv));
1334 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1335 ShowInsetCursor(bv);
1340 UpdatableInset * InsetText::GetLockingInset()
1342 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1346 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1350 if (the_locking_inset)
1351 return the_locking_inset->GetFirstLockingInsetOfType(c);
1356 bool InsetText::ShowInsetDialog(BufferView * bv) const
1358 if (the_locking_inset)
1359 return the_locking_inset->ShowInsetDialog(bv);
1364 std::vector<string> const InsetText::getLabelList() const
1366 std::vector<string> label_list;
1368 Paragraph * tpar = par;
1370 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1371 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1372 for (; beg != end; ++beg) {
1373 std::vector<string> const l = (*beg)->getLabelList();
1374 label_list.insert(label_list.end(), l.begin(), l.end());
1376 tpar = tpar->next();
1382 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1385 if (the_locking_inset) {
1386 the_locking_inset->SetFont(bv, font, toggleall, selectall);
1389 if (TEXT(bv)->selection.set()) {
1390 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1391 bv->text->cursor.par()->previous(),
1392 bv->text->cursor.par()->next());
1396 TEXT(bv)->setFont(bv, font, toggleall);
1398 TEXT(bv)->clearSelection(bv);
1399 bv->fitCursor(TEXT(bv));
1400 if (selectall || TEXT(bv)->selection.set())
1401 UpdateLocal(bv, FULL, true);
1403 UpdateLocal(bv, CURSOR_PAR, true);
1407 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1409 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1413 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1414 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1416 LyXFont const font =
1417 TEXT(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1419 x = inset->width(bv, font);
1420 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1423 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1425 //inset_x = cx(bv) - top_x + drawTextXOffset;
1426 //inset_y = cy(bv) + drawTextYOffset;
1427 inset->Edit(bv, x, y, 0);
1428 if (!the_locking_inset)
1430 UpdateLocal(bv, CURSOR, false);
1437 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1440 x -= drawTextXOffset;
1442 int dummyy = y + insetAscent;
1443 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1450 inset_x = cx(bv) - top_x + drawTextXOffset;
1451 inset_y = cy(bv) + drawTextYOffset;
1452 inset->Edit(bv, x - inset_x, y - inset_y, button);
1453 if (!the_locking_inset)
1455 UpdateLocal(bv, CURSOR, false);
1462 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1464 int w = UpdatableInset::getMaxWidth(bv, inset);
1469 w = w - top_x + owner()->x();
1472 w -= (2 * TEXT_TO_INSET_OFFSET);
1474 // return w - (2*TEXT_TO_INSET_OFFSET);
1478 void InsetText::SetParagraphData(Paragraph * p)
1480 // delete all instances of LyXText before deleting the paragraps used
1482 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1483 delete (*cit).second;
1488 Paragraph * tmp = par->next();
1493 par = new Paragraph(*p);
1494 par->setInsetOwner(this);
1495 Paragraph * np = par;
1498 np->next(new Paragraph(*p));
1499 np->next()->previous(np);
1501 np->setInsetOwner(this);
1508 void InsetText::SetText(string const & data)
1511 LyXFont font(LyXFont::ALL_SANE);
1512 for (unsigned int i=0; i < data.length(); ++i)
1513 par->insertChar(i, data[i], font);
1517 void InsetText::SetAutoBreakRows(bool flag)
1519 if (flag != autoBreakRows) {
1520 autoBreakRows = flag;
1528 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1530 if (how != drawFrame_) {
1533 UpdateLocal(bv, DRAW_FRAME, false);
1538 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1540 if (frame_color != col) {
1543 UpdateLocal(bv, DRAW_FRAME, false);
1548 int InsetText::cx(BufferView * bv) const
1550 LyXText * text = TEXT(bv);
1551 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1552 if (the_locking_inset) {
1553 LyXFont font = text->getFont(bv->buffer(),
1555 text->cursor.pos());
1556 if (font.isVisibleRightToLeft())
1557 x -= the_locking_inset->width(bv, font);
1563 int InsetText::cy(BufferView * bv) const
1566 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1570 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1572 return TEXT(bv)->cursor.pos();
1576 Paragraph * InsetText::cpar(BufferView * bv) const
1578 return TEXT(bv)->cursor.par();
1582 bool InsetText::cboundary(BufferView * bv) const
1584 return TEXT(bv)->cursor.boundary();
1588 Row * InsetText::crow(BufferView * bv) const
1590 return TEXT(bv)->cursor.row();
1594 LyXText * InsetText::getLyXText(BufferView const * lbv,
1595 bool const recursive) const
1597 // Super UGLY! (Lgb)
1598 BufferView * bv = const_cast<BufferView *>(lbv);
1600 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1601 if (recursive && the_locking_inset)
1602 return the_locking_inset->getLyXText(bv);
1605 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1608 if (the_locking_inset) {
1609 lt->setCursor(bv, inset_par, inset_pos, true, inset_boundary);
1611 return the_locking_inset->getLyXText(bv);
1617 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1619 if ((cache.find(bv) == cache.end()) || !cache[bv])
1624 /// then remove all LyXText in text-insets
1625 Paragraph * p = par;
1626 for (; p; p = p->next()) {
1627 p->deleteInsetsLyXText(bv);
1633 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1635 if (!par->next() && !par->size()) // no data, resize not neccessary!
1637 // one endless line, resize normally not necessary
1638 if (!force && getMaxWidth(bv, this) < 0)
1640 if ((cache.find(bv) == cache.end()) || !cache[bv])
1643 Paragraph * lpar = 0;
1644 Paragraph * selstartpar = 0;
1645 Paragraph * selendpar = 0;
1646 Paragraph::size_type pos = 0;
1647 Paragraph::size_type selstartpos = 0;
1648 Paragraph::size_type selendpos = 0;
1649 bool boundary = false;
1650 bool selstartboundary = false;
1651 bool selendboundary = false;
1652 bool selection = false;
1653 bool mark_set = false;
1655 // ProhibitInput(bv);
1658 lpar = TEXT(bv)->cursor.par();
1659 pos = TEXT(bv)->cursor.pos();
1660 boundary = TEXT(bv)->cursor.boundary();
1661 selstartpar = TEXT(bv)->selection.start.par();
1662 selstartpos = TEXT(bv)->selection.start.pos();
1663 selstartboundary = TEXT(bv)->selection.start.boundary();
1664 selendpar = TEXT(bv)->selection.end.par();
1665 selendpos = TEXT(bv)->selection.end.pos();
1666 selendboundary = TEXT(bv)->selection.end.boundary();
1667 selection = TEXT(bv)->selection.set();
1668 mark_set = TEXT(bv)->selection.mark();
1670 deleteLyXText(bv, (the_locking_inset == 0) || force);
1673 TEXT(bv)->selection.set(true);
1674 /* at this point just to avoid the Delete-Empty-Paragraph
1675 * Mechanism when setting the cursor */
1676 TEXT(bv)->selection.mark(mark_set);
1678 TEXT(bv)->setCursor(bv, selstartpar, selstartpos,
1679 true, selstartboundary);
1680 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1681 TEXT(bv)->setCursor(bv, selendpar, selendpos,
1682 true, selendboundary);
1683 TEXT(bv)->setSelection(bv);
1684 TEXT(bv)->setCursor(bv, lpar, pos);
1686 TEXT(bv)->setCursor(bv, lpar, pos, true, boundary);
1687 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1688 TEXT(bv)->selection.set(false);
1692 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1693 // this will scroll the screen such that the cursor becomes visible
1694 bv->updateScrollbar();
1696 if (the_locking_inset) {
1697 /// then resize all LyXText in text-insets
1698 inset_x = cx(bv) - top_x + drawTextXOffset;
1699 inset_y = cy(bv) + drawTextYOffset;
1700 for (Paragraph * p = par; p; p = p->next()) {
1701 p->resizeInsetsLyXText(bv);
1708 void InsetText::removeNewlines()
1710 for (Paragraph * p = par; p; p = p->next()) {
1711 for (int i = 0; i < p->size(); ++i) {
1712 if (p->getChar(i) == Paragraph::META_NEWLINE)
1719 bool InsetText::nodraw() const
1721 if (the_locking_inset)
1722 return the_locking_inset->nodraw();
1723 return UpdatableInset::nodraw();
1727 int InsetText::scroll(bool recursive) const
1729 int sx = UpdatableInset::scroll(false);
1731 if (recursive && the_locking_inset)
1732 sx += the_locking_inset->scroll(recursive);
1738 bool InsetText::doClearArea() const
1740 return !locked || (need_update & (FULL|INIT));
1744 void InsetText::selectAll(BufferView * bv)
1746 TEXT(bv)->cursorTop(bv);
1747 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1748 TEXT(bv)->cursorBottom(bv);
1749 TEXT(bv)->setSelection(bv);
1753 void InsetText::clearSelection(BufferView * bv)
1755 TEXT(bv)->clearSelection(bv);
1759 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1762 int h = insetAscent + insetDescent;
1763 int ty = baseline - insetAscent;
1769 if ((ty + h) > pain.paperHeight())
1770 h = pain.paperHeight();
1771 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1772 w = pain.paperWidth();
1773 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);