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"
51 #include "undo_funcs.h"
60 extern unsigned char getCurrentTextClass(Buffer *);
61 extern bool math_insert_greek(BufferView *, char);
62 extern int greek_kb_flag;
64 InsetText::InsetText()
71 InsetText::InsetText(InsetText const & ins, bool same_id)
76 autoBreakRows = ins.autoBreakRows;
80 InsetText & InsetText::operator=(InsetText const & it)
83 autoBreakRows = it.autoBreakRows;
88 void InsetText::init(InsetText const * ins, bool same_id)
96 the_locking_inset = 0;
102 autoBreakRows = false;
106 setParagraphData(ins->par);
107 autoBreakRows = ins->autoBreakRows;
108 drawFrame_ = ins->drawFrame_;
112 par->setInsetOwner(this);
113 frame_color = LColor::insetframe;
116 last_drawn_width = -1;
117 frame_is_visible = false;
122 InsetText::~InsetText()
129 Paragraph * tmp = par->next();
136 void InsetText::clear()
140 // now also delete all caches this should be safe, hopefully
144 Paragraph * tmp = par->next();
152 Inset * InsetText::clone(Buffer const &, bool same_id) const
154 InsetText * t = new InsetText(*this, same_id);
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(bv, 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(bv, 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) &&
459 t->updateInset(bv, the_locking_inset);
462 if (t->status() == LyXText::NEED_MORE_REFRESH)
466 Row * row = t->GetRowNearY(y_temp);
467 insetAscent = row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
468 insetDescent = t->height - row->ascent_of_text() + TEXT_TO_INSET_OFFSET;
473 void InsetText::setUpdateStatus(BufferView * bv, int what) const
475 LyXText * t = getLyXText(bv);
477 if (t->status() == LyXText::NEED_MORE_REFRESH)
479 else if (t->status() == LyXText::NEED_VERY_LITTLE_REFRESH)
480 need_update |= CURSOR_PAR;
482 // this to not draw a selection when we redraw all of it!
483 if ((need_update & (INIT|FULL)) && (need_update & CURSOR))
484 t->clearSelection(bv);
488 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty)
490 LyXText * t = getLyXText(bv);
492 setUpdateStatus(bv, what);
493 if ((need_update != CURSOR) || (t->status() != LyXText::UNCHANGED) ||
496 bv->updateInset(this, mark_dirty);
498 bv->owner()->showState();
499 if (old_par != cpar(bv)) {
500 bv->owner()->setLayout(cpar(bv)->getLayout());
506 string const InsetText::editMessage() const
508 return _("Opened Text Inset");
512 void InsetText::edit(BufferView * bv, int x, int y, unsigned int button)
514 // par->SetInsetOwner(this);
515 UpdatableInset::edit(bv, x, y, button);
517 if (!bv->lockInset(this)) {
518 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
522 the_locking_inset = 0;
523 inset_pos = inset_x = inset_y = 0;
524 inset_boundary = false;
527 int tmp_y = (y < 0) ? 0 : y;
528 LyXText * t = getLyXText(bv);
530 if (!checkAndActivateInset(bv, x, tmp_y, button))
531 t->setCursorFromCoordinates(bv, x - drawTextXOffset,
533 t->selection.cursor = t->cursor;
536 updateLocal(bv, CURSOR, false);
538 // If the inset is empty set the language of the current font to the
539 // language to the surronding text (if different).
540 if (par->size() == 0 && !par->next() &&
541 bv->getParentLanguage(this) != t->current_font.language()) {
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 int code = CURSOR|CLEAR_FRAME;
559 LyXText * t = getLyXText(bv);
560 if (t->selection.set()) {
561 t->clearSelection(bv);
563 } else if (owner()) {
564 bv->owner()->setLayout(owner()->getLyXText(bv)
565 ->cursor.par()->getLayout());
567 bv->owner()->setLayout(bv->text->cursor.par()->getLayout());
568 updateLocal(bv, code, false);
572 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
574 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
578 if (inset == cpar(bv)->getInset(cpos(bv))) {
579 lyxerr[Debug::INSETS] << "OK" << endl;
580 the_locking_inset = inset;
581 inset_x = cx(bv) - top_x + drawTextXOffset;
582 inset_y = cy(bv) + drawTextYOffset;
583 inset_pos = cpos(bv);
584 inset_par = cpar(bv);
585 inset_boundary = cboundary(bv);
587 getLyXText(bv)->clearSelection(bv);
588 getLyXText(bv)->sel_cursor = getLyXText(bv)->cursor;
589 getLyXText(bv)->UpdateInset(bv, the_locking_inset);
591 updateLocal(bv, CURSOR, false);
594 } else if (the_locking_inset && (the_locking_inset == inset)) {
595 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
596 lyxerr[Debug::INSETS] << "OK" << endl;
597 inset_x = cx(bv) - top_x + drawTextXOffset;
598 inset_y = cy(bv) + drawTextYOffset;
600 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
602 } else if (the_locking_inset) {
603 lyxerr[Debug::INSETS] << "MAYBE" << endl;
604 return the_locking_inset->lockInsetInInset(bv, inset);
606 lyxerr[Debug::INSETS] << "NOT OK" << endl;
611 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
614 if (!the_locking_inset)
616 if (the_locking_inset == inset) {
617 the_locking_inset->insetUnlock(bv);
618 getLyXText(bv)->updateInset(bv, inset);
619 the_locking_inset = 0;
621 moveRight(bv, false);
622 old_par = 0; // force layout setting
626 updateLocal(bv, CURSOR, false);
629 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
633 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
635 if (!the_locking_inset)
637 if (the_locking_inset != inset) {
638 getLyXText(bv)->updateInset(bv, the_locking_inset);
639 setUpdateStatus(bv, CURSOR_PAR);
640 return the_locking_inset->updateInsetInInset(bv, inset);
642 // updateLocal(bv, FULL, false);
643 if (getLyXText(bv)->updateInset(bv, inset))
644 updateLocal(bv, CURSOR_PAR, false);
645 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
646 inset_x = cx(bv) - top_x + drawTextXOffset;
647 inset_y = cy(bv) + drawTextYOffset;
653 void InsetText::insetButtonPress(BufferView * bv, int x, int y, int button)
657 int tmp_x = x - drawTextXOffset;
658 int tmp_y = y + insetAscent - getLyXText(bv)->first;
659 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y, button);
662 if (the_locking_inset) {
663 if (the_locking_inset == inset) {
664 the_locking_inset->insetButtonPress(bv,x-inset_x,y-inset_y,button);
665 no_selection = false;
668 // otherwise unlock the_locking_inset and lock the new inset
669 the_locking_inset->insetUnlock(bv);
670 inset_x = cx(bv) - top_x + drawTextXOffset;
671 inset_y = cy(bv) + drawTextYOffset;
672 the_locking_inset = static_cast<UpdatableInset*>(inset);
673 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
674 inset->edit(bv, x - inset_x, y - inset_y, button);
675 if (the_locking_inset)
676 updateLocal(bv, CURSOR, false);
677 no_selection = false;
680 // otherwise only unlock the_locking_inset
681 the_locking_inset->insetUnlock(bv);
682 the_locking_inset = 0;
684 if (bv->theLockingInset()) {
685 if (inset && inset->editable() == Inset::HIGHLY_EDITABLE) {
686 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
687 inset_x = cx(bv) - top_x + drawTextXOffset;
688 inset_y = cy(bv) + drawTextYOffset;
689 inset_pos = cpos(bv);
690 inset_par = cpar(bv);
691 inset_boundary = cboundary(bv);
692 the_locking_inset = uinset;
693 uinset->insetButtonPress(bv, x - inset_x, y - inset_y,
695 uinset->edit(bv, x - inset_x, y - inset_y, 0);
696 if (the_locking_inset)
697 updateLocal(bv, CURSOR, false);
698 no_selection = false;
702 if (!inset) { // && (button == 2)) {
703 bool paste_internally = false;
704 if ((button == 2) && getLyXText(bv)->selection.set()) {
705 localDispatch(bv, LFUN_COPY, "");
706 paste_internally = true;
708 getLyXText(bv)->setCursorFromCoordinates(bv, x-drawTextXOffset,
710 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
711 updateLocal(bv, CURSOR, false);
712 bv->owner()->setLayout(cpar(bv)->getLayout());
714 // Insert primary selection with middle mouse
715 // if there is a local selection in the current buffer,
718 if (paste_internally)
719 localDispatch(bv, LFUN_PASTE, "");
721 localDispatch(bv, LFUN_PASTESELECTION,
726 no_selection = false;
730 void InsetText::insetButtonRelease(BufferView * bv, int x, int y, int button)
732 UpdatableInset * inset = 0;
734 if (the_locking_inset) {
735 the_locking_inset->insetButtonRelease(bv,
736 x - inset_x, y - inset_y,
739 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
740 inset = static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
741 if (inset->editable() == Inset::HIGHLY_EDITABLE) {
742 inset->insetButtonRelease(bv,
744 y - inset_y, button);
746 inset_x = cx(bv) - top_x + drawTextXOffset;
747 inset_y = cy(bv) + drawTextYOffset;
748 inset->insetButtonRelease(bv,
750 y - inset_y, button);
752 x - inset_x, y - inset_y, button);
754 updateLocal(bv, CURSOR_PAR, false);
757 no_selection = false;
761 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, int state)
765 if (the_locking_inset) {
766 the_locking_inset->insetMotionNotify(bv, x - inset_x,
770 LyXText * t = getLyXText(bv);
772 t->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
774 if (t->toggle_cursor.par() != t->toggle_end_cursor.par() ||
775 t->toggle_cursor.pos() != t->toggle_end_cursor.pos())
776 updateLocal(bv, SELECTION, false);
781 void InsetText::insetKeyPress(XKeyEvent * xke)
783 if (the_locking_inset) {
784 the_locking_inset->insetKeyPress(xke);
790 UpdatableInset::RESULT
791 InsetText::localDispatch(BufferView * bv,
792 kb_action action, string const & arg)
794 no_selection = false;
795 UpdatableInset::RESULT
796 result= UpdatableInset::localDispatch(bv, action, arg);
797 if (result != UNDISPATCHED) {
802 if ((action < 0) && arg.empty())
805 if (the_locking_inset) {
806 result = the_locking_inset->localDispatch(bv, action, arg);
807 if (result == DISPATCHED_NOUPDATE)
809 else if (result == DISPATCHED) {
810 updateLocal(bv, CURSOR_PAR, false);
812 } else if (result == FINISHED) {
813 bool dispatched = false;
815 case LFUN_UNKNOWN_ACTION:
816 case LFUN_BREAKPARAGRAPH:
818 moveRightIntern(bv, false, false);
821 if (!getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
822 moveRightIntern(bv, false, false);
826 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
827 moveRightIntern(bv, false, false);
833 the_locking_inset = 0;
841 case LFUN_SELFINSERT:
842 if (bv->buffer()->isReadonly()) {
843 // setErrorMessage(N_("Document is read only"));
847 /* Automatically delete the currently selected
848 * text and replace it with what is being
849 * typed in now. Depends on lyxrc settings
850 * "auto_region_delete", which defaults to
853 LyXText * t = getLyXText(bv);
854 setUndo(bv, Undo::INSERT,
855 t->cursor.par(), t->cursor.par()->next());
857 if (lyxrc.auto_region_delete) {
858 if (t->selection.set()) {
859 t->cutSelection(bv, false);
862 t->clearSelection(bv);
863 for (string::size_type i = 0; i < arg.length(); ++i) {
865 if (!math_insert_greek(bv, arg[i])) {
866 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
867 } else if (!the_locking_inset) {
868 (void)moveRight(bv, false);
871 bv->owner()->getIntl()->getTrans().TranslateAndInsert(arg[i], getLyXText(bv));
875 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
876 updateLocal(bv, CURSOR_PAR, true);
877 result = DISPATCHED_NOUPDATE;
879 // --- Cursor Movements -----------------------------------
882 moveRight(bv, false, true);
883 getLyXText(bv)->setSelection(bv);
884 updateLocal(bv, SELECTION, false);
887 result = moveRight(bv);
889 updateLocal(bv, CURSOR, false);
893 moveLeft(bv, false, true);
894 getLyXText(bv)->setSelection(bv);
895 updateLocal(bv, SELECTION, false);
899 result = moveLeft(bv);
900 updateLocal(bv, CURSOR, false);
905 getLyXText(bv)->setSelection(bv);
906 updateLocal(bv, SELECTION, false);
910 result = moveDown(bv);
911 updateLocal(bv, CURSOR, false);
916 getLyXText(bv)->setSelection(bv);
917 updateLocal(bv, SELECTION, false);
922 updateLocal(bv, CURSOR, false);
926 getLyXText(bv)->cursorHome(bv);
927 updateLocal(bv, CURSOR, false);
930 getLyXText(bv)->cursorEnd(bv);
931 updateLocal(bv, CURSOR, false);
933 case LFUN_BACKSPACE: {
934 LyXText * t = getLyXText(bv);
935 setUndo(bv, Undo::DELETE,
936 t->cursor.par(), t->cursor.par()->next());
937 if (t->selection.set())
941 updateLocal(bv, CURSOR_PAR, true);
946 LyXText * t = getLyXText(bv);
947 setUndo(bv, Undo::DELETE,
948 t->cursor.par(), t->cursor.par()->next());
949 if (t->selection.set()) {
954 updateLocal(bv, CURSOR_PAR, true);
959 LyXText * t = getLyXText(bv);
960 setUndo(bv, Undo::DELETE,
961 t->cursor.par(), t->cursor.par()->next());
963 updateLocal(bv, CURSOR_PAR, true);
969 getLyXText(bv)->copySelection(bv);
970 updateLocal(bv, CURSOR_PAR, false);
972 case LFUN_PASTESELECTION:
974 string const clip(bv->getClipboard());
978 if (arg == "paragraph") {
979 getLyXText(bv)->insertStringAsParagraphs(bv, clip);
981 getLyXText(bv)->insertStringAsLines(bv, clip);
983 updateLocal(bv, CURSOR_PAR, true);
987 if (!autoBreakRows) {
989 if (CutAndPaste::nrOfParagraphs() > 1) {
990 WriteAlert(_("Impossible operation"),
991 _("Cannot include more than one paragraph!"),
996 LyXText * t = getLyXText(bv);
997 setUndo(bv, Undo::INSERT,
998 t->cursor.par(), t->cursor.par()->next());
999 t->pasteSelection(bv);
1000 updateLocal(bv, CURSOR_PAR, true);
1004 case LFUN_BREAKPARAGRAPH:
1007 getLyXText(bv)->breakParagraph(bv, 0);
1008 updateLocal(bv, FULL, true);
1010 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1013 getLyXText(bv)->breakParagraph(bv, 1);
1014 updateLocal(bv, FULL, true);
1017 case LFUN_BREAKLINE: {
1020 LyXText * t = getLyXText(bv);
1021 setUndo(bv, Undo::INSERT,
1022 t->cursor.par(), t->cursor.par()->next());
1023 t->insertChar(bv, Paragraph::META_NEWLINE);
1024 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 LyXText * t = getLyXText(bv);
1342 setUndo(bv, Undo::INSERT,
1343 t->cursor.par(), t->cursor.par()->next());
1344 inset->setOwner(this);
1345 hideInsetCursor(bv);
1347 t->insertInset(bv, inset);
1349 if ((cpar(bv)->GetChar(cpos(bv)) != Paragraph::META_INSET) ||
1350 (cpar(bv)->GetInset(cpos(bv)) != inset))
1354 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1355 showInsetCursor(bv);
1360 UpdatableInset * InsetText::getLockingInset()
1362 return the_locking_inset ? the_locking_inset->getLockingInset() : this;
1366 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1370 if (the_locking_inset)
1371 return the_locking_inset->getFirstLockingInsetOfType(c);
1376 bool InsetText::showInsetDialog(BufferView * bv) const
1378 if (the_locking_inset)
1379 return the_locking_inset->showInsetDialog(bv);
1384 std::vector<string> const InsetText::getLabelList() const
1386 std::vector<string> label_list;
1388 Paragraph * tpar = par;
1390 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1391 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1392 for (; beg != end; ++beg) {
1393 std::vector<string> const l = (*beg)->getLabelList();
1394 label_list.insert(label_list.end(), l.begin(), l.end());
1396 tpar = tpar->next();
1402 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1405 if (the_locking_inset) {
1406 the_locking_inset->setFont(bv, font, toggleall, selectall);
1409 LyXText * t = getLyXText(bv);
1410 if (t->selection.set()) {
1411 setUndo(bv, Undo::EDIT,
1412 t->cursor.par(), t->cursor.par()->next());
1417 t->toggleFree(bv, font, toggleall);
1419 t->setFont(bv, font, toggleall);
1422 t->clearSelection(bv);
1424 if (selectall || t->selection.set())
1425 updateLocal(bv, FULL, true);
1427 updateLocal(bv, CURSOR_PAR, true);
1431 bool InsetText::checkAndActivateInset(BufferView * bv, bool behind)
1433 if (cpar(bv)->getChar(cpos(bv)) == Paragraph::META_INSET) {
1437 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
1438 if (!inset || inset->editable() != Inset::HIGHLY_EDITABLE)
1440 LyXFont const font =
1441 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1443 x = inset->width(bv, font);
1444 y = font.isRightToLeft() ? 0 : inset->descent(bv, font);
1447 y = font.isRightToLeft() ? inset->descent(bv, font) : 0;
1449 //inset_x = cx(bv) - top_x + drawTextXOffset;
1450 //inset_y = cy(bv) + drawTextYOffset;
1451 inset->edit(bv, x, y, 0);
1452 if (!the_locking_inset)
1454 updateLocal(bv, CURSOR, false);
1461 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
1464 x -= drawTextXOffset;
1466 int dummyy = y + insetAscent;
1467 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy, button);
1474 inset_x = cx(bv) - top_x + drawTextXOffset;
1475 inset_y = cy(bv) + drawTextYOffset;
1476 inset->edit(bv, x - inset_x, y - inset_y, button);
1477 if (!the_locking_inset)
1479 updateLocal(bv, CURSOR, false);
1486 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
1488 int w = UpdatableInset::getMaxWidth(bv, inset);
1493 w = w - top_x + owner()->x();
1496 w -= (2 * TEXT_TO_INSET_OFFSET);
1498 // return w - (2*TEXT_TO_INSET_OFFSET);
1502 void InsetText::setParagraphData(Paragraph * p)
1506 // now also delete all caches this should be safe, hopefully
1510 Paragraph * tmp = par->next();
1515 par = new Paragraph(*p);
1516 par->setInsetOwner(this);
1517 Paragraph * np = par;
1520 np->next(new Paragraph(*p));
1521 np->next()->previous(np);
1523 np->setInsetOwner(this);
1530 void InsetText::setText(string const & data)
1533 LyXFont font(LyXFont::ALL_SANE);
1534 for (unsigned int i=0; i < data.length(); ++i)
1535 par->insertChar(i, data[i], font);
1539 void InsetText::setAutoBreakRows(bool flag)
1541 if (flag != autoBreakRows) {
1542 autoBreakRows = flag;
1550 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
1552 if (how != drawFrame_) {
1555 updateLocal(bv, DRAW_FRAME, false);
1560 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
1562 if (frame_color != col) {
1565 updateLocal(bv, DRAW_FRAME, false);
1570 int InsetText::cx(BufferView * bv) const
1572 LyXText * text = getLyXText(bv);
1573 int x = text->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
1574 if (the_locking_inset) {
1575 LyXFont font = text->getFont(bv->buffer(),
1577 text->cursor.pos());
1578 if (font.isVisibleRightToLeft())
1579 x -= the_locking_inset->width(bv, font);
1585 int InsetText::cy(BufferView * bv) const
1588 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
1592 Paragraph::size_type InsetText::cpos(BufferView * bv) const
1594 return getLyXText(bv)->cursor.pos();
1598 Paragraph * InsetText::cpar(BufferView * bv) const
1600 return getLyXText(bv)->cursor.par();
1604 bool InsetText::cboundary(BufferView * bv) const
1606 return getLyXText(bv)->cursor.boundary();
1610 Row * InsetText::crow(BufferView * bv) const
1612 return getLyXText(bv)->cursor.row();
1616 LyXText * InsetText::getLyXText(BufferView const * lbv,
1617 bool const recursive) const
1619 if (!recursive && (cached_bview == lbv))
1620 return cached_text.get();
1622 // Super UGLY! (Lgb)
1623 BufferView * bv = const_cast<BufferView *>(lbv);
1626 Cache::iterator it = cache.find(bv);
1628 if (it != cache.end()) {
1629 lyx::Assert(it->second.get());
1631 cached_text = it->second;
1632 if (recursive && the_locking_inset) {
1633 return the_locking_inset->getLyXText(bv);
1635 return cached_text.get();
1637 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
1638 cached_text->init(bv);
1640 cache.insert(make_pair(bv, cached_text));
1642 if (the_locking_inset) {
1643 cached_text->setCursor(bv, inset_par, inset_pos,
1644 true, inset_boundary);
1646 return the_locking_inset->getLyXText(bv);
1649 return cached_text.get();
1653 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
1657 Cache::iterator it = cache.find(bv);
1659 if (it == cache.end()) {
1663 lyx::Assert(it->second.get());
1667 /// then remove all LyXText in text-insets
1668 Paragraph * p = par;
1669 for (; p; p = p->next()) {
1670 p->deleteInsetsLyXText(bv);
1676 void InsetText::resizeLyXText(BufferView * bv, bool force) const
1678 if (!par->next() && !par->size()) // no data, resize not neccessary!
1680 // one endless line, resize normally not necessary
1681 if (!force && getMaxWidth(bv, this) < 0)
1684 Cache::iterator it = cache.find(bv);
1685 if (it == cache.end()) {
1688 lyx::Assert(it->second.get());
1690 Paragraph * lpar = 0;
1691 Paragraph * selstartpar = 0;
1692 Paragraph * selendpar = 0;
1693 Paragraph::size_type pos = 0;
1694 Paragraph::size_type selstartpos = 0;
1695 Paragraph::size_type selendpos = 0;
1696 bool boundary = false;
1697 bool selstartboundary = false;
1698 bool selendboundary = false;
1699 bool selection = false;
1700 bool mark_set = false;
1702 // bv->owner()->prohibitInput();
1705 LyXText * t = getLyXText(bv);
1706 lpar = t->cursor.par();
1707 pos = t->cursor.pos();
1708 boundary = t->cursor.boundary();
1709 selstartpar = t->selection.start.par();
1710 selstartpos = t->selection.start.pos();
1711 selstartboundary = t->selection.start.boundary();
1712 selendpar = t->selection.end.par();
1713 selendpos = t->selection.end.pos();
1714 selendboundary = t->selection.end.boundary();
1715 selection = t->selection.set();
1716 mark_set = t->selection.mark();
1718 deleteLyXText(bv, (the_locking_inset == 0) || force);
1721 LyXText * t = getLyXText(bv);
1723 t->selection.set(true);
1724 /* at this point just to avoid the Delete-Empty-Paragraph
1725 * Mechanism when setting the cursor */
1726 t->selection.mark(mark_set);
1728 t->setCursor(bv, selstartpar, selstartpos, true, selstartboundary);
1729 t->selection.cursor = t->cursor;
1730 t->setCursor(bv, selendpar, selendpos, true, selendboundary);
1731 t->setSelection(bv);
1732 t->setCursor(bv, lpar, pos);
1734 t->setCursor(bv, lpar, pos, true, boundary);
1735 t->selection.cursor = t->cursor;
1736 t->selection.set(false);
1740 LyXText * t = getLyXText(bv);
1741 t->first = bv->screen()->topCursorVisible(t);
1744 // this will scroll the screen such that the cursor becomes visible
1745 bv->updateScrollbar();
1746 // bv->owner()->allowInput();
1747 if (the_locking_inset) {
1748 /// then resize all LyXText in text-insets
1749 inset_x = cx(bv) - top_x + drawTextXOffset;
1750 inset_y = cy(bv) + drawTextYOffset;
1751 for (Paragraph * p = par; p; p = p->next()) {
1752 p->resizeInsetsLyXText(bv);
1759 void InsetText::removeNewlines()
1761 for (Paragraph * p = par; p; p = p->next()) {
1762 for (int i = 0; i < p->size(); ++i) {
1763 if (p->getChar(i) == Paragraph::META_NEWLINE)
1770 bool InsetText::nodraw() const
1772 if (the_locking_inset)
1773 return the_locking_inset->nodraw();
1774 return UpdatableInset::nodraw();
1778 int InsetText::scroll(bool recursive) const
1780 int sx = UpdatableInset::scroll(false);
1782 if (recursive && the_locking_inset)
1783 sx += the_locking_inset->scroll(recursive);
1789 bool InsetText::doClearArea() const
1791 return !locked || (need_update & (FULL|INIT));
1795 void InsetText::selectAll(BufferView * bv)
1797 getLyXText(bv)->cursorTop(bv);
1798 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1799 getLyXText(bv)->cursorBottom(bv);
1800 getLyXText(bv)->setSelection(bv);
1804 void InsetText::clearSelection(BufferView * bv)
1806 getLyXText(bv)->clearSelection(bv);
1810 void InsetText::clearInset(Painter & pain, int baseline, bool & cleared) const
1813 int h = insetAscent + insetDescent;
1814 int ty = baseline - insetAscent;
1820 if ((ty + h) > pain.paperHeight())
1821 h = pain.paperHeight();
1822 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
1823 w = pain.paperWidth();
1824 pain.fillRectangle(top_x+drawTextXOffset, ty, w, h);
1830 Paragraph * InsetText::getParFromID(int id) const
1832 Paragraph * result = par;
1833 Paragraph * ires = 0;
1834 while (result && result->id() != id) {
1835 if ((ires = result->getParFromID(id)))
1837 result = result->next();
1843 Paragraph * InsetText::firstParagraph() const
1846 if (the_locking_inset)
1847 if ((result = the_locking_inset->firstParagraph()))
1853 LyXCursor const & InsetText::cursor(BufferView * bv) const
1855 if (the_locking_inset)
1856 return the_locking_inset->cursor(bv);
1857 return getLyXText(bv)->cursor;
1861 Paragraph * InsetText::paragraph() const
1867 void InsetText::paragraph(Paragraph * p)
1871 // we now have to update/redraw all instances
1872 for (Cache::iterator cit = cache.begin(); cit != cache.end(); ++cit) {
1877 // redraw myself when asked for
1878 need_update |= INIT;
1882 Inset * InsetText::getInsetFromID(int id_arg) const
1885 return const_cast<InsetText *>(this);
1887 Paragraph * lp = par;
1890 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
1891 en = lp->inset_iterator_end();
1894 if ((*it)->id() == id_arg)
1896 Inset * in = (*it)->getInsetFromID(id_arg);