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);
180 // delete all instances of LyXText before deleting the paragraps used
182 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
183 delete (*cit).second;
188 LyXParagraph * tmp = par->next();
193 par = new LyXParagraph;
196 token = lex.GetString();
199 if (token == "\\end_inset")
201 if (const_cast<Buffer*>(buf)->
202 parseSingleLyXformat2Token(lex, par, return_par,
203 token, pos, depth, font)) {
204 // the_end read this should NEVER happen
205 lex.printError("\\the_end read in inset! Error in document!");
213 return_par->SetInsetOwner(this);
214 return_par = return_par->next();
217 if (token != "\\end_inset") {
218 lex.printError("Missing \\end_inset at this point. "
225 int InsetText::ascent(BufferView * bv, LyXFont const &) const
228 Row * row = TEXT(bv)->GetRowNearY(y_temp);
229 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
234 int InsetText::descent(BufferView * bv, LyXFont const &) const
237 Row * row = TEXT(bv)->GetRowNearY(y_temp);
238 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
239 TEXT_TO_INSET_OFFSET;
244 int InsetText::width(BufferView * bv, LyXFont const &) const
246 insetWidth = max(textWidth(bv),
247 (int)TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
252 int InsetText::textWidth(BufferView * bv) const
254 int const w = getMaxWidth(bv, this);
259 void InsetText::draw(BufferView * bv, LyXFont const & f,
260 int baseline, float & x, bool cleared) const
265 Painter & pain = bv->painter();
267 // no draw is necessary !!!
268 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
270 top_baseline = baseline;
272 if (!cleared && (need_update & CLEAR_FRAME))
273 clearFrame(pain, cleared);
280 UpdatableInset::draw(bv, f, baseline, x, cleared);
283 x += static_cast<float>(scroll());
285 // update insetWidth and insetHeight with dummy calls
287 (void)descent(bv, f);
290 // if top_x differs we have a rule down and we don't have to clear anything
291 if (!cleared && (top_x == int(x)) &&
292 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
293 (last_drawn_width!=insetWidth)))
296 int h = insetAscent + insetDescent;
297 int ty = baseline - insetAscent;
303 if ((ty + h) > pain.paperHeight())
304 h = pain.paperHeight();
305 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
306 w = pain.paperWidth();
307 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
311 if (!cleared && (need_update == NONE))
314 if (top_x != int(x)) {
317 bv->text->status = LyXText::CHANGED_IN_DRAW;
321 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
323 if (cleared || (last_drawn_width != insetWidth)) {
325 last_drawn_width = insetWidth;
328 top_baseline = baseline;
329 top_y = baseline - ascent(bv, f);
330 last_width = width(bv, f);
331 last_height = ascent(bv, f) + descent(bv, f);
333 if (the_locking_inset && (cpar(bv) == inset_par) && (cpos(bv) == inset_pos)) {
334 inset_x = cx(bv) - top_x + drawTextXOffset;
335 inset_y = cy(bv) + drawTextYOffset;
337 if (!cleared && (need_update == CURSOR) && !TEXT(bv)->selection) {
338 drawFrame(pain, cleared);
343 x += TEXT_TO_INSET_OFFSET;
346 #warning Jürgen, why is this a block of its own? (Lgb)
347 #warning because you told me to define variables only in local context (Jug)!
348 #warning then make it a function/method of its own. (Lgb)
352 Row * row = TEXT(bv)->GetRowNearY(y);
353 int y_offset = baseline - row->ascent_of_text();
354 int ph = pain.paperHeight();
357 while ((row != 0) && ((y+row->height()) <= 0)) {
359 first += row->height();
364 TEXT(bv)->first = first;
368 while ((row != 0) && (yf < ph)) {
369 TEXT(bv)->GetVisibleRow(bv, y+y_offset, int(x), row,
375 } else if (!locked) {
376 if (need_update & CURSOR) {
377 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
378 TEXT(bv)->ClearSelection(bv);
379 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
381 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
384 if (need_update & SELECTION)
385 bv->screen()->ToggleToggle(TEXT(bv), bv, y_offset, int(x));
386 else if (need_update & CURSOR) {
387 bv->screen()->ToggleSelection(TEXT(bv), bv, true, y_offset,int(x));
388 TEXT(bv)->ClearSelection(bv);
389 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
391 bv->screen()->Update(TEXT(bv), bv, y_offset, int(x));
395 TEXT(bv)->refresh_y = 0;
396 TEXT(bv)->status = LyXText::UNCHANGED;
397 if ((need_update != CURSOR_PAR) &&
398 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
399 drawFrame(pain, cleared);
400 else if (need_update & CLEAR_FRAME)
401 clearFrame(pain, cleared);
402 x += width(bv, f) - TEXT_TO_INSET_OFFSET;
403 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
405 } else if (need_update != INIT)
410 void InsetText::drawFrame(Painter & pain, bool cleared) const
412 if (!frame_is_visible || cleared) {
413 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
414 insetWidth - 1, insetAscent + insetDescent - 1,
416 frame_is_visible = true;
421 void InsetText::clearFrame(Painter & pain, bool cleared) const
423 if (frame_is_visible) {
425 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
426 insetWidth - 1, insetAscent + insetDescent - 1,
429 frame_is_visible = false;
434 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
440 owner()->update(bv, font, true);
443 if (the_locking_inset) {
444 inset_x = cx(bv) - top_x + drawTextXOffset;
445 inset_y = cy(bv) + drawTextYOffset;
446 the_locking_inset->update(bv, font, reinit);
449 if (need_update == INIT) {
454 int oldw = insetWidth;
455 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
456 if (oldw != insetWidth) {
459 // update(bv, font, reinit);
462 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
465 TEXT(bv)->UpdateInset(bv, the_locking_inset);
468 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
472 Row * row = TEXT(bv)->GetRowNearY(y_temp);
473 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
474 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
475 TEXT_TO_INSET_OFFSET;
480 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
483 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
485 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
486 need_update |= CURSOR_PAR;
488 // this to not draw a selection when we redraw all of it!
489 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
490 TEXT(bv)->ClearSelection(bv);
494 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
496 TEXT(bv)->FullRebreak(bv);
497 SetUpdateStatus(bv, what);
498 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
500 bv->updateInset(this, mark_dirty);
501 bv->owner()->showState();
502 if (old_par != cpar(bv)) {
503 bv->owner()->setLayout(cpar(bv)->GetLayout());
509 string const InsetText::EditMessage() const
511 return _("Opened Text Inset");
515 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
517 // par->SetInsetOwner(this);
518 UpdatableInset::Edit(bv, x, y, button);
520 if (!bv->lockInset(this)) {
521 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
525 the_locking_inset = 0;
526 inset_pos = inset_x = inset_y = 0;
527 inset_boundary = false;
530 int tmp_y = (y < 0)?0:y;
531 if (!checkAndActivateInset(bv, x, tmp_y, button))
532 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
534 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
535 bv->text->FinishUndo();
537 UpdateLocal(bv, CURSOR, false);
539 // If the inset is empty set the language of the current font to the
540 // language to the surronding text.
541 if (par->size() == 0 && !par->next()) {
542 LyXFont font(LyXFont::ALL_IGNORE);
543 font.setLanguage(bv->getParentLanguage(this));
544 SetFont(bv, font, false);
549 void InsetText::InsetUnlock(BufferView * bv)
551 if (the_locking_inset) {
552 the_locking_inset->InsetUnlock(bv);
553 the_locking_inset = 0;
556 no_selection = false;
558 UpdateLocal(bv, CLEAR_FRAME|CURSOR, false);
560 bv->owner()->setLayout(owner()->getLyXText(bv)
561 ->cursor.par()->GetLayout());
563 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
567 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
569 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
573 if (inset == cpar(bv)->GetInset(cpos(bv))) {
574 lyxerr[Debug::INSETS] << "OK" << endl;
575 the_locking_inset = inset;
576 inset_x = cx(bv) - top_x + drawTextXOffset;
577 inset_y = cy(bv) + drawTextYOffset;
578 inset_pos = cpos(bv);
579 inset_par = cpar(bv);
580 inset_boundary = cboundary(bv);
582 TEXT(bv)->ClearSelection(bv);
583 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
584 TEXT(bv)->UpdateInset(bv, the_locking_inset);
586 UpdateLocal(bv, CURSOR, false);
589 } else if (the_locking_inset && (the_locking_inset == inset)) {
590 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
591 lyxerr[Debug::INSETS] << "OK" << endl;
592 inset_x = cx(bv) - top_x + drawTextXOffset;
593 inset_y = cy(bv) + drawTextYOffset;
595 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
597 } else if (the_locking_inset) {
598 lyxerr[Debug::INSETS] << "MAYBE" << endl;
599 return the_locking_inset->LockInsetInInset(bv, inset);
601 lyxerr[Debug::INSETS] << "NOT OK" << endl;
606 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
609 if (!the_locking_inset)
611 if (the_locking_inset == inset) {
612 the_locking_inset->InsetUnlock(bv);
613 TEXT(bv)->UpdateInset(bv, inset);
614 the_locking_inset = 0;
616 moveRight(bv, false);
617 old_par = 0; // force layout setting
621 UpdateLocal(bv, CURSOR, false);
624 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
628 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
630 if (!the_locking_inset)
632 if (the_locking_inset != inset) {
633 TEXT(bv)->UpdateInset(bv, the_locking_inset);
634 SetUpdateStatus(bv, CURSOR_PAR);
635 return the_locking_inset->UpdateInsetInInset(bv, inset);
637 // UpdateLocal(bv, FULL, false);
638 if (TEXT(bv)->UpdateInset(bv, inset))
639 UpdateLocal(bv, CURSOR_PAR, false);
640 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
641 inset_x = cx(bv) - top_x + drawTextXOffset;
642 inset_y = cy(bv) + drawTextYOffset;
648 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
652 int tmp_x = x - drawTextXOffset;
653 int tmp_y = y + insetAscent - TEXT(bv)->first;
654 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
657 if (the_locking_inset) {
658 if (the_locking_inset == inset) {
659 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
660 no_selection = false;
663 // otherwise unlock the_locking_inset and lock the new inset
664 the_locking_inset->InsetUnlock(bv);
665 inset_x = cx(bv) - top_x + drawTextXOffset;
666 inset_y = cy(bv) + drawTextYOffset;
667 the_locking_inset = static_cast<UpdatableInset*>(inset);
668 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
669 inset->Edit(bv, x - inset_x, y - inset_y, button);
670 if (the_locking_inset)
671 UpdateLocal(bv, CURSOR, false);
672 no_selection = false;
675 // otherwise only unlock the_locking_inset
676 the_locking_inset->InsetUnlock(bv);
677 the_locking_inset = 0;
679 if (bv->theLockingInset()) {
680 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
681 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
682 inset_x = cx(bv) - top_x + drawTextXOffset;
683 inset_y = cy(bv) + drawTextYOffset;
684 inset_pos = cpos(bv);
685 inset_par = cpar(bv);
686 inset_boundary = cboundary(bv);
687 the_locking_inset = uinset;
688 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
690 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
691 if (the_locking_inset)
692 UpdateLocal(bv, CURSOR, false);
693 no_selection = false;
697 if (!inset) { // && (button == 2)) {
698 bool paste_internally = false;
699 if ((button == 2) && TEXT(bv)->selection) {
700 LocalDispatch(bv, LFUN_COPY, "");
701 paste_internally = true;
703 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
705 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
706 UpdateLocal(bv, CURSOR, false);
707 bv->owner()->setLayout(cpar(bv)->GetLayout());
709 // Insert primary selection with middle mouse
710 // if there is a local selection in the current buffer,
713 if (paste_internally)
714 LocalDispatch(bv, LFUN_PASTE, "");
716 LocalDispatch(bv, LFUN_PASTESELECTION,
721 no_selection = false;
725 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
727 UpdatableInset * inset = 0;
729 if (the_locking_inset) {
730 the_locking_inset->InsetButtonRelease(bv,
731 x - inset_x, y - inset_y,
734 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
735 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
736 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
737 inset->InsetButtonRelease(bv,
739 y - inset_y, button);
741 inset_x = cx(bv) - top_x + drawTextXOffset;
742 inset_y = cy(bv) + drawTextYOffset;
743 inset->InsetButtonRelease(bv,
745 y - inset_y, button);
747 x - inset_x, y - inset_y, button);
749 UpdateLocal(bv, CURSOR_PAR, false);
752 no_selection = false;
756 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
760 if (the_locking_inset) {
761 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
766 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
768 TEXT(bv)->SetSelection(bv);
769 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
770 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
771 UpdateLocal(bv, SELECTION, false);
776 void InsetText::InsetKeyPress(XKeyEvent * xke)
778 if (the_locking_inset) {
779 the_locking_inset->InsetKeyPress(xke);
785 UpdatableInset::RESULT
786 InsetText::LocalDispatch(BufferView * bv,
787 kb_action action, string const & arg)
789 no_selection = false;
790 UpdatableInset::RESULT
791 result= UpdatableInset::LocalDispatch(bv, action, arg);
792 if (result != UNDISPATCHED) {
797 if ((action < 0) && arg.empty())
800 if (the_locking_inset) {
801 result = the_locking_inset->LocalDispatch(bv, action, arg);
802 if (result == DISPATCHED_NOUPDATE)
804 else if (result == DISPATCHED) {
805 UpdateLocal(bv, CURSOR_PAR, false);
807 } else if (result == FINISHED) {
808 bool dispatched = false;
810 case LFUN_UNKNOWN_ACTION:
811 case LFUN_BREAKPARAGRAPH:
813 moveRightIntern(bv, false, false);
816 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
817 moveRightIntern(bv, false, false);
821 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
822 moveRightIntern(bv, false, false);
828 the_locking_inset = 0;
836 case LFUN_UNKNOWN_ACTION:
837 if (bv->buffer()->isReadonly()) {
838 // setErrorMessage(N_("Document is read only"));
842 /* Automatically delete the currently selected
843 * text and replace it with what is being
844 * typed in now. Depends on lyxrc settings
845 * "auto_region_delete", which defaults to
848 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
849 bv->text->cursor.par()->previous(),
850 bv->text->cursor.par()->next());
852 if (lyxrc.auto_region_delete) {
853 if (TEXT(bv)->selection){
854 TEXT(bv)->CutSelection(bv, false);
857 TEXT(bv)->ClearSelection(bv);
858 for (string::size_type i = 0; i < arg.length(); ++i) {
860 if (!math_insert_greek(bv, arg[i])) {
861 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
862 } else if (!the_locking_inset) {
863 (void)moveRight(bv, false);
866 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
870 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
871 UpdateLocal(bv, CURSOR_PAR, true);
872 result=DISPATCHED_NOUPDATE;
874 // --- Cursor Movements -----------------------------------
876 bv->text->FinishUndo();
877 moveRight(bv, false, true);
878 TEXT(bv)->SetSelection(bv);
879 UpdateLocal(bv, SELECTION, false);
882 result = moveRight(bv);
883 bv->text->FinishUndo();
884 UpdateLocal(bv, CURSOR, false);
887 bv->text->FinishUndo();
888 moveLeft(bv, false, true);
889 TEXT(bv)->SetSelection(bv);
890 UpdateLocal(bv, SELECTION, false);
893 bv->text->FinishUndo();
894 result = moveLeft(bv);
895 UpdateLocal(bv, CURSOR, false);
898 bv->text->FinishUndo();
900 TEXT(bv)->SetSelection(bv);
901 UpdateLocal(bv, SELECTION, false);
904 bv->text->FinishUndo();
905 result = moveDown(bv);
906 UpdateLocal(bv, CURSOR, false);
909 bv->text->FinishUndo();
911 TEXT(bv)->SetSelection(bv);
912 UpdateLocal(bv, SELECTION, false);
915 bv->text->FinishUndo();
917 UpdateLocal(bv, CURSOR, false);
920 bv->text->FinishUndo();
921 TEXT(bv)->CursorHome(bv);
922 UpdateLocal(bv, CURSOR, false);
925 TEXT(bv)->CursorEnd(bv);
926 UpdateLocal(bv, CURSOR, false);
929 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
930 bv->text->cursor.par()->previous(),
931 bv->text->cursor.par()->next());
932 if (TEXT(bv)->selection)
933 TEXT(bv)->CutSelection(bv);
935 TEXT(bv)->Backspace(bv);
936 UpdateLocal(bv, CURSOR_PAR, true);
939 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
940 bv->text->cursor.par()->previous(),
941 bv->text->cursor.par()->next());
942 if (TEXT(bv)->selection)
943 TEXT(bv)->CutSelection(bv);
945 TEXT(bv)->Delete(bv);
946 UpdateLocal(bv, CURSOR_PAR, true);
949 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
950 bv->text->cursor.par()->previous(),
951 bv->text->cursor.par()->next());
952 TEXT(bv)->CutSelection(bv);
953 UpdateLocal(bv, CURSOR_PAR, true);
956 bv->text->FinishUndo();
957 TEXT(bv)->CopySelection(bv);
958 UpdateLocal(bv, CURSOR_PAR, false);
960 case LFUN_PASTESELECTION:
962 string clip(bv->workarea()->getClipboard());
966 if (arg == "paragraph") {
967 TEXT(bv)->InsertStringB(bv, clip);
969 TEXT(bv)->InsertStringA(bv, clip);
971 UpdateLocal(bv, CURSOR_PAR, true);
975 if (!autoBreakRows) {
978 if (cap.nrOfParagraphs() > 1) {
979 WriteAlert(_("Impossible operation"),
980 _("Cannot include more than one paragraph!"),
985 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
986 bv->text->cursor.par()->previous(),
987 bv->text->cursor.par()->next());
988 TEXT(bv)->PasteSelection(bv);
989 UpdateLocal(bv, CURSOR_PAR, true);
991 case LFUN_BREAKPARAGRAPH:
994 TEXT(bv)->BreakParagraph(bv, 0);
995 UpdateLocal(bv, FULL, true);
997 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1000 TEXT(bv)->BreakParagraph(bv, 1);
1001 UpdateLocal(bv, FULL, true);
1003 case LFUN_BREAKLINE:
1006 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1007 bv->text->cursor.par()->previous(),
1008 bv->text->cursor.par()->next());
1009 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1010 UpdateLocal(bv, CURSOR_PAR, true);
1013 // do not set layouts on non breakable textinsets
1014 if (autoBreakRows) {
1015 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1017 // Derive layout number from given argument (string)
1018 // and current buffer's textclass (number). */
1019 LyXTextClassList::ClassList::size_type tclass =
1020 bv->buffer()->params.textclass;
1021 std::pair <bool, LyXTextClass::size_type> layout =
1022 textclasslist.NumberOfLayout(tclass, arg);
1024 // If the entry is obsolete, use the new one instead.
1026 string obs = textclasslist.Style(tclass,layout.second).
1029 layout = textclasslist.NumberOfLayout(tclass, obs);
1032 // see if we found the layout number:
1033 if (!layout.first) {
1034 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1035 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1039 if (cur_layout != layout.second) {
1040 cur_layout = layout.second;
1041 TEXT(bv)->SetLayout(bv, layout.second);
1042 bv->owner()->setLayout(cpar(bv)->GetLayout());
1043 UpdateLocal(bv, CURSOR_PAR, true);
1046 // reset the layout box
1047 bv->owner()->setLayout(cpar(bv)->GetLayout());
1050 case LFUN_PARAGRAPH_SPACING:
1051 // This one is absolutely not working. When fiddling with this
1052 // it also seems to me that the paragraphs inside the insettext
1053 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1055 LyXParagraph * par = TEXT(bv)->cursor.par();
1056 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1057 float cur_value = 1.0;
1058 if (cur_spacing == Spacing::Other) {
1059 cur_value = par->params.spacing().getValue();
1062 std::istringstream istr(arg.c_str());
1065 Spacing::Space new_spacing = cur_spacing;
1066 float new_value = cur_value;
1068 lyxerr << "Missing argument to `paragraph-spacing'"
1070 } else if (tmp == "single") {
1071 new_spacing = Spacing::Single;
1072 } else if (tmp == "onehalf") {
1073 new_spacing = Spacing::Onehalf;
1074 } else if (tmp == "double") {
1075 new_spacing = Spacing::Double;
1076 } else if (tmp == "other") {
1077 new_spacing = Spacing::Other;
1080 lyxerr << "new_value = " << tmpval << endl;
1083 } else if (tmp == "default") {
1084 new_spacing = Spacing::Default;
1086 lyxerr << _("Unknown spacing argument: ")
1089 if (cur_spacing != new_spacing || cur_value != new_value) {
1090 par->params.spacing(Spacing(new_spacing, new_value));
1091 //TEXT(bv)->RedoParagraph(owner->view());
1092 UpdateLocal(bv, CURSOR_PAR, true);
1093 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1099 if (!bv->Dispatch(action, arg))
1100 result = UNDISPATCHED;
1104 /// If the action has deleted all text in the inset, we need to change the
1105 // language to the language to the surronding text.
1106 if (par->size() == 0 && !par->next()) {
1107 LyXFont font(LyXFont::ALL_IGNORE);
1108 font.setLanguage(bv->getParentLanguage(this));
1109 SetFont(bv, font, false);
1112 if (result != FINISHED) {
1113 ShowInsetCursor(bv);
1115 bv->unlockInset(this);
1120 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1123 buf->latexParagraphs(os, par, 0, texrow);
1124 return texrow.rows();
1128 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1130 LyXParagraph * p = par;
1131 unsigned int lines = 0;
1135 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);
1258 UpdatableInset::RESULT
1259 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1261 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1262 return moveRightIntern(bv, true, activate_inset, selecting);
1264 return moveLeftIntern(bv, true, activate_inset, selecting);
1268 UpdatableInset::RESULT
1269 InsetText::moveRightIntern(BufferView * bv, bool behind,
1270 bool activate_inset, bool selecting)
1272 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1274 if (activate_inset && checkAndActivateInset(bv, behind))
1276 TEXT(bv)->CursorRight(bv);
1278 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1279 return DISPATCHED_NOUPDATE;
1283 UpdatableInset::RESULT
1284 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1285 bool activate_inset, bool selecting)
1287 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1289 TEXT(bv)->CursorLeft(bv);
1291 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1292 if (activate_inset && checkAndActivateInset(bv, behind))
1294 return DISPATCHED_NOUPDATE;
1298 UpdatableInset::RESULT
1299 InsetText::moveUp(BufferView * bv)
1301 if (!crow(bv)->previous())
1303 TEXT(bv)->CursorUp(bv);
1304 return DISPATCHED_NOUPDATE;
1308 UpdatableInset::RESULT
1309 InsetText::moveDown(BufferView * bv)
1311 if (!crow(bv)->next())
1313 TEXT(bv)->CursorDown(bv);
1314 return DISPATCHED_NOUPDATE;
1318 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1320 if (the_locking_inset) {
1321 if (the_locking_inset->InsertInsetAllowed(inset))
1322 return the_locking_inset->InsertInset(bv, inset);
1325 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1326 bv->text->cursor.par()->previous(),
1327 bv->text->cursor.par()->next());
1328 inset->setOwner(this);
1329 HideInsetCursor(bv);
1330 TEXT(bv)->InsertInset(bv, inset);
1332 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1333 (cpar(bv)->GetInset(cpos(bv)) != inset))
1334 TEXT(bv)->CursorLeft(bv);
1336 bv->fitCursor(TEXT(bv));
1337 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1338 ShowInsetCursor(bv);
1343 UpdatableInset * InsetText::GetLockingInset()
1345 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1349 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1353 if (the_locking_inset)
1354 return the_locking_inset->GetFirstLockingInsetOfType(c);
1359 bool InsetText::ShowInsetDialog(BufferView * bv) const
1361 if (the_locking_inset)
1362 return the_locking_inset->ShowInsetDialog(bv);
1367 std::vector<string> const InsetText::getLabelList() const
1369 std::vector<string> label_list;
1371 LyXParagraph * tpar = par;
1373 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1374 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1375 for (; beg != end; ++beg) {
1376 std::vector<string> const l = (*beg)->getLabelList();
1377 label_list.insert(label_list.end(), l.begin(), l.end());
1379 tpar = tpar->next();
1385 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
1387 if (TEXT(bv)->selection) {
1388 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1389 bv->text->cursor.par()->previous(),
1390 bv->text->cursor.par()->next());
1392 TEXT(bv)->SetFont(bv, font, toggleall);
1393 bv->fitCursor(TEXT(bv));
1394 UpdateLocal(bv, CURSOR_PAR, true);
1398 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1400 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1404 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1405 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1407 LyXFont const font =
1408 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1410 x = inset->width(bv, font);
1411 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1414 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1416 //inset_x = cx(bv) - top_x + drawTextXOffset;
1417 //inset_y = cy(bv) + drawTextYOffset;
1418 inset->Edit(bv, x, y, 0);
1419 if (!the_locking_inset)
1421 UpdateLocal(bv, CURSOR, false);
1428 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1433 dummyx = x = x - drawTextXOffset;
1434 dummyy = y + insetAscent;
1435 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1442 inset_x = cx(bv) - top_x + drawTextXOffset;
1443 inset_y = cy(bv) + drawTextYOffset;
1444 inset->Edit(bv, x - inset_x, y - inset_y, button);
1445 if (!the_locking_inset)
1447 UpdateLocal(bv, CURSOR, false);
1454 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1456 int w = UpdatableInset::getMaxWidth(bv, inset);
1461 w = w - top_x + owner()->x();
1464 w -= (2 * TEXT_TO_INSET_OFFSET);
1466 // return w - (2*TEXT_TO_INSET_OFFSET);
1470 void InsetText::SetParagraphData(LyXParagraph * p)
1472 // delete all instances of LyXText before deleting the paragraps used
1474 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1475 delete (*cit).second;
1480 LyXParagraph * tmp = par->next();
1486 par->SetInsetOwner(this);
1487 LyXParagraph * np = par;
1490 np->next(p->Clone());
1491 np->next()->previous(np);
1493 np->SetInsetOwner(this);
1500 void InsetText::SetText(string const & data)
1503 LyXFont font(LyXFont::ALL_SANE);
1504 for (unsigned int i=0; i < data.length(); ++i)
1505 par->InsertChar(i, data[i], font);
1509 void InsetText::SetAutoBreakRows(bool flag)
1511 if (flag != autoBreakRows) {
1512 autoBreakRows = flag;
1520 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1522 if (how != drawFrame_) {
1525 UpdateLocal(bv, DRAW_FRAME, false);
1530 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1532 if (frame_color != col) {
1535 UpdateLocal(bv, DRAW_FRAME, false);
1540 int InsetText::cx(BufferView * bv) const
1542 LyXText * text = TEXT(bv);
1543 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1544 if (the_locking_inset) {
1545 LyXFont font = text->GetFont(bv->buffer(),
1547 text->cursor.pos());
1548 if (font.isVisibleRightToLeft())
1549 x -= the_locking_inset->width(bv, font);
1555 int InsetText::cy(BufferView * bv) const
1558 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1562 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1564 return TEXT(bv)->cursor.pos();
1568 LyXParagraph * InsetText::cpar(BufferView * bv) const
1570 return TEXT(bv)->cursor.par();
1573 bool InsetText::cboundary(BufferView * bv) const
1575 return TEXT(bv)->cursor.boundary();
1579 Row * InsetText::crow(BufferView * bv) const
1581 return TEXT(bv)->cursor.row();
1585 LyXText * InsetText::getLyXText(BufferView const * lbv,
1586 bool const recursive) const
1588 // Super UGLY! (Lgb)
1589 BufferView * bv = const_cast<BufferView *>(lbv);
1591 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1592 if (recursive && the_locking_inset)
1593 return the_locking_inset->getLyXText(bv);
1596 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1599 if (the_locking_inset) {
1600 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1602 return the_locking_inset->getLyXText(bv);
1608 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1610 if ((cache.find(bv) == cache.end()) || !cache[bv])
1615 /// then remove all LyXText in text-insets
1616 LyXParagraph * p = par;
1617 for (; p; p = p->next()) {
1618 p->deleteInsetsLyXText(bv);
1624 void InsetText::resizeLyXText(BufferView * bv) const
1626 if (!par->next() && !par->size()) // resize not neccessary!
1628 if ((cache.find(bv) == cache.end()) || !cache[bv])
1631 LyXParagraph * lpar = 0;
1632 LyXParagraph * selstartpar = 0;
1633 LyXParagraph * selendpar = 0;
1634 LyXParagraph::size_type pos = 0;
1635 LyXParagraph::size_type selstartpos = 0;
1636 LyXParagraph::size_type selendpos = 0;
1637 bool boundary = false;
1638 bool selstartboundary = false;
1639 bool selendboundary = false;
1643 // ProhibitInput(bv);
1646 lpar = TEXT(bv)->cursor.par();
1647 pos = TEXT(bv)->cursor.pos();
1648 boundary = TEXT(bv)->cursor.boundary();
1649 selstartpar = TEXT(bv)->sel_start_cursor.par();
1650 selstartpos = TEXT(bv)->sel_start_cursor.pos();
1651 selstartboundary = TEXT(bv)->sel_start_cursor.boundary();
1652 selendpar = TEXT(bv)->sel_end_cursor.par();
1653 selendpos = TEXT(bv)->sel_end_cursor.pos();
1654 selendboundary = TEXT(bv)->sel_end_cursor.boundary();
1655 selection = TEXT(bv)->selection;
1656 mark_set = TEXT(bv)->mark_set;
1658 deleteLyXText(bv, (the_locking_inset == 0));
1661 TEXT(bv)->selection = true;
1662 /* at this point just to avoid the Delete-Empty-Paragraph
1663 * Mechanism when setting the cursor */
1664 TEXT(bv)->mark_set = mark_set;
1666 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1667 true, selstartboundary);
1668 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1669 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1670 true, selendboundary);
1671 TEXT(bv)->SetSelection(bv);
1672 TEXT(bv)->SetCursor(bv, lpar, pos);
1674 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1675 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
1676 TEXT(bv)->selection = false;
1680 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1681 // this will scroll the screen such that the cursor becomes visible
1682 bv->updateScrollbar();
1684 if (the_locking_inset) {
1685 /// then resize all LyXText in text-insets
1686 inset_x = cx(bv) - top_x + drawTextXOffset;
1687 inset_y = cy(bv) + drawTextYOffset;
1688 for (LyXParagraph * p = par; p; p = p->next()) {
1689 p->resizeInsetsLyXText(bv);
1696 void InsetText::removeNewlines()
1698 for (LyXParagraph * p = par; p; p = p->next()) {
1699 for (int i = 0; i < p->size(); ++i) {
1700 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1706 bool InsetText::nodraw() const
1708 if (the_locking_inset)
1709 return the_locking_inset->nodraw();
1710 return UpdatableInset::nodraw();
1713 int InsetText::scroll(bool recursive) const
1715 int sx = UpdatableInset::scroll(false);
1717 if (recursive && the_locking_inset)
1718 sx += the_locking_inset->scroll(recursive);
1723 bool InsetText::doClearArea() const
1725 return !locked || (need_update & (FULL|INIT));