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)))
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) && (TEXT(bv)->width != old_max_width)){
299 old_max_width = TEXT(bv)->width;
300 bv->text->status = LyXText::CHANGED_IN_DRAW;
304 clearInset(pain, baseline, cleared);
310 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
312 if (cleared || (last_drawn_width != insetWidth)) {
314 last_drawn_width = insetWidth;
317 top_baseline = baseline;
318 top_y = baseline - ascent(bv, f);
319 last_width = width(bv, f);
320 last_height = ascent(bv, f) + descent(bv, f);
322 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
323 inset_x = cx(bv) - top_x + drawTextXOffset;
324 inset_y = cy(bv) + drawTextYOffset;
326 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection.set()) {
327 drawFrame(pain, cleared);
332 x += TEXT_TO_INSET_OFFSET;
335 #warning Jürgen, why is this a block of its own? (Lgb)
336 #warning because you told me to define variables only in local context (Jug)!
337 #warning then make it a function/method of its own. (Lgb)
341 Row * row = TEXT(bv)->GetRowNearY(y);
342 int y_offset = baseline - row->ascent_of_text();
343 int ph = pain.paperHeight();
346 while ((row != 0) && ((y+row->height()) <= 0)) {
348 first += row->height();
353 TEXT(bv)->first = first;
357 while ((row != 0) && (yf < ph)) {
358 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
364 } else if (!locked) {
365 if (need_update & CURSOR) {
366 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
367 TEXT(bv)->ClearSelection(bv);
368 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
370 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
373 if (need_update & SELECTION)
374 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
375 else if (need_update & CURSOR) {
376 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
377 TEXT(bv)->ClearSelection(bv);
378 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
380 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
384 TEXT(bv)->refresh_y = 0;
385 TEXT(bv)->status = LyXText::UNCHANGED;
386 if ((need_update != CURSOR_PAR) &&
387 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
388 drawFrame(pain, cleared);
389 else if (need_update & CLEAR_FRAME)
390 clearFrame(pain, cleared);
391 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
392 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
394 } else if (need_update != INIT)
399 void InsetText::drawFrame(Painter & pain, bool cleared) const
401 if (!frame_is_visible || cleared) {
402 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
403 insetWidth - 1, insetAscent + insetDescent - 1,
405 frame_is_visible = true;
410 void InsetText::clearFrame(Painter & pain, bool cleared) const
412 if (frame_is_visible) {
414 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
415 insetWidth - 1, insetAscent + insetDescent - 1,
418 frame_is_visible = false;
423 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
426 int ww = TEXT(bv)->width;
427 TEXT(bv)->BreakParagraph(bv);
428 if (ww != TEXT(bv)->width)
435 owner()->update(bv, font, true);
438 if (the_locking_inset) {
439 inset_x = cx(bv) - top_x + drawTextXOffset;
440 inset_y = cy(bv) + drawTextYOffset;
441 the_locking_inset->update(bv, font, reinit);
444 if (need_update == INIT) {
449 int oldw = insetWidth;
450 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
451 if (oldw != insetWidth) {
454 // update(bv, font, reinit);
457 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
460 TEXT(bv)->UpdateInset(bv, the_locking_inset);
463 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
467 Row * row = TEXT(bv)->GetRowNearY(y_temp);
468 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
469 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
470 TEXT_TO_INSET_OFFSET;
475 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
478 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
480 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
481 need_update |= CURSOR_PAR;
483 // this to not draw a selection when we redraw all of it!
484 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
485 TEXT(bv)->ClearSelection(bv);
489 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
491 TEXT(bv)->FullRebreak(bv);
492 SetUpdateStatus(bv, what);
493 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
494 TEXT(bv)->selection.set())
495 bv->updateInset(this, mark_dirty);
496 bv->owner()->showState();
497 if (old_par != cpar(bv)) {
498 bv->owner()->setLayout(cpar(bv)->GetLayout());
504 string const InsetText::EditMessage() const
506 return _("Opened Text Inset");
510 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
512 // par->SetInsetOwner(this);
513 UpdatableInset::Edit(bv, x, y, button);
515 if (!bv->lockInset(this)) {
516 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
520 the_locking_inset = 0;
521 inset_pos = inset_x = inset_y = 0;
522 inset_boundary = false;
525 int tmp_y = (y < 0)?0:y;
526 if (!checkAndActivateInset(bv, x, tmp_y, button))
527 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
529 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
530 bv->text->FinishUndo();
532 UpdateLocal(bv, CURSOR, false);
534 // If the inset is empty set the language of the current font to the
535 // language to the surronding text (if different).
536 if (par->size() == 0 && !par->next() &&
537 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
539 LyXFont font(LyXFont::ALL_IGNORE);
540 font.setLanguage(bv->getParentLanguage(this));
541 SetFont(bv, font, false);
546 void InsetText::InsetUnlock(BufferView * bv)
548 if (the_locking_inset) {
549 the_locking_inset->InsetUnlock(bv);
550 the_locking_inset = 0;
553 no_selection = false;
555 int code = CURSOR|CLEAR_FRAME;
556 if (TEXT(bv)->selection.set()) {
557 TEXT(bv)->ClearSelection(bv);
560 bv->owner()->setLayout(owner()->getLyXText(bv)
561 ->cursor.par()->GetLayout());
563 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
564 UpdateLocal(bv, code, false);
568 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
570 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
574 if (inset == cpar(bv)->GetInset(cpos(bv))) {
575 lyxerr[Debug::INSETS] << "OK" << endl;
576 the_locking_inset = inset;
577 inset_x = cx(bv) - top_x + drawTextXOffset;
578 inset_y = cy(bv) + drawTextYOffset;
579 inset_pos = cpos(bv);
580 inset_par = cpar(bv);
581 inset_boundary = cboundary(bv);
583 TEXT(bv)->ClearSelection(bv);
584 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
585 TEXT(bv)->UpdateInset(bv, the_locking_inset);
587 UpdateLocal(bv, CURSOR, false);
590 } else if (the_locking_inset && (the_locking_inset == inset)) {
591 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
592 lyxerr[Debug::INSETS] << "OK" << endl;
593 inset_x = cx(bv) - top_x + drawTextXOffset;
594 inset_y = cy(bv) + drawTextYOffset;
596 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
598 } else if (the_locking_inset) {
599 lyxerr[Debug::INSETS] << "MAYBE" << endl;
600 return the_locking_inset->LockInsetInInset(bv, inset);
602 lyxerr[Debug::INSETS] << "NOT OK" << endl;
607 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
610 if (!the_locking_inset)
612 if (the_locking_inset == inset) {
613 the_locking_inset->InsetUnlock(bv);
614 TEXT(bv)->UpdateInset(bv, inset);
615 the_locking_inset = 0;
617 moveRight(bv, false);
618 old_par = 0; // force layout setting
622 UpdateLocal(bv, CURSOR, false);
625 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
629 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
631 if (!the_locking_inset)
633 if (the_locking_inset != inset) {
634 TEXT(bv)->UpdateInset(bv, the_locking_inset);
635 SetUpdateStatus(bv, CURSOR_PAR);
636 return the_locking_inset->UpdateInsetInInset(bv, inset);
638 // UpdateLocal(bv, FULL, false);
639 if (TEXT(bv)->UpdateInset(bv, inset))
640 UpdateLocal(bv, CURSOR_PAR, false);
641 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
642 inset_x = cx(bv) - top_x + drawTextXOffset;
643 inset_y = cy(bv) + drawTextYOffset;
649 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
653 int tmp_x = x - drawTextXOffset;
654 int tmp_y = y + insetAscent - TEXT(bv)->first;
655 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
658 if (the_locking_inset) {
659 if (the_locking_inset == inset) {
660 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
661 no_selection = false;
664 // otherwise unlock the_locking_inset and lock the new inset
665 the_locking_inset->InsetUnlock(bv);
666 inset_x = cx(bv) - top_x + drawTextXOffset;
667 inset_y = cy(bv) + drawTextYOffset;
668 the_locking_inset = static_cast<UpdatableInset*>(inset);
669 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
670 inset->Edit(bv, x - inset_x, y - inset_y, button);
671 if (the_locking_inset)
672 UpdateLocal(bv, CURSOR, false);
673 no_selection = false;
676 // otherwise only unlock the_locking_inset
677 the_locking_inset->InsetUnlock(bv);
678 the_locking_inset = 0;
680 if (bv->theLockingInset()) {
681 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
682 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
683 inset_x = cx(bv) - top_x + drawTextXOffset;
684 inset_y = cy(bv) + drawTextYOffset;
685 inset_pos = cpos(bv);
686 inset_par = cpar(bv);
687 inset_boundary = cboundary(bv);
688 the_locking_inset = uinset;
689 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
691 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
692 if (the_locking_inset)
693 UpdateLocal(bv, CURSOR, false);
694 no_selection = false;
698 if (!inset) { // && (button == 2)) {
699 bool paste_internally = false;
700 if ((button == 2) && TEXT(bv)->selection.set()) {
701 LocalDispatch(bv, LFUN_COPY, "");
702 paste_internally = true;
704 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
706 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
707 UpdateLocal(bv, CURSOR, false);
708 bv->owner()->setLayout(cpar(bv)->GetLayout());
710 // Insert primary selection with middle mouse
711 // if there is a local selection in the current buffer,
714 if (paste_internally)
715 LocalDispatch(bv, LFUN_PASTE, "");
717 LocalDispatch(bv, LFUN_PASTESELECTION,
722 no_selection = false;
726 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
728 UpdatableInset * inset = 0;
730 if (the_locking_inset) {
731 the_locking_inset->InsetButtonRelease(bv,
732 x - inset_x, y - inset_y,
735 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
736 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
737 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
738 inset->InsetButtonRelease(bv,
740 y - inset_y, button);
742 inset_x = cx(bv) - top_x + drawTextXOffset;
743 inset_y = cy(bv) + drawTextYOffset;
744 inset->InsetButtonRelease(bv,
746 y - inset_y, button);
748 x - inset_x, y - inset_y, button);
750 UpdateLocal(bv, CURSOR_PAR, false);
753 no_selection = false;
757 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
761 if (the_locking_inset) {
762 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
767 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
769 TEXT(bv)->SetSelection(bv);
770 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
771 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
772 UpdateLocal(bv, SELECTION, false);
777 void InsetText::InsetKeyPress(XKeyEvent * xke)
779 if (the_locking_inset) {
780 the_locking_inset->InsetKeyPress(xke);
786 UpdatableInset::RESULT
787 InsetText::LocalDispatch(BufferView * bv,
788 kb_action action, string const & arg)
790 no_selection = false;
791 UpdatableInset::RESULT
792 result= UpdatableInset::LocalDispatch(bv, action, arg);
793 if (result != UNDISPATCHED) {
798 if ((action < 0) && arg.empty())
801 if (the_locking_inset) {
802 result = the_locking_inset->LocalDispatch(bv, action, arg);
803 if (result == DISPATCHED_NOUPDATE)
805 else if (result == DISPATCHED) {
806 UpdateLocal(bv, CURSOR_PAR, false);
808 } else if (result == FINISHED) {
809 bool dispatched = false;
811 case LFUN_UNKNOWN_ACTION:
812 case LFUN_BREAKPARAGRAPH:
814 moveRightIntern(bv, false, false);
817 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
818 moveRightIntern(bv, false, false);
822 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
823 moveRightIntern(bv, false, false);
829 the_locking_inset = 0;
837 case LFUN_UNKNOWN_ACTION:
838 if (bv->buffer()->isReadonly()) {
839 // setErrorMessage(N_("Document is read only"));
843 /* Automatically delete the currently selected
844 * text and replace it with what is being
845 * typed in now. Depends on lyxrc settings
846 * "auto_region_delete", which defaults to
849 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
850 bv->text->cursor.par()->previous(),
851 bv->text->cursor.par()->next());
853 if (lyxrc.auto_region_delete) {
854 if (TEXT(bv)->selection.set()) {
855 TEXT(bv)->CutSelection(bv, false);
858 TEXT(bv)->ClearSelection(bv);
859 for (string::size_type i = 0; i < arg.length(); ++i) {
861 if (!math_insert_greek(bv, arg[i])) {
862 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
863 } else if (!the_locking_inset) {
864 (void)moveRight(bv, false);
867 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
871 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
872 UpdateLocal(bv, CURSOR_PAR, true);
873 result=DISPATCHED_NOUPDATE;
875 // --- Cursor Movements -----------------------------------
877 bv->text->FinishUndo();
878 moveRight(bv, false, true);
879 TEXT(bv)->SetSelection(bv);
880 UpdateLocal(bv, SELECTION, false);
883 result = moveRight(bv);
884 bv->text->FinishUndo();
885 UpdateLocal(bv, CURSOR, false);
888 bv->text->FinishUndo();
889 moveLeft(bv, false, true);
890 TEXT(bv)->SetSelection(bv);
891 UpdateLocal(bv, SELECTION, false);
894 bv->text->FinishUndo();
895 result = moveLeft(bv);
896 UpdateLocal(bv, CURSOR, false);
899 bv->text->FinishUndo();
901 TEXT(bv)->SetSelection(bv);
902 UpdateLocal(bv, SELECTION, false);
905 bv->text->FinishUndo();
906 result = moveDown(bv);
907 UpdateLocal(bv, CURSOR, false);
910 bv->text->FinishUndo();
912 TEXT(bv)->SetSelection(bv);
913 UpdateLocal(bv, SELECTION, false);
916 bv->text->FinishUndo();
918 UpdateLocal(bv, CURSOR, false);
921 bv->text->FinishUndo();
922 TEXT(bv)->CursorHome(bv);
923 UpdateLocal(bv, CURSOR, false);
926 TEXT(bv)->CursorEnd(bv);
927 UpdateLocal(bv, CURSOR, false);
930 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
931 bv->text->cursor.par()->previous(),
932 bv->text->cursor.par()->next());
933 if (TEXT(bv)->selection.set())
934 TEXT(bv)->CutSelection(bv);
936 TEXT(bv)->Backspace(bv);
937 UpdateLocal(bv, CURSOR_PAR, true);
940 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
941 bv->text->cursor.par()->previous(),
942 bv->text->cursor.par()->next());
943 if (TEXT(bv)->selection.set())
944 TEXT(bv)->CutSelection(bv);
946 TEXT(bv)->Delete(bv);
947 UpdateLocal(bv, CURSOR_PAR, true);
950 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
951 bv->text->cursor.par()->previous(),
952 bv->text->cursor.par()->next());
953 TEXT(bv)->CutSelection(bv);
954 UpdateLocal(bv, CURSOR_PAR, true);
957 bv->text->FinishUndo();
958 TEXT(bv)->CopySelection(bv);
959 UpdateLocal(bv, CURSOR_PAR, false);
961 case LFUN_PASTESELECTION:
963 string const clip(bv->getClipboard());
967 if (arg == "paragraph") {
968 TEXT(bv)->InsertStringB(bv, clip);
970 TEXT(bv)->InsertStringA(bv, clip);
972 UpdateLocal(bv, CURSOR_PAR, true);
976 if (!autoBreakRows) {
979 if (cap.nrOfParagraphs() > 1) {
980 WriteAlert(_("Impossible operation"),
981 _("Cannot include more than one paragraph!"),
986 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
987 bv->text->cursor.par()->previous(),
988 bv->text->cursor.par()->next());
989 TEXT(bv)->PasteSelection(bv);
990 UpdateLocal(bv, CURSOR_PAR, true);
992 case LFUN_BREAKPARAGRAPH:
995 TEXT(bv)->BreakParagraph(bv, 0);
996 UpdateLocal(bv, FULL, true);
998 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1001 TEXT(bv)->BreakParagraph(bv, 1);
1002 UpdateLocal(bv, FULL, true);
1004 case LFUN_BREAKLINE:
1007 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1008 bv->text->cursor.par()->previous(),
1009 bv->text->cursor.par()->next());
1010 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1011 UpdateLocal(bv, CURSOR_PAR, true);
1014 // do not set layouts on non breakable textinsets
1015 if (autoBreakRows) {
1016 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1018 // Derive layout number from given argument (string)
1019 // and current buffer's textclass (number). */
1020 LyXTextClassList::ClassList::size_type tclass =
1021 bv->buffer()->params.textclass;
1022 std::pair <bool, LyXTextClass::size_type> layout =
1023 textclasslist.NumberOfLayout(tclass, arg);
1025 // If the entry is obsolete, use the new one instead.
1027 string obs = textclasslist.Style(tclass,layout.second).
1030 layout = textclasslist.NumberOfLayout(tclass, obs);
1033 // see if we found the layout number:
1034 if (!layout.first) {
1035 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1036 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1040 if (cur_layout != layout.second) {
1041 cur_layout = layout.second;
1042 TEXT(bv)->SetLayout(bv, layout.second);
1043 bv->owner()->setLayout(cpar(bv)->GetLayout());
1044 UpdateLocal(bv, CURSOR_PAR, true);
1047 // reset the layout box
1048 bv->owner()->setLayout(cpar(bv)->GetLayout());
1051 case LFUN_PARAGRAPH_SPACING:
1052 // This one is absolutely not working. When fiddling with this
1053 // it also seems to me that the paragraphs inside the insettext
1054 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1056 LyXParagraph * par = TEXT(bv)->cursor.par();
1057 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1058 float cur_value = 1.0;
1059 if (cur_spacing == Spacing::Other) {
1060 cur_value = par->params.spacing().getValue();
1063 std::istringstream istr(arg.c_str());
1066 Spacing::Space new_spacing = cur_spacing;
1067 float new_value = cur_value;
1069 lyxerr << "Missing argument to `paragraph-spacing'"
1071 } else if (tmp == "single") {
1072 new_spacing = Spacing::Single;
1073 } else if (tmp == "onehalf") {
1074 new_spacing = Spacing::Onehalf;
1075 } else if (tmp == "double") {
1076 new_spacing = Spacing::Double;
1077 } else if (tmp == "other") {
1078 new_spacing = Spacing::Other;
1081 lyxerr << "new_value = " << tmpval << endl;
1084 } else if (tmp == "default") {
1085 new_spacing = Spacing::Default;
1087 lyxerr << _("Unknown spacing argument: ")
1090 if (cur_spacing != new_spacing || cur_value != new_value) {
1091 par->params.spacing(Spacing(new_spacing, new_value));
1092 //TEXT(bv)->RedoParagraph(owner->view());
1093 UpdateLocal(bv, CURSOR_PAR, true);
1094 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1100 if (!bv->Dispatch(action, arg))
1101 result = UNDISPATCHED;
1105 /// If the action has deleted all text in the inset, we need to change the
1106 // language to the language to the surronding text.
1107 if (par->size() == 0 && !par->next()) {
1108 LyXFont font(LyXFont::ALL_IGNORE);
1109 font.setLanguage(bv->getParentLanguage(this));
1110 SetFont(bv, font, false);
1113 if (result != FINISHED) {
1114 ShowInsetCursor(bv);
1116 bv->unlockInset(this);
1121 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1124 buf->latexParagraphs(os, par, 0, texrow);
1125 return texrow.rows();
1129 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1131 LyXParagraph * p = par;
1132 unsigned int lines = 0;
1135 string const tmp = buf->asciiParagraph(p, linelen);
1136 lines += countChar(tmp, '\n');
1144 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1146 LyXParagraph * p = par;
1147 unsigned int lines = 0;
1152 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1160 void InsetText::Validate(LaTeXFeatures & features) const
1162 LyXParagraph * p = par;
1164 p->validate(features);
1170 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1172 if (textclasslist.Style(buf->params.textclass,
1173 p->GetLayout()).labeltype != LABEL_MANUAL)
1176 return p->BeginningOfMainBody();
1180 void InsetText::GetCursorPos(BufferView * bv,
1181 int & x, int & y) const
1188 unsigned int InsetText::InsetInInsetY()
1190 if (!the_locking_inset)
1193 return (inset_y + the_locking_inset->InsetInInsetY());
1197 void InsetText::ToggleInsetCursor(BufferView * bv)
1199 if (the_locking_inset) {
1200 the_locking_inset->ToggleInsetCursor(bv);
1204 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1206 int const asc = lyxfont::maxAscent(font);
1207 int const desc = lyxfont::maxDescent(font);
1209 if (isCursorVisible())
1210 bv->hideLockedInsetCursor();
1212 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1213 toggleCursorVisible();
1217 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1219 if (the_locking_inset) {
1220 the_locking_inset->ShowInsetCursor(bv);
1223 if (!isCursorVisible()) {
1224 LyXFont const font =
1225 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1227 int const asc = lyxfont::maxAscent(font);
1228 int const desc = lyxfont::maxDescent(font);
1230 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1232 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1233 setCursorVisible(true);
1238 void InsetText::HideInsetCursor(BufferView * bv)
1240 if (isCursorVisible()) {
1241 bv->hideLockedInsetCursor();
1242 setCursorVisible(false);
1244 if (the_locking_inset)
1245 the_locking_inset->HideInsetCursor(bv);
1249 UpdatableInset::RESULT
1250 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1252 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1253 return moveLeftIntern(bv, false, activate_inset, selecting);
1255 return moveRightIntern(bv, false, activate_inset, selecting);
1259 UpdatableInset::RESULT
1260 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1262 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1263 return moveRightIntern(bv, true, activate_inset, selecting);
1265 return moveLeftIntern(bv, true, activate_inset, selecting);
1269 UpdatableInset::RESULT
1270 InsetText::moveRightIntern(BufferView * bv, bool behind,
1271 bool activate_inset, bool selecting)
1273 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1275 if (activate_inset && checkAndActivateInset(bv, behind))
1277 TEXT(bv)->CursorRight(bv);
1279 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1280 return DISPATCHED_NOUPDATE;
1284 UpdatableInset::RESULT
1285 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1286 bool activate_inset, bool selecting)
1288 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1290 TEXT(bv)->CursorLeft(bv);
1292 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1293 if (activate_inset && checkAndActivateInset(bv, behind))
1295 return DISPATCHED_NOUPDATE;
1299 UpdatableInset::RESULT
1300 InsetText::moveUp(BufferView * bv)
1302 if (!crow(bv)->previous())
1304 TEXT(bv)->CursorUp(bv);
1305 return DISPATCHED_NOUPDATE;
1309 UpdatableInset::RESULT
1310 InsetText::moveDown(BufferView * bv)
1312 if (!crow(bv)->next())
1314 TEXT(bv)->CursorDown(bv);
1315 return DISPATCHED_NOUPDATE;
1319 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1321 if (the_locking_inset) {
1322 if (the_locking_inset->InsertInsetAllowed(inset))
1323 return the_locking_inset->InsertInset(bv, inset);
1326 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1327 bv->text->cursor.par()->previous(),
1328 bv->text->cursor.par()->next());
1329 inset->setOwner(this);
1330 HideInsetCursor(bv);
1331 TEXT(bv)->InsertInset(bv, inset);
1333 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1334 (cpar(bv)->GetInset(cpos(bv)) != inset))
1335 TEXT(bv)->CursorLeft(bv);
1337 bv->fitCursor(TEXT(bv));
1338 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1339 ShowInsetCursor(bv);
1344 UpdatableInset * InsetText::GetLockingInset()
1346 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1350 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1354 if (the_locking_inset)
1355 return the_locking_inset->GetFirstLockingInsetOfType(c);
1360 bool InsetText::ShowInsetDialog(BufferView * bv) const
1362 if (the_locking_inset)
1363 return the_locking_inset->ShowInsetDialog(bv);
1368 std::vector<string> const InsetText::getLabelList() const
1370 std::vector<string> label_list;
1372 LyXParagraph * tpar = par;
1374 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1375 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1376 for (; beg != end; ++beg) {
1377 std::vector<string> const l = (*beg)->getLabelList();
1378 label_list.insert(label_list.end(), l.begin(), l.end());
1380 tpar = tpar->next();
1386 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1389 if (the_locking_inset) {
1390 the_locking_inset->SetFont(bv, font, toggleall, selectall);
1393 if (TEXT(bv)->selection.set()) {
1394 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1395 bv->text->cursor.par()->previous(),
1396 bv->text->cursor.par()->next());
1400 TEXT(bv)->SetFont(bv, font, toggleall);
1402 TEXT(bv)->ClearSelection(bv);
1403 bv->fitCursor(TEXT(bv));
1404 if (selectall || TEXT(bv)->selection.set())
1405 UpdateLocal(bv, FULL, true);
1407 UpdateLocal(bv, CURSOR_PAR, true);
1411 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1413 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1417 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1418 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1420 LyXFont const font =
1421 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1423 x = inset->width(bv, font);
1424 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1427 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1429 //inset_x = cx(bv) - top_x + drawTextXOffset;
1430 //inset_y = cy(bv) + drawTextYOffset;
1431 inset->Edit(bv, x, y, 0);
1432 if (!the_locking_inset)
1434 UpdateLocal(bv, CURSOR, false);
1441 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1446 dummyx = x = x - drawTextXOffset;
1447 dummyy = y + insetAscent;
1448 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1455 inset_x = cx(bv) - top_x + drawTextXOffset;
1456 inset_y = cy(bv) + drawTextYOffset;
1457 inset->Edit(bv, x - inset_x, y - inset_y, button);
1458 if (!the_locking_inset)
1460 UpdateLocal(bv, CURSOR, false);
1467 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1469 int w = UpdatableInset::getMaxWidth(bv, inset);
1474 w = w - top_x + owner()->x();
1477 w -= (2 * TEXT_TO_INSET_OFFSET);
1479 // return w - (2*TEXT_TO_INSET_OFFSET);
1483 void InsetText::SetParagraphData(LyXParagraph * p)
1485 // delete all instances of LyXText before deleting the paragraps used
1487 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1488 delete (*cit).second;
1493 LyXParagraph * tmp = par->next();
1498 par = new LyXParagraph(*p);
1499 par->SetInsetOwner(this);
1500 LyXParagraph * np = par;
1503 np->next(new LyXParagraph(*p));
1504 np->next()->previous(np);
1506 np->SetInsetOwner(this);
1513 void InsetText::SetText(string const & data)
1516 LyXFont font(LyXFont::ALL_SANE);
1517 for (unsigned int i=0; i < data.length(); ++i)
1518 par->InsertChar(i, data[i], font);
1522 void InsetText::SetAutoBreakRows(bool flag)
1524 if (flag != autoBreakRows) {
1525 autoBreakRows = flag;
1533 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1535 if (how != drawFrame_) {
1538 UpdateLocal(bv, DRAW_FRAME, false);
1543 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1545 if (frame_color != col) {
1548 UpdateLocal(bv, DRAW_FRAME, false);
1553 int InsetText::cx(BufferView * bv) const
1555 LyXText * text = TEXT(bv);
1556 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1557 if (the_locking_inset) {
1558 LyXFont font = text->GetFont(bv->buffer(),
1560 text->cursor.pos());
1561 if (font.isVisibleRightToLeft())
1562 x -= the_locking_inset->width(bv, font);
1568 int InsetText::cy(BufferView * bv) const
1571 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1575 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1577 return TEXT(bv)->cursor.pos();
1581 LyXParagraph * InsetText::cpar(BufferView * bv) const
1583 return TEXT(bv)->cursor.par();
1587 bool InsetText::cboundary(BufferView * bv) const
1589 return TEXT(bv)->cursor.boundary();
1593 Row * InsetText::crow(BufferView * bv) const
1595 return TEXT(bv)->cursor.row();
1599 LyXText * InsetText::getLyXText(BufferView const * lbv,
1600 bool const recursive) const
1602 // Super UGLY! (Lgb)
1603 BufferView * bv = const_cast<BufferView *>(lbv);
1605 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1606 if (recursive && the_locking_inset)
1607 return the_locking_inset->getLyXText(bv);
1610 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1613 if (the_locking_inset) {
1614 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1616 return the_locking_inset->getLyXText(bv);
1622 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1624 if ((cache.find(bv) == cache.end()) || !cache[bv])
1629 /// then remove all LyXText in text-insets
1630 LyXParagraph * p = par;
1631 for (; p; p = p->next()) {
1632 p->deleteInsetsLyXText(bv);
1638 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1640 if (!par->next() && !par->size()) // no data, resize not neccessary!
1642 // one endless line, resize normally not necessary
1643 if (!force && getMaxWidth(bv, this) < 0)
1645 if ((cache.find(bv) == cache.end()) || !cache[bv])
1648 LyXParagraph * lpar = 0;
1649 LyXParagraph * selstartpar = 0;
1650 LyXParagraph * selendpar = 0;
1651 LyXParagraph::size_type pos = 0;
1652 LyXParagraph::size_type selstartpos = 0;
1653 LyXParagraph::size_type selendpos = 0;
1654 bool boundary = false;
1655 bool selstartboundary = false;
1656 bool selendboundary = false;
1657 bool selection = false;
1658 bool mark_set = false;
1660 // ProhibitInput(bv);
1663 lpar = TEXT(bv)->cursor.par();
1664 pos = TEXT(bv)->cursor.pos();
1665 boundary = TEXT(bv)->cursor.boundary();
1666 selstartpar = TEXT(bv)->selection.start.par();
1667 selstartpos = TEXT(bv)->selection.start.pos();
1668 selstartboundary = TEXT(bv)->selection.start.boundary();
1669 selendpar = TEXT(bv)->selection.end.par();
1670 selendpos = TEXT(bv)->selection.end.pos();
1671 selendboundary = TEXT(bv)->selection.end.boundary();
1672 selection = TEXT(bv)->selection.set();
1673 mark_set = TEXT(bv)->selection.mark();
1675 deleteLyXText(bv, (the_locking_inset == 0) || force);
1678 TEXT(bv)->selection.set(true);
1679 /* at this point just to avoid the Delete-Empty-Paragraph
1680 * Mechanism when setting the cursor */
1681 TEXT(bv)->selection.mark(mark_set);
1683 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1684 true, selstartboundary);
1685 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1686 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1687 true, selendboundary);
1688 TEXT(bv)->SetSelection(bv);
1689 TEXT(bv)->SetCursor(bv, lpar, pos);
1691 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1692 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1693 TEXT(bv)->selection.set(false);
1697 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1698 // this will scroll the screen such that the cursor becomes visible
1699 bv->updateScrollbar();
1701 if (the_locking_inset) {
1702 /// then resize all LyXText in text-insets
1703 inset_x = cx(bv) - top_x + drawTextXOffset;
1704 inset_y = cy(bv) + drawTextYOffset;
1705 for (LyXParagraph * p = par; p; p = p->next()) {
1706 p->resizeInsetsLyXText(bv);
1713 void InsetText::removeNewlines()
1715 for (LyXParagraph * p = par; p; p = p->next()) {
1716 for (int i = 0; i < p->size(); ++i) {
1717 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1724 bool InsetText::nodraw() const
1726 if (the_locking_inset)
1727 return the_locking_inset->nodraw();
1728 return UpdatableInset::nodraw();
1732 int InsetText::scroll(bool recursive) const
1734 int sx = UpdatableInset::scroll(false);
1736 if (recursive && the_locking_inset)
1737 sx += the_locking_inset->scroll(recursive);
1743 bool InsetText::doClearArea() const
1745 return !locked || (need_update & (FULL|INIT));
1748 void InsetText::selectAll(BufferView * bv)
1750 TEXT(bv)->CursorTop(bv);
1751 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1752 TEXT(bv)->CursorBottom(bv);
1753 TEXT(bv)->SetSelection(bv);
1756 void InsetText::clearSelection(BufferView * bv)
1758 TEXT(bv)->ClearSelection(bv);
1761 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1764 int h = insetAscent + insetDescent;
1765 int ty = baseline - insetAscent;
1771 if ((ty + h) > pain.paperHeight())
1772 h = pain.paperHeight();
1773 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1774 w = pain.paperWidth();
1775 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);