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 "LaTeXFeatures.h"
38 #include "lyx_cb.h" // added for Dispatch functions
44 #include "lastfiles.h"
45 #include "metricsinfo.h"
46 #include "paragraph.h"
47 #include "paragraph_funcs.h"
48 #include "ParagraphParameters.h"
49 #include "pariterator.h"
50 #include "rowpainter.h"
54 #include "insets/insetref.h"
55 #include "insets/insettext.h"
57 #include "frontends/Alert.h"
58 #include "frontends/Dialogs.h"
59 #include "frontends/FileDialog.h"
60 #include "frontends/font_metrics.h"
61 #include "frontends/LyXView.h"
62 #include "frontends/LyXScreenFactory.h"
63 #include "frontends/screen.h"
64 #include "frontends/WorkArea.h"
65 #include "frontends/WorkAreaFactory.h"
67 #include "graphics/Previews.h"
69 #include "support/convert.h"
70 #include "support/filefilterlist.h"
71 #include "support/filetools.h"
72 #include "support/forkedcontr.h"
73 #include "support/package.h"
74 #include "support/types.h"
76 #include <boost/bind.hpp>
77 #include <boost/current_function.hpp>
84 using lyx::support::AddPath;
85 using lyx::support::bformat;
86 using lyx::support::FileFilterList;
87 using lyx::support::FileSearch;
88 using lyx::support::ForkedcallsController;
89 using lyx::support::IsDirWriteable;
90 using lyx::support::MakeDisplayPath;
91 using lyx::support::MakeAbsPath;
92 using lyx::support::package;
95 using std::istringstream;
100 using std::mem_fun_ref;
104 extern BufferList bufferlist;
109 unsigned int const saved_positions_num = 20;
111 // All the below connection objects are needed because of a bug in some
112 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
113 // to these connections we avoid a segfault upon startup, and also at exit.
116 boost::signals::connection dispatchcon;
117 boost::signals::connection timecon;
118 boost::signals::connection doccon;
119 boost::signals::connection resizecon;
120 boost::signals::connection kpresscon;
121 boost::signals::connection selectioncon;
122 boost::signals::connection lostcon;
125 /// Get next inset of this class from current cursor position
127 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
130 DocIterator it = cur;
131 if (it.nextInset() &&
132 it.nextInset()->lyxCode() == code) {
133 inset = static_cast<T*>(it.nextInset());
141 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
142 int width, int height)
143 : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
144 using_xterm_cursor(false), cursor_(bv) ,
145 anchor_ref_(0), offset_ref_(0)
147 xsel_cache_.set = false;
149 workarea_.reset(WorkAreaFactory::create(*owner_, width, height));
150 screen_.reset(LyXScreenFactory::create(workarea()));
153 doccon = workarea().scrollDocView
154 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
155 resizecon = workarea().workAreaResize
156 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
157 dispatchcon = workarea().dispatch
158 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
159 kpresscon = workarea().workAreaKeyPress
160 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
161 selectioncon = workarea().selectionRequested
162 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
163 lostcon = workarea().selectionLost
164 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
166 timecon = cursor_timeout.timeout
167 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
168 cursor_timeout.start();
169 saved_positions.resize(saved_positions_num);
173 void BufferView::Pimpl::addError(ErrorItem const & ei)
175 errorlist_.push_back(ei);
179 void BufferView::Pimpl::showReadonly(bool)
181 owner_->updateWindowTitle();
182 owner_->getDialogs().updateBufferDependent(false);
186 void BufferView::Pimpl::connectBuffer(Buffer & buf)
188 if (errorConnection_.connected())
193 boost::bind(&BufferView::Pimpl::addError, this, _1));
197 boost::bind(&LyXView::message, owner_, _1));
201 boost::bind(&LyXView::busy, owner_, _1));
204 buf.updateTitles.connect(
205 boost::bind(&LyXView::updateWindowTitle, owner_));
208 buf.resetAutosaveTimers.connect(
209 boost::bind(&LyXView::resetAutosaveTimer, owner_));
211 readonlyConnection_ =
212 buf.readonly.connect(
213 boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
217 boost::bind(&BufferView::Pimpl::setBuffer, this, (Buffer *)0));
221 void BufferView::Pimpl::disconnectBuffer()
223 errorConnection_.disconnect();
224 messageConnection_.disconnect();
225 busyConnection_.disconnect();
226 titleConnection_.disconnect();
227 timerConnection_.disconnect();
228 readonlyConnection_.disconnect();
229 closingConnection_.disconnect();
233 void BufferView::Pimpl::newFile(string const & filename, string const & tname,
236 setBuffer(::newFile(filename, tname, isNamed));
240 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
242 // Get absolute path of file and add ".lyx"
243 // to the filename if necessary
244 string s = FileSearch(string(), filename, "lyx");
246 bool const found = !s.empty();
251 // File already open?
252 if (bufferlist.exists(s)) {
253 string const file = MakeDisplayPath(s, 20);
254 string text = bformat(_("The document %1$s is already "
255 "loaded.\n\nDo you want to revert "
256 "to the saved version?"), file);
257 int const ret = Alert::prompt(_("Revert to saved document?"),
258 text, 0, 1, _("&Revert"), _("&Switch to document"));
261 setBuffer(bufferlist.getBuffer(s));
264 // FIXME: should be LFUN_REVERT
265 if (!bufferlist.close(bufferlist.getBuffer(s), false))
267 // Fall through to new load. (Asger)
273 b = bufferlist.newBuffer(s);
275 if (!::loadLyXFile(b, s)) {
276 bufferlist.release(b);
280 string text = bformat(_("The document %1$s does not yet "
281 "exist.\n\nDo you want to create "
282 "a new document?"), s);
283 int const ret = Alert::prompt(_("Create new document?"),
284 text, 0, 1, _("&Create"), _("Cancel"));
287 b = ::newFile(s, string(), true);
293 bv_->showErrorList(_("Parse"));
296 LyX::ref().lastfiles().newFile(b->fileName());
302 WorkArea & BufferView::Pimpl::workarea() const
304 return *workarea_.get();
308 LyXScreen & BufferView::Pimpl::screen() const
310 return *screen_.get();
314 Painter & BufferView::Pimpl::painter() const
316 return workarea().getPainter();
320 void BufferView::Pimpl::setBuffer(Buffer * b)
322 lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
323 << "[ b = " << b << "]" << endl;
328 // If we are closing current buffer, switch to the first in
331 lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
332 << " No Buffer!" << endl;
333 // We are closing the buffer, use the first buffer as current
334 buffer_ = bufferlist.first();
335 owner_->getDialogs().hideBufferDependent();
337 // Set current buffer
342 cursor_ = LCursor(*bv_);
347 // If we're quitting lyx, don't bother updating stuff
352 lyxerr[Debug::INFO] << BOOST_CURRENT_FUNCTION
353 << "Buffer addr: " << buffer_ << endl;
354 connectBuffer(*buffer_);
356 cursor_.push(buffer_->inset());
357 cursor_.resetAnchor();
358 buffer_->text().init(bv_);
359 buffer_->text().setCurrentFont(cursor_);
361 // Buffer-dependent dialogs should be updated or
362 // hidden. This should go here because some dialogs (eg ToC)
363 // require bv_->text.
364 owner_->getDialogs().updateBufferDependent(true);
369 owner_->updateMenubar();
370 owner_->updateToolbars();
371 owner_->updateLayoutChoice();
372 owner_->updateWindowTitle();
374 // This is done after the layout combox has been populated
376 owner_->setLayout(cursor_.paragraph().layout()->name());
378 if (buffer_ && lyx::graphics::Previews::status() != LyXRC::PREVIEW_OFF)
379 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
383 void BufferView::Pimpl::resizeCurrentBuffer()
385 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION << endl;
387 owner_->message(_("Formatting document..."));
389 LyXText * text = bv_->text();
399 // Reset the "Formatting..." message
400 owner_->clearMessage();
406 void BufferView::Pimpl::updateScrollbar()
409 lyxerr[Debug::DEBUG] << BOOST_CURRENT_FUNCTION
410 << " no text in updateScrollbar" << endl;
411 workarea().setScrollbarParams(0, 0, 0);
415 LyXText & t = *bv_->text();
416 if (anchor_ref_ > int(t.paragraphs().size()) - 1) {
417 anchor_ref_ = int(t.paragraphs().size()) - 1;
422 << BOOST_CURRENT_FUNCTION
423 << " Updating scrollbar: height: " << t.paragraphs().size()
424 << " curr par: " << cursor_.bottom().pit()
425 << " default height " << defaultRowHeight() << endl;
427 // It would be better to fix the scrollbar to understand
428 // values in [0..1] and divide everything by wh
429 int const wh = workarea().workHeight() / 4;
430 int const h = t.getPar(anchor_ref_).height();
431 workarea().setScrollbarParams(t.paragraphs().size() * wh, anchor_ref_ * wh + int(offset_ref_ * wh / float(h)), int (wh * defaultRowHeight() / float(h)));
432 // workarea().setScrollbarParams(t.paragraphs().size(), anchor_ref_, 1);
436 void BufferView::Pimpl::scrollDocView(int value)
438 lyxerr[Debug::GUI] << BOOST_CURRENT_FUNCTION
439 << "[ value = " << value << "]" << endl;
444 screen().hideCursor();
446 int const wh = workarea().workHeight() / 4;
448 LyXText & t = *bv_->text();
450 float const bar = value / float(wh * t.paragraphs().size());
452 anchor_ref_ = int(bar * t.paragraphs().size());
453 t.redoParagraph(anchor_ref_);
454 int const h = t.getPar(anchor_ref_).height();
455 offset_ref_ = int((bar * t.paragraphs().size() - anchor_ref_) * h);
458 if (!lyxrc.cursor_follows_scrollbar)
461 int const height = 2 * defaultRowHeight();
462 int const first = height;
463 int const last = workarea().workHeight() - height;
464 LCursor & cur = cursor_;
466 bv_funcs::CurStatus st = bv_funcs::status(bv_, cur);
469 case bv_funcs::CUR_ABOVE:
470 t.setCursorFromCoordinates(cur, 0, first);
471 cur.clearSelection();
473 case bv_funcs::CUR_BELOW:
474 t.setCursorFromCoordinates(cur, 0, last);
475 cur.clearSelection();
477 case bv_funcs::CUR_INSIDE:
478 int const y = bv_funcs::getPos(cur).y_;
479 int const newy = min(last, max(y, first));
481 cur.reset(buffer_->inset());
482 t.setCursorFromCoordinates(cur, 0, newy);
485 owner_->updateLayoutChoice();
489 void BufferView::Pimpl::scroll(int /*lines*/)
494 // LyXText const * t = bv_->text();
495 // int const line_height = defaultRowHeight();
497 // // The new absolute coordinate
498 // int new_top_y = top_y() + lines * line_height;
500 // // Restrict to a valid value
501 // new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
502 // new_top_y = std::max(0, new_top_y);
504 // scrollDocView(new_top_y);
506 // // Update the scrollbar.
507 // workarea().setScrollbarParams(t->height(), top_y(), defaultRowHeight());
511 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
512 key_modifier::state state)
514 owner_->getLyXFunc().processKeySym(key, state);
516 /* This is perhaps a bit of a hack. When we move
517 * around, or type, it's nice to be able to see
518 * the cursor immediately after the keypress. So
519 * we reset the toggle timeout and force the visibility
520 * of the cursor. Note we cannot do this inside
521 * dispatch() itself, because that's called recursively.
524 cursor_timeout.restart();
525 screen().showCursor(*bv_);
530 void BufferView::Pimpl::selectionRequested()
537 LCursor & cur = cursor_;
539 if (!cur.selection()) {
540 xsel_cache_.set = false;
544 if (!xsel_cache_.set ||
545 cur.top() != xsel_cache_.cursor ||
546 cur.anchor_.top() != xsel_cache_.anchor)
548 xsel_cache_.cursor = cur.top();
549 xsel_cache_.anchor = cur.anchor_.top();
550 xsel_cache_.set = cur.selection();
551 sel = cur.selectionAsString(false);
553 workarea().putClipboard(sel);
558 void BufferView::Pimpl::selectionLost()
561 screen().hideCursor();
562 cursor_.clearSelection();
563 xsel_cache_.set = false;
568 void BufferView::Pimpl::workAreaResize()
570 static int work_area_width;
571 static int work_area_height;
573 bool const widthChange = workarea().workWidth() != work_area_width;
574 bool const heightChange = workarea().workHeight() != work_area_height;
576 // Update from work area
577 work_area_width = workarea().workWidth();
578 work_area_height = workarea().workHeight();
580 if (buffer_ && widthChange) {
581 // The visible LyXView need a resize
582 resizeCurrentBuffer();
585 if (widthChange || heightChange)
588 // Always make sure that the scrollbar is sane.
590 owner_->updateLayoutChoice();
594 bool BufferView::Pimpl::fitCursor()
596 if (bv_funcs::status(bv_, cursor_) == bv_funcs::CUR_INSIDE) {
597 LyXFont const font = cursor_.getFont();
598 int const asc = font_metrics::maxAscent(font);
599 int const des = font_metrics::maxDescent(font);
600 Point p = bv_funcs::getPos(cursor_);
601 if (p.y_ - asc >= 0 && p.y_ + des < workarea().workHeight())
609 void BufferView::Pimpl::update(bool fitcursor, bool forceupdate)
611 lyxerr << BOOST_CURRENT_FUNCTION
612 << "[fitcursor = " << fitcursor << ','
613 << " forceupdate = " << forceupdate
614 << "] buffer: " << buffer_ << endl;
616 // Check needed to survive LyX startup
618 // Update macro store
619 buffer_->buildMacros();
620 // First drawing step
623 std::swap(theCoords, backup);
624 theCoords.startUpdating();
626 ViewMetricsInfo vi = metrics();
628 if (fitcursor && fitCursor()) {
633 // Second drawing step
634 screen().redraw(*bv_, vi);
636 // Abort updating of the coord cache - just restore the old one
637 std::swap(theCoords, backup);
644 owner_->view_state_changed();
648 // Callback for cursor timer
649 void BufferView::Pimpl::cursorToggle()
652 screen().toggleCursor(*bv_);
654 // Use this opportunity to deal with any child processes that
655 // have finished but are waiting to communicate this fact
656 // to the rest of LyX.
657 ForkedcallsController & fcc = ForkedcallsController::get();
658 if (fcc.processesCompleted())
659 fcc.handleCompletedProcesses();
662 cursor_timeout.restart();
666 bool BufferView::Pimpl::available() const
668 return buffer_ && bv_->text();
672 Change const BufferView::Pimpl::getCurrentChange()
674 if (!buffer_->params().tracking_changes)
675 return Change(Change::UNCHANGED);
677 LyXText * text = bv_->getLyXText();
678 LCursor & cur = cursor_;
680 if (!cur.selection())
681 return Change(Change::UNCHANGED);
683 return text->getPar(cur.selBegin().pit()).
684 lookupChangeFull(cur.selBegin().pos());
688 void BufferView::Pimpl::savePosition(unsigned int i)
690 if (i >= saved_positions_num)
692 BOOST_ASSERT(cursor_.inTexted());
693 saved_positions[i] = Position(buffer_->fileName(),
694 cursor_.paragraph().id(),
697 owner_->message(bformat(_("Saved bookmark %1$d"), i));
701 void BufferView::Pimpl::restorePosition(unsigned int i)
703 if (i >= saved_positions_num)
706 string const fname = saved_positions[i].filename;
708 cursor_.clearSelection();
710 if (fname != buffer_->fileName()) {
712 if (bufferlist.exists(fname))
713 b = bufferlist.getBuffer(fname);
715 b = bufferlist.newBuffer(fname);
716 // Don't ask, just load it
717 ::loadLyXFile(b, fname);
723 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
724 if (par == buffer_->par_iterator_end())
727 bv_->setCursor(makeDocIterator(par, min(par->size(), saved_positions[i].par_pos)));
730 owner_->message(bformat(_("Moved to bookmark %1$d"), i));
734 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
736 return i < saved_positions_num && !saved_positions[i].filename.empty();
740 void BufferView::Pimpl::switchKeyMap()
742 if (!lyxrc.rtl_support)
745 Intl & intl = owner_->getIntl();
746 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
747 if (intl.keymap == Intl::PRIMARY)
750 if (intl.keymap == Intl::SECONDARY)
756 void BufferView::Pimpl::center()
758 CursorSlice & bot = cursor_.bottom();
759 lyx::pit_type const pit = bot.pit();
760 bot.text()->redoParagraph(pit);
761 Paragraph const & par = bot.text()->paragraphs()[pit];
763 offset_ref_ = bv_funcs::coordOffset(cursor_).y_ + par.ascent()
764 - workarea().workHeight() / 2;
768 void BufferView::Pimpl::stuffClipboard(string const & content) const
770 workarea().putClipboard(content);
774 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
776 string filename = filenm;
778 if (filename.empty()) {
779 // Launch a file browser
780 string initpath = lyxrc.document_path;
783 string const trypath = owner_->buffer()->filePath();
784 // If directory is writeable, use this as default.
785 if (IsDirWriteable(trypath))
789 FileDialog fileDlg(_("Select LyX document to insert"),
791 make_pair(string(_("Documents|#o#O")),
792 string(lyxrc.document_path)),
793 make_pair(string(_("Examples|#E#e")),
794 string(AddPath(package().system_support(), "examples"))));
796 FileDialog::Result result =
797 fileDlg.open(initpath,
798 FileFilterList(_("LyX Documents (*.lyx)")),
801 if (result.first == FileDialog::Later)
804 filename = result.second;
806 // check selected filename
807 if (filename.empty()) {
808 owner_->message(_("Canceled."));
813 // Get absolute path of file and add ".lyx"
814 // to the filename if necessary
815 filename = FileSearch(string(), filename, "lyx");
817 string const disp_fn = MakeDisplayPath(filename);
818 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
820 cursor_.clearSelection();
821 bv_->getLyXText()->breakParagraph(cursor_);
823 BOOST_ASSERT(cursor_.inTexted());
825 string const fname = MakeAbsPath(filename);
826 bool const res = buffer_->readFile(fname, cursor_.pit());
827 resizeCurrentBuffer();
829 string s = res ? _("Document %1$s inserted.")
830 : _("Could not insert document %1$s");
831 owner_->message(bformat(s, disp_fn));
835 void BufferView::Pimpl::trackChanges()
837 bool const tracking = buffer_->params().tracking_changes;
840 for_each(buffer_->par_iterator_begin(),
841 buffer_->par_iterator_end(),
842 bind(&Paragraph::trackChanges, _1, Change::UNCHANGED));
843 buffer_->params().tracking_changes = true;
845 // We cannot allow undos beyond the freeze point
846 buffer_->undostack().clear();
849 bv_->text()->setCursor(cursor_, 0, 0);
851 #warning changes FIXME
853 bool const found = lyx::find::findNextChange(bv_);
855 owner_->getDialogs().show("changes");
859 for_each(buffer_->par_iterator_begin(),
860 buffer_->par_iterator_end(),
861 mem_fun_ref(&Paragraph::untrackChanges));
863 buffer_->params().tracking_changes = false;
866 buffer_->redostack().clear();
870 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
872 lyxerr << BOOST_CURRENT_FUNCTION
873 << "[ cmd0 " << cmd0 << "]" << endl;
875 // This is only called for mouse related events including
876 // LFUN_FILE_OPEN generated by drag-and-drop.
877 FuncRequest cmd = cmd0;
880 if (cmd.action == LFUN_FILE_OPEN) {
881 owner_->dispatch(cmd);
889 cur.push(buffer_->inset());
890 cur.selection() = cursor_.selection();
892 // Doesn't go through lyxfunc, so we need to update
893 // the layout choice etc. ourselves
895 // E.g. Qt mouse press when no buffer
899 screen().hideCursor();
901 // Either the inset under the cursor or the
902 // surrounding LyXText will handle this event.
904 // Build temporary cursor.
905 cmd.y = min(max(cmd.y,-1), workarea().workHeight());
906 InsetBase * inset = bv_->text()->editXY(cur, cmd.x, cmd.y);
907 lyxerr << BOOST_CURRENT_FUNCTION
908 << " * hit inset at tip: " << inset << endl;
909 lyxerr << BOOST_CURRENT_FUNCTION
910 << " * created temp cursor:" << cur << endl;
912 // Put anchor at the same position.
915 // Try to dispatch to an non-editable inset near this position
916 // via the temp cursor. If the inset wishes to change the real
917 // cursor it has to do so explicitly by using
918 // cur.bv().cursor() = cur; (or similar)
920 inset->dispatch(cur, cmd);
922 // Now dispatch to the temporary cursor. If the real cursor should
923 // be modified, the inset's dispatch has to do so explicitly.
924 if (!cur.result().dispatched())
927 if (cur.result().dispatched()) {
928 // Redraw if requested or necessary.
929 update(cur.result().update(), cur.result().update());
932 // See workAreaKeyPress
933 cursor_timeout.restart();
934 screen().showCursor(*bv_);
936 // Skip these when selecting
937 if (cmd.action != LFUN_MOUSE_MOTION) {
938 owner_->updateLayoutChoice();
939 owner_->updateToolbars();
942 // Slight hack: this is only called currently when we
943 // clicked somewhere, so we force through the display
944 // of the new status here.
945 owner_->clearMessage();
950 FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd)
954 switch (cmd.action) {
957 flag.enabled(!buffer_->undostack().empty());
960 flag.enabled(!buffer_->redostack().empty());
962 case LFUN_FILE_INSERT:
963 case LFUN_FILE_INSERT_ASCII_PARA:
964 case LFUN_FILE_INSERT_ASCII:
965 case LFUN_FONT_STATE:
966 case LFUN_INSERT_LABEL:
967 case LFUN_BOOKMARK_SAVE:
969 case LFUN_GOTO_PARAGRAPH:
972 case LFUN_REFERENCE_GOTO:
974 case LFUN_WORD_REPLACE:
979 case LFUN_WORDS_COUNT:
983 case LFUN_BOOKMARK_GOTO:
984 flag.enabled(isSavedPosition(convert<unsigned int>(cmd.argument)));
986 case LFUN_TRACK_CHANGES:
988 flag.setOnOff(buffer_->params().tracking_changes);
991 case LFUN_OUTPUT_CHANGES: {
992 LaTeXFeatures features(*buffer_, buffer_->params(), false);
993 flag.enabled(buffer_ && buffer_->params().tracking_changes
994 && features.isAvailable("dvipost"));
995 flag.setOnOff(buffer_->params().output_changes);
999 case LFUN_MERGE_CHANGES:
1000 case LFUN_ACCEPT_CHANGE: // what about these two
1001 case LFUN_REJECT_CHANGE: // what about these two
1002 case LFUN_ACCEPT_ALL_CHANGES:
1003 case LFUN_REJECT_ALL_CHANGES:
1004 flag.enabled(buffer_ && buffer_->params().tracking_changes);
1007 flag.enabled(false);
1015 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
1017 //lyxerr << BOOST_CURRENT_FUNCTION
1018 // << [ cmd = " << cmd << "]" << endl;
1020 // Make sure that the cached BufferView is correct.
1021 lyxerr[Debug::ACTION] << BOOST_CURRENT_FUNCTION
1022 << " action[" << cmd.action << ']'
1023 << " arg[" << cmd.argument << ']'
1024 << " x[" << cmd.x << ']'
1025 << " y[" << cmd.y << ']'
1026 << " button[" << cmd.button() << ']'
1029 LCursor & cur = cursor_;
1031 switch (cmd.action) {
1035 cur.message(_("Undo"));
1036 cur.clearSelection();
1037 if (!textUndo(*bv_))
1038 cur.message(_("No further undo information"));
1046 cur.message(_("Redo"));
1047 cur.clearSelection();
1048 if (!textRedo(*bv_))
1049 cur.message(_("No further redo information"));
1055 case LFUN_FILE_INSERT:
1056 MenuInsertLyXFile(cmd.argument);
1059 case LFUN_FILE_INSERT_ASCII_PARA:
1060 InsertAsciiFile(bv_, cmd.argument, true);
1063 case LFUN_FILE_INSERT_ASCII:
1064 InsertAsciiFile(bv_, cmd.argument, false);
1067 case LFUN_FONT_STATE:
1068 cur.message(cur.currentState());
1071 case LFUN_BOOKMARK_SAVE:
1072 savePosition(convert<unsigned int>(cmd.argument));
1075 case LFUN_BOOKMARK_GOTO:
1076 restorePosition(convert<unsigned int>(cmd.argument));
1079 case LFUN_REF_GOTO: {
1080 string label = cmd.argument;
1081 if (label.empty()) {
1083 getInsetByCode<InsetRef>(cursor_,
1084 InsetBase::REF_CODE);
1086 label = inset->getContents();
1092 bv_->gotoLabel(label);
1096 case LFUN_GOTO_PARAGRAPH: {
1097 int const id = convert<int>(cmd.argument);
1098 ParIterator par = buffer_->getParFromID(id);
1099 if (par == buffer_->par_iterator_end()) {
1100 lyxerr[Debug::INFO] << "No matching paragraph found! ["
1101 << id << ']' << endl;
1104 lyxerr[Debug::INFO] << "Paragraph " << par->id()
1105 << " found." << endl;
1109 bv_->setCursor(makeDocIterator(par, 0));
1116 case LFUN_GOTOERROR:
1117 bv_funcs::gotoInset(bv_, InsetBase::ERROR_CODE, false);
1121 bv_funcs::gotoInset(bv_, InsetBase::NOTE_CODE, false);
1124 case LFUN_REFERENCE_GOTO: {
1125 vector<InsetBase_code> tmp;
1126 tmp.push_back(InsetBase::LABEL_CODE);
1127 tmp.push_back(InsetBase::REF_CODE);
1128 bv_funcs::gotoInset(bv_, tmp, true);
1132 case LFUN_TRACK_CHANGES:
1136 case LFUN_OUTPUT_CHANGES: {
1137 bool const state = buffer_->params().output_changes;
1138 buffer_->params().output_changes = !state;
1142 case LFUN_MERGE_CHANGES:
1143 owner_->getDialogs().show("changes");
1146 case LFUN_ACCEPT_ALL_CHANGES: {
1147 cursor_.reset(buffer_->inset());
1148 #ifdef WITH_WARNINGS
1149 #warning FIXME changes
1151 while (lyx::find::findNextChange(bv_))
1152 bv_->getLyXText()->acceptChange(cursor_);
1157 case LFUN_REJECT_ALL_CHANGES: {
1158 cursor_.reset(buffer_->inset());
1159 #ifdef WITH_WARNINGS
1160 #warning FIXME changes
1162 while (lyx::find::findNextChange(bv_))
1163 bv_->getLyXText()->rejectChange(cursor_);
1167 case LFUN_WORD_FIND:
1168 lyx::find::find(bv_, cmd);
1171 case LFUN_WORD_REPLACE:
1172 lyx::find::replace(bv_, cmd);
1176 cur.clearSelection();
1178 cur.message(N_("Mark off"));
1182 cur.clearSelection();
1185 cur.message(N_("Mark on"));
1189 cur.clearSelection();
1192 cur.message(N_("Mark removed"));
1195 cur.message(N_("Mark set"));
1204 case LFUN_WORDS_COUNT: {
1205 DocIterator from, to;
1206 if (cur.selection()) {
1207 from = cur.selectionBegin();
1208 to = cur.selectionEnd();
1210 from = doc_iterator_begin(buffer_->inset());
1211 to = doc_iterator_end(buffer_->inset());
1213 int const count = countWords(from, to);
1216 if (cur.selection())
1217 message = bformat(_("%1$d words in selection."),
1220 message = bformat(_("%1$d words in document."),
1224 if (cur.selection())
1225 message = _("One word in selection.");
1227 message = _("One word in document.");
1230 Alert::information(_("Count words"), message);
1241 ViewMetricsInfo BufferView::Pimpl::metrics()
1243 // Remove old position cache
1245 BufferView & bv = *bv_;
1246 LyXText * const text = bv.text();
1247 if (anchor_ref_ > int(text->paragraphs().size() - 1)) {
1248 anchor_ref_ = int(text->paragraphs().size() - 1);
1252 lyx::pit_type const pit = anchor_ref_;
1255 size_t const npit = text->paragraphs().size();
1257 lyxerr << BOOST_CURRENT_FUNCTION
1258 << " npit: " << npit
1259 << " pit1: " << pit1
1260 << " pit2: " << pit2
1263 // Rebreak anchor par
1264 text->redoParagraph(pit);
1265 int y0 = text->getPar(pit1).ascent() - offset_ref_;
1267 // Redo paragraphs above cursor if necessary
1269 while (y1 > 0 && pit1 > 0) {
1270 y1 -= text->getPar(pit1).ascent();
1272 text->redoParagraph(pit1);
1273 y1 -= text->getPar(pit1).descent();
1277 // Take care of ascent of first line
1278 y1 -= text->getPar(pit1).ascent();
1280 // Normalize anchor for next time
1284 // Grey at the beginning is ugly
1285 if (pit1 == 0 && y1 > 0) {
1291 // Redo paragraphs below cursor if necessary
1293 while (y2 < bv.workHeight() && pit2 < int(npit) - 1) {
1294 y2 += text->getPar(pit2).descent();
1296 text->redoParagraph(pit2);
1297 y2 += text->getPar(pit2).ascent();
1300 // Take care of descent of last line
1301 y2 += text->getPar(pit2).descent();
1303 // The coordinates of all these paragraphs are correct, cache them
1305 for (lyx::pit_type pit = pit1; pit <= pit2; ++pit) {
1306 y += text->getPar(pit).ascent();
1307 theCoords.parPos()[text][pit] = Point(0, y);
1308 y += text->getPar(pit).descent();
1311 lyxerr << BOOST_CURRENT_FUNCTION
1316 return ViewMetricsInfo(pit1, pit2, y1, y2);