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
10 * \author Jürgen Vigna
12 * Full author contact details are available in file CREDITS.
17 #include "BufferView.h"
20 #include "buffer_funcs.h"
21 #include "BufferList.h"
22 #include "BufferParams.h"
23 #include "CoordCache.h"
25 #include "CutAndPaste.h"
26 #include "DispatchResult.h"
27 #include "EmbeddedFiles.h"
28 #include "ErrorList.h"
30 #include "FloatList.h"
31 #include "FuncRequest.h"
32 #include "FuncStatus.h"
34 #include "InsetIterator.h"
36 #include "LaTeXFeatures.h"
42 #include "MetricsInfo.h"
43 #include "Paragraph.h"
44 #include "paragraph_funcs.h"
45 #include "ParagraphParameters.h"
46 #include "ParIterator.h"
49 #include "TextClass.h"
50 #include "TextMetrics.h"
53 #include "WordLangTuple.h"
55 #include "insets/InsetBibtex.h"
56 #include "insets/InsetCommand.h" // ChangeRefs
57 #include "insets/InsetRef.h"
58 #include "insets/InsetText.h"
60 #include "frontends/alert.h"
61 #include "frontends/Delegates.h"
62 #include "frontends/FontMetrics.h"
63 #include "frontends/Painter.h"
64 #include "frontends/Selection.h"
66 #include "graphics/Previews.h"
68 #include "support/convert.h"
69 #include "support/debug.h"
70 #include "support/FileFilterList.h"
71 #include "support/filetools.h"
72 #include "support/gettext.h"
73 #include "support/lstrings.h"
74 #include "support/Package.h"
75 #include "support/types.h"
84 using namespace lyx::support;
88 namespace Alert = frontend::Alert;
92 /// Return an inset of this class if it exists at the current cursor position
94 T * getInsetByCode(Cursor const & cur, InsetCode code)
97 Inset * inset = it.nextInset();
98 if (inset && inset->lyxCode() == code)
99 return static_cast<T*>(inset);
104 bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
107 bool findNextInset(DocIterator & dit, vector<InsetCode> const & codes,
108 docstring const & contents)
110 DocIterator tmpdit = dit;
113 Inset const * inset = tmpdit.nextInset();
115 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()
116 && (contents.empty() ||
117 static_cast<InsetCommand const *>(inset)->getFirstNonOptParam() == contents)) {
121 tmpdit.forwardInset();
128 /// Looks for next inset with one of the given codes.
129 bool findInset(DocIterator & dit, vector<InsetCode> const & codes,
133 DocIterator tmpdit = dit;
134 tmpdit.forwardInset();
139 Inset const * inset = tmpdit.nextInset();
141 && find(codes.begin(), codes.end(), inset->lyxCode()) != codes.end()) {
142 contents = static_cast<InsetCommand const *>(inset)->getFirstNonOptParam();
146 if (!findNextInset(tmpdit, codes, contents)) {
147 if (dit.depth() != 1 || dit.pit() != 0 || dit.pos() != 0) {
148 tmpdit = doc_iterator_begin(tmpdit.bottom().inset());
149 if (!findNextInset(tmpdit, codes, contents))
160 /// Looks for next inset with the given code
161 void findInset(DocIterator & dit, InsetCode code, bool same_content)
163 findInset(dit, vector<InsetCode>(1, code), same_content);
167 /// Moves cursor to the next inset with one of the given codes.
168 void gotoInset(BufferView * bv, vector<InsetCode> const & codes,
171 Cursor tmpcur = bv->cursor();
172 if (!findInset(tmpcur, codes, same_content)) {
173 bv->cursor().message(_("No more insets"));
177 tmpcur.clearSelection();
178 bv->setCursor(tmpcur);
182 /// Moves cursor to the next inset with given code.
183 void gotoInset(BufferView * bv, InsetCode code, bool same_content)
185 gotoInset(bv, vector<InsetCode>(1, code), same_content);
189 /// A map from a Text to the associated text metrics
190 typedef map<Text const *, TextMetrics> TextMetricsCache;
192 enum ScreenUpdateStrategy {
202 /////////////////////////////////////////////////////////////////////
206 /////////////////////////////////////////////////////////////////////
208 struct BufferView::Private
210 Private(BufferView & bv): wh_(0), cursor_(bv),
211 anchor_pit_(0), anchor_ypos_(0),
212 last_inset_(0), gui_(0)
216 ScrollbarParameters scrollbarParameters_;
218 ScreenUpdateStrategy update_strategy_;
220 CoordCache coord_cache_;
222 /// Estimated average par height for scrollbar.
224 /// this is used to handle XSelection events in the right manner.
233 pit_type anchor_pit_;
237 vector<int> par_height_;
239 /// keyboard mapping object.
242 /// last visited inset.
243 /** kept to send setMouseHover(false).
244 * Not owned, so don't delete.
248 mutable TextMetricsCache text_metrics_;
251 /** Not owned, so don't delete.
253 frontend::GuiBufferViewDelegate * gui_;
257 BufferView::BufferView(Buffer & buf)
258 : width_(0), height_(0), buffer_(buf), d(new Private(*this))
260 d->xsel_cache_.set = false;
261 d->intl_.initKeyMapper(lyxrc.use_kbmap);
263 d->cursor_.push(buffer_.inset());
264 d->cursor_.resetAnchor();
265 d->cursor_.setCurrentFont();
267 if (graphics::Previews::status() != LyXRC::PREVIEW_OFF)
268 graphics::Previews::get().generateBufferPreviews(buffer_);
272 BufferView::~BufferView()
274 // current buffer is going to be switched-off, save cursor pos
275 // Ideally, the whole cursor stack should be saved, but session
276 // currently can only handle bottom (whole document) level pit and pos.
277 // That is to say, if a cursor is in a nested inset, it will be
278 // restore to the left of the top level inset.
279 LastFilePosSection::FilePos fp;
280 fp.pit = d->cursor_.bottom().pit();
281 fp.pos = d->cursor_.bottom().pos();
282 LyX::ref().session().lastFilePos().save(buffer_.fileName(), fp);
288 Intl & BufferView::getIntl()
294 Intl const & BufferView::getIntl() const
300 CoordCache & BufferView::coordCache()
302 return d->coord_cache_;
306 CoordCache const & BufferView::coordCache() const
308 return d->coord_cache_;
312 Buffer & BufferView::buffer()
318 Buffer const & BufferView::buffer() const
324 bool BufferView::fitCursor()
326 if (cursorStatus(d->cursor_) == CUR_INSIDE) {
327 frontend::FontMetrics const & fm =
328 theFontMetrics(d->cursor_.getFont().fontInfo());
329 int const asc = fm.maxAscent();
330 int const des = fm.maxDescent();
331 Point const p = getPos(d->cursor_, d->cursor_.boundary());
332 if (p.y_ - asc >= 0 && p.y_ + des < height_)
339 void BufferView::processUpdateFlags(Update::flags flags)
341 // last_inset_ points to the last visited inset. This pointer may become
342 // invalid because of keyboard editing. Since all such operations
343 // causes screen update(), I reset last_inset_ to avoid such a problem.
345 // This is close to a hot-path.
346 LYXERR(Debug::DEBUG, "BufferView::processUpdateFlags()"
347 << "[fitcursor = " << (flags & Update::FitCursor)
348 << ", forceupdate = " << (flags & Update::Force)
349 << ", singlepar = " << (flags & Update::SinglePar)
350 << "] buffer: " << &buffer_);
352 // Update macro store
353 if (!(cursor().inMathed() && cursor().inMacroMode()))
354 buffer_.updateMacros();
356 // Now do the first drawing step if needed. This consists on updating
357 // the CoordCache in updateMetrics().
358 // The second drawing step is done in WorkArea::redraw() if needed.
360 // Case when no explicit update is requested.
362 // no need to redraw anything.
363 d->update_strategy_ = NoScreenUpdate;
367 if (flags == Update::Decoration) {
368 d->update_strategy_ = DecorationUpdate;
373 if (flags == Update::FitCursor
374 || flags == (Update::Decoration | Update::FitCursor)) {
375 // tell the frontend to update the screen if needed.
380 if (flags & Update::Decoration) {
381 d->update_strategy_ = DecorationUpdate;
385 // no screen update is needed.
386 d->update_strategy_ = NoScreenUpdate;
390 bool const full_metrics = flags & Update::Force || !singleParUpdate();
393 // We have to update the full screen metrics.
396 if (!(flags & Update::FitCursor)) {
397 // Nothing to do anymore. Trigger a redraw and return
402 // updateMetrics() does not update paragraph position
403 // This is done at draw() time. So we need a redraw!
407 // The cursor is off screen so ensure it is visible.
414 void BufferView::updateScrollbar()
419 Text & t = buffer_.text();
420 TextMetrics & tm = d->text_metrics_[&t];
422 LYXERR(Debug::GUI, " Updating scrollbar: height: "
423 << t.paragraphs().size()
424 << " curr par: " << d->cursor_.bottom().pit()
425 << " default height " << defaultRowHeight());
427 int const parsize = int(t.paragraphs().size());
428 if (d->par_height_.size() != parsize) {
429 d->par_height_.clear();
430 // FIXME: We assume a default paragraph height of 4 rows. This
431 // should probably be pondered with the screen width.
432 d->par_height_.resize(parsize, defaultRowHeight() * 4);
435 // It would be better to fix the scrollbar to understand
436 // values in [0..1] and divide everything by wh
438 // Look at paragraph heights on-screen
439 pit_type first_visible_pit = -1;
440 pair<pit_type, ParagraphMetrics const *> first = tm.first();
441 pair<pit_type, ParagraphMetrics const *> last = tm.last();
442 for (pit_type pit = first.first; pit <= last.first; ++pit) {
443 ParagraphMetrics const & pm = tm.parMetrics(pit);
444 d->par_height_[pit] = pm.height();
445 if (first_visible_pit >= 0 || pm.position() + pm.descent() <= 0)
447 first_visible_pit = pit;
448 LYXERR(Debug::SCROLLING, "first visible pit " << first_visible_pit);
449 // FIXME: we should look for the first visible row within
450 // the deepest inset!
451 int row_pos = pm.position();
452 size_t const nrows = pm.rows().size();
453 for (size_t i = 0; i != nrows; ++i) {
454 Row const & row = pm.rows()[i];
456 LYXERR(Debug::SCROLLING, "first visible row " << i
457 << "(row pos = " << row_pos << ");");
460 row_pos += row.height();
462 d->scrollbarParameters_.position = row_pos;
465 d->scrollbarParameters_.height = 0;
466 for (size_t i = 0; i != d->par_height_.size(); ++i) {
467 if (i == first_visible_pit)
468 d->scrollbarParameters_.position += d->scrollbarParameters_.height;
469 d->scrollbarParameters_.height += d->par_height_[i];
472 // We prefer fixed size line scrolling.
473 d->scrollbarParameters_.lineScrollHeight = defaultRowHeight();
477 ScrollbarParameters const & BufferView::scrollbarParameters() const
479 return d->scrollbarParameters_;
483 void BufferView::scrollDocView(int value)
485 int const offset = value - d->scrollbarParameters_.position;
486 //TextMetrics & tm = d->text_metrics_[&buffer_.text()];
487 if (abs(offset) <= 3*height_) {
493 for (size_t i = 0; i != d->par_height_.size(); ++i) {
494 par_pos += d->par_height_[i];
495 if (par_pos >= value) {
496 d->anchor_pit_ = pit_type(i);
501 LYXERR(Debug::SCROLLING, "value = " << value
502 << "\tanchor_ref_ = " << d->anchor_pit_
503 << "\tpar_pos = " << par_pos);
505 d->anchor_ypos_ = par_pos - value;
511 // FIXME: this method is not working well.
512 void BufferView::setCursorFromScrollbar()
514 TextMetrics & tm = d->text_metrics_[&buffer_.text()];
516 int const height = 2 * defaultRowHeight();
517 int const first = height;
518 int const last = height_ - height;
519 Cursor & cur = d->cursor_;
521 switch (cursorStatus(cur)) {
523 // We reset the cursor because cursorStatus() does not
524 // work when the cursor is within mathed.
525 cur.reset(buffer_.inset());
526 tm.setCursorFromCoordinates(cur, 0, first);
527 cur.clearSelection();
530 // We reset the cursor because cursorStatus() does not
531 // work when the cursor is within mathed.
532 cur.reset(buffer_.inset());
533 tm.setCursorFromCoordinates(cur, 0, last);
534 cur.clearSelection();
537 int const y = getPos(cur, cur.boundary()).y_;
538 int const newy = min(last, max(y, first));
540 cur.reset(buffer_.inset());
541 tm.setCursorFromCoordinates(cur, 0, newy);
547 Change const BufferView::getCurrentChange() const
549 if (!d->cursor_.selection())
550 return Change(Change::UNCHANGED);
552 DocIterator dit = d->cursor_.selectionBegin();
553 return dit.paragraph().lookupChange(dit.pos());
557 // this could be used elsewhere as well?
558 // FIXME: This does not work within mathed!
559 CursorStatus BufferView::cursorStatus(DocIterator const & dit) const
561 Point const p = getPos(dit, dit.boundary());
564 if (p.y_ > workHeight())
570 void BufferView::saveBookmark(unsigned int idx)
572 // tenatively save bookmark, id and pos will be used to
573 // acturately locate a bookmark in a 'live' lyx session.
574 // pit and pos will be updated with bottom level pit/pos
576 LyX::ref().session().bookmarks().save(
578 d->cursor_.bottom().pit(),
579 d->cursor_.bottom().pos(),
580 d->cursor_.paragraph().id(),
585 // emit message signal.
586 message(_("Save bookmark"));
590 bool BufferView::moveToPosition(pit_type bottom_pit, pos_type bottom_pos,
591 int top_id, pos_type top_pos)
593 bool success = false;
596 d->cursor_.clearSelection();
598 // if a valid par_id is given, try it first
599 // This is the case for a 'live' bookmark when unique paragraph ID
600 // is used to track bookmarks.
602 ParIterator par = buffer_.getParFromID(top_id);
603 if (par != buffer_.par_iterator_end()) {
604 doc_it = makeDocIterator(par, min(par->size(), top_pos));
605 // Some slices of the iterator may not be
606 // reachable (e.g. closed collapsable inset)
607 // so the dociterator may need to be
608 // shortened. Otherwise, setCursor may crash
609 // lyx when the cursor can not be set to these
611 size_t const n = doc_it.depth();
612 for (size_t i = 0; i < n; ++i)
613 if (doc_it[i].inset().editable() != Inset::HIGHLY_EDITABLE) {
621 // if top_id == 0, or searching through top_id failed
622 // This is the case for a 'restored' bookmark when only bottom
623 // (document level) pit was saved. Because of this, bookmark
624 // restoration is inaccurate. If a bookmark was within an inset,
625 // it will be restored to the left of the outmost inset that contains
627 if (static_cast<size_t>(bottom_pit) < buffer_.paragraphs().size()) {
628 doc_it = doc_iterator_begin(buffer_.inset());
629 doc_it.pit() = bottom_pit;
630 doc_it.pos() = min(bottom_pos, doc_it.paragraph().size());
635 // Note: only bottom (document) level pit is set.
637 // set the current font.
638 d->cursor_.setCurrentFont();
639 // To center the screen on this new position we need the
640 // paragraph position which is computed at draw() time.
641 // So we need a redraw!
651 void BufferView::translateAndInsert(char_type c, Text * t, Cursor & cur)
653 if (lyxrc.rtl_support) {
654 if (d->cursor_.real_current_font.isRightToLeft()) {
655 if (d->intl_.keymap == Intl::PRIMARY)
656 d->intl_.keyMapSec();
658 if (d->intl_.keymap == Intl::SECONDARY)
659 d->intl_.keyMapPrim();
663 d->intl_.getTransManager().translateAndInsert(c, t, cur);
667 int BufferView::workWidth() const
673 void BufferView::showCursor()
675 // We are not properly started yet, delay until resizing is
680 LYXERR(Debug::SCROLLING, "recentering!");
682 CursorSlice & bot = d->cursor_.bottom();
683 TextMetrics & tm = d->text_metrics_[bot.text()];
685 int const bot_pit = d->cursor_.bottom().pit();
686 if (bot_pit == tm.first().first - 1)
687 tm.newParMetricsUp();
688 else if (bot_pit == tm.last().first + 1)
689 tm.newParMetricsDown();
691 if (tm.has(bot_pit)) {
692 ParagraphMetrics const & pm = tm.parMetrics(bot_pit);
693 int offset = coordOffset(d->cursor_, d->cursor_.boundary()).y_;
694 int ypos = pm.position() + offset;
695 Dimension const & row_dim = d->cursor_.textRow().dimension();
696 if (ypos - row_dim.ascent() < 0)
697 scrollUp(- ypos + row_dim.ascent());
698 else if (ypos + row_dim.descent() > height_)
699 scrollDown(ypos - height_ + row_dim.descent());
700 // else, nothing to do, the cursor is already visible so we just return.
704 tm.redoParagraph(bot_pit);
705 ParagraphMetrics const & pm = tm.parMetrics(bot_pit);
706 int offset = coordOffset(d->cursor_, d->cursor_.boundary()).y_;
708 d->anchor_pit_ = bot_pit;
709 Dimension const & row_dim = d->cursor_.textRow().dimension();
711 if (d->anchor_pit_ == 0)
712 d->anchor_ypos_ = offset + pm.ascent();
713 else if (d->anchor_pit_ >= pos_type(bot.text()->paragraphs().size() - 1)) {
714 d->anchor_pit_ = bot.text()->paragraphs().size() - 1;
715 d->anchor_ypos_ = height_ - offset - row_dim.descent();
717 d->anchor_ypos_ = offset + pm.ascent() - height_ / 2;
725 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
729 Cursor & cur = d->cursor_;
731 switch (cmd.action) {
734 flag.enabled(buffer_.undo().hasUndoStack());
737 flag.enabled(buffer_.undo().hasRedoStack());
739 case LFUN_FILE_INSERT:
740 case LFUN_FILE_INSERT_PLAINTEXT_PARA:
741 case LFUN_FILE_INSERT_PLAINTEXT:
742 case LFUN_BOOKMARK_SAVE:
743 // FIXME: Actually, these LFUNS should be moved to Text
744 flag.enabled(cur.inTexted());
746 case LFUN_FONT_STATE:
747 case LFUN_LABEL_INSERT:
748 case LFUN_INFO_INSERT:
749 case LFUN_PARAGRAPH_GOTO:
751 case LFUN_REFERENCE_NEXT:
753 case LFUN_WORD_REPLACE:
756 case LFUN_MARK_TOGGLE:
757 case LFUN_SCREEN_RECENTER:
758 case LFUN_BIBTEX_DATABASE_ADD:
759 case LFUN_BIBTEX_DATABASE_DEL:
760 case LFUN_WORDS_COUNT:
761 case LFUN_NEXT_INSET_TOGGLE:
765 case LFUN_LABEL_GOTO: {
766 flag.enabled(!cmd.argument().empty()
767 || getInsetByCode<InsetRef>(cur, REF_CODE));
771 case LFUN_CHANGES_TRACK:
773 flag.setOnOff(buffer_.params().trackChanges);
776 case LFUN_CHANGES_OUTPUT:
778 flag.setOnOff(buffer_.params().outputChanges);
781 case LFUN_CHANGES_MERGE:
782 case LFUN_CHANGE_NEXT:
783 case LFUN_ALL_CHANGES_ACCEPT:
784 case LFUN_ALL_CHANGES_REJECT:
785 // TODO: context-sensitive enabling of LFUNs
786 // In principle, these command should only be enabled if there
787 // is a change in the document. However, without proper
788 // optimizations, this will inevitably result in poor performance.
792 case LFUN_BUFFER_TOGGLE_COMPRESSION: {
793 flag.setOnOff(buffer_.params().compressed);
797 case LFUN_BUFFER_TOGGLE_EMBEDDING: {
798 flag.setOnOff(buffer_.params().embedded);
803 case LFUN_SCREEN_DOWN:
807 // FIXME: LFUN_SCREEN_DOWN_SELECT should be removed from
808 // everywhere else before this can enabled:
809 case LFUN_SCREEN_UP_SELECT:
810 case LFUN_SCREEN_DOWN_SELECT:
814 case LFUN_LAYOUT_TABULAR:
815 flag.enabled(cur.innerInsetOfType(TABULAR_CODE));
819 case LFUN_LAYOUT_PARAGRAPH:
820 flag.enabled(cur.inset().forceDefaultParagraphs(cur.idx()));
823 case LFUN_INSET_SETTINGS: {
824 InsetCode code = cur.inset().lyxCode();
828 enable = cmd.argument() == "tabular";
831 enable = cmd.argument() == "ert";
834 enable = cmd.argument() == "float";
837 enable = cmd.argument() == "wrap";
840 enable = cmd.argument() == "note";
843 enable = cmd.argument() == "branch";
846 enable = cmd.argument() == "box";
849 enable = cmd.argument() == "listings";
854 flag.enabled(enable);
858 case LFUN_DIALOG_SHOW_NEW_INSET:
859 flag.enabled(cur.inset().lyxCode() != ERT_CODE &&
860 cur.inset().lyxCode() != LISTINGS_CODE);
861 if (cur.inset().lyxCode() == CAPTION_CODE) {
863 if (cur.inset().getStatus(cur, cmd, flag))
876 bool BufferView::dispatch(FuncRequest const & cmd)
878 //lyxerr << [ cmd = " << cmd << "]" << endl;
880 // Make sure that the cached BufferView is correct.
881 LYXERR(Debug::ACTION, " action[" << cmd.action << ']'
882 << " arg[" << to_utf8(cmd.argument()) << ']'
883 << " x[" << cmd.x << ']'
884 << " y[" << cmd.y << ']'
885 << " button[" << cmd.button() << ']');
887 Cursor & cur = d->cursor_;
889 switch (cmd.action) {
892 cur.message(_("Undo"));
893 cur.clearSelection();
895 cur.message(_("No further undo information"));
899 cur.message(_("Redo"));
900 cur.clearSelection();
902 cur.message(_("No further redo information"));
905 case LFUN_FONT_STATE:
906 cur.message(cur.currentState());
909 case LFUN_BOOKMARK_SAVE:
910 saveBookmark(convert<unsigned int>(to_utf8(cmd.argument())));
913 case LFUN_LABEL_GOTO: {
914 docstring label = cmd.argument();
917 getInsetByCode<InsetRef>(d->cursor_,
920 label = inset->getParam("reference");
921 // persistent=false: use temp_bookmark
931 case LFUN_PARAGRAPH_GOTO: {
932 int const id = convert<int>(to_utf8(cmd.argument()));
934 for (Buffer * b = &buffer_; i == 0 || b != &buffer_;
935 b = theBufferList().next(b)) {
937 ParIterator par = b->getParFromID(id);
938 if (par == b->par_iterator_end()) {
939 LYXERR(Debug::INFO, "No matching paragraph found! [" << id << "].");
941 LYXERR(Debug::INFO, "Paragraph " << par->id()
942 << " found in buffer `"
943 << b->absFileName() << "'.");
947 setCursor(makeDocIterator(par, 0));
949 // Switch to other buffer view and resend cmd
950 theLyXFunc().dispatch(FuncRequest(
951 LFUN_BUFFER_SWITCH, b->absFileName()));
952 theLyXFunc().dispatch(cmd);
962 gotoInset(this, NOTE_CODE, false);
965 case LFUN_REFERENCE_NEXT: {
966 vector<InsetCode> tmp;
967 tmp.push_back(LABEL_CODE);
968 tmp.push_back(REF_CODE);
969 gotoInset(this, tmp, true);
973 case LFUN_CHANGES_TRACK:
974 buffer_.params().trackChanges = !buffer_.params().trackChanges;
977 case LFUN_CHANGES_OUTPUT:
978 buffer_.params().outputChanges = !buffer_.params().outputChanges;
979 if (buffer_.params().outputChanges) {
980 bool dvipost = LaTeXFeatures::isAvailable("dvipost");
981 bool xcolorsoul = LaTeXFeatures::isAvailable("soul") &&
982 LaTeXFeatures::isAvailable("xcolor");
984 if (!dvipost && !xcolorsoul) {
985 Alert::warning(_("Changes not shown in LaTeX output"),
986 _("Changes will not be highlighted in LaTeX output, "
987 "because neither dvipost nor xcolor/soul are installed.\n"
988 "Please install these packages or redefine "
989 "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
990 } else if (!xcolorsoul) {
991 Alert::warning(_("Changes not shown in LaTeX output"),
992 _("Changes will not be highlighted in LaTeX output "
993 "when using pdflatex, because xcolor and soul are not installed.\n"
994 "Please install both packages or redefine "
995 "\\lyxadded and \\lyxdeleted in the LaTeX preamble."));
1000 case LFUN_CHANGE_NEXT:
1001 findNextChange(this);
1004 case LFUN_CHANGES_MERGE:
1005 if (findNextChange(this))
1006 showDialog("changes");
1009 case LFUN_ALL_CHANGES_ACCEPT:
1010 // select complete document
1011 d->cursor_.reset(buffer_.inset());
1012 d->cursor_.selHandle(true);
1013 buffer_.text().cursorBottom(d->cursor_);
1014 // accept everything in a single step to support atomic undo
1015 buffer_.text().acceptOrRejectChanges(d->cursor_, Text::ACCEPT);
1018 case LFUN_ALL_CHANGES_REJECT:
1019 // select complete document
1020 d->cursor_.reset(buffer_.inset());
1021 d->cursor_.selHandle(true);
1022 buffer_.text().cursorBottom(d->cursor_);
1023 // reject everything in a single step to support atomic undo
1024 // Note: reject does not work recursively; the user may have to repeat the operation
1025 buffer_.text().acceptOrRejectChanges(d->cursor_, Text::REJECT);
1028 case LFUN_WORD_FIND:
1032 case LFUN_WORD_REPLACE: {
1033 bool has_deleted = false;
1034 if (cur.selection()) {
1035 DocIterator beg = cur.selectionBegin();
1036 DocIterator end = cur.selectionEnd();
1037 if (beg.pit() == end.pit()) {
1038 for (pos_type p = beg.pos() ; p < end.pos() ; ++p) {
1039 if (cur.paragraph().isDeleted(p))
1044 replace(this, cmd, has_deleted);
1049 cur.clearSelection();
1051 cur.message(from_utf8(N_("Mark off")));
1055 cur.clearSelection();
1058 cur.message(from_utf8(N_("Mark on")));
1061 case LFUN_MARK_TOGGLE:
1062 cur.clearSelection();
1065 cur.message(from_utf8(N_("Mark removed")));
1068 cur.message(from_utf8(N_("Mark set")));
1073 case LFUN_SCREEN_RECENTER:
1077 case LFUN_BIBTEX_DATABASE_ADD: {
1078 Cursor tmpcur = d->cursor_;
1079 findInset(tmpcur, BIBTEX_CODE, false);
1080 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
1083 if (inset->addDatabase(to_utf8(cmd.argument())))
1084 buffer_.updateBibfilesCache();
1089 case LFUN_BIBTEX_DATABASE_DEL: {
1090 Cursor tmpcur = d->cursor_;
1091 findInset(tmpcur, BIBTEX_CODE, false);
1092 InsetBibtex * inset = getInsetByCode<InsetBibtex>(tmpcur,
1095 if (inset->delDatabase(to_utf8(cmd.argument())))
1096 buffer_.updateBibfilesCache();
1101 case LFUN_WORDS_COUNT: {
1102 DocIterator from, to;
1103 if (cur.selection()) {
1104 from = cur.selectionBegin();
1105 to = cur.selectionEnd();
1107 from = doc_iterator_begin(buffer_.inset());
1108 to = doc_iterator_end(buffer_.inset());
1110 int const count = countWords(from, to);
1113 if (cur.selection())
1114 message = bformat(_("%1$d words in selection."),
1117 message = bformat(_("%1$d words in document."),
1121 if (cur.selection())
1122 message = _("One word in selection.");
1124 message = _("One word in document.");
1127 Alert::information(_("Count words"), message);
1131 case LFUN_BUFFER_TOGGLE_COMPRESSION:
1132 // turn compression on/off
1133 buffer_.params().compressed = !buffer_.params().compressed;
1136 case LFUN_BUFFER_TOGGLE_EMBEDDING:
1137 // turn embedding on/off
1138 buffer_.embeddedFiles().enable(!buffer_.params().embedded);
1141 case LFUN_NEXT_INSET_TOGGLE: {
1142 // this is the real function we want to invoke
1143 FuncRequest tmpcmd = FuncRequest(LFUN_INSET_TOGGLE, cmd.origin);
1144 // if there is an inset at cursor, see whether it
1146 Inset * inset = cur.nextInset();
1148 if (inset->isActive()) {
1149 Cursor tmpcur = cur;
1150 tmpcur.pushBackward(*inset);
1151 inset->dispatch(tmpcur, tmpcmd);
1152 if (tmpcur.result().dispatched()) {
1155 } else if (inset->editable() == Inset::IS_EDITABLE) {
1156 inset->edit(cur, true);
1159 // if it did not work, try the underlying inset.
1160 if (!cur.result().dispatched())
1161 cur.dispatch(tmpcmd);
1163 if (cur.result().dispatched())
1164 cur.clearSelection();
1169 case LFUN_SCREEN_UP:
1170 case LFUN_SCREEN_DOWN: {
1171 Point p = getPos(cur, cur.boundary());
1172 if (p.y_ < 0 || p.y_ > height_) {
1173 // The cursor is off-screen so recenter before proceeding.
1175 p = getPos(cur, cur.boundary());
1177 scroll(cmd.action == LFUN_SCREEN_UP? - height_ : height_);
1178 cur.reset(buffer_.inset());
1179 d->text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_);
1180 //FIXME: what to do with cur.x_target()?
1185 case LFUN_SCREEN_UP_SELECT:
1186 case LFUN_SCREEN_DOWN_SELECT: {
1187 // Those two are not ready yet for consumption.
1190 cur.selHandle(true);
1191 size_t initial_depth = cur.depth();
1192 Point const p = getPos(cur, cur.boundary());
1193 scroll(cmd.action == LFUN_SCREEN_UP_SELECT? - height_ : height_);
1194 // FIXME: We need to verify if the cursor stayed within an inset...
1195 //cur.reset(buffer_.inset());
1196 d->text_metrics_[&buffer_.text()].editXY(cur, p.x_, p.y_);
1198 while (cur.depth() > initial_depth) {
1201 // FIXME: we need to do a redraw again because of the selection
1202 // But no screen update is needed.
1203 d->update_strategy_ = NoScreenUpdate;
1216 docstring const BufferView::requestSelection()
1218 Cursor & cur = d->cursor_;
1220 if (!cur.selection()) {
1221 d->xsel_cache_.set = false;
1225 if (!d->xsel_cache_.set ||
1226 cur.top() != d->xsel_cache_.cursor ||
1227 cur.anchor_.top() != d->xsel_cache_.anchor)
1229 d->xsel_cache_.cursor = cur.top();
1230 d->xsel_cache_.anchor = cur.anchor_.top();
1231 d->xsel_cache_.set = cur.selection();
1232 return cur.selectionAsString(false);
1238 void BufferView::clearSelection()
1240 d->cursor_.clearSelection();
1241 // Clear the selection buffer. Otherwise a subsequent
1242 // middle-mouse-button paste would use the selection buffer,
1243 // not the more current external selection.
1244 cap::clearSelection();
1245 d->xsel_cache_.set = false;
1246 // The buffer did not really change, but this causes the
1247 // redraw we need because we cleared the selection above.
1252 void BufferView::resize(int width, int height)
1254 // Update from work area
1258 // Clear the paragraph height cache.
1259 d->par_height_.clear();
1265 Inset const * BufferView::getCoveringInset(Text const & text, int x, int y)
1267 TextMetrics & tm = d->text_metrics_[&text];
1268 Inset * inset = tm.checkInsetHit(x, y);
1272 if (!inset->descendable())
1273 // No need to go further down if the inset is not
1277 size_t cell_number = inset->nargs();
1278 // Check all the inner cell.
1279 for (size_t i = 0; i != cell_number; ++i) {
1280 Text const * inner_text = inset->getText(i);
1283 Inset const * inset_deeper =
1284 getCoveringInset(*inner_text, x, y);
1286 return inset_deeper;
1294 void BufferView::mouseEventDispatch(FuncRequest const & cmd0)
1296 //lyxerr << "[ cmd0 " << cmd0 << "]" << endl;
1298 // This is only called for mouse related events including
1299 // LFUN_FILE_OPEN generated by drag-and-drop.
1300 FuncRequest cmd = cmd0;
1303 cur.push(buffer_.inset());
1304 cur.selection() = d->cursor_.selection();
1306 // Either the inset under the cursor or the
1307 // surrounding Text will handle this event.
1309 // make sure we stay within the screen...
1310 cmd.y = min(max(cmd.y, -1), height_);
1312 if (cmd.action == LFUN_MOUSE_MOTION && cmd.button() == mouse_button::none) {
1314 // Get inset under mouse, if there is one.
1315 Inset const * covering_inset =
1316 getCoveringInset(buffer_.text(), cmd.x, cmd.y);
1317 if (covering_inset == d->last_inset_)
1318 // Same inset, no need to do anything...
1321 bool need_redraw = false;
1322 // const_cast because of setMouseHover().
1323 Inset * inset = const_cast<Inset *>(covering_inset);
1325 // Remove the hint on the last hovered inset (if any).
1326 need_redraw |= d->last_inset_->setMouseHover(false);
1328 // Highlighted the newly hovered inset (if any).
1329 need_redraw |= inset->setMouseHover(true);
1330 d->last_inset_ = inset;
1334 LYXERR(Debug::PAINTING, "Mouse hover detected at: ("
1335 << cmd.x << ", " << cmd.y << ")");
1337 d->update_strategy_ = DecorationUpdate;
1339 // This event (moving without mouse click) is not passed further.
1340 // This should be changed if it is further utilized.
1345 // Build temporary cursor.
1346 Inset * inset = d->text_metrics_[&buffer_.text()].editXY(cur, cmd.x, cmd.y);
1348 // Put anchor at the same position.
1351 // Try to dispatch to an non-editable inset near this position
1352 // via the temp cursor. If the inset wishes to change the real
1353 // cursor it has to do so explicitly by using
1354 // cur.bv().cursor() = cur; (or similar)
1356 inset->dispatch(cur, cmd);
1358 // Now dispatch to the temporary cursor. If the real cursor should
1359 // be modified, the inset's dispatch has to do so explicitly.
1360 if (!cur.result().dispatched())
1363 //Do we have a selection?
1364 theSelection().haveSelection(cursor().selection());
1366 // If the command has been dispatched,
1367 if (cur.result().dispatched()
1368 // an update is asked,
1369 && cur.result().update())
1370 processUpdateFlags(cur.result().update());
1374 void BufferView::scroll(int y)
1383 void BufferView::scrollDown(int offset)
1385 Text * text = &buffer_.text();
1386 TextMetrics & tm = d->text_metrics_[text];
1387 int ymax = height_ + offset;
1389 pair<pit_type, ParagraphMetrics const *> last = tm.last();
1390 int bottom_pos = last.second->position() + last.second->descent();
1391 if (last.first + 1 == int(text->paragraphs().size())) {
1392 if (bottom_pos <= height_)
1394 offset = min(offset, bottom_pos - height_);
1397 if (bottom_pos > ymax)
1399 tm.newParMetricsDown();
1401 d->anchor_ypos_ -= offset;
1407 void BufferView::scrollUp(int offset)
1409 Text * text = &buffer_.text();
1410 TextMetrics & tm = d->text_metrics_[text];
1411 int ymin = - offset;
1413 pair<pit_type, ParagraphMetrics const *> first = tm.first();
1414 int top_pos = first.second->position() - first.second->ascent();
1415 if (first.first == 0) {
1418 offset = min(offset, - top_pos);
1423 tm.newParMetricsUp();
1425 d->anchor_ypos_ += offset;
1431 void BufferView::setCursorFromRow(int row)
1436 buffer_.texrow().getIdFromRow(row, tmpid, tmppos);
1438 d->cursor_.reset(buffer_.inset());
1440 buffer_.text().setCursor(d->cursor_, 0, 0);
1442 buffer_.text().setCursor(d->cursor_, buffer_.getParFromID(tmpid).pit(), tmppos);
1446 void BufferView::gotoLabel(docstring const & label)
1448 for (InsetIterator it = inset_iterator_begin(buffer_.inset()); it; ++it) {
1449 vector<docstring> labels;
1450 it->getLabelList(buffer_, labels);
1451 if (find(labels.begin(), labels.end(), label) != labels.end()) {
1453 processUpdateFlags(Update::FitCursor);
1460 TextMetrics const & BufferView::textMetrics(Text const * t) const
1462 return const_cast<BufferView *>(this)->textMetrics(t);
1466 TextMetrics & BufferView::textMetrics(Text const * t)
1468 TextMetricsCache::iterator tmc_it = d->text_metrics_.find(t);
1469 if (tmc_it == d->text_metrics_.end()) {
1470 tmc_it = d->text_metrics_.insert(
1471 make_pair(t, TextMetrics(this, const_cast<Text *>(t)))).first;
1473 return tmc_it->second;
1477 ParagraphMetrics const & BufferView::parMetrics(Text const * t,
1480 return textMetrics(t).parMetrics(pit);
1484 int BufferView::workHeight() const
1490 void BufferView::setCursor(DocIterator const & dit)
1492 size_t const n = dit.depth();
1493 for (size_t i = 0; i < n; ++i)
1494 dit[i].inset().edit(d->cursor_, true);
1496 d->cursor_.setCursor(dit);
1497 d->cursor_.selection() = false;
1501 bool BufferView::checkDepm(Cursor & cur, Cursor & old)
1503 // Would be wrong to delete anything if we have a selection.
1504 if (cur.selection())
1507 bool need_anchor_change = false;
1508 bool changed = d->cursor_.text()->deleteEmptyParagraphMechanism(cur, old,
1509 need_anchor_change);
1511 if (need_anchor_change)
1517 updateLabels(buffer_);
1525 bool BufferView::mouseSetCursor(Cursor & cur, bool select)
1527 BOOST_ASSERT(&cur.bv() == this);
1530 // this event will clear selection so we save selection for
1531 // persistent selection
1532 cap::saveSelection(cursor());
1534 // Has the cursor just left the inset?
1535 bool badcursor = false;
1536 bool leftinset = (&d->cursor_.inset() != &cur.inset());
1538 badcursor = notifyCursorLeaves(d->cursor_, cur);
1540 // FIXME: shift-mouse selection doesn't work well across insets.
1541 bool do_selection = select && &d->cursor_.anchor().inset() == &cur.inset();
1543 // do the dEPM magic if needed
1544 // FIXME: (1) move this to InsetText::notifyCursorLeaves?
1545 // FIXME: (2) if we had a working InsetText::notifyCursorLeaves,
1546 // the leftinset bool would not be necessary (badcursor instead).
1547 bool update = leftinset;
1548 if (!do_selection && !badcursor && d->cursor_.inTexted())
1549 update |= checkDepm(cur, d->cursor_);
1551 // if the cursor was in an empty script inset and the new
1552 // position is in the nucleus of the inset, notifyCursorLeaves
1553 // will kill the script inset itself. So we check all the
1554 // elements of the cursor to make sure that they are correct.
1555 // For an example, see bug 2933:
1556 // http://bugzilla.lyx.org/show_bug.cgi?id=2933
1557 // The code below could maybe be moved to a DocIterator method.
1558 //lyxerr << "cur before " << cur <<endl;
1559 DocIterator dit(cur.inset());
1560 dit.push_back(cur.bottom());
1562 while (i < cur.depth() && dit.nextInset() == &cur[i].inset()) {
1563 dit.push_back(cur[i]);
1566 //lyxerr << "5 cur after" << dit <<endl;
1568 d->cursor_.setCursor(dit);
1569 d->cursor_.boundary(cur.boundary());
1571 d->cursor_.setSelection();
1573 d->cursor_.clearSelection();
1575 d->cursor_.finishUndo();
1576 d->cursor_.setCurrentFont();
1581 void BufferView::putSelectionAt(DocIterator const & cur,
1582 int length, bool backwards)
1584 d->cursor_.clearSelection();
1590 d->cursor_.pos() += length;
1591 d->cursor_.setSelection(d->cursor_, -length);
1593 d->cursor_.setSelection(d->cursor_, length);
1598 Cursor & BufferView::cursor()
1604 Cursor const & BufferView::cursor() const
1610 pit_type BufferView::anchor_ref() const
1612 return d->anchor_pit_;
1616 bool BufferView::singleParUpdate()
1618 Text & buftext = buffer_.text();
1619 pit_type const bottom_pit = d->cursor_.bottom().pit();
1620 TextMetrics & tm = textMetrics(&buftext);
1621 int old_height = tm.parMetrics(bottom_pit).height();
1623 // In Single Paragraph mode, rebreak only
1624 // the (main text, not inset!) paragraph containing the cursor.
1625 // (if this paragraph contains insets etc., rebreaking will
1626 // recursively descend)
1627 tm.redoParagraph(bottom_pit);
1628 ParagraphMetrics const & pm = tm.parMetrics(bottom_pit);
1629 if (pm.height() != old_height)
1630 // Paragraph height has changed so we cannot proceed to
1631 // the singlePar optimisation.
1634 d->update_strategy_ = SingleParUpdate;
1636 LYXERR(Debug::PAINTING, "\ny1: " << pm.position() - pm.ascent()
1637 << " y2: " << pm.position() + pm.descent()
1638 << " pit: " << bottom_pit
1639 << " singlepar: 1");
1644 void BufferView::updateMetrics()
1646 Text & buftext = buffer_.text();
1647 pit_type const npit = int(buftext.paragraphs().size());
1649 // Clear out the position cache in case of full screen redraw,
1650 d->coord_cache_.clear();
1652 // Clear out paragraph metrics to avoid having invalid metrics
1653 // in the cache from paragraphs not relayouted below
1654 // The complete text metrics will be redone.
1655 d->text_metrics_.clear();
1657 TextMetrics & tm = textMetrics(&buftext);
1659 // Rebreak anchor paragraph.
1660 tm.redoParagraph(d->anchor_pit_);
1661 ParagraphMetrics & anchor_pm = tm.par_metrics_[d->anchor_pit_];
1662 anchor_pm.setPosition(d->anchor_ypos_);
1664 LYXERR(Debug::PAINTING, "metrics: "
1665 << " anchor pit = " << d->anchor_pit_
1666 << " anchor ypos = " << d->anchor_ypos_);
1668 // Redo paragraphs above anchor if necessary.
1669 int y1 = d->anchor_ypos_ - anchor_pm.ascent();
1670 // We are now just above the anchor paragraph.
1671 pit_type pit1 = d->anchor_pit_ - 1;
1672 for (; pit1 >= 0 && y1 > 0; --pit1) {
1673 tm.redoParagraph(pit1);
1674 ParagraphMetrics & pm = tm.par_metrics_[pit1];
1676 // Save the paragraph position in the cache.
1681 // Redo paragraphs below the anchor if necessary.
1682 int y2 = d->anchor_ypos_ + anchor_pm.descent();
1683 // We are now just below the anchor paragraph.
1684 pit_type pit2 = d->anchor_pit_ + 1;
1685 for (; pit2 < npit && y2 < height_; ++pit2) {
1686 tm.redoParagraph(pit2);
1687 ParagraphMetrics & pm = tm.par_metrics_[pit2];
1689 // Save the paragraph position in the cache.
1694 LYXERR(Debug::PAINTING, "Metrics: "
1695 << " anchor pit = " << d->anchor_pit_
1696 << " anchor ypos = " << d->anchor_ypos_
1699 << " pit1 = " << pit1
1700 << " pit2 = " << pit2);
1702 d->update_strategy_ = FullScreenUpdate;
1704 if (lyxerr.debugging(Debug::WORKAREA)) {
1705 LYXERR(Debug::WORKAREA, "BufferView::updateMetrics");
1706 d->coord_cache_.dump();
1711 void BufferView::insertLyXFile(FileName const & fname)
1713 BOOST_ASSERT(d->cursor_.inTexted());
1715 // Get absolute path of file and add ".lyx"
1716 // to the filename if necessary
1717 FileName filename = fileSearch(string(), fname.absFilename(), "lyx");
1719 docstring const disp_fn = makeDisplayPath(filename.absFilename());
1720 // emit message signal.
1721 message(bformat(_("Inserting document %1$s..."), disp_fn));
1724 Buffer buf("", false);
1725 if (buf.loadLyXFile(filename)) {
1726 ErrorList & el = buffer_.errorList("Parse");
1727 // Copy the inserted document error list into the current buffer one.
1728 el = buf.errorList("Parse");
1729 buffer_.undo().recordUndo(d->cursor_);
1730 cap::pasteParagraphList(d->cursor_, buf.paragraphs(),
1731 buf.params().getTextClassPtr(), el);
1732 res = _("Document %1$s inserted.");
1734 res = _("Could not insert document %1$s");
1739 // emit message signal.
1740 message(bformat(res, disp_fn));
1741 buffer_.errors("Parse");
1745 Point BufferView::coordOffset(DocIterator const & dit, bool boundary) const
1751 // Addup contribution of nested insets, from inside to outside,
1752 // keeping the outer paragraph for a special handling below
1753 for (size_t i = dit.depth() - 1; i >= 1; --i) {
1754 CursorSlice const & sl = dit[i];
1758 // get relative position inside sl.inset()
1759 sl.inset().cursorPos(*this, sl, boundary && (i + 1 == dit.depth()), xx, yy);
1761 // Make relative position inside of the edited inset relative to sl.inset()
1765 // In case of an RTL inset, the edited inset will be positioned to the left
1768 bool boundary_i = boundary && i + 1 == dit.depth();
1769 bool rtl = textMetrics(sl.text()).isRTL(sl, boundary_i);
1774 // remember width for the case that sl.inset() is positioned in an RTL inset
1775 if (i && dit[i - 1].text()) {
1776 // If this Inset is inside a Text Inset, retrieve the Dimension
1777 // from the containing text instead of using Inset::dimension() which
1778 // might not be implemented.
1779 // FIXME (Abdel 23/09/2007): this is a bit messy because of the
1780 // elimination of Inset::dim_ cache. This coordOffset() method needs
1781 // to be rewritten in light of the new design.
1782 Dimension const & dim = parMetrics(dit[i - 1].text(),
1783 dit[i - 1].pit()).insetDimension(&sl.inset());
1786 Dimension const dim = sl.inset().dimension(*this);
1790 //lyxerr << "Cursor::getPos, i: "
1791 // << i << " x: " << xx << " y: " << y << endl;
1794 // Add contribution of initial rows of outermost paragraph
1795 CursorSlice const & sl = dit[0];
1796 TextMetrics const & tm = textMetrics(sl.text());
1797 ParagraphMetrics const & pm = tm.parMetrics(sl.pit());
1798 BOOST_ASSERT(!pm.rows().empty());
1799 y -= pm.rows()[0].ascent();
1801 // FIXME: document this mess
1803 if (sl.pos() > 0 && dit.depth() == 1) {
1805 if (pos && boundary)
1807 // lyxerr << "coordOffset: boundary:" << boundary << " depth:" << dit.depth() << " pos:" << pos << " sl.pos:" << sl.pos() << endl;
1808 rend = pm.pos2row(pos);
1810 rend = pm.pos2row(sl.pos());
1812 size_t rend = pm.pos2row(sl.pos());
1814 for (size_t rit = 0; rit != rend; ++rit)
1815 y += pm.rows()[rit].height();
1816 y += pm.rows()[rend].ascent();
1818 TextMetrics const & bottom_tm = textMetrics(dit.bottom().text());
1820 // Make relative position from the nested inset now bufferview absolute.
1821 int xx = bottom_tm.cursorX(dit.bottom(), boundary && dit.depth() == 1);
1824 // In the RTL case place the nested inset at the left of the cursor in
1825 // the outer paragraph
1826 bool boundary_1 = boundary && 1 == dit.depth();
1827 bool rtl = bottom_tm.isRTL(dit.bottom(), boundary_1);
1835 Point BufferView::getPos(DocIterator const & dit, bool boundary) const
1837 CursorSlice const & bot = dit.bottom();
1838 TextMetrics const & tm = textMetrics(bot.text());
1839 if (!tm.has(bot.pit()))
1840 return Point(-1, -1);
1842 Point p = coordOffset(dit, boundary); // offset from outer paragraph
1843 p.y_ += tm.parMetrics(bot.pit()).position();
1848 void BufferView::draw(frontend::Painter & pain)
1850 LYXERR(Debug::PAINTING, "\t\t*** START DRAWING ***");
1851 Text & text = buffer_.text();
1852 TextMetrics const & tm = d->text_metrics_[&text];
1853 int const y = tm.first().second->position();
1854 PainterInfo pi(this, pain);
1856 switch (d->update_strategy_) {
1858 case NoScreenUpdate:
1859 // If no screen painting is actually needed, only some the different
1860 // coordinates of insets and paragraphs needs to be updated.
1861 pi.full_repaint = true;
1862 pi.pain.setDrawingEnabled(false);
1866 case SingleParUpdate:
1867 pi.full_repaint = false;
1868 // In general, only the current row of the outermost paragraph
1869 // will be redrawn. Particular cases where selection spans
1870 // multiple paragraph are correctly detected in TextMetrics.
1874 case DecorationUpdate:
1875 // FIXME: We should also distinguish DecorationUpdate to avoid text
1876 // drawing if possible. This is not possible to do easily right now
1877 // because of the single backing pixmap.
1879 case FullScreenUpdate:
1880 // The whole screen, including insets, will be refreshed.
1881 pi.full_repaint = true;
1883 // Clear background.
1884 pain.fillRectangle(0, 0, width_, height_,
1885 buffer_.inset().backgroundColor());
1890 // and possibly grey out below
1891 pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
1892 int const y2 = lastpm.second->position() + lastpm.second->descent();
1894 pain.fillRectangle(0, y2, width_, height_ - y2, Color_bottomarea);
1897 LYXERR(Debug::PAINTING, "\n\t\t*** END DRAWING ***");
1899 // The scrollbar needs an update.
1902 // Normalize anchor for next time
1903 pair<pit_type, ParagraphMetrics const *> firstpm = tm.first();
1904 pair<pit_type, ParagraphMetrics const *> lastpm = tm.last();
1905 for (pit_type pit = firstpm.first; pit <= lastpm.first; ++pit) {
1906 ParagraphMetrics const & pm = tm.parMetrics(pit);
1907 if (pm.position() + pm.descent() > 0) {
1908 d->anchor_pit_ = pit;
1909 d->anchor_ypos_ = pm.position();
1913 LYXERR(Debug::PAINTING, "Found new anchor pit = " << d->anchor_pit_
1914 << " anchor ypos = " << d->anchor_ypos_);
1918 void BufferView::message(docstring const & msg)
1921 d->gui_->message(msg);
1925 void BufferView::showDialog(string const & name)
1928 d->gui_->showDialog(name, string());
1932 void BufferView::showDialog(string const & name,
1933 string const & data, Inset * inset)
1936 d->gui_->showDialog(name, data, inset);
1940 void BufferView::updateDialog(string const & name, string const & data)
1943 d->gui_->updateDialog(name, data);
1947 void BufferView::setGuiDelegate(frontend::GuiBufferViewDelegate * gui)
1953 // FIXME: Move this out of BufferView again
1954 docstring BufferView::contentsOfPlaintextFile(FileName const & fname)
1956 if (!fname.isReadableFile()) {
1957 docstring const error = from_ascii(strerror(errno));
1958 docstring const file = makeDisplayPath(fname.absFilename(), 50);
1959 docstring const text =
1960 bformat(_("Could not read the specified document\n"
1961 "%1$s\ndue to the error: %2$s"), file, error);
1962 Alert::error(_("Could not read file"), text);
1966 if (!fname.isReadableFile()) {
1967 docstring const file = makeDisplayPath(fname.absFilename(), 50);
1968 docstring const text =
1969 bformat(_("%1$s\n is not readable."), file);
1970 Alert::error(_("Could not open file"), text);
1974 // FIXME UNICODE: We don't know the encoding of the file
1975 docstring file_content = fname.fileContents("UTF-8");
1976 if (file_content.empty()) {
1977 Alert::error(_("Reading not UTF-8 encoded file"),
1978 _("The file is not UTF-8 encoded.\n"
1979 "It will be read as local 8Bit-encoded.\n"
1980 "If this does not give the correct result\n"
1981 "then please change the encoding of the file\n"
1982 "to UTF-8 with a program other than LyX.\n"));
1983 file_content = fname.fileContents("local8bit");
1986 return normalize_c(file_content);
1990 void BufferView::insertPlaintextFile(FileName const & f, bool asParagraph)
1992 docstring const tmpstr = contentsOfPlaintextFile(f);
1997 Cursor & cur = cursor();
1998 cap::replaceSelection(cur);
1999 buffer_.undo().recordUndo(cur);
2001 cur.innerText()->insertStringAsParagraphs(cur, tmpstr);
2003 cur.innerText()->insertStringAsLines(cur, tmpstr);