2 /* This file is part of
3 * ======================================================
5 * LyX, The Document Processor
7 * Copyright 1998-2000 The LyX Team.
9 * ======================================================
20 #pragma implementation
23 #include "insettext.h"
24 #include "lyxparagraph.h"
28 #include "commandtags.h"
31 #include "BufferView.h"
33 #include "LaTeXFeatures.h"
35 #include "lyx_gui_misc.h"
37 #include "lyxcursor.h"
38 #include "CutAndPaste.h"
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, 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);
181 // delete all instances of LyXText before deleting the paragraps used
183 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
184 delete (*cit).second;
189 LyXParagraph * tmp = par->next();
194 par = new LyXParagraph;
201 token = lex.GetString();
204 if (token == "\\end_inset")
206 if (const_cast<Buffer*>(buf)->
207 parseSingleLyXformat2Token(lex, par, return_par,
208 token, pos, depth, font)) {
209 // the_end read this should NEVER happen
210 lex.printError("\\the_end read in inset! Error in document!");
218 return_par->SetInsetOwner(this);
219 return_par = return_par->next();
222 if (token != "\\end_inset") {
223 lex.printError("Missing \\end_inset at this point. "
230 int InsetText::ascent(BufferView * bv, LyXFont const &) const
233 Row * row = TEXT(bv)->GetRowNearY(y_temp);
234 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
239 int InsetText::descent(BufferView * bv, LyXFont const &) const
242 Row * row = TEXT(bv)->GetRowNearY(y_temp);
243 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
244 TEXT_TO_INSET_OFFSET;
249 int InsetText::width(BufferView * bv, LyXFont const &) const
251 insetWidth = max(textWidth(bv),
252 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
257 int InsetText::textWidth(BufferView * bv) const
259 int const w = getMaxWidth(bv, this);
264 void InsetText::draw(BufferView * bv, LyXFont const & f,
265 int baseline, float & x, bool cleared) const
270 Painter & pain = bv->painter();
272 // no draw is necessary !!!
273 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
275 top_baseline = baseline;
277 if (!cleared && (need_update & CLEAR_FRAME))
278 clearFrame(pain, cleared);
285 UpdatableInset::draw(bv, f, baseline, x, cleared);
288 x += static_cast<float>(scroll());
290 // update insetWidth and insetHeight with dummy calls
292 (void)descent(bv, f);
295 // if top_x differs we have a rule down and we don't have to clear anything
296 if (!cleared && (top_x == int(x)) &&
297 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
298 (last_drawn_width!=insetWidth)))
301 int h = insetAscent + insetDescent;
302 int ty = baseline - insetAscent;
308 if ((ty + h) > pain.paperHeight())
309 h = pain.paperHeight();
310 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
311 w = pain.paperWidth();
312 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
316 if (!cleared && (need_update == NONE))
319 if (top_x != int(x)) {
322 bv->text->status = LyXText::CHANGED_IN_DRAW;
326 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
328 if (cleared || (last_drawn_width != insetWidth)) {
330 last_drawn_width = insetWidth;
333 top_baseline = baseline;
334 top_y = baseline - ascent(bv, f);
335 last_width = width(bv, f);
336 last_height = ascent(bv, f) + descent(bv, f);
338 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
339 inset_x = cx(bv) - top_x + drawTextXOffset;
340 inset_y = cy(bv) + drawTextYOffset;
342 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
343 drawFrame(pain, cleared);
348 x += TEXT_TO_INSET_OFFSET;
351 #warning Jürgen, why is this a block of its own? (Lgb)
352 #warning because you told me to define variables only in local context (Jug)!
353 #warning then make it a function/method of its own. (Lgb)
357 Row * row = TEXT(bv)->GetRowNearY(y);
358 int y_offset = baseline - row->ascent_of_text();
359 int ph = pain.paperHeight();
362 while ((row != 0) && ((y+row->height()) <= 0)) {
364 first += row->height();
369 TEXT(bv)->first = first;
373 while ((row != 0) && (yf < ph)) {
374 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
380 } else if (!locked) {
381 if (need_update & CURSOR) {
382 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
383 TEXT(bv)->ClearSelection(bv);
384 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
386 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
389 if (need_update & SELECTION)
390 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
391 else if (need_update & CURSOR) {
392 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
393 TEXT(bv)->ClearSelection(bv);
394 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
396 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
400 TEXT(bv)->refresh_y = 0;
401 TEXT(bv)->status = LyXText::UNCHANGED;
402 if ((need_update != CURSOR_PAR) &&
403 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
404 drawFrame(pain, cleared);
405 else if (need_update & CLEAR_FRAME)
406 clearFrame(pain, cleared);
407 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
408 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
410 } else if (need_update != INIT)
415 void InsetText::drawFrame(Painter & pain, bool cleared) const
417 if (!frame_is_visible || cleared) {
418 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
419 insetWidth - 1, insetAscent + insetDescent - 1,
421 frame_is_visible = true;
426 void InsetText::clearFrame(Painter & pain, bool cleared) const
428 if (frame_is_visible) {
430 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
431 insetWidth - 1, insetAscent + insetDescent - 1,
434 frame_is_visible = false;
439 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
445 owner()->update(bv, font, true);
448 if (the_locking_inset) {
449 inset_x = cx(bv) - top_x + drawTextXOffset;
450 inset_y = cy(bv) + drawTextYOffset;
451 the_locking_inset->update(bv, font, reinit);
454 if (need_update == INIT) {
459 int oldw = insetWidth;
460 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
461 if (oldw != insetWidth) {
464 // update(bv, font, reinit);
467 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
470 TEXT(bv)->UpdateInset(bv, the_locking_inset);
473 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
477 Row * row = TEXT(bv)->GetRowNearY(y_temp);
478 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
479 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
480 TEXT_TO_INSET_OFFSET;
485 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
488 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
490 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
491 need_update |= CURSOR_PAR;
493 // this to not draw a selection when we redraw all of it!
494 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
495 TEXT(bv)->ClearSelection(bv);
499 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
501 TEXT(bv)->FullRebreak(bv);
502 SetUpdateStatus(bv, what);
503 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
505 bv->updateInset(this, mark_dirty);
506 bv->owner()->showState();
507 if (old_par != cpar(bv)) {
508 bv->owner()->setLayout(cpar(bv)->GetLayout());
514 string const InsetText::EditMessage() const
516 return _("Opened Text Inset");
520 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
522 // par->SetInsetOwner(this);
523 UpdatableInset::Edit(bv, x, y, button);
525 if (!bv->lockInset(this)) {
526 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
530 the_locking_inset = 0;
531 inset_pos = inset_x = inset_y = 0;
532 inset_boundary = false;
535 int tmp_y = (y < 0)?0:y;
536 if (!checkAndActivateInset(bv, x, tmp_y, button))
537 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
539 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
540 bv->text->FinishUndo();
542 UpdateLocal(bv, CURSOR, false);
544 // If the inset is empty set the language of the current font to the
545 // language to the surronding text.
546 if (par->size() == 0 && !par->next()) {
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 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
565 bv->owner()->setLayout(owner()->getLyXText(bv)
566 ->cursor.par()->GetLayout());
568 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
572 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
574 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
578 if (inset == cpar(bv)->GetInset(cpos(bv))) {
579 lyxerr[Debug::INSETS] << "OK" << endl;
580 the_locking_inset = inset;
581 inset_x = cx(bv) - top_x + drawTextXOffset;
582 inset_y = cy(bv) + drawTextYOffset;
583 inset_pos = cpos(bv);
584 inset_par = cpar(bv);
585 inset_boundary = cboundary(bv);
587 TEXT(bv)->ClearSelection(bv);
588 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
589 TEXT(bv)->UpdateInset(bv, the_locking_inset);
591 UpdateLocal(bv, CURSOR, false);
594 } else if (the_locking_inset && (the_locking_inset == inset)) {
595 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
596 lyxerr[Debug::INSETS] << "OK" << endl;
597 inset_x = cx(bv) - top_x + drawTextXOffset;
598 inset_y = cy(bv) + drawTextYOffset;
600 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
602 } else if (the_locking_inset) {
603 lyxerr[Debug::INSETS] << "MAYBE" << endl;
604 return the_locking_inset->LockInsetInInset(bv, inset);
606 lyxerr[Debug::INSETS] << "NOT OK" << endl;
611 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
614 if (!the_locking_inset)
616 if (the_locking_inset == inset) {
617 the_locking_inset->InsetUnlock(bv);
618 TEXT(bv)->UpdateInset(bv, inset);
619 the_locking_inset = 0;
621 moveRight(bv, false);
622 old_par = 0; // force layout setting
626 UpdateLocal(bv, CURSOR, false);
629 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
633 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
635 if (!the_locking_inset)
637 if (the_locking_inset != inset) {
638 TEXT(bv)->UpdateInset(bv, the_locking_inset);
639 SetUpdateStatus(bv, CURSOR_PAR);
640 return the_locking_inset->UpdateInsetInInset(bv, inset);
642 // UpdateLocal(bv, FULL, false);
643 if (TEXT(bv)->UpdateInset(bv, inset))
644 UpdateLocal(bv, CURSOR_PAR, false);
645 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
646 inset_x = cx(bv) - top_x + drawTextXOffset;
647 inset_y = cy(bv) + drawTextYOffset;
653 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
657 int tmp_x = x - drawTextXOffset;
658 int tmp_y = y + insetAscent - TEXT(bv)->first;
659 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
662 if (the_locking_inset) {
663 if (the_locking_inset == inset) {
664 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
665 no_selection = false;
668 // otherwise unlock the_locking_inset and lock the new inset
669 the_locking_inset->InsetUnlock(bv);
670 inset_x = cx(bv) - top_x + drawTextXOffset;
671 inset_y = cy(bv) + drawTextYOffset;
672 the_locking_inset = static_cast<UpdatableInset*>(inset);
673 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
674 inset->Edit(bv, x - inset_x, y - inset_y, button);
675 if (the_locking_inset)
676 UpdateLocal(bv, CURSOR, false);
677 no_selection = false;
680 // otherwise only unlock the_locking_inset
681 the_locking_inset->InsetUnlock(bv);
682 the_locking_inset = 0;
684 if (bv->theLockingInset()) {
685 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
686 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
687 inset_x = cx(bv) - top_x + drawTextXOffset;
688 inset_y = cy(bv) + drawTextYOffset;
689 inset_pos = cpos(bv);
690 inset_par = cpar(bv);
691 inset_boundary = cboundary(bv);
692 the_locking_inset = uinset;
693 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
695 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
696 if (the_locking_inset)
697 UpdateLocal(bv, CURSOR, false);
698 no_selection = false;
702 if (!inset) { // && (button == 2)) {
703 bool paste_internally = false;
704 if ((button == 2) && TEXT(bv)->selection) {
705 LocalDispatch(bv, LFUN_COPY, "");
706 paste_internally = true;
708 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
710 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
711 UpdateLocal(bv, CURSOR, false);
712 bv->owner()->setLayout(cpar(bv)->GetLayout());
714 // Insert primary selection with middle mouse
715 // if there is a local selection in the current buffer,
718 if (paste_internally)
719 LocalDispatch(bv, LFUN_PASTE, "");
721 LocalDispatch(bv, LFUN_PASTESELECTION,
726 no_selection = false;
730 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
732 UpdatableInset * inset = 0;
734 if (the_locking_inset) {
735 the_locking_inset->InsetButtonRelease(bv,
736 x - inset_x, y - inset_y,
739 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
740 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
741 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
742 inset->InsetButtonRelease(bv,
744 y - inset_y, button);
746 inset_x = cx(bv) - top_x + drawTextXOffset;
747 inset_y = cy(bv) + drawTextYOffset;
748 inset->InsetButtonRelease(bv,
750 y - inset_y, button);
752 x - inset_x, y - inset_y, button);
754 UpdateLocal(bv, CURSOR_PAR, false);
757 no_selection = false;
761 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
765 if (the_locking_inset) {
766 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
771 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
773 TEXT(bv)->SetSelection(bv);
774 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
775 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
776 UpdateLocal(bv, SELECTION, false);
781 void InsetText::InsetKeyPress(XKeyEvent * xke)
783 if (the_locking_inset) {
784 the_locking_inset->InsetKeyPress(xke);
790 UpdatableInset::RESULT
791 InsetText::LocalDispatch(BufferView * bv,
792 kb_action action, string const & arg)
794 no_selection = false;
795 UpdatableInset::RESULT
796 result= UpdatableInset::LocalDispatch(bv, action, arg);
797 if (result != UNDISPATCHED) {
802 if ((action < 0) && arg.empty())
805 if (the_locking_inset) {
806 result = the_locking_inset->LocalDispatch(bv, action, arg);
807 if (result == DISPATCHED_NOUPDATE)
809 else if (result == DISPATCHED) {
810 UpdateLocal(bv, CURSOR_PAR, false);
812 } else if (result == FINISHED) {
813 bool dispatched = false;
815 case LFUN_UNKNOWN_ACTION:
816 case LFUN_BREAKPARAGRAPH:
818 moveRightIntern(bv, false, false);
821 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
822 moveRightIntern(bv, false, false);
826 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
827 moveRightIntern(bv, false, false);
833 the_locking_inset = 0;
841 case LFUN_UNKNOWN_ACTION:
842 if (bv->buffer()->isReadonly()) {
843 // setErrorMessage(N_("Document is read only"));
847 /* Automatically delete the currently selected
848 * text and replace it with what is being
849 * typed in now. Depends on lyxrc settings
850 * "auto_region_delete", which defaults to
853 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
854 bv->text->cursor.par()->previous(),
855 bv->text->cursor.par()->next());
857 if (lyxrc.auto_region_delete) {
858 if (TEXT(bv)->selection){
859 TEXT(bv)->CutSelection(bv, false);
862 TEXT(bv)->ClearSelection(bv);
863 for (string::size_type i = 0; i < arg.length(); ++i) {
865 if (!math_insert_greek(bv, arg[i])) {
866 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
867 } else if (!the_locking_inset) {
868 (void)moveRight(bv, false);
871 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
875 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
876 UpdateLocal(bv, CURSOR_PAR, true);
877 result=DISPATCHED_NOUPDATE;
879 // --- Cursor Movements -----------------------------------
881 bv->text->FinishUndo();
882 moveRight(bv, false, true);
883 TEXT(bv)->SetSelection(bv);
884 UpdateLocal(bv, SELECTION, false);
887 result = moveRight(bv);
888 bv->text->FinishUndo();
889 UpdateLocal(bv, CURSOR, false);
892 bv->text->FinishUndo();
893 moveLeft(bv, false, true);
894 TEXT(bv)->SetSelection(bv);
895 UpdateLocal(bv, SELECTION, false);
898 bv->text->FinishUndo();
899 result = moveLeft(bv);
900 UpdateLocal(bv, CURSOR, false);
903 bv->text->FinishUndo();
905 TEXT(bv)->SetSelection(bv);
906 UpdateLocal(bv, SELECTION, false);
909 bv->text->FinishUndo();
910 result = moveDown(bv);
911 UpdateLocal(bv, CURSOR, false);
914 bv->text->FinishUndo();
916 TEXT(bv)->SetSelection(bv);
917 UpdateLocal(bv, SELECTION, false);
920 bv->text->FinishUndo();
922 UpdateLocal(bv, CURSOR, false);
925 bv->text->FinishUndo();
926 TEXT(bv)->CursorHome(bv);
927 UpdateLocal(bv, CURSOR, false);
930 TEXT(bv)->CursorEnd(bv);
931 UpdateLocal(bv, CURSOR, false);
934 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
935 bv->text->cursor.par()->previous(),
936 bv->text->cursor.par()->next());
937 if (TEXT(bv)->selection)
938 TEXT(bv)->CutSelection(bv);
940 TEXT(bv)->Backspace(bv);
941 UpdateLocal(bv, CURSOR_PAR, true);
944 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
945 bv->text->cursor.par()->previous(),
946 bv->text->cursor.par()->next());
947 if (TEXT(bv)->selection)
948 TEXT(bv)->CutSelection(bv);
950 TEXT(bv)->Delete(bv);
951 UpdateLocal(bv, CURSOR_PAR, true);
954 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
955 bv->text->cursor.par()->previous(),
956 bv->text->cursor.par()->next());
957 TEXT(bv)->CutSelection(bv);
958 UpdateLocal(bv, CURSOR_PAR, true);
961 bv->text->FinishUndo();
962 TEXT(bv)->CopySelection(bv);
963 UpdateLocal(bv, CURSOR_PAR, false);
965 case LFUN_PASTESELECTION:
967 string clip(bv->getClipboard());
971 if (arg == "paragraph") {
972 TEXT(bv)->InsertStringB(bv, clip);
974 TEXT(bv)->InsertStringA(bv, clip);
976 UpdateLocal(bv, CURSOR_PAR, true);
980 if (!autoBreakRows) {
983 if (cap.nrOfParagraphs() > 1) {
984 WriteAlert(_("Impossible operation"),
985 _("Cannot include more than one paragraph!"),
990 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
991 bv->text->cursor.par()->previous(),
992 bv->text->cursor.par()->next());
993 TEXT(bv)->PasteSelection(bv);
994 UpdateLocal(bv, CURSOR_PAR, true);
996 case LFUN_BREAKPARAGRAPH:
999 TEXT(bv)->BreakParagraph(bv, 0);
1000 UpdateLocal(bv, FULL, true);
1002 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1005 TEXT(bv)->BreakParagraph(bv, 1);
1006 UpdateLocal(bv, FULL, true);
1008 case LFUN_BREAKLINE:
1011 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1012 bv->text->cursor.par()->previous(),
1013 bv->text->cursor.par()->next());
1014 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1015 UpdateLocal(bv, CURSOR_PAR, true);
1018 // do not set layouts on non breakable textinsets
1019 if (autoBreakRows) {
1020 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1022 // Derive layout number from given argument (string)
1023 // and current buffer's textclass (number). */
1024 LyXTextClassList::ClassList::size_type tclass =
1025 bv->buffer()->params.textclass;
1026 std::pair <bool, LyXTextClass::size_type> layout =
1027 textclasslist.NumberOfLayout(tclass, arg);
1029 // If the entry is obsolete, use the new one instead.
1031 string obs = textclasslist.Style(tclass,layout.second).
1034 layout = textclasslist.NumberOfLayout(tclass, obs);
1037 // see if we found the layout number:
1038 if (!layout.first) {
1039 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1040 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1044 if (cur_layout != layout.second) {
1045 cur_layout = layout.second;
1046 TEXT(bv)->SetLayout(bv, layout.second);
1047 bv->owner()->setLayout(cpar(bv)->GetLayout());
1048 UpdateLocal(bv, CURSOR_PAR, true);
1051 // reset the layout box
1052 bv->owner()->setLayout(cpar(bv)->GetLayout());
1055 case LFUN_PARAGRAPH_SPACING:
1056 // This one is absolutely not working. When fiddling with this
1057 // it also seems to me that the paragraphs inside the insettext
1058 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1060 LyXParagraph * par = TEXT(bv)->cursor.par();
1061 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1062 float cur_value = 1.0;
1063 if (cur_spacing == Spacing::Other) {
1064 cur_value = par->params.spacing().getValue();
1067 std::istringstream istr(arg.c_str());
1070 Spacing::Space new_spacing = cur_spacing;
1071 float new_value = cur_value;
1073 lyxerr << "Missing argument to `paragraph-spacing'"
1075 } else if (tmp == "single") {
1076 new_spacing = Spacing::Single;
1077 } else if (tmp == "onehalf") {
1078 new_spacing = Spacing::Onehalf;
1079 } else if (tmp == "double") {
1080 new_spacing = Spacing::Double;
1081 } else if (tmp == "other") {
1082 new_spacing = Spacing::Other;
1085 lyxerr << "new_value = " << tmpval << endl;
1088 } else if (tmp == "default") {
1089 new_spacing = Spacing::Default;
1091 lyxerr << _("Unknown spacing argument: ")
1094 if (cur_spacing != new_spacing || cur_value != new_value) {
1095 par->params.spacing(Spacing(new_spacing, new_value));
1096 //TEXT(bv)->RedoParagraph(owner->view());
1097 UpdateLocal(bv, CURSOR_PAR, true);
1098 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1104 if (!bv->Dispatch(action, arg))
1105 result = UNDISPATCHED;
1109 /// If the action has deleted all text in the inset, we need to change the
1110 // language to the language to the surronding text.
1111 if (par->size() == 0 && !par->next()) {
1112 LyXFont font(LyXFont::ALL_IGNORE);
1113 font.setLanguage(bv->getParentLanguage(this));
1114 SetFont(bv, font, false);
1117 if (result != FINISHED) {
1118 ShowInsetCursor(bv);
1120 bv->unlockInset(this);
1125 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1128 buf->latexParagraphs(os, par, 0, texrow);
1129 return texrow.rows();
1133 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1135 LyXParagraph * p = par;
1136 unsigned int lines = 0;
1140 tmp = buf->asciiParagraph(p, linelen);
1141 lines += countChar(tmp, '\n');
1149 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1151 LyXParagraph * p = par;
1152 unsigned int lines = 0;
1157 buf->SimpleDocBookOnePar(os,tmp,p,desc,0);
1165 void InsetText::Validate(LaTeXFeatures & features) const
1167 LyXParagraph * p = par;
1169 p->validate(features);
1175 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1177 if (textclasslist.Style(buf->params.textclass,
1178 p->GetLayout()).labeltype != LABEL_MANUAL)
1181 return p->BeginningOfMainBody();
1185 void InsetText::GetCursorPos(BufferView * bv,
1186 int & x, int & y) const
1193 unsigned int InsetText::InsetInInsetY()
1195 if (!the_locking_inset)
1198 return (inset_y + the_locking_inset->InsetInInsetY());
1202 void InsetText::ToggleInsetCursor(BufferView * bv)
1204 if (the_locking_inset) {
1205 the_locking_inset->ToggleInsetCursor(bv);
1209 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1211 int const asc = lyxfont::maxAscent(font);
1212 int const desc = lyxfont::maxDescent(font);
1214 if (isCursorVisible())
1215 bv->hideLockedInsetCursor();
1217 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1218 toggleCursorVisible();
1222 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1224 if (the_locking_inset) {
1225 the_locking_inset->ShowInsetCursor(bv);
1228 if (!isCursorVisible()) {
1229 LyXFont const font =
1230 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1232 int const asc = lyxfont::maxAscent(font);
1233 int const desc = lyxfont::maxDescent(font);
1235 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1237 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1238 setCursorVisible(true);
1243 void InsetText::HideInsetCursor(BufferView * bv)
1245 if (isCursorVisible()) {
1246 bv->hideLockedInsetCursor();
1247 setCursorVisible(false);
1249 if (the_locking_inset)
1250 the_locking_inset->HideInsetCursor(bv);
1254 UpdatableInset::RESULT
1255 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1257 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1258 return moveLeftIntern(bv, false, activate_inset, selecting);
1260 return moveRightIntern(bv, false, activate_inset, selecting);
1263 UpdatableInset::RESULT
1264 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1266 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1267 return moveRightIntern(bv, true, activate_inset, selecting);
1269 return moveLeftIntern(bv, true, activate_inset, selecting);
1273 UpdatableInset::RESULT
1274 InsetText::moveRightIntern(BufferView * bv, bool behind,
1275 bool activate_inset, bool selecting)
1277 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1279 if (activate_inset && checkAndActivateInset(bv, behind))
1281 TEXT(bv)->CursorRight(bv);
1283 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1284 return DISPATCHED_NOUPDATE;
1288 UpdatableInset::RESULT
1289 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1290 bool activate_inset, bool selecting)
1292 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1294 TEXT(bv)->CursorLeft(bv);
1296 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1297 if (activate_inset && checkAndActivateInset(bv, behind))
1299 return DISPATCHED_NOUPDATE;
1303 UpdatableInset::RESULT
1304 InsetText::moveUp(BufferView * bv)
1306 if (!crow(bv)->previous())
1308 TEXT(bv)->CursorUp(bv);
1309 return DISPATCHED_NOUPDATE;
1313 UpdatableInset::RESULT
1314 InsetText::moveDown(BufferView * bv)
1316 if (!crow(bv)->next())
1318 TEXT(bv)->CursorDown(bv);
1319 return DISPATCHED_NOUPDATE;
1323 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1325 if (the_locking_inset) {
1326 if (the_locking_inset->InsertInsetAllowed(inset))
1327 return the_locking_inset->InsertInset(bv, inset);
1330 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1331 bv->text->cursor.par()->previous(),
1332 bv->text->cursor.par()->next());
1333 inset->setOwner(this);
1334 HideInsetCursor(bv);
1335 TEXT(bv)->InsertInset(bv, inset);
1337 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1338 (cpar(bv)->GetInset(cpos(bv)) != inset))
1339 TEXT(bv)->CursorLeft(bv);
1341 bv->fitCursor(TEXT(bv));
1342 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1343 ShowInsetCursor(bv);
1348 UpdatableInset * InsetText::GetLockingInset()
1350 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1354 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1358 if (the_locking_inset)
1359 return the_locking_inset->GetFirstLockingInsetOfType(c);
1364 bool InsetText::ShowInsetDialog(BufferView * bv) const
1366 if (the_locking_inset)
1367 return the_locking_inset->ShowInsetDialog(bv);
1372 std::vector<string> const InsetText::getLabelList() const
1374 std::vector<string> label_list;
1376 LyXParagraph * tpar = par;
1378 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1379 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1380 for (; beg != end; ++beg) {
1381 std::vector<string> const l = (*beg)->getLabelList();
1382 label_list.insert(label_list.end(), l.begin(), l.end());
1384 tpar = tpar->next();
1390 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1392 if (TEXT(bv)->selection) {
1393 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1394 bv->text->cursor.par()->previous(),
1395 bv->text->cursor.par()->next());
1397 TEXT(bv)->SetFont(bv, font, toggleall);
1398 bv->fitCursor(TEXT(bv));
1399 UpdateLocal(bv, CURSOR_PAR, true);
1403 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1405 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1409 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1410 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1412 LyXFont const font =
1413 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1415 x = inset->width(bv, font);
1416 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1419 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1421 //inset_x = cx(bv) - top_x + drawTextXOffset;
1422 //inset_y = cy(bv) + drawTextYOffset;
1423 inset->Edit(bv, x, y, 0);
1424 if (!the_locking_inset)
1426 UpdateLocal(bv, CURSOR, false);
1433 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1438 dummyx = x = x - drawTextXOffset;
1439 dummyy = y + insetAscent;
1440 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1447 inset_x = cx(bv) - top_x + drawTextXOffset;
1448 inset_y = cy(bv) + drawTextYOffset;
1449 inset->Edit(bv, x - inset_x, y - inset_y, button);
1450 if (!the_locking_inset)
1452 UpdateLocal(bv, CURSOR, false);
1459 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1461 int w = UpdatableInset::getMaxWidth(bv, inset);
1466 w = w - top_x + owner()->x();
1469 w -= (2 * TEXT_TO_INSET_OFFSET);
1471 // return w - (2*TEXT_TO_INSET_OFFSET);
1475 void InsetText::SetParagraphData(LyXParagraph * p)
1477 // delete all instances of LyXText before deleting the paragraps used
1479 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1480 delete (*cit).second;
1485 LyXParagraph * tmp = par->next();
1493 par = new LyXParagraph(*p);
1495 par->SetInsetOwner(this);
1496 LyXParagraph * np = par;
1500 np->next(p->Clone());
1502 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();
1586 bool InsetText::cboundary(BufferView * bv) const
1588 return TEXT(bv)->cursor.boundary();
1592 Row * InsetText::crow(BufferView * bv) const
1594 return TEXT(bv)->cursor.row();
1598 LyXText * InsetText::getLyXText(BufferView const * lbv,
1599 bool const recursive) const
1601 // Super UGLY! (Lgb)
1602 BufferView * bv = const_cast<BufferView *>(lbv);
1604 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1605 if (recursive && the_locking_inset)
1606 return the_locking_inset->getLyXText(bv);
1609 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1612 if (the_locking_inset) {
1613 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1615 return the_locking_inset->getLyXText(bv);
1621 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1623 if ((cache.find(bv) == cache.end()) || !cache[bv])
1628 /// then remove all LyXText in text-insets
1629 LyXParagraph * p = par;
1630 for (; p; p = p->next()) {
1631 p->deleteInsetsLyXText(bv);
1637 void InsetText::resizeLyXText(BufferView * bv) const
1639 if (!par->next() && !par->size()) // resize not neccessary!
1641 if ((cache.find(bv) == cache.end()) || !cache[bv])
1644 LyXParagraph * lpar = 0;
1645 LyXParagraph * selstartpar = 0;
1646 LyXParagraph * selendpar = 0;
1647 LyXParagraph::size_type pos = 0;
1648 LyXParagraph::size_type selstartpos = 0;
1649 LyXParagraph::size_type selendpos = 0;
1650 bool boundary = false;
1651 bool selstartboundary = false;
1652 bool selendboundary = false;
1656 // ProhibitInput(bv);
1659 lpar = TEXT(bv)->cursor.par();
1660 pos = TEXT(bv)->cursor.pos();
1661 boundary = TEXT(bv)->cursor.boundary();
1662 selstartpar = TEXT(bv)->sel_start_cursor.par();
1663 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1664 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1665 selendpar = TEXT(bv)->sel_end_cursor.par();
1666 selendpos = TEXT(bv)->sel_end_cursor.pos();
1667 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1668 selection = TEXT(bv)->selection;
1669 mark_set = TEXT(bv)->mark_set;
1671 deleteLyXText(bv, (the_locking_inset == 0));
1674 TEXT(bv)->selection = true;
1675 /* at this point just to avoid the Delete-Empty-Paragraph
1676 * Mechanism when setting the cursor */
1677 TEXT(bv)->mark_set = mark_set;
1679 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1680 true, selstartboundary);
1681 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1682 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1683 true, selendboundary);
1684 TEXT(bv)->SetSelection(bv);
1685 TEXT(bv)->SetCursor(bv, lpar, pos);
1687 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1688 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1689 TEXT(bv)->selection = false;
1693 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1694 // this will scroll the screen such that the cursor becomes visible
1695 bv->updateScrollbar();
1697 if (the_locking_inset) {
1698 /// then resize all LyXText in text-insets
1699 inset_x = cx(bv) - top_x + drawTextXOffset;
1700 inset_y = cy(bv) + drawTextYOffset;
1701 for (LyXParagraph * p = par; p; p = p->next()) {
1702 p->resizeInsetsLyXText(bv);
1709 void InsetText::removeNewlines()
1711 for (LyXParagraph * p = par; p; p = p->next()) {
1712 for (int i = 0; i < p->size(); ++i) {
1713 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();
1726 int InsetText::scroll(bool recursive) const
1728 int sx = UpdatableInset::scroll(false);
1730 if (recursive && the_locking_inset)
1731 sx += the_locking_inset->scroll(recursive);
1736 bool InsetText::doClearArea() const
1738 return !locked || (need_update & (FULL|INIT));