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"
22 #include "commandtags.h"
24 #include "frontends/LyXView.h"
25 #include "BufferView.h"
26 #include "LaTeXFeatures.h"
27 #include "frontends/Painter.h"
29 #include "lyxcursor.h"
30 #include "CutAndPaste.h"
31 #include "frontends/font_metrics.h"
36 #include "trans_mgr.h"
37 #include "frontends/screen.h"
40 #include "ParagraphParameters.h"
41 #include "undo_funcs.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;
195 last_drawn_width = -1;
196 frame_is_visible = false;
199 in_insetAllowed = false;
203 InsetText::~InsetText()
210 Paragraph * tmp = par->next();
217 void InsetText::clear()
219 // This is a gross hack...
220 LyXLayout_ptr old_layout = par->layout();
223 Paragraph * tmp = par->next();
228 par->setInsetOwner(this);
229 par->layout(old_layout);
236 Inset * InsetText::clone(Buffer const &, bool same_id) const
238 return new InsetText(*this, same_id);
242 void InsetText::write(Buffer const * buf, ostream & os) const
245 writeParagraphData(buf, os);
249 void InsetText::writeParagraphData(Buffer const * buf, ostream & os) const
251 par->writeFile(buf, os, buf->params, 0);
255 void InsetText::read(Buffer const * buf, LyXLex & lex)
259 Paragraph * return_par = 0;
260 Paragraph::depth_type depth = 0;
261 LyXFont font(LyXFont::ALL_INHERIT);
267 token = lex.getString();
270 if (token == "\\end_inset") {
271 #ifndef NO_COMPABILITY
272 const_cast<Buffer*>(buf)->insertErtContents(par, pos, false);
277 if (const_cast<Buffer*>(buf)->
278 parseSingleLyXformat2Token(lex, par, return_par,
279 token, pos, depth, font)) {
280 // the_end read this should NEVER happen
281 lex.printError("\\the_end read in inset! Error in document!");
289 return_par->setInsetOwner(this);
290 return_par = return_par->next();
293 if (token != "\\end_inset") {
294 lex.printError("Missing \\end_inset at this point. "
301 int InsetText::ascent(BufferView * bv, LyXFont const &) const
303 insetAscent = getLyXText(bv)->firstRow()->ascent_of_text() +
304 TEXT_TO_INSET_OFFSET;
309 int InsetText::descent(BufferView * bv, LyXFont const &) const
311 LyXText * llt = getLyXText(bv);
312 insetDescent = llt->height - llt->firstRow()->ascent_of_text() +
313 TEXT_TO_INSET_OFFSET;
318 int InsetText::width(BufferView * bv, LyXFont const &) const
320 insetWidth = max(textWidth(bv), (int)getLyXText(bv)->width) +
321 (2 * TEXT_TO_INSET_OFFSET);
322 insetWidth = max(insetWidth, 10);
327 int InsetText::textWidth(BufferView * bv, bool fordraw) const
330 if (!autoBreakRows) {
333 w = getMaxWidth(bv, this);
336 return max(w - (2 * TEXT_TO_INSET_OFFSET),
337 (int)getLyXText(bv)->width);
341 return w - (2 * TEXT_TO_INSET_OFFSET);
345 void InsetText::draw(BufferView * bv, LyXFont const & f,
346 int baseline, float & x, bool cleared) const
351 Painter & pain = bv->painter();
353 // this is the first thing we have to ask because if the x pos
354 // changed we have to do a complete rebreak of the text as we
355 // may have few space to draw in. Well we should check on this too
357 if (top_x != int(x)) {
360 int nw = getMaxWidth(bv, this);
361 if (nw > 0 && old_max_width != nw) {
364 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
369 // call these methods so that insetWidth, insetAscent and
370 // insetDescent have the right values.
375 // repaint the background if needed
376 if (cleared && backgroundColor() != LColor::background) {
377 clearInset(bv, baseline, cleared);
380 // no draw is necessary !!!
381 if ((drawFrame_ == LOCKED) && !locked && !par->size()) {
382 top_baseline = baseline;
384 if (need_update & CLEAR_FRAME)
385 clearFrame(pain, cleared);
392 x += static_cast<float>(scroll());
394 // if top_x differs we did it already
395 if (!cleared && (old_x == int(x))
396 && ((need_update&(INIT|FULL)) || (top_baseline != baseline)
397 ||(last_drawn_width != insetWidth)))
399 // Condition necessary to eliminate bug 59 attachment 37
401 clearInset(bv, baseline, cleared);
405 frame_is_visible = false;
407 if (!cleared && (need_update == NONE)) {
409 drawFrame(pain, cleared);
413 top_baseline = baseline;
414 top_y = baseline - insetAscent;
416 if (last_drawn_width != insetWidth) {
418 clearInset(bv, baseline, cleared);
420 last_drawn_width = insetWidth;
423 if (the_locking_inset && (cpar(bv) == inset_par)
424 && (cpos(bv) == inset_pos)) {
425 inset_x = cix(bv) - top_x + drawTextXOffset;
426 inset_y = ciy(bv) + drawTextYOffset;
428 if (!cleared && (need_update == CURSOR)
429 && !getLyXText(bv)->selection.set()) {
430 drawFrame(pain, cleared);
440 x += TEXT_TO_INSET_OFFSET;
442 Row * row = lt->firstRow();
443 int y_offset = baseline - row->ascent_of_text();
444 int ph = pain.paperHeight();
447 while ((row != 0) && ((y+row->height()) <= 0)) {
449 first += row->height();
453 lt->first_y = -y_offset;
460 if (cleared || (need_update&(INIT|FULL))) {
461 int yf = y_offset + first;
463 while ((row != 0) && (yf < ph)) {
464 lt->getVisibleRow(bv, y + y_offset + first, int(x),
465 row, y + lt->first_y, cleared);
466 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
467 lt->need_break_row = row;
469 lt->setCursor(bv, lt->cursor.par(),
471 if (lt->selection.set()) {
472 lt->setCursor(bv, lt->selection.start,
473 lt->selection.start.par(),
474 lt->selection.start.pos());
475 lt->setCursor(bv, lt->selection.end,
476 lt->selection.end.par(),
477 lt->selection.end.pos());
485 } else if (!locked) {
486 if (need_update & CURSOR) {
487 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
488 lt->clearSelection();
489 lt->selection.cursor = lt->cursor;
491 bv->screen().update(lt, bv, y_offset, int(x));
494 if (need_update & SELECTION) {
495 bv->screen().toggleToggle(lt, bv, y_offset, int(x));
496 } else if (need_update & CURSOR) {
497 bv->screen().toggleSelection(lt, bv, true, y_offset,int(x));
498 lt->clearSelection();
499 lt->selection.cursor = lt->cursor;
501 bv->screen().update(lt, bv, y_offset, int(x));
506 lt->status(bv, LyXText::UNCHANGED);
507 if ((drawFrame_ == ALWAYS) ||
508 ((cleared || (need_update != CURSOR_PAR)) &&
509 (drawFrame_ == LOCKED) && locked))
511 drawFrame(pain, cleared);
512 } else if (need_update & CLEAR_FRAME) {
513 clearFrame(pain, cleared);
516 x += insetWidth - TEXT_TO_INSET_OFFSET;
518 if (bv->text->status() == LyXText::CHANGED_IN_DRAW) {
520 } else if (need_update != INIT) {
528 void InsetText::drawFrame(Painter & pain, bool cleared) const
530 static int const ttoD2 = TEXT_TO_INSET_OFFSET / 2;
531 if (!frame_is_visible || cleared) {
532 frame_x = top_x + ttoD2;
533 frame_y = top_baseline - insetAscent + ttoD2;
534 frame_w = insetWidth - TEXT_TO_INSET_OFFSET;
535 frame_h = insetAscent + insetDescent - TEXT_TO_INSET_OFFSET;
536 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
538 frame_is_visible = true;
543 void InsetText::clearFrame(Painter & pain, bool cleared) const
545 if (frame_is_visible) {
547 pain.rectangle(frame_x, frame_y, frame_w, frame_h,
550 frame_is_visible = false;
555 void InsetText::update(BufferView * bv, LyXFont const & font, bool reinit)
558 if (reinit && owner()) {
560 owner()->update(bv, font, true);
565 if (reinit || need_update == INIT) {
567 // we should put this call where we set need_update to INIT!
570 owner()->update(bv, font, true);
575 if (!autoBreakRows && par->next())
576 collapseParagraphs(bv);
578 if (the_locking_inset) {
579 inset_x = cix(bv) - top_x + drawTextXOffset;
580 inset_y = ciy(bv) + drawTextYOffset;
581 the_locking_inset->update(bv, font, reinit);
589 if ((need_update & CURSOR_PAR) && (lt->status() == LyXText::UNCHANGED) &&
592 lt->updateInset(bv, the_locking_inset);
594 if (lt->status() == LyXText::NEED_MORE_REFRESH)
602 void InsetText::setUpdateStatus(BufferView * bv, int what) const
604 // this does nothing dangerous so use only a localized buffer
605 LyXText * llt = getLyXText(bv);
608 // we have to redraw us full if our LyXText NEEDS_MORE_REFRES or
609 // if we don't break row so that we only have one row to update!
610 if ((llt->status() == LyXText::NEED_MORE_REFRESH) ||
612 (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH)))
615 } else if (llt->status() == LyXText::NEED_VERY_LITTLE_REFRESH) {
616 need_update |= CURSOR_PAR;
619 // this to not draw a selection when we redraw all of it!
620 if (need_update & CURSOR && !(need_update & SELECTION)) {
621 if (llt->selection.set())
623 llt->clearSelection();
628 void InsetText::updateLocal(BufferView * bv, int what, bool mark_dirty) const
630 if (!autoBreakRows && par->next())
631 collapseParagraphs(bv);
638 setUpdateStatus(bv, what);
639 bool flag = mark_dirty ||
640 (((need_update != CURSOR) && (need_update != NONE)) ||
641 (lt->status() != LyXText::UNCHANGED) || lt->selection.set());
642 if (!lt->selection.set())
643 lt->selection.cursor = lt->cursor;
647 // IMO this is not anymore needed as we do this in fitInsetCursor!
648 // and we always get "true" as returnvalue of this function in the
649 // case of a locking inset (Jug 20020412)
650 if (locked && (need_update & CURSOR) && bv->fitCursor())
656 bv->updateInset(const_cast<InsetText *>(this), mark_dirty);
658 if (need_update == CURSOR)
660 bv->owner()->view_state_changed();
661 bv->owner()->updateMenubar();
662 bv->owner()->updateToolbar();
663 if (old_par != cpar(bv)) {
664 bv->owner()->setLayout(cpar(bv)->layout()->name());
670 string const InsetText::editMessage() const
672 return _("Opened Text Inset");
676 void InsetText::edit(BufferView * bv, int x, int y, mouse_button::state button)
678 UpdatableInset::edit(bv, x, y, button);
680 if (!bv->lockInset(this)) {
681 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
685 the_locking_inset = 0;
686 inset_pos = inset_x = inset_y = 0;
687 inset_boundary = false;
690 int tmp_y = (y < 0) ? 0 : y;
696 // we put here -1 and not button as now the button in the
697 // edit call should not be needed we will fix this in 1.3.x
698 // cycle hopefully (Jug 20020509)
699 // FIXME: GUII I've changed this to none: probably WRONG
700 if (!checkAndActivateInset(bv, x, tmp_y, mouse_button::none)) {
701 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
703 lt->cursor.x_fix(lt->cursor.x());
705 lt->clearSelection();
707 // If the inset is empty set the language of the current font to the
708 // language to the surronding text (if different).
709 if (par->size() == 0 && !par->next() &&
710 bv->getParentLanguage(this) != lt->current_font.language())
712 LyXFont font(LyXFont::ALL_IGNORE);
713 font.setLanguage(bv->getParentLanguage(this));
714 setFont(bv, font, false);
720 if (drawFrame_ == LOCKED)
721 code = CURSOR|DRAW_FRAME;
722 updateLocal(bv, code, false);
725 // Tell the paragraph dialog that we've entered an insettext.
726 bv->owner()->getDialogs()->updateParagraph();
730 void InsetText::edit(BufferView * bv, bool front)
732 UpdatableInset::edit(bv, front);
734 if (!bv->lockInset(this)) {
735 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
739 the_locking_inset = 0;
740 inset_pos = inset_x = inset_y = 0;
741 inset_boundary = false;
750 lt->setCursor(bv, par, 0);
755 // int const pos = (p->size() ? p->size()-1 : p->size());
756 lt->setCursor(bv, p, p->size());
758 lt->clearSelection();
760 // If the inset is empty set the language of the current font to the
761 // language to the surronding text (if different).
762 if (par->size() == 0 && !par->next() &&
763 bv->getParentLanguage(this) != lt->current_font.language()) {
764 LyXFont font(LyXFont::ALL_IGNORE);
765 font.setLanguage(bv->getParentLanguage(this));
766 setFont(bv, font, false);
771 if (drawFrame_ == LOCKED)
772 code = CURSOR|DRAW_FRAME;
773 updateLocal(bv, code, false);
778 void InsetText::insetUnlock(BufferView * bv)
780 if (the_locking_inset) {
781 the_locking_inset->insetUnlock(bv);
782 the_locking_inset = 0;
783 updateLocal(bv, CURSOR_PAR, false);
790 if (drawFrame_ == LOCKED)
791 code = CURSOR|CLEAR_FRAME;
795 if (drawFrame_ == LOCKED)
803 if (lt->selection.set()) {
804 lt->clearSelection();
806 } else if (owner()) {
807 bv->owner()->setLayout(owner()->getLyXText(bv)
808 ->cursor.par()->layout()->name());
810 bv->owner()->setLayout(bv->text->cursor.par()->layout()->name());
811 // hack for deleteEmptyParMech
813 lt->setCursor(bv, par, 0);
814 } else if (par->next()) {
815 lt->setCursor(bv, par->next(), 0);
820 updateLocal(bv, code, false);
823 setUpdateStatus(bv, code);
828 void InsetText::lockInset(BufferView * bv)
831 the_locking_inset = 0;
832 inset_pos = inset_x = inset_y = 0;
833 inset_boundary = false;
841 lt->setCursor(bv, par, 0);
842 lt->clearSelection();
844 // If the inset is empty set the language of the current font to the
845 // language to the surronding text (if different).
846 if (par->size() == 0 && !par->next() &&
847 bv->getParentLanguage(this) != lt->current_font.language()) {
848 LyXFont font(LyXFont::ALL_IGNORE);
849 font.setLanguage(bv->getParentLanguage(this));
850 setFont(bv, font, false);
855 if (drawFrame_ == LOCKED)
856 code = CURSOR|DRAW_FRAME;
857 setUpdateStatus(bv, code);
861 void InsetText::lockInset(BufferView * bv, UpdatableInset * inset)
863 the_locking_inset = inset;
864 inset_x = cix(bv) - top_x + drawTextXOffset;
865 inset_y = ciy(bv) + drawTextYOffset;
866 inset_pos = cpos(bv);
867 inset_par = cpar(bv);
868 inset_boundary = cboundary(bv);
869 updateLocal(bv, CURSOR, false);
873 bool InsetText::lockInsetInInset(BufferView * bv, UpdatableInset * inset)
875 lyxerr[Debug::INSETS] << "InsetText::LockInsetInInset("
879 if (!the_locking_inset) {
881 int const id = inset->id();
883 Paragraph::inset_iterator it =
884 p->inset_iterator_begin();
885 Paragraph::inset_iterator const end =
886 p->inset_iterator_end();
887 for (; it != end; ++it) {
888 if ((*it) == inset) {
889 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
890 lockInset(bv, inset);
893 if ((*it)->getInsetFromID(id)) {
894 getLyXText(bv)->setCursorIntern(bv, p, it.getPos());
896 return the_locking_inset->lockInsetInInset(bv, inset);
903 if (inset == cpar(bv)->getInset(cpos(bv))) {
904 lyxerr[Debug::INSETS] << "OK" << endl;
905 lockInset(bv, inset);
907 } else if (the_locking_inset && (the_locking_inset == inset)) {
908 if (cpar(bv) == inset_par && cpos(bv) == inset_pos) {
909 lyxerr[Debug::INSETS] << "OK" << endl;
910 inset_x = cix(bv) - top_x + drawTextXOffset;
911 inset_y = ciy(bv) + drawTextYOffset;
913 lyxerr[Debug::INSETS] << "cursor.pos != inset_pos" << endl;
915 } else if (the_locking_inset) {
916 lyxerr[Debug::INSETS] << "MAYBE" << endl;
917 return the_locking_inset->lockInsetInInset(bv, inset);
919 lyxerr[Debug::INSETS] << "NOT OK" << endl;
924 bool InsetText::unlockInsetInInset(BufferView * bv, UpdatableInset * inset,
927 if (!the_locking_inset)
929 if (the_locking_inset == inset) {
930 the_locking_inset->insetUnlock(bv);
931 getLyXText(bv)->updateInset(bv, inset);
932 the_locking_inset = 0;
934 moveRightIntern(bv, true, false);
935 old_par = 0; // force layout setting
939 updateLocal(bv, CURSOR, false);
942 return the_locking_inset->unlockInsetInInset(bv, inset, lr);
946 bool InsetText::updateInsetInInset(BufferView * bv, Inset * inset)
948 if (!autoBreakRows && par->next())
949 collapseParagraphs(bv);
957 if (inset->owner() != this) {
958 int ustat = CURSOR_PAR;
960 UpdatableInset * tl_inset = the_locking_inset;
962 found = tl_inset->updateInsetInInset(bv, inset);
964 tl_inset = static_cast<UpdatableInset *>(inset);
965 while(tl_inset->owner() && tl_inset->owner() != this)
966 tl_inset = static_cast<UpdatableInset *>(tl_inset->owner());
967 if (!tl_inset->owner())
969 found = tl_inset->updateInsetInInset(bv, inset);
973 lt->updateInset(bv, tl_inset);
977 setUpdateStatus(bv, ustat);
980 bool found = lt->updateInset(bv, inset);
984 setUpdateStatus(bv, CURSOR_PAR);
985 if (the_locking_inset &&
986 cpar(bv) == inset_par && cpos(bv) == inset_pos)
988 inset_x = cix(bv) - top_x + drawTextXOffset;
989 inset_y = ciy(bv) + drawTextYOffset;
996 void InsetText::insetButtonPress(BufferView * bv,
997 int x, int y, mouse_button::state button)
1001 // use this to check mouse motion for selection!
1008 int tmp_x = x - drawTextXOffset;
1009 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1010 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1012 hideInsetCursor(bv);
1013 if (the_locking_inset) {
1014 if (the_locking_inset == inset) {
1015 the_locking_inset->insetButtonPress(bv,
1023 // otherwise unlock the_locking_inset and lock the new inset
1024 the_locking_inset->insetUnlock(bv);
1025 inset_x = cix(bv) - top_x + drawTextXOffset;
1026 inset_y = ciy(bv) + drawTextYOffset;
1027 the_locking_inset = 0;
1028 inset->insetButtonPress(bv, x - inset_x,
1029 y - inset_y, button);
1030 // inset->edit(bv, x - inset_x, y - inset_y, button);
1031 if (the_locking_inset)
1032 updateLocal(bv, CURSOR, false);
1036 // otherwise only unlock the_locking_inset
1037 the_locking_inset->insetUnlock(bv);
1038 the_locking_inset = 0;
1041 no_selection = false;
1043 if (bv->theLockingInset()) {
1044 if (isHighlyEditableInset(inset)) {
1045 // We just have to lock the inset before calling a
1046 // PressEvent on it!
1047 UpdatableInset * uinset = static_cast<UpdatableInset*>(inset);
1048 if (!bv->lockInset(uinset)) {
1049 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1051 inset->insetButtonPress(bv, x - inset_x, y - inset_y, button);
1052 if (the_locking_inset)
1053 updateLocal(bv, CURSOR, false);
1057 if (!inset) { // && (button == mouse_button::button2)) {
1058 bool paste_internally = false;
1059 if ((button == mouse_button::button2) && getLyXText(bv)->selection.set()) {
1060 localDispatch(bv, LFUN_COPY, "");
1061 paste_internally = true;
1065 lt = getLyXText(bv);
1068 int old_first_y = lt->first_y;
1070 lt->setCursorFromCoordinates(bv, x - drawTextXOffset,
1072 // set the selection cursor!
1073 lt->selection.cursor = lt->cursor;
1074 lt->cursor.x_fix(lt->cursor.x());
1076 if (lt->selection.set()) {
1077 lt->clearSelection();
1080 updateLocal(bv, FULL, false);
1082 lt->clearSelection();
1085 updateLocal(bv, CURSOR, false);
1088 bv->owner()->setLayout(cpar(bv)->layout()->name());
1090 // we moved the view we cannot do mouse selection in this case!
1091 if (getLyXText(bv)->first_y != old_first_y)
1092 no_selection = true;
1094 // Insert primary selection with middle mouse
1095 // if there is a local selection in the current buffer,
1097 if (button == mouse_button::button2) {
1098 if (paste_internally)
1099 localDispatch(bv, LFUN_PASTE, "");
1101 localDispatch(bv, LFUN_PASTESELECTION,
1105 getLyXText(bv)->clearSelection();
1107 showInsetCursor(bv);
1111 bool InsetText::insetButtonRelease(BufferView * bv,
1112 int x, int y, mouse_button::state button)
1114 no_selection = true;
1115 if (the_locking_inset) {
1116 return the_locking_inset->insetButtonRelease(bv,
1117 x - inset_x, y - inset_y,
1120 int tmp_x = x - drawTextXOffset;
1121 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1122 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1125 if (isHighlyEditableInset(inset)) {
1126 ret = inset->insetButtonRelease(bv, x - inset_x,
1127 y - inset_y, button);
1129 inset_x = cix(bv) - top_x + drawTextXOffset;
1130 inset_y = ciy(bv) + drawTextYOffset;
1131 ret = inset->insetButtonRelease(bv, x - inset_x,
1132 y - inset_y, button);
1133 inset->edit(bv, x - inset_x,
1134 y - inset_y, button);
1136 updateLocal(bv, CURSOR_PAR, false);
1142 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1144 if (the_locking_inset) {
1145 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1150 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1155 lt = getLyXText(bv);
1158 hideInsetCursor(bv);
1159 LyXCursor cur = lt->cursor;
1160 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1161 lt->cursor.x_fix(lt->cursor.x());
1162 if (cur == lt->cursor) {
1167 lt->setSelection(bv);
1168 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1169 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1173 updateLocal(bv, SELECTION, false);
1175 showInsetCursor(bv);
1179 UpdatableInset::RESULT
1180 InsetText::localDispatch(BufferView * bv,
1181 kb_action action, string const & arg)
1183 bool was_empty = par->size() == 0 && !par->next();
1184 no_selection = false;
1185 UpdatableInset::RESULT
1186 result= UpdatableInset::localDispatch(bv, action, arg);
1187 if (result != UNDISPATCHED) {
1191 result = DISPATCHED;
1192 if ((action < 0) && arg.empty())
1195 if (the_locking_inset) {
1196 result = the_locking_inset->localDispatch(bv, action, arg);
1197 if (result == DISPATCHED_NOUPDATE)
1199 else if (result == DISPATCHED) {
1200 updateLocal(bv, CURSOR_PAR, false);
1202 } else if (result >= FINISHED) {
1204 case FINISHED_RIGHT:
1205 moveRightIntern(bv, false, false);
1206 result = DISPATCHED;
1209 if ((result = moveUp(bv)) >= FINISHED) {
1210 updateLocal(bv, CURSOR, false);
1211 bv->unlockInset(this);
1216 LyXText *lt = getLyXText(bv);
1217 if (lt->cursor.irow()->next()) {
1218 lt->setCursorFromCoordinates(
1219 bv, lt->cursor.ix() + inset_x,
1221 lt->cursor.irow()->baseline() +
1222 lt->cursor.irow()->height() + 1);
1223 lt->cursor.x_fix(lt->cursor.x());
1224 updateLocal(bv, CURSOR, false);
1226 bv->unlockInset(this);
1231 result = DISPATCHED;
1234 the_locking_inset = 0;
1235 updateLocal(bv, CURSOR, false);
1239 hideInsetCursor(bv);
1242 lt = getLyXText(bv);
1246 int updflag = false;
1249 case LFUN_SELFINSERT:
1250 if (bv->buffer()->isReadonly()) {
1251 // setErrorMessage(N_("Document is read only"));
1255 /* Automatically delete the currently selected
1256 * text and replace it with what is being
1257 * typed in now. Depends on lyxrc settings
1258 * "auto_region_delete", which defaults to
1261 setUndo(bv, Undo::INSERT,
1262 lt->cursor.par(), lt->cursor.par()->next());
1264 if (lyxrc.auto_region_delete) {
1265 if (lt->selection.set()) {
1266 lt->cutSelection(bv, false);
1269 lt->clearSelection();
1270 for (string::size_type i = 0; i < arg.length(); ++i) {
1271 bv->owner()->getIntl()->getTransManager().
1272 TranslateAndInsert(arg[i], lt);
1275 lt->selection.cursor = lt->cursor;
1276 updwhat = CURSOR | CURSOR_PAR;
1278 result = DISPATCHED_NOUPDATE;
1280 // --- Cursor Movements -----------------------------------
1283 moveRight(bv, false, true);
1284 lt->setSelection(bv);
1285 updwhat = SELECTION;
1288 result = moveRight(bv);
1294 moveLeft(bv, false, true);
1295 lt->setSelection(bv);
1296 updwhat = SELECTION;
1300 result = moveLeft(bv);
1306 lt->setSelection(bv);
1307 updwhat = SELECTION;
1311 result = moveDown(bv);
1317 lt->setSelection(bv);
1318 updwhat = SELECTION;
1322 result = moveUp(bv);
1334 case LFUN_BACKSPACE: {
1335 setUndo(bv, Undo::DELETE,
1336 lt->cursor.par(), lt->cursor.par()->next());
1337 if (lt->selection.set())
1338 lt->cutSelection(bv);
1341 updwhat = CURSOR_PAR;
1347 setUndo(bv, Undo::DELETE,
1348 lt->cursor.par(), lt->cursor.par()->next());
1349 if (lt->selection.set()) {
1350 lt->cutSelection(bv);
1354 updwhat = CURSOR_PAR;
1360 setUndo(bv, Undo::DELETE,
1361 lt->cursor.par(), lt->cursor.par()->next());
1362 lt->cutSelection(bv);
1363 updwhat = CURSOR_PAR;
1370 lt->copySelection(bv);
1371 updwhat = CURSOR_PAR;
1373 case LFUN_PASTESELECTION:
1375 string const clip(bv->getClipboard());
1379 if (arg == "paragraph") {
1380 lt->insertStringAsParagraphs(bv, clip);
1382 lt->insertStringAsLines(bv, clip);
1385 lt->clearSelection();
1387 updwhat = CURSOR_PAR;
1392 if (!autoBreakRows) {
1394 if (CutAndPaste::nrOfParagraphs() > 1) {
1395 Alert::alert(_("Impossible operation"),
1396 _("Cannot include more than one paragraph!"),
1401 setUndo(bv, Undo::INSERT,
1402 lt->cursor.par(), lt->cursor.par()->next());
1403 lt->pasteSelection(bv);
1405 lt->clearSelection();
1406 updwhat = CURSOR_PAR;
1411 case LFUN_BREAKPARAGRAPH:
1412 if (!autoBreakRows) {
1413 result = DISPATCHED;
1416 lt->breakParagraph(bv, 0);
1417 updwhat = CURSOR | FULL;
1420 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1421 if (!autoBreakRows) {
1422 result = DISPATCHED;
1425 lt->breakParagraph(bv, 1);
1426 updwhat = CURSOR | FULL;
1430 case LFUN_BREAKLINE: {
1431 if (!autoBreakRows) {
1432 result = DISPATCHED;
1435 setUndo(bv, Undo::INSERT,
1436 lt->cursor.par(), lt->cursor.par()->next());
1437 lt->insertChar(bv, Paragraph::META_NEWLINE);
1438 updwhat = CURSOR | CURSOR_PAR;
1444 // do not set layouts on non breakable textinsets
1445 if (autoBreakRows) {
1446 string cur_layout = cpar(bv)->layout()->name();
1448 // Derive layout number from given argument (string)
1449 // and current buffer's textclass (number). */
1450 LyXTextClass const & tclass =
1451 bv->buffer()->params.getLyXTextClass();
1452 string layout = arg;
1453 bool hasLayout = tclass.hasLayout(layout);
1455 // If the entry is obsolete, use the new one instead.
1457 string const & obs =
1458 tclass[layout]->obsoleted_by();
1463 // see if we found the layout number:
1465 string const msg = string(N_("Layout ")) + arg + N_(" not known");
1466 bv->owner()->getLyXFunc()->dispatch(LFUN_MESSAGE, msg);
1470 if (cur_layout != layout) {
1471 cur_layout = layout;
1472 lt->setLayout(bv, layout);
1473 bv->owner()->setLayout(cpar(bv)->layout()->name());
1474 updwhat = CURSOR_PAR;
1478 // reset the layout box
1479 bv->owner()->setLayout(cpar(bv)->layout()->name());
1482 case LFUN_PARAGRAPH_SPACING:
1483 // This one is absolutely not working. When fiddling with this
1484 // it also seems to me that the paragraphs inside the insettext
1485 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1487 Paragraph * par = lt->cursor.par();
1488 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1489 float cur_value = 1.0;
1490 if (cur_spacing == Spacing::Other) {
1491 cur_value = par->params().spacing().getValue();
1494 istringstream istr(arg.c_str());
1497 Spacing::Space new_spacing = cur_spacing;
1498 float new_value = cur_value;
1500 lyxerr << "Missing argument to `paragraph-spacing'"
1502 } else if (tmp == "single") {
1503 new_spacing = Spacing::Single;
1504 } else if (tmp == "onehalf") {
1505 new_spacing = Spacing::Onehalf;
1506 } else if (tmp == "double") {
1507 new_spacing = Spacing::Double;
1508 } else if (tmp == "other") {
1509 new_spacing = Spacing::Other;
1512 lyxerr << "new_value = " << tmpval << endl;
1515 } else if (tmp == "default") {
1516 new_spacing = Spacing::Default;
1518 lyxerr << _("Unknown spacing argument: ")
1521 if (cur_spacing != new_spacing || cur_value != new_value) {
1522 par->params().spacing(Spacing(new_spacing, new_value));
1523 updwhat = CURSOR_PAR;
1530 if (!bv->dispatch(action, arg))
1531 result = UNDISPATCHED;
1538 updateLocal(bv, updwhat, updflag);
1539 /// If the action has deleted all text in the inset, we need to change the
1540 // language to the language of the surronding text.
1541 if (!was_empty && par->size() == 0 && !par->next()) {
1542 LyXFont font(LyXFont::ALL_IGNORE);
1543 font.setLanguage(bv->getParentLanguage(this));
1544 setFont(bv, font, false);
1547 if (result >= FINISHED)
1548 bv->unlockInset(this);
1550 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1551 result = DISPATCHED;
1556 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1559 buf->latexParagraphs(os, par, 0, texrow, moving_arg);
1560 return texrow.rows();
1564 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1566 Paragraph * p = par;
1567 unsigned int lines = 0;
1570 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1571 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1579 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1581 Paragraph * p = par;
1582 unsigned int lines = 0;
1584 vector<string> environment_stack(10);
1585 vector<string> environment_inner(10);
1587 int const command_depth = 0;
1590 Paragraph::depth_type depth = 0; // paragraph depth
1594 int desc_on = 0; // description mode
1596 LyXLayout_ptr const & style = p->layout();
1598 // environment tag closing
1599 for (; depth > p->params().depth(); --depth) {
1600 if (environment_inner[depth] != "!-- --") {
1601 item_name = "listitem";
1602 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1603 if (environment_inner[depth] == "varlistentry")
1604 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1606 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1607 environment_stack[depth].erase();
1608 environment_inner[depth].erase();
1611 if (depth == p->params().depth()
1612 && environment_stack[depth] != style->latexname()
1613 && !environment_stack[depth].empty()) {
1614 if (environment_inner[depth] != "!-- --") {
1615 item_name= "listitem";
1616 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1617 if (environment_inner[depth] == "varlistentry")
1618 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1621 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1623 environment_stack[depth].erase();
1624 environment_inner[depth].erase();
1627 // Write opening SGML tags.
1628 switch (style->latextype) {
1629 case LATEX_PARAGRAPH:
1630 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1634 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1638 case LATEX_ENVIRONMENT:
1639 case LATEX_ITEM_ENVIRONMENT:
1640 if (depth < p->params().depth()) {
1641 depth = p->params().depth();
1642 environment_stack[depth].erase();
1645 if (environment_stack[depth] != style->latexname()) {
1646 if (environment_stack.size() == depth + 1) {
1647 environment_stack.push_back("!-- --");
1648 environment_inner.push_back("!-- --");
1650 environment_stack[depth] = style->latexname();
1651 environment_inner[depth] = "!-- --";
1652 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1654 if (environment_inner[depth] != "!-- --") {
1655 item_name= "listitem";
1656 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1657 if (environment_inner[depth] == "varlistentry")
1658 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1662 if (style->latextype == LATEX_ENVIRONMENT) {
1663 if (!style->latexparam().empty()) {
1664 if (style->latexparam() == "CDATA")
1667 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1672 desc_on = (style->labeltype == LABEL_MANUAL);
1674 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1675 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1677 item_name = desc_on?"term":"para";
1678 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1682 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1686 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1690 // write closing SGML tags
1691 switch (style->latextype) {
1692 case LATEX_ENVIRONMENT:
1693 if (!style->latexparam().empty()) {
1694 if (style->latexparam() == "CDATA")
1697 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1700 case LATEX_ITEM_ENVIRONMENT:
1701 if (desc_on == 1) break;
1703 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1705 case LATEX_PARAGRAPH:
1706 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1709 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1715 for (int d = depth; d >= 0; --d) {
1716 if (!environment_stack[depth].empty()) {
1717 if (environment_inner[depth] != "!-- --") {
1718 item_name = "listitem";
1719 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1720 if (environment_inner[depth] == "varlistentry")
1721 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1724 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1732 void InsetText::validate(LaTeXFeatures & features) const
1734 Paragraph * p = par;
1736 p->validate(features);
1742 int InsetText::beginningOfMainBody(Paragraph * p) const
1744 if (p->layout()->labeltype != LABEL_MANUAL)
1747 return p->beginningOfMainBody();
1751 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1753 if (the_locking_inset) {
1754 the_locking_inset->getCursorPos(bv, x, y);
1757 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1758 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1762 int InsetText::insetInInsetY() const
1764 if (!the_locking_inset)
1767 return (inset_y + the_locking_inset->insetInInsetY());
1771 void InsetText::toggleInsetCursor(BufferView * bv)
1773 if (the_locking_inset) {
1774 the_locking_inset->toggleInsetCursor(bv);
1778 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1780 int const asc = font_metrics::maxAscent(font);
1781 int const desc = font_metrics::maxDescent(font);
1783 if (isCursorVisible())
1784 bv->hideLockedInsetCursor();
1786 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1787 toggleCursorVisible();
1791 void InsetText::showInsetCursor(BufferView * bv, bool show)
1793 if (the_locking_inset) {
1794 the_locking_inset->showInsetCursor(bv, show);
1797 if (!isCursorVisible()) {
1798 LyXFont const font =
1799 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1801 int const asc = font_metrics::maxAscent(font);
1802 int const desc = font_metrics::maxDescent(font);
1804 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1806 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1807 setCursorVisible(true);
1812 void InsetText::hideInsetCursor(BufferView * bv)
1814 if (isCursorVisible()) {
1815 bv->hideLockedInsetCursor();
1816 setCursorVisible(false);
1818 if (the_locking_inset)
1819 the_locking_inset->hideInsetCursor(bv);
1823 void InsetText::fitInsetCursor(BufferView * bv) const
1825 if (the_locking_inset) {
1826 the_locking_inset->fitInsetCursor(bv);
1829 LyXFont const font =
1830 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1832 int const asc = font_metrics::maxAscent(font);
1833 int const desc = font_metrics::maxDescent(font);
1835 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1836 need_update |= FULL;
1840 UpdatableInset::RESULT
1841 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1843 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1844 return moveLeftIntern(bv, false, activate_inset, selecting);
1846 return moveRightIntern(bv, true, activate_inset, selecting);
1850 UpdatableInset::RESULT
1851 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1853 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1854 return moveRightIntern(bv, true, activate_inset, selecting);
1856 return moveLeftIntern(bv, false, activate_inset, selecting);
1860 UpdatableInset::RESULT
1861 InsetText::moveRightIntern(BufferView * bv, bool front,
1862 bool activate_inset, bool selecting)
1864 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1865 return FINISHED_RIGHT;
1866 if (activate_inset && checkAndActivateInset(bv, front))
1868 getLyXText(bv)->cursorRight(bv);
1870 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1871 return DISPATCHED_NOUPDATE;
1875 UpdatableInset::RESULT
1876 InsetText::moveLeftIntern(BufferView * bv, bool front,
1877 bool activate_inset, bool selecting)
1879 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1881 getLyXText(bv)->cursorLeft(bv);
1883 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1884 if (activate_inset && checkAndActivateInset(bv, front))
1886 return DISPATCHED_NOUPDATE;
1890 UpdatableInset::RESULT
1891 InsetText::moveUp(BufferView * bv)
1893 if (!crow(bv)->previous())
1895 getLyXText(bv)->cursorUp(bv);
1896 return DISPATCHED_NOUPDATE;
1900 UpdatableInset::RESULT
1901 InsetText::moveDown(BufferView * bv)
1903 if (!crow(bv)->next())
1904 return FINISHED_DOWN;
1905 getLyXText(bv)->cursorDown(bv);
1906 return DISPATCHED_NOUPDATE;
1910 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1912 if (the_locking_inset) {
1913 if (the_locking_inset->insetAllowed(inset))
1914 return the_locking_inset->insertInset(bv, inset);
1917 inset->setOwner(this);
1918 hideInsetCursor(bv);
1919 getLyXText(bv)->insertInset(bv, inset);
1921 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1926 bool InsetText::insetAllowed(Inset::Code code) const
1928 // in_insetAllowed is a really gross hack,
1929 // to allow us to call the owner's insetAllowed
1930 // without stack overflow, which can happen
1931 // when the owner uses InsetCollapsable::insetAllowed()
1933 if (in_insetAllowed)
1935 in_insetAllowed = true;
1936 if (the_locking_inset)
1937 ret = the_locking_inset->insetAllowed(code);
1939 ret = owner()->insetAllowed(code);
1940 in_insetAllowed = false;
1945 UpdatableInset * InsetText::getLockingInset() const
1947 return the_locking_inset ? the_locking_inset->getLockingInset() :
1948 const_cast<InsetText *>(this);
1952 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1956 if (the_locking_inset)
1957 return the_locking_inset->getFirstLockingInsetOfType(c);
1962 bool InsetText::showInsetDialog(BufferView * bv) const
1964 if (the_locking_inset)
1965 return the_locking_inset->showInsetDialog(bv);
1970 vector<string> const InsetText::getLabelList() const
1972 vector<string> label_list;
1974 Paragraph * tpar = par;
1976 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1977 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1978 for (; beg != end; ++beg) {
1979 vector<string> const l = (*beg)->getLabelList();
1980 label_list.insert(label_list.end(), l.begin(), l.end());
1982 tpar = tpar->next();
1988 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1991 if (the_locking_inset) {
1992 the_locking_inset->setFont(bv, font, toggleall, selectall);
1995 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1996 getLyXText(bv)->setFont(bv, font, toggleall);
2001 lt = getLyXText(bv);
2004 if (lt->selection.set()) {
2005 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2009 lt->toggleFree(bv, font, toggleall);
2011 lt->clearSelection();
2013 bool flag = (selectall || lt->selection.set());
2017 updateLocal(bv, FULL, true);
2019 updateLocal(bv, CURSOR_PAR, true);
2023 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2025 if (cpar(bv)->isInset(cpos(bv))) {
2027 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2028 if (!isHighlyEditableInset(inset))
2030 inset->edit(bv, front);
2031 if (!the_locking_inset)
2033 updateLocal(bv, CURSOR, false);
2040 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2041 mouse_button::state button)
2043 x -= drawTextXOffset;
2045 int dummyy = y + insetAscent;
2046 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2047 // we only do the edit() call if the inset was hit by the mouse
2048 // or if it is a highly editable inset. So we should call this
2049 // function from our own edit with button < 0.
2050 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2052 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2060 inset_x = cix(bv) - top_x + drawTextXOffset;
2061 inset_y = ciy(bv) + drawTextYOffset;
2062 inset->edit(bv, x - inset_x, y - inset_y, button);
2063 if (!the_locking_inset)
2065 updateLocal(bv, CURSOR, false);
2072 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2075 int w = UpdatableInset::getMaxWidth(bv, inset);
2080 w = w - top_x + owner()->x();
2083 w -= (2 * TEXT_TO_INSET_OFFSET);
2086 return UpdatableInset::getMaxWidth(bv, inset);
2091 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2093 // we have to unlock any locked inset otherwise we're in troubles
2094 the_locking_inset = 0;
2096 Paragraph * tmp = par->next();
2101 par = new Paragraph(*p, same_id);
2102 par->setInsetOwner(this);
2103 Paragraph * np = par;
2106 np->next(new Paragraph(*p, same_id));
2107 np->next()->previous(np);
2109 np->setInsetOwner(this);
2116 void InsetText::setText(string const & data, LyXFont const & font)
2119 for (unsigned int i=0; i < data.length(); ++i)
2120 par->insertChar(i, data[i], font);
2125 void InsetText::setAutoBreakRows(bool flag)
2127 if (flag != autoBreakRows) {
2128 autoBreakRows = flag;
2136 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2138 if (how != drawFrame_) {
2141 updateLocal(bv, DRAW_FRAME, false);
2146 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2148 if (frame_color != col) {
2151 updateLocal(bv, DRAW_FRAME, false);
2156 int InsetText::cx(BufferView * bv) const
2158 // we do nothing dangerous so we use a local cache
2159 LyXText * llt = getLyXText(bv);
2160 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2161 if (the_locking_inset) {
2162 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2164 if (font.isVisibleRightToLeft())
2165 x -= the_locking_inset->width(bv, font);
2171 int InsetText::cix(BufferView * bv) const
2173 // we do nothing dangerous so we use a local cache
2174 LyXText * llt = getLyXText(bv);
2175 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2176 if (the_locking_inset) {
2177 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2179 if (font.isVisibleRightToLeft())
2180 x -= the_locking_inset->width(bv, font);
2186 int InsetText::cy(BufferView * bv) const
2189 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2193 int InsetText::ciy(BufferView * bv) const
2196 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2200 pos_type InsetText::cpos(BufferView * bv) const
2202 return getLyXText(bv)->cursor.pos();
2206 Paragraph * InsetText::cpar(BufferView * bv) const
2208 return getLyXText(bv)->cursor.par();
2212 bool InsetText::cboundary(BufferView * bv) const
2214 return getLyXText(bv)->cursor.boundary();
2218 Row * InsetText::crow(BufferView * bv) const
2220 return getLyXText(bv)->cursor.row();
2224 LyXText * InsetText::getLyXText(BufferView const * lbv,
2225 bool const recursive) const
2227 if (cached_bview == lbv) {
2228 if (recursive && the_locking_inset)
2229 return the_locking_inset->getLyXText(lbv, true);
2230 LyXText * lt = cached_text.get();
2231 lyx::Assert(lt && lt->firstRow()->par() == par);
2234 // Super UGLY! (Lgb)
2235 BufferView * bv = const_cast<BufferView *>(lbv);
2238 Cache::iterator it = cache.find(bv);
2240 if (it != cache.end()) {
2243 } else if (do_resize) {
2244 resizeLyXText(do_resize);
2246 if (lt || !it->second.remove) {
2247 lyx::Assert(it->second.text.get());
2248 cached_text = it->second.text;
2249 if (recursive && the_locking_inset) {
2250 return the_locking_inset->getLyXText(bv, true);
2252 return cached_text.get();
2253 } else if (it->second.remove) {
2255 saveLyXTextState(it->second.text.get());
2261 // when we have to reinit the existing LyXText!
2263 it->second.text->init(bv);
2264 restoreLyXTextState(bv, it->second.text.get());
2265 it->second.remove = false;
2267 cached_text = it->second.text;
2268 if (the_locking_inset && recursive) {
2269 return the_locking_inset->getLyXText(bv);
2271 return cached_text.get();
2274 // we are here only if we don't have a BufferView * in the cache!!!
2276 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2277 cached_text->init(bv);
2278 restoreLyXTextState(bv, cached_text.get());
2280 cache.insert(make_pair(bv, cached_text));
2282 if (the_locking_inset && recursive) {
2283 return the_locking_inset->getLyXText(bv);
2285 return cached_text.get();
2289 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2293 Cache::iterator it = cache.find(bv);
2295 if (it == cache.end()) {
2299 lyx::Assert(it->second.text.get());
2301 it->second.remove = true;
2303 /// then remove all LyXText in text-insets
2304 Paragraph * p = par;
2305 for (; p; p = p->next()) {
2306 p->deleteInsetsLyXText(bv);
2312 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2315 // we cannot resize this because we are in use!
2316 // so do this on the next possible getLyXText()
2321 // lyxerr << "InsetText::resizeLyXText\n";
2322 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2323 // we have to do this as a fixed width may have changed!
2324 LyXText * t = getLyXText(bv);
2325 saveLyXTextState(t);
2327 restoreLyXTextState(bv, t);
2330 // one endless line, resize normally not necessary
2331 if (!force && getMaxWidth(bv, this) < 0)
2334 Cache::iterator it = cache.find(bv);
2335 if (it == cache.end()) {
2338 lyx::Assert(it->second.text.get());
2340 LyXText * t = it->second.text.get();
2341 saveLyXTextState(t);
2342 for (Paragraph * p = par; p; p = p->next()) {
2343 p->resizeInsetsLyXText(bv);
2346 restoreLyXTextState(bv, t);
2347 if (the_locking_inset) {
2348 inset_x = cix(bv) - top_x + drawTextXOffset;
2349 inset_y = ciy(bv) + drawTextYOffset;
2352 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2354 updateLocal(bv, FULL, false);
2355 // this will scroll the screen such that the cursor becomes visible
2356 bv->updateScrollbar();
2358 need_update |= FULL;
2363 void InsetText::reinitLyXText() const
2366 // we cannot resize this because we are in use!
2367 // so do this on the next possible getLyXText()
2373 // lyxerr << "InsetText::reinitLyXText\n";
2374 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2375 lyx::Assert(it->second.text.get());
2377 LyXText * t = it->second.text.get();
2378 BufferView * bv = it->first;
2380 saveLyXTextState(t);
2381 for (Paragraph * p = par; p; p = p->next()) {
2382 p->resizeInsetsLyXText(bv);
2385 restoreLyXTextState(bv, t);
2386 if (the_locking_inset) {
2387 inset_x = cix(bv) - top_x + drawTextXOffset;
2388 inset_y = ciy(bv) + drawTextYOffset;
2390 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2392 updateLocal(bv, FULL, false);
2393 // this will scroll the screen such that the cursor becomes visible
2394 bv->updateScrollbar();
2402 void InsetText::removeNewlines()
2404 bool changed = false;
2406 for (Paragraph * p = par; p; p = p->next()) {
2407 for (int i = 0; i < p->size(); ++i) {
2408 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2419 bool InsetText::nodraw() const
2421 if (the_locking_inset)
2422 return the_locking_inset->nodraw();
2423 return UpdatableInset::nodraw();
2427 int InsetText::scroll(bool recursive) const
2429 int sx = UpdatableInset::scroll(false);
2431 if (recursive && the_locking_inset)
2432 sx += the_locking_inset->scroll(recursive);
2438 bool InsetText::doClearArea() const
2440 return !locked || (need_update & (FULL|INIT));
2444 void InsetText::selectAll(BufferView * bv)
2446 getLyXText(bv)->cursorTop(bv);
2447 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2448 getLyXText(bv)->cursorBottom(bv);
2449 getLyXText(bv)->setSelection(bv);
2453 void InsetText::clearSelection(BufferView * bv)
2455 getLyXText(bv)->clearSelection();
2459 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2461 Painter & pain = bv->painter();
2463 int h = insetAscent + insetDescent;
2464 int ty = baseline - insetAscent;
2470 if ((ty + h) > pain.paperHeight())
2471 h = pain.paperHeight();
2472 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2473 w = pain.paperWidth();
2474 // w -= TEXT_TO_INSET_OFFSET;
2475 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2478 frame_is_visible = false;
2482 Paragraph * InsetText::getParFromID(int id) const
2485 Paragraph * result = par;
2486 Paragraph * ires = 0;
2487 while (result && result->id() != id) {
2488 if ((ires = result->getParFromID(id)))
2490 result = result->next();
2494 Paragraph * tmp = par;
2496 if (tmp->id() == id) {
2499 Paragraph * tmp2 = tmp->getParFromID(id);
2510 Paragraph * InsetText::firstParagraph() const
2513 if (the_locking_inset)
2514 if ((result = the_locking_inset->firstParagraph()))
2520 Paragraph * InsetText::getFirstParagraph(int i) const
2522 return (i == 0) ? par : 0;
2526 LyXCursor const & InsetText::cursor(BufferView * bv) const
2528 if (the_locking_inset)
2529 return the_locking_inset->cursor(bv);
2530 return getLyXText(bv)->cursor;
2534 Paragraph * InsetText::paragraph() const
2540 void InsetText::paragraph(Paragraph * p)
2542 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2543 // caller of this function has to take care of it. This IS important
2544 // as we could have to insert a paragraph before this one and just
2545 // link the actual to a new ones next and set it with this function
2548 // set ourself as owner for all the paragraphs inserted!
2549 Paragraph * np = par;
2551 np->setInsetOwner(this);
2555 // redraw myself when asked for
2560 Inset * InsetText::getInsetFromID(int id_arg) const
2563 return const_cast<InsetText *>(this);
2565 Paragraph * lp = par;
2568 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2569 en = lp->inset_iterator_end();
2572 if ((*it)->id() == id_arg)
2574 Inset * in = (*it)->getInsetFromID(id_arg);
2584 WordLangTuple InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2590 lt = getLyXText(bv);
2593 if (the_locking_inset) {
2594 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2595 if (!word.word().empty()) {
2601 // we have to go on checking so move cursor to the next char
2602 lt->cursor.pos(lt->cursor.pos() + 1);
2604 word = lt->selectNextWordToSpellcheck(bv, value);
2605 if (word.word().empty())
2606 bv->unlockInset(const_cast<InsetText *>(this));
2615 void InsetText::selectSelectedWord(BufferView * bv)
2617 if (the_locking_inset) {
2618 the_locking_inset->selectSelectedWord(bv);
2621 getLyXText(bv)->selectSelectedWord(bv);
2622 updateLocal(bv, SELECTION, false);
2626 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2628 if (the_locking_inset) {
2629 the_locking_inset->toggleSelection(bv, kill_selection);
2633 lt = getLyXText(bv);
2637 int x = top_x + TEXT_TO_INSET_OFFSET;
2639 Row * row = lt->firstRow();
2640 int y_offset = top_baseline - row->ascent_of_text();
2642 while ((row != 0) && ((y+row->height()) <= 0)) {
2649 if (need_update & SELECTION)
2651 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2657 bool InsetText::searchForward(BufferView * bv, string const & str,
2662 lt = getLyXText(bv);
2665 if (the_locking_inset) {
2666 if (the_locking_inset->searchForward(bv, str, cs, mw))
2668 lt->cursorRight(bv, true);
2670 lyxfind::SearchResult result =
2671 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2673 if (result == lyxfind::SR_FOUND) {
2674 LyXCursor cur = lt->cursor;
2675 bv->unlockInset(bv->theLockingInset());
2676 if (bv->lockInset(this))
2679 lt->setSelectionOverString(bv, str);
2680 updateLocal(bv, SELECTION, false);
2684 return (result != lyxfind::SR_NOT_FOUND);
2687 bool InsetText::searchBackward(BufferView * bv, string const & str,
2690 if (the_locking_inset) {
2691 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2696 lt = getLyXText(bv);
2700 Paragraph * p = par;
2703 lt->setCursor(bv, p, p->size());
2705 lyxfind::SearchResult result =
2706 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2708 if (result == lyxfind::SR_FOUND) {
2709 LyXCursor cur = lt->cursor;
2710 bv->unlockInset(bv->theLockingInset());
2711 if (bv->lockInset(this))
2714 lt->setSelectionOverString(bv, str);
2715 updateLocal(bv, SELECTION, false);
2719 return (result != lyxfind::SR_NOT_FOUND);
2723 bool InsetText::checkInsertChar(LyXFont & font)
2726 return owner()->checkInsertChar(font);
2731 void InsetText::collapseParagraphs(BufferView * bv) const
2733 BufferParams const & bparams = bv->buffer()->params;
2734 LyXText * llt = getLyXText(bv);
2736 while(par->next()) {
2737 if (par->size() && par->next()->size() &&
2738 !par->isSeparator(par->size()-1))
2740 par->insertChar(par->size(), ' ');
2742 if (llt->selection.set()) {
2743 if (llt->selection.start.par() == par->next()) {
2744 llt->selection.start.par(par);
2745 llt->selection.start.pos(
2746 llt->selection.start.pos() + par->size());
2748 if (llt->selection.end.par() == par->next()) {
2749 llt->selection.end.par(par);
2750 llt->selection.end.pos(
2751 llt->selection.end.pos() + par->size());
2754 par->pasteParagraph(bparams);
2760 void InsetText::getDrawFont(LyXFont & font) const
2764 owner()->getDrawFont(font);
2768 void InsetText::appendParagraphs(BufferParams const & bparams,
2772 Paragraph * tmpbuf = newpar;
2773 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2775 while (tmpbuf->next()) {
2776 tmpbuf = tmpbuf->next();
2777 lastbuffer->next(new Paragraph(*tmpbuf, false));
2778 lastbuffer->next()->previous(lastbuffer);
2779 lastbuffer = lastbuffer->next();
2782 while (lastbuffer->next())
2783 lastbuffer = lastbuffer->next();
2784 if (newpar->size() && lastbuffer->size() &&
2785 !lastbuffer->isSeparator(lastbuffer->size()-1))
2787 lastbuffer->insertChar(lastbuffer->size(), ' ');
2790 // make the buf exactly the same layout than our last paragraph
2791 buf->makeSameLayout(lastbuffer);
2794 lastbuffer->next(buf);
2795 buf->previous(lastbuffer);
2796 lastbuffer->pasteParagraph(bparams);
2802 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2804 Paragraph * par = getFirstParagraph(0);
2806 Paragraph::inset_iterator it = par->inset_iterator_begin();
2807 Paragraph::inset_iterator end = par->inset_iterator_end();
2808 for (; it != end; ++it) {
2809 it->addPreview(loader);