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>
82 using lyx::support::AddPath;
83 using lyx::support::bformat;
84 using lyx::support::FileFilterList;
85 using lyx::support::FileSearch;
86 using lyx::support::ForkedcallsController;
87 using lyx::support::IsDirWriteable;
88 using lyx::support::MakeDisplayPath;
89 using lyx::support::MakeAbsPath;
90 using lyx::support::package;
93 using std::istringstream;
98 using std::mem_fun_ref;
101 extern BufferList bufferlist;
106 unsigned int const saved_positions_num = 20;
108 // All the below connection objects are needed because of a bug in some
109 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
110 // to these connections we avoid a segfault upon startup, and also at exit.
113 boost::signals::connection dispatchcon;
114 boost::signals::connection timecon;
115 boost::signals::connection doccon;
116 boost::signals::connection resizecon;
117 boost::signals::connection kpresscon;
118 boost::signals::connection selectioncon;
119 boost::signals::connection lostcon;
122 /// Get next inset of this class from current cursor position
124 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
127 DocIterator it = cur;
128 if (it.nextInset() &&
129 it.nextInset()->lyxCode() == code) {
130 inset = static_cast<T*>(it.nextInset());
139 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
140 int width, int height)
141 : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
142 using_xterm_cursor(false), cursor_(bv) ,
143 anchor_ref_(0), offset_ref_(0)
145 xsel_cache_.set = false;
147 workarea_.reset(WorkAreaFactory::create(*owner_, width, height));
148 screen_.reset(LyXScreenFactory::create(workarea()));
151 doccon = workarea().scrollDocView
152 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
153 resizecon = workarea().workAreaResize
154 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
155 dispatchcon = workarea().dispatch
156 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
157 kpresscon = workarea().workAreaKeyPress
158 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
159 selectioncon = workarea().selectionRequested
160 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
161 lostcon = workarea().selectionLost
162 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
164 timecon = cursor_timeout.timeout
165 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
166 cursor_timeout.start();
167 saved_positions.resize(saved_positions_num);
171 void BufferView::Pimpl::addError(ErrorItem const & ei)
173 errorlist_.push_back(ei);
177 void BufferView::Pimpl::showReadonly(bool)
179 owner_->updateWindowTitle();
180 owner_->getDialogs().updateBufferDependent(false);
184 void BufferView::Pimpl::connectBuffer(Buffer & buf)
186 if (errorConnection_.connected())
191 boost::bind(&BufferView::Pimpl::addError, this, _1));
195 boost::bind(&LyXView::message, owner_, _1));
199 boost::bind(&LyXView::busy, owner_, _1));
202 buf.updateTitles.connect(
203 boost::bind(&LyXView::updateWindowTitle, owner_));
206 buf.resetAutosaveTimers.connect(
207 boost::bind(&LyXView::resetAutosaveTimer, owner_));
209 readonlyConnection_ =
210 buf.readonly.connect(
211 boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
215 boost::bind(&BufferView::Pimpl::setBuffer, this, (Buffer *)0));
219 void BufferView::Pimpl::disconnectBuffer()
221 errorConnection_.disconnect();
222 messageConnection_.disconnect();
223 busyConnection_.disconnect();
224 titleConnection_.disconnect();
225 timerConnection_.disconnect();
226 readonlyConnection_.disconnect();
227 closingConnection_.disconnect();
231 void BufferView::Pimpl::newFile(string const & filename, string const & tname,
234 setBuffer(::newFile(filename, tname, isNamed));
238 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
240 // get absolute path of file and add ".lyx" to the filename if
242 string s = FileSearch(string(), filename, "lyx");
244 bool const found = !s.empty();
249 // file already open?
250 if (bufferlist.exists(s)) {
251 string const file = MakeDisplayPath(s, 20);
252 string text = bformat(_("The document %1$s is already "
253 "loaded.\n\nDo you want to revert "
254 "to the saved version?"), file);
255 int const ret = Alert::prompt(_("Revert to saved document?"),
256 text, 0, 1, _("&Revert"), _("&Switch to document"));
259 setBuffer(bufferlist.getBuffer(s));
262 // FIXME: should be LFUN_REVERT
263 if (!bufferlist.close(bufferlist.getBuffer(s), false))
265 // Fall through to new load. (Asger)
271 b = bufferlist.newBuffer(s);
273 if (!::loadLyXFile(b, s)) {
274 bufferlist.release(b);
278 string text = bformat(_("The document %1$s does not yet "
279 "exist.\n\nDo you want to create "
280 "a new document?"), s);
281 int const ret = Alert::prompt(_("Create new document?"),
282 text, 0, 1, _("&Create"), _("Cancel"));
285 b = ::newFile(s, string(), true);
291 bv_->showErrorList(_("Parse"));
294 LyX::ref().lastfiles().newFile(b->fileName());
300 WorkArea & BufferView::Pimpl::workarea() const
302 return *workarea_.get();
306 LyXScreen & BufferView::Pimpl::screen() const
308 return *screen_.get();
312 Painter & BufferView::Pimpl::painter() const
314 return workarea().getPainter();
318 void BufferView::Pimpl::setBuffer(Buffer * b)
320 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
325 // if we are closing current buffer, switch to the first in
328 lyxerr[Debug::INFO] << " No Buffer!" << endl;
329 // we are closing the buffer, use the first buffer as current
330 buffer_ = bufferlist.first();
331 owner_->getDialogs().hideBufferDependent();
333 // set current buffer
338 cursor_ = LCursor(*bv_);
343 // if we're quitting lyx, don't bother updating stuff
348 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
349 connectBuffer(*buffer_);
351 cursor_.push(buffer_->inset());
352 cursor_.resetAnchor();
353 buffer_->text().init(bv_);
354 buffer_->text().setCurrentFont(cursor_);
356 // Buffer-dependent dialogs should be updated or
357 // hidden. This should go here because some dialogs (eg ToC)
358 // require bv_->text.
359 owner_->getDialogs().updateBufferDependent(true);
364 owner_->updateMenubar();
365 owner_->updateToolbars();
366 owner_->updateLayoutChoice();
367 owner_->updateWindowTitle();
369 // This is done after the layout combox has been populated
371 owner_->setLayout(cursor_.paragraph().layout()->name());
373 if (buffer_ && lyx::graphics::Previews::status() != LyXRC::PREVIEW_OFF)
374 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
378 void BufferView::Pimpl::resizeCurrentBuffer()
380 lyxerr[Debug::DEBUG] << "resizeCurrentBuffer" << endl;
382 owner_->message(_("Formatting document..."));
384 LyXText * text = bv_->text();
394 // reset the "Formatting..." message
395 owner_->clearMessage();
401 void BufferView::Pimpl::updateScrollbar()
404 lyxerr[Debug::DEBUG] << "no text in updateScrollbar" << endl;
405 workarea().setScrollbarParams(0, 0, 0);
409 LyXText & t = *bv_->text();
410 if (anchor_ref_ > int(t.paragraphs().size()) - 1) {
411 anchor_ref_ = int(t.paragraphs().size()) - 1;
416 << "Updating scrollbar: height: " << t.paragraphs().size()
417 << " curr par: " << bv_->cursor().bottom().pit()
418 << " default height " << defaultRowHeight() << endl;
420 //it would be better to fix the scrollbar to understand
421 //values in [0..1] and divide everything by wh
422 int const wh = workarea().workHeight() / 4;
423 int const h = t.getPar(anchor_ref_).height();
424 workarea().setScrollbarParams(t.paragraphs().size() * wh, anchor_ref_ * wh + int(offset_ref_ * wh / float(h)), int (wh * defaultRowHeight() / float(h)));
425 // workarea().setScrollbarParams(t.paragraphs().size(), anchor_ref_, 1);
429 void BufferView::Pimpl::scrollDocView(int value)
431 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
436 screen().hideCursor();
438 int const wh = workarea().workHeight() / 4;
440 LyXText & t = *bv_->text();
442 float const bar = value / float(wh * t.paragraphs().size());
444 anchor_ref_ = int(bar * t.paragraphs().size());
445 t.redoParagraph(anchor_ref_);
446 int const h = t.getPar(anchor_ref_).height();
447 offset_ref_ = int((bar * t.paragraphs().size() - anchor_ref_) * h);
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) {
589 LyXFont const font = bv_->cursor().getFont();
590 int const asc = font_metrics::maxAscent(font);
591 int const des = font_metrics::maxDescent(font);
592 Point p = bv_funcs::getPos(bv_->cursor());
593 if (p.y_ - asc >= 0 && p.y_ + des < bv_->workHeight())
601 void BufferView::Pimpl::update(bool fitcursor, bool forceupdate)
603 lyxerr << "BufferView::Pimpl::update(fc=" << fitcursor << ", fu="
604 << forceupdate << ") buffer: " << buffer_ << endl;
606 // check needed to survive LyX startup
608 // update macro store
609 buffer_->buildMacros();
610 // first drawing step
613 std::swap(theCoords, backup);
614 theCoords.startUpdating();
616 ViewMetricsInfo vi = metrics();
618 if (fitcursor && fitCursor()) {
623 // second drawing step
624 screen().redraw(*bv_, vi);
625 theCoords.doneUpdating();
627 // Abort updating of the coord cache - just restore the old one
628 std::swap(theCoords, backup);
635 bv_->owner()->view_state_changed();
639 // Callback for cursor timer
640 void BufferView::Pimpl::cursorToggle()
643 screen().toggleCursor(*bv_);
645 // Use this opportunity to deal with any child processes that
646 // have finished but are waiting to communicate this fact
647 // to the rest of LyX.
648 ForkedcallsController & fcc = ForkedcallsController::get();
649 if (fcc.processesCompleted())
650 fcc.handleCompletedProcesses();
653 cursor_timeout.restart();
657 bool BufferView::Pimpl::available() const
659 return buffer_ && bv_->text();
663 Change const BufferView::Pimpl::getCurrentChange()
665 if (!bv_->buffer()->params().tracking_changes)
666 return Change(Change::UNCHANGED);
668 LyXText * text = bv_->getLyXText();
669 LCursor & cur = bv_->cursor();
671 if (!cur.selection())
672 return Change(Change::UNCHANGED);
674 return text->getPar(cur.selBegin().pit()).
675 lookupChangeFull(cur.selBegin().pos());
679 void BufferView::Pimpl::savePosition(unsigned int i)
681 if (i >= saved_positions_num)
683 BOOST_ASSERT(bv_->cursor().inTexted());
684 saved_positions[i] = Position(buffer_->fileName(),
685 bv_->cursor().paragraph().id(),
686 bv_->cursor().pos());
688 owner_->message(bformat(_("Saved bookmark %1$d"), i));
692 void BufferView::Pimpl::restorePosition(unsigned int i)
694 if (i >= saved_positions_num)
697 string const fname = saved_positions[i].filename;
699 bv_->cursor().clearSelection();
701 if (fname != buffer_->fileName()) {
703 if (bufferlist.exists(fname))
704 b = bufferlist.getBuffer(fname);
706 b = bufferlist.newBuffer(fname);
707 ::loadLyXFile(b, fname); // don't ask, just load it
713 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
714 if (par == buffer_->par_iterator_end())
717 bv_->text()->setCursor(bv_->cursor(), par.pit(),
718 min(par->size(), saved_positions[i].par_pos));
721 owner_->message(bformat(_("Moved to bookmark %1$d"), i));
725 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
727 return i < saved_positions_num && !saved_positions[i].filename.empty();
731 void BufferView::Pimpl::switchKeyMap()
733 if (!lyxrc.rtl_support)
736 Intl & intl = owner_->getIntl();
737 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
738 if (intl.keymap == Intl::PRIMARY)
741 if (intl.keymap == Intl::SECONDARY)
747 void BufferView::Pimpl::center()
749 CursorSlice & bot = bv_->cursor().bottom();
750 lyx::pit_type const pit = bot.pit();
751 bot.text()->redoParagraph(pit);
752 Paragraph const & par = bot.text()->paragraphs()[pit];
754 offset_ref_ = bv_funcs::coordOffset(bv_->cursor()).y_ + par.ascent()
755 - workarea().workHeight() / 2;
759 void BufferView::Pimpl::stuffClipboard(string const & content) const
761 workarea().putClipboard(content);
765 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
767 string filename = filenm;
769 if (filename.empty()) {
770 // Launch a file browser
771 string initpath = lyxrc.document_path;
774 string const trypath = owner_->buffer()->filePath();
775 // If directory is writeable, use this as default.
776 if (IsDirWriteable(trypath))
780 FileDialog fileDlg(_("Select LyX document to insert"),
782 make_pair(string(_("Documents|#o#O")),
783 string(lyxrc.document_path)),
784 make_pair(string(_("Examples|#E#e")),
785 string(AddPath(package().system_support(), "examples"))));
787 FileDialog::Result result =
788 fileDlg.open(initpath,
789 FileFilterList(_("LyX Documents (*.lyx)")),
792 if (result.first == FileDialog::Later)
795 filename = result.second;
797 // check selected filename
798 if (filename.empty()) {
799 owner_->message(_("Canceled."));
804 // get absolute path of file and add ".lyx" to the filename if
806 filename = FileSearch(string(), filename, "lyx");
808 string const disp_fn = MakeDisplayPath(filename);
809 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
811 bv_->cursor().clearSelection();
812 bv_->getLyXText()->breakParagraph(bv_->cursor());
814 BOOST_ASSERT(bv_->cursor().inTexted());
816 string const fname = MakeAbsPath(filename);
817 bool const res = bv_->buffer()->readFile(fname, bv_->cursor().pit());
820 string s = res ? _("Document %1$s inserted.")
821 : _("Could not insert document %1$s");
822 owner_->message(bformat(s, disp_fn));
826 void BufferView::Pimpl::trackChanges()
828 Buffer * buf = bv_->buffer();
829 bool const tracking = buf->params().tracking_changes;
832 for_each(buf->par_iterator_begin(),
833 buf->par_iterator_end(),
834 bind(&Paragraph::trackChanges, _1, Change::UNCHANGED));
835 buf->params().tracking_changes = true;
837 // we cannot allow undos beyond the freeze point
838 buf->undostack().clear();
841 bv_->text()->setCursor(bv_->cursor(), 0, 0);
843 #warning changes FIXME
845 bool found = lyx::find::findNextChange(bv_);
847 owner_->getDialogs().show("changes");
851 for_each(buf->par_iterator_begin(),
852 buf->par_iterator_end(),
853 mem_fun_ref(&Paragraph::untrackChanges));
855 buf->params().tracking_changes = false;
858 buf->redostack().clear();
862 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
864 lyxerr << "BufferView::Pimpl::workAreaDispatch: request: "
865 << cmd0 << std::endl;
866 // this is only called for mouse related events including
867 // LFUN_FILE_OPEN generated by drag-and-drop.
868 FuncRequest cmd = cmd0;
871 if (cmd.action == LFUN_FILE_OPEN) {
872 owner_->dispatch(cmd);
880 cur.push(bv_->buffer()->inset());
881 cur.selection() = bv_->cursor().selection();
883 // Doesn't go through lyxfunc, so we need to update
884 // the layout choice etc. ourselves
886 // e.g. Qt mouse press when no buffer
890 screen().hideCursor();
892 // Either the inset under the cursor or the
893 // surrounding LyXText will handle this event.
895 // Build temporary cursor.
896 cmd.y = min(max(cmd.y,-1), bv_->workHeight());
897 InsetBase * inset = bv_->text()->editXY(cur, cmd.x, cmd.y);
898 lyxerr << " * hit inset at tip: " << inset << endl;
899 lyxerr << " * created temp cursor:" << cur << endl;
901 // Put anchor at the same position.
904 // Try to dispatch to an non-editable inset near this position
905 // via the temp cursor. If the inset wishes to change the real
906 // cursor it has to do so explicitly by using
907 // cur.bv().cursor() = cur; (or similar)
909 inset->dispatch(cur, cmd);
911 // Now dispatch to the temporary cursor. If the real cursor should
912 // be modified, the inset's dispatch has to do so explicitly.
913 if (!cur.result().dispatched())
916 if (cur.result().dispatched()) {
917 // Redraw if requested or necessary.
918 update(cur.result().update(), cur.result().update());
921 // see workAreaKeyPress
922 cursor_timeout.restart();
923 screen().showCursor(*bv_);
925 // skip these when selecting
926 if (cmd.action != LFUN_MOUSE_MOTION) {
927 owner_->updateLayoutChoice();
928 owner_->updateToolbars();
931 // slight hack: this is only called currently when we
932 // clicked somewhere, so we force through the display
933 // of the new status here.
934 owner_->clearMessage();
939 FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd)
941 Buffer * buf = bv_->buffer();
945 switch (cmd.action) {
948 flag.enabled(!buf->undostack().empty());
951 flag.enabled(!buf->redostack().empty());
953 case LFUN_FILE_INSERT:
954 case LFUN_FILE_INSERT_ASCII_PARA:
955 case LFUN_FILE_INSERT_ASCII:
956 case LFUN_FONT_STATE:
957 case LFUN_INSERT_LABEL:
958 case LFUN_BOOKMARK_SAVE:
961 case LFUN_WORD_REPLACE:
966 case LFUN_WORDS_COUNT:
970 case LFUN_BOOKMARK_GOTO:
971 flag.enabled(bv_->isSavedPosition(convert<unsigned int>(cmd.argument)));
973 case LFUN_TRACK_CHANGES:
975 flag.setOnOff(buf->params().tracking_changes);
978 case LFUN_OUTPUT_CHANGES: {
979 LaTeXFeatures features(*buf, buf->params(), false);
980 flag.enabled(buf && buf->params().tracking_changes
981 && features.isAvailable("dvipost"));
982 flag.setOnOff(buf->params().output_changes);
986 case LFUN_MERGE_CHANGES:
987 case LFUN_ACCEPT_CHANGE: // what about these two
988 case LFUN_REJECT_CHANGE: // what about these two
989 case LFUN_ACCEPT_ALL_CHANGES:
990 case LFUN_REJECT_ALL_CHANGES:
991 flag.enabled(buf && buf->params().tracking_changes);
1002 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
1004 //lyxerr << "BufferView::Pimpl::dispatch cmd: " << cmd << std::endl;
1005 // Make sure that the cached BufferView is correct.
1006 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1007 << " action[" << cmd.action << ']'
1008 << " arg[" << cmd.argument << ']'
1009 << " x[" << cmd.x << ']'
1010 << " y[" << cmd.y << ']'
1011 << " button[" << cmd.button() << ']'
1014 LCursor & cur = bv_->cursor();
1016 switch (cmd.action) {
1020 cur.message(_("Undo"));
1021 cur.clearSelection();
1022 if (!textUndo(*bv_))
1023 cur.message(_("No further undo information"));
1031 cur.message(_("Redo"));
1032 cur.clearSelection();
1033 if (!textRedo(*bv_))
1034 cur.message(_("No further redo information"));
1040 case LFUN_FILE_INSERT:
1041 MenuInsertLyXFile(cmd.argument);
1044 case LFUN_FILE_INSERT_ASCII_PARA:
1045 InsertAsciiFile(bv_, cmd.argument, true);
1048 case LFUN_FILE_INSERT_ASCII:
1049 InsertAsciiFile(bv_, cmd.argument, false);
1052 case LFUN_FONT_STATE:
1053 cur.message(cur.currentState());
1056 case LFUN_BOOKMARK_SAVE:
1057 savePosition(convert<unsigned int>(cmd.argument));
1060 case LFUN_BOOKMARK_GOTO:
1061 restorePosition(convert<unsigned int>(cmd.argument));
1064 case LFUN_REF_GOTO: {
1065 string label = cmd.argument;
1066 if (label.empty()) {
1068 getInsetByCode<InsetRef>(bv_->cursor(),
1069 InsetBase::REF_CODE);
1071 label = inset->getContents();
1077 bv_->gotoLabel(label);
1081 case LFUN_TRACK_CHANGES:
1085 case LFUN_OUTPUT_CHANGES: {
1086 Buffer * buf = bv_->buffer();
1087 bool const state = buf->params().output_changes;
1088 buf->params().output_changes = !state;
1092 case LFUN_MERGE_CHANGES:
1093 owner_->getDialogs().show("changes");
1096 case LFUN_ACCEPT_ALL_CHANGES: {
1097 bv_->cursor().reset(bv_->buffer()->inset());
1098 #ifdef WITH_WARNINGS
1099 #warning FIXME changes
1101 while (lyx::find::findNextChange(bv_))
1102 bv_->getLyXText()->acceptChange(bv_->cursor());
1107 case LFUN_REJECT_ALL_CHANGES: {
1108 bv_->cursor().reset(bv_->buffer()->inset());
1109 #ifdef WITH_WARNINGS
1110 #warning FIXME changes
1112 while (lyx::find::findNextChange(bv_))
1113 bv_->getLyXText()->rejectChange(bv_->cursor());
1117 case LFUN_WORD_FIND:
1118 lyx::find::find(bv_, cmd);
1121 case LFUN_WORD_REPLACE:
1122 lyx::find::replace(bv_, cmd);
1126 cur.clearSelection();
1128 cur.message(N_("Mark off"));
1132 cur.clearSelection();
1135 cur.message(N_("Mark on"));
1139 cur.clearSelection();
1142 cur.message(N_("Mark removed"));
1145 cur.message(N_("Mark set"));
1154 case LFUN_WORDS_COUNT: {
1155 DocIterator from, to;
1156 if (cur.selection()) {
1157 from = cur.selectionBegin();
1158 to = cur.selectionEnd();
1160 from = doc_iterator_begin(bv_->buffer()->inset());
1161 to = doc_iterator_end(bv_->buffer()->inset());
1163 int const count = countWords(from, to);
1166 if (cur.selection())
1167 message = bformat(_("%1$d words in selection."),
1170 message = bformat(_("%1$d words in document."),
1174 if (cur.selection())
1175 message = _("One word in selection.");
1177 message = _("One word in document.");
1180 Alert::information(_("Count words"), message);
1191 ViewMetricsInfo BufferView::Pimpl::metrics()
1193 // remove old position cache
1195 BufferView & bv = *bv_;
1196 LyXText * const text = bv.text();
1197 if (anchor_ref_ > int(text->paragraphs().size() - 1)) {
1198 anchor_ref_ = int(text->paragraphs().size() - 1);
1202 lyx::pit_type const pit = anchor_ref_;
1205 size_t npit = text->paragraphs().size();
1206 lyxerr << "npit: " << npit << " pit1: " << pit1
1207 << " pit2: " << pit2 << endl;
1209 // rebreak anchor par
1210 text->redoParagraph(pit);
1211 int y0 = text->getPar(pit1).ascent() - offset_ref_;
1213 // redo paragraphs above cursor if necessary
1215 while (y1 > 0 && pit1 > 0) {
1216 y1 -= text->getPar(pit1).ascent();
1218 text->redoParagraph(pit1);
1219 y1 -= text->getPar(pit1).descent();
1223 // take care of ascent of first line
1224 y1 -= text->getPar(pit1).ascent();
1226 //normalize anchor for next time
1230 // grey at the beginning is ugly
1231 if (pit1 == 0 && y1 > 0) {
1237 // redo paragraphs below cursor if necessary
1239 while (y2 < bv.workHeight() && pit2 < int(npit) - 1) {
1240 y2 += text->getPar(pit2).descent();
1242 text->redoParagraph(pit2);
1243 y2 += text->getPar(pit2).ascent();
1246 // take care of descent of last line
1247 y2 += text->getPar(pit2).descent();
1249 // the coordinates of all these paragraphs are correct, cache them
1251 for (lyx::pit_type pit = pit1; pit <= pit2; ++pit) {
1252 y += text->getPar(pit).ascent();
1253 theCoords.parPos()[text][pit] = Point(0, y);
1254 y += text->getPar(pit).descent();
1257 lyxerr << "bv:metrics: y1: " << y1 << " y2: " << y2 << endl;
1258 return ViewMetricsInfo(pit1, pit2, y1, y2);