2 * \file BufferView_pimpl.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Alfredo Braunstein
8 * \author Lars Gullik Bjønnes
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "BufferView_pimpl.h"
23 #include "buffer_funcs.h"
24 #include "bufferlist.h"
25 #include "bufferparams.h"
26 #include "coordcache.h"
29 #include "dispatchresult.h"
31 #include "FloatList.h"
32 #include "funcrequest.h"
33 #include "FuncStatus.h"
36 #include "insetiterator.h"
37 #include "lyx_cb.h" // added for Dispatch functions
43 #include "lastfiles.h"
44 #include "metricsinfo.h"
45 #include "paragraph.h"
46 #include "paragraph_funcs.h"
47 #include "ParagraphParameters.h"
48 #include "pariterator.h"
49 #include "rowpainter.h"
53 #include "insets/insetref.h"
54 #include "insets/insettext.h"
56 #include "frontends/Alert.h"
57 #include "frontends/Dialogs.h"
58 #include "frontends/FileDialog.h"
59 #include "frontends/LyXView.h"
60 #include "frontends/LyXScreenFactory.h"
61 #include "frontends/screen.h"
62 #include "frontends/WorkArea.h"
63 #include "frontends/WorkAreaFactory.h"
65 #include "graphics/Previews.h"
67 #include "support/filefilterlist.h"
68 #include "support/filetools.h"
69 #include "support/forkedcontr.h"
70 #include "support/path_defines.h"
71 #include "support/tostr.h"
72 #include "support/types.h"
74 #include <boost/bind.hpp>
80 using lyx::support::AddPath;
81 using lyx::support::bformat;
82 using lyx::support::FileFilterList;
83 using lyx::support::FileSearch;
84 using lyx::support::ForkedcallsController;
85 using lyx::support::IsDirWriteable;
86 using lyx::support::MakeDisplayPath;
87 using lyx::support::MakeAbsPath;
88 using lyx::support::strToUnsignedInt;
89 using lyx::support::system_lyxdir;
92 using std::istringstream;
97 using std::mem_fun_ref;
100 extern BufferList bufferlist;
105 unsigned int const saved_positions_num = 20;
107 // All the below connection objects are needed because of a bug in some
108 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
109 // to these connections we avoid a segfault upon startup, and also at exit.
112 boost::signals::connection dispatchcon;
113 boost::signals::connection timecon;
114 boost::signals::connection doccon;
115 boost::signals::connection resizecon;
116 boost::signals::connection kpresscon;
117 boost::signals::connection selectioncon;
118 boost::signals::connection lostcon;
121 /// Get next inset of this class from current cursor position
123 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
126 DocIterator it = cur;
127 if (it.nextInset() &&
128 it.nextInset()->lyxCode() == code) {
129 inset = static_cast<T*>(it.nextInset());
138 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
139 int width, int height)
140 : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
141 using_xterm_cursor(false), cursor_(bv) ,
142 anchor_ref_(0), offset_ref_(0)
144 xsel_cache_.set = false;
146 workarea_.reset(WorkAreaFactory::create(*owner_, width, height));
147 screen_.reset(LyXScreenFactory::create(workarea()));
150 doccon = workarea().scrollDocView
151 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
152 resizecon = workarea().workAreaResize
153 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
154 dispatchcon = workarea().dispatch
155 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
156 kpresscon = workarea().workAreaKeyPress
157 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
158 selectioncon = workarea().selectionRequested
159 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
160 lostcon = workarea().selectionLost
161 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
163 timecon = cursor_timeout.timeout
164 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
165 cursor_timeout.start();
166 saved_positions.resize(saved_positions_num);
170 void BufferView::Pimpl::addError(ErrorItem const & ei)
172 errorlist_.push_back(ei);
176 void BufferView::Pimpl::showReadonly(bool)
178 owner_->updateWindowTitle();
179 owner_->getDialogs().updateBufferDependent(false);
183 void BufferView::Pimpl::connectBuffer(Buffer & buf)
185 if (errorConnection_.connected())
190 boost::bind(&BufferView::Pimpl::addError, this, _1));
194 boost::bind(&LyXView::message, owner_, _1));
198 boost::bind(&LyXView::busy, owner_, _1));
201 buf.updateTitles.connect(
202 boost::bind(&LyXView::updateWindowTitle, owner_));
205 buf.resetAutosaveTimers.connect(
206 boost::bind(&LyXView::resetAutosaveTimer, owner_));
208 readonlyConnection_ =
209 buf.readonly.connect(
210 boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
214 boost::bind(&BufferView::Pimpl::setBuffer, this, (Buffer *)0));
218 void BufferView::Pimpl::disconnectBuffer()
220 errorConnection_.disconnect();
221 messageConnection_.disconnect();
222 busyConnection_.disconnect();
223 titleConnection_.disconnect();
224 timerConnection_.disconnect();
225 readonlyConnection_.disconnect();
226 closingConnection_.disconnect();
230 void BufferView::Pimpl::newFile(string const & filename, string const & tname,
233 setBuffer(::newFile(filename, tname, isNamed));
237 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
239 // get absolute path of file and add ".lyx" to the filename if
241 string s = FileSearch(string(), filename, "lyx");
243 bool const found = !s.empty();
248 // file already open?
249 if (bufferlist.exists(s)) {
250 string const file = MakeDisplayPath(s, 20);
251 string text = bformat(_("The document %1$s is already "
252 "loaded.\n\nDo you want to revert "
253 "to the saved version?"), file);
254 int const ret = Alert::prompt(_("Revert to saved document?"),
255 text, 0, 1, _("&Revert"), _("&Switch to document"));
258 setBuffer(bufferlist.getBuffer(s));
261 // FIXME: should be LFUN_REVERT
262 if (!bufferlist.close(bufferlist.getBuffer(s), false))
264 // Fall through to new load. (Asger)
270 b = bufferlist.newBuffer(s);
272 if (!::loadLyXFile(b, s)) {
273 bufferlist.release(b);
277 string text = bformat(_("The document %1$s does not yet "
278 "exist.\n\nDo you want to create "
279 "a new document?"), s);
280 int const ret = Alert::prompt(_("Create new document?"),
281 text, 0, 1, _("&Create"), _("Cancel"));
284 b = ::newFile(s, string(), true);
290 bv_->showErrorList(_("Parse"));
293 LyX::ref().lastfiles().newFile(b->fileName());
299 WorkArea & BufferView::Pimpl::workarea() const
301 return *workarea_.get();
305 LyXScreen & BufferView::Pimpl::screen() const
307 return *screen_.get();
311 Painter & BufferView::Pimpl::painter() const
313 return workarea().getPainter();
317 void BufferView::Pimpl::setBuffer(Buffer * b)
319 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
324 // if we are closing current buffer, switch to the first in
327 lyxerr[Debug::INFO] << " No Buffer!" << endl;
328 // we are closing the buffer, use the first buffer as current
329 buffer_ = bufferlist.first();
330 owner_->getDialogs().hideBufferDependent();
332 // set current buffer
337 cursor_ = LCursor(*bv_);
342 // if we're quitting lyx, don't bother updating stuff
347 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
348 connectBuffer(*buffer_);
350 cursor_.push(buffer_->inset());
351 cursor_.resetAnchor();
352 buffer_->text().init(bv_);
353 buffer_->text().setCurrentFont(cursor_);
355 // Buffer-dependent dialogs should be updated or
356 // hidden. This should go here because some dialogs (eg ToC)
357 // require bv_->text.
358 owner_->getDialogs().updateBufferDependent(true);
363 owner_->updateMenubar();
364 owner_->updateToolbars();
365 owner_->updateLayoutChoice();
366 owner_->updateWindowTitle();
368 // This is done after the layout combox has been populated
370 owner_->setLayout(cursor_.paragraph().layout()->name());
372 if (buffer_ && lyx::graphics::Previews::status() != LyXRC::PREVIEW_OFF)
373 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
377 void BufferView::Pimpl::resizeCurrentBuffer()
379 lyxerr[Debug::DEBUG] << "resizeCurrentBuffer" << endl;
381 owner_->message(_("Formatting document..."));
383 LyXText * text = bv_->text();
393 // reset the "Formatting..." message
394 owner_->clearMessage();
400 void BufferView::Pimpl::updateScrollbar()
403 lyxerr[Debug::DEBUG] << "no text in updateScrollbar" << endl;
404 workarea().setScrollbarParams(0, 0, 0);
408 LyXText & t = *bv_->text();
409 if (anchor_ref_ > int(t.paragraphs().size()) - 1) {
410 anchor_ref_ = int(t.paragraphs().size()) - 1;
415 << "Updating scrollbar: height: " << t.paragraphs().size()
416 << " curr par: " << bv_->cursor().bottom().pit()
417 << " default height " << defaultRowHeight() << endl;
419 //it would be better to fix the scrollbar to understand
420 //values in [0..1] and divide everything by wh
421 int const wh = workarea().workHeight() / 4;
422 int const h = t.getPar(anchor_ref_).height();
423 workarea().setScrollbarParams(t.paragraphs().size() * wh, anchor_ref_ * wh + int(offset_ref_ * wh / float(h)), int (wh * defaultRowHeight() / float(h)));
424 // workarea().setScrollbarParams(t.paragraphs().size(), anchor_ref_, 1);
428 void BufferView::Pimpl::scrollDocView(int value)
430 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
435 screen().hideCursor();
437 int const wh = workarea().workHeight() / 4;
439 LyXText & t = *bv_->text();
441 float const bar = value / float(wh * t.paragraphs().size());
443 anchor_ref_ = int(bar * t.paragraphs().size());
444 t.redoParagraph(anchor_ref_);
445 int const h = t.getPar(anchor_ref_).height();
446 offset_ref_ = int((bar * t.paragraphs().size() - anchor_ref_) * h);
447 lyxerr << "scrolling: " << value << std::endl;
450 if (!lyxrc.cursor_follows_scrollbar)
453 int const height = 2 * defaultRowHeight();
454 int const first = height;
455 int const last = workarea().workHeight() - height;
456 LCursor & cur = bv_->cursor();
458 bv_funcs::CurStatus st = bv_funcs::status(bv_, cur);
461 case bv_funcs::CUR_ABOVE:
462 t.setCursorFromCoordinates(cur, 0, first);
463 cur.clearSelection();
465 case bv_funcs::CUR_BELOW:
466 t.setCursorFromCoordinates(cur, 0, last);
467 cur.clearSelection();
469 case bv_funcs::CUR_INSIDE:
470 int const y = bv_funcs::getPos(cur).y_;
471 int const newy = min(last, max(y, first));
473 cur.reset(bv_->buffer()->inset());
474 t.setCursorFromCoordinates(cur, 0, newy);
477 owner_->updateLayoutChoice();
481 void BufferView::Pimpl::scroll(int lines)
486 // LyXText const * t = bv_->text();
487 // int const line_height = defaultRowHeight();
489 // // The new absolute coordinate
490 // int new_top_y = top_y() + lines * line_height;
492 // // Restrict to a valid value
493 // new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
494 // new_top_y = std::max(0, new_top_y);
496 // scrollDocView(new_top_y);
498 // // Update the scrollbar.
499 // workarea().setScrollbarParams(t->height(), top_y(), defaultRowHeight());
503 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
504 key_modifier::state state)
506 bv_->owner()->getLyXFunc().processKeySym(key, state);
508 /* This is perhaps a bit of a hack. When we move
509 * around, or type, it's nice to be able to see
510 * the cursor immediately after the keypress. So
511 * we reset the toggle timeout and force the visibility
512 * of the cursor. Note we cannot do this inside
513 * dispatch() itself, because that's called recursively.
516 cursor_timeout.restart();
517 screen().showCursor(*bv_);
522 void BufferView::Pimpl::selectionRequested()
529 LCursor & cur = bv_->cursor();
531 if (!cur.selection()) {
532 xsel_cache_.set = false;
536 if (!xsel_cache_.set ||
537 cur.back() != xsel_cache_.cursor ||
538 cur.anchor_.back() != xsel_cache_.anchor)
540 xsel_cache_.cursor = cur.back();
541 xsel_cache_.anchor = cur.anchor_.back();
542 xsel_cache_.set = cur.selection();
543 sel = cur.selectionAsString(false);
545 workarea().putClipboard(sel);
550 void BufferView::Pimpl::selectionLost()
553 screen().hideCursor();
554 bv_->cursor().clearSelection();
555 xsel_cache_.set = false;
560 void BufferView::Pimpl::workAreaResize()
562 static int work_area_width;
563 static int work_area_height;
565 bool const widthChange = workarea().workWidth() != work_area_width;
566 bool const heightChange = workarea().workHeight() != work_area_height;
568 // update from work area
569 work_area_width = workarea().workWidth();
570 work_area_height = workarea().workHeight();
572 if (buffer_ && widthChange) {
573 // The visible LyXView need a resize
574 resizeCurrentBuffer();
577 if (widthChange || heightChange)
580 // always make sure that the scrollbar is sane.
582 owner_->updateLayoutChoice();
586 bool BufferView::Pimpl::fitCursor()
588 if (bv_funcs::status(bv_, bv_->cursor()) == bv_funcs::CUR_INSIDE) {
590 bv_->cursor().getDim(asc, des);
591 Point p = bv_funcs::getPos(bv_->cursor());
592 if (p.y_ - asc >= 0 && p.y_ + des < bv_->workHeight())
600 void BufferView::Pimpl::update(bool fitcursor, bool forceupdate)
602 lyxerr << "BufferView::Pimpl::update(fc=" << fitcursor << ", fu="
603 << forceupdate << ") buffer: " << buffer_ << endl;
605 // check needed to survive LyX startup
607 // update macro store
608 buffer_->buildMacros();
609 // first drawing step
612 std::swap(theCoords, backup);
614 ViewMetricsInfo vi = metrics();
616 if (fitcursor && fitCursor()) {
621 // second drawing step
622 screen().redraw(*bv_, vi);
624 std::swap(theCoords, backup);
630 bv_->owner()->view_state_changed();
634 // Callback for cursor timer
635 void BufferView::Pimpl::cursorToggle()
638 screen().toggleCursor(*bv_);
640 // Use this opportunity to deal with any child processes that
641 // have finished but are waiting to communicate this fact
642 // to the rest of LyX.
643 ForkedcallsController & fcc = ForkedcallsController::get();
644 if (fcc.processesCompleted())
645 fcc.handleCompletedProcesses();
648 cursor_timeout.restart();
652 bool BufferView::Pimpl::available() const
654 return buffer_ && bv_->text();
658 Change const BufferView::Pimpl::getCurrentChange()
660 if (!bv_->buffer()->params().tracking_changes)
661 return Change(Change::UNCHANGED);
663 LyXText * text = bv_->getLyXText();
664 LCursor & cur = bv_->cursor();
666 if (!cur.selection())
667 return Change(Change::UNCHANGED);
669 return text->getPar(cur.selBegin().pit()).
670 lookupChangeFull(cur.selBegin().pos());
674 void BufferView::Pimpl::savePosition(unsigned int i)
676 if (i >= saved_positions_num)
678 BOOST_ASSERT(bv_->cursor().inTexted());
679 saved_positions[i] = Position(buffer_->fileName(),
680 bv_->cursor().paragraph().id(),
681 bv_->cursor().pos());
683 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
687 void BufferView::Pimpl::restorePosition(unsigned int i)
689 if (i >= saved_positions_num)
692 string const fname = saved_positions[i].filename;
694 bv_->cursor().clearSelection();
696 if (fname != buffer_->fileName()) {
698 if (bufferlist.exists(fname))
699 b = bufferlist.getBuffer(fname);
701 b = bufferlist.newBuffer(fname);
702 ::loadLyXFile(b, fname); // don't ask, just load it
708 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
709 if (par == buffer_->par_iterator_end())
712 bv_->text()->setCursor(bv_->cursor(), par.pit(),
713 min(par->size(), saved_positions[i].par_pos));
716 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
720 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
722 return i < saved_positions_num && !saved_positions[i].filename.empty();
726 void BufferView::Pimpl::switchKeyMap()
728 if (!lyxrc.rtl_support)
731 Intl & intl = owner_->getIntl();
732 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
733 if (intl.keymap == Intl::PRIMARY)
736 if (intl.keymap == Intl::SECONDARY)
742 void BufferView::Pimpl::center()
744 CursorSlice const & bot = bv_->cursor().bottom();
745 lyx::pit_type const pit = bot.pit();
746 bot.text()->redoParagraph(pit);
747 Paragraph const & par = bot.text()->paragraphs()[pit];
749 offset_ref_ = bv_funcs::coordOffset(bv_->cursor()).y_ + par.ascent()
750 - workarea().workHeight() / 2;
754 void BufferView::Pimpl::stuffClipboard(string const & content) const
756 workarea().putClipboard(content);
760 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
762 string filename = filenm;
764 if (filename.empty()) {
765 // Launch a file browser
766 string initpath = lyxrc.document_path;
769 string const trypath = owner_->buffer()->filePath();
770 // If directory is writeable, use this as default.
771 if (IsDirWriteable(trypath))
775 FileDialog fileDlg(_("Select LyX document to insert"),
777 make_pair(string(_("Documents|#o#O")),
778 string(lyxrc.document_path)),
779 make_pair(string(_("Examples|#E#e")),
780 string(AddPath(system_lyxdir(), "examples"))));
782 FileDialog::Result result =
783 fileDlg.open(initpath,
784 FileFilterList(_("LyX Documents (*.lyx)")),
787 if (result.first == FileDialog::Later)
790 filename = result.second;
792 // check selected filename
793 if (filename.empty()) {
794 owner_->message(_("Canceled."));
799 // get absolute path of file and add ".lyx" to the filename if
801 filename = FileSearch(string(), filename, "lyx");
803 string const disp_fn = MakeDisplayPath(filename);
804 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
806 bv_->cursor().clearSelection();
807 bv_->text()->breakParagraph(bv_->cursor());
809 BOOST_ASSERT(bv_->cursor().inTexted());
811 string const fname = MakeAbsPath(filename);
812 bool const res = bv_->buffer()->readFile(fname, bv_->cursor().pit());
815 string s = res ? _("Document %1$s inserted.")
816 : _("Could not insert document %1$s");
817 owner_->message(bformat(s, disp_fn));
821 void BufferView::Pimpl::trackChanges()
823 Buffer * buf = bv_->buffer();
824 bool const tracking = buf->params().tracking_changes;
827 for_each(buf->par_iterator_begin(),
828 buf->par_iterator_end(),
829 bind(&Paragraph::trackChanges, _1, Change::UNCHANGED));
830 buf->params().tracking_changes = true;
832 // we cannot allow undos beyond the freeze point
833 buf->undostack().clear();
836 bv_->text()->setCursor(bv_->cursor(), 0, 0);
838 #warning changes FIXME
840 bool found = lyx::find::findNextChange(bv_);
842 owner_->getDialogs().show("changes");
846 for_each(buf->par_iterator_begin(),
847 buf->par_iterator_end(),
848 mem_fun_ref(&Paragraph::untrackChanges));
850 buf->params().tracking_changes = false;
853 buf->redostack().clear();
857 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
859 lyxerr << "BufferView::Pimpl::workAreaDispatch: request: "
860 << cmd0 << std::endl;
861 // this is only called for mouse related events including
862 // LFUN_FILE_OPEN generated by drag-and-drop.
863 FuncRequest cmd = cmd0;
866 if (cmd.action == LFUN_FILE_OPEN) {
867 owner_->dispatch(cmd);
875 cur.push(bv_->buffer()->inset());
876 cur.selection() = bv_->cursor().selection();
878 // Doesn't go through lyxfunc, so we need to update
879 // the layout choice etc. ourselves
881 // e.g. Qt mouse press when no buffer
885 screen().hideCursor();
887 // Either the inset under the cursor or the
888 // surrounding LyXText will handle this event.
890 // Build temporary cursor.
891 cmd.y = min(max(cmd.y,-1), bv_->workHeight());
892 InsetBase * inset = bv_->text()->editXY(cur, cmd.x, cmd.y);
893 lyxerr << " * hit inset at tip: " << inset << endl;
894 lyxerr << " * created temp cursor:" << cur << endl;
896 // Put anchor at the same position.
899 // Try to dispatch to an non-editable inset near this position
900 // via the temp cursor. If the inset wishes to change the real
901 // cursor it has to do so explicitly by using
902 // cur.bv().cursor() = cur; (or similar)
904 inset->dispatch(cur, cmd);
906 // Now dispatch to the temporary cursor. If the real cursor should
907 // be modified, the inset's dispatch has to do so explicitly.
908 if (!cur.result().dispatched())
911 if (cur.result().dispatched()) {
912 // Redraw if requested or necessary.
913 update(cur.result().update(), cur.result().update());
916 // see workAreaKeyPress
917 cursor_timeout.restart();
918 screen().showCursor(*bv_);
920 // skip these when selecting
921 if (cmd.action != LFUN_MOUSE_MOTION) {
922 owner_->updateLayoutChoice();
923 owner_->updateToolbars();
926 // slight hack: this is only called currently when we
927 // clicked somewhere, so we force through the display
928 // of the new status here.
929 owner_->clearMessage();
934 FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd)
936 Buffer * buf = bv_->buffer();
940 switch (cmd.action) {
943 flag.enabled(!buf->undostack().empty());
946 flag.enabled(!buf->redostack().empty());
948 case LFUN_FILE_INSERT:
949 case LFUN_FILE_INSERT_ASCII_PARA:
950 case LFUN_FILE_INSERT_ASCII:
951 case LFUN_FONT_STATE:
952 case LFUN_INSERT_LABEL:
953 case LFUN_BOOKMARK_SAVE:
956 case LFUN_WORD_REPLACE:
961 case LFUN_WORDS_COUNT:
965 case LFUN_BOOKMARK_GOTO:
966 flag.enabled(bv_->isSavedPosition(strToUnsignedInt(cmd.argument)));
968 case LFUN_TRACK_CHANGES:
970 flag.setOnOff(buf->params().tracking_changes);
973 case LFUN_MERGE_CHANGES:
974 case LFUN_ACCEPT_CHANGE: // what about these two
975 case LFUN_REJECT_CHANGE: // what about these two
976 case LFUN_ACCEPT_ALL_CHANGES:
977 case LFUN_REJECT_ALL_CHANGES:
978 flag.enabled(buf && buf->params().tracking_changes);
989 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
991 //lyxerr << "BufferView::Pimpl::dispatch cmd: " << cmd << std::endl;
992 // Make sure that the cached BufferView is correct.
993 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
994 << " action[" << cmd.action << ']'
995 << " arg[" << cmd.argument << ']'
996 << " x[" << cmd.x << ']'
997 << " y[" << cmd.y << ']'
998 << " button[" << cmd.button() << ']'
1001 LCursor & cur = bv_->cursor();
1003 switch (cmd.action) {
1007 cur.message(_("Undo"));
1008 cur.clearSelection();
1009 if (!textUndo(*bv_))
1010 cur.message(_("No further undo information"));
1018 cur.message(_("Redo"));
1019 cur.clearSelection();
1020 if (!textRedo(*bv_))
1021 cur.message(_("No further redo information"));
1027 case LFUN_FILE_INSERT:
1028 MenuInsertLyXFile(cmd.argument);
1031 case LFUN_FILE_INSERT_ASCII_PARA:
1032 InsertAsciiFile(bv_, cmd.argument, true);
1035 case LFUN_FILE_INSERT_ASCII:
1036 InsertAsciiFile(bv_, cmd.argument, false);
1039 case LFUN_FONT_STATE:
1040 cur.message(cur.currentState());
1043 case LFUN_BOOKMARK_SAVE:
1044 savePosition(strToUnsignedInt(cmd.argument));
1047 case LFUN_BOOKMARK_GOTO:
1048 restorePosition(strToUnsignedInt(cmd.argument));
1051 case LFUN_REF_GOTO: {
1052 string label = cmd.argument;
1053 if (label.empty()) {
1055 getInsetByCode<InsetRef>(bv_->cursor(),
1056 InsetBase::REF_CODE);
1058 label = inset->getContents();
1064 bv_->gotoLabel(label);
1068 case LFUN_TRACK_CHANGES:
1072 case LFUN_MERGE_CHANGES:
1073 owner_->getDialogs().show("changes");
1076 case LFUN_ACCEPT_ALL_CHANGES: {
1077 bv_->cursor().reset(bv_->buffer()->inset());
1078 #ifdef WITH_WARNINGS
1079 #warning FIXME changes
1081 while (lyx::find::findNextChange(bv_))
1082 bv_->getLyXText()->acceptChange(bv_->cursor());
1087 case LFUN_REJECT_ALL_CHANGES: {
1088 bv_->cursor().reset(bv_->buffer()->inset());
1089 #ifdef WITH_WARNINGS
1090 #warning FIXME changes
1092 while (lyx::find::findNextChange(bv_))
1093 bv_->getLyXText()->rejectChange(bv_->cursor());
1097 case LFUN_WORD_FIND:
1098 lyx::find::find(bv_, cmd);
1101 case LFUN_WORD_REPLACE:
1102 lyx::find::replace(bv_, cmd);
1106 cur.clearSelection();
1108 cur.message(N_("Mark off"));
1112 cur.clearSelection();
1115 cur.message(N_("Mark on"));
1119 cur.clearSelection();
1122 cur.message(N_("Mark removed"));
1125 cur.message(N_("Mark set"));
1134 case LFUN_WORDS_COUNT: {
1135 DocIterator from, to;
1136 if (cur.selection()) {
1137 from = cur.selectionBegin();
1138 to = cur.selectionEnd();
1140 from = doc_iterator_begin(bv_->buffer()->inset());
1141 to = doc_iterator_end(bv_->buffer()->inset());
1143 int const count = countWords(from, to);
1146 if (cur.selection())
1147 message = bformat(_("%1$s words in selection."),
1150 message = bformat(_("%1$s words in document."),
1154 if (cur.selection())
1155 message = _("One word in selection.");
1157 message = _("One word in document.");
1160 Alert::information(_("Count words"), message);
1171 ViewMetricsInfo BufferView::Pimpl::metrics()
1173 // remove old position cache
1175 BufferView & bv = *bv_;
1176 LyXText * const text = bv.text();
1177 if (anchor_ref_ > int(text->paragraphs().size() - 1)) {
1178 anchor_ref_ = int(text->paragraphs().size() - 1);
1182 lyx::pit_type const pit = anchor_ref_;
1185 size_t npit = text->paragraphs().size();
1186 lyxerr << "npit: " << npit << " pit1: " << pit1
1187 << " pit2: " << pit2 << endl;
1189 // rebreak anchor par
1190 text->redoParagraph(pit);
1191 int y0 = text->getPar(pit1).ascent() - offset_ref_;
1193 // redo paragraphs above cursor if necessary
1195 while (y1 > 0 && pit1 > 0) {
1196 y1 -= text->getPar(pit1).ascent();
1198 text->redoParagraph(pit1);
1199 y1 -= text->getPar(pit1).descent();
1203 // take care of ascent of first line
1204 y1 -= text->getPar(pit1).ascent();
1206 //normalize anchor for next time
1210 // grey at the beginning is ugly
1211 if (pit1 == 0 && y1 > 0) {
1217 // redo paragraphs below cursor if necessary
1219 while (y2 < bv.workHeight() && pit2 < int(npit) - 1) {
1220 y2 += text->getPar(pit2).descent();
1222 text->redoParagraph(pit2);
1223 y2 += text->getPar(pit2).ascent();
1226 // take care of descent of last line
1227 y2 += text->getPar(pit2).descent();
1229 // the coordinates of all these paragraphs are correct, cache them
1231 for (lyx::pit_type pit = pit1; pit <= pit2; ++pit) {
1232 y += text->getPar(pit).ascent();
1233 theCoords.pars_[text][pit] = Point(0, y);
1234 y += text->getPar(pit).descent();
1237 lyxerr << "bv:metrics: y1: " << y1 << " y2: " << y2 << endl;
1238 return ViewMetricsInfo(pit1, pit2, y1, y2);