1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
19 #pragma implementation
22 #include "insettext.h"
23 #include "paragraph.h"
27 #include "commandtags.h"
30 #include "BufferView.h"
32 #include "LaTeXFeatures.h"
34 #include "lyx_gui_misc.h"
36 #include "lyxcursor.h"
37 #include "CutAndPaste.h"
40 #include "support/textutils.h"
41 #include "support/LAssert.h"
45 #include "trans_mgr.h"
46 #include "lyxscreen.h"
50 #include "ParagraphParameters.h"
58 extern unsigned char getCurrentTextClass(Buffer *);
59 extern bool math_insert_greek(BufferView *, char);
60 extern int greek_kb_flag;
62 InsetText::InsetText()
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 Paragraph * 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 Paragraph * tmp = par->next();
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 Paragraph * return_par = 0;
177 Paragraph::depth_type depth = 0;
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 = getLyXText(bv)->getRowNearY(y_temp);
217 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
222 int InsetText::descent(BufferView * bv, LyXFont const &) const
224 LyXText * t = getLyXText(bv);
226 Row * row = t->getRowNearY(y_temp);
227 insetDescent = t->height - row->ascent_of_text() +
228 TEXT_TO_INSET_OFFSET;
233 int InsetText::width(BufferView * bv, LyXFont const &) const
235 insetWidth = max(textWidth(bv),
236 (int)getLyXText(bv)->width + (2 * TEXT_TO_INSET_OFFSET));
241 int InsetText::textWidth(BufferView * bv) const
243 int const w = getMaxWidth(bv, this);
248 void InsetText::draw(BufferView * bv, LyXFont const & f,
249 int baseline, float & x, bool cleared) const
254 Painter & pain = bv->painter();
256 // no draw is necessary !!!
257 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
259 top_baseline = baseline;
261 if (!cleared && (need_update & CLEAR_FRAME))
262 clearFrame(pain, cleared);
264 frame_is_visible = false;
271 UpdatableInset::draw(bv, f, baseline, x, cleared);
274 x += static_cast<float>(scroll());
277 // update insetWidth and insetHeight with dummy calls
279 (void)descent(bv, f);
283 // if top_x differs we have a rule down and we don't have to clear anything
284 if (!cleared && (top_x == int(x)) &&
285 ((need_update&(INIT|FULL)) || (top_baseline!=baseline) ||
286 (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) &&
297 (getLyXText(bv)->width != old_max_width)) {
300 old_max_width = getLyXText(bv)->width;
301 bv->text->status = LyXText::CHANGED_IN_DRAW;
305 clearInset(pain, baseline, cleared);
311 // lyxerr << "InsetText::draw[" << this << "](" << need_update << ":" << int(x) << ":" << top_x << ")\n";
313 if (cleared || (last_drawn_width != insetWidth)) {
315 last_drawn_width = insetWidth;
318 top_baseline = baseline;
319 top_y = baseline - ascent(bv, f);
320 last_width = width(bv, f);
321 last_height = ascent(bv, f) + descent(bv, f);
323 if (the_locking_inset && (cpar(bv) == inset_par)
324 && (cpos(bv) == inset_pos)) {
325 inset_x = cx(bv) - top_x + drawTextXOffset;
326 inset_y = cy(bv) + drawTextYOffset;
328 if (!cleared && (need_update == CURSOR)
329 && !getLyXText(bv)->selection.set()) {
330 drawFrame(pain, cleared);
331 x += last_width; // was width(bv, f);
335 x += TEXT_TO_INSET_OFFSET;
338 Row * row = getLyXText(bv)->getRowNearY(y);
339 int y_offset = baseline - row->ascent_of_text();
340 int ph = pain.paperHeight();
343 while ((row != 0) && ((y+row->height()) <= 0)) {
345 first += row->height();
350 getLyXText(bv)->first = first;
354 while ((row != 0) && (yf < ph)) {
355 getLyXText(bv)->getVisibleRow(bv, y+y_offset, int(x), row,
361 } else if (!locked) {
362 if (need_update & CURSOR) {
363 bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
364 getLyXText(bv)->clearSelection(bv);
365 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
367 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
370 if (need_update & SELECTION)
371 bv->screen()->ToggleToggle(getLyXText(bv), bv, y_offset, int(x));
372 else if (need_update & CURSOR) {
373 bv->screen()->ToggleSelection(getLyXText(bv), bv, true, y_offset,int(x));
374 getLyXText(bv)->clearSelection(bv);
375 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
377 bv->screen()->Update(getLyXText(bv), bv, y_offset, int(x));
381 getLyXText(bv)->refresh_y = 0;
382 getLyXText(bv)->status = LyXText::UNCHANGED;
383 if ((need_update != CURSOR_PAR) &&
384 ((drawFrame_ == ALWAYS) || ((drawFrame_ == LOCKED) && locked)))
385 drawFrame(pain, cleared);
386 else if (need_update & CLEAR_FRAME)
387 clearFrame(pain, cleared);
388 x += last_width /* was width(bv, f) */ - TEXT_TO_INSET_OFFSET;
389 if (bv->text->status==LyXText::CHANGED_IN_DRAW) {
391 } else if (need_update != INIT)
396 void InsetText::drawFrame(Painter & pain, bool cleared) const
398 if (!frame_is_visible || cleared) {
399 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
400 insetWidth - 1, insetAscent + insetDescent - 1,
402 frame_is_visible = true;
407 void InsetText::clearFrame(Painter & pain, bool cleared) const
409 if (frame_is_visible) {
411 pain.rectangle(top_x + 1, top_baseline - insetAscent + 1,
412 insetWidth - 1, insetAscent + insetDescent - 1,
415 frame_is_visible = false;
420 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
422 LyXText * t = getLyXText(bv);
426 t->BreakParagraph(bv);
434 owner()->update(bv, font, true);
437 if (the_locking_inset) {
438 inset_x = cx(bv) - top_x + drawTextXOffset;
439 inset_y = cy(bv) + drawTextYOffset;
440 the_locking_inset->update(bv, font, reinit);
443 if (need_update == INIT) {
448 int oldw = insetWidth;
449 insetWidth = t->width + (2 * TEXT_TO_INSET_OFFSET);
450 if (oldw != insetWidth) {
453 // update(bv, font, reinit);
456 if ((need_update & CURSOR_PAR) && t->status == LyXText::UNCHANGED &&
458 t->updateInset(bv, the_locking_inset);
461 if (t->status == LyXText::NEED_MORE_REFRESH)
465 Row * row = t->GetRowNearY(y_temp);
466 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
467 insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
472 void InsetText::setUpdateStatus(BufferView * bv, int what) const
474 LyXText * t = getLyXText(bv);
476 if (t->status == LyXText::NEED_MORE_REFRESH)
478 else if (t->status == LyXText::NEED_VERY_LITTLE_REFRESH)
479 need_update |= CURSOR_PAR;
481 // this to not draw a selection when we redraw all of it!
482 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
483 t->clearSelection(bv);
487 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
489 LyXText * t = getLyXText(bv);
491 setUpdateStatus(bv, what);
492 if (need_update != CURSOR || t->status != LyXText::UNCHANGED ||
494 bv->updateInset(this, mark_dirty);
495 bv->owner()->showState();
496 if (old_par != cpar(bv)) {
497 bv->owner()->setLayout(cpar(bv)->getLayout());
503 string const InsetText::editMessage() const
505 return _("Opened Text Inset");
509 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
511 // par->SetInsetOwner(this);
512 UpdatableInset::edit(bv, x, y, button);
514 if (!bv->lockInset(this)) {
515 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
519 the_locking_inset = 0;
520 inset_pos = inset_x = inset_y = 0;
521 inset_boundary = false;
524 int tmp_y = (y < 0) ? 0 : y;
525 LyXText * t = getLyXText(bv);
527 if (!checkAndActivateInset(bv, x, tmp_y, button)) {
528 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
532 t->selection.cursor = t->cursor;
533 bv->text->finishUndo();
535 updateLocal(bv, CURSOR, false);
537 // If the inset is empty set the language of the current font to the
538 // language to the surronding text (if different).
539 if (par->size() == 0 && !par->next() &&
540 bv->getParentLanguage(this) != t->current_font.language()) {
541 LyXFont font(LyXFont::ALL_IGNORE);
542 font.setLanguage(bv->getParentLanguage(this));
543 setFont(bv, font, false);
548 void InsetText::insetUnlock(BufferView * bv)
550 if (the_locking_inset) {
551 the_locking_inset->insetUnlock(bv);
552 the_locking_inset = 0;
555 no_selection = false;
557 int code = CURSOR|CLEAR_FRAME;
558 LyXText * t = getLyXText(bv);
559 if (t->selection.set()) {
560 t->clearSelection(bv);
562 } else if (owner()) {
563 bv->owner()->setLayout(owner()->getLyXText(bv)
564 ->cursor.par()->getLayout());
566 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
567 updateLocal(bv, code, false);
571 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
573 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
577 if (inset == cpar(bv)->getInset(cpos(bv))) {
578 lyxerr[Debug::INSETS] << "OK" << endl;
579 the_locking_inset = inset;
580 inset_x = cx(bv) - top_x + drawTextXOffset;
581 inset_y = cy(bv) + drawTextYOffset;
582 inset_pos = cpos(bv);
583 inset_par = cpar(bv);
584 inset_boundary = cboundary(bv);
586 getLyXText(bv)->clearSelection(bv);
587 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
588 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
590 updateLocal(bv, CURSOR, false);
593 } else if (the_locking_inset && (the_locking_inset == inset)) {
594 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
595 lyxerr[Debug::INSETS] << "OK" << endl;
596 inset_x = cx(bv) - top_x + drawTextXOffset;
597 inset_y = cy(bv) + drawTextYOffset;
599 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
601 } else if (the_locking_inset) {
602 lyxerr[Debug::INSETS] << "MAYBE" << endl;
603 return the_locking_inset->lockInsetInInset(bv, inset);
605 lyxerr[Debug::INSETS] << "NOT OK" << endl;
610 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
613 if (!the_locking_inset)
615 if (the_locking_inset == inset) {
616 the_locking_inset->insetUnlock(bv);
617 getLyXText(bv)->updateInset(bv, inset);
618 the_locking_inset = 0;
620 moveRight(bv, false);
621 old_par = 0; // force layout setting
625 updateLocal(bv, CURSOR, false);
628 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
632 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
634 if (!the_locking_inset)
636 if (the_locking_inset != inset) {
637 getLyXText(bv)->updateInset(bv, the_locking_inset);
638 setUpdateStatus(bv, CURSOR_PAR);
639 return the_locking_inset->updateInsetInInset(bv, inset);
641 // updateLocal(bv, FULL, false);
642 if (getLyXText(bv)->updateInset(bv, inset))
643 updateLocal(bv, CURSOR_PAR, false);
644 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
645 inset_x = cx(bv) - top_x + drawTextXOffset;
646 inset_y = cy(bv) + drawTextYOffset;
652 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
656 int tmp_x = x - drawTextXOffset;
657 int tmp_y = y + insetAscent - getLyXText(bv)->first;
658 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
661 if (the_locking_inset) {
662 if (the_locking_inset == inset) {
663 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
664 no_selection = false;
667 // otherwise unlock the_locking_inset and lock the new inset
668 the_locking_inset->insetUnlock(bv);
669 inset_x = cx(bv) - top_x + drawTextXOffset;
670 inset_y = cy(bv) + drawTextYOffset;
671 the_locking_inset = static_cast<UpdatableInset*>(inset);
672 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
673 inset->edit(bv, x - inset_x, y - inset_y, button);
674 if (the_locking_inset)
675 updateLocal(bv, CURSOR, false);
676 no_selection = false;
679 // otherwise only unlock the_locking_inset
680 the_locking_inset->insetUnlock(bv);
681 the_locking_inset = 0;
683 if (bv->theLockingInset()) {
684 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
685 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
686 inset_x = cx(bv) - top_x + drawTextXOffset;
687 inset_y = cy(bv) + drawTextYOffset;
688 inset_pos = cpos(bv);
689 inset_par = cpar(bv);
690 inset_boundary = cboundary(bv);
691 the_locking_inset = uinset;
692 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
694 uinset->edit(bv, x - inset_x, y - inset_y, 0);
695 if (the_locking_inset)
696 updateLocal(bv, CURSOR, false);
697 no_selection = false;
701 if (!inset) { // && (button == 2)) {
702 bool paste_internally = false;
703 if ((button == 2) && getLyXText(bv)->selection.set()) {
704 localDispatch(bv, LFUN_COPY, "");
705 paste_internally = true;
707 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
709 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
710 updateLocal(bv, CURSOR, false);
711 bv->owner()->setLayout(cpar(bv)->getLayout());
713 // Insert primary selection with middle mouse
714 // if there is a local selection in the current buffer,
717 if (paste_internally)
718 localDispatch(bv, LFUN_PASTE, "");
720 localDispatch(bv, LFUN_PASTESELECTION,
725 no_selection = false;
729 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
731 UpdatableInset * inset = 0;
733 if (the_locking_inset) {
734 the_locking_inset->insetButtonRelease(bv,
735 x - inset_x, y - inset_y,
738 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
739 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
740 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
741 inset->insetButtonRelease(bv,
743 y - inset_y, button);
745 inset_x = cx(bv) - top_x + drawTextXOffset;
746 inset_y = cy(bv) + drawTextYOffset;
747 inset->insetButtonRelease(bv,
749 y - inset_y, button);
751 x - inset_x, y - inset_y, button);
753 updateLocal(bv, CURSOR_PAR, false);
756 no_selection = false;
760 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
764 if (the_locking_inset) {
765 the_locking_inset->insetMotionNotify(bv, x - inset_x,
769 LyXText * t = getLyXText(bv);
771 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
773 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
774 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
775 updateLocal(bv, SELECTION, false);
780 void InsetText::insetKeyPress(XKeyEvent * xke)
782 if (the_locking_inset) {
783 the_locking_inset->insetKeyPress(xke);
789 UpdatableInset::RESULT
790 InsetText::localDispatch(BufferView * bv,
791 kb_action action, string const & arg)
793 no_selection = false;
794 UpdatableInset::RESULT
795 result= UpdatableInset::localDispatch(bv, action, arg);
796 if (result != UNDISPATCHED) {
801 if ((action < 0) && arg.empty())
804 if (the_locking_inset) {
805 result = the_locking_inset->localDispatch(bv, action, arg);
806 if (result == DISPATCHED_NOUPDATE)
808 else if (result == DISPATCHED) {
809 updateLocal(bv, CURSOR_PAR, false);
811 } else if (result == FINISHED) {
812 bool dispatched = false;
814 case LFUN_UNKNOWN_ACTION:
815 case LFUN_BREAKPARAGRAPH:
817 moveRightIntern(bv, false, false);
820 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
821 moveRightIntern(bv, false, false);
825 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
826 moveRightIntern(bv, false, false);
832 the_locking_inset = 0;
840 case LFUN_SELFINSERT:
841 if (bv->buffer()->isReadonly()) {
842 // setErrorMessage(N_("Document is read only"));
846 /* Automatically delete the currently selected
847 * text and replace it with what is being
848 * typed in now. Depends on lyxrc settings
849 * "auto_region_delete", which defaults to
852 bv->text->setUndo(bv->buffer(), Undo::INSERT,
853 bv->text->cursor.par()->previous(),
854 bv->text->cursor.par()->next());
856 if (lyxrc.auto_region_delete) {
857 if (getLyXText(bv)->selection.set()) {
858 getLyXText(bv)->cutSelection(bv, false);
861 getLyXText(bv)->clearSelection(bv);
862 for (string::size_type i = 0; i < arg.length(); ++i) {
864 if (!math_insert_greek(bv, arg[i])) {
865 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
866 } else if (!the_locking_inset) {
867 (void)moveRight(bv, false);
870 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
874 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
875 updateLocal(bv, CURSOR_PAR, true);
876 result = DISPATCHED_NOUPDATE;
878 // --- Cursor Movements -----------------------------------
880 bv->text->finishUndo();
881 moveRight(bv, false, true);
882 getLyXText(bv)->setSelection(bv);
883 updateLocal(bv, SELECTION, false);
886 result = moveRight(bv);
887 bv->text->finishUndo();
888 updateLocal(bv, CURSOR, false);
891 bv->text->finishUndo();
892 moveLeft(bv, false, true);
893 getLyXText(bv)->setSelection(bv);
894 updateLocal(bv, SELECTION, false);
897 bv->text->finishUndo();
898 result = moveLeft(bv);
899 updateLocal(bv, CURSOR, false);
902 bv->text->finishUndo();
904 getLyXText(bv)->setSelection(bv);
905 updateLocal(bv, SELECTION, false);
908 bv->text->finishUndo();
909 result = moveDown(bv);
910 updateLocal(bv, CURSOR, false);
913 bv->text->finishUndo();
915 getLyXText(bv)->setSelection(bv);
916 updateLocal(bv, SELECTION, false);
919 bv->text->finishUndo();
921 updateLocal(bv, CURSOR, false);
924 bv->text->finishUndo();
925 getLyXText(bv)->cursorHome(bv);
926 updateLocal(bv, CURSOR, false);
929 getLyXText(bv)->cursorEnd(bv);
930 updateLocal(bv, CURSOR, false);
932 case LFUN_BACKSPACE: {
933 bv->text->setUndo(bv->buffer(), Undo::DELETE,
934 bv->text->cursor.par()->previous(),
935 bv->text->cursor.par()->next());
936 LyXText * t = getLyXText(bv);
938 if (t->selection.set()) {
944 updateLocal(bv, CURSOR_PAR, true);
950 bv->text->setUndo(bv->buffer(), Undo::DELETE,
951 bv->text->cursor.par()->previous(),
952 bv->text->cursor.par()->next());
953 LyXText * t = getLyXText(bv);
955 if (t->selection.set()) {
960 updateLocal(bv, CURSOR_PAR, true);
965 bv->text->setUndo(bv->buffer(), Undo::DELETE,
966 bv->text->cursor.par()->previous(),
967 bv->text->cursor.par()->next());
968 getLyXText(bv)->cutSelection(bv);
969 updateLocal(bv, CURSOR_PAR, true);
972 bv->text->finishUndo();
973 getLyXText(bv)->copySelection(bv);
974 updateLocal(bv, CURSOR_PAR, false);
976 case LFUN_PASTESELECTION:
978 string const clip(bv->getClipboard());
982 if (arg == "paragraph") {
983 getLyXText(bv)->insertStringAsParagraphs(bv, clip);
985 getLyXText(bv)->insertStringAsLines(bv, clip);
987 updateLocal(bv, CURSOR_PAR, true);
991 if (!autoBreakRows) {
994 if (cap.nrOfParagraphs() > 1) {
995 WriteAlert(_("Impossible operation"),
996 _("Cannot include more than one paragraph!"),
1001 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1002 bv->text->cursor.par()->previous(),
1003 bv->text->cursor.par()->next());
1004 getLyXText(bv)->pasteSelection(bv);
1005 updateLocal(bv, CURSOR_PAR, true);
1007 case LFUN_BREAKPARAGRAPH:
1010 getLyXText(bv)->breakParagraph(bv, 0);
1011 updateLocal(bv, FULL, true);
1013 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1016 getLyXText(bv)->breakParagraph(bv, 1);
1017 updateLocal(bv, FULL, true);
1019 case LFUN_BREAKLINE:
1022 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1023 bv->text->cursor.par()->previous(),
1024 bv->text->cursor.par()->next());
1025 getLyXText(bv)->insertChar(bv, Paragraph::META_NEWLINE);
1026 updateLocal(bv, CURSOR_PAR, true);
1029 // do not set layouts on non breakable textinsets
1030 if (autoBreakRows) {
1031 LyXTextClass::size_type cur_layout = cpar(bv)->layout;
1033 // Derive layout number from given argument (string)
1034 // and current buffer's textclass (number). */
1035 LyXTextClassList::ClassList::size_type tclass =
1036 bv->buffer()->params.textclass;
1037 std::pair <bool, LyXTextClass::size_type> layout =
1038 textclasslist.NumberOfLayout(tclass, arg);
1040 // If the entry is obsolete, use the new one instead.
1042 string obs = textclasslist.Style(tclass,layout.second).
1045 layout = textclasslist.NumberOfLayout(tclass, obs);
1048 // see if we found the layout number:
1049 if (!layout.first) {
1050 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1051 bv->owner()->getLyXFunc()->Dispatch(LFUN_MESSAGE, msg);
1055 if (cur_layout != layout.second) {
1056 cur_layout = layout.second;
1057 getLyXText(bv)->setLayout(bv, layout.second);
1058 bv->owner()->setLayout(cpar(bv)->getLayout());
1059 updateLocal(bv, CURSOR_PAR, true);
1062 // reset the layout box
1063 bv->owner()->setLayout(cpar(bv)->getLayout());
1066 case LFUN_PARAGRAPH_SPACING:
1067 // This one is absolutely not working. When fiddling with this
1068 // it also seems to me that the paragraphs inside the insettext
1069 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1071 Paragraph * par = getLyXText(bv)->cursor.par();
1072 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1073 float cur_value = 1.0;
1074 if (cur_spacing == Spacing::Other) {
1075 cur_value = par->params().spacing().getValue();
1078 std::istringstream istr(arg.c_str());
1081 Spacing::Space new_spacing = cur_spacing;
1082 float new_value = cur_value;
1084 lyxerr << "Missing argument to `paragraph-spacing'"
1086 } else if (tmp == "single") {
1087 new_spacing = Spacing::Single;
1088 } else if (tmp == "onehalf") {
1089 new_spacing = Spacing::Onehalf;
1090 } else if (tmp == "double") {
1091 new_spacing = Spacing::Double;
1092 } else if (tmp == "other") {
1093 new_spacing = Spacing::Other;
1096 lyxerr << "new_value = " << tmpval << endl;
1099 } else if (tmp == "default") {
1100 new_spacing = Spacing::Default;
1102 lyxerr << _("Unknown spacing argument: ")
1105 if (cur_spacing != new_spacing || cur_value != new_value) {
1106 par->params().spacing(Spacing(new_spacing, new_value));
1107 //getLyXText(bv)->RedoParagraph(owner->view());
1108 updateLocal(bv, CURSOR_PAR, true);
1109 //bv->update(BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1115 if (!bv->Dispatch(action, arg))
1116 result = UNDISPATCHED;
1120 /// If the action has deleted all text in the inset, we need to change the
1121 // language to the language to the surronding text.
1122 if (par->size() == 0 && !par->next()) {
1123 LyXFont font(LyXFont::ALL_IGNORE);
1124 font.setLanguage(bv->getParentLanguage(this));
1125 setFont(bv, font, false);
1128 if (result != FINISHED) {
1129 showInsetCursor(bv);
1131 bv->unlockInset(this);
1136 int InsetText::latex(Buffer const * buf, ostream & os, bool, bool) const
1139 buf->latexParagraphs(os, par, 0, texrow);
1140 return texrow.rows();
1144 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1146 Paragraph * p = par;
1147 unsigned int lines = 0;
1150 string const tmp = buf->asciiParagraph(p, linelen);
1151 lines += countChar(tmp, '\n');
1159 int InsetText::docBook(Buffer const * buf, ostream & os) const
1161 Paragraph * p = par;
1162 unsigned int lines = 0;
1167 buf->simpleDocBookOnePar(os, tmp, p, desc, 0);
1175 void InsetText::validate(LaTeXFeatures & features) const
1177 Paragraph * p = par;
1179 p->validate(features);
1185 int InsetText::beginningOfMainBody(Buffer const * buf, Paragraph * p) const
1187 if (textclasslist.Style(buf->params.textclass,
1188 p->getLayout()).labeltype != LABEL_MANUAL)
1191 return p->beginningOfMainBody();
1195 void InsetText::getCursorPos(BufferView * bv,
1196 int & x, int & y) const
1203 unsigned int InsetText::insetInInsetY()
1205 if (!the_locking_inset)
1208 return (inset_y + the_locking_inset->insetInInsetY());
1212 void InsetText::toggleInsetCursor(BufferView * bv)
1214 if (the_locking_inset) {
1215 the_locking_inset->toggleInsetCursor(bv);
1219 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1221 int const asc = lyxfont::maxAscent(font);
1222 int const desc = lyxfont::maxDescent(font);
1224 if (isCursorVisible())
1225 bv->hideLockedInsetCursor();
1227 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1228 toggleCursorVisible();
1232 void InsetText::showInsetCursor(BufferView * bv, bool show)
1234 if (the_locking_inset) {
1235 the_locking_inset->showInsetCursor(bv);
1238 if (!isCursorVisible()) {
1239 LyXFont const font =
1240 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1242 int const asc = lyxfont::maxAscent(font);
1243 int const desc = lyxfont::maxDescent(font);
1245 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1247 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1248 setCursorVisible(true);
1253 void InsetText::hideInsetCursor(BufferView * bv)
1255 if (isCursorVisible()) {
1256 bv->hideLockedInsetCursor();
1257 setCursorVisible(false);
1259 if (the_locking_inset)
1260 the_locking_inset->hideInsetCursor(bv);
1264 UpdatableInset::RESULT
1265 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1267 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1268 return moveLeftIntern(bv, false, activate_inset, selecting);
1270 return moveRightIntern(bv, false, activate_inset, selecting);
1274 UpdatableInset::RESULT
1275 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1277 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1278 return moveRightIntern(bv, true, activate_inset, selecting);
1280 return moveLeftIntern(bv, true, activate_inset, selecting);
1284 UpdatableInset::RESULT
1285 InsetText::moveRightIntern(BufferView * bv, bool behind,
1286 bool activate_inset, bool selecting)
1288 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1290 if (activate_inset && checkAndActivateInset(bv, behind))
1292 getLyXText(bv)->cursorRight(bv);
1294 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1295 return DISPATCHED_NOUPDATE;
1299 UpdatableInset::RESULT
1300 InsetText::moveLeftIntern(BufferView * bv, bool behind,
1301 bool activate_inset, bool selecting)
1303 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1305 getLyXText(bv)->cursorLeft(bv);
1307 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1308 if (activate_inset && checkAndActivateInset(bv, behind))
1310 return DISPATCHED_NOUPDATE;
1314 UpdatableInset::RESULT
1315 InsetText::moveUp(BufferView * bv)
1317 if (!crow(bv)->previous())
1319 getLyXText(bv)->cursorUp(bv);
1320 return DISPATCHED_NOUPDATE;
1324 UpdatableInset::RESULT
1325 InsetText::moveDown(BufferView * bv)
1327 if (!crow(bv)->next())
1329 getLyXText(bv)->cursorDown(bv);
1330 return DISPATCHED_NOUPDATE;
1334 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1336 if (the_locking_inset) {
1337 if (the_locking_inset->insertInsetAllowed(inset))
1338 return the_locking_inset->insertInset(bv, inset);
1341 bv->text->setUndo(bv->buffer(), Undo::INSERT,
1342 bv->text->cursor.par()->previous(),
1343 bv->text->cursor.par()->next());
1344 inset->setOwner(this);
1345 hideInsetCursor(bv);
1346 getLyXText(bv)->insertInset(bv, inset);
1348 if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1349 (cpar(bv)->GetInset(cpos(bv)) != inset))
1350 getLyXText(bv)->CursorLeft(bv);
1352 bv->fitCursor(getLyXText(bv));
1353 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1354 showInsetCursor(bv);
1359 UpdatableInset * InsetText::getLockingInset()
1361 return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1365 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1369 if (the_locking_inset)
1370 return the_locking_inset->getFirstLockingInsetOfType(c);
1375 bool InsetText::showInsetDialog(BufferView * bv) const
1377 if (the_locking_inset)
1378 return the_locking_inset->showInsetDialog(bv);
1383 std::vector<string> const InsetText::getLabelList() const
1385 std::vector<string> label_list;
1387 Paragraph * tpar = par;
1389 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1390 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1391 for (; beg != end; ++beg) {
1392 std::vector<string> const l = (*beg)->getLabelList();
1393 label_list.insert(label_list.end(), l.begin(), l.end());
1395 tpar = tpar->next();
1401 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1404 if (the_locking_inset) {
1405 the_locking_inset->setFont(bv, font, toggleall, selectall);
1408 if (getLyXText(bv)->selection.set()) {
1409 bv->text->setUndo(bv->buffer(), Undo::EDIT,
1410 bv->text->cursor.par()->previous(),
1411 bv->text->cursor.par()->next());
1415 getLyXText(bv)->setFont(bv, font, toggleall);
1417 getLyXText(bv)->clearSelection(bv);
1418 bv->fitCursor(getLyXText(bv));
1419 if (selectall || getLyXText(bv)->selection.set())
1420 updateLocal(bv, FULL, true);
1422 updateLocal(bv, CURSOR_PAR, true);
1426 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1428 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1432 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1433 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1435 LyXFont const font =
1436 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1438 x = inset->width(bv, font);
1439 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1442 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1444 //inset_x = cx(bv) - top_x + drawTextXOffset;
1445 //inset_y = cy(bv) + drawTextYOffset;
1446 inset->edit(bv, x, y, 0);
1447 if (!the_locking_inset)
1449 updateLocal(bv, CURSOR, false);
1456 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1459 x -= drawTextXOffset;
1461 int dummyy = y + insetAscent;
1462 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1469 inset_x = cx(bv) - top_x + drawTextXOffset;
1470 inset_y = cy(bv) + drawTextYOffset;
1471 inset->edit(bv, x - inset_x, y - inset_y, button);
1472 if (!the_locking_inset)
1474 updateLocal(bv, CURSOR, false);
1481 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1483 int w = UpdatableInset::getMaxWidth(bv, inset);
1488 w = w - top_x + owner()->x();
1491 w -= (2 * TEXT_TO_INSET_OFFSET);
1493 // return w - (2*TEXT_TO_INSET_OFFSET);
1497 void InsetText::setParagraphData(Paragraph * p)
1499 // delete all instances of LyXText before deleting the paragraps used
1501 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit){
1502 delete (*cit).second;
1507 Paragraph * tmp = par->next();
1512 par = new Paragraph(*p);
1513 par->setInsetOwner(this);
1514 Paragraph * np = par;
1517 np->next(new Paragraph(*p));
1518 np->next()->previous(np);
1520 np->setInsetOwner(this);
1527 void InsetText::setText(string const & data)
1530 LyXFont font(LyXFont::ALL_SANE);
1531 for (unsigned int i=0; i < data.length(); ++i)
1532 par->insertChar(i, data[i], font);
1536 void InsetText::setAutoBreakRows(bool flag)
1538 if (flag != autoBreakRows) {
1539 autoBreakRows = flag;
1547 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1549 if (how != drawFrame_) {
1552 updateLocal(bv, DRAW_FRAME, false);
1557 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1559 if (frame_color != col) {
1562 updateLocal(bv, DRAW_FRAME, false);
1567 int InsetText::cx(BufferView * bv) const
1569 LyXText * text = getLyXText(bv);
1570 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1571 if (the_locking_inset) {
1572 LyXFont font = text->getFont(bv->buffer(),
1574 text->cursor.pos());
1575 if (font.isVisibleRightToLeft())
1576 x -= the_locking_inset->width(bv, font);
1582 int InsetText::cy(BufferView * bv) const
1585 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1589 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1591 return getLyXText(bv)->cursor.pos();
1595 Paragraph * InsetText::cpar(BufferView * bv) const
1597 return getLyXText(bv)->cursor.par();
1601 bool InsetText::cboundary(BufferView * bv) const
1603 return getLyXText(bv)->cursor.boundary();
1607 Row * InsetText::crow(BufferView * bv) const
1609 return getLyXText(bv)->cursor.row();
1613 LyXText * InsetText::getLyXText(BufferView const * lbv,
1614 bool const recursive) const
1616 // Super UGLY! (Lgb)
1617 BufferView * bv = const_cast<BufferView *>(lbv);
1619 if ((cache.find(bv) != cache.end()) && cache[bv]) {
1620 if (recursive && the_locking_inset)
1621 return the_locking_inset->getLyXText(bv);
1624 LyXText * lt = new LyXText(const_cast<InsetText *>(this));
1627 if (the_locking_inset) {
1628 lt->setCursor(bv, inset_par, inset_pos, true, inset_boundary);
1630 return the_locking_inset->getLyXText(bv);
1636 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1638 if ((cache.find(bv) == cache.end()) || !cache[bv])
1643 /// then remove all LyXText in text-insets
1644 Paragraph * p = par;
1645 for (; p; p = p->next()) {
1646 p->deleteInsetsLyXText(bv);
1652 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1654 if (!par->next() && !par->size()) // no data, resize not neccessary!
1656 // one endless line, resize normally not necessary
1657 if (!force && getMaxWidth(bv, this) < 0)
1659 if ((cache.find(bv) == cache.end()) || !cache[bv])
1662 Paragraph * lpar = 0;
1663 Paragraph * selstartpar = 0;
1664 Paragraph * selendpar = 0;
1665 Paragraph::size_type pos = 0;
1666 Paragraph::size_type selstartpos = 0;
1667 Paragraph::size_type selendpos = 0;
1668 bool boundary = false;
1669 bool selstartboundary = false;
1670 bool selendboundary = false;
1671 bool selection = false;
1672 bool mark_set = false;
1674 // ProhibitInput(bv);
1677 LyXText * t = getLyXText(bv);
1678 lpar = t->cursor.par();
1679 pos = t->cursor.pos();
1680 boundary = t->cursor.boundary();
1681 selstartpar = t->selection.start.par();
1682 selstartpos = t->selection.start.pos();
1683 selstartboundary = t->selection.start.boundary();
1684 selendpar = t->selection.end.par();
1685 selendpos = t->selection.end.pos();
1686 selendboundary = t->selection.end.boundary();
1687 selection = t->selection.set();
1688 mark_set = t->selection.mark();
1690 deleteLyXText(bv, (the_locking_inset == 0) || force);
1693 LyXText * t = getLyXText(bv);
1695 t->selection.set(true);
1696 /* at this point just to avoid the Delete-Empty-Paragraph
1697 * Mechanism when setting the cursor */
1698 t->selection.mark(mark_set);
1700 t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1701 t->selection.cursor = t->cursor;
1702 t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1703 t->setSelection(bv);
1704 t->setCursor(bv, lpar, pos);
1706 t->setCursor(bv, lpar, pos, true, boundary);
1707 t->selection.cursor = t->cursor;
1708 t->selection.set(false);
1712 LyXText * t = getLyXText(bv);
1713 t->first = bv->screen()->TopCursorVisible(t);
1716 // this will scroll the screen such that the cursor becomes visible
1717 bv->updateScrollbar();
1719 if (the_locking_inset) {
1720 /// then resize all LyXText in text-insets
1721 inset_x = cx(bv) - top_x + drawTextXOffset;
1722 inset_y = cy(bv) + drawTextYOffset;
1723 for (Paragraph * p = par; p; p = p->next()) {
1724 p->resizeInsetsLyXText(bv);
1731 void InsetText::removeNewlines()
1733 for (Paragraph * p = par; p; p = p->next()) {
1734 for (int i = 0; i < p->size(); ++i) {
1735 if (p->getChar(i) == Paragraph::META_NEWLINE)
1742 bool InsetText::nodraw() const
1744 if (the_locking_inset)
1745 return the_locking_inset->nodraw();
1746 return UpdatableInset::nodraw();
1750 int InsetText::scroll(bool recursive) const
1752 int sx = UpdatableInset::scroll(false);
1754 if (recursive && the_locking_inset)
1755 sx += the_locking_inset->scroll(recursive);
1761 bool InsetText::doClearArea() const
1763 return !locked || (need_update & (FULL|INIT));
1767 void InsetText::selectAll(BufferView * bv)
1769 getLyXText(bv)->cursorTop(bv);
1770 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1771 getLyXText(bv)->cursorBottom(bv);
1772 getLyXText(bv)->setSelection(bv);
1776 void InsetText::clearSelection(BufferView * bv)
1778 getLyXText(bv)->clearSelection(bv);
1782 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1785 int h = insetAscent + insetDescent;
1786 int ty = baseline - insetAscent;
1792 if ((ty + h) > pain.paperHeight())
1793 h = pain.paperHeight();
1794 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1795 w = pain.paperWidth();
1796 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);