2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2001 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"
41 #include "support/textutils.h"
42 #include "support/LAssert.h"
46 #include "trans_mgr.h"
47 #include "lyxscreen.h"
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
62 InsetText::InsetText()
64 par = new LyXParagraph;
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 LyXParagraph * 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 LyXParagraph * tmp = par->next();
148 par = new LyXParagraph;
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 LyXParagraph * return_par = 0;
177 char depth = 0; // signed or unsigned?
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)))
288 int h = insetAscent + insetDescent;
289 int ty = baseline - insetAscent;
295 if ((ty + h) > pain.paperHeight())
296 h = pain.paperHeight();
297 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
298 w = pain.paperWidth();
299 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
304 frame_is_visible = false;
306 if (!cleared && (need_update == NONE))
309 if (top_x != int(x)) {
310 if ((getMaxWidth(bv, this) > 0) && (TEXT(bv)->width != old_max_width)){
313 old_max_width = TEXT(bv)->width;
314 bv->text->status = LyXText::CHANGED_IN_DRAW;
324 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
326 if (cleared || (last_drawn_width != insetWidth)) {
328 last_drawn_width = insetWidth;
331 top_baseline = baseline;
332 top_y = baseline - ascent(bv, f);
333 last_width = width(bv, f);
334 last_height = ascent(bv, f) + descent(bv, f);
336 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
337 inset_x = cx(bv) - top_x + drawTextXOffset;
338 inset_y = cy(bv) + drawTextYOffset;
340 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection.set()) {
341 drawFrame(pain, cleared);
346 x += TEXT_TO_INSET_OFFSET;
349 #warning Jürgen, why is this a block of its own? (Lgb)
350 #warning because you told me to define variables only in local context (Jug)!
351 #warning then make it a function/method of its own. (Lgb)
355 Row * row = TEXT(bv)->GetRowNearY(y);
356 int y_offset = baseline - row->ascent_of_text();
357 int ph = pain.paperHeight();
360 while ((row != 0) && ((y+row->height()) <= 0)) {
362 first += row->height();
367 TEXT(bv)->first = first;
371 while ((row != 0) && (yf < ph)) {
372 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
378 } else if (!locked) {
379 if (need_update & CURSOR) {
380 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
381 TEXT(bv)->ClearSelection(bv);
382 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
384 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
387 if (need_update & SELECTION)
388 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
389 else if (need_update & CURSOR) {
390 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
391 TEXT(bv)->ClearSelection(bv);
392 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
394 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
398 TEXT(bv)->refresh_y = 0;
399 TEXT(bv)->status = LyXText::UNCHANGED;
400 if ((need_update != CURSOR_PAR) &&
401 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
402 drawFrame(pain, cleared);
403 else if (need_update & CLEAR_FRAME)
404 clearFrame(pain, cleared);
405 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
406 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
408 } else if (need_update != INIT)
413 void InsetText::drawFrame(Painter & pain, bool cleared) const
415 if (!frame_is_visible || cleared) {
416 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
417 insetWidth - 1, insetAscent + insetDescent - 1,
419 frame_is_visible = true;
424 void InsetText::clearFrame(Painter & pain, bool cleared) const
426 if (frame_is_visible) {
428 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
429 insetWidth - 1, insetAscent + insetDescent - 1,
432 frame_is_visible = false;
437 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
443 owner()->update(bv, font, true);
446 if (the_locking_inset) {
447 inset_x = cx(bv) - top_x + drawTextXOffset;
448 inset_y = cy(bv) + drawTextYOffset;
449 the_locking_inset->update(bv, font, reinit);
452 if (need_update == INIT) {
457 int oldw = insetWidth;
458 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
459 if (oldw != insetWidth) {
462 // update(bv, font, reinit);
465 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
468 TEXT(bv)->UpdateInset(bv, the_locking_inset);
471 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
475 Row * row = TEXT(bv)->GetRowNearY(y_temp);
476 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
477 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
478 TEXT_TO_INSET_OFFSET;
483 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
486 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
488 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
489 need_update |= CURSOR_PAR;
491 // this to not draw a selection when we redraw all of it!
492 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
493 TEXT(bv)->ClearSelection(bv);
497 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
499 TEXT(bv)->FullRebreak(bv);
500 SetUpdateStatus(bv, what);
501 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
502 TEXT(bv)->selection.set())
503 bv->updateInset(this, mark_dirty);
504 bv->owner()->showState();
505 if (old_par != cpar(bv)) {
506 bv->owner()->setLayout(cpar(bv)->GetLayout());
512 string const InsetText::EditMessage() const
514 return _("Opened Text Inset");
518 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
520 // par->SetInsetOwner(this);
521 UpdatableInset::Edit(bv, x, y, button);
523 if (!bv->lockInset(this)) {
524 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
528 the_locking_inset = 0;
529 inset_pos = inset_x = inset_y = 0;
530 inset_boundary = false;
533 int tmp_y = (y < 0)?0:y;
534 if (!checkAndActivateInset(bv, x, tmp_y, button))
535 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
537 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
538 bv->text->FinishUndo();
540 UpdateLocal(bv, CURSOR, false);
542 // If the inset is empty set the language of the current font to the
543 // language to the surronding text (if different).
544 if (par->size() == 0 && !par->next() &&
545 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
547 LyXFont font(LyXFont::ALL_IGNORE);
548 font.setLanguage(bv->getParentLanguage(this));
549 SetFont(bv, font, false);
554 void InsetText::InsetUnlock(BufferView * bv)
556 if (the_locking_inset) {
557 the_locking_inset->InsetUnlock(bv);
558 the_locking_inset = 0;
561 no_selection = false;
563 int code = CURSOR|CLEAR_FRAME;
564 if (TEXT(bv)->selection.set()) {
565 TEXT(bv)->ClearSelection(bv);
568 bv->owner()->setLayout(owner()->getLyXText(bv)
569 ->cursor.par()->GetLayout());
571 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
572 UpdateLocal(bv, code, false);
576 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
578 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
582 if (inset == cpar(bv)->GetInset(cpos(bv))) {
583 lyxerr[Debug::INSETS] << "OK" << endl;
584 the_locking_inset = inset;
585 inset_x = cx(bv) - top_x + drawTextXOffset;
586 inset_y = cy(bv) + drawTextYOffset;
587 inset_pos = cpos(bv);
588 inset_par = cpar(bv);
589 inset_boundary = cboundary(bv);
591 TEXT(bv)->ClearSelection(bv);
592 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
593 TEXT(bv)->UpdateInset(bv, the_locking_inset);
595 UpdateLocal(bv, CURSOR, false);
598 } else if (the_locking_inset && (the_locking_inset == inset)) {
599 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
600 lyxerr[Debug::INSETS] << "OK" << endl;
601 inset_x = cx(bv) - top_x + drawTextXOffset;
602 inset_y = cy(bv) + drawTextYOffset;
604 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
606 } else if (the_locking_inset) {
607 lyxerr[Debug::INSETS] << "MAYBE" << endl;
608 return the_locking_inset->LockInsetInInset(bv, inset);
610 lyxerr[Debug::INSETS] << "NOT OK" << endl;
615 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
618 if (!the_locking_inset)
620 if (the_locking_inset == inset) {
621 the_locking_inset->InsetUnlock(bv);
622 TEXT(bv)->UpdateInset(bv, inset);
623 the_locking_inset = 0;
625 moveRight(bv, false);
626 old_par = 0; // force layout setting
630 UpdateLocal(bv, CURSOR, false);
633 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
637 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
639 if (!the_locking_inset)
641 if (the_locking_inset != inset) {
642 TEXT(bv)->UpdateInset(bv, the_locking_inset);
643 SetUpdateStatus(bv, CURSOR_PAR);
644 return the_locking_inset->UpdateInsetInInset(bv, inset);
646 // UpdateLocal(bv, FULL, false);
647 if (TEXT(bv)->UpdateInset(bv, inset))
648 UpdateLocal(bv, CURSOR_PAR, false);
649 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
650 inset_x = cx(bv) - top_x + drawTextXOffset;
651 inset_y = cy(bv) + drawTextYOffset;
657 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
661 int tmp_x = x - drawTextXOffset;
662 int tmp_y = y + insetAscent - TEXT(bv)->first;
663 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
666 if (the_locking_inset) {
667 if (the_locking_inset == inset) {
668 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
669 no_selection = false;
672 // otherwise unlock the_locking_inset and lock the new inset
673 the_locking_inset->InsetUnlock(bv);
674 inset_x = cx(bv) - top_x + drawTextXOffset;
675 inset_y = cy(bv) + drawTextYOffset;
676 the_locking_inset = static_cast<UpdatableInset*>(inset);
677 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
678 inset->Edit(bv, x - inset_x, y - inset_y, button);
679 if (the_locking_inset)
680 UpdateLocal(bv, CURSOR, false);
681 no_selection = false;
684 // otherwise only unlock the_locking_inset
685 the_locking_inset->InsetUnlock(bv);
686 the_locking_inset = 0;
688 if (bv->theLockingInset()) {
689 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
690 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
691 inset_x = cx(bv) - top_x + drawTextXOffset;
692 inset_y = cy(bv) + drawTextYOffset;
693 inset_pos = cpos(bv);
694 inset_par = cpar(bv);
695 inset_boundary = cboundary(bv);
696 the_locking_inset = uinset;
697 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
699 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
700 if (the_locking_inset)
701 UpdateLocal(bv, CURSOR, false);
702 no_selection = false;
706 if (!inset) { // && (button == 2)) {
707 bool paste_internally = false;
708 if ((button == 2) && TEXT(bv)->selection.set()) {
709 LocalDispatch(bv, LFUN_COPY, "");
710 paste_internally = true;
712 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
714 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
715 UpdateLocal(bv, CURSOR, false);
716 bv->owner()->setLayout(cpar(bv)->GetLayout());
718 // Insert primary selection with middle mouse
719 // if there is a local selection in the current buffer,
722 if (paste_internally)
723 LocalDispatch(bv, LFUN_PASTE, "");
725 LocalDispatch(bv, LFUN_PASTESELECTION,
730 no_selection = false;
734 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
736 UpdatableInset * inset = 0;
738 if (the_locking_inset) {
739 the_locking_inset->InsetButtonRelease(bv,
740 x - inset_x, y - inset_y,
743 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
744 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
745 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
746 inset->InsetButtonRelease(bv,
748 y - inset_y, button);
750 inset_x = cx(bv) - top_x + drawTextXOffset;
751 inset_y = cy(bv) + drawTextYOffset;
752 inset->InsetButtonRelease(bv,
754 y - inset_y, button);
756 x - inset_x, y - inset_y, button);
758 UpdateLocal(bv, CURSOR_PAR, false);
761 no_selection = false;
765 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
769 if (the_locking_inset) {
770 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
775 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
777 TEXT(bv)->SetSelection(bv);
778 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
779 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
780 UpdateLocal(bv, SELECTION, false);
785 void InsetText::InsetKeyPress(XKeyEvent * xke)
787 if (the_locking_inset) {
788 the_locking_inset->InsetKeyPress(xke);
794 UpdatableInset::RESULT
795 InsetText::LocalDispatch(BufferView * bv,
796 kb_action action, string const & arg)
798 no_selection = false;
799 UpdatableInset::RESULT
800 result= UpdatableInset::LocalDispatch(bv, action, arg);
801 if (result != UNDISPATCHED) {
806 if ((action < 0) && arg.empty())
809 if (the_locking_inset) {
810 result = the_locking_inset->LocalDispatch(bv, action, arg);
811 if (result == DISPATCHED_NOUPDATE)
813 else if (result == DISPATCHED) {
814 UpdateLocal(bv, CURSOR_PAR, false);
816 } else if (result == FINISHED) {
817 bool dispatched = false;
819 case LFUN_UNKNOWN_ACTION:
820 case LFUN_BREAKPARAGRAPH:
822 moveRightIntern(bv, false, false);
825 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
826 moveRightIntern(bv, false, false);
830 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
831 moveRightIntern(bv, false, false);
837 the_locking_inset = 0;
845 case LFUN_UNKNOWN_ACTION:
846 if (bv->buffer()->isReadonly()) {
847 // setErrorMessage(N_("Document is read only"));
851 /* Automatically delete the currently selected
852 * text and replace it with what is being
853 * typed in now. Depends on lyxrc settings
854 * "auto_region_delete", which defaults to
857 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
858 bv->text->cursor.par()->previous(),
859 bv->text->cursor.par()->next());
861 if (lyxrc.auto_region_delete) {
862 if (TEXT(bv)->selection.set()) {
863 TEXT(bv)->CutSelection(bv, false);
866 TEXT(bv)->ClearSelection(bv);
867 for (string::size_type i = 0; i < arg.length(); ++i) {
869 if (!math_insert_greek(bv, arg[i])) {
870 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
871 } else if (!the_locking_inset) {
872 (void)moveRight(bv, false);
875 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
879 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
880 UpdateLocal(bv, CURSOR_PAR, true);
881 result=DISPATCHED_NOUPDATE;
883 // --- Cursor Movements -----------------------------------
885 bv->text->FinishUndo();
886 moveRight(bv, false, true);
887 TEXT(bv)->SetSelection(bv);
888 UpdateLocal(bv, SELECTION, false);
891 result = moveRight(bv);
892 bv->text->FinishUndo();
893 UpdateLocal(bv, CURSOR, false);
896 bv->text->FinishUndo();
897 moveLeft(bv, false, true);
898 TEXT(bv)->SetSelection(bv);
899 UpdateLocal(bv, SELECTION, false);
902 bv->text->FinishUndo();
903 result = moveLeft(bv);
904 UpdateLocal(bv, CURSOR, false);
907 bv->text->FinishUndo();
909 TEXT(bv)->SetSelection(bv);
910 UpdateLocal(bv, SELECTION, false);
913 bv->text->FinishUndo();
914 result = moveDown(bv);
915 UpdateLocal(bv, CURSOR, false);
918 bv->text->FinishUndo();
920 TEXT(bv)->SetSelection(bv);
921 UpdateLocal(bv, SELECTION, false);
924 bv->text->FinishUndo();
926 UpdateLocal(bv, CURSOR, false);
929 bv->text->FinishUndo();
930 TEXT(bv)->CursorHome(bv);
931 UpdateLocal(bv, CURSOR, false);
934 TEXT(bv)->CursorEnd(bv);
935 UpdateLocal(bv, CURSOR, false);
938 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
939 bv->text->cursor.par()->previous(),
940 bv->text->cursor.par()->next());
941 if (TEXT(bv)->selection.set())
942 TEXT(bv)->CutSelection(bv);
944 TEXT(bv)->Backspace(bv);
945 UpdateLocal(bv, CURSOR_PAR, true);
948 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
949 bv->text->cursor.par()->previous(),
950 bv->text->cursor.par()->next());
951 if (TEXT(bv)->selection.set())
952 TEXT(bv)->CutSelection(bv);
954 TEXT(bv)->Delete(bv);
955 UpdateLocal(bv, CURSOR_PAR, true);
958 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
959 bv->text->cursor.par()->previous(),
960 bv->text->cursor.par()->next());
961 TEXT(bv)->CutSelection(bv);
962 UpdateLocal(bv, CURSOR_PAR, true);
965 bv->text->FinishUndo();
966 TEXT(bv)->CopySelection(bv);
967 UpdateLocal(bv, CURSOR_PAR, false);
969 case LFUN_PASTESELECTION:
971 string const clip(bv->getClipboard());
975 if (arg == "paragraph") {
976 TEXT(bv)->InsertStringB(bv, clip);
978 TEXT(bv)->InsertStringA(bv, clip);
980 UpdateLocal(bv, CURSOR_PAR, true);
984 if (!autoBreakRows) {
987 if (cap.nrOfParagraphs() > 1) {
988 WriteAlert(_("Impossible operation"),
989 _("Cannot include more than one paragraph!"),
994 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
995 bv->text->cursor.par()->previous(),
996 bv->text->cursor.par()->next());
997 TEXT(bv)->PasteSelection(bv);
998 UpdateLocal(bv, CURSOR_PAR, true);
1000 case LFUN_BREAKPARAGRAPH:
1003 TEXT(bv)->BreakParagraph(bv, 0);
1004 UpdateLocal(bv, FULL, true);
1006 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1009 TEXT(bv)->BreakParagraph(bv, 1);
1010 UpdateLocal(bv, FULL, true);
1012 case LFUN_BREAKLINE:
1015 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1016 bv->text->cursor.par()->previous(),
1017 bv->text->cursor.par()->next());
1018 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1019 UpdateLocal(bv, CURSOR_PAR, true);
1022 // do not set layouts on non breakable textinsets
1023 if (autoBreakRows) {
1024 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1026 // Derive layout number from given argument (string)
1027 // and current buffer's textclass (number). */
1028 LyXTextClassList::ClassList::size_type tclass =
1029 bv->buffer()->params.textclass;
1030 std::pair <bool, LyXTextClass::size_type> layout =
1031 textclasslist.NumberOfLayout(tclass, arg);
1033 // If the entry is obsolete, use the new one instead.
1035 string obs = textclasslist.Style(tclass,layout.second).
1038 layout = textclasslist.NumberOfLayout(tclass, obs);
1041 // see if we found the layout number:
1042 if (!layout.first) {
1043 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1044 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1048 if (cur_layout != layout.second) {
1049 cur_layout = layout.second;
1050 TEXT(bv)->SetLayout(bv, layout.second);
1051 bv->owner()->setLayout(cpar(bv)->GetLayout());
1052 UpdateLocal(bv, CURSOR_PAR, true);
1055 // reset the layout box
1056 bv->owner()->setLayout(cpar(bv)->GetLayout());
1059 case LFUN_PARAGRAPH_SPACING:
1060 // This one is absolutely not working. When fiddling with this
1061 // it also seems to me that the paragraphs inside the insettext
1062 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1064 LyXParagraph * par = TEXT(bv)->cursor.par();
1065 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1066 float cur_value = 1.0;
1067 if (cur_spacing == Spacing::Other) {
1068 cur_value = par->params.spacing().getValue();
1071 std::istringstream istr(arg.c_str());
1074 Spacing::Space new_spacing = cur_spacing;
1075 float new_value = cur_value;
1077 lyxerr << "Missing argument to `paragraph-spacing'"
1079 } else if (tmp == "single") {
1080 new_spacing = Spacing::Single;
1081 } else if (tmp == "onehalf") {
1082 new_spacing = Spacing::Onehalf;
1083 } else if (tmp == "double") {
1084 new_spacing = Spacing::Double;
1085 } else if (tmp == "other") {
1086 new_spacing = Spacing::Other;
1089 lyxerr << "new_value = " << tmpval << endl;
1092 } else if (tmp == "default") {
1093 new_spacing = Spacing::Default;
1095 lyxerr << _("Unknown spacing argument: ")
1098 if (cur_spacing != new_spacing || cur_value != new_value) {
1099 par->params.spacing(Spacing(new_spacing, new_value));
1100 //TEXT(bv)->RedoParagraph(owner->view());
1101 UpdateLocal(bv, CURSOR_PAR, true);
1102 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1108 if (!bv->Dispatch(action, arg))
1109 result = UNDISPATCHED;
1113 /// If the action has deleted all text in the inset, we need to change the
1114 // language to the language to the surronding text.
1115 if (par->size() == 0 && !par->next()) {
1116 LyXFont font(LyXFont::ALL_IGNORE);
1117 font.setLanguage(bv->getParentLanguage(this));
1118 SetFont(bv, font, false);
1121 if (result != FINISHED) {
1122 ShowInsetCursor(bv);
1124 bv->unlockInset(this);
1129 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1132 buf->latexParagraphs(os, par, 0, texrow);
1133 return texrow.rows();
1137 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1139 LyXParagraph * p = par;
1140 unsigned int lines = 0;
1143 string const tmp = buf->asciiParagraph(p, linelen);
1144 lines += countChar(tmp, '\n');
1152 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1154 LyXParagraph * p = par;
1155 unsigned int lines = 0;
1160 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1168 void InsetText::Validate(LaTeXFeatures & features) const
1170 LyXParagraph * p = par;
1172 p->validate(features);
1178 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1180 if (textclasslist.Style(buf->params.textclass,
1181 p->GetLayout()).labeltype != LABEL_MANUAL)
1184 return p->BeginningOfMainBody();
1188 void InsetText::GetCursorPos(BufferView * bv,
1189 int & x, int & y) const
1196 unsigned int InsetText::InsetInInsetY()
1198 if (!the_locking_inset)
1201 return (inset_y + the_locking_inset->InsetInInsetY());
1205 void InsetText::ToggleInsetCursor(BufferView * bv)
1207 if (the_locking_inset) {
1208 the_locking_inset->ToggleInsetCursor(bv);
1212 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1214 int const asc = lyxfont::maxAscent(font);
1215 int const desc = lyxfont::maxDescent(font);
1217 if (isCursorVisible())
1218 bv->hideLockedInsetCursor();
1220 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1221 toggleCursorVisible();
1225 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1227 if (the_locking_inset) {
1228 the_locking_inset->ShowInsetCursor(bv);
1231 if (!isCursorVisible()) {
1232 LyXFont const font =
1233 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1235 int const asc = lyxfont::maxAscent(font);
1236 int const desc = lyxfont::maxDescent(font);
1238 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1240 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1241 setCursorVisible(true);
1246 void InsetText::HideInsetCursor(BufferView * bv)
1248 if (isCursorVisible()) {
1249 bv->hideLockedInsetCursor();
1250 setCursorVisible(false);
1252 if (the_locking_inset)
1253 the_locking_inset->HideInsetCursor(bv);
1257 UpdatableInset::RESULT
1258 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1260 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1261 return moveLeftIntern(bv, false, activate_inset, selecting);
1263 return moveRightIntern(bv, false, activate_inset, selecting);
1267 UpdatableInset::RESULT
1268 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1270 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1271 return moveRightIntern(bv, true, activate_inset, selecting);
1273 return moveLeftIntern(bv, true, activate_inset, selecting);
1277 UpdatableInset::RESULT
1278 InsetText::moveRightIntern(BufferView * bv, bool behind,
1279 bool activate_inset, bool selecting)
1281 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1283 if (activate_inset && checkAndActivateInset(bv, behind))
1285 TEXT(bv)->CursorRight(bv);
1287 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1288 return DISPATCHED_NOUPDATE;
1292 UpdatableInset::RESULT
1293 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1294 bool activate_inset, bool selecting)
1296 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1298 TEXT(bv)->CursorLeft(bv);
1300 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1301 if (activate_inset && checkAndActivateInset(bv, behind))
1303 return DISPATCHED_NOUPDATE;
1307 UpdatableInset::RESULT
1308 InsetText::moveUp(BufferView * bv)
1310 if (!crow(bv)->previous())
1312 TEXT(bv)->CursorUp(bv);
1313 return DISPATCHED_NOUPDATE;
1317 UpdatableInset::RESULT
1318 InsetText::moveDown(BufferView * bv)
1320 if (!crow(bv)->next())
1322 TEXT(bv)->CursorDown(bv);
1323 return DISPATCHED_NOUPDATE;
1327 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1329 if (the_locking_inset) {
1330 if (the_locking_inset->InsertInsetAllowed(inset))
1331 return the_locking_inset->InsertInset(bv, inset);
1334 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1335 bv->text->cursor.par()->previous(),
1336 bv->text->cursor.par()->next());
1337 inset->setOwner(this);
1338 HideInsetCursor(bv);
1339 TEXT(bv)->InsertInset(bv, inset);
1341 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1342 (cpar(bv)->GetInset(cpos(bv)) != inset))
1343 TEXT(bv)->CursorLeft(bv);
1345 bv->fitCursor(TEXT(bv));
1346 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1347 ShowInsetCursor(bv);
1352 UpdatableInset * InsetText::GetLockingInset()
1354 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1358 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1362 if (the_locking_inset)
1363 return the_locking_inset->GetFirstLockingInsetOfType(c);
1368 bool InsetText::ShowInsetDialog(BufferView * bv) const
1370 if (the_locking_inset)
1371 return the_locking_inset->ShowInsetDialog(bv);
1376 std::vector<string> const InsetText::getLabelList() const
1378 std::vector<string> label_list;
1380 LyXParagraph * tpar = par;
1382 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1383 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1384 for (; beg != end; ++beg) {
1385 std::vector<string> const l = (*beg)->getLabelList();
1386 label_list.insert(label_list.end(), l.begin(), l.end());
1388 tpar = tpar->next();
1394 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1397 if (TEXT(bv)->selection.set()) {
1398 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1399 bv->text->cursor.par()->previous(),
1400 bv->text->cursor.par()->next());
1404 TEXT(bv)->SetFont(bv, font, toggleall);
1406 TEXT(bv)->ClearSelection(bv);
1407 bv->fitCursor(TEXT(bv));
1408 if (selectall || TEXT(bv)->selection.set())
1409 UpdateLocal(bv, FULL, true);
1411 UpdateLocal(bv, CURSOR_PAR, true);
1415 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1417 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1421 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1422 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1424 LyXFont const font =
1425 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1427 x = inset->width(bv, font);
1428 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1431 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1433 //inset_x = cx(bv) - top_x + drawTextXOffset;
1434 //inset_y = cy(bv) + drawTextYOffset;
1435 inset->Edit(bv, x, y, 0);
1436 if (!the_locking_inset)
1438 UpdateLocal(bv, CURSOR, false);
1445 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1450 dummyx = x = x - drawTextXOffset;
1451 dummyy = y + insetAscent;
1452 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1459 inset_x = cx(bv) - top_x + drawTextXOffset;
1460 inset_y = cy(bv) + drawTextYOffset;
1461 inset->Edit(bv, x - inset_x, y - inset_y, button);
1462 if (!the_locking_inset)
1464 UpdateLocal(bv, CURSOR, false);
1471 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1473 int w = UpdatableInset::getMaxWidth(bv, inset);
1478 w = w - top_x + owner()->x();
1481 w -= (2 * TEXT_TO_INSET_OFFSET);
1483 // return w - (2*TEXT_TO_INSET_OFFSET);
1487 void InsetText::SetParagraphData(LyXParagraph * p)
1489 // delete all instances of LyXText before deleting the paragraps used
1491 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1492 delete (*cit).second;
1497 LyXParagraph * tmp = par->next();
1502 par = new LyXParagraph(*p);
1503 par->SetInsetOwner(this);
1504 LyXParagraph * np = par;
1507 np->next(new LyXParagraph(*p));
1508 np->next()->previous(np);
1510 np->SetInsetOwner(this);
1517 void InsetText::SetText(string const & data)
1520 LyXFont font(LyXFont::ALL_SANE);
1521 for (unsigned int i=0; i < data.length(); ++i)
1522 par->InsertChar(i, data[i], font);
1526 void InsetText::SetAutoBreakRows(bool flag)
1528 if (flag != autoBreakRows) {
1529 autoBreakRows = flag;
1537 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1539 if (how != drawFrame_) {
1542 UpdateLocal(bv, DRAW_FRAME, false);
1547 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1549 if (frame_color != col) {
1552 UpdateLocal(bv, DRAW_FRAME, false);
1557 int InsetText::cx(BufferView * bv) const
1559 LyXText * text = TEXT(bv);
1560 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1561 if (the_locking_inset) {
1562 LyXFont font = text->GetFont(bv->buffer(),
1564 text->cursor.pos());
1565 if (font.isVisibleRightToLeft())
1566 x -= the_locking_inset->width(bv, font);
1572 int InsetText::cy(BufferView * bv) const
1575 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1579 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1581 return TEXT(bv)->cursor.pos();
1585 LyXParagraph * InsetText::cpar(BufferView * bv) const
1587 return TEXT(bv)->cursor.par();
1591 bool InsetText::cboundary(BufferView * bv) const
1593 return TEXT(bv)->cursor.boundary();
1597 Row * InsetText::crow(BufferView * bv) const
1599 return TEXT(bv)->cursor.row();
1603 LyXText * InsetText::getLyXText(BufferView const * lbv,
1604 bool const recursive) const
1606 // Super UGLY! (Lgb)
1607 BufferView * bv = const_cast<BufferView *>(lbv);
1609 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1610 if (recursive && the_locking_inset)
1611 return the_locking_inset->getLyXText(bv);
1614 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1617 if (the_locking_inset) {
1618 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1620 return the_locking_inset->getLyXText(bv);
1626 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1628 if ((cache.find(bv) == cache.end()) || !cache[bv])
1633 /// then remove all LyXText in text-insets
1634 LyXParagraph * p = par;
1635 for (; p; p = p->next()) {
1636 p->deleteInsetsLyXText(bv);
1642 void InsetText::resizeLyXText(BufferView * bv) const
1644 if (!par->next() && !par->size()) // no data, resize not neccessary!
1646 if (getMaxWidth(bv, this) < 0) // one endless line, no resize necessary
1648 if ((cache.find(bv) == cache.end()) || !cache[bv])
1651 LyXParagraph * lpar = 0;
1652 LyXParagraph * selstartpar = 0;
1653 LyXParagraph * selendpar = 0;
1654 LyXParagraph::size_type pos = 0;
1655 LyXParagraph::size_type selstartpos = 0;
1656 LyXParagraph::size_type selendpos = 0;
1657 bool boundary = false;
1658 bool selstartboundary = false;
1659 bool selendboundary = false;
1660 bool selection = false;
1661 bool mark_set = false;
1663 // ProhibitInput(bv);
1666 lpar = TEXT(bv)->cursor.par();
1667 pos = TEXT(bv)->cursor.pos();
1668 boundary = TEXT(bv)->cursor.boundary();
1669 selstartpar = TEXT(bv)->selection.start.par();
1670 selstartpos = TEXT(bv)->selection.start.pos();
1671 selstartboundary = TEXT(bv)->selection.start.boundary();
1672 selendpar = TEXT(bv)->selection.end.par();
1673 selendpos = TEXT(bv)->selection.end.pos();
1674 selendboundary = TEXT(bv)->selection.end.boundary();
1675 selection = TEXT(bv)->selection.set();
1676 mark_set = TEXT(bv)->selection.mark();
1678 deleteLyXText(bv, (the_locking_inset == 0));
1681 TEXT(bv)->selection.set(true);
1682 /* at this point just to avoid the Delete-Empty-Paragraph
1683 * Mechanism when setting the cursor */
1684 TEXT(bv)->selection.mark(mark_set);
1686 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1687 true, selstartboundary);
1688 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1689 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1690 true, selendboundary);
1691 TEXT(bv)->SetSelection(bv);
1692 TEXT(bv)->SetCursor(bv, lpar, pos);
1694 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1695 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1696 TEXT(bv)->selection.set(false);
1700 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1701 // this will scroll the screen such that the cursor becomes visible
1702 bv->updateScrollbar();
1704 if (the_locking_inset) {
1705 /// then resize all LyXText in text-insets
1706 inset_x = cx(bv) - top_x + drawTextXOffset;
1707 inset_y = cy(bv) + drawTextYOffset;
1708 for (LyXParagraph * p = par; p; p = p->next()) {
1709 p->resizeInsetsLyXText(bv);
1716 void InsetText::removeNewlines()
1718 for (LyXParagraph * p = par; p; p = p->next()) {
1719 for (int i = 0; i < p->size(); ++i) {
1720 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1727 bool InsetText::nodraw() const
1729 if (the_locking_inset)
1730 return the_locking_inset->nodraw();
1731 return UpdatableInset::nodraw();
1735 int InsetText::scroll(bool recursive) const
1737 int sx = UpdatableInset::scroll(false);
1739 if (recursive && the_locking_inset)
1740 sx += the_locking_inset->scroll(recursive);
1746 bool InsetText::doClearArea() const
1748 return !locked || (need_update & (FULL|INIT));
1751 void InsetText::selectAll(BufferView * bv)
1753 TEXT(bv)->CursorTop(bv);
1754 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1755 TEXT(bv)->CursorBottom(bv);
1756 TEXT(bv)->SetSelection(bv);
1759 void InsetText::clearSelection(BufferView * bv)
1761 TEXT(bv)->ClearSelection(bv);