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 "lyxparagraph.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"
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;
99 autoBreakRows = false;
103 SetParagraphData(ins->par);
104 autoBreakRows = ins->autoBreakRows;
105 drawFrame_ = ins->drawFrame_;
107 par->SetInsetOwner(this);
108 frame_color = LColor::insetframe;
111 last_drawn_width = -1;
112 frame_is_visible = false;
116 InsetText::~InsetText()
118 // delete all instances of LyXText before deleting the paragraps used
120 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
121 delete (*cit).second;
126 LyXParagraph * tmp = par->next();
133 void InsetText::clear()
135 // delete all instances of LyXText before deleting the paragraps used
137 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
138 delete (*cit).second;
143 LyXParagraph * tmp = par->next();
147 par = new LyXParagraph;
151 Inset * InsetText::Clone(Buffer const &) const
153 InsetText * t = new InsetText(*this);
158 void InsetText::Write(Buffer const * buf, ostream & os) const
161 WriteParagraphData(buf, os);
165 void InsetText::WriteParagraphData(Buffer const * buf, ostream & os) const
167 par->writeFile(buf, os, buf->params, 0);
171 void InsetText::Read(Buffer const * buf, LyXLex & lex)
175 LyXParagraph * return_par = 0;
176 char depth = 0; // signed or unsigned?
177 LyXFont font(LyXFont::ALL_INHERIT);
183 token = lex.GetString();
186 if (token == "\\end_inset")
188 if (const_cast<Buffer*>(buf)->
189 parseSingleLyXformat2Token(lex, par, return_par,
190 token, pos, depth, font)) {
191 // the_end read this should NEVER happen
192 lex.printError("\\the_end read in inset! Error in document!");
200 return_par->SetInsetOwner(this);
201 return_par = return_par->next();
204 if (token != "\\end_inset") {
205 lex.printError("Missing \\end_inset at this point. "
212 int InsetText::ascent(BufferView * bv, LyXFont const &) const
215 Row * row = TEXT(bv)->GetRowNearY(y_temp);
216 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
221 int InsetText::descent(BufferView * bv, LyXFont const &) const
224 Row * row = TEXT(bv)->GetRowNearY(y_temp);
225 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
226 TEXT_TO_INSET_OFFSET;
231 int InsetText::width(BufferView * bv, LyXFont const &) const
233 insetWidth = max(textWidth(bv),
234 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
239 int InsetText::textWidth(BufferView * bv) const
241 int const w = getMaxWidth(bv, this);
246 void InsetText::draw(BufferView * bv, LyXFont const & f,
247 int baseline, float & x, bool cleared) const
252 Painter & pain = bv->painter();
254 // no draw is necessary !!!
255 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
257 top_baseline = baseline;
259 if (!cleared && (need_update & CLEAR_FRAME))
260 clearFrame(pain, cleared);
262 frame_is_visible = false;
269 UpdatableInset::draw(bv, f, baseline, x, cleared);
272 x += static_cast<float>(scroll());
275 // update insetWidth and insetHeight with dummy calls
277 (void)descent(bv, f);
281 // if top_x differs we have a rule down and we don't have to clear anything
282 if (!cleared && (top_x == int(x)) &&
283 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
284 (last_drawn_width!=insetWidth)))
286 clearInset(pain, baseline, cleared);
289 frame_is_visible = false;
291 if (!cleared && (need_update == NONE))
294 if (top_x != int(x)) {
295 if ((getMaxWidth(bv, this) > 0) &&
296 (TEXT(bv)->width != old_max_width))
300 old_max_width = TEXT(bv)->width;
301 bv->text->status = LyXText::CHANGED_IN_DRAW;
305 clearInset(pain, baseline, cleared);
311 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
313 if (cleared || (last_drawn_width != insetWidth)) {
315 last_drawn_width = insetWidth;
318 top_baseline = baseline;
319 top_y = baseline - ascent(bv, f);
320 last_width = width(bv, f);
321 last_height = ascent(bv, f) + descent(bv, f);
323 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
324 inset_x = cx(bv) - top_x + drawTextXOffset;
325 inset_y = cy(bv) + drawTextYOffset;
327 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection.set()) {
328 drawFrame(pain, cleared);
333 x += TEXT_TO_INSET_OFFSET;
336 Row * row = TEXT(bv)->GetRowNearY(y);
337 int y_offset = baseline - row->ascent_of_text();
338 int ph = pain.paperHeight();
341 while ((row != 0) && ((y+row->height()) <= 0)) {
343 first += row->height();
348 TEXT(bv)->first = first;
352 while ((row != 0) && (yf < ph)) {
353 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
359 } else if (!locked) {
360 if (need_update & CURSOR) {
361 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
362 TEXT(bv)->ClearSelection(bv);
363 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
365 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
368 if (need_update & SELECTION)
369 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
370 else if (need_update & CURSOR) {
371 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
372 TEXT(bv)->ClearSelection(bv);
373 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
375 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
379 TEXT(bv)->refresh_y = 0;
380 TEXT(bv)->status = LyXText::UNCHANGED;
381 if ((need_update != CURSOR_PAR) &&
382 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
383 drawFrame(pain, cleared);
384 else if (need_update & CLEAR_FRAME)
385 clearFrame(pain, cleared);
386 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
387 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
389 } else if (need_update != INIT)
394 void InsetText::drawFrame(Painter & pain, bool cleared) const
396 if (!frame_is_visible || cleared) {
397 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
398 insetWidth - 1, insetAscent + insetDescent - 1,
400 frame_is_visible = true;
405 void InsetText::clearFrame(Painter & pain, bool cleared) const
407 if (frame_is_visible) {
409 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
410 insetWidth - 1, insetAscent + insetDescent - 1,
413 frame_is_visible = false;
418 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
421 int ww = TEXT(bv)->width;
422 TEXT(bv)->BreakParagraph(bv);
423 if (ww != TEXT(bv)->width)
430 owner()->update(bv, font, true);
433 if (the_locking_inset) {
434 inset_x = cx(bv) - top_x + drawTextXOffset;
435 inset_y = cy(bv) + drawTextYOffset;
436 the_locking_inset->update(bv, font, reinit);
439 if (need_update == INIT) {
444 int oldw = insetWidth;
445 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
446 if (oldw != insetWidth) {
449 // update(bv, font, reinit);
452 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
455 TEXT(bv)->UpdateInset(bv, the_locking_inset);
458 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
462 Row * row = TEXT(bv)->GetRowNearY(y_temp);
463 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
464 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
465 TEXT_TO_INSET_OFFSET;
470 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
473 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
475 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
476 need_update |= CURSOR_PAR;
478 // this to not draw a selection when we redraw all of it!
479 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
480 TEXT(bv)->ClearSelection(bv);
484 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
486 TEXT(bv)->FullRebreak(bv);
487 SetUpdateStatus(bv, what);
488 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
489 TEXT(bv)->selection.set())
490 bv->updateInset(this, mark_dirty);
491 bv->owner()->showState();
492 if (old_par != cpar(bv)) {
493 bv->owner()->setLayout(cpar(bv)->GetLayout());
499 string const InsetText::EditMessage() const
501 return _("Opened Text Inset");
505 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
507 // par->SetInsetOwner(this);
508 UpdatableInset::Edit(bv, x, y, button);
510 if (!bv->lockInset(this)) {
511 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
515 the_locking_inset = 0;
516 inset_pos = inset_x = inset_y = 0;
517 inset_boundary = false;
520 int tmp_y = (y < 0)?0:y;
521 if (!checkAndActivateInset(bv, x, tmp_y, button))
522 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
524 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
525 bv->text->FinishUndo();
527 UpdateLocal(bv, CURSOR, false);
529 // If the inset is empty set the language of the current font to the
530 // language to the surronding text (if different).
531 if (par->size() == 0 && !par->next() &&
532 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
534 LyXFont font(LyXFont::ALL_IGNORE);
535 font.setLanguage(bv->getParentLanguage(this));
536 SetFont(bv, font, false);
541 void InsetText::InsetUnlock(BufferView * bv)
543 if (the_locking_inset) {
544 the_locking_inset->InsetUnlock(bv);
545 the_locking_inset = 0;
548 no_selection = false;
550 int code = CURSOR|CLEAR_FRAME;
551 if (TEXT(bv)->selection.set()) {
552 TEXT(bv)->ClearSelection(bv);
555 bv->owner()->setLayout(owner()->getLyXText(bv)
556 ->cursor.par()->GetLayout());
558 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
559 UpdateLocal(bv, code, false);
563 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
565 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
569 if (inset == cpar(bv)->GetInset(cpos(bv))) {
570 lyxerr[Debug::INSETS] << "OK" << endl;
571 the_locking_inset = inset;
572 inset_x = cx(bv) - top_x + drawTextXOffset;
573 inset_y = cy(bv) + drawTextYOffset;
574 inset_pos = cpos(bv);
575 inset_par = cpar(bv);
576 inset_boundary = cboundary(bv);
578 TEXT(bv)->ClearSelection(bv);
579 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
580 TEXT(bv)->UpdateInset(bv, the_locking_inset);
582 UpdateLocal(bv, CURSOR, false);
585 } else if (the_locking_inset && (the_locking_inset == inset)) {
586 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
587 lyxerr[Debug::INSETS] << "OK" << endl;
588 inset_x = cx(bv) - top_x + drawTextXOffset;
589 inset_y = cy(bv) + drawTextYOffset;
591 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
593 } else if (the_locking_inset) {
594 lyxerr[Debug::INSETS] << "MAYBE" << endl;
595 return the_locking_inset->LockInsetInInset(bv, inset);
597 lyxerr[Debug::INSETS] << "NOT OK" << endl;
602 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
605 if (!the_locking_inset)
607 if (the_locking_inset == inset) {
608 the_locking_inset->InsetUnlock(bv);
609 TEXT(bv)->UpdateInset(bv, inset);
610 the_locking_inset = 0;
612 moveRight(bv, false);
613 old_par = 0; // force layout setting
617 UpdateLocal(bv, CURSOR, false);
620 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
624 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
626 if (!the_locking_inset)
628 if (the_locking_inset != inset) {
629 TEXT(bv)->UpdateInset(bv, the_locking_inset);
630 SetUpdateStatus(bv, CURSOR_PAR);
631 return the_locking_inset->UpdateInsetInInset(bv, inset);
633 // UpdateLocal(bv, FULL, false);
634 if (TEXT(bv)->UpdateInset(bv, inset))
635 UpdateLocal(bv, CURSOR_PAR, false);
636 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
637 inset_x = cx(bv) - top_x + drawTextXOffset;
638 inset_y = cy(bv) + drawTextYOffset;
644 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
648 int tmp_x = x - drawTextXOffset;
649 int tmp_y = y + insetAscent - TEXT(bv)->first;
650 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
653 if (the_locking_inset) {
654 if (the_locking_inset == inset) {
655 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
656 no_selection = false;
659 // otherwise unlock the_locking_inset and lock the new inset
660 the_locking_inset->InsetUnlock(bv);
661 inset_x = cx(bv) - top_x + drawTextXOffset;
662 inset_y = cy(bv) + drawTextYOffset;
663 the_locking_inset = static_cast<UpdatableInset*>(inset);
664 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
665 inset->Edit(bv, x - inset_x, y - inset_y, button);
666 if (the_locking_inset)
667 UpdateLocal(bv, CURSOR, false);
668 no_selection = false;
671 // otherwise only unlock the_locking_inset
672 the_locking_inset->InsetUnlock(bv);
673 the_locking_inset = 0;
675 if (bv->theLockingInset()) {
676 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
677 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
678 inset_x = cx(bv) - top_x + drawTextXOffset;
679 inset_y = cy(bv) + drawTextYOffset;
680 inset_pos = cpos(bv);
681 inset_par = cpar(bv);
682 inset_boundary = cboundary(bv);
683 the_locking_inset = uinset;
684 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
686 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
687 if (the_locking_inset)
688 UpdateLocal(bv, CURSOR, false);
689 no_selection = false;
693 if (!inset) { // && (button == 2)) {
694 bool paste_internally = false;
695 if ((button == 2) && TEXT(bv)->selection.set()) {
696 LocalDispatch(bv, LFUN_COPY, "");
697 paste_internally = true;
699 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
701 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
702 UpdateLocal(bv, CURSOR, false);
703 bv->owner()->setLayout(cpar(bv)->GetLayout());
705 // Insert primary selection with middle mouse
706 // if there is a local selection in the current buffer,
709 if (paste_internally)
710 LocalDispatch(bv, LFUN_PASTE, "");
712 LocalDispatch(bv, LFUN_PASTESELECTION,
717 no_selection = false;
721 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
723 UpdatableInset * inset = 0;
725 if (the_locking_inset) {
726 the_locking_inset->InsetButtonRelease(bv,
727 x - inset_x, y - inset_y,
730 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
731 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
732 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
733 inset->InsetButtonRelease(bv,
735 y - inset_y, button);
737 inset_x = cx(bv) - top_x + drawTextXOffset;
738 inset_y = cy(bv) + drawTextYOffset;
739 inset->InsetButtonRelease(bv,
741 y - inset_y, button);
743 x - inset_x, y - inset_y, button);
745 UpdateLocal(bv, CURSOR_PAR, false);
748 no_selection = false;
752 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
756 if (the_locking_inset) {
757 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
762 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
764 TEXT(bv)->SetSelection(bv);
765 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
766 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
767 UpdateLocal(bv, SELECTION, false);
772 void InsetText::InsetKeyPress(XKeyEvent * xke)
774 if (the_locking_inset) {
775 the_locking_inset->InsetKeyPress(xke);
781 UpdatableInset::RESULT
782 InsetText::LocalDispatch(BufferView * bv,
783 kb_action action, string const & arg)
785 no_selection = false;
786 UpdatableInset::RESULT
787 result= UpdatableInset::LocalDispatch(bv, action, arg);
788 if (result != UNDISPATCHED) {
793 if ((action < 0) && arg.empty())
796 if (the_locking_inset) {
797 result = the_locking_inset->LocalDispatch(bv, action, arg);
798 if (result == DISPATCHED_NOUPDATE)
800 else if (result == DISPATCHED) {
801 UpdateLocal(bv, CURSOR_PAR, false);
803 } else if (result == FINISHED) {
804 bool dispatched = false;
806 case LFUN_UNKNOWN_ACTION:
807 case LFUN_BREAKPARAGRAPH:
809 moveRightIntern(bv, false, false);
812 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
813 moveRightIntern(bv, false, false);
817 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
818 moveRightIntern(bv, false, false);
824 the_locking_inset = 0;
832 case LFUN_UNKNOWN_ACTION:
833 if (bv->buffer()->isReadonly()) {
834 // setErrorMessage(N_("Document is read only"));
838 /* Automatically delete the currently selected
839 * text and replace it with what is being
840 * typed in now. Depends on lyxrc settings
841 * "auto_region_delete", which defaults to
844 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
845 bv->text->cursor.par()->previous(),
846 bv->text->cursor.par()->next());
848 if (lyxrc.auto_region_delete) {
849 if (TEXT(bv)->selection.set()) {
850 TEXT(bv)->CutSelection(bv, false);
853 TEXT(bv)->ClearSelection(bv);
854 for (string::size_type i = 0; i < arg.length(); ++i) {
856 if (!math_insert_greek(bv, arg[i])) {
857 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
858 } else if (!the_locking_inset) {
859 (void)moveRight(bv, false);
862 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
866 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
867 UpdateLocal(bv, CURSOR_PAR, true);
868 result=DISPATCHED_NOUPDATE;
870 // --- Cursor Movements -----------------------------------
872 bv->text->FinishUndo();
873 moveRight(bv, false, true);
874 TEXT(bv)->SetSelection(bv);
875 UpdateLocal(bv, SELECTION, false);
878 result = moveRight(bv);
879 bv->text->FinishUndo();
880 UpdateLocal(bv, CURSOR, false);
883 bv->text->FinishUndo();
884 moveLeft(bv, false, true);
885 TEXT(bv)->SetSelection(bv);
886 UpdateLocal(bv, SELECTION, false);
889 bv->text->FinishUndo();
890 result = moveLeft(bv);
891 UpdateLocal(bv, CURSOR, false);
894 bv->text->FinishUndo();
896 TEXT(bv)->SetSelection(bv);
897 UpdateLocal(bv, SELECTION, false);
900 bv->text->FinishUndo();
901 result = moveDown(bv);
902 UpdateLocal(bv, CURSOR, false);
905 bv->text->FinishUndo();
907 TEXT(bv)->SetSelection(bv);
908 UpdateLocal(bv, SELECTION, false);
911 bv->text->FinishUndo();
913 UpdateLocal(bv, CURSOR, false);
916 bv->text->FinishUndo();
917 TEXT(bv)->CursorHome(bv);
918 UpdateLocal(bv, CURSOR, false);
921 TEXT(bv)->CursorEnd(bv);
922 UpdateLocal(bv, CURSOR, false);
925 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
926 bv->text->cursor.par()->previous(),
927 bv->text->cursor.par()->next());
928 if (TEXT(bv)->selection.set())
929 TEXT(bv)->CutSelection(bv);
931 TEXT(bv)->Backspace(bv);
932 UpdateLocal(bv, CURSOR_PAR, true);
935 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
936 bv->text->cursor.par()->previous(),
937 bv->text->cursor.par()->next());
938 if (TEXT(bv)->selection.set())
939 TEXT(bv)->CutSelection(bv);
941 TEXT(bv)->Delete(bv);
942 UpdateLocal(bv, CURSOR_PAR, true);
945 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
946 bv->text->cursor.par()->previous(),
947 bv->text->cursor.par()->next());
948 TEXT(bv)->CutSelection(bv);
949 UpdateLocal(bv, CURSOR_PAR, true);
952 bv->text->FinishUndo();
953 TEXT(bv)->CopySelection(bv);
954 UpdateLocal(bv, CURSOR_PAR, false);
956 case LFUN_PASTESELECTION:
958 string const clip(bv->getClipboard());
962 if (arg == "paragraph") {
963 TEXT(bv)->InsertStringB(bv, clip);
965 TEXT(bv)->InsertStringA(bv, clip);
967 UpdateLocal(bv, CURSOR_PAR, true);
971 if (!autoBreakRows) {
974 if (cap.nrOfParagraphs() > 1) {
975 WriteAlert(_("Impossible operation"),
976 _("Cannot include more than one paragraph!"),
981 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
982 bv->text->cursor.par()->previous(),
983 bv->text->cursor.par()->next());
984 TEXT(bv)->PasteSelection(bv);
985 UpdateLocal(bv, CURSOR_PAR, true);
987 case LFUN_BREAKPARAGRAPH:
990 TEXT(bv)->BreakParagraph(bv, 0);
991 UpdateLocal(bv, FULL, true);
993 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
996 TEXT(bv)->BreakParagraph(bv, 1);
997 UpdateLocal(bv, FULL, true);
1002 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1003 bv->text->cursor.par()->previous(),
1004 bv->text->cursor.par()->next());
1005 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1006 UpdateLocal(bv, CURSOR_PAR, true);
1009 // do not set layouts on non breakable textinsets
1010 if (autoBreakRows) {
1011 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1013 // Derive layout number from given argument (string)
1014 // and current buffer's textclass (number). */
1015 LyXTextClassList::ClassList::size_type tclass =
1016 bv->buffer()->params.textclass;
1017 std::pair <bool, LyXTextClass::size_type> layout =
1018 textclasslist.NumberOfLayout(tclass, arg);
1020 // If the entry is obsolete, use the new one instead.
1022 string obs = textclasslist.Style(tclass,layout.second).
1025 layout = textclasslist.NumberOfLayout(tclass, obs);
1028 // see if we found the layout number:
1029 if (!layout.first) {
1030 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1031 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1035 if (cur_layout != layout.second) {
1036 cur_layout = layout.second;
1037 TEXT(bv)->SetLayout(bv, layout.second);
1038 bv->owner()->setLayout(cpar(bv)->GetLayout());
1039 UpdateLocal(bv, CURSOR_PAR, true);
1042 // reset the layout box
1043 bv->owner()->setLayout(cpar(bv)->GetLayout());
1046 case LFUN_PARAGRAPH_SPACING:
1047 // This one is absolutely not working. When fiddling with this
1048 // it also seems to me that the paragraphs inside the insettext
1049 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1051 LyXParagraph * par = TEXT(bv)->cursor.par();
1052 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1053 float cur_value = 1.0;
1054 if (cur_spacing == Spacing::Other) {
1055 cur_value = par->params.spacing().getValue();
1058 std::istringstream istr(arg.c_str());
1061 Spacing::Space new_spacing = cur_spacing;
1062 float new_value = cur_value;
1064 lyxerr << "Missing argument to `paragraph-spacing'"
1066 } else if (tmp == "single") {
1067 new_spacing = Spacing::Single;
1068 } else if (tmp == "onehalf") {
1069 new_spacing = Spacing::Onehalf;
1070 } else if (tmp == "double") {
1071 new_spacing = Spacing::Double;
1072 } else if (tmp == "other") {
1073 new_spacing = Spacing::Other;
1076 lyxerr << "new_value = " << tmpval << endl;
1079 } else if (tmp == "default") {
1080 new_spacing = Spacing::Default;
1082 lyxerr << _("Unknown spacing argument: ")
1085 if (cur_spacing != new_spacing || cur_value != new_value) {
1086 par->params.spacing(Spacing(new_spacing, new_value));
1087 //TEXT(bv)->RedoParagraph(owner->view());
1088 UpdateLocal(bv, CURSOR_PAR, true);
1089 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1095 if (!bv->Dispatch(action, arg))
1096 result = UNDISPATCHED;
1100 /// If the action has deleted all text in the inset, we need to change the
1101 // language to the language to the surronding text.
1102 if (par->size() == 0 && !par->next()) {
1103 LyXFont font(LyXFont::ALL_IGNORE);
1104 font.setLanguage(bv->getParentLanguage(this));
1105 SetFont(bv, font, false);
1108 if (result != FINISHED) {
1109 ShowInsetCursor(bv);
1111 bv->unlockInset(this);
1116 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1119 buf->latexParagraphs(os, par, 0, texrow);
1120 return texrow.rows();
1124 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1126 LyXParagraph * p = par;
1127 unsigned int lines = 0;
1130 string const tmp = buf->asciiParagraph(p, linelen);
1131 lines += countChar(tmp, '\n');
1139 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1141 LyXParagraph * p = par;
1142 unsigned int lines = 0;
1147 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1155 void InsetText::Validate(LaTeXFeatures & features) const
1157 LyXParagraph * p = par;
1159 p->validate(features);
1165 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1167 if (textclasslist.Style(buf->params.textclass,
1168 p->GetLayout()).labeltype != LABEL_MANUAL)
1171 return p->BeginningOfMainBody();
1175 void InsetText::GetCursorPos(BufferView * bv,
1176 int & x, int & y) const
1183 unsigned int InsetText::InsetInInsetY()
1185 if (!the_locking_inset)
1188 return (inset_y + the_locking_inset->InsetInInsetY());
1192 void InsetText::ToggleInsetCursor(BufferView * bv)
1194 if (the_locking_inset) {
1195 the_locking_inset->ToggleInsetCursor(bv);
1199 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1201 int const asc = lyxfont::maxAscent(font);
1202 int const desc = lyxfont::maxDescent(font);
1204 if (isCursorVisible())
1205 bv->hideLockedInsetCursor();
1207 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1208 toggleCursorVisible();
1212 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1214 if (the_locking_inset) {
1215 the_locking_inset->ShowInsetCursor(bv);
1218 if (!isCursorVisible()) {
1219 LyXFont const font =
1220 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1222 int const asc = lyxfont::maxAscent(font);
1223 int const desc = lyxfont::maxDescent(font);
1225 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1227 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1228 setCursorVisible(true);
1233 void InsetText::HideInsetCursor(BufferView * bv)
1235 if (isCursorVisible()) {
1236 bv->hideLockedInsetCursor();
1237 setCursorVisible(false);
1239 if (the_locking_inset)
1240 the_locking_inset->HideInsetCursor(bv);
1244 UpdatableInset::RESULT
1245 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1247 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1248 return moveLeftIntern(bv, false, activate_inset, selecting);
1250 return moveRightIntern(bv, false, activate_inset, selecting);
1254 UpdatableInset::RESULT
1255 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1257 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1258 return moveRightIntern(bv, true, activate_inset, selecting);
1260 return moveLeftIntern(bv, true, activate_inset, selecting);
1264 UpdatableInset::RESULT
1265 InsetText::moveRightIntern(BufferView * bv, bool behind,
1266 bool activate_inset, bool selecting)
1268 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1270 if (activate_inset && checkAndActivateInset(bv, behind))
1272 TEXT(bv)->CursorRight(bv);
1274 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1275 return DISPATCHED_NOUPDATE;
1279 UpdatableInset::RESULT
1280 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1281 bool activate_inset, bool selecting)
1283 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1285 TEXT(bv)->CursorLeft(bv);
1287 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1288 if (activate_inset && checkAndActivateInset(bv, behind))
1290 return DISPATCHED_NOUPDATE;
1294 UpdatableInset::RESULT
1295 InsetText::moveUp(BufferView * bv)
1297 if (!crow(bv)->previous())
1299 TEXT(bv)->CursorUp(bv);
1300 return DISPATCHED_NOUPDATE;
1304 UpdatableInset::RESULT
1305 InsetText::moveDown(BufferView * bv)
1307 if (!crow(bv)->next())
1309 TEXT(bv)->CursorDown(bv);
1310 return DISPATCHED_NOUPDATE;
1314 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1316 if (the_locking_inset) {
1317 if (the_locking_inset->InsertInsetAllowed(inset))
1318 return the_locking_inset->InsertInset(bv, inset);
1321 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1322 bv->text->cursor.par()->previous(),
1323 bv->text->cursor.par()->next());
1324 inset->setOwner(this);
1325 HideInsetCursor(bv);
1326 TEXT(bv)->InsertInset(bv, inset);
1328 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1329 (cpar(bv)->GetInset(cpos(bv)) != inset))
1330 TEXT(bv)->CursorLeft(bv);
1332 bv->fitCursor(TEXT(bv));
1333 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1334 ShowInsetCursor(bv);
1339 UpdatableInset * InsetText::GetLockingInset()
1341 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1345 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1349 if (the_locking_inset)
1350 return the_locking_inset->GetFirstLockingInsetOfType(c);
1355 bool InsetText::ShowInsetDialog(BufferView * bv) const
1357 if (the_locking_inset)
1358 return the_locking_inset->ShowInsetDialog(bv);
1363 std::vector<string> const InsetText::getLabelList() const
1365 std::vector<string> label_list;
1367 LyXParagraph * tpar = par;
1369 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1370 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1371 for (; beg != end; ++beg) {
1372 std::vector<string> const l = (*beg)->getLabelList();
1373 label_list.insert(label_list.end(), l.begin(), l.end());
1375 tpar = tpar->next();
1381 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1384 if (the_locking_inset) {
1385 the_locking_inset->SetFont(bv, font, toggleall, selectall);
1388 if (TEXT(bv)->selection.set()) {
1389 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1390 bv->text->cursor.par()->previous(),
1391 bv->text->cursor.par()->next());
1395 TEXT(bv)->SetFont(bv, font, toggleall);
1397 TEXT(bv)->ClearSelection(bv);
1398 bv->fitCursor(TEXT(bv));
1399 if (selectall || TEXT(bv)->selection.set())
1400 UpdateLocal(bv, FULL, true);
1402 UpdateLocal(bv, CURSOR_PAR, true);
1406 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1408 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1412 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1413 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1415 LyXFont const font =
1416 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1418 x = inset->width(bv, font);
1419 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1422 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1424 //inset_x = cx(bv) - top_x + drawTextXOffset;
1425 //inset_y = cy(bv) + drawTextYOffset;
1426 inset->Edit(bv, x, y, 0);
1427 if (!the_locking_inset)
1429 UpdateLocal(bv, CURSOR, false);
1436 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1441 dummyx = x = x - drawTextXOffset;
1442 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(LyXParagraph * 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 LyXParagraph * tmp = par->next();
1493 par = new LyXParagraph(*p);
1494 par->SetInsetOwner(this);
1495 LyXParagraph * np = par;
1498 np->next(new LyXParagraph(*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 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1572 return TEXT(bv)->cursor.pos();
1576 LyXParagraph * 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 LyXParagraph * 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 LyXParagraph * lpar = 0;
1644 LyXParagraph * selstartpar = 0;
1645 LyXParagraph * selendpar = 0;
1646 LyXParagraph::size_type pos = 0;
1647 LyXParagraph::size_type selstartpos = 0;
1648 LyXParagraph::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 (LyXParagraph * p = par; p; p = p->next()) {
1701 p->resizeInsetsLyXText(bv);
1708 void InsetText::removeNewlines()
1710 for (LyXParagraph * p = par; p; p = p->next()) {
1711 for (int i = 0; i < p->size(); ++i) {
1712 if (p->GetChar(i) == LyXParagraph::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));
1743 void InsetText::selectAll(BufferView * bv)
1745 TEXT(bv)->CursorTop(bv);
1746 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1747 TEXT(bv)->CursorBottom(bv);
1748 TEXT(bv)->SetSelection(bv);
1751 void InsetText::clearSelection(BufferView * bv)
1753 TEXT(bv)->ClearSelection(bv);
1756 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1759 int h = insetAscent + insetDescent;
1760 int ty = baseline - insetAscent;
1766 if ((ty + h) > pain.paperHeight())
1767 h = pain.paperHeight();
1768 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1769 w = pain.paperWidth();
1770 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);