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)
429 owner()->update(bv, font, true);
432 if (the_locking_inset) {
433 inset_x = cx(bv) - top_x + drawTextXOffset;
434 inset_y = cy(bv) + drawTextYOffset;
435 the_locking_inset->update(bv, font, reinit);
438 if (need_update == INIT) {
443 int oldw = insetWidth;
444 insetWidth = TEXT(bv)->width + (2 * TEXT_TO_INSET_OFFSET);
445 if (oldw != insetWidth) {
448 // update(bv, font, reinit);
451 if ((need_update&CURSOR_PAR) && (TEXT(bv)->status==LyXText::UNCHANGED) &&
454 TEXT(bv)->UpdateInset(bv, the_locking_inset);
457 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
461 Row * row = TEXT(bv)->GetRowNearY(y_temp);
462 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
463 insetDescent = TEXT(bv)->height - row->ascent_of_text() +
464 TEXT_TO_INSET_OFFSET;
469 void InsetText::SetUpdateStatus(BufferView * bv, int what) const
472 if (TEXT(bv)->status == LyXText::NEED_MORE_REFRESH)
474 else if (TEXT(bv)->status == LyXText::NEED_VERY_LITTLE_REFRESH)
475 need_update |= CURSOR_PAR;
477 // this to not draw a selection when we redraw all of it!
478 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
479 TEXT(bv)->ClearSelection(bv);
483 void InsetText::UpdateLocal(BufferView * bv, int what, bool mark_dirty)
485 TEXT(bv)->FullRebreak(bv);
486 SetUpdateStatus(bv, what);
487 if ((need_update != CURSOR) || (TEXT(bv)->status != LyXText::UNCHANGED) ||
488 TEXT(bv)->selection.set())
489 bv->updateInset(this, mark_dirty);
490 bv->owner()->showState();
491 if (old_par != cpar(bv)) {
492 bv->owner()->setLayout(cpar(bv)->GetLayout());
498 string const InsetText::EditMessage() const
500 return _("Opened Text Inset");
504 void InsetText::Edit(BufferView * bv, int x, int y, unsigned int button)
506 // par->SetInsetOwner(this);
507 UpdatableInset::Edit(bv, x, y, button);
509 if (!bv->lockInset(this)) {
510 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
514 the_locking_inset = 0;
515 inset_pos = inset_x = inset_y = 0;
516 inset_boundary = false;
519 int tmp_y = (y < 0)?0:y;
520 if (!checkAndActivateInset(bv, x, tmp_y, button))
521 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
523 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
524 bv->text->FinishUndo();
526 UpdateLocal(bv, CURSOR, false);
528 // If the inset is empty set the language of the current font to the
529 // language to the surronding text (if different).
530 if (par->size() == 0 && !par->next() &&
531 bv->getParentLanguage(this) != TEXT(bv)->current_font.language())
533 LyXFont font(LyXFont::ALL_IGNORE);
534 font.setLanguage(bv->getParentLanguage(this));
535 SetFont(bv, font, false);
540 void InsetText::InsetUnlock(BufferView * bv)
542 if (the_locking_inset) {
543 the_locking_inset->InsetUnlock(bv);
544 the_locking_inset = 0;
547 no_selection = false;
549 int code = CURSOR|CLEAR_FRAME;
550 if (TEXT(bv)->selection.set()) {
551 TEXT(bv)->ClearSelection(bv);
554 bv->owner()->setLayout(owner()->getLyXText(bv)
555 ->cursor.par()->GetLayout());
557 bv->owner()->setLayout(bv->text->cursor.par()->GetLayout());
558 UpdateLocal(bv, code, false);
562 bool InsetText::LockInsetInInset(BufferView * bv, UpdatableInset * inset)
564 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
568 if (inset == cpar(bv)->GetInset(cpos(bv))) {
569 lyxerr[Debug::INSETS] << "OK" << endl;
570 the_locking_inset = inset;
571 inset_x = cx(bv) - top_x + drawTextXOffset;
572 inset_y = cy(bv) + drawTextYOffset;
573 inset_pos = cpos(bv);
574 inset_par = cpar(bv);
575 inset_boundary = cboundary(bv);
577 TEXT(bv)->ClearSelection(bv);
578 TEXT(bv)->sel_cursor = TEXT(bv)->cursor;
579 TEXT(bv)->UpdateInset(bv, the_locking_inset);
581 UpdateLocal(bv, CURSOR, false);
584 } else if (the_locking_inset && (the_locking_inset == inset)) {
585 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
586 lyxerr[Debug::INSETS] << "OK" << endl;
587 inset_x = cx(bv) - top_x + drawTextXOffset;
588 inset_y = cy(bv) + drawTextYOffset;
590 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
592 } else if (the_locking_inset) {
593 lyxerr[Debug::INSETS] << "MAYBE" << endl;
594 return the_locking_inset->LockInsetInInset(bv, inset);
596 lyxerr[Debug::INSETS] << "NOT OK" << endl;
601 bool InsetText::UnlockInsetInInset(BufferView * bv, UpdatableInset * inset,
604 if (!the_locking_inset)
606 if (the_locking_inset == inset) {
607 the_locking_inset->InsetUnlock(bv);
608 TEXT(bv)->UpdateInset(bv, inset);
609 the_locking_inset = 0;
611 moveRight(bv, false);
612 old_par = 0; // force layout setting
616 UpdateLocal(bv, CURSOR, false);
619 return the_locking_inset->UnlockInsetInInset(bv, inset, lr);
623 bool InsetText::UpdateInsetInInset(BufferView * bv, Inset * inset)
625 if (!the_locking_inset)
627 if (the_locking_inset != inset) {
628 TEXT(bv)->UpdateInset(bv, the_locking_inset);
629 SetUpdateStatus(bv, CURSOR_PAR);
630 return the_locking_inset->UpdateInsetInInset(bv, inset);
632 // UpdateLocal(bv, FULL, false);
633 if (TEXT(bv)->UpdateInset(bv, inset))
634 UpdateLocal(bv, CURSOR_PAR, false);
635 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
636 inset_x = cx(bv) - top_x + drawTextXOffset;
637 inset_y = cy(bv) + drawTextYOffset;
643 void InsetText::InsetButtonPress(BufferView * bv, int x, int y, int button)
647 int tmp_x = x - drawTextXOffset;
648 int tmp_y = y + insetAscent - TEXT(bv)->first;
649 Inset * inset = bv->checkInsetHit(TEXT(bv), tmp_x, tmp_y, button);
652 if (the_locking_inset) {
653 if (the_locking_inset == inset) {
654 the_locking_inset->InsetButtonPress(bv,x-inset_x,y-inset_y,button);
655 no_selection = false;
658 // otherwise unlock the_locking_inset and lock the new inset
659 the_locking_inset->InsetUnlock(bv);
660 inset_x = cx(bv) - top_x + drawTextXOffset;
661 inset_y = cy(bv) + drawTextYOffset;
662 the_locking_inset = static_cast<UpdatableInset*>(inset);
663 inset->InsetButtonPress(bv, x - inset_x, y - inset_y, button);
664 inset->Edit(bv, x - inset_x, y - inset_y, button);
665 if (the_locking_inset)
666 UpdateLocal(bv, CURSOR, false);
667 no_selection = false;
670 // otherwise only unlock the_locking_inset
671 the_locking_inset->InsetUnlock(bv);
672 the_locking_inset = 0;
674 if (bv->theLockingInset()) {
675 if (inset && inset->Editable() == Inset::HIGHLY_EDITABLE) {
676 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
677 inset_x = cx(bv) - top_x + drawTextXOffset;
678 inset_y = cy(bv) + drawTextYOffset;
679 inset_pos = cpos(bv);
680 inset_par = cpar(bv);
681 inset_boundary = cboundary(bv);
682 the_locking_inset = uinset;
683 uinset->InsetButtonPress(bv, x - inset_x, y - inset_y,
685 uinset->Edit(bv, x - inset_x, y - inset_y, 0);
686 if (the_locking_inset)
687 UpdateLocal(bv, CURSOR, false);
688 no_selection = false;
692 if (!inset) { // && (button == 2)) {
693 bool paste_internally = false;
694 if ((button == 2) && TEXT(bv)->selection.set()) {
695 LocalDispatch(bv, LFUN_COPY, "");
696 paste_internally = true;
698 TEXT(bv)->SetCursorFromCoordinates(bv, x-drawTextXOffset,
700 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
701 UpdateLocal(bv, CURSOR, false);
702 bv->owner()->setLayout(cpar(bv)->GetLayout());
704 // Insert primary selection with middle mouse
705 // if there is a local selection in the current buffer,
708 if (paste_internally)
709 LocalDispatch(bv, LFUN_PASTE, "");
711 LocalDispatch(bv, LFUN_PASTESELECTION,
716 no_selection = false;
720 void InsetText::InsetButtonRelease(BufferView * bv, int x, int y, int button)
722 UpdatableInset * inset = 0;
724 if (the_locking_inset) {
725 the_locking_inset->InsetButtonRelease(bv,
726 x - inset_x, y - inset_y,
729 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
730 inset = static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
731 if (inset->Editable() == Inset::HIGHLY_EDITABLE) {
732 inset->InsetButtonRelease(bv,
734 y - inset_y, button);
736 inset_x = cx(bv) - top_x + drawTextXOffset;
737 inset_y = cy(bv) + drawTextYOffset;
738 inset->InsetButtonRelease(bv,
740 y - inset_y, button);
742 x - inset_x, y - inset_y, button);
744 UpdateLocal(bv, CURSOR_PAR, false);
747 no_selection = false;
751 void InsetText::InsetMotionNotify(BufferView * bv, int x, int y, int state)
755 if (the_locking_inset) {
756 the_locking_inset->InsetMotionNotify(bv, x - inset_x,
761 TEXT(bv)->SetCursorFromCoordinates(bv, x - drawTextXOffset,
763 TEXT(bv)->SetSelection(bv);
764 if (TEXT(bv)->toggle_cursor.par()!=TEXT(bv)->toggle_end_cursor.par() ||
765 TEXT(bv)->toggle_cursor.pos()!=TEXT(bv)->toggle_end_cursor.pos())
766 UpdateLocal(bv, SELECTION, false);
771 void InsetText::InsetKeyPress(XKeyEvent * xke)
773 if (the_locking_inset) {
774 the_locking_inset->InsetKeyPress(xke);
780 UpdatableInset::RESULT
781 InsetText::LocalDispatch(BufferView * bv,
782 kb_action action, string const & arg)
784 no_selection = false;
785 UpdatableInset::RESULT
786 result= UpdatableInset::LocalDispatch(bv, action, arg);
787 if (result != UNDISPATCHED) {
792 if ((action < 0) && arg.empty())
795 if (the_locking_inset) {
796 result = the_locking_inset->LocalDispatch(bv, action, arg);
797 if (result == DISPATCHED_NOUPDATE)
799 else if (result == DISPATCHED) {
800 UpdateLocal(bv, CURSOR_PAR, false);
802 } else if (result == FINISHED) {
803 bool dispatched = false;
805 case LFUN_UNKNOWN_ACTION:
806 case LFUN_BREAKPARAGRAPH:
808 moveRightIntern(bv, false, false);
811 if (!TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
812 moveRightIntern(bv, false, false);
816 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
817 moveRightIntern(bv, false, false);
823 the_locking_inset = 0;
831 case LFUN_UNKNOWN_ACTION:
832 if (bv->buffer()->isReadonly()) {
833 // setErrorMessage(N_("Document is read only"));
837 /* Automatically delete the currently selected
838 * text and replace it with what is being
839 * typed in now. Depends on lyxrc settings
840 * "auto_region_delete", which defaults to
843 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
844 bv->text->cursor.par()->previous(),
845 bv->text->cursor.par()->next());
847 if (lyxrc.auto_region_delete) {
848 if (TEXT(bv)->selection.set()) {
849 TEXT(bv)->CutSelection(bv, false);
852 TEXT(bv)->ClearSelection(bv);
853 for (string::size_type i = 0; i < arg.length(); ++i) {
855 if (!math_insert_greek(bv, arg[i])) {
856 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
857 } else if (!the_locking_inset) {
858 (void)moveRight(bv, false);
861 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], TEXT(bv));
865 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
866 UpdateLocal(bv, CURSOR_PAR, true);
867 result=DISPATCHED_NOUPDATE;
869 // --- Cursor Movements -----------------------------------
871 bv->text->FinishUndo();
872 moveRight(bv, false, true);
873 TEXT(bv)->SetSelection(bv);
874 UpdateLocal(bv, SELECTION, false);
877 result = moveRight(bv);
878 bv->text->FinishUndo();
879 UpdateLocal(bv, CURSOR, false);
882 bv->text->FinishUndo();
883 moveLeft(bv, false, true);
884 TEXT(bv)->SetSelection(bv);
885 UpdateLocal(bv, SELECTION, false);
888 bv->text->FinishUndo();
889 result = moveLeft(bv);
890 UpdateLocal(bv, CURSOR, false);
893 bv->text->FinishUndo();
895 TEXT(bv)->SetSelection(bv);
896 UpdateLocal(bv, SELECTION, false);
899 bv->text->FinishUndo();
900 result = moveDown(bv);
901 UpdateLocal(bv, CURSOR, false);
904 bv->text->FinishUndo();
906 TEXT(bv)->SetSelection(bv);
907 UpdateLocal(bv, SELECTION, false);
910 bv->text->FinishUndo();
912 UpdateLocal(bv, CURSOR, false);
915 bv->text->FinishUndo();
916 TEXT(bv)->CursorHome(bv);
917 UpdateLocal(bv, CURSOR, false);
920 TEXT(bv)->CursorEnd(bv);
921 UpdateLocal(bv, CURSOR, false);
924 bv->text->SetUndo(bv->buffer(), Undo::DELETE,
925 bv->text->cursor.par()->previous(),
926 bv->text->cursor.par()->next());
927 if (TEXT(bv)->selection.set())
928 TEXT(bv)->CutSelection(bv);
930 TEXT(bv)->Backspace(bv);
931 UpdateLocal(bv, CURSOR_PAR, true);
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.set())
938 TEXT(bv)->CutSelection(bv);
940 TEXT(bv)->Delete(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 TEXT(bv)->CutSelection(bv);
948 UpdateLocal(bv, CURSOR_PAR, true);
951 bv->text->FinishUndo();
952 TEXT(bv)->CopySelection(bv);
953 UpdateLocal(bv, CURSOR_PAR, false);
955 case LFUN_PASTESELECTION:
957 string const clip(bv->getClipboard());
961 if (arg == "paragraph") {
962 TEXT(bv)->InsertStringB(bv, clip);
964 TEXT(bv)->InsertStringA(bv, clip);
966 UpdateLocal(bv, CURSOR_PAR, true);
970 if (!autoBreakRows) {
973 if (cap.nrOfParagraphs() > 1) {
974 WriteAlert(_("Impossible operation"),
975 _("Cannot include more than one paragraph!"),
980 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
981 bv->text->cursor.par()->previous(),
982 bv->text->cursor.par()->next());
983 TEXT(bv)->PasteSelection(bv);
984 UpdateLocal(bv, CURSOR_PAR, true);
986 case LFUN_BREAKPARAGRAPH:
989 TEXT(bv)->BreakParagraph(bv, 0);
990 UpdateLocal(bv, FULL, true);
992 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
995 TEXT(bv)->BreakParagraph(bv, 1);
996 UpdateLocal(bv, FULL, true);
1001 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1002 bv->text->cursor.par()->previous(),
1003 bv->text->cursor.par()->next());
1004 TEXT(bv)->InsertChar(bv, LyXParagraph::META_NEWLINE);
1005 UpdateLocal(bv, CURSOR_PAR, true);
1008 // do not set layouts on non breakable textinsets
1009 if (autoBreakRows) {
1010 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1012 // Derive layout number from given argument (string)
1013 // and current buffer's textclass (number). */
1014 LyXTextClassList::ClassList::size_type tclass =
1015 bv->buffer()->params.textclass;
1016 std::pair <bool, LyXTextClass::size_type> layout =
1017 textclasslist.NumberOfLayout(tclass, arg);
1019 // If the entry is obsolete, use the new one instead.
1021 string obs = textclasslist.Style(tclass,layout.second).
1024 layout = textclasslist.NumberOfLayout(tclass, obs);
1027 // see if we found the layout number:
1028 if (!layout.first) {
1029 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1030 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1034 if (cur_layout != layout.second) {
1035 cur_layout = layout.second;
1036 TEXT(bv)->SetLayout(bv, layout.second);
1037 bv->owner()->setLayout(cpar(bv)->GetLayout());
1038 UpdateLocal(bv, CURSOR_PAR, true);
1041 // reset the layout box
1042 bv->owner()->setLayout(cpar(bv)->GetLayout());
1045 case LFUN_PARAGRAPH_SPACING:
1046 // This one is absolutely not working. When fiddling with this
1047 // it also seems to me that the paragraphs inside the insettext
1048 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1050 LyXParagraph * par = TEXT(bv)->cursor.par();
1051 Spacing::Space cur_spacing = par->params.spacing().getSpace();
1052 float cur_value = 1.0;
1053 if (cur_spacing == Spacing::Other) {
1054 cur_value = par->params.spacing().getValue();
1057 std::istringstream istr(arg.c_str());
1060 Spacing::Space new_spacing = cur_spacing;
1061 float new_value = cur_value;
1063 lyxerr << "Missing argument to `paragraph-spacing'"
1065 } else if (tmp == "single") {
1066 new_spacing = Spacing::Single;
1067 } else if (tmp == "onehalf") {
1068 new_spacing = Spacing::Onehalf;
1069 } else if (tmp == "double") {
1070 new_spacing = Spacing::Double;
1071 } else if (tmp == "other") {
1072 new_spacing = Spacing::Other;
1075 lyxerr << "new_value = " << tmpval << endl;
1078 } else if (tmp == "default") {
1079 new_spacing = Spacing::Default;
1081 lyxerr << _("Unknown spacing argument: ")
1084 if (cur_spacing != new_spacing || cur_value != new_value) {
1085 par->params.spacing(Spacing(new_spacing, new_value));
1086 //TEXT(bv)->RedoParagraph(owner->view());
1087 UpdateLocal(bv, CURSOR_PAR, true);
1088 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1094 if (!bv->Dispatch(action, arg))
1095 result = UNDISPATCHED;
1099 /// If the action has deleted all text in the inset, we need to change the
1100 // language to the language to the surronding text.
1101 if (par->size() == 0 && !par->next()) {
1102 LyXFont font(LyXFont::ALL_IGNORE);
1103 font.setLanguage(bv->getParentLanguage(this));
1104 SetFont(bv, font, false);
1107 if (result != FINISHED) {
1108 ShowInsetCursor(bv);
1110 bv->unlockInset(this);
1115 int InsetText::Latex(Buffer const * buf, ostream & os, bool, bool) const
1118 buf->latexParagraphs(os, par, 0, texrow);
1119 return texrow.rows();
1123 int InsetText::Ascii(Buffer const * buf, ostream & os, int linelen) const
1125 LyXParagraph * p = par;
1126 unsigned int lines = 0;
1129 string const tmp = buf->asciiParagraph(p, linelen);
1130 lines += countChar(tmp, '\n');
1138 int InsetText::DocBook(Buffer const * buf, ostream & os) const
1140 LyXParagraph * p = par;
1141 unsigned int lines = 0;
1146 buf->SimpleDocBookOnePar(os, tmp, p, desc, 0);
1154 void InsetText::Validate(LaTeXFeatures & features) const
1156 LyXParagraph * p = par;
1158 p->validate(features);
1164 int InsetText::BeginningOfMainBody(Buffer const * buf, LyXParagraph * p) const
1166 if (textclasslist.Style(buf->params.textclass,
1167 p->GetLayout()).labeltype != LABEL_MANUAL)
1170 return p->BeginningOfMainBody();
1174 void InsetText::GetCursorPos(BufferView * bv,
1175 int & x, int & y) const
1182 unsigned int InsetText::InsetInInsetY()
1184 if (!the_locking_inset)
1187 return (inset_y + the_locking_inset->InsetInInsetY());
1191 void InsetText::ToggleInsetCursor(BufferView * bv)
1193 if (the_locking_inset) {
1194 the_locking_inset->ToggleInsetCursor(bv);
1198 LyXFont const font(TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv)));
1200 int const asc = lyxfont::maxAscent(font);
1201 int const desc = lyxfont::maxDescent(font);
1203 if (isCursorVisible())
1204 bv->hideLockedInsetCursor();
1206 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1207 toggleCursorVisible();
1211 void InsetText::ShowInsetCursor(BufferView * bv, bool show)
1213 if (the_locking_inset) {
1214 the_locking_inset->ShowInsetCursor(bv);
1217 if (!isCursorVisible()) {
1218 LyXFont const font =
1219 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1221 int const asc = lyxfont::maxAscent(font);
1222 int const desc = lyxfont::maxDescent(font);
1224 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1226 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1227 setCursorVisible(true);
1232 void InsetText::HideInsetCursor(BufferView * bv)
1234 if (isCursorVisible()) {
1235 bv->hideLockedInsetCursor();
1236 setCursorVisible(false);
1238 if (the_locking_inset)
1239 the_locking_inset->HideInsetCursor(bv);
1243 UpdatableInset::RESULT
1244 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1246 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1247 return moveLeftIntern(bv, false, activate_inset, selecting);
1249 return moveRightIntern(bv, false, activate_inset, selecting);
1253 UpdatableInset::RESULT
1254 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1256 if (TEXT(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1257 return moveRightIntern(bv, true, activate_inset, selecting);
1259 return moveLeftIntern(bv, true, activate_inset, selecting);
1263 UpdatableInset::RESULT
1264 InsetText::moveRightIntern(BufferView * bv, bool behind,
1265 bool activate_inset, bool selecting)
1267 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1269 if (activate_inset && checkAndActivateInset(bv, behind))
1271 TEXT(bv)->CursorRight(bv);
1273 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1274 return DISPATCHED_NOUPDATE;
1278 UpdatableInset::RESULT
1279 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1280 bool activate_inset, bool selecting)
1282 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1284 TEXT(bv)->CursorLeft(bv);
1286 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1287 if (activate_inset && checkAndActivateInset(bv, behind))
1289 return DISPATCHED_NOUPDATE;
1293 UpdatableInset::RESULT
1294 InsetText::moveUp(BufferView * bv)
1296 if (!crow(bv)->previous())
1298 TEXT(bv)->CursorUp(bv);
1299 return DISPATCHED_NOUPDATE;
1303 UpdatableInset::RESULT
1304 InsetText::moveDown(BufferView * bv)
1306 if (!crow(bv)->next())
1308 TEXT(bv)->CursorDown(bv);
1309 return DISPATCHED_NOUPDATE;
1313 bool InsetText::InsertInset(BufferView * bv, Inset * inset)
1315 if (the_locking_inset) {
1316 if (the_locking_inset->InsertInsetAllowed(inset))
1317 return the_locking_inset->InsertInset(bv, inset);
1320 bv->text->SetUndo(bv->buffer(), Undo::INSERT,
1321 bv->text->cursor.par()->previous(),
1322 bv->text->cursor.par()->next());
1323 inset->setOwner(this);
1324 HideInsetCursor(bv);
1325 TEXT(bv)->InsertInset(bv, inset);
1327 if ((cpar(bv)->GetChar(cpos(bv)) != LyXParagraph::META_INSET) ||
1328 (cpar(bv)->GetInset(cpos(bv)) != inset))
1329 TEXT(bv)->CursorLeft(bv);
1331 bv->fitCursor(TEXT(bv));
1332 UpdateLocal(bv, CURSOR_PAR|CURSOR, true);
1333 ShowInsetCursor(bv);
1338 UpdatableInset * InsetText::GetLockingInset()
1340 return the_locking_inset ? the_locking_inset->GetLockingInset() : this;
1344 UpdatableInset * InsetText::GetFirstLockingInsetOfType(Inset::Code c)
1348 if (the_locking_inset)
1349 return the_locking_inset->GetFirstLockingInsetOfType(c);
1354 bool InsetText::ShowInsetDialog(BufferView * bv) const
1356 if (the_locking_inset)
1357 return the_locking_inset->ShowInsetDialog(bv);
1362 std::vector<string> const InsetText::getLabelList() const
1364 std::vector<string> label_list;
1366 LyXParagraph * tpar = par;
1368 LyXParagraph::inset_iterator beg = tpar->inset_iterator_begin();
1369 LyXParagraph::inset_iterator end = tpar->inset_iterator_end();
1370 for (; beg != end; ++beg) {
1371 std::vector<string> const l = (*beg)->getLabelList();
1372 label_list.insert(label_list.end(), l.begin(), l.end());
1374 tpar = tpar->next();
1380 void InsetText::SetFont(BufferView * bv, LyXFont const & font, bool toggleall,
1383 if (the_locking_inset) {
1384 the_locking_inset->SetFont(bv, font, toggleall, selectall);
1387 if (TEXT(bv)->selection.set()) {
1388 bv->text->SetUndo(bv->buffer(), Undo::EDIT,
1389 bv->text->cursor.par()->previous(),
1390 bv->text->cursor.par()->next());
1394 TEXT(bv)->SetFont(bv, font, toggleall);
1396 TEXT(bv)->ClearSelection(bv);
1397 bv->fitCursor(TEXT(bv));
1398 if (selectall || TEXT(bv)->selection.set())
1399 UpdateLocal(bv, FULL, true);
1401 UpdateLocal(bv, CURSOR_PAR, true);
1405 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1407 if (cpar(bv)->GetChar(cpos(bv)) == LyXParagraph::META_INSET) {
1411 static_cast<UpdatableInset*>(cpar(bv)->GetInset(cpos(bv)));
1412 if (!inset || inset->Editable() != Inset::HIGHLY_EDITABLE)
1414 LyXFont const font =
1415 TEXT(bv)->GetFont(bv->buffer(), cpar(bv), cpos(bv));
1417 x = inset->width(bv, font);
1418 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1421 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1423 //inset_x = cx(bv) - top_x + drawTextXOffset;
1424 //inset_y = cy(bv) + drawTextYOffset;
1425 inset->Edit(bv, x, y, 0);
1426 if (!the_locking_inset)
1428 UpdateLocal(bv, CURSOR, false);
1435 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1440 dummyx = x = x - drawTextXOffset;
1441 dummyy = y + insetAscent;
1442 Inset * inset = bv->checkInsetHit(TEXT(bv), dummyx, dummyy, button);
1449 inset_x = cx(bv) - top_x + drawTextXOffset;
1450 inset_y = cy(bv) + drawTextYOffset;
1451 inset->Edit(bv, x - inset_x, y - inset_y, button);
1452 if (!the_locking_inset)
1454 UpdateLocal(bv, CURSOR, false);
1461 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1463 int w = UpdatableInset::getMaxWidth(bv, inset);
1468 w = w - top_x + owner()->x();
1471 w -= (2 * TEXT_TO_INSET_OFFSET);
1473 // return w - (2*TEXT_TO_INSET_OFFSET);
1477 void InsetText::SetParagraphData(LyXParagraph * p)
1479 // delete all instances of LyXText before deleting the paragraps used
1481 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1482 delete (*cit).second;
1487 LyXParagraph * tmp = par->next();
1492 par = new LyXParagraph(*p);
1493 par->SetInsetOwner(this);
1494 LyXParagraph * np = par;
1497 np->next(new LyXParagraph(*p));
1498 np->next()->previous(np);
1500 np->SetInsetOwner(this);
1507 void InsetText::SetText(string const & data)
1510 LyXFont font(LyXFont::ALL_SANE);
1511 for (unsigned int i=0; i < data.length(); ++i)
1512 par->InsertChar(i, data[i], font);
1516 void InsetText::SetAutoBreakRows(bool flag)
1518 if (flag != autoBreakRows) {
1519 autoBreakRows = flag;
1527 void InsetText::SetDrawFrame(BufferView * bv, DrawFrame how)
1529 if (how != drawFrame_) {
1532 UpdateLocal(bv, DRAW_FRAME, false);
1537 void InsetText::SetFrameColor(BufferView * bv, LColor::color col)
1539 if (frame_color != col) {
1542 UpdateLocal(bv, DRAW_FRAME, false);
1547 int InsetText::cx(BufferView * bv) const
1549 LyXText * text = TEXT(bv);
1550 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1551 if (the_locking_inset) {
1552 LyXFont font = text->GetFont(bv->buffer(),
1554 text->cursor.pos());
1555 if (font.isVisibleRightToLeft())
1556 x -= the_locking_inset->width(bv, font);
1562 int InsetText::cy(BufferView * bv) const
1565 return TEXT(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1569 LyXParagraph::size_type InsetText::cpos(BufferView * bv) const
1571 return TEXT(bv)->cursor.pos();
1575 LyXParagraph * InsetText::cpar(BufferView * bv) const
1577 return TEXT(bv)->cursor.par();
1581 bool InsetText::cboundary(BufferView * bv) const
1583 return TEXT(bv)->cursor.boundary();
1587 Row * InsetText::crow(BufferView * bv) const
1589 return TEXT(bv)->cursor.row();
1593 LyXText * InsetText::getLyXText(BufferView const * lbv,
1594 bool const recursive) const
1596 // Super UGLY! (Lgb)
1597 BufferView * bv = const_cast<BufferView *>(lbv);
1599 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1600 if (recursive && the_locking_inset)
1601 return the_locking_inset->getLyXText(bv);
1604 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1607 if (the_locking_inset) {
1608 lt->SetCursor(bv, inset_par, inset_pos, true, inset_boundary);
1610 return the_locking_inset->getLyXText(bv);
1616 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1618 if ((cache.find(bv) == cache.end()) || !cache[bv])
1623 /// then remove all LyXText in text-insets
1624 LyXParagraph * p = par;
1625 for (; p; p = p->next()) {
1626 p->deleteInsetsLyXText(bv);
1632 void InsetText::resizeLyXText(BufferView * bv) const
1634 if (!par->next() && !par->size()) // no data, resize not neccessary!
1636 if (getMaxWidth(bv, this) < 0) // one endless line, no resize necessary
1638 if ((cache.find(bv) == cache.end()) || !cache[bv])
1641 LyXParagraph * lpar = 0;
1642 LyXParagraph * selstartpar = 0;
1643 LyXParagraph * selendpar = 0;
1644 LyXParagraph::size_type pos = 0;
1645 LyXParagraph::size_type selstartpos = 0;
1646 LyXParagraph::size_type selendpos = 0;
1647 bool boundary = false;
1648 bool selstartboundary = false;
1649 bool selendboundary = false;
1650 bool selection = false;
1651 bool mark_set = false;
1653 // ProhibitInput(bv);
1656 lpar = TEXT(bv)->cursor.par();
1657 pos = TEXT(bv)->cursor.pos();
1658 boundary = TEXT(bv)->cursor.boundary();
1659 selstartpar = TEXT(bv)->selection.start.par();
1660 selstartpos = TEXT(bv)->selection.start.pos();
1661 selstartboundary = TEXT(bv)->selection.start.boundary();
1662 selendpar = TEXT(bv)->selection.end.par();
1663 selendpos = TEXT(bv)->selection.end.pos();
1664 selendboundary = TEXT(bv)->selection.end.boundary();
1665 selection = TEXT(bv)->selection.set();
1666 mark_set = TEXT(bv)->selection.mark();
1668 deleteLyXText(bv, (the_locking_inset == 0));
1671 TEXT(bv)->selection.set(true);
1672 /* at this point just to avoid the Delete-Empty-Paragraph
1673 * Mechanism when setting the cursor */
1674 TEXT(bv)->selection.mark(mark_set);
1676 TEXT(bv)->SetCursor(bv, selstartpar, selstartpos,
1677 true, selstartboundary);
1678 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1679 TEXT(bv)->SetCursor(bv, selendpar, selendpos,
1680 true, selendboundary);
1681 TEXT(bv)->SetSelection(bv);
1682 TEXT(bv)->SetCursor(bv, lpar, pos);
1684 TEXT(bv)->SetCursor(bv, lpar, pos, true, boundary);
1685 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1686 TEXT(bv)->selection.set(false);
1690 TEXT(bv)->first = bv->screen()->TopCursorVisible(TEXT(bv));
1691 // this will scroll the screen such that the cursor becomes visible
1692 bv->updateScrollbar();
1694 if (the_locking_inset) {
1695 /// then resize all LyXText in text-insets
1696 inset_x = cx(bv) - top_x + drawTextXOffset;
1697 inset_y = cy(bv) + drawTextYOffset;
1698 for (LyXParagraph * p = par; p; p = p->next()) {
1699 p->resizeInsetsLyXText(bv);
1706 void InsetText::removeNewlines()
1708 for (LyXParagraph * p = par; p; p = p->next()) {
1709 for (int i = 0; i < p->size(); ++i) {
1710 if (p->GetChar(i) == LyXParagraph::META_NEWLINE)
1717 bool InsetText::nodraw() const
1719 if (the_locking_inset)
1720 return the_locking_inset->nodraw();
1721 return UpdatableInset::nodraw();
1725 int InsetText::scroll(bool recursive) const
1727 int sx = UpdatableInset::scroll(false);
1729 if (recursive && the_locking_inset)
1730 sx += the_locking_inset->scroll(recursive);
1736 bool InsetText::doClearArea() const
1738 return !locked || (need_update & (FULL|INIT));
1741 void InsetText::selectAll(BufferView * bv)
1743 TEXT(bv)->CursorTop(bv);
1744 TEXT(bv)->selection.cursor = TEXT(bv)->cursor;
1745 TEXT(bv)->CursorBottom(bv);
1746 TEXT(bv)->SetSelection(bv);
1749 void InsetText::clearSelection(BufferView * bv)
1751 TEXT(bv)->ClearSelection(bv);
1754 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1757 int h = insetAscent + insetDescent;
1758 int ty = baseline - insetAscent;
1764 if ((ty + h) > pain.paperHeight())
1765 h = pain.paperHeight();
1766 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1767 w = pain.paperWidth();
1768 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);