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;
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 // PressFuncRequest 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, FuncRequest(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, FuncRequest(LFUN_PASTE));
1101 localDispatch(bv, FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1104 getLyXText(bv)->clearSelection();
1106 showInsetCursor(bv);
1110 bool InsetText::insetButtonRelease(BufferView * bv,
1111 int x, int y, mouse_button::state button)
1113 no_selection = true;
1114 if (the_locking_inset) {
1115 return the_locking_inset->insetButtonRelease(bv,
1116 x - inset_x, y - inset_y,
1119 int tmp_x = x - drawTextXOffset;
1120 int tmp_y = y + insetAscent - getLyXText(bv)->first_y;
1121 Inset * inset = bv->checkInsetHit(getLyXText(bv), tmp_x, tmp_y);
1124 if (isHighlyEditableInset(inset)) {
1125 ret = inset->insetButtonRelease(bv, x - inset_x,
1126 y - inset_y, button);
1128 inset_x = cix(bv) - top_x + drawTextXOffset;
1129 inset_y = ciy(bv) + drawTextYOffset;
1130 ret = inset->insetButtonRelease(bv, x - inset_x,
1131 y - inset_y, button);
1132 inset->edit(bv, x - inset_x,
1133 y - inset_y, button);
1135 updateLocal(bv, CURSOR_PAR, false);
1141 void InsetText::insetMotionNotify(BufferView * bv, int x, int y, mouse_button::state state)
1143 if (the_locking_inset) {
1144 the_locking_inset->insetMotionNotify(bv, x - inset_x,
1149 if (no_selection || ((mouse_x == x) && (mouse_y == y)))
1154 lt = getLyXText(bv);
1157 hideInsetCursor(bv);
1158 LyXCursor cur = lt->cursor;
1159 lt->setCursorFromCoordinates(bv, x - drawTextXOffset, y + insetAscent);
1160 lt->cursor.x_fix(lt->cursor.x());
1161 if (cur == lt->cursor) {
1166 lt->setSelection(bv);
1167 bool flag = (lt->toggle_cursor.par() != lt->toggle_end_cursor.par() ||
1168 lt->toggle_cursor.pos() != lt->toggle_end_cursor.pos());
1172 updateLocal(bv, SELECTION, false);
1174 showInsetCursor(bv);
1178 UpdatableInset::RESULT
1179 InsetText::localDispatch(BufferView * bv, FuncRequest const & ev)
1181 bool was_empty = par->size() == 0 && !par->next();
1182 no_selection = false;
1183 UpdatableInset::RESULT
1184 result= UpdatableInset::localDispatch(bv, ev);
1185 if (result != UNDISPATCHED)
1188 result = DISPATCHED;
1189 if (ev.action < 0 && ev.argument.empty())
1192 if (the_locking_inset) {
1193 result = the_locking_inset->localDispatch(bv, ev);
1194 if (result == DISPATCHED_NOUPDATE)
1196 else if (result == DISPATCHED) {
1197 updateLocal(bv, CURSOR_PAR, false);
1199 } else if (result >= FINISHED) {
1201 case FINISHED_RIGHT:
1202 moveRightIntern(bv, false, false);
1203 result = DISPATCHED;
1206 if ((result = moveUp(bv)) >= FINISHED) {
1207 updateLocal(bv, CURSOR, false);
1208 bv->unlockInset(this);
1213 LyXText *lt = getLyXText(bv);
1214 if (lt->cursor.irow()->next()) {
1215 lt->setCursorFromCoordinates(
1216 bv, lt->cursor.ix() + inset_x,
1218 lt->cursor.irow()->baseline() +
1219 lt->cursor.irow()->height() + 1);
1220 lt->cursor.x_fix(lt->cursor.x());
1221 updateLocal(bv, CURSOR, false);
1223 bv->unlockInset(this);
1228 result = DISPATCHED;
1231 the_locking_inset = 0;
1232 updateLocal(bv, CURSOR, false);
1236 hideInsetCursor(bv);
1239 lt = getLyXText(bv);
1243 int updflag = false;
1244 switch (ev.action) {
1246 case LFUN_SELFINSERT:
1247 if (bv->buffer()->isReadonly()) {
1248 // setErrorMessage(N_("Document is read only"));
1251 if (!ev.argument.empty()) {
1252 /* Automatically delete the currently selected
1253 * text and replace it with what is being
1254 * typed in now. Depends on lyxrc settings
1255 * "auto_region_delete", which defaults to
1258 setUndo(bv, Undo::INSERT,
1259 lt->cursor.par(), lt->cursor.par()->next());
1261 if (lyxrc.auto_region_delete) {
1262 if (lt->selection.set()) {
1263 lt->cutSelection(bv, false, false);
1266 lt->clearSelection();
1267 for (string::size_type i = 0; i < ev.argument.length(); ++i) {
1268 bv->owner()->getIntl()->getTransManager().
1269 TranslateAndInsert(ev.argument[i], lt);
1272 lt->selection.cursor = lt->cursor;
1273 updwhat = CURSOR | CURSOR_PAR;
1275 result = DISPATCHED_NOUPDATE;
1277 // --- Cursor Movements -----------------------------------
1280 moveRight(bv, false, true);
1281 lt->setSelection(bv);
1282 updwhat = SELECTION;
1285 result = moveRight(bv);
1291 moveLeft(bv, false, true);
1292 lt->setSelection(bv);
1293 updwhat = SELECTION;
1297 result = moveLeft(bv);
1303 lt->setSelection(bv);
1304 updwhat = SELECTION;
1308 result = moveDown(bv);
1314 lt->setSelection(bv);
1315 updwhat = SELECTION;
1319 result = moveUp(bv);
1331 case LFUN_BACKSPACE: {
1332 setUndo(bv, Undo::DELETE,
1333 lt->cursor.par(), lt->cursor.par()->next());
1334 if (lt->selection.set())
1335 lt->cutSelection(bv, true, false);
1338 updwhat = CURSOR_PAR;
1344 setUndo(bv, Undo::DELETE,
1345 lt->cursor.par(), lt->cursor.par()->next());
1346 if (lt->selection.set()) {
1347 lt->cutSelection(bv, true, false);
1351 updwhat = CURSOR_PAR;
1357 setUndo(bv, Undo::DELETE,
1358 lt->cursor.par(), lt->cursor.par()->next());
1359 lt->cutSelection(bv);
1360 updwhat = CURSOR_PAR;
1367 lt->copySelection(bv);
1368 updwhat = CURSOR_PAR;
1370 case LFUN_PASTESELECTION:
1372 string const clip(bv->getClipboard());
1376 if (ev.argument == "paragraph") {
1377 lt->insertStringAsParagraphs(bv, clip);
1379 lt->insertStringAsLines(bv, clip);
1382 lt->clearSelection();
1384 updwhat = CURSOR_PAR;
1389 if (!autoBreakRows) {
1391 if (CutAndPaste::nrOfParagraphs() > 1) {
1392 Alert::alert(_("Impossible operation"),
1393 _("Cannot include more than one paragraph!"),
1398 setUndo(bv, Undo::INSERT,
1399 lt->cursor.par(), lt->cursor.par()->next());
1400 lt->pasteSelection(bv);
1402 lt->clearSelection();
1403 updwhat = CURSOR_PAR;
1408 case LFUN_BREAKPARAGRAPH:
1409 if (!autoBreakRows) {
1410 result = DISPATCHED;
1413 lt->breakParagraph(bv, 0);
1414 updwhat = CURSOR | FULL;
1417 case LFUN_BREAKPARAGRAPHKEEPLAYOUT:
1418 if (!autoBreakRows) {
1419 result = DISPATCHED;
1422 lt->breakParagraph(bv, 1);
1423 updwhat = CURSOR | FULL;
1427 case LFUN_BREAKLINE: {
1428 if (!autoBreakRows) {
1429 result = DISPATCHED;
1432 setUndo(bv, Undo::INSERT,
1433 lt->cursor.par(), lt->cursor.par()->next());
1434 lt->insertChar(bv, Paragraph::META_NEWLINE);
1435 updwhat = CURSOR | CURSOR_PAR;
1441 // do not set layouts on non breakable textinsets
1442 if (autoBreakRows) {
1443 string cur_layout = cpar(bv)->layout()->name();
1445 // Derive layout number from given argument (string)
1446 // and current buffer's textclass (number). */
1447 LyXTextClass const & tclass =
1448 bv->buffer()->params.getLyXTextClass();
1449 string layout = ev.argument;
1450 bool hasLayout = tclass.hasLayout(layout);
1452 // If the entry is obsolete, use the new one instead.
1454 string const & obs =
1455 tclass[layout]->obsoleted_by();
1460 // see if we found the layout number:
1462 FuncRequest lf(LFUN_MESSAGE, N_("Layout ") + ev.argument + N_(" not known"));
1463 bv->owner()->getLyXFunc()->dispatch(lf);
1467 if (cur_layout != layout) {
1468 cur_layout = layout;
1469 lt->setLayout(bv, layout);
1470 bv->owner()->setLayout(cpar(bv)->layout()->name());
1471 updwhat = CURSOR_PAR;
1475 // reset the layout box
1476 bv->owner()->setLayout(cpar(bv)->layout()->name());
1479 case LFUN_PARAGRAPH_SPACING:
1480 // This one is absolutely not working. When fiddling with this
1481 // it also seems to me that the paragraphs inside the insettext
1482 // inherit bufferparams/paragraphparams in a strange way. (Lgb)
1484 Paragraph * par = lt->cursor.par();
1485 Spacing::Space cur_spacing = par->params().spacing().getSpace();
1486 float cur_value = 1.0;
1487 if (cur_spacing == Spacing::Other) {
1488 cur_value = par->params().spacing().getValue();
1491 istringstream istr(ev.argument.c_str());
1494 Spacing::Space new_spacing = cur_spacing;
1495 float new_value = cur_value;
1497 lyxerr << "Missing argument to `paragraph-spacing'"
1499 } else if (tmp == "single") {
1500 new_spacing = Spacing::Single;
1501 } else if (tmp == "onehalf") {
1502 new_spacing = Spacing::Onehalf;
1503 } else if (tmp == "double") {
1504 new_spacing = Spacing::Double;
1505 } else if (tmp == "other") {
1506 new_spacing = Spacing::Other;
1509 lyxerr << "new_value = " << tmpval << endl;
1512 } else if (tmp == "default") {
1513 new_spacing = Spacing::Default;
1515 lyxerr << _("Unknown spacing argument: ")
1516 << ev.argument << endl;
1518 if (cur_spacing != new_spacing || cur_value != new_value) {
1519 par->params().spacing(Spacing(new_spacing, new_value));
1520 updwhat = CURSOR_PAR;
1527 if (!bv->dispatch(ev))
1528 result = UNDISPATCHED;
1535 updateLocal(bv, updwhat, updflag);
1536 /// If the action has deleted all text in the inset, we need to change the
1537 // language to the language of the surronding text.
1538 if (!was_empty && par->size() == 0 && !par->next()) {
1539 LyXFont font(LyXFont::ALL_IGNORE);
1540 font.setLanguage(bv->getParentLanguage(this));
1541 setFont(bv, font, false);
1544 if (result >= FINISHED)
1545 bv->unlockInset(this);
1547 if (result == DISPATCHED_NOUPDATE && (need_update & FULL))
1548 result = DISPATCHED;
1553 int InsetText::latex(Buffer const * buf, ostream & os, bool moving_arg, bool) const
1556 buf->latexParagraphs(os, par, 0, texrow, moving_arg);
1557 return texrow.rows();
1561 int InsetText::ascii(Buffer const * buf, ostream & os, int linelen) const
1563 Paragraph * p = par;
1564 unsigned int lines = 0;
1567 string const tmp = buf->asciiParagraph(p, linelen, p->previous()==0);
1568 lines += lyx::count(tmp.begin(), tmp.end(), '\n');
1576 int InsetText::docbook(Buffer const * buf, ostream & os, bool mixcont) const
1578 Paragraph * p = par;
1579 unsigned int lines = 0;
1581 vector<string> environment_stack(10);
1582 vector<string> environment_inner(10);
1584 int const command_depth = 0;
1587 Paragraph::depth_type depth = 0; // paragraph depth
1591 int desc_on = 0; // description mode
1593 LyXLayout_ptr const & style = p->layout();
1595 // environment tag closing
1596 for (; depth > p->params().depth(); --depth) {
1597 if (environment_inner[depth] != "!-- --") {
1598 item_name = "listitem";
1599 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1600 if (environment_inner[depth] == "varlistentry")
1601 lines += buf->sgmlCloseTag(os, depth+command_depth, mixcont, environment_inner[depth]);
1603 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1604 environment_stack[depth].erase();
1605 environment_inner[depth].erase();
1608 if (depth == p->params().depth()
1609 && environment_stack[depth] != style->latexname()
1610 && !environment_stack[depth].empty()) {
1611 if (environment_inner[depth] != "!-- --") {
1612 item_name= "listitem";
1613 lines += buf->sgmlCloseTag(os, command_depth+depth, mixcont, item_name);
1614 if (environment_inner[depth] == "varlistentry")
1615 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1618 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1620 environment_stack[depth].erase();
1621 environment_inner[depth].erase();
1624 // Write opening SGML tags.
1625 switch (style->latextype) {
1626 case LATEX_PARAGRAPH:
1627 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1631 buf->sgmlError(p, 0, _("Error : LatexType Command not allowed here.\n"));
1635 case LATEX_ENVIRONMENT:
1636 case LATEX_ITEM_ENVIRONMENT:
1637 if (depth < p->params().depth()) {
1638 depth = p->params().depth();
1639 environment_stack[depth].erase();
1642 if (environment_stack[depth] != style->latexname()) {
1643 if (environment_stack.size() == depth + 1) {
1644 environment_stack.push_back("!-- --");
1645 environment_inner.push_back("!-- --");
1647 environment_stack[depth] = style->latexname();
1648 environment_inner[depth] = "!-- --";
1649 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1651 if (environment_inner[depth] != "!-- --") {
1652 item_name= "listitem";
1653 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1654 if (environment_inner[depth] == "varlistentry")
1655 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1659 if (style->latextype == LATEX_ENVIRONMENT) {
1660 if (!style->latexparam().empty()) {
1661 if (style->latexparam() == "CDATA")
1664 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexparam());
1669 desc_on = (style->labeltype == LABEL_MANUAL);
1671 environment_inner[depth] = desc_on?"varlistentry":"listitem";
1672 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, environment_inner[depth]);
1674 item_name = desc_on?"term":"para";
1675 lines += buf->sgmlOpenTag(os, depth + 1 + command_depth, mixcont, item_name);
1679 lines += buf->sgmlOpenTag(os, depth + command_depth, mixcont, style->latexname());
1683 buf->simpleDocBookOnePar(os, p, desc_on, depth + 1 + command_depth);
1687 // write closing SGML tags
1688 switch (style->latextype) {
1689 case LATEX_ENVIRONMENT:
1690 if (!style->latexparam().empty()) {
1691 if (style->latexparam() == "CDATA")
1694 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexparam());
1697 case LATEX_ITEM_ENVIRONMENT:
1698 if (desc_on == 1) break;
1700 lines += buf->sgmlCloseTag(os, depth + 1 + command_depth, mixcont, end_tag);
1702 case LATEX_PARAGRAPH:
1703 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1706 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, style->latexname());
1712 for (int d = depth; d >= 0; --d) {
1713 if (!environment_stack[depth].empty()) {
1714 if (environment_inner[depth] != "!-- --") {
1715 item_name = "listitem";
1716 lines += buf->sgmlCloseTag(os, command_depth + depth, mixcont, item_name);
1717 if (environment_inner[depth] == "varlistentry")
1718 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_inner[depth]);
1721 lines += buf->sgmlCloseTag(os, depth + command_depth, mixcont, environment_stack[depth]);
1729 void InsetText::validate(LaTeXFeatures & features) const
1731 Paragraph * p = par;
1733 p->validate(features);
1739 int InsetText::beginningOfMainBody(Paragraph * p) const
1741 if (p->layout()->labeltype != LABEL_MANUAL)
1744 return p->beginningOfMainBody();
1748 void InsetText::getCursorPos(BufferView * bv, int & x, int & y) const
1750 if (the_locking_inset) {
1751 the_locking_inset->getCursorPos(bv, x, y);
1754 x = cx(bv) - top_x - TEXT_TO_INSET_OFFSET;
1755 y = cy(bv) - TEXT_TO_INSET_OFFSET;
1759 int InsetText::insetInInsetY() const
1761 if (!the_locking_inset)
1764 return (inset_y + the_locking_inset->insetInInsetY());
1768 void InsetText::toggleInsetCursor(BufferView * bv)
1770 if (the_locking_inset) {
1771 the_locking_inset->toggleInsetCursor(bv);
1775 LyXFont const font(getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv)));
1777 int const asc = font_metrics::maxAscent(font);
1778 int const desc = font_metrics::maxDescent(font);
1780 if (isCursorVisible())
1781 bv->hideLockedInsetCursor();
1783 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1784 toggleCursorVisible();
1788 void InsetText::showInsetCursor(BufferView * bv, bool show)
1790 if (the_locking_inset) {
1791 the_locking_inset->showInsetCursor(bv, show);
1794 if (!isCursorVisible()) {
1795 LyXFont const font =
1796 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1798 int const asc = font_metrics::maxAscent(font);
1799 int const desc = font_metrics::maxDescent(font);
1801 bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1803 bv->showLockedInsetCursor(cx(bv), cy(bv), asc, desc);
1804 setCursorVisible(true);
1809 void InsetText::hideInsetCursor(BufferView * bv)
1811 if (isCursorVisible()) {
1812 bv->hideLockedInsetCursor();
1813 setCursorVisible(false);
1815 if (the_locking_inset)
1816 the_locking_inset->hideInsetCursor(bv);
1820 void InsetText::fitInsetCursor(BufferView * bv) const
1822 if (the_locking_inset) {
1823 the_locking_inset->fitInsetCursor(bv);
1826 LyXFont const font =
1827 getLyXText(bv)->getFont(bv->buffer(), cpar(bv), cpos(bv));
1829 int const asc = font_metrics::maxAscent(font);
1830 int const desc = font_metrics::maxDescent(font);
1832 if (bv->fitLockedInsetCursor(cx(bv), cy(bv), asc, desc))
1833 need_update |= FULL;
1837 UpdatableInset::RESULT
1838 InsetText::moveRight(BufferView * bv, bool activate_inset, bool selecting)
1840 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1841 return moveLeftIntern(bv, false, activate_inset, selecting);
1843 return moveRightIntern(bv, true, activate_inset, selecting);
1847 UpdatableInset::RESULT
1848 InsetText::moveLeft(BufferView * bv, bool activate_inset, bool selecting)
1850 if (getLyXText(bv)->cursor.par()->isRightToLeftPar(bv->buffer()->params))
1851 return moveRightIntern(bv, true, activate_inset, selecting);
1853 return moveLeftIntern(bv, false, activate_inset, selecting);
1857 UpdatableInset::RESULT
1858 InsetText::moveRightIntern(BufferView * bv, bool front,
1859 bool activate_inset, bool selecting)
1861 if (!cpar(bv)->next() && (cpos(bv) >= cpar(bv)->size()))
1862 return FINISHED_RIGHT;
1863 if (activate_inset && checkAndActivateInset(bv, front))
1865 getLyXText(bv)->cursorRight(bv);
1867 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1868 return DISPATCHED_NOUPDATE;
1872 UpdatableInset::RESULT
1873 InsetText::moveLeftIntern(BufferView * bv, bool front,
1874 bool activate_inset, bool selecting)
1876 if (!cpar(bv)->previous() && (cpos(bv) <= 0))
1878 getLyXText(bv)->cursorLeft(bv);
1880 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
1881 if (activate_inset && checkAndActivateInset(bv, front))
1883 return DISPATCHED_NOUPDATE;
1887 UpdatableInset::RESULT
1888 InsetText::moveUp(BufferView * bv)
1890 if (!crow(bv)->previous())
1892 getLyXText(bv)->cursorUp(bv);
1893 return DISPATCHED_NOUPDATE;
1897 UpdatableInset::RESULT
1898 InsetText::moveDown(BufferView * bv)
1900 if (!crow(bv)->next())
1901 return FINISHED_DOWN;
1902 getLyXText(bv)->cursorDown(bv);
1903 return DISPATCHED_NOUPDATE;
1907 bool InsetText::insertInset(BufferView * bv, Inset * inset)
1909 if (the_locking_inset) {
1910 if (the_locking_inset->insetAllowed(inset))
1911 return the_locking_inset->insertInset(bv, inset);
1914 inset->setOwner(this);
1915 hideInsetCursor(bv);
1916 getLyXText(bv)->insertInset(bv, inset);
1918 updateLocal(bv, CURSOR_PAR|CURSOR, true);
1923 bool InsetText::insetAllowed(Inset::Code code) const
1925 // in_insetAllowed is a really gross hack,
1926 // to allow us to call the owner's insetAllowed
1927 // without stack overflow, which can happen
1928 // when the owner uses InsetCollapsable::insetAllowed()
1930 if (in_insetAllowed)
1932 in_insetAllowed = true;
1933 if (the_locking_inset)
1934 ret = the_locking_inset->insetAllowed(code);
1936 ret = owner()->insetAllowed(code);
1937 in_insetAllowed = false;
1942 UpdatableInset * InsetText::getLockingInset() const
1944 return the_locking_inset ? the_locking_inset->getLockingInset() :
1945 const_cast<InsetText *>(this);
1949 UpdatableInset * InsetText::getFirstLockingInsetOfType(Inset::Code c)
1953 if (the_locking_inset)
1954 return the_locking_inset->getFirstLockingInsetOfType(c);
1959 bool InsetText::showInsetDialog(BufferView * bv) const
1961 if (the_locking_inset)
1962 return the_locking_inset->showInsetDialog(bv);
1967 vector<string> const InsetText::getLabelList() const
1969 vector<string> label_list;
1971 Paragraph * tpar = par;
1973 Paragraph::inset_iterator beg = tpar->inset_iterator_begin();
1974 Paragraph::inset_iterator end = tpar->inset_iterator_end();
1975 for (; beg != end; ++beg) {
1976 vector<string> const l = (*beg)->getLabelList();
1977 label_list.insert(label_list.end(), l.begin(), l.end());
1979 tpar = tpar->next();
1985 void InsetText::setFont(BufferView * bv, LyXFont const & font, bool toggleall,
1988 if (the_locking_inset) {
1989 the_locking_inset->setFont(bv, font, toggleall, selectall);
1992 if ((!par->next() && !par->size()) || !cpar(bv)->size()) {
1993 getLyXText(bv)->setFont(bv, font, toggleall);
1998 lt = getLyXText(bv);
2001 if (lt->selection.set()) {
2002 setUndo(bv, Undo::EDIT, lt->cursor.par(), lt->cursor.par()->next());
2006 lt->toggleFree(bv, font, toggleall);
2008 lt->clearSelection();
2010 bool flag = (selectall || lt->selection.set());
2014 updateLocal(bv, FULL, true);
2016 updateLocal(bv, CURSOR_PAR, true);
2020 bool InsetText::checkAndActivateInset(BufferView * bv, bool front)
2022 if (cpar(bv)->isInset(cpos(bv))) {
2024 static_cast<UpdatableInset*>(cpar(bv)->getInset(cpos(bv)));
2025 if (!isHighlyEditableInset(inset))
2027 inset->edit(bv, front);
2028 if (!the_locking_inset)
2030 updateLocal(bv, CURSOR, false);
2037 bool InsetText::checkAndActivateInset(BufferView * bv, int x, int y,
2038 mouse_button::state button)
2040 x -= drawTextXOffset;
2042 int dummyy = y + insetAscent;
2043 Inset * inset = bv->checkInsetHit(getLyXText(bv), dummyx, dummyy);
2044 // we only do the edit() call if the inset was hit by the mouse
2045 // or if it is a highly editable inset. So we should call this
2046 // function from our own edit with button < 0.
2047 // FIXME: GUII jbl. I've changed this to ::none for now which is probably
2049 if (button == mouse_button::none && !isHighlyEditableInset(inset))
2057 inset_x = cix(bv) - top_x + drawTextXOffset;
2058 inset_y = ciy(bv) + drawTextYOffset;
2059 inset->edit(bv, x - inset_x, y - inset_y, button);
2060 if (!the_locking_inset)
2062 updateLocal(bv, CURSOR, false);
2069 int InsetText::getMaxWidth(BufferView * bv, UpdatableInset const * inset) const
2072 int w = UpdatableInset::getMaxWidth(bv, inset);
2077 w = w - top_x + owner()->x();
2080 w -= (2 * TEXT_TO_INSET_OFFSET);
2083 return UpdatableInset::getMaxWidth(bv, inset);
2088 void InsetText::setParagraphData(Paragraph * p, bool same_id)
2090 // we have to unlock any locked inset otherwise we're in troubles
2091 the_locking_inset = 0;
2093 Paragraph * tmp = par->next();
2098 par = new Paragraph(*p, same_id);
2099 par->setInsetOwner(this);
2100 Paragraph * np = par;
2103 np->next(new Paragraph(*p, same_id));
2104 np->next()->previous(np);
2106 np->setInsetOwner(this);
2113 void InsetText::setText(string const & data, LyXFont const & font)
2116 for (unsigned int i=0; i < data.length(); ++i)
2117 par->insertChar(i, data[i], font);
2122 void InsetText::setAutoBreakRows(bool flag)
2124 if (flag != autoBreakRows) {
2125 autoBreakRows = flag;
2133 void InsetText::setDrawFrame(BufferView * bv, DrawFrame how)
2135 if (how != drawFrame_) {
2138 updateLocal(bv, DRAW_FRAME, false);
2143 void InsetText::setFrameColor(BufferView * bv, LColor::color col)
2145 if (frame_color != col) {
2148 updateLocal(bv, DRAW_FRAME, false);
2153 int InsetText::cx(BufferView * bv) const
2155 // we do nothing dangerous so we use a local cache
2156 LyXText * llt = getLyXText(bv);
2157 int x = llt->cursor.x() + top_x + TEXT_TO_INSET_OFFSET;
2158 if (the_locking_inset) {
2159 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2161 if (font.isVisibleRightToLeft())
2162 x -= the_locking_inset->width(bv, font);
2168 int InsetText::cix(BufferView * bv) const
2170 // we do nothing dangerous so we use a local cache
2171 LyXText * llt = getLyXText(bv);
2172 int x = llt->cursor.ix() + top_x + TEXT_TO_INSET_OFFSET;
2173 if (the_locking_inset) {
2174 LyXFont font = llt->getFont(bv->buffer(), llt->cursor.par(),
2176 if (font.isVisibleRightToLeft())
2177 x -= the_locking_inset->width(bv, font);
2183 int InsetText::cy(BufferView * bv) const
2186 return getLyXText(bv)->cursor.y() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2190 int InsetText::ciy(BufferView * bv) const
2193 return getLyXText(bv)->cursor.iy() - ascent(bv, font) + TEXT_TO_INSET_OFFSET;
2197 pos_type InsetText::cpos(BufferView * bv) const
2199 return getLyXText(bv)->cursor.pos();
2203 Paragraph * InsetText::cpar(BufferView * bv) const
2205 return getLyXText(bv)->cursor.par();
2209 bool InsetText::cboundary(BufferView * bv) const
2211 return getLyXText(bv)->cursor.boundary();
2215 Row * InsetText::crow(BufferView * bv) const
2217 return getLyXText(bv)->cursor.row();
2221 LyXText * InsetText::getLyXText(BufferView const * lbv,
2222 bool const recursive) const
2224 if (cached_bview == lbv) {
2225 if (recursive && the_locking_inset)
2226 return the_locking_inset->getLyXText(lbv, true);
2227 LyXText * lt = cached_text.get();
2228 lyx::Assert(lt && lt->firstRow()->par() == par);
2231 // Super UGLY! (Lgb)
2232 BufferView * bv = const_cast<BufferView *>(lbv);
2235 Cache::iterator it = cache.find(bv);
2237 if (it != cache.end()) {
2240 } else if (do_resize) {
2241 resizeLyXText(do_resize);
2243 if (lt || !it->second.remove) {
2244 lyx::Assert(it->second.text.get());
2245 cached_text = it->second.text;
2246 if (recursive && the_locking_inset) {
2247 return the_locking_inset->getLyXText(bv, true);
2249 return cached_text.get();
2250 } else if (it->second.remove) {
2252 saveLyXTextState(it->second.text.get());
2258 // when we have to reinit the existing LyXText!
2260 it->second.text->init(bv);
2261 restoreLyXTextState(bv, it->second.text.get());
2262 it->second.remove = false;
2264 cached_text = it->second.text;
2265 if (the_locking_inset && recursive) {
2266 return the_locking_inset->getLyXText(bv);
2268 return cached_text.get();
2271 // we are here only if we don't have a BufferView * in the cache!!!
2273 cached_text.reset(new LyXText(const_cast<InsetText *>(this)));
2274 cached_text->init(bv);
2275 restoreLyXTextState(bv, cached_text.get());
2277 cache.insert(make_pair(bv, cached_text));
2279 if (the_locking_inset && recursive) {
2280 return the_locking_inset->getLyXText(bv);
2282 return cached_text.get();
2286 void InsetText::deleteLyXText(BufferView * bv, bool recursive) const
2290 Cache::iterator it = cache.find(bv);
2292 if (it == cache.end()) {
2296 lyx::Assert(it->second.text.get());
2298 it->second.remove = true;
2300 /// then remove all LyXText in text-insets
2301 Paragraph * p = par;
2302 for (; p; p = p->next()) {
2303 p->deleteInsetsLyXText(bv);
2309 void InsetText::resizeLyXText(BufferView * bv, bool force) const
2312 // we cannot resize this because we are in use!
2313 // so do this on the next possible getLyXText()
2318 // lyxerr << "InsetText::resizeLyXText\n";
2319 if (!par->next() && !par->size()) { // no data, resize not neccessary!
2320 // we have to do this as a fixed width may have changed!
2321 LyXText * t = getLyXText(bv);
2322 saveLyXTextState(t);
2324 restoreLyXTextState(bv, t);
2327 // one endless line, resize normally not necessary
2328 if (!force && getMaxWidth(bv, this) < 0)
2331 Cache::iterator it = cache.find(bv);
2332 if (it == cache.end()) {
2335 lyx::Assert(it->second.text.get());
2337 LyXText * t = it->second.text.get();
2338 saveLyXTextState(t);
2339 for (Paragraph * p = par; p; p = p->next()) {
2340 p->resizeInsetsLyXText(bv);
2343 restoreLyXTextState(bv, t);
2344 if (the_locking_inset) {
2345 inset_x = cix(bv) - top_x + drawTextXOffset;
2346 inset_y = ciy(bv) + drawTextYOffset;
2349 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2351 updateLocal(bv, FULL, false);
2352 // this will scroll the screen such that the cursor becomes visible
2353 bv->updateScrollbar();
2355 need_update |= FULL;
2360 void InsetText::reinitLyXText() const
2363 // we cannot resize this because we are in use!
2364 // so do this on the next possible getLyXText()
2370 // lyxerr << "InsetText::reinitLyXText\n";
2371 for(Cache::iterator it = cache.begin(); it != cache.end(); ++it) {
2372 lyx::Assert(it->second.text.get());
2374 LyXText * t = it->second.text.get();
2375 BufferView * bv = it->first;
2377 saveLyXTextState(t);
2378 for (Paragraph * p = par; p; p = p->next()) {
2379 p->resizeInsetsLyXText(bv);
2382 restoreLyXTextState(bv, t);
2383 if (the_locking_inset) {
2384 inset_x = cix(bv) - top_x + drawTextXOffset;
2385 inset_y = ciy(bv) + drawTextYOffset;
2387 t->first_y = bv->screen().topCursorVisible(t->cursor, t->first_y);
2389 updateLocal(bv, FULL, false);
2390 // this will scroll the screen such that the cursor becomes visible
2391 bv->updateScrollbar();
2399 void InsetText::removeNewlines()
2401 bool changed = false;
2403 for (Paragraph * p = par; p; p = p->next()) {
2404 for (int i = 0; i < p->size(); ++i) {
2405 if (p->getChar(i) == Paragraph::META_NEWLINE) {
2416 bool InsetText::nodraw() const
2418 if (the_locking_inset)
2419 return the_locking_inset->nodraw();
2420 return UpdatableInset::nodraw();
2424 int InsetText::scroll(bool recursive) const
2426 int sx = UpdatableInset::scroll(false);
2428 if (recursive && the_locking_inset)
2429 sx += the_locking_inset->scroll(recursive);
2435 bool InsetText::doClearArea() const
2437 return !locked || (need_update & (FULL|INIT));
2441 void InsetText::selectAll(BufferView * bv)
2443 getLyXText(bv)->cursorTop(bv);
2444 getLyXText(bv)->selection.cursor = getLyXText(bv)->cursor;
2445 getLyXText(bv)->cursorBottom(bv);
2446 getLyXText(bv)->setSelection(bv);
2450 void InsetText::clearSelection(BufferView * bv)
2452 getLyXText(bv)->clearSelection();
2456 void InsetText::clearInset(BufferView * bv, int baseline, bool & cleared) const
2458 Painter & pain = bv->painter();
2460 int h = insetAscent + insetDescent;
2461 int ty = baseline - insetAscent;
2467 if ((ty + h) > pain.paperHeight())
2468 h = pain.paperHeight();
2469 if ((top_x + drawTextXOffset + w) > pain.paperWidth())
2470 w = pain.paperWidth();
2471 // w -= TEXT_TO_INSET_OFFSET;
2472 pain.fillRectangle(top_x + 1, ty + 1, w - 1, h - 1, backgroundColor());
2475 frame_is_visible = false;
2479 Paragraph * InsetText::getParFromID(int id) const
2482 Paragraph * result = par;
2483 Paragraph * ires = 0;
2484 while (result && result->id() != id) {
2485 if ((ires = result->getParFromID(id)))
2487 result = result->next();
2491 Paragraph * tmp = par;
2493 if (tmp->id() == id) {
2496 Paragraph * tmp2 = tmp->getParFromID(id);
2507 Paragraph * InsetText::firstParagraph() const
2510 if (the_locking_inset)
2511 if ((result = the_locking_inset->firstParagraph()))
2517 Paragraph * InsetText::getFirstParagraph(int i) const
2519 return (i == 0) ? par : 0;
2523 LyXCursor const & InsetText::cursor(BufferView * bv) const
2525 if (the_locking_inset)
2526 return the_locking_inset->cursor(bv);
2527 return getLyXText(bv)->cursor;
2531 Paragraph * InsetText::paragraph() const
2537 void InsetText::paragraph(Paragraph * p)
2539 // GENERAL COMMENT: We don't have to free the old paragraphs as the
2540 // caller of this function has to take care of it. This IS important
2541 // as we could have to insert a paragraph before this one and just
2542 // link the actual to a new ones next and set it with this function
2545 // set ourself as owner for all the paragraphs inserted!
2546 Paragraph * np = par;
2548 np->setInsetOwner(this);
2552 // redraw myself when asked for
2557 Inset * InsetText::getInsetFromID(int id_arg) const
2560 return const_cast<InsetText *>(this);
2562 Paragraph * lp = par;
2565 for (Paragraph::inset_iterator it = lp->inset_iterator_begin(),
2566 en = lp->inset_iterator_end();
2569 if ((*it)->id() == id_arg)
2571 Inset * in = (*it)->getInsetFromID(id_arg);
2581 WordLangTuple InsetText::selectNextWordToSpellcheck(BufferView * bv, float & value) const
2587 lt = getLyXText(bv);
2590 if (the_locking_inset) {
2591 word = the_locking_inset->selectNextWordToSpellcheck(bv, value);
2592 if (!word.word().empty()) {
2598 // we have to go on checking so move cursor to the next char
2599 lt->cursor.pos(lt->cursor.pos() + 1);
2601 word = lt->selectNextWordToSpellcheck(bv, value);
2602 if (word.word().empty())
2603 bv->unlockInset(const_cast<InsetText *>(this));
2612 void InsetText::selectSelectedWord(BufferView * bv)
2614 if (the_locking_inset) {
2615 the_locking_inset->selectSelectedWord(bv);
2618 getLyXText(bv)->selectSelectedWord(bv);
2619 updateLocal(bv, SELECTION, false);
2623 void InsetText::toggleSelection(BufferView * bv, bool kill_selection)
2625 if (the_locking_inset) {
2626 the_locking_inset->toggleSelection(bv, kill_selection);
2630 lt = getLyXText(bv);
2634 int x = top_x + TEXT_TO_INSET_OFFSET;
2636 Row * row = lt->firstRow();
2637 int y_offset = top_baseline - row->ascent_of_text();
2639 while ((row != 0) && ((y+row->height()) <= 0)) {
2646 if (need_update & SELECTION)
2648 bv->screen().toggleSelection(lt, bv, kill_selection, y_offset, x);
2654 bool InsetText::searchForward(BufferView * bv, string const & str,
2659 lt = getLyXText(bv);
2662 if (the_locking_inset) {
2663 if (the_locking_inset->searchForward(bv, str, cs, mw))
2665 lt->cursorRight(bv, true);
2667 lyxfind::SearchResult result =
2668 lyxfind::LyXFind(bv, lt, str, true, cs, mw);
2670 if (result == lyxfind::SR_FOUND) {
2671 LyXCursor cur = lt->cursor;
2672 bv->unlockInset(bv->theLockingInset());
2673 if (bv->lockInset(this))
2676 lt->setSelectionOverString(bv, str);
2677 updateLocal(bv, SELECTION, false);
2681 return (result != lyxfind::SR_NOT_FOUND);
2684 bool InsetText::searchBackward(BufferView * bv, string const & str,
2687 if (the_locking_inset) {
2688 if (the_locking_inset->searchBackward(bv, str, cs, mw))
2693 lt = getLyXText(bv);
2697 Paragraph * p = par;
2700 lt->setCursor(bv, p, p->size());
2702 lyxfind::SearchResult result =
2703 lyxfind::LyXFind(bv, lt, str, false, cs, mw);
2705 if (result == lyxfind::SR_FOUND) {
2706 LyXCursor cur = lt->cursor;
2707 bv->unlockInset(bv->theLockingInset());
2708 if (bv->lockInset(this))
2711 lt->setSelectionOverString(bv, str);
2712 updateLocal(bv, SELECTION, false);
2716 return (result != lyxfind::SR_NOT_FOUND);
2720 bool InsetText::checkInsertChar(LyXFont & font)
2723 return owner()->checkInsertChar(font);
2728 void InsetText::collapseParagraphs(BufferView * bv) const
2730 BufferParams const & bparams = bv->buffer()->params;
2731 LyXText * llt = getLyXText(bv);
2733 while(par->next()) {
2734 if (par->size() && par->next()->size() &&
2735 !par->isSeparator(par->size()-1))
2737 par->insertChar(par->size(), ' ');
2739 if (llt->selection.set()) {
2740 if (llt->selection.start.par() == par->next()) {
2741 llt->selection.start.par(par);
2742 llt->selection.start.pos(
2743 llt->selection.start.pos() + par->size());
2745 if (llt->selection.end.par() == par->next()) {
2746 llt->selection.end.par(par);
2747 llt->selection.end.pos(
2748 llt->selection.end.pos() + par->size());
2751 par->pasteParagraph(bparams);
2757 void InsetText::getDrawFont(LyXFont & font) const
2761 owner()->getDrawFont(font);
2765 void InsetText::appendParagraphs(BufferParams const & bparams,
2769 Paragraph * tmpbuf = newpar;
2770 Paragraph * lastbuffer = buf = new Paragraph(*tmpbuf, false);
2772 while (tmpbuf->next()) {
2773 tmpbuf = tmpbuf->next();
2774 lastbuffer->next(new Paragraph(*tmpbuf, false));
2775 lastbuffer->next()->previous(lastbuffer);
2776 lastbuffer = lastbuffer->next();
2779 while (lastbuffer->next())
2780 lastbuffer = lastbuffer->next();
2781 if (newpar->size() && lastbuffer->size() &&
2782 !lastbuffer->isSeparator(lastbuffer->size()-1))
2784 lastbuffer->insertChar(lastbuffer->size(), ' ');
2787 // make the buf exactly the same layout than our last paragraph
2788 buf->makeSameLayout(lastbuffer);
2791 lastbuffer->next(buf);
2792 buf->previous(lastbuffer);
2793 lastbuffer->pasteParagraph(bparams);
2799 void InsetText::addPreview(grfx::PreviewLoader & loader) const
2801 Paragraph * par = getFirstParagraph(0);
2803 Paragraph::inset_iterator it = par->inset_iterator_begin();
2804 Paragraph::inset_iterator end = par->inset_iterator_end();
2805 for (; it != end; ++it) {
2806 it->addPreview(loader);