1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insettext.h"
18 #include "paragraph.h"
23 #include "frontends/LyXView.h"
24 #include "BufferView.h"
25 #include "LaTeXFeatures.h"
26 #include "frontends/Painter.h"
28 #include "lyxcursor.h"
29 #include "CutAndPaste.h"
30 #include "frontends/font_metrics.h"
35 #include "trans_mgr.h"
36 #include "frontends/screen.h"
39 #include "ParagraphParameters.h"
40 #include "undo_funcs.h"
42 #include "funcrequest.h"
44 #include "frontends/Alert.h"
45 #include "frontends/Dialogs.h"
47 #include "support/textutils.h"
48 #include "support/LAssert.h"
49 #include "support/lstrings.h"
50 #include "support/lyxalgo.h" // lyx::count
67 using lyx::textclass_type;
69 extern unsigned char getCurrentTextClass(Buffer *);
70 extern bool math_insert_greek(BufferView *, char);
71 extern int greek_kb_flag;
74 // These functions should probably go into bufferview_funcs somehow (Jug)
76 void InsetText::saveLyXTextState(LyXText * t) const
78 // check if my paragraphs are still valid
81 if (p == t->cursor.par())
86 if (p && t->cursor.pos() <= p->size()) {
87 sstate.lpar = t->cursor.par();
88 sstate.pos = t->cursor.pos();
89 sstate.boundary = t->cursor.boundary();
90 sstate.selstartpar = t->selection.start.par();
91 sstate.selstartpos = t->selection.start.pos();
92 sstate.selstartboundary = t->selection.start.boundary();
93 sstate.selendpar = t->selection.end.par();
94 sstate.selendpos = t->selection.end.pos();
95 sstate.selendboundary = t->selection.end.boundary();
96 sstate.selection = t->selection.set();
97 sstate.mark_set = t->selection.mark();
98 sstate.refresh = t->refresh_row != 0;
104 void InsetText::restoreLyXTextState(BufferView * bv, LyXText * t) const
107 t->selection.set(true);
108 /* at this point just to avoid the Delete-Empty-Paragraph
109 * Mechanism when setting the cursor */
110 t->selection.mark(sstate.mark_set);
111 if (sstate.selection) {
112 t->setCursor(bv, sstate.selstartpar, sstate.selstartpos,
113 true, sstate.selstartboundary);
114 t->selection.cursor = t->cursor;
115 t->setCursor(bv, sstate.selendpar, sstate.selendpos,
116 true, sstate.selendboundary);
118 t->setCursor(bv, sstate.lpar, sstate.pos);
120 t->setCursor(bv, sstate.lpar, sstate.pos, true, sstate.boundary);
121 t->selection.cursor = t->cursor;
122 t->selection.set(false);
124 if (sstate.refresh) {
130 InsetText::InnerCache::InnerCache(boost::shared_ptr<LyXText> t)
137 InsetText::InsetText(BufferParams const & bp)
138 : UpdatableInset(), lt(0), in_update(false), do_resize(0),
142 par->layout(bp.getLyXTextClass().defaultLayout());
147 InsetText::InsetText(InsetText const & in, bool same_id)
148 : UpdatableInset(in, same_id), lt(0), in_update(false), do_resize(0),
156 InsetText & InsetText::operator=(InsetText const & it)
163 void InsetText::init(InsetText const * ins, bool same_id)
166 setParagraphData(ins->par, same_id);
167 autoBreakRows = ins->autoBreakRows;
168 drawFrame_ = ins->drawFrame_;
169 frame_color = ins->frame_color;
175 p->setInsetOwner(this);
178 the_locking_inset = 0;
180 frame_color = LColor::insetframe;
181 autoBreakRows = false;
194 last_drawn_width = -1;
195 frame_is_visible = false;
198 in_insetAllowed = false;
202 InsetText::~InsetText()
209 Paragraph * tmp = par->next();
216 void InsetText::clear()
218 // This is a gross hack...
219 LyXLayout_ptr old_layout = par->layout();
222 Paragraph * tmp = par->next();
227 par->setInsetOwner(this);
228 par->layout(old_layout);
235 Inset * InsetText::clone(Buffer const &, bool same_id) const
237 return new InsetText(*this, same_id);
241 void InsetText::write(Buffer const * buf, ostream & os) const
244 writeParagraphData(buf, os);
248 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
250 par->writeFile(buf, os, buf->params, 0);
254 void InsetText::read(Buffer const * buf, LyXLex & lex)
258 Paragraph * return_par = 0;
259 Paragraph::depth_type depth = 0;
260 LyXFont font(LyXFont::ALL_INHERIT);
266 token = lex.getString();
269 if (token == "\\end_inset") {
270 #ifndef NO_COMPABILITY
271 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
276 if (const_cast<Buffer*>(buf)->
277 parseSingleLyXformat2Token(lex, par, return_par,
278 token, pos, depth, font)) {
279 // the_end read this should NEVER happen
280 lex.printError("\\the_end read in inset! Error in document!");
288 return_par->setInsetOwner(this);
289 return_par = return_par->next();
292 if (token != "\\end_inset") {
293 lex.printError("Missing \\end_inset at this point. "
300 int InsetText::ascent(BufferView * bv, LyXFont const &) const
302 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
303 TEXT_TO_INSET_OFFSET;
308 int InsetText::descent(BufferView * bv, LyXFont const &) const
310 LyXText * llt = getLyXText(bv);
311 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
312 TEXT_TO_INSET_OFFSET;
317 int InsetText::width(BufferView * bv, LyXFont const &) const
319 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
320 (2 * TEXT_TO_INSET_OFFSET);
321 insetWidth = max(insetWidth, 10);
326 int InsetText::textWidth(BufferView * bv, bool fordraw) const
329 if (!autoBreakRows) {
332 w = getMaxWidth(bv, this);
335 return max(w - (2 * TEXT_TO_INSET_OFFSET),
336 (int)getLyXText(bv)->width);
340 return w - (2 * TEXT_TO_INSET_OFFSET);
344 void InsetText::draw(BufferView * bv, LyXFont const & f,
345 int baseline, float & x, bool cleared) const
350 Painter & pain = bv->painter();
352 // this is the first thing we have to ask because if the x pos
353 // changed we have to do a complete rebreak of the text as we
354 // may have few space to draw in. Well we should check on this too
356 if (top_x != int(x)) {
359 int nw = getMaxWidth(bv, this);
360 if (nw > 0 && old_max_width != nw) {
363 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
368 // call these methods so that insetWidth, insetAscent and
369 // insetDescent have the right values.
374 // repaint the background if needed
375 if (cleared && backgroundColor() != LColor::background) {
376 clearInset(bv, baseline, cleared);
379 // no draw is necessary !!!
380 if ((drawFrame_ == LOCKED) && !locked && par->empty()) {
381 top_baseline = baseline;
383 if (need_update & CLEAR_FRAME)
384 clearFrame(pain, cleared);
390 x += static_cast<float>(scroll());
392 // if top_x differs we did it already
393 if (!cleared && (old_x == int(x))
394 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
395 ||(last_drawn_width != insetWidth)))
397 // Condition necessary to eliminate bug 59 attachment 37
399 clearInset(bv, baseline, cleared);
403 frame_is_visible = false;
405 if (!cleared && (need_update == NONE)) {
407 drawFrame(pain, cleared);
411 top_baseline = baseline;
412 top_y = baseline - insetAscent;
414 if (last_drawn_width != insetWidth) {
416 clearInset(bv, baseline, cleared);
418 last_drawn_width = insetWidth;
421 if (the_locking_inset && (cpar(bv) == inset_par)
422 && (cpos(bv) == inset_pos)) {
423 inset_x = cix(bv) - top_x + drawTextXOffset;
424 inset_y = ciy(bv) + drawTextYOffset;
426 if (!cleared && (need_update == CURSOR)
427 && !getLyXText(bv)->selection.set()) {
428 drawFrame(pain, cleared);
438 x += TEXT_TO_INSET_OFFSET;
440 Row * row = lt->firstRow();
441 int y_offset = baseline - row->ascent_of_text();
442 int ph = pain.paperHeight();
445 while ((row != 0) && ((y+row->height()) <= 0)) {
447 first += row->height();
451 lt->first_y = -y_offset;
458 if (cleared || (need_update&(INIT|FULL))) {
459 int yf = y_offset + first;
461 while ((row != 0) && (yf < ph)) {
462 lt->getVisibleRow(bv, y + y_offset + first, int(x),
463 row, y + lt->first_y, cleared);
464 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
465 lt->need_break_row = row;
467 lt->setCursor(bv, lt->cursor.par(),
469 if (lt->selection.set()) {
470 lt->setCursor(bv, lt->selection.start,
471 lt->selection.start.par(),
472 lt->selection.start.pos());
473 lt->setCursor(bv, lt->selection.end,
474 lt->selection.end.par(),
475 lt->selection.end.pos());
483 } else if (!locked) {
484 if (need_update & CURSOR) {
485 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
486 lt->clearSelection();
487 lt->selection.cursor = lt->cursor;
489 bv->screen().update(lt, bv, y_offset, int(x));
492 if (need_update & SELECTION) {
493 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
494 } else if (need_update & CURSOR) {
495 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
496 lt->clearSelection();
497 lt->selection.cursor = lt->cursor;
499 bv->screen().update(lt, bv, y_offset, int(x));
504 lt->status(bv, LyXText::UNCHANGED);
505 if ((drawFrame_ == ALWAYS) ||
506 ((cleared || (need_update != CURSOR_PAR)) &&
507 (drawFrame_ == LOCKED) && locked))
509 drawFrame(pain, cleared);
510 } else if (need_update & CLEAR_FRAME) {
511 clearFrame(pain, cleared);
514 x += insetWidth - TEXT_TO_INSET_OFFSET;
516 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
518 } else if (need_update != INIT) {
526 void InsetText::drawFrame(Painter & pain, bool cleared) const
528 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
529 if (!frame_is_visible || cleared) {
530 frame_x = top_x + ttoD2;
531 frame_y = top_baseline - insetAscent + ttoD2;
532 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
533 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
534 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
536 frame_is_visible = true;
541 void InsetText::clearFrame(Painter & pain, bool cleared) const
543 if (frame_is_visible) {
545 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
548 frame_is_visible = false;
553 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
556 if (reinit && owner()) {
558 owner()->update(bv, font, true);
563 if (reinit || need_update == INIT) {
565 // we should put this call where we set need_update to INIT!
568 owner()->update(bv, font, true);
573 if (!autoBreakRows && par->next())
574 collapseParagraphs(bv);
576 if (the_locking_inset) {
577 inset_x = cix(bv) - top_x + drawTextXOffset;
578 inset_y = ciy(bv) + drawTextYOffset;
579 the_locking_inset->update(bv, font, reinit);
587 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
590 lt->updateInset(bv, the_locking_inset);
592 if (lt->status() == LyXText::NEED_MORE_REFRESH)
600 void InsetText::setUpdateStatus(BufferView * bv, int what) const
602 // this does nothing dangerous so use only a localized buffer
603 LyXText * llt = getLyXText(bv);
606 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
607 // if we don't break row so that we only have one row to update!
608 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
610 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
613 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
614 need_update |= CURSOR_PAR;
617 // this to not draw a selection when we redraw all of it!
618 if (need_update & CURSOR && !(need_update & SELECTION)) {
619 if (llt->selection.set())
621 llt->clearSelection();
626 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
628 if (!autoBreakRows && par->next())
629 collapseParagraphs(bv);
636 setUpdateStatus(bv, what);
637 bool flag = mark_dirty ||
638 (((need_update != CURSOR) && (need_update != NONE)) ||
639 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
640 if (!lt->selection.set())
641 lt->selection.cursor = lt->cursor;
645 // IMO this is not anymore needed as we do this in fitInsetCursor!
646 // and we always get "true" as returnvalue of this function in the
647 // case of a locking inset (Jug 20020412)
648 if (locked && (need_update & CURSOR) && bv->fitCursor())
654 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
656 if (need_update == CURSOR)
658 bv->owner()->view_state_changed();
659 bv->owner()->updateMenubar();
660 bv->owner()->updateToolbar();
661 if (old_par != cpar(bv)) {
662 bv->owner()->setLayout(cpar(bv)->layout()->name());
668 string const InsetText::editMessage() const
670 return _("Opened Text Inset");
674 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
676 UpdatableInset::edit(bv, x, y, button);
678 if (!bv->lockInset(this)) {
679 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
683 the_locking_inset = 0;
684 inset_pos = inset_x = inset_y = 0;
685 inset_boundary = false;
688 int tmp_y = (y < 0) ? 0 : y;
694 // we put here -1 and not button as now the button in the
695 // edit call should not be needed we will fix this in 1.3.x
696 // cycle hopefully (Jug 20020509)
697 // FIXME: GUII I've changed this to none: probably WRONG
698 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
699 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
701 lt->cursor.x_fix(lt->cursor.x());
703 lt->clearSelection();
705 // If the inset is empty set the language of the current font to the
706 // language to the surronding text (if different).
707 if (par->empty() && !par->next() &&
708 bv->getParentLanguage(this) != lt->current_font.language())
710 LyXFont font(LyXFont::ALL_IGNORE);
711 font.setLanguage(bv->getParentLanguage(this));
712 setFont(bv, font, false);
718 if (drawFrame_ == LOCKED)
719 code = CURSOR|DRAW_FRAME;
720 updateLocal(bv, code, false);
723 // Tell the paragraph dialog that we've entered an insettext.
724 bv->owner()->getDialogs()->updateParagraph();
728 void InsetText::edit(BufferView * bv, bool front)
730 UpdatableInset::edit(bv, front);
732 if (!bv->lockInset(this)) {
733 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
737 the_locking_inset = 0;
738 inset_pos = inset_x = inset_y = 0;
739 inset_boundary = false;
748 lt->setCursor(bv, par, 0);
753 // int const pos = (p->size() ? p->size()-1 : p->size());
754 lt->setCursor(bv, p, p->size());
756 lt->clearSelection();
758 // If the inset is empty set the language of the current font to the
759 // language to the surronding text (if different).
760 if (par->empty() && !par->next() &&
761 bv->getParentLanguage(this) != lt->current_font.language()) {
762 LyXFont font(LyXFont::ALL_IGNORE);
763 font.setLanguage(bv->getParentLanguage(this));
764 setFont(bv, font, false);
769 if (drawFrame_ == LOCKED)
770 code = CURSOR|DRAW_FRAME;
771 updateLocal(bv, code, false);
776 void InsetText::insetUnlock(BufferView * bv)
778 if (the_locking_inset) {
779 the_locking_inset->insetUnlock(bv);
780 the_locking_inset = 0;
781 updateLocal(bv, CURSOR_PAR, false);
788 if (drawFrame_ == LOCKED)
789 code = CURSOR|CLEAR_FRAME;
793 if (drawFrame_ == LOCKED)
801 if (lt->selection.set()) {
802 lt->clearSelection();
804 } else if (owner()) {
805 bv->owner()->setLayout(owner()->getLyXText(bv)
806 ->cursor.par()->layout()->name());
808 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
809 // hack for deleteEmptyParMech
811 lt->setCursor(bv, par, 0);
812 } else if (par->next()) {
813 lt->setCursor(bv, par->next(), 0);
818 updateLocal(bv, code, false);
821 setUpdateStatus(bv, code);
826 void InsetText::lockInset(BufferView * bv)
829 the_locking_inset = 0;
830 inset_pos = inset_x = inset_y = 0;
831 inset_boundary = false;
839 lt->setCursor(bv, par, 0);
840 lt->clearSelection();
842 // If the inset is empty set the language of the current font to the
843 // language to the surronding text (if different).
844 if (par->empty() && !par->next() &&
845 bv->getParentLanguage(this) != lt->current_font.language()) {
846 LyXFont font(LyXFont::ALL_IGNORE);
847 font.setLanguage(bv->getParentLanguage(this));
848 setFont(bv, font, false);
853 if (drawFrame_ == LOCKED)
854 code = CURSOR|DRAW_FRAME;
855 setUpdateStatus(bv, code);
859 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
861 the_locking_inset = inset;
862 inset_x = cix(bv) - top_x + drawTextXOffset;
863 inset_y = ciy(bv) + drawTextYOffset;
864 inset_pos = cpos(bv);
865 inset_par = cpar(bv);
866 inset_boundary = cboundary(bv);
867 updateLocal(bv, CURSOR, false);
871 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
873 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
877 if (!the_locking_inset) {
879 int const id = inset->id();
881 Paragraph::inset_iterator it =
882 p->inset_iterator_begin();
883 Paragraph::inset_iterator const end =
884 p->inset_iterator_end();
885 for (; it != end; ++it) {
886 if ((*it) == inset) {
887 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
888 lockInset(bv, inset);
891 if ((*it)->getInsetFromID(id)) {
892 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
894 return the_locking_inset->lockInsetInInset(bv, inset);
901 if (inset == cpar(bv)->getInset(cpos(bv))) {
902 lyxerr[Debug::INSETS] << "OK" << endl;
903 lockInset(bv, inset);
905 } else if (the_locking_inset && (the_locking_inset == inset)) {
906 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
907 lyxerr[Debug::INSETS] << "OK" << endl;
908 inset_x = cix(bv) - top_x + drawTextXOffset;
909 inset_y = ciy(bv) + drawTextYOffset;
911 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
913 } else if (the_locking_inset) {
914 lyxerr[Debug::INSETS] << "MAYBE" << endl;
915 return the_locking_inset->lockInsetInInset(bv, inset);
917 lyxerr[Debug::INSETS] << "NOT OK" << endl;
922 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
925 if (!the_locking_inset)
927 if (the_locking_inset == inset) {
928 the_locking_inset->insetUnlock(bv);
929 getLyXText(bv)->updateInset(bv, inset);
930 the_locking_inset = 0;
932 moveRightIntern(bv, true, false);
933 old_par = 0; // force layout setting
937 updateLocal(bv, CURSOR, false);
940 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
944 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
946 if (!autoBreakRows && par->next())
947 collapseParagraphs(bv);
955 if (inset->owner() != this) {
956 int ustat = CURSOR_PAR;
958 UpdatableInset * tl_inset = the_locking_inset;
960 found = tl_inset->updateInsetInInset(bv, inset);
962 tl_inset = static_cast<UpdatableInset *>(inset);
963 while(tl_inset->owner() && tl_inset->owner() != this)
964 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
965 if (!tl_inset->owner())
967 found = tl_inset->updateInsetInInset(bv, inset);
971 lt->updateInset(bv, tl_inset);
975 setUpdateStatus(bv, ustat);
978 bool found = lt->updateInset(bv, inset);
982 setUpdateStatus(bv, CURSOR_PAR);
983 if (the_locking_inset &&
984 cpar(bv) == inset_par && cpos(bv) == inset_pos)
986 inset_x = cix(bv) - top_x + drawTextXOffset;
987 inset_y = ciy(bv) + drawTextYOffset;
994 void InsetText::insetButtonPress(BufferView * bv,
995 int x, int y, mouse_button::state button)
999 // use this to check mouse motion for selection!
1006 int tmp_x = x - drawTextXOffset;
1007 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1008 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1010 hideInsetCursor(bv);
1011 if (the_locking_inset) {
1012 if (the_locking_inset == inset) {
1013 the_locking_inset->insetButtonPress(bv,
1021 // otherwise unlock the_locking_inset and lock the new inset
1022 the_locking_inset->insetUnlock(bv);
1023 inset_x = cix(bv) - top_x + drawTextXOffset;
1024 inset_y = ciy(bv) + drawTextYOffset;
1025 the_locking_inset = 0;
1026 inset->insetButtonPress(bv, x - inset_x,
1027 y - inset_y, button);
1028 // inset->edit(bv, x - inset_x, y - inset_y, button);
1029 if (the_locking_inset)
1030 updateLocal(bv, CURSOR, false);
1034 // otherwise only unlock the_locking_inset
1035 the_locking_inset->insetUnlock(bv);
1036 the_locking_inset = 0;
1039 no_selection = false;
1041 if (bv->theLockingInset()) {
1042 if (isHighlyEditableInset(inset)) {
1043 // We just have to lock the inset before calling a
1044 // PressFuncRequest on it!
1045 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1046 if (!bv->lockInset(uinset)) {
1047 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1049 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1050 if (the_locking_inset)
1051 updateLocal(bv, CURSOR, false);
1055 if (!inset) { // && (button == mouse_button::button2)) {
1056 bool paste_internally = false;
1057 if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
1058 localDispatch(bv, FuncRequest(LFUN_COPY));
1059 paste_internally = true;
1063 lt = getLyXText(bv);
1066 int old_first_y = lt->first_y;
1068 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1070 // set the selection cursor!
1071 lt->selection.cursor = lt->cursor;
1072 lt->cursor.x_fix(lt->cursor.x());
1074 if (lt->selection.set()) {
1075 lt->clearSelection();
1078 updateLocal(bv, FULL, false);
1080 lt->clearSelection();
1083 updateLocal(bv, CURSOR, false);
1086 bv->owner()->setLayout(cpar(bv)->layout()->name());
1088 // we moved the view we cannot do mouse selection in this case!
1089 if (getLyXText(bv)->first_y != old_first_y)
1090 no_selection = true;
1092 // Insert primary selection with middle mouse
1093 // if there is a local selection in the current buffer,
1095 if (button == mouse_button::button2) {
1096 if (paste_internally)
1097 localDispatch(bv, FuncRequest(LFUN_PASTE));
1099 localDispatch(bv, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1102 getLyXText(bv)->clearSelection();
1104 showInsetCursor(bv);
1108 bool InsetText::insetButtonRelease(BufferView * bv,
1109 int x, int y, mouse_button::state button)
1111 no_selection = true;
1112 if (the_locking_inset) {
1113 return the_locking_inset->insetButtonRelease(bv,
1114 x - inset_x, y - inset_y,
1117 int tmp_x = x - drawTextXOffset;
1118 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1119 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1122 if (isHighlyEditableInset(inset)) {
1123 ret = inset->insetButtonRelease(bv, x - inset_x,
1124 y - inset_y, button);
1126 inset_x = cix(bv) - top_x + drawTextXOffset;
1127 inset_y = ciy(bv) + drawTextYOffset;
1128 ret = inset->insetButtonRelease(bv, x - inset_x,
1129 y - inset_y, button);
1130 inset->edit(bv, x - inset_x,
1131 y - inset_y, button);
1133 updateLocal(bv, CURSOR_PAR, false);
1139 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1141 if (the_locking_inset) {
1142 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1147 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1152 lt = getLyXText(bv);
1155 hideInsetCursor(bv);
1156 LyXCursor cur = lt->cursor;
1157 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1158 lt->cursor.x_fix(lt->cursor.x());
1159 if (cur == lt->cursor) {
1164 lt->setSelection(bv);
1165 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1166 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1170 updateLocal(bv, SELECTION, false);
1172 showInsetCursor(bv);
1176 UpdatableInset::RESULT
1177 InsetText::localDispatch(BufferView * bv, FuncRequest const & ev)
1179 bool was_empty = (par->empty() && !par->next());
1180 no_selection = false;
1181 UpdatableInset::RESULT
1182 result= UpdatableInset::localDispatch(bv, ev);
1183 if (result != UNDISPATCHED)
1186 result = DISPATCHED;
1187 if (ev.action < 0 && ev.argument.empty())
1190 if (the_locking_inset) {
1191 result = the_locking_inset->localDispatch(bv, ev);
1192 if (result == DISPATCHED_NOUPDATE)
1194 else if (result == DISPATCHED) {
1195 updateLocal(bv, CURSOR_PAR, false);
1197 } else if (result >= FINISHED) {
1199 case FINISHED_RIGHT:
1200 moveRightIntern(bv, false, false);
1201 result = DISPATCHED;
1204 if ((result = moveUp(bv)) >= FINISHED) {
1205 updateLocal(bv, CURSOR, false);
1206 bv->unlockInset(this);
1211 LyXText *lt = getLyXText(bv);
1212 if (lt->cursor.irow()->next()) {
1213 lt->setCursorFromCoordinates(
1214 bv, lt->cursor.ix() + inset_x,
1216 lt->cursor.irow()->baseline() +
1217 lt->cursor.irow()->height() + 1);
1218 lt->cursor.x_fix(lt->cursor.x());
1219 updateLocal(bv, CURSOR, false);
1221 bv->unlockInset(this);
1226 result = DISPATCHED;
1229 the_locking_inset = 0;
1230 updateLocal(bv, CURSOR, false);
1234 hideInsetCursor(bv);
1237 lt = getLyXText(bv);
1241 int updflag = false;
1242 switch (ev.action) {
1244 case LFUN_SELFINSERT:
1245 if (bv->buffer()->isReadonly()) {
1246 // setErrorMessage(N_("Document is read only"));
1249 if (!ev.argument.empty()) {
1250 /* Automatically delete the currently selected
1251 * text and replace it with what is being
1252 * typed in now. Depends on lyxrc settings
1253 * "auto_region_delete", which defaults to
1256 setUndo(bv, Undo::INSERT,
1257 lt->cursor.par(), lt->cursor.par()->next());
1259 if (lyxrc.auto_region_delete) {
1260 if (lt->selection.set()) {
1261 lt->cutSelection(bv, false, false);
1264 lt->clearSelection();
1265 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1266 bv->owner()->getIntl()->getTransManager().
1267 TranslateAndInsert(ev.argument[i], lt);
1270 lt->selection.cursor = lt->cursor;
1271 updwhat = CURSOR | CURSOR_PAR;
1273 result = DISPATCHED_NOUPDATE;
1275 // --- Cursor Movements -----------------------------------
1278 moveRight(bv, false, true);
1279 lt->setSelection(bv);
1280 updwhat = SELECTION;
1283 result = moveRight(bv);
1289 moveLeft(bv, false, true);
1290 lt->setSelection(bv);
1291 updwhat = SELECTION;
1295 result = moveLeft(bv);
1301 lt->setSelection(bv);
1302 updwhat = SELECTION;
1306 result = moveDown(bv);
1312 lt->setSelection(bv);
1313 updwhat = SELECTION;
1317 result = moveUp(bv);
1329 case LFUN_BACKSPACE: {
1330 setUndo(bv, Undo::DELETE,
1331 lt->cursor.par(), lt->cursor.par()->next());
1332 if (lt->selection.set())
1333 lt->cutSelection(bv, true, false);
1336 updwhat = CURSOR_PAR;
1342 setUndo(bv, Undo::DELETE,
1343 lt->cursor.par(), lt->cursor.par()->next());
1344 if (lt->selection.set()) {
1345 lt->cutSelection(bv, true, false);
1349 updwhat = CURSOR_PAR;
1355 setUndo(bv, Undo::DELETE,
1356 lt->cursor.par(), lt->cursor.par()->next());
1357 lt->cutSelection(bv);
1358 updwhat = CURSOR_PAR;
1365 lt->copySelection(bv);
1366 updwhat = CURSOR_PAR;
1368 case LFUN_PASTESELECTION:
1370 string const clip(bv->getClipboard());
1374 if (ev.argument == "paragraph") {
1375 lt->insertStringAsParagraphs(bv, clip);
1377 lt->insertStringAsLines(bv, clip);
1380 lt->clearSelection();
1382 updwhat = CURSOR_PAR;
1387 if (!autoBreakRows) {
1389 if (CutAndPaste::nrOfParagraphs() > 1) {
1390 Alert::alert(_("Impossible operation"),
1391 _("Cannot include more than one paragraph!"),
1396 setUndo(bv, Undo::INSERT,
1397 lt->cursor.par(), lt->cursor.par()->next());
1398 lt->pasteSelection(bv);
1400 lt->clearSelection();
1401 updwhat = CURSOR_PAR;
1406 case LFUN_BREAKPARAGRAPH:
1407 if (!autoBreakRows) {
1408 result = DISPATCHED;
1411 lt->breakParagraph(bv, 0);
1412 updwhat = CURSOR | FULL;
1415 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1416 if (!autoBreakRows) {
1417 result = DISPATCHED;
1420 lt->breakParagraph(bv, 1);
1421 updwhat = CURSOR | FULL;
1425 case LFUN_BREAKLINE: {
1426 if (!autoBreakRows) {
1427 result = DISPATCHED;
1430 setUndo(bv, Undo::INSERT,
1431 lt->cursor.par(), lt->cursor.par()->next());
1432 lt->insertChar(bv, Paragraph::META_NEWLINE);
1433 updwhat = CURSOR | CURSOR_PAR;
1439 // do not set layouts on non breakable textinsets
1440 if (autoBreakRows) {
1441 string cur_layout = cpar(bv)->layout()->name();
1443 // Derive layout number from given argument (string)
1444 // and current buffer's textclass (number). */
1445 LyXTextClass const & tclass =
1446 bv->buffer()->params.getLyXTextClass();
1447 string layout = ev.argument;
1448 bool hasLayout = tclass.hasLayout(layout);
1450 // If the entry is obsolete, use the new one instead.
1452 string const & obs =
1453 tclass[layout]->obsoleted_by();
1458 // see if we found the layout number:
1460 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1461 bv->owner()->getLyXFunc()->dispatch(lf);
1465 if (cur_layout != layout) {
1466 cur_layout = layout;
1467 lt->setLayout(bv, layout);
1468 bv->owner()->setLayout(cpar(bv)->layout()->name());
1469 updwhat = CURSOR_PAR;
1473 // reset the layout box
1474 bv->owner()->setLayout(cpar(bv)->layout()->name());
1477 case LFUN_PARAGRAPH_SPACING:
1478 // This one is absolutely not working. When fiddling with this
1479 // it also seems to me that the paragraphs inside the insettext
1480 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1482 Paragraph * par = lt->cursor.par();
1483 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1484 float cur_value = 1.0;
1485 if (cur_spacing == Spacing::Other) {
1486 cur_value = par->params().spacing().getValue();
1489 istringstream istr(ev.argument.c_str());
1492 Spacing::Space new_spacing = cur_spacing;
1493 float new_value = cur_value;
1495 lyxerr << "Missing argument to `paragraph-spacing'"
1497 } else if (tmp == "single") {
1498 new_spacing = Spacing::Single;
1499 } else if (tmp == "onehalf") {
1500 new_spacing = Spacing::Onehalf;
1501 } else if (tmp == "double") {
1502 new_spacing = Spacing::Double;
1503 } else if (tmp == "other") {
1504 new_spacing = Spacing::Other;
1507 lyxerr << "new_value = " << tmpval << endl;
1510 } else if (tmp == "default") {
1511 new_spacing = Spacing::Default;
1513 lyxerr << _("Unknown spacing argument: ")
1514 << ev.argument << endl;
1516 if (cur_spacing != new_spacing || cur_value != new_value) {
1517 par->params().spacing(Spacing(new_spacing, new_value));
1518 updwhat = CURSOR_PAR;
1525 if (!bv->dispatch(ev))
1526 result = UNDISPATCHED;
1533 updateLocal(bv, updwhat, updflag);
1534 /// If the action has deleted all text in the inset, we need to change the
1535 // language to the language of the surronding text.
1536 if (!was_empty && par->empty() && !par->next()) {
1537 LyXFont font(LyXFont::ALL_IGNORE);
1538 font.setLanguage(bv->getParentLanguage(this));
1539 setFont(bv, font, false);
1542 if (result >= FINISHED)
1543 bv->unlockInset(this);
1545 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1546 result = DISPATCHED;
1551 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1554 buf->latexParagraphs(os, par, 0, texrow, moving_arg);
1555 return texrow.rows();
1559 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1561 Paragraph * p = par;
1562 unsigned int lines = 0;
1565 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1566 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1574 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1576 Paragraph * p = par;
1577 unsigned int lines = 0;
1579 vector<string> environment_stack(10);
1580 vector<string> environment_inner(10);
1582 int const command_depth = 0;
1585 Paragraph::depth_type depth = 0; // paragraph depth
1589 int desc_on = 0; // description mode
1591 LyXLayout_ptr const & style = p->layout();
1593 // environment tag closing
1594 for (; depth > p->params().depth(); --depth) {
1595 if (environment_inner[depth] != "!-- --") {
1596 item_name = "listitem";
1597 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1598 if (environment_inner[depth] == "varlistentry")
1599 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1601 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1602 environment_stack[depth].erase();
1603 environment_inner[depth].erase();
1606 if (depth == p->params().depth()
1607 && environment_stack[depth] != style->latexname()
1608 && !environment_stack[depth].empty()) {
1609 if (environment_inner[depth] != "!-- --") {
1610 item_name= "listitem";
1611 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1612 if (environment_inner[depth] == "varlistentry")
1613 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1616 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1618 environment_stack[depth].erase();
1619 environment_inner[depth].erase();
1622 // Write opening SGML tags.
1623 switch (style->latextype) {
1624 case LATEX_PARAGRAPH:
1625 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1629 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1633 case LATEX_ENVIRONMENT:
1634 case LATEX_ITEM_ENVIRONMENT:
1635 if (depth < p->params().depth()) {
1636 depth = p->params().depth();
1637 environment_stack[depth].erase();
1640 if (environment_stack[depth] != style->latexname()) {
1641 if (environment_stack.size() == depth + 1) {
1642 environment_stack.push_back("!-- --");
1643 environment_inner.push_back("!-- --");
1645 environment_stack[depth] = style->latexname();
1646 environment_inner[depth] = "!-- --";
1647 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1649 if (environment_inner[depth] != "!-- --") {
1650 item_name= "listitem";
1651 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1652 if (environment_inner[depth] == "varlistentry")
1653 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1657 if (style->latextype == LATEX_ENVIRONMENT) {
1658 if (!style->latexparam().empty()) {
1659 if (style->latexparam() == "CDATA")
1662 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1667 desc_on = (style->labeltype == LABEL_MANUAL);
1669 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1670 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1672 item_name = desc_on?"term":"para";
1673 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1677 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1681 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1685 // write closing SGML tags
1686 switch (style->latextype) {
1687 case LATEX_ENVIRONMENT:
1688 if (!style->latexparam().empty()) {
1689 if (style->latexparam() == "CDATA")
1692 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1695 case LATEX_ITEM_ENVIRONMENT:
1696 if (desc_on == 1) break;
1698 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1700 case LATEX_PARAGRAPH:
1701 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1704 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1710 for (int d = depth; d >= 0; --d) {
1711 if (!environment_stack[depth].empty()) {
1712 if (environment_inner[depth] != "!-- --") {
1713 item_name = "listitem";
1714 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1715 if (environment_inner[depth] == "varlistentry")
1716 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1719 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1727 void InsetText::validate(LaTeXFeatures & features) const
1729 Paragraph * p = par;
1731 p->validate(features);
1737 int InsetText::beginningOfMainBody(Paragraph * p) const
1739 if (p->layout()->labeltype != LABEL_MANUAL)
1742 return p->beginningOfMainBody();
1746 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1748 if (the_locking_inset) {
1749 the_locking_inset->getCursorPos(bv, x, y);
1752 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1753 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1757 int InsetText::insetInInsetY() const
1759 if (!the_locking_inset)
1762 return (inset_y + the_locking_inset->insetInInsetY());
1766 void InsetText::toggleInsetCursor(BufferView * bv)
1768 if (the_locking_inset) {
1769 the_locking_inset->toggleInsetCursor(bv);
1773 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1775 int const asc = font_metrics::maxAscent(font);
1776 int const desc = font_metrics::maxDescent(font);
1778 if (isCursorVisible())
1779 bv->hideLockedInsetCursor();
1781 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1782 toggleCursorVisible();
1786 void InsetText::showInsetCursor(BufferView * bv, bool show)
1788 if (the_locking_inset) {
1789 the_locking_inset->showInsetCursor(bv, show);
1792 if (!isCursorVisible()) {
1793 LyXFont const font =
1794 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1796 int const asc = font_metrics::maxAscent(font);
1797 int const desc = font_metrics::maxDescent(font);
1799 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1801 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1802 setCursorVisible(true);
1807 void InsetText::hideInsetCursor(BufferView * bv)
1809 if (isCursorVisible()) {
1810 bv->hideLockedInsetCursor();
1811 setCursorVisible(false);
1813 if (the_locking_inset)
1814 the_locking_inset->hideInsetCursor(bv);
1818 void InsetText::fitInsetCursor(BufferView * bv) const
1820 if (the_locking_inset) {
1821 the_locking_inset->fitInsetCursor(bv);
1824 LyXFont const font =
1825 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1827 int const asc = font_metrics::maxAscent(font);
1828 int const desc = font_metrics::maxDescent(font);
1830 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1831 need_update |= FULL;
1835 UpdatableInset::RESULT
1836 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1838 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1839 return moveLeftIntern(bv, false, activate_inset, selecting);
1841 return moveRightIntern(bv, true, activate_inset, selecting);
1845 UpdatableInset::RESULT
1846 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1848 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1849 return moveRightIntern(bv, true, activate_inset, selecting);
1851 return moveLeftIntern(bv, false, activate_inset, selecting);
1855 UpdatableInset::RESULT
1856 InsetText::moveRightIntern(BufferView * bv, bool front,
1857 bool activate_inset, bool selecting)
1859 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1860 return FINISHED_RIGHT;
1861 if (activate_inset && checkAndActivateInset(bv, front))
1863 getLyXText(bv)->cursorRight(bv);
1865 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1866 return DISPATCHED_NOUPDATE;
1870 UpdatableInset::RESULT
1871 InsetText::moveLeftIntern(BufferView * bv, bool front,
1872 bool activate_inset, bool selecting)
1874 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1876 getLyXText(bv)->cursorLeft(bv);
1878 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1879 if (activate_inset && checkAndActivateInset(bv, front))
1881 return DISPATCHED_NOUPDATE;
1885 UpdatableInset::RESULT
1886 InsetText::moveUp(BufferView * bv)
1888 if (!crow(bv)->previous())
1890 getLyXText(bv)->cursorUp(bv);
1891 return DISPATCHED_NOUPDATE;
1895 UpdatableInset::RESULT
1896 InsetText::moveDown(BufferView * bv)
1898 if (!crow(bv)->next())
1899 return FINISHED_DOWN;
1900 getLyXText(bv)->cursorDown(bv);
1901 return DISPATCHED_NOUPDATE;
1905 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1907 if (the_locking_inset) {
1908 if (the_locking_inset->insetAllowed(inset))
1909 return the_locking_inset->insertInset(bv, inset);
1912 inset->setOwner(this);
1913 hideInsetCursor(bv);
1914 getLyXText(bv)->insertInset(bv, inset);
1916 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1921 bool InsetText::insetAllowed(Inset::Code code) const
1923 // in_insetAllowed is a really gross hack,
1924 // to allow us to call the owner's insetAllowed
1925 // without stack overflow, which can happen
1926 // when the owner uses InsetCollapsable::insetAllowed()
1928 if (in_insetAllowed)
1930 in_insetAllowed = true;
1931 if (the_locking_inset)
1932 ret = the_locking_inset->insetAllowed(code);
1934 ret = owner()->insetAllowed(code);
1935 in_insetAllowed = false;
1940 UpdatableInset * InsetText::getLockingInset() const
1942 return the_locking_inset ? the_locking_inset->getLockingInset() :
1943 const_cast<InsetText *>(this);
1947 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1951 if (the_locking_inset)
1952 return the_locking_inset->getFirstLockingInsetOfType(c);
1957 bool InsetText::showInsetDialog(BufferView * bv) const
1959 if (the_locking_inset)
1960 return the_locking_inset->showInsetDialog(bv);
1965 vector<string> const InsetText::getLabelList() const
1967 vector<string> label_list;
1969 Paragraph * tpar = par;
1971 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1972 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1973 for (; beg != end; ++beg) {
1974 vector<string> const l = (*beg)->getLabelList();
1975 label_list.insert(label_list.end(), l.begin(), l.end());
1977 tpar = tpar->next();
1983 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1986 if (the_locking_inset) {
1987 the_locking_inset->setFont(bv, font, toggleall, selectall);
1990 if ((!par->next() && par->empty()) || cpar(bv)->empty()) {
1991 getLyXText(bv)->setFont(bv, font, toggleall);
1996 lt = getLyXText(bv);
1999 if (lt->selection.set()) {
2000 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2004 lt->toggleFree(bv, font, toggleall);
2006 lt->clearSelection();
2008 bool flag = (selectall || lt->selection.set());
2012 updateLocal(bv, FULL, true);
2014 updateLocal(bv, CURSOR_PAR, true);
2018 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2020 if (cpar(bv)->isInset(cpos(bv))) {
2022 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2023 if (!isHighlyEditableInset(inset))
2025 inset->edit(bv, front);
2026 if (!the_locking_inset)
2028 updateLocal(bv, CURSOR, false);
2035 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2036 mouse_button::state button)
2038 x -= drawTextXOffset;
2040 int dummyy = y + insetAscent;
2041 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2042 // we only do the edit() call if the inset was hit by the mouse
2043 // or if it is a highly editable inset. So we should call this
2044 // function from our own edit with button < 0.
2045 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2047 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2055 inset_x = cix(bv) - top_x + drawTextXOffset;
2056 inset_y = ciy(bv) + drawTextYOffset;
2057 inset->edit(bv, x - inset_x, y - inset_y, button);
2058 if (!the_locking_inset)
2060 updateLocal(bv, CURSOR, false);
2067 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2070 int w = UpdatableInset::getMaxWidth(bv, inset);
2075 w = w - top_x + owner()->x();
2078 w -= (2 * TEXT_TO_INSET_OFFSET);
2081 return UpdatableInset::getMaxWidth(bv, inset);
2086 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2088 // we have to unlock any locked inset otherwise we're in troubles
2089 the_locking_inset = 0;
2091 Paragraph * tmp = par->next();
2096 par = new Paragraph(*p, same_id);
2097 par->setInsetOwner(this);
2098 Paragraph * np = par;
2101 np->next(new Paragraph(*p, same_id));
2102 np->next()->previous(np);
2104 np->setInsetOwner(this);
2111 void InsetText::setText(string const & data, LyXFont const & font)
2114 for (unsigned int i=0; i < data.length(); ++i)
2115 par->insertChar(i, data[i], font);
2120 void InsetText::setAutoBreakRows(bool flag)
2122 if (flag != autoBreakRows) {
2123 autoBreakRows = flag;
2131 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2133 if (how != drawFrame_) {
2136 updateLocal(bv, DRAW_FRAME, false);
2141 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2143 if (frame_color != col) {
2146 updateLocal(bv, DRAW_FRAME, false);
2151 int InsetText::cx(BufferView * bv) const
2153 // we do nothing dangerous so we use a local cache
2154 LyXText * llt = getLyXText(bv);
2155 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2156 if (the_locking_inset) {
2157 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2159 if (font.isVisibleRightToLeft())
2160 x -= the_locking_inset->width(bv, font);
2166 int InsetText::cix(BufferView * bv) const
2168 // we do nothing dangerous so we use a local cache
2169 LyXText * llt = getLyXText(bv);
2170 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2171 if (the_locking_inset) {
2172 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2174 if (font.isVisibleRightToLeft())
2175 x -= the_locking_inset->width(bv, font);
2181 int InsetText::cy(BufferView * bv) const
2184 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2188 int InsetText::ciy(BufferView * bv) const
2191 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2195 pos_type InsetText::cpos(BufferView * bv) const
2197 return getLyXText(bv)->cursor.pos();
2201 Paragraph * InsetText::cpar(BufferView * bv) const
2203 return getLyXText(bv)->cursor.par();
2207 bool InsetText::cboundary(BufferView * bv) const
2209 return getLyXText(bv)->cursor.boundary();
2213 Row * InsetText::crow(BufferView * bv) const
2215 return getLyXText(bv)->cursor.row();
2219 LyXText * InsetText::getLyXText(BufferView const * lbv,
2220 bool const recursive) const
2222 if (cached_bview == lbv) {
2223 if (recursive && the_locking_inset)
2224 return the_locking_inset->getLyXText(lbv, true);
2225 LyXText * lt = cached_text.get();
2226 lyx::Assert(lt && lt->firstRow()->par() == par);
2229 // Super UGLY! (Lgb)
2230 BufferView * bv = const_cast<BufferView *>(lbv);
2233 Cache::iterator it = cache.find(bv);
2235 if (it != cache.end()) {
2238 } else if (do_resize) {
2239 resizeLyXText(do_resize);
2241 if (lt || !it->second.remove) {
2242 lyx::Assert(it->second.text.get());
2243 cached_text = it->second.text;
2244 if (recursive && the_locking_inset) {
2245 return the_locking_inset->getLyXText(bv, true);
2247 return cached_text.get();
2248 } else if (it->second.remove) {
2250 saveLyXTextState(it->second.text.get());
2256 // when we have to reinit the existing LyXText!
2258 it->second.text->init(bv);
2259 restoreLyXTextState(bv, it->second.text.get());
2260 it->second.remove = false;
2262 cached_text = it->second.text;
2263 if (the_locking_inset && recursive) {
2264 return the_locking_inset->getLyXText(bv);
2266 return cached_text.get();
2269 // we are here only if we don't have a BufferView * in the cache!!!
2271 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2272 cached_text->init(bv);
2273 restoreLyXTextState(bv, cached_text.get());
2275 cache.insert(make_pair(bv, cached_text));
2277 if (the_locking_inset && recursive) {
2278 return the_locking_inset->getLyXText(bv);
2280 return cached_text.get();
2284 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2288 Cache::iterator it = cache.find(bv);
2290 if (it == cache.end()) {
2294 lyx::Assert(it->second.text.get());
2296 it->second.remove = true;
2298 /// then remove all LyXText in text-insets
2299 Paragraph * p = par;
2300 for (; p; p = p->next()) {
2301 p->deleteInsetsLyXText(bv);
2307 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2310 // we cannot resize this because we are in use!
2311 // so do this on the next possible getLyXText()
2316 // lyxerr << "InsetText::resizeLyXText\n";
2317 if (!par->next() && par->empty()) { // no data, resize not neccessary!
2318 // we have to do this as a fixed width may have changed!
2319 LyXText * t = getLyXText(bv);
2320 saveLyXTextState(t);
2322 restoreLyXTextState(bv, t);
2325 // one endless line, resize normally not necessary
2326 if (!force && getMaxWidth(bv, this) < 0)
2329 Cache::iterator it = cache.find(bv);
2330 if (it == cache.end()) {
2333 lyx::Assert(it->second.text.get());
2335 LyXText * t = it->second.text.get();
2336 saveLyXTextState(t);
2337 for (Paragraph * p = par; p; p = p->next()) {
2338 p->resizeInsetsLyXText(bv);
2341 restoreLyXTextState(bv, t);
2342 if (the_locking_inset) {
2343 inset_x = cix(bv) - top_x + drawTextXOffset;
2344 inset_y = ciy(bv) + drawTextYOffset;
2347 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2349 updateLocal(bv, FULL, false);
2350 // this will scroll the screen such that the cursor becomes visible
2351 bv->updateScrollbar();
2353 need_update |= FULL;
2358 void InsetText::reinitLyXText() const
2361 // we cannot resize this because we are in use!
2362 // so do this on the next possible getLyXText()
2368 // lyxerr << "InsetText::reinitLyXText\n";
2369 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2370 lyx::Assert(it->second.text.get());
2372 LyXText * t = it->second.text.get();
2373 BufferView * bv = it->first;
2375 saveLyXTextState(t);
2376 for (Paragraph * p = par; p; p = p->next()) {
2377 p->resizeInsetsLyXText(bv);
2380 restoreLyXTextState(bv, t);
2381 if (the_locking_inset) {
2382 inset_x = cix(bv) - top_x + drawTextXOffset;
2383 inset_y = ciy(bv) + drawTextYOffset;
2385 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2387 updateLocal(bv, FULL, false);
2388 // this will scroll the screen such that the cursor becomes visible
2389 bv->updateScrollbar();
2397 void InsetText::removeNewlines()
2399 bool changed = false;
2401 for (Paragraph * p = par; p; p = p->next()) {
2402 for (int i = 0; i < p->size(); ++i) {
2403 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2414 bool InsetText::nodraw() const
2416 if (the_locking_inset)
2417 return the_locking_inset->nodraw();
2418 return UpdatableInset::nodraw();
2422 int InsetText::scroll(bool recursive) const
2424 int sx = UpdatableInset::scroll(false);
2426 if (recursive && the_locking_inset)
2427 sx += the_locking_inset->scroll(recursive);
2433 bool InsetText::doClearArea() const
2435 return !locked || (need_update & (FULL|INIT));
2439 void InsetText::selectAll(BufferView * bv)
2441 getLyXText(bv)->cursorTop(bv);
2442 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2443 getLyXText(bv)->cursorBottom(bv);
2444 getLyXText(bv)->setSelection(bv);
2448 void InsetText::clearSelection(BufferView * bv)
2450 getLyXText(bv)->clearSelection();
2454 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2456 Painter & pain = bv->painter();
2458 int h = insetAscent + insetDescent;
2459 int ty = baseline - insetAscent;
2465 if ((ty + h) > pain.paperHeight())
2466 h = pain.paperHeight();
2467 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2468 w = pain.paperWidth();
2469 // w -= TEXT_TO_INSET_OFFSET;
2470 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2473 frame_is_visible = false;
2477 Paragraph * InsetText::getParFromID(int id) const
2480 Paragraph * result = par;
2481 Paragraph * ires = 0;
2482 while (result && result->id() != id) {
2483 if ((ires = result->getParFromID(id)))
2485 result = result->next();
2489 Paragraph * tmp = par;
2491 if (tmp->id() == id) {
2494 Paragraph * tmp2 = tmp->getParFromID(id);
2505 Paragraph * InsetText::firstParagraph() const
2508 if (the_locking_inset)
2509 if ((result = the_locking_inset->firstParagraph()))
2515 Paragraph * InsetText::getFirstParagraph(int i) const
2517 return (i == 0) ? par : 0;
2521 LyXCursor const & InsetText::cursor(BufferView * bv) const
2523 if (the_locking_inset)
2524 return the_locking_inset->cursor(bv);
2525 return getLyXText(bv)->cursor;
2529 Paragraph * InsetText::paragraph() const
2535 void InsetText::paragraph(Paragraph * p)
2537 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2538 // caller of this function has to take care of it. This IS important
2539 // as we could have to insert a paragraph before this one and just
2540 // link the actual to a new ones next and set it with this function
2543 // set ourself as owner for all the paragraphs inserted!
2544 Paragraph * np = par;
2546 np->setInsetOwner(this);
2550 // redraw myself when asked for
2555 Inset * InsetText::getInsetFromID(int id_arg) const
2558 return const_cast<InsetText *>(this);
2560 Paragraph * lp = par;
2563 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2564 en = lp->inset_iterator_end();
2567 if ((*it)->id() == id_arg)
2569 Inset * in = (*it)->getInsetFromID(id_arg);
2579 WordLangTuple InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2585 lt = getLyXText(bv);
2588 if (the_locking_inset) {
2589 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2590 if (!word.word().empty()) {
2596 // we have to go on checking so move cursor to the next char
2597 lt->cursor.pos(lt->cursor.pos() + 1);
2599 word = lt->selectNextWordToSpellcheck(bv, value);
2600 if (word.word().empty())
2601 bv->unlockInset(const_cast<InsetText *>(this));
2610 void InsetText::selectSelectedWord(BufferView * bv)
2612 if (the_locking_inset) {
2613 the_locking_inset->selectSelectedWord(bv);
2616 getLyXText(bv)->selectSelectedWord(bv);
2617 updateLocal(bv, SELECTION, false);
2621 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2623 if (the_locking_inset) {
2624 the_locking_inset->toggleSelection(bv, kill_selection);
2628 lt = getLyXText(bv);
2632 int x = top_x + TEXT_TO_INSET_OFFSET;
2634 Row * row = lt->firstRow();
2635 int y_offset = top_baseline - row->ascent_of_text();
2637 while ((row != 0) && ((y+row->height()) <= 0)) {
2644 if (need_update & SELECTION)
2646 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2652 bool InsetText::searchForward(BufferView * bv, string const & str,
2657 lt = getLyXText(bv);
2660 if (the_locking_inset) {
2661 if (the_locking_inset->searchForward(bv, str, cs, mw))
2663 lt->cursorRight(bv, true);
2665 lyxfind::SearchResult result =
2666 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2668 if (result == lyxfind::SR_FOUND) {
2669 LyXCursor cur = lt->cursor;
2670 bv->unlockInset(bv->theLockingInset());
2671 if (bv->lockInset(this))
2674 lt->setSelectionOverString(bv, str);
2675 updateLocal(bv, SELECTION, false);
2679 return (result != lyxfind::SR_NOT_FOUND);
2682 bool InsetText::searchBackward(BufferView * bv, string const & str,
2685 if (the_locking_inset) {
2686 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2691 lt = getLyXText(bv);
2695 Paragraph * p = par;
2698 lt->setCursor(bv, p, p->size());
2700 lyxfind::SearchResult result =
2701 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2703 if (result == lyxfind::SR_FOUND) {
2704 LyXCursor cur = lt->cursor;
2705 bv->unlockInset(bv->theLockingInset());
2706 if (bv->lockInset(this))
2709 lt->setSelectionOverString(bv, str);
2710 updateLocal(bv, SELECTION, false);
2714 return (result != lyxfind::SR_NOT_FOUND);
2718 bool InsetText::checkInsertChar(LyXFont & font)
2721 return owner()->checkInsertChar(font);
2726 void InsetText::collapseParagraphs(BufferView * bv) const
2728 BufferParams const & bparams = bv->buffer()->params;
2729 LyXText * llt = getLyXText(bv);
2731 while(par->next()) {
2732 if (!par->empty() && !par->next()->empty() &&
2733 !par->isSeparator(par->size() - 1))
2735 par->insertChar(par->size(), ' ');
2737 if (llt->selection.set()) {
2738 if (llt->selection.start.par() == par->next()) {
2739 llt->selection.start.par(par);
2740 llt->selection.start.pos(
2741 llt->selection.start.pos() + par->size());
2743 if (llt->selection.end.par() == par->next()) {
2744 llt->selection.end.par(par);
2745 llt->selection.end.pos(
2746 llt->selection.end.pos() + par->size());
2749 par->pasteParagraph(bparams);
2755 void InsetText::getDrawFont(LyXFont & font) const
2759 owner()->getDrawFont(font);
2763 void InsetText::appendParagraphs(BufferParams const & bparams,
2767 Paragraph * tmpbuf = newpar;
2768 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2770 while (tmpbuf->next()) {
2771 tmpbuf = tmpbuf->next();
2772 lastbuffer->next(new Paragraph(*tmpbuf, false));
2773 lastbuffer->next()->previous(lastbuffer);
2774 lastbuffer = lastbuffer->next();
2777 while (lastbuffer->next())
2778 lastbuffer = lastbuffer->next();
2779 if (!newpar->empty() && !lastbuffer->empty() &&
2780 !lastbuffer->isSeparator(lastbuffer->size() - 1))
2782 lastbuffer->insertChar(lastbuffer->size(), ' ');
2785 // make the buf exactly the same layout than our last paragraph
2786 buf->makeSameLayout(lastbuffer);
2789 lastbuffer->next(buf);
2790 buf->previous(lastbuffer);
2791 lastbuffer->pasteParagraph(bparams);
2797 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2799 Paragraph * par = getFirstParagraph(0);
2801 Paragraph::inset_iterator it = par->inset_iterator_begin();
2802 Paragraph::inset_iterator end = par->inset_iterator_end();
2803 for (; it != end; ++it) {
2804 it->addPreview(loader);