LyX 1.6.0 release candidate 4 (rc4)
[lyx.git] / src / BufferView.cpp
blobcea5b723c6d890a1c0e60e68bc9f1ec7bd906b31
1 /**
2 * \file BufferView.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alfredo Braunstein
7 * \author Lars Gullik Bjønnes
8 * \author John Levon
9 * \author André Pönitz
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
15 #include <config.h>
17 #include "BufferView.h"
19 #include "BranchList.h"
20 #include "Buffer.h"
21 #include "buffer_funcs.h"
22 #include "BufferList.h"
23 #include "BufferParams.h"
24 #include "CoordCache.h"
25 #include "Cursor.h"
26 #include "CutAndPaste.h"
27 #include "DispatchResult.h"
28 #include "ErrorList.h"
29 #include "factory.h"
30 #include "FloatList.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
33 #include "Intl.h"
34 #include "InsetIterator.h"
35 #include "Language.h"
36 #include "LaTeXFeatures.h"
37 #include "LyX.h"
38 #include "lyxfind.h"
39 #include "LyXFunc.h"
40 #include "Layout.h"
41 #include "LyXRC.h"
42 #include "MetricsInfo.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
47 #include "Session.h"
48 #include "Text.h"
49 #include "TextClass.h"
50 #include "TextMetrics.h"
51 #include "TexRow.h"
52 #include "TocBackend.h"
53 #include "VSpace.h"
54 #include "WordLangTuple.h"
56 #include "insets/InsetBibtex.h"
57 #include "insets/InsetCommand.h" // ChangeRefs
58 #include "insets/InsetExternal.h"
59 #include "insets/InsetGraphics.h"
60 #include "insets/InsetRef.h"
61 #include "insets/InsetText.h"
62 #include "insets/InsetNote.h"
64 #include "frontends/alert.h"
65 #include "frontends/Application.h"
66 #include "frontends/Delegates.h"
67 #include "frontends/FontMetrics.h"
68 #include "frontends/Painter.h"
69 #include "frontends/Selection.h"
71 #include "graphics/Previews.h"
73 #include "support/convert.h"
74 #include "support/debug.h"
75 #include "support/ExceptionMessage.h"
76 #include "support/filetools.h"
77 #include "support/gettext.h"
78 #include "support/lstrings.h"
79 #include "support/Package.h"
80 #include "support/types.h"
82 #include <cerrno>
83 #include <fstream>
84 #include <functional>
85 #include <iterator>
86 #include <vector>
88 using namespace std;
89 using namespace lyx::support;
91 namespace lyx {
93 namespace Alert = frontend::Alert;
95 namespace {
97 /// Return an inset of this class if it exists at the current cursor position
98 template <class T>
99 T * getInsetByCode(Cursor const & cur, InsetCode code)
101 DocIterator it = cur;
102 Inset * inset = it.nextInset();
103 if (inset && inset->lyxCode() == code)
104 return static_cast<T*>(inset);
105 return 0;
109 bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
110 bool same_content);
112 bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
113 docstring const & contents)
115 DocIterator tmpdit = dit;
117 while (tmpdit) {
118 Inset const * inset = tmpdit.nextInset();
119 if (inset
120 && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
121 && (contents.empty() ||
122 static_cast<InsetCommand const *>(inset)->getFirstNonOptParam() == contents)) {
123 dit = tmpdit;
124 return true;
126 tmpdit.forwardInset();
129 return false;
133 /// Looks for next inset with one of the given codes.
134 bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
135 bool same_content)
137 docstring contents;
138 DocIterator tmpdit = dit;
139 tmpdit.forwardInset();
140 if (!tmpdit)
141 return false;
143 if (same_content) {
144 Inset const * inset = tmpdit.nextInset();
145 if (inset
146 && std::find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
147 contents = static_cast<InsetCommand const *>(inset)->getFirstNonOptParam();
151 if (!findNextInset(tmpdit, codes, contents)) {
152 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
153 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
154 if (!findNextInset(tmpdit, codes, contents))
155 return false;
156 } else
157 return false;
160 dit = tmpdit;
161 return true;
165 /// Looks for next inset with the given code
166 void findInset(DocIterator & dit, InsetCode code, bool same_content)
168 findInset(dit, vector<InsetCode>(1, code), same_content);
172 /// Moves cursor to the next inset with one of the given codes.
173 void gotoInset(BufferView * bv, vector<InsetCode> const & codes,
174 bool same_content)
176 Cursor tmpcur = bv->cursor();
177 if (!findInset(tmpcur, codes, same_content)) {
178 bv->cursor().message(_("No more insets"));
179 return;
182 tmpcur.clearSelection();
183 bv->setCursor(tmpcur);
184 bv->showCursor();
188 /// Moves cursor to the next inset with given code.
189 void gotoInset(BufferView * bv, InsetCode code, bool same_content)
191 gotoInset(bv, vector<InsetCode>(1, code), same_content);
195 /// A map from a Text to the associated text metrics
196 typedef map<Text const *, TextMetrics> TextMetricsCache;
198 enum ScreenUpdateStrategy {
199 NoScreenUpdate,
200 SingleParUpdate,
201 FullScreenUpdate,
202 DecorationUpdate
205 } // anon namespace
208 /////////////////////////////////////////////////////////////////////
210 // BufferView
212 /////////////////////////////////////////////////////////////////////
214 struct BufferView::Private
216 Private(BufferView & bv): wh_(0), cursor_(bv),
217 anchor_pit_(0), anchor_ypos_(0),
218 inlineCompletionUniqueChars_(0),
219 last_inset_(0), gui_(0)
223 ScrollbarParameters scrollbarParameters_;
225 ScreenUpdateStrategy update_strategy_;
227 CoordCache coord_cache_;
229 /// Estimated average par height for scrollbar.
230 int wh_;
231 /// this is used to handle XSelection events in the right manner.
232 struct {
233 CursorSlice cursor;
234 CursorSlice anchor;
235 bool set;
236 } xsel_cache_;
238 Cursor cursor_;
240 pit_type anchor_pit_;
242 int anchor_ypos_;
244 vector<int> par_height_;
247 DocIterator inlineCompletionPos_;
249 docstring inlineCompletion_;
251 size_t inlineCompletionUniqueChars_;
253 /// keyboard mapping object.
254 Intl intl_;
256 /// last visited inset.
257 /** kept to send setMouseHover(false).
258 * Not owned, so don't delete.
260 Inset * last_inset_;
262 mutable TextMetricsCache text_metrics_;
264 /// Whom to notify.
265 /** Not owned, so don't delete.
267 frontend::GuiBufferViewDelegate * gui_;
269 /// Cache for Find Next
270 FuncRequest search_request_cache_;
274 BufferView::BufferView(Buffer & buf)
275 : width_(0), height_(0), full_screen_(false), buffer_(buf), d(new Private(*this))
277 d->xsel_cache_.set = false;
278 d->intl_.initKeyMapper(lyxrc.use_kbmap);
280 d->cursor_.push(buffer_.inset());
281 d->cursor_.resetAnchor();
282 d->cursor_.setCurrentFont();
284 if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
285 thePreviews().generateBufferPreviews(buffer_);
289 BufferView::~BufferView()
291 // current buffer is going to be switched-off, save cursor pos
292 // Ideally, the whole cursor stack should be saved, but session
293 // currently can only handle bottom (whole document) level pit and pos.
294 // That is to say, if a cursor is in a nested inset, it will be
295 // restore to the left of the top level inset.
296 LastFilePosSection::FilePos fp;
297 fp.pit = d->cursor_.bottom().pit();
298 fp.pos = d->cursor_.bottom().pos();
299 theSession().lastFilePos().save(buffer_.fileName(), fp);
301 delete d;
305 int BufferView::rightMargin() const
307 // The additional test for the case the outliner is opened.
308 if (!full_screen_ ||
309 !lyxrc.full_screen_limit ||
310 width_ < lyxrc.full_screen_width + 20)
311 return 10;
313 return (width_ - lyxrc.full_screen_width) / 2;
317 int BufferView::leftMargin() const
319 return rightMargin();
323 bool BufferView::isTopScreen() const
325 return d->scrollbarParameters_.position == d->scrollbarParameters_.min;
329 bool BufferView::isBottomScreen() const
331 return d->scrollbarParameters_.position == d->scrollbarParameters_.max;
335 Intl & BufferView::getIntl()
337 return d->intl_;
341 Intl const & BufferView::getIntl() const
343 return d->intl_;
347 CoordCache & BufferView::coordCache()
349 return d->coord_cache_;
353 CoordCache const & BufferView::coordCache() const
355 return d->coord_cache_;
359 Buffer & BufferView::buffer()
361 return buffer_;
365 Buffer const & BufferView::buffer() const
367 return buffer_;
371 bool BufferView::fitCursor()
373 if (cursorStatus(d->cursor_) == CUR_INSIDE) {
374 frontend::FontMetrics const & fm =
375 theFontMetrics(d->cursor_.getFont().fontInfo());
376 int const asc = fm.maxAscent();
377 int const des = fm.maxDescent();
378 Point const p = getPos(d->cursor_, d->cursor_.boundary());
379 if (p.y_ - asc >= 0 && p.y_ + des < height_)
380 return false;
382 return true;
386 void BufferView::processUpdateFlags(Update::flags flags)
388 // last_inset_ points to the last visited inset. This pointer may become
389 // invalid because of keyboard editing. Since all such operations
390 // causes screen update(), I reset last_inset_ to avoid such a problem.
391 d->last_inset_ = 0;
392 // This is close to a hot-path.
393 LYXERR(Debug::DEBUG, "BufferView::processUpdateFlags()"
394 << "[fitcursor = " << (flags & Update::FitCursor)
395 << ", forceupdate = " << (flags & Update::Force)
396 << ", singlepar = " << (flags & Update::SinglePar)
397 << "] buffer: " << &buffer_);
399 buffer_.updateMacros();
401 // Now do the first drawing step if needed. This consists on updating
402 // the CoordCache in updateMetrics().
403 // The second drawing step is done in WorkArea::redraw() if needed.
405 // Case when no explicit update is requested.
406 if (!flags) {
407 // no need to redraw anything.
408 d->update_strategy_ = NoScreenUpdate;
409 return;
412 if (flags == Update::Decoration) {
413 d->update_strategy_ = DecorationUpdate;
414 buffer_.changed();
415 return;
418 if (flags == Update::FitCursor
419 || flags == (Update::Decoration | Update::FitCursor)) {
420 // tell the frontend to update the screen if needed.
421 if (fitCursor()) {
422 showCursor();
423 return;
425 if (flags & Update::Decoration) {
426 d->update_strategy_ = DecorationUpdate;
427 buffer_.changed();
428 return;
430 // no screen update is needed.
431 d->update_strategy_ = NoScreenUpdate;
432 return;
435 bool const full_metrics = flags & Update::Force || !singleParUpdate();
437 if (full_metrics)
438 // We have to update the full screen metrics.
439 updateMetrics();
441 if (!(flags & Update::FitCursor)) {
442 // Nothing to do anymore. Trigger a redraw and return
443 buffer_.changed();
444 return;
447 // updateMetrics() does not update paragraph position
448 // This is done at draw() time. So we need a redraw!
449 buffer_.changed();
451 if (fitCursor()) {
452 // The cursor is off screen so ensure it is visible.
453 // refresh it:
454 showCursor();
459 void BufferView::updateScrollbar()
461 if (height_ == 0 && width_ == 0)
462 return;
464 // We prefer fixed size line scrolling.
465 d->scrollbarParameters_.single_step = defaultRowHeight();
466 // We prefer full screen page scrolling.
467 d->scrollbarParameters_.page_step = height_;
469 Text & t = buffer_.text();
470 TextMetrics & tm = d->text_metrics_[&t];
472 LYXERR(Debug::GUI, " Updating scrollbar: height: "
473 << t.paragraphs().size()
474 << " curr par: " << d->cursor_.bottom().pit()
475 << " default height " << defaultRowHeight());
477 size_t const parsize = t.paragraphs().size();
478 if (d->par_height_.size() != parsize) {
479 d->par_height_.clear();
480 // FIXME: We assume a default paragraph height of 2 rows. This
481 // should probably be pondered with the screen width.
482 d->par_height_.resize(parsize, defaultRowHeight() * 2);
485 // Look at paragraph heights on-screen
486 pair<pit_type, ParagraphMetrics const *> first = tm.first();
487 pair<pit_type, ParagraphMetrics const *> last = tm.last();
488 for (pit_type pit = first.first; pit <= last.first; ++pit) {
489 d->par_height_[pit] = tm.parMetrics(pit).height();
490 LYXERR(Debug::SCROLLING, "storing height for pit " << pit << " : "
491 << d->par_height_[pit]);
494 int top_pos = first.second->position() - first.second->ascent();
495 int bottom_pos = last.second->position() + last.second->descent();
496 bool first_visible = first.first == 0 && top_pos >= 0;
497 bool last_visible = last.first + 1 == int(parsize) && bottom_pos <= height_;
498 if (first_visible && last_visible) {
499 d->scrollbarParameters_.min = 0;
500 d->scrollbarParameters_.max = 0;
501 return;
504 d->scrollbarParameters_.min = top_pos;
505 for (size_t i = 0; i != size_t(first.first); ++i)
506 d->scrollbarParameters_.min -= d->par_height_[i];
507 d->scrollbarParameters_.max = bottom_pos;
508 for (size_t i = last.first + 1; i != parsize; ++i)
509 d->scrollbarParameters_.max += d->par_height_[i];
511 d->scrollbarParameters_.position = 0;
512 // The reference is the top position so we remove one page.
513 d->scrollbarParameters_.max -= d->scrollbarParameters_.page_step;
517 ScrollbarParameters const & BufferView::scrollbarParameters() const
519 return d->scrollbarParameters_;
523 docstring BufferView::toolTip(int x, int y) const
525 // Get inset under mouse, if there is one.
526 Inset const * covering_inset = getCoveringInset(buffer_.text(), x, y);
527 if (!covering_inset)
528 // No inset, no tooltip...
529 return docstring();
530 return covering_inset->toolTip(*this, x, y);
534 docstring BufferView::contextMenu(int x, int y) const
536 //If there is a selection, return the containing inset menu
537 if (d->cursor_.selection())
538 return d->cursor_.inset().contextMenu(*this, x, y);
540 // Get inset under mouse, if there is one.
541 Inset const * covering_inset = getCoveringInset(buffer_.text(), x, y);
542 if (covering_inset)
543 return covering_inset->contextMenu(*this, x, y);
545 return buffer_.inset().contextMenu(*this, x, y);
549 void BufferView::scrollDocView(int value)
551 int const offset = value - d->scrollbarParameters_.position;
552 // If the offset is less than 2 screen height, prefer to scroll instead.
553 if (abs(offset) <= 2 * height_) {
554 scroll(offset);
555 updateMetrics();
556 buffer_.changed();
557 return;
560 // cut off at the top
561 if (value <= d->scrollbarParameters_.min) {
562 DocIterator dit = doc_iterator_begin(buffer_.inset());
563 showCursor(dit);
564 LYXERR(Debug::SCROLLING, "scroll to top");
565 return;
568 // cut off at the bottom
569 if (value >= d->scrollbarParameters_.max) {
570 DocIterator dit = doc_iterator_end(buffer_.inset());
571 dit.backwardPos();
572 showCursor(dit);
573 LYXERR(Debug::SCROLLING, "scroll to bottom");
574 return;
577 // find paragraph at target position
578 int par_pos = d->scrollbarParameters_.min;
579 pit_type i = 0;
580 for (; i != int(d->par_height_.size()); ++i) {
581 par_pos += d->par_height_[i];
582 if (par_pos >= value)
583 break;
586 if (par_pos < value) {
587 // It seems we didn't find the correct pit so stay on the safe side and
588 // scroll to bottom.
589 LYXERR0("scrolling position not found!");
590 scrollDocView(d->scrollbarParameters_.max);
591 return;
594 DocIterator dit = doc_iterator_begin(buffer_.inset());
595 dit.pit() = i;
596 LYXERR(Debug::SCROLLING, "value = " << value << " -> scroll to pit " << i);
597 showCursor(dit);
601 // FIXME: this method is not working well.
602 void BufferView::setCursorFromScrollbar()
604 TextMetrics & tm = d->text_metrics_[&buffer_.text()];
606 int const height = 2 * defaultRowHeight();
607 int const first = height;
608 int const last = height_ - height;
609 int newy = 0;
610 Cursor const & oldcur = d->cursor_;
612 switch (cursorStatus(oldcur)) {
613 case CUR_ABOVE:
614 newy = first;
615 break;
616 case CUR_BELOW:
617 newy = last;
618 break;
619 case CUR_INSIDE:
620 int const y = getPos(oldcur, oldcur.boundary()).y_;
621 newy = min(last, max(y, first));
622 if (y == newy)
623 return;
625 // We reset the cursor because cursorStatus() does not
626 // work when the cursor is within mathed.
627 Cursor cur(*this);
628 cur.reset(buffer_.inset());
629 tm.setCursorFromCoordinates(cur, 0, newy);
630 mouseSetCursor(cur);
634 Change const BufferView::getCurrentChange() const
636 if (!d->cursor_.selection())
637 return Change(Change::UNCHANGED);
639 DocIterator dit = d->cursor_.selectionBegin();
640 return dit.paragraph().lookupChange(dit.pos());
644 // this could be used elsewhere as well?
645 // FIXME: This does not work within mathed!
646 CursorStatus BufferView::cursorStatus(DocIterator const & dit) const
648 Point const p = getPos(dit, dit.boundary());
649 if (p.y_ < 0)
650 return CUR_ABOVE;
651 if (p.y_ > workHeight())
652 return CUR_BELOW;
653 return CUR_INSIDE;
657 void BufferView::saveBookmark(unsigned int idx)
659 // tenatively save bookmark, id and pos will be used to
660 // acturately locate a bookmark in a 'live' lyx session.
661 // pit and pos will be updated with bottom level pit/pos
662 // when lyx exits.
663 theSession().bookmarks().save(
664 buffer_.fileName(),
665 d->cursor_.bottom().pit(),
666 d->cursor_.bottom().pos(),
667 d->cursor_.paragraph().id(),
668 d->cursor_.pos(),
671 if (idx)
672 // emit message signal.
673 message(_("Save bookmark"));
677 bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
678 int top_id, pos_type top_pos)
680 bool success = false;
681 DocIterator dit;
683 d->cursor_.clearSelection();
685 // if a valid par_id is given, try it first
686 // This is the case for a 'live' bookmark when unique paragraph ID
687 // is used to track bookmarks.
688 if (top_id > 0) {
689 dit = buffer_.getParFromID(top_id);
690 if (!dit.atEnd()) {
691 dit.pos() = min(dit.paragraph().size(), top_pos);
692 // Some slices of the iterator may not be
693 // reachable (e.g. closed collapsable inset)
694 // so the dociterator may need to be
695 // shortened. Otherwise, setCursor may crash
696 // lyx when the cursor can not be set to these
697 // insets.
698 size_t const n = dit.depth();
699 for (size_t i = 0; i < n; ++i)
700 if (dit[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
701 dit.resize(i);
702 break;
704 success = true;
708 // if top_id == 0, or searching through top_id failed
709 // This is the case for a 'restored' bookmark when only bottom
710 // (document level) pit was saved. Because of this, bookmark
711 // restoration is inaccurate. If a bookmark was within an inset,
712 // it will be restored to the left of the outmost inset that contains
713 // the bookmark.
714 if (bottom_pit < int(buffer_.paragraphs().size())) {
715 dit = doc_iterator_begin(buffer_.inset());
717 dit.pit() = bottom_pit;
718 dit.pos() = min(bottom_pos, dit.paragraph().size());
719 success = true;
722 if (success) {
723 // Note: only bottom (document) level pit is set.
724 setCursor(dit);
725 // set the current font.
726 d->cursor_.setCurrentFont();
727 // To center the screen on this new position we need the
728 // paragraph position which is computed at draw() time.
729 // So we need a redraw!
730 buffer_.changed();
731 if (fitCursor())
732 showCursor();
735 return success;
739 void BufferView::translateAndInsert(char_type c, Text * t, Cursor & cur)
741 if (lyxrc.rtl_support) {
742 if (d->cursor_.real_current_font.isRightToLeft()) {
743 if (d->intl_.keymap == Intl::PRIMARY)
744 d->intl_.keyMapSec();
745 } else {
746 if (d->intl_.keymap == Intl::SECONDARY)
747 d->intl_.keyMapPrim();
751 d->intl_.getTransManager().translateAndInsert(c, t, cur);
755 int BufferView::workWidth() const
757 return width_;
761 void BufferView::showCursor()
763 showCursor(d->cursor_);
767 void BufferView::showCursor(DocIterator const & dit)
769 // We are not properly started yet, delay until resizing is
770 // done.
771 if (height_ == 0)
772 return;
774 LYXERR(Debug::SCROLLING, "recentering!");
776 CursorSlice const & bot = dit.bottom();
777 TextMetrics & tm = d->text_metrics_[bot.text()];
779 pos_type const max_pit = pos_type(bot.text()->paragraphs().size() - 1);
780 int bot_pit = bot.pit();
781 if (bot_pit > max_pit) {
782 // FIXME: Why does this happen?
783 LYXERR0("bottom pit is greater that max pit: "
784 << bot_pit << " > " << max_pit);
785 bot_pit = max_pit;
788 if (bot_pit == tm.first().first - 1)
789 tm.newParMetricsUp();
790 else if (bot_pit == tm.last().first + 1)
791 tm.newParMetricsDown();
793 if (tm.contains(bot_pit)) {
794 ParagraphMetrics const & pm = tm.parMetrics(bot_pit);
795 LASSERT(!pm.rows().empty(), /**/);
796 // FIXME: smooth scrolling doesn't work in mathed.
797 CursorSlice const & cs = dit.innerTextSlice();
798 int offset = coordOffset(dit, dit.boundary()).y_;
799 int ypos = pm.position() + offset;
800 Dimension const & row_dim =
801 pm.getRow(cs.pos(), dit.boundary()).dimension();
802 int scrolled = 0;
803 if (ypos - row_dim.ascent() < 0)
804 scrolled = scrollUp(- ypos + row_dim.ascent());
805 else if (ypos + row_dim.descent() > height_)
806 scrolled = scrollDown(ypos - height_ + row_dim.descent());
807 // else, nothing to do, the cursor is already visible so we just return.
808 if (scrolled != 0) {
809 updateMetrics();
810 buffer_.changed();
812 return;
815 // fix inline completion position
816 if (d->inlineCompletionPos_.fixIfBroken())
817 d->inlineCompletionPos_ = DocIterator();
819 tm.redoParagraph(bot_pit);
820 ParagraphMetrics const & pm = tm.parMetrics(bot_pit);
821 int offset = coordOffset(dit, dit.boundary()).y_;
823 d->anchor_pit_ = bot_pit;
824 CursorSlice const & cs = dit.innerTextSlice();
825 Dimension const & row_dim =
826 pm.getRow(cs.pos(), dit.boundary()).dimension();
828 if (d->anchor_pit_ == 0)
829 d->anchor_ypos_ = offset + pm.ascent();
830 else if (d->anchor_pit_ == max_pit)
831 d->anchor_ypos_ = height_ - offset - row_dim.descent();
832 else
833 d->anchor_ypos_ = defaultRowHeight() * 2 - offset - row_dim.descent();
835 updateMetrics();
836 buffer_.changed();
840 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
842 FuncStatus flag;
844 Cursor & cur = d->cursor_;
846 switch (cmd.action) {
848 case LFUN_UNDO:
849 flag.setEnabled(buffer_.undo().hasUndoStack());
850 break;
851 case LFUN_REDO:
852 flag.setEnabled(buffer_.undo().hasRedoStack());
853 break;
854 case LFUN_FILE_INSERT:
855 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
856 case LFUN_FILE_INSERT_PLAINTEXT:
857 case LFUN_BOOKMARK_SAVE:
858 // FIXME: Actually, these LFUNS should be moved to Text
859 flag.setEnabled(cur.inTexted());
860 break;
861 case LFUN_FONT_STATE:
862 case LFUN_LABEL_INSERT:
863 case LFUN_INFO_INSERT:
864 case LFUN_INSET_EDIT:
865 case LFUN_PARAGRAPH_GOTO:
866 case LFUN_NOTE_NEXT:
867 case LFUN_REFERENCE_NEXT:
868 case LFUN_WORD_FIND:
869 case LFUN_WORD_REPLACE:
870 case LFUN_MARK_OFF:
871 case LFUN_MARK_ON:
872 case LFUN_MARK_TOGGLE:
873 case LFUN_SCREEN_RECENTER:
874 case LFUN_BIBTEX_DATABASE_ADD:
875 case LFUN_BIBTEX_DATABASE_DEL:
876 case LFUN_NOTES_MUTATE:
877 case LFUN_ALL_INSETS_TOGGLE:
878 case LFUN_STATISTICS:
879 flag.setEnabled(true);
880 break;
882 case LFUN_NEXT_INSET_TOGGLE:
883 case LFUN_NEXT_INSET_MODIFY: {
884 // this is the real function we want to invoke
885 FuncRequest tmpcmd = cmd;
886 tmpcmd.action = (cmd.action == LFUN_NEXT_INSET_TOGGLE)
887 ? LFUN_INSET_TOGGLE : LFUN_INSET_MODIFY;
888 // if there is an inset at cursor, see whether it
889 // handles the lfun, other start from scratch
890 Inset * inset = cur.nextInset();
891 if (!inset || !inset->getStatus(cur, tmpcmd, flag))
892 flag = lyx::getStatus(tmpcmd);
893 break;
896 case LFUN_LABEL_GOTO: {
897 flag.setEnabled(!cmd.argument().empty()
898 || getInsetByCode<InsetRef>(cur, REF_CODE));
899 break;
902 case LFUN_CHANGES_TRACK:
903 flag.setEnabled(true);
904 flag.setOnOff(buffer_.params().trackChanges);
905 break;
907 case LFUN_CHANGES_OUTPUT:
908 flag.setEnabled(true);
909 flag.setOnOff(buffer_.params().outputChanges);
910 break;
912 case LFUN_CHANGES_MERGE:
913 case LFUN_CHANGE_NEXT:
914 case LFUN_ALL_CHANGES_ACCEPT:
915 case LFUN_ALL_CHANGES_REJECT:
916 // TODO: context-sensitive enabling of LFUNs
917 // In principle, these command should only be enabled if there
918 // is a change in the document. However, without proper
919 // optimizations, this will inevitably result in poor performance.
920 flag.setEnabled(true);
921 break;
923 case LFUN_BUFFER_TOGGLE_COMPRESSION: {
924 flag.setOnOff(buffer_.params().compressed);
925 break;
928 case LFUN_SCREEN_UP:
929 case LFUN_SCREEN_DOWN:
930 case LFUN_SCROLL:
931 case LFUN_SCREEN_UP_SELECT:
932 case LFUN_SCREEN_DOWN_SELECT:
933 flag.setEnabled(true);
934 break;
936 case LFUN_LAYOUT_TABULAR:
937 flag.setEnabled(cur.innerInsetOfType(TABULAR_CODE));
938 break;
940 case LFUN_LAYOUT:
941 flag.setEnabled(!cur.inset().forcePlainLayout(cur.idx()));
942 break;
944 case LFUN_LAYOUT_PARAGRAPH:
945 flag.setEnabled(cur.inset().allowParagraphCustomization(cur.idx()));
946 break;
948 case LFUN_INSET_SETTINGS: {
949 InsetCode code = cur.inset().lyxCode();
950 if (cmd.getArg(0) == insetName(code)) {
951 flag.setEnabled(true);
952 break;
954 bool enable = false;
955 InsetCode next_code = cur.nextInset()
956 ? cur.nextInset()->lyxCode() : NO_CODE;
957 //FIXME: remove these special cases:
958 switch (next_code) {
959 case TABULAR_CODE:
960 case ERT_CODE:
961 case FLOAT_CODE:
962 case WRAP_CODE:
963 case NOTE_CODE:
964 case BRANCH_CODE:
965 case BOX_CODE:
966 case LISTINGS_CODE:
967 enable = (cmd.argument().empty() ||
968 cmd.getArg(0) == insetName(next_code));
969 break;
970 default:
971 break;
973 flag.setEnabled(enable);
974 break;
977 case LFUN_DIALOG_SHOW_NEW_INSET:
978 flag.setEnabled(cur.inset().lyxCode() != ERT_CODE &&
979 cur.inset().lyxCode() != LISTINGS_CODE);
980 if (cur.inset().lyxCode() == CAPTION_CODE) {
981 FuncStatus flag;
982 if (cur.inset().getStatus(cur, cmd, flag))
983 return flag;
985 break;
987 case LFUN_BRANCH_ACTIVATE:
988 case LFUN_BRANCH_DEACTIVATE: {
989 bool enable = false;
990 docstring const branchName = cmd.argument();
991 if (!branchName.empty())
992 enable = buffer_.params().branchlist().find(branchName);
993 flag.setEnabled(enable);
994 break;
997 default:
998 flag.setEnabled(false);
1001 return flag;
1005 bool BufferView::dispatch(FuncRequest const & cmd)
1007 //lyxerr << [ cmd = " << cmd << "]" << endl;
1009 // Make sure that the cached BufferView is correct.
1010 LYXERR(Debug::ACTION, " action[" << cmd.action << ']'
1011 << " arg[" << to_utf8(cmd.argument()) << ']'
1012 << " x[" << cmd.x << ']'
1013 << " y[" << cmd.y << ']'
1014 << " button[" << cmd.button() << ']');
1016 Cursor & cur = d->cursor_;
1018 switch (cmd.action) {
1020 case LFUN_UNDO:
1021 cur.message(_("Undo"));
1022 cur.clearSelection();
1023 if (!cur.textUndo())
1024 cur.message(_("No further undo information"));
1025 else
1026 processUpdateFlags(Update::Force | Update::FitCursor);
1027 break;
1029 case LFUN_REDO:
1030 cur.message(_("Redo"));
1031 cur.clearSelection();
1032 if (!cur.textRedo())
1033 cur.message(_("No further redo information"));
1034 else
1035 processUpdateFlags(Update::Force | Update::FitCursor);
1036 break;
1038 case LFUN_FONT_STATE:
1039 cur.message(cur.currentState());
1040 break;
1042 case LFUN_BOOKMARK_SAVE:
1043 saveBookmark(convert<unsigned int>(to_utf8(cmd.argument())));
1044 break;
1046 case LFUN_LABEL_GOTO: {
1047 docstring label = cmd.argument();
1048 if (label.empty()) {
1049 InsetRef * inset =
1050 getInsetByCode<InsetRef>(d->cursor_,
1051 REF_CODE);
1052 if (inset) {
1053 label = inset->getParam("reference");
1054 // persistent=false: use temp_bookmark
1055 saveBookmark(0);
1059 if (!label.empty())
1060 gotoLabel(label);
1061 break;
1064 case LFUN_INSET_EDIT: {
1065 FuncRequest fr(cmd);
1066 // if there is an inset at cursor, see whether it
1067 // can be modified.
1068 Inset * inset = cur.nextInset();
1069 if (inset)
1070 inset->dispatch(cur, fr);
1071 // if it did not work, try the underlying inset.
1072 if (!inset || !cur.result().dispatched())
1073 cur.dispatch(cmd);
1075 // FIXME I'm adding the last break to solve a crash,
1076 // but that is obviously not right.
1077 if (!cur.result().dispatched())
1078 // It did not work too; no action needed.
1079 break;
1080 break;
1083 case LFUN_PARAGRAPH_GOTO: {
1084 int const id = convert<int>(cmd.getArg(0));
1085 int const pos = convert<int>(cmd.getArg(1));
1086 int i = 0;
1087 for (Buffer * b = &buffer_; i == 0 || b != &buffer_;
1088 b = theBufferList().next(b)) {
1090 DocIterator dit = b->getParFromID(id);
1091 if (dit.atEnd()) {
1092 LYXERR(Debug::INFO, "No matching paragraph found! [" << id << "].");
1093 ++i;
1094 continue;
1096 LYXERR(Debug::INFO, "Paragraph " << dit.paragraph().id()
1097 << " found in buffer `"
1098 << b->absFileName() << "'.");
1100 if (b == &buffer_) {
1101 // Set the cursor
1102 dit.pos() = pos;
1103 setCursor(dit);
1104 processUpdateFlags(Update::Force | Update::FitCursor);
1105 } else {
1106 // Switch to other buffer view and resend cmd
1107 theLyXFunc().dispatch(FuncRequest(
1108 LFUN_BUFFER_SWITCH, b->absFileName()));
1109 theLyXFunc().dispatch(cmd);
1111 break;
1113 break;
1116 case LFUN_NOTE_NEXT:
1117 gotoInset(this, NOTE_CODE, false);
1118 break;
1120 case LFUN_REFERENCE_NEXT: {
1121 vector<InsetCode> tmp;
1122 tmp.push_back(LABEL_CODE);
1123 tmp.push_back(REF_CODE);
1124 gotoInset(this, tmp, true);
1125 break;
1128 case LFUN_CHANGES_TRACK:
1129 buffer_.params().trackChanges = !buffer_.params().trackChanges;
1130 break;
1132 case LFUN_CHANGES_OUTPUT:
1133 buffer_.params().outputChanges = !buffer_.params().outputChanges;
1134 if (buffer_.params().outputChanges) {
1135 bool dvipost = LaTeXFeatures::isAvailable("dvipost");
1136 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
1137 LaTeXFeatures::isAvailable("xcolor");
1139 if (!dvipost && !xcolorsoul) {
1140 Alert::warning(_("Changes not shown in LaTeX output"),
1141 _("Changes will not be highlighted in LaTeX output, "
1142 "because neither dvipost nor xcolor/soul are installed.\n"
1143 "Please install these packages or redefine "
1144 "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
1145 } else if (!xcolorsoul) {
1146 Alert::warning(_("Changes not shown in LaTeX output"),
1147 _("Changes will not be highlighted in LaTeX output "
1148 "when using pdflatex, because xcolor and soul are not installed.\n"
1149 "Please install both packages or redefine "
1150 "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
1153 break;
1155 case LFUN_CHANGE_NEXT:
1156 findNextChange(this);
1157 break;
1159 case LFUN_CHANGES_MERGE:
1160 if (findNextChange(this))
1161 showDialog("changes");
1162 break;
1164 case LFUN_ALL_CHANGES_ACCEPT:
1165 // select complete document
1166 d->cursor_.reset(buffer_.inset());
1167 d->cursor_.selHandle(true);
1168 buffer_.text().cursorBottom(d->cursor_);
1169 // accept everything in a single step to support atomic undo
1170 buffer_.text().acceptOrRejectChanges(d->cursor_, Text::ACCEPT);
1171 // FIXME: Move this LFUN to Buffer so that we don't have to do this:
1172 processUpdateFlags(Update::Force | Update::FitCursor);
1173 break;
1175 case LFUN_ALL_CHANGES_REJECT:
1176 // select complete document
1177 d->cursor_.reset(buffer_.inset());
1178 d->cursor_.selHandle(true);
1179 buffer_.text().cursorBottom(d->cursor_);
1180 // reject everything in a single step to support atomic undo
1181 // Note: reject does not work recursively; the user may have to repeat the operation
1182 buffer_.text().acceptOrRejectChanges(d->cursor_, Text::REJECT);
1183 // FIXME: Move this LFUN to Buffer so that we don't have to do this:
1184 processUpdateFlags(Update::Force | Update::FitCursor);
1185 break;
1187 case LFUN_WORD_FIND: {
1188 FuncRequest req = cmd;
1189 if (cmd.argument().empty() && !d->search_request_cache_.argument().empty())
1190 req = d->search_request_cache_;
1191 if (req.argument().empty()) {
1192 theLyXFunc().dispatch(FuncRequest(LFUN_DIALOG_SHOW, "findreplace"));
1193 break;
1195 if (find(this, req))
1196 showCursor();
1197 else
1198 message(_("String not found!"));
1199 d->search_request_cache_ = req;
1200 break;
1203 case LFUN_WORD_REPLACE: {
1204 bool has_deleted = false;
1205 if (cur.selection()) {
1206 DocIterator beg = cur.selectionBegin();
1207 DocIterator end = cur.selectionEnd();
1208 if (beg.pit() == end.pit()) {
1209 for (pos_type p = beg.pos() ; p < end.pos() ; ++p) {
1210 if (cur.paragraph().isDeleted(p))
1211 has_deleted = true;
1215 replace(this, cmd, has_deleted);
1216 break;
1219 case LFUN_MARK_OFF:
1220 cur.clearSelection();
1221 cur.resetAnchor();
1222 cur.message(from_utf8(N_("Mark off")));
1223 break;
1225 case LFUN_MARK_ON:
1226 cur.clearSelection();
1227 cur.setMark(true);
1228 cur.resetAnchor();
1229 cur.message(from_utf8(N_("Mark on")));
1230 break;
1232 case LFUN_MARK_TOGGLE:
1233 cur.clearSelection();
1234 if (cur.mark()) {
1235 cur.setMark(false);
1236 cur.message(from_utf8(N_("Mark removed")));
1237 } else {
1238 cur.setMark(true);
1239 cur.message(from_utf8(N_("Mark set")));
1241 cur.resetAnchor();
1242 break;
1244 case LFUN_SCREEN_RECENTER:
1245 showCursor();
1246 break;
1248 case LFUN_BIBTEX_DATABASE_ADD: {
1249 Cursor tmpcur = d->cursor_;
1250 findInset(tmpcur, BIBTEX_CODE, false);
1251 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
1252 BIBTEX_CODE);
1253 if (inset) {
1254 if (inset->addDatabase(cmd.argument()))
1255 buffer_.updateBibfilesCache();
1257 break;
1260 case LFUN_BIBTEX_DATABASE_DEL: {
1261 Cursor tmpcur = d->cursor_;
1262 findInset(tmpcur, BIBTEX_CODE, false);
1263 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
1264 BIBTEX_CODE);
1265 if (inset) {
1266 if (inset->delDatabase(cmd.argument()))
1267 buffer_.updateBibfilesCache();
1269 break;
1272 case LFUN_STATISTICS: {
1273 DocIterator from, to;
1274 if (cur.selection()) {
1275 from = cur.selectionBegin();
1276 to = cur.selectionEnd();
1277 } else {
1278 from = doc_iterator_begin(buffer_.inset());
1279 to = doc_iterator_end(buffer_.inset());
1281 int const words = countWords(from, to);
1282 int const chars = countChars(from, to, false);
1283 int const chars_blanks = countChars(from, to, true);
1284 docstring message;
1285 if (cur.selection())
1286 message = _("Statistics for the selection:");
1287 else
1288 message = _("Statistics for the document:");
1289 message += "\n\n";
1290 if (words != 1)
1291 message += bformat(_("%1$d words"), words);
1292 else
1293 message += _("One word");
1294 message += "\n";
1295 if (chars_blanks != 1)
1296 message += bformat(_("%1$d characters (including blanks)"),
1297 chars_blanks);
1298 else
1299 message += _("One character (including blanks)");
1300 message += "\n";
1301 if (chars != 1)
1302 message += bformat(_("%1$d characters (excluding blanks)"),
1303 chars);
1304 else
1305 message += _("One character (excluding blanks)");
1307 Alert::information(_("Statistics"), message);
1309 break;
1311 case LFUN_BUFFER_TOGGLE_COMPRESSION:
1312 // turn compression on/off
1313 buffer_.params().compressed = !buffer_.params().compressed;
1314 break;
1316 case LFUN_NEXT_INSET_TOGGLE: {
1317 // create the the real function we want to invoke
1318 FuncRequest tmpcmd = cmd;
1319 tmpcmd.action = LFUN_INSET_TOGGLE;
1320 // if there is an inset at cursor, see whether it
1321 // wants to toggle.
1322 Inset * inset = cur.nextInset();
1323 if (inset) {
1324 if (inset->isActive()) {
1325 Cursor tmpcur = cur;
1326 tmpcur.pushBackward(*inset);
1327 inset->dispatch(tmpcur, tmpcmd);
1328 if (tmpcur.result().dispatched())
1329 cur.dispatched();
1330 } else
1331 inset->dispatch(cur, tmpcmd);
1333 // if it did not work, try the underlying inset.
1334 if (!inset || !cur.result().dispatched())
1335 cur.dispatch(tmpcmd);
1337 if (!cur.result().dispatched())
1338 // It did not work too; no action needed.
1339 break;
1340 cur.clearSelection();
1341 processUpdateFlags(Update::SinglePar | Update::FitCursor);
1342 break;
1345 case LFUN_NEXT_INSET_MODIFY: {
1346 // create the the real function we want to invoke
1347 FuncRequest tmpcmd = cmd;
1348 tmpcmd.action = LFUN_INSET_MODIFY;
1349 // if there is an inset at cursor, see whether it
1350 // can be modified.
1351 Inset * inset = cur.nextInset();
1352 if (inset)
1353 inset->dispatch(cur, tmpcmd);
1354 // if it did not work, try the underlying inset.
1355 if (!inset || !cur.result().dispatched())
1356 cur.dispatch(tmpcmd);
1358 if (!cur.result().dispatched())
1359 // It did not work too; no action needed.
1360 break;
1361 cur.clearSelection();
1362 processUpdateFlags(Update::Force | Update::FitCursor);
1363 break;
1366 case LFUN_SCREEN_UP:
1367 case LFUN_SCREEN_DOWN: {
1368 Point p = getPos(cur, cur.boundary());
1369 if (p.y_ < 0 || p.y_ > height_) {
1370 // The cursor is off-screen so recenter before proceeding.
1371 showCursor();
1372 p = getPos(cur, cur.boundary());
1374 int const scrolled = scroll(cmd.action == LFUN_SCREEN_UP
1375 ? - height_ : height_);
1376 if (cmd.action == LFUN_SCREEN_UP && scrolled > - height_)
1377 p = Point(0, 0);
1378 if (cmd.action == LFUN_SCREEN_DOWN && scrolled < height_)
1379 p = Point(width_, height_);
1380 cur.reset(buffer_.inset());
1381 updateMetrics();
1382 buffer_.changed();
1383 d->text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_);
1384 //FIXME: what to do with cur.x_target()?
1385 cur.finishUndo();
1386 break;
1389 case LFUN_SCROLL:
1390 lfunScroll(cmd);
1391 break;
1393 case LFUN_SCREEN_UP_SELECT: {
1394 cur.selHandle(true);
1395 if (isTopScreen()) {
1396 lyx::dispatch(FuncRequest(LFUN_BUFFER_BEGIN_SELECT));
1397 cur.finishUndo();
1398 break;
1400 int y = getPos(cur, cur.boundary()).y_;
1401 int const ymin = y - height_ + defaultRowHeight();
1402 while (y > ymin && cur.up())
1403 y = getPos(cur, cur.boundary()).y_;
1405 cur.finishUndo();
1406 processUpdateFlags(Update::SinglePar | Update::FitCursor);
1407 break;
1410 case LFUN_SCREEN_DOWN_SELECT: {
1411 cur.selHandle(true);
1412 if (isBottomScreen()) {
1413 lyx::dispatch(FuncRequest(LFUN_BUFFER_END_SELECT));
1414 cur.finishUndo();
1415 break;
1417 int y = getPos(cur, cur.boundary()).y_;
1418 int const ymax = y + height_ - defaultRowHeight();
1419 while (y < ymax && cur.down())
1420 y = getPos(cur, cur.boundary()).y_;
1422 cur.finishUndo();
1423 processUpdateFlags(Update::SinglePar | Update::FitCursor);
1424 break;
1427 case LFUN_BRANCH_ACTIVATE:
1428 case LFUN_BRANCH_DEACTIVATE:
1429 buffer_.dispatch(cmd);
1430 processUpdateFlags(Update::Force);
1431 break;
1433 // This could be rewriten using some command like forall <insetname> <command>
1434 // once the insets refactoring is done.
1435 case LFUN_NOTES_MUTATE: {
1436 if (cmd.argument().empty())
1437 break;
1439 if (mutateNotes(cur, cmd.getArg(0), cmd.getArg(1))) {
1440 processUpdateFlags(Update::Force);
1442 break;
1445 case LFUN_ALL_INSETS_TOGGLE: {
1446 string action;
1447 string const name = split(to_utf8(cmd.argument()), action, ' ');
1448 InsetCode const inset_code = insetCode(name);
1450 FuncRequest fr(LFUN_INSET_TOGGLE, action);
1452 Inset & inset = cur.buffer().inset();
1453 InsetIterator it = inset_iterator_begin(inset);
1454 InsetIterator const end = inset_iterator_end(inset);
1455 for (; it != end; ++it) {
1456 if (it->asInsetCollapsable()
1457 && (inset_code == NO_CODE
1458 || inset_code == it->lyxCode())) {
1459 Cursor tmpcur = cur;
1460 tmpcur.pushBackward(*it);
1461 it->dispatch(tmpcur, fr);
1464 processUpdateFlags(Update::Force | Update::FitCursor);
1465 break;
1468 default:
1469 return false;
1472 return true;
1476 docstring const BufferView::requestSelection()
1478 Cursor & cur = d->cursor_;
1480 LYXERR(Debug::SELECTION, "requestSelection: cur.selection: " << cur.selection());
1481 if (!cur.selection()) {
1482 d->xsel_cache_.set = false;
1483 return docstring();
1486 LYXERR(Debug::SELECTION, "requestSelection: xsel_cache.set: " << d->xsel_cache_.set);
1487 if (!d->xsel_cache_.set ||
1488 cur.top() != d->xsel_cache_.cursor ||
1489 cur.anchor_.top() != d->xsel_cache_.anchor)
1491 d->xsel_cache_.cursor = cur.top();
1492 d->xsel_cache_.anchor = cur.anchor_.top();
1493 d->xsel_cache_.set = cur.selection();
1494 return cur.selectionAsString(false);
1496 return docstring();
1500 void BufferView::clearSelection()
1502 d->cursor_.clearSelection();
1503 // Clear the selection buffer. Otherwise a subsequent
1504 // middle-mouse-button paste would use the selection buffer,
1505 // not the more current external selection.
1506 cap::clearSelection();
1507 d->xsel_cache_.set = false;
1508 // The buffer did not really change, but this causes the
1509 // redraw we need because we cleared the selection above.
1510 buffer_.changed();
1514 void BufferView::resize(int width, int height)
1516 // Update from work area
1517 width_ = width;
1518 height_ = height;
1520 // Clear the paragraph height cache.
1521 d->par_height_.clear();
1522 // Redo the metrics.
1523 updateMetrics();
1527 Inset const * BufferView::getCoveringInset(Text const & text,
1528 int x, int y) const
1530 TextMetrics & tm = d->text_metrics_[&text];
1531 Inset * inset = tm.checkInsetHit(x, y);
1532 if (!inset)
1533 return 0;
1535 if (!inset->descendable())
1536 // No need to go further down if the inset is not
1537 // descendable.
1538 return inset;
1540 size_t cell_number = inset->nargs();
1541 // Check all the inner cell.
1542 for (size_t i = 0; i != cell_number; ++i) {
1543 Text const * inner_text = inset->getText(i);
1544 if (inner_text) {
1545 // Try deeper.
1546 Inset const * inset_deeper =
1547 getCoveringInset(*inner_text, x, y);
1548 if (inset_deeper)
1549 return inset_deeper;
1553 return inset;
1557 void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
1559 //lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
1561 // This is only called for mouse related events including
1562 // LFUN_FILE_OPEN generated by drag-and-drop.
1563 FuncRequest cmd = cmd0;
1565 Cursor old = cursor();
1566 Cursor cur(*this);
1567 cur.push(buffer_.inset());
1568 cur.setSelection(d->cursor_.selection());
1570 // Either the inset under the cursor or the
1571 // surrounding Text will handle this event.
1573 // make sure we stay within the screen...
1574 cmd.y = min(max(cmd.y, -1), height_);
1576 if (cmd.action == LFUN_MOUSE_MOTION && cmd.button() == mouse_button::none) {
1578 // Get inset under mouse, if there is one.
1579 Inset const * covering_inset =
1580 getCoveringInset(buffer_.text(), cmd.x, cmd.y);
1581 if (covering_inset == d->last_inset_)
1582 // Same inset, no need to do anything...
1583 return;
1585 bool need_redraw = false;
1586 // const_cast because of setMouseHover().
1587 Inset * inset = const_cast<Inset *>(covering_inset);
1588 if (d->last_inset_)
1589 // Remove the hint on the last hovered inset (if any).
1590 need_redraw |= d->last_inset_->setMouseHover(false);
1591 if (inset)
1592 // Highlighted the newly hovered inset (if any).
1593 need_redraw |= inset->setMouseHover(true);
1594 d->last_inset_ = inset;
1595 if (!need_redraw)
1596 return;
1598 LYXERR(Debug::PAINTING, "Mouse hover detected at: ("
1599 << cmd.x << ", " << cmd.y << ")");
1601 d->update_strategy_ = DecorationUpdate;
1603 // This event (moving without mouse click) is not passed further.
1604 // This should be changed if it is further utilized.
1605 buffer_.changed();
1606 return;
1609 // Build temporary cursor.
1610 Inset * inset = d->text_metrics_[&buffer_.text()].editXY(cur, cmd.x, cmd.y);
1612 // Put anchor at the same position.
1613 cur.resetAnchor();
1615 cur.beginUndoGroup();
1617 // Try to dispatch to an non-editable inset near this position
1618 // via the temp cursor. If the inset wishes to change the real
1619 // cursor it has to do so explicitly by using
1620 // cur.bv().cursor() = cur; (or similar)
1621 if (inset)
1622 inset->dispatch(cur, cmd);
1624 // Now dispatch to the temporary cursor. If the real cursor should
1625 // be modified, the inset's dispatch has to do so explicitly.
1626 if (!inset || !cur.result().dispatched())
1627 cur.dispatch(cmd);
1629 cur.endUndoGroup();
1631 // Notify left insets
1632 if (cur != old) {
1633 old.fixIfBroken();
1634 bool badcursor = notifyCursorLeaves(old, cur);
1635 if (badcursor)
1636 cursor().fixIfBroken();
1639 // Do we have a selection?
1640 theSelection().haveSelection(cursor().selection());
1642 // If the command has been dispatched,
1643 if (cur.result().dispatched() || cur.result().update())
1644 processUpdateFlags(cur.result().update());
1648 void BufferView::lfunScroll(FuncRequest const & cmd)
1650 string const scroll_type = cmd.getArg(0);
1651 int const scroll_step =
1652 (scroll_type == "line") ? d->scrollbarParameters_.single_step
1653 : (scroll_type == "page") ? d->scrollbarParameters_.page_step : 0;
1654 if (scroll_step == 0)
1655 return;
1656 string const scroll_quantity = cmd.getArg(1);
1657 if (scroll_quantity == "up")
1658 scrollUp(scroll_step);
1659 else if (scroll_quantity == "down")
1660 scrollDown(scroll_step);
1661 else {
1662 int const scroll_value = convert<int>(scroll_quantity);
1663 if (scroll_value)
1664 scroll(scroll_step * scroll_value);
1666 updateMetrics();
1667 buffer_.changed();
1671 int BufferView::scroll(int y)
1673 if (y > 0)
1674 return scrollDown(y);
1675 if (y < 0)
1676 return scrollUp(-y);
1677 return 0;
1681 int BufferView::scrollDown(int offset)
1683 Text * text = &buffer_.text();
1684 TextMetrics & tm = d->text_metrics_[text];
1685 int ymax = height_ + offset;
1686 while (true) {
1687 pair<pit_type, ParagraphMetrics const *> last = tm.last();
1688 int bottom_pos = last.second->position() + last.second->descent();
1689 if (last.first + 1 == int(text->paragraphs().size())) {
1690 if (bottom_pos <= height_)
1691 return 0;
1692 offset = min(offset, bottom_pos - height_);
1693 break;
1695 if (bottom_pos > ymax)
1696 break;
1697 tm.newParMetricsDown();
1699 d->anchor_ypos_ -= offset;
1700 return -offset;
1704 int BufferView::scrollUp(int offset)
1706 Text * text = &buffer_.text();
1707 TextMetrics & tm = d->text_metrics_[text];
1708 int ymin = - offset;
1709 while (true) {
1710 pair<pit_type, ParagraphMetrics const *> first = tm.first();
1711 int top_pos = first.second->position() - first.second->ascent();
1712 if (first.first == 0) {
1713 if (top_pos >= 0)
1714 return 0;
1715 offset = min(offset, - top_pos);
1716 break;
1718 if (top_pos < ymin)
1719 break;
1720 tm.newParMetricsUp();
1722 d->anchor_ypos_ += offset;
1723 return offset;
1727 void BufferView::setCursorFromRow(int row)
1729 int tmpid = -1;
1730 int tmppos = -1;
1732 buffer_.texrow().getIdFromRow(row, tmpid, tmppos);
1734 d->cursor_.reset(buffer_.inset());
1735 if (tmpid == -1)
1736 buffer_.text().setCursor(d->cursor_, 0, 0);
1737 else
1738 buffer_.text().setCursor(d->cursor_, buffer_.getParFromID(tmpid).pit(), tmppos);
1742 bool BufferView::setCursorFromInset(Inset const * inset)
1744 // are we already there?
1745 if (cursor().nextInset() == inset)
1746 return true;
1748 // Inset is not at cursor position. Find it in the document.
1749 Cursor cur(*this);
1750 cur.reset(buffer().inset());
1751 while (cur && cur.nextInset() != inset)
1752 cur.forwardInset();
1754 if (cur) {
1755 setCursor(cur);
1756 return true;
1758 return false;
1762 void BufferView::gotoLabel(docstring const & label)
1764 Toc & toc = buffer().tocBackend().toc("label");
1765 TocIterator toc_it = toc.begin();
1766 TocIterator end = toc.end();
1767 for (; toc_it != end; ++toc_it) {
1768 if (label == toc_it->str())
1769 dispatch(toc_it->action());
1771 //FIXME: We could do a bit more searching thanks to this:
1772 //InsetLabel const * inset = buffer_.insetLabel(label);
1776 TextMetrics const & BufferView::textMetrics(Text const * t) const
1778 return const_cast<BufferView *>(this)->textMetrics(t);
1782 TextMetrics & BufferView::textMetrics(Text const * t)
1784 TextMetricsCache::iterator tmc_it = d->text_metrics_.find(t);
1785 if (tmc_it == d->text_metrics_.end()) {
1786 tmc_it = d->text_metrics_.insert(
1787 make_pair(t, TextMetrics(this, const_cast<Text *>(t)))).first;
1789 return tmc_it->second;
1793 ParagraphMetrics const & BufferView::parMetrics(Text const * t,
1794 pit_type pit) const
1796 return textMetrics(t).parMetrics(pit);
1800 int BufferView::workHeight() const
1802 return height_;
1806 void BufferView::setCursor(DocIterator const & dit)
1808 size_t const n = dit.depth();
1809 for (size_t i = 0; i < n; ++i)
1810 dit[i].inset().edit(d->cursor_, true);
1812 d->cursor_.setCursor(dit);
1813 d->cursor_.setSelection(false);
1817 bool BufferView::checkDepm(Cursor & cur, Cursor & old)
1819 // Would be wrong to delete anything if we have a selection.
1820 if (cur.selection())
1821 return false;
1823 bool need_anchor_change = false;
1824 bool changed = d->cursor_.text()->deleteEmptyParagraphMechanism(cur, old,
1825 need_anchor_change);
1827 if (need_anchor_change)
1828 cur.resetAnchor();
1830 if (!changed)
1831 return false;
1833 d->cursor_ = cur;
1835 updateLabels(buffer_);
1837 updateMetrics();
1838 buffer_.changed();
1839 return true;
1843 bool BufferView::mouseSetCursor(Cursor & cur, bool select)
1845 LASSERT(&cur.bv() == this, /**/);
1847 if (!select)
1848 // this event will clear selection so we save selection for
1849 // persistent selection
1850 cap::saveSelection(cursor());
1852 // Has the cursor just left the inset?
1853 bool badcursor = false;
1854 bool leftinset = (&d->cursor_.inset() != &cur.inset());
1855 if (leftinset) {
1856 d->cursor_.fixIfBroken();
1857 badcursor = notifyCursorLeaves(d->cursor_, cur);
1858 if (badcursor)
1859 cur.fixIfBroken();
1862 // FIXME: shift-mouse selection doesn't work well across insets.
1863 bool do_selection = select && &d->cursor_.anchor().inset() == &cur.inset();
1865 // do the dEPM magic if needed
1866 // FIXME: (1) move this to InsetText::notifyCursorLeaves?
1867 // FIXME: (2) if we had a working InsetText::notifyCursorLeaves,
1868 // the leftinset bool would not be necessary (badcursor instead).
1869 bool update = leftinset;
1870 if (!do_selection && !badcursor && d->cursor_.inTexted())
1871 update |= checkDepm(cur, d->cursor_);
1873 d->cursor_.setCursor(cur);
1874 d->cursor_.boundary(cur.boundary());
1875 if (do_selection)
1876 d->cursor_.setSelection();
1877 else
1878 d->cursor_.clearSelection();
1880 d->cursor_.finishUndo();
1881 d->cursor_.setCurrentFont();
1882 return update;
1886 void BufferView::putSelectionAt(DocIterator const & cur,
1887 int length, bool backwards)
1889 d->cursor_.clearSelection();
1891 setCursor(cur);
1893 if (length) {
1894 if (backwards) {
1895 d->cursor_.pos() += length;
1896 d->cursor_.setSelection(d->cursor_, -length);
1897 } else
1898 d->cursor_.setSelection(d->cursor_, length);
1900 // Ensure a redraw happens in any case because the new selection could
1901 // possibly be on the same screen as the previous selection.
1902 processUpdateFlags(Update::Force | Update::FitCursor);
1906 Cursor & BufferView::cursor()
1908 return d->cursor_;
1912 Cursor const & BufferView::cursor() const
1914 return d->cursor_;
1918 pit_type BufferView::anchor_ref() const
1920 return d->anchor_pit_;
1924 bool BufferView::singleParUpdate()
1926 Text & buftext = buffer_.text();
1927 pit_type const bottom_pit = d->cursor_.bottom().pit();
1928 TextMetrics & tm = textMetrics(&buftext);
1929 int old_height = tm.parMetrics(bottom_pit).height();
1931 // make sure inline completion pointer is ok
1932 if (d->inlineCompletionPos_.fixIfBroken())
1933 d->inlineCompletionPos_ = DocIterator();
1935 // In Single Paragraph mode, rebreak only
1936 // the (main text, not inset!) paragraph containing the cursor.
1937 // (if this paragraph contains insets etc., rebreaking will
1938 // recursively descend)
1939 tm.redoParagraph(bottom_pit);
1940 ParagraphMetrics const & pm = tm.parMetrics(bottom_pit);
1941 if (pm.height() != old_height)
1942 // Paragraph height has changed so we cannot proceed to
1943 // the singlePar optimisation.
1944 return false;
1946 d->update_strategy_ = SingleParUpdate;
1948 LYXERR(Debug::PAINTING, "\ny1: " << pm.position() - pm.ascent()
1949 << " y2: " << pm.position() + pm.descent()
1950 << " pit: " << bottom_pit
1951 << " singlepar: 1");
1952 return true;
1956 void BufferView::updateMetrics()
1958 if (height_ == 0 || width_ == 0)
1959 return;
1961 Text & buftext = buffer_.text();
1962 pit_type const npit = int(buftext.paragraphs().size());
1964 // Clear out the position cache in case of full screen redraw,
1965 d->coord_cache_.clear();
1967 // Clear out paragraph metrics to avoid having invalid metrics
1968 // in the cache from paragraphs not relayouted below
1969 // The complete text metrics will be redone.
1970 d->text_metrics_.clear();
1972 TextMetrics & tm = textMetrics(&buftext);
1974 // make sure inline completion pointer is ok
1975 if (d->inlineCompletionPos_.fixIfBroken())
1976 d->inlineCompletionPos_ = DocIterator();
1978 if (d->anchor_pit_ >= npit)
1979 // The anchor pit must have been deleted...
1980 d->anchor_pit_ = npit - 1;
1982 // Rebreak anchor paragraph.
1983 tm.redoParagraph(d->anchor_pit_);
1984 ParagraphMetrics & anchor_pm = tm.par_metrics_[d->anchor_pit_];
1986 // position anchor
1987 if (d->anchor_pit_ == 0) {
1988 int scrollRange = d->scrollbarParameters_.max - d->scrollbarParameters_.min;
1990 // Complete buffer visible? Then it's easy.
1991 if (scrollRange == 0)
1992 d->anchor_ypos_ = anchor_pm.ascent();
1994 // FIXME: Some clever handling needed to show
1995 // the _first_ paragraph up to the top if the cursor is
1996 // in the first line.
1998 anchor_pm.setPosition(d->anchor_ypos_);
2000 LYXERR(Debug::PAINTING, "metrics: "
2001 << " anchor pit = " << d->anchor_pit_
2002 << " anchor ypos = " << d->anchor_ypos_);
2004 // Redo paragraphs above anchor if necessary.
2005 int y1 = d->anchor_ypos_ - anchor_pm.ascent();
2006 // We are now just above the anchor paragraph.
2007 pit_type pit1 = d->anchor_pit_ - 1;
2008 for (; pit1 >= 0 && y1 >= 0; --pit1) {
2009 tm.redoParagraph(pit1);
2010 ParagraphMetrics & pm = tm.par_metrics_[pit1];
2011 y1 -= pm.descent();
2012 // Save the paragraph position in the cache.
2013 pm.setPosition(y1);
2014 y1 -= pm.ascent();
2017 // Redo paragraphs below the anchor if necessary.
2018 int y2 = d->anchor_ypos_ + anchor_pm.descent();
2019 // We are now just below the anchor paragraph.
2020 pit_type pit2 = d->anchor_pit_ + 1;
2021 for (; pit2 < npit && y2 <= height_; ++pit2) {
2022 tm.redoParagraph(pit2);
2023 ParagraphMetrics & pm = tm.par_metrics_[pit2];
2024 y2 += pm.ascent();
2025 // Save the paragraph position in the cache.
2026 pm.setPosition(y2);
2027 y2 += pm.descent();
2030 LYXERR(Debug::PAINTING, "Metrics: "
2031 << " anchor pit = " << d->anchor_pit_
2032 << " anchor ypos = " << d->anchor_ypos_
2033 << " y1 = " << y1
2034 << " y2 = " << y2
2035 << " pit1 = " << pit1
2036 << " pit2 = " << pit2);
2038 d->update_strategy_ = FullScreenUpdate;
2040 if (lyxerr.debugging(Debug::WORKAREA)) {
2041 LYXERR(Debug::WORKAREA, "BufferView::updateMetrics");
2042 d->coord_cache_.dump();
2047 void BufferView::insertLyXFile(FileName const & fname)
2049 LASSERT(d->cursor_.inTexted(), /**/);
2051 // Get absolute path of file and add ".lyx"
2052 // to the filename if necessary
2053 FileName filename = fileSearch(string(), fname.absFilename(), "lyx");
2055 docstring const disp_fn = makeDisplayPath(filename.absFilename());
2056 // emit message signal.
2057 message(bformat(_("Inserting document %1$s..."), disp_fn));
2059 docstring res;
2060 Buffer buf("", false);
2061 if (buf.loadLyXFile(filename)) {
2062 ErrorList & el = buffer_.errorList("Parse");
2063 // Copy the inserted document error list into the current buffer one.
2064 el = buf.errorList("Parse");
2065 buffer_.undo().recordUndo(d->cursor_);
2066 cap::pasteParagraphList(d->cursor_, buf.paragraphs(),
2067 buf.params().documentClassPtr(), el);
2068 res = _("Document %1$s inserted.");
2069 } else {
2070 res = _("Could not insert document %1$s");
2073 updateMetrics();
2074 buffer_.changed();
2075 // emit message signal.
2076 message(bformat(res, disp_fn));
2077 buffer_.errors("Parse");
2081 Point BufferView::coordOffset(DocIterator const & dit, bool boundary) const
2083 int x = 0;
2084 int y = 0;
2085 int lastw = 0;
2087 // Addup contribution of nested insets, from inside to outside,
2088 // keeping the outer paragraph for a special handling below
2089 for (size_t i = dit.depth() - 1; i >= 1; --i) {
2090 CursorSlice const & sl = dit[i];
2091 int xx = 0;
2092 int yy = 0;
2094 // get relative position inside sl.inset()
2095 sl.inset().cursorPos(*this, sl, boundary && (i + 1 == dit.depth()), xx, yy);
2097 // Make relative position inside of the edited inset relative to sl.inset()
2098 x += xx;
2099 y += yy;
2101 // In case of an RTL inset, the edited inset will be positioned to the left
2102 // of xx:yy
2103 if (sl.text()) {
2104 bool boundary_i = boundary && i + 1 == dit.depth();
2105 bool rtl = textMetrics(sl.text()).isRTL(sl, boundary_i);
2106 if (rtl)
2107 x -= lastw;
2110 // remember width for the case that sl.inset() is positioned in an RTL inset
2111 if (i && dit[i - 1].text()) {
2112 // If this Inset is inside a Text Inset, retrieve the Dimension
2113 // from the containing text instead of using Inset::dimension() which
2114 // might not be implemented.
2115 // FIXME (Abdel 23/09/2007): this is a bit messy because of the
2116 // elimination of Inset::dim_ cache. This coordOffset() method needs
2117 // to be rewritten in light of the new design.
2118 Dimension const & dim = parMetrics(dit[i - 1].text(),
2119 dit[i - 1].pit()).insetDimension(&sl.inset());
2120 lastw = dim.wid;
2121 } else {
2122 Dimension const dim = sl.inset().dimension(*this);
2123 lastw = dim.wid;
2126 //lyxerr << "Cursor::getPos, i: "
2127 // << i << " x: " << xx << " y: " << y << endl;
2130 // Add contribution of initial rows of outermost paragraph
2131 CursorSlice const & sl = dit[0];
2132 TextMetrics const & tm = textMetrics(sl.text());
2133 ParagraphMetrics const & pm = tm.parMetrics(sl.pit());
2134 LASSERT(!pm.rows().empty(), /**/);
2135 y -= pm.rows()[0].ascent();
2136 #if 1
2137 // FIXME: document this mess
2138 size_t rend;
2139 if (sl.pos() > 0 && dit.depth() == 1) {
2140 int pos = sl.pos();
2141 if (pos && boundary)
2142 --pos;
2143 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << endl;
2144 rend = pm.pos2row(pos);
2145 } else
2146 rend = pm.pos2row(sl.pos());
2147 #else
2148 size_t rend = pm.pos2row(sl.pos());
2149 #endif
2150 for (size_t rit = 0; rit != rend; ++rit)
2151 y += pm.rows()[rit].height();
2152 y += pm.rows()[rend].ascent();
2154 TextMetrics const & bottom_tm = textMetrics(dit.bottom().text());
2156 // Make relative position from the nested inset now bufferview absolute.
2157 int xx = bottom_tm.cursorX(dit.bottom(), boundary && dit.depth() == 1);
2158 x += xx;
2160 // In the RTL case place the nested inset at the left of the cursor in
2161 // the outer paragraph
2162 bool boundary_1 = boundary && 1 == dit.depth();
2163 bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
2164 if (rtl)
2165 x -= lastw;
2167 return Point(x, y);
2171 Point BufferView::getPos(DocIterator const & dit, bool boundary) const
2173 CursorSlice const & bot = dit.bottom();
2174 TextMetrics const & tm = textMetrics(bot.text());
2175 if (!tm.contains(bot.pit()))
2176 return Point(-1, -1);
2178 Point p = coordOffset(dit, boundary); // offset from outer paragraph
2179 p.y_ += tm.parMetrics(bot.pit()).position();
2180 return p;
2184 void BufferView::draw(frontend::Painter & pain)
2186 if (height_ == 0 || width_ == 0)
2187 return;
2188 LYXERR(Debug::PAINTING, "\t\t*** START DRAWING ***");
2189 Text & text = buffer_.text();
2190 TextMetrics const & tm = d->text_metrics_[&text];
2191 int const y = tm.first().second->position();
2192 PainterInfo pi(this, pain);
2194 switch (d->update_strategy_) {
2196 case NoScreenUpdate:
2197 // If no screen painting is actually needed, only some the different
2198 // coordinates of insets and paragraphs needs to be updated.
2199 pi.full_repaint = true;
2200 pi.pain.setDrawingEnabled(false);
2201 tm.draw(pi, 0, y);
2202 break;
2204 case SingleParUpdate:
2205 pi.full_repaint = false;
2206 // In general, only the current row of the outermost paragraph
2207 // will be redrawn. Particular cases where selection spans
2208 // multiple paragraph are correctly detected in TextMetrics.
2209 tm.draw(pi, 0, y);
2210 break;
2212 case DecorationUpdate:
2213 // FIXME: We should also distinguish DecorationUpdate to avoid text
2214 // drawing if possible. This is not possible to do easily right now
2215 // because of the single backing pixmap.
2217 case FullScreenUpdate:
2218 // The whole screen, including insets, will be refreshed.
2219 pi.full_repaint = true;
2221 // Clear background.
2222 pain.fillRectangle(0, 0, width_, height_,
2223 buffer_.inset().backgroundColor());
2225 // Draw everything.
2226 tm.draw(pi, 0, y);
2228 // and possibly grey out below
2229 pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
2230 int const y2 = lastpm.second->position() + lastpm.second->descent();
2231 if (y2 < height_)
2232 pain.fillRectangle(0, y2, width_, height_ - y2, Color_bottomarea);
2233 break;
2235 LYXERR(Debug::PAINTING, "\n\t\t*** END DRAWING ***");
2237 // The scrollbar needs an update.
2238 updateScrollbar();
2240 // Normalize anchor for next time
2241 pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
2242 pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
2243 for (pit_type pit = firstpm.first; pit <= lastpm.first; ++pit) {
2244 ParagraphMetrics const & pm = tm.parMetrics(pit);
2245 if (pm.position() + pm.descent() > 0) {
2246 d->anchor_pit_ = pit;
2247 d->anchor_ypos_ = pm.position();
2248 break;
2251 LYXERR(Debug::PAINTING, "Found new anchor pit = " << d->anchor_pit_
2252 << " anchor ypos = " << d->anchor_ypos_);
2256 void BufferView::message(docstring const & msg)
2258 if (d->gui_)
2259 d->gui_->message(msg);
2263 void BufferView::showDialog(string const & name)
2265 if (d->gui_)
2266 d->gui_->showDialog(name, string());
2270 void BufferView::showDialog(string const & name,
2271 string const & data, Inset * inset)
2273 if (d->gui_)
2274 d->gui_->showDialog(name, data, inset);
2278 void BufferView::updateDialog(string const & name, string const & data)
2280 if (d->gui_)
2281 d->gui_->updateDialog(name, data);
2285 void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
2287 d->gui_ = gui;
2291 // FIXME: Move this out of BufferView again
2292 docstring BufferView::contentsOfPlaintextFile(FileName const & fname)
2294 if (!fname.isReadableFile()) {
2295 docstring const error = from_ascii(strerror(errno));
2296 docstring const file = makeDisplayPath(fname.absFilename(), 50);
2297 docstring const text =
2298 bformat(_("Could not read the specified document\n"
2299 "%1$s\ndue to the error: %2$s"), file, error);
2300 Alert::error(_("Could not read file"), text);
2301 return docstring();
2304 if (!fname.isReadableFile()) {
2305 docstring const file = makeDisplayPath(fname.absFilename(), 50);
2306 docstring const text =
2307 bformat(_("%1$s\n is not readable."), file);
2308 Alert::error(_("Could not open file"), text);
2309 return docstring();
2312 // FIXME UNICODE: We don't know the encoding of the file
2313 docstring file_content = fname.fileContents("UTF-8");
2314 if (file_content.empty()) {
2315 Alert::error(_("Reading not UTF-8 encoded file"),
2316 _("The file is not UTF-8 encoded.\n"
2317 "It will be read as local 8Bit-encoded.\n"
2318 "If this does not give the correct result\n"
2319 "then please change the encoding of the file\n"
2320 "to UTF-8 with a program other than LyX.\n"));
2321 file_content = fname.fileContents("local8bit");
2324 return normalize_c(file_content);
2328 void BufferView::insertPlaintextFile(FileName const & f, bool asParagraph)
2330 docstring const tmpstr = contentsOfPlaintextFile(f);
2332 if (tmpstr.empty())
2333 return;
2335 Cursor & cur = cursor();
2336 cap::replaceSelection(cur);
2337 buffer_.undo().recordUndo(cur);
2338 if (asParagraph)
2339 cur.innerText()->insertStringAsParagraphs(cur, tmpstr);
2340 else
2341 cur.innerText()->insertStringAsLines(cur, tmpstr);
2343 updateMetrics();
2344 buffer_.changed();
2348 docstring const & BufferView::inlineCompletion() const
2350 return d->inlineCompletion_;
2354 size_t const & BufferView::inlineCompletionUniqueChars() const
2356 return d->inlineCompletionUniqueChars_;
2360 DocIterator const & BufferView::inlineCompletionPos() const
2362 return d->inlineCompletionPos_;
2366 bool samePar(DocIterator const & a, DocIterator const & b)
2368 if (a.empty() && b.empty())
2369 return true;
2370 if (a.empty() || b.empty())
2371 return false;
2372 return &a.innerParagraph() == &b.innerParagraph();
2376 void BufferView::setInlineCompletion(Cursor & cur, DocIterator const & pos,
2377 docstring const & completion, size_t uniqueChars)
2379 uniqueChars = min(completion.size(), uniqueChars);
2380 bool changed = d->inlineCompletion_ != completion
2381 || d->inlineCompletionUniqueChars_ != uniqueChars;
2382 bool singlePar = true;
2383 d->inlineCompletion_ = completion;
2384 d->inlineCompletionUniqueChars_ = min(completion.size(), uniqueChars);
2386 //lyxerr << "setInlineCompletion pos=" << pos << " completion=" << completion << " uniqueChars=" << uniqueChars << std::endl;
2388 // at new position?
2389 DocIterator const & old = d->inlineCompletionPos_;
2390 if (old != pos) {
2391 //lyxerr << "inlineCompletionPos changed" << std::endl;
2392 // old or pos are in another paragraph?
2393 if ((!samePar(cur, pos) && !pos.empty())
2394 || (!samePar(cur, old) && !old.empty())) {
2395 singlePar = false;
2396 //lyxerr << "different paragraph" << std::endl;
2398 d->inlineCompletionPos_ = pos;
2401 // set update flags
2402 if (changed) {
2403 if (singlePar && !(cur.disp_.update() & Update::Force))
2404 cur.updateFlags(cur.disp_.update() | Update::SinglePar);
2405 else
2406 cur.updateFlags(cur.disp_.update() | Update::Force);
2410 } // namespace lyx