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 Braustein
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"
28 #include "dispatchresult.h"
30 #include "FloatList.h"
31 #include "funcrequest.h"
34 #include "iterators.h"
35 #include "lyx_cb.h" // added for Dispatch functions
41 #include "lastfiles.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetref.h"
51 #include "frontends/Alert.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/FileDialog.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/LyXScreenFactory.h"
56 #include "frontends/screen.h"
57 #include "frontends/WorkArea.h"
58 #include "frontends/WorkAreaFactory.h"
60 #include "graphics/Previews.h"
62 #include "mathed/formulabase.h"
64 #include "support/filetools.h"
65 #include "support/path_defines.h"
66 #include "support/tostr.h"
68 #include <boost/bind.hpp>
70 using bv_funcs::currentState;
74 using lyx::support::AddPath;
75 using lyx::support::bformat;
76 using lyx::support::FileSearch;
77 using lyx::support::IsDirWriteable;
78 using lyx::support::MakeDisplayPath;
79 using lyx::support::strToUnsignedInt;
80 using lyx::support::system_lyxdir;
88 extern BufferList bufferlist;
93 unsigned int const saved_positions_num = 20;
95 // All the below connection objects are needed because of a bug in some
96 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
97 // to these connections we avoid a segfault upon startup, and also at exit.
100 boost::signals::connection dispatchcon;
101 boost::signals::connection timecon;
102 boost::signals::connection doccon;
103 boost::signals::connection resizecon;
104 boost::signals::connection kpresscon;
105 boost::signals::connection selectioncon;
106 boost::signals::connection lostcon;
112 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
113 int xpos, int ypos, int width, int height)
114 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
115 using_xterm_cursor(false), cursor_(bv)
117 xsel_cache_.set = false;
119 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
120 screen_.reset(LyXScreenFactory::create(workarea()));
123 doccon = workarea().scrollDocView
124 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
125 resizecon = workarea().workAreaResize
126 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
127 dispatchcon = workarea().dispatch
128 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
129 kpresscon = workarea().workAreaKeyPress
130 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
131 selectioncon = workarea().selectionRequested
132 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
133 lostcon = workarea().selectionLost
134 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
136 timecon = cursor_timeout.timeout
137 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
138 cursor_timeout.start();
139 saved_positions.resize(saved_positions_num);
143 void BufferView::Pimpl::addError(ErrorItem const & ei)
145 errorlist_.push_back(ei);
149 void BufferView::Pimpl::showReadonly(bool)
151 owner_->updateWindowTitle();
152 owner_->getDialogs().updateBufferDependent(false);
156 void BufferView::Pimpl::connectBuffer(Buffer & buf)
158 if (errorConnection_.connected())
161 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
162 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
163 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
164 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
165 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
166 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
167 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
171 void BufferView::Pimpl::disconnectBuffer()
173 errorConnection_.disconnect();
174 messageConnection_.disconnect();
175 busyConnection_.disconnect();
176 titleConnection_.disconnect();
177 timerConnection_.disconnect();
178 readonlyConnection_.disconnect();
179 closingConnection_.disconnect();
183 bool BufferView::Pimpl::newFile(string const & filename,
184 string const & tname,
187 Buffer * b = ::newFile(filename, tname, isNamed);
193 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
195 // get absolute path of file and add ".lyx" to the filename if
197 string s = FileSearch(string(), filename, "lyx");
199 bool const found = !s.empty();
204 // file already open?
205 if (bufferlist.exists(s)) {
206 string const file = MakeDisplayPath(s, 20);
207 string text = bformat(_("The document %1$s is already "
208 "loaded.\n\nDo you want to revert "
209 "to the saved version?"), file);
210 int const ret = Alert::prompt(_("Revert to saved document?"),
211 text, 0, 1, _("&Revert"), _("&Switch to document"));
214 buffer(bufferlist.getBuffer(s));
217 // FIXME: should be LFUN_REVERT
218 if (!bufferlist.close(bufferlist.getBuffer(s), false))
220 // Fall through to new load. (Asger)
227 b = bufferlist.newBuffer(s);
229 if (!::loadLyXFile(b, s)) {
230 bufferlist.release(b);
234 string text = bformat(_("The document %1$s does not yet "
235 "exist.\n\nDo you want to create "
236 "a new document?"), s);
237 int const ret = Alert::prompt(_("Create new document?"),
238 text, 0, 1, _("&Create"), _("Cancel"));
241 b = ::newFile(s, string(), true);
247 bv_->showErrorList(_("Parse"));
250 LyX::ref().lastfiles().newFile(b->fileName());
256 WorkArea & BufferView::Pimpl::workarea() const
258 return *workarea_.get();
262 LyXScreen & BufferView::Pimpl::screen() const
264 return *screen_.get();
268 Painter & BufferView::Pimpl::painter() const
270 return workarea().getPainter();
274 void BufferView::Pimpl::top_y(int y)
280 int BufferView::Pimpl::top_y() const
286 void BufferView::Pimpl::buffer(Buffer * b)
288 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
292 //delete bv_->text();
296 // set current buffer
301 // if we're quitting lyx, don't bother updating stuff
305 // if we are closing the buffer, use the first buffer as current
307 buffer_ = bufferlist.first();
310 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
311 connectBuffer(*buffer_);
313 buffer_->text().init(bv_);
314 buffer_->text().textwidth_ = workarea().workWidth();
315 buffer_->text().fullRebreak();
317 // If we don't have a text object for this, we make one
318 if (bv_->text() == 0)
319 resizeCurrentBuffer();
321 // FIXME: needed when ?
324 // Buffer-dependent dialogs should be updated or
325 // hidden. This should go here because some dialogs (eg ToC)
326 // require bv_->text.
327 owner_->getDialogs().updateBufferDependent(true);
329 lyxerr[Debug::INFO] << " No Buffer!" << endl;
330 owner_->getDialogs().hideBufferDependent();
335 owner_->updateMenubar();
336 owner_->updateToolbar();
337 owner_->updateLayoutChoice();
338 owner_->updateWindowTitle();
340 // Don't forget to update the Layout
342 owner_->setLayout(bv_->text()->cursorPar()->layout()->name());
344 if (lyx::graphics::Previews::activated() && buffer_)
345 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
349 bool BufferView::Pimpl::fitCursor()
351 lyxerr << "BufferView::Pimpl::fitCursor." << endl;
352 if (screen().fitCursor(bv_)) {
360 void BufferView::Pimpl::redoCurrentBuffer()
362 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
363 if (buffer_ && bv_->text()) {
364 resizeCurrentBuffer();
366 owner_->updateLayoutChoice();
371 void BufferView::Pimpl::resizeCurrentBuffer()
373 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
376 int selstartpar = -1;
380 pos_type selstartpos = 0;
381 pos_type selendpos = 0;
382 bool selection = false;
383 bool mark_set = false;
387 owner_->message(_("Formatting document..."));
389 lyxerr << "### resizeCurrentBuffer: text " << bv_->text() << endl;
393 par = bv_->text()->cursor.par();
394 pos = bv_->text()->cursor.pos();
395 selstartpar = bv_->text()->selStart().par();
396 selstartpos = bv_->text()->selStart().pos();
397 selendpar = bv_->text()->selEnd().par();
398 selendpos = bv_->text()->selEnd().pos();
399 selection = bv_->text()->selection.set();
400 mark_set = bv_->text()->selection.mark();
401 bv_->text()->textwidth_ = bv_->workWidth();
402 bv_->text()->fullRebreak();
406 bv_->text()->selection.set(true);
407 // At this point just to avoid the Delete-Empty-Paragraph-
408 // Mechanism when setting the cursor.
409 bv_->text()->selection.mark(mark_set);
411 bv_->text()->setCursor(selstartpar, selstartpos);
412 bv_->text()->selection.cursor = bv_->text()->cursor;
413 bv_->text()->setCursor(selendpar, selendpos);
414 bv_->text()->setSelection();
415 bv_->text()->setCursor(par, pos);
417 bv_->text()->setCursor(par, pos);
418 bv_->text()->selection.cursor = bv_->text()->cursor;
419 bv_->text()->selection.set(false);
428 // reset the "Formatting..." message
429 owner_->clearMessage();
435 void BufferView::Pimpl::updateScrollbar()
438 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
439 workarea().setScrollbarParams(0, 0, 0);
443 LyXText const & t = *bv_->text();
445 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
446 << top_y() << ", default height " << defaultRowHeight() << endl;
448 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
452 void BufferView::Pimpl::scrollDocView(int value)
454 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
459 screen().hideCursor();
462 screen().redraw(*bv_);
464 if (!lyxrc.cursor_follows_scrollbar)
467 int const height = defaultRowHeight();
468 int const first = top_y() + height;
469 int const last = top_y() + workarea().workHeight() - height;
471 LyXText * text = bv_->text();
472 if (text->cursor.y() < first)
473 text->setCursorFromCoordinates(0, first);
474 else if (text->cursor.y() > last)
475 text->setCursorFromCoordinates(0, last);
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 LyXText * text = bv_->getLyXText();
531 if (!text->selection.set()) {
532 xsel_cache_.set = false;
536 if (!xsel_cache_.set ||
537 text->cursor != xsel_cache_.cursor ||
538 text->selection.cursor != xsel_cache_.selection_cursor)
540 xsel_cache_.cursor = text->cursor;
541 xsel_cache_.selection_cursor = text->selection.cursor;
542 xsel_cache_.set = text->selection.set();
543 sel = text->selectionAsString(*bv_->buffer(), false);
545 workarea().putClipboard(sel);
550 void BufferView::Pimpl::selectionLost()
553 screen().hideCursor();
554 bv_->getLyXText()->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();
574 // The visible LyXView need a resize
575 resizeCurrentBuffer();
579 if (widthChange || heightChange)
582 // always make sure that the scrollbar is sane.
584 owner_->updateLayoutChoice();
588 void BufferView::Pimpl::update()
590 //lyxerr << "BufferView::update()" << endl;
591 // fix cursor coordinate cache in case something went wrong
593 // check needed to survive LyX startup
594 if (bv_->getLyXText()) {
595 // update all 'visible' paragraphs
596 ParagraphList::iterator beg;
597 ParagraphList::iterator end;
598 getParsInRange(buffer_->paragraphs(),
599 top_y(), top_y() + workarea().workHeight(),
601 bv_->text()->redoParagraphs(beg, end);
602 bv_->getLyXText()->redoCursor();
605 screen().redraw(*bv_);
609 // Callback for cursor timer
610 void BufferView::Pimpl::cursorToggle()
613 cursor_timeout.restart();
617 screen().toggleCursor(*bv_);
618 cursor_timeout.restart();
622 bool BufferView::Pimpl::available() const
624 return buffer_ && bv_->text();
628 Change const BufferView::Pimpl::getCurrentChange()
630 if (!bv_->buffer()->params().tracking_changes)
631 return Change(Change::UNCHANGED);
633 LyXText * text = bv_->getLyXText();
635 if (!text->selection.set())
636 return Change(Change::UNCHANGED);
638 return text->getPar(text->selStart())
639 ->lookupChangeFull(text->selStart().pos());
643 void BufferView::Pimpl::savePosition(unsigned int i)
645 if (i >= saved_positions_num)
647 saved_positions[i] = Position(buffer_->fileName(),
648 bv_->text()->cursorPar()->id(),
649 bv_->text()->cursor.pos());
651 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
655 void BufferView::Pimpl::restorePosition(unsigned int i)
657 if (i >= saved_positions_num)
660 string const fname = saved_positions[i].filename;
662 bv_->text()->clearSelection();
664 if (fname != buffer_->fileName()) {
666 if (bufferlist.exists(fname))
667 b = bufferlist.getBuffer(fname);
669 b = bufferlist.newBuffer(fname);
670 ::loadLyXFile(b, fname); // don't ask, just load it
676 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
677 if (par == buffer_->par_iterator_end())
680 bv_->text()->setCursor(par.pit(),
681 min(par->size(), saved_positions[i].par_pos));
684 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
688 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
690 return i < saved_positions_num && !saved_positions[i].filename.empty();
694 void BufferView::Pimpl::switchKeyMap()
696 if (!lyxrc.rtl_support)
699 Intl & intl = owner_->getIntl();
700 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
701 if (intl.keymap == Intl::PRIMARY)
704 if (intl.keymap == Intl::SECONDARY)
710 void BufferView::Pimpl::center()
712 LyXText * text = bv_->text();
714 text->clearSelection();
715 int const half_height = workarea().workHeight() / 2;
716 int new_y = std::max(0, text->cursor.y() - half_height);
718 // FIXME: look at this comment again ...
719 // This updates top_y() but means the fitCursor() call
720 // from the update(FITCUR) doesn't realise that we might
721 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
722 // the scrollbar to be updated as it should, so we have
723 // to do it manually. Any operation that does a center()
724 // and also might have moved top_y() must make sure to call
725 // updateScrollbar() currently. Never mind that this is a
726 // pretty obfuscated way of updating t->top_y()
731 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
733 workarea().putClipboard(stuff);
737 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
740 LyXCursor cursor = bv_->getLyXText()->cursor;
741 Buffer::inset_iterator it =
742 find_if(Buffer::inset_iterator(
743 cursorPar(), cursor.pos()),
744 buffer_->inset_iterator_end(),
745 lyx::compare_memfun(&Inset::lyxCode, code));
746 return it != buffer_->inset_iterator_end() ? (*it) : 0;
748 // Ok, this is a little bit too brute force but it
749 // should work for now. Better infrastructure is coming. (Lgb)
751 Buffer * b = bv_->buffer();
752 LyXText * text = bv_->getLyXText();
754 Buffer::inset_iterator beg = b->inset_iterator_begin();
755 Buffer::inset_iterator end = b->inset_iterator_end();
757 bool cursor_par_seen = false;
759 for (; beg != end; ++beg) {
760 if (beg.getPar() == text->cursorPar()) {
761 cursor_par_seen = true;
763 if (cursor_par_seen) {
764 if (beg.getPar() == text->cursorPar()
765 && beg.getPos() >= text->cursor.pos()) {
767 } else if (beg.getPar() != text->cursorPar()) {
774 // Now find the first inset that matches code.
775 for (; beg != end; ++beg) {
776 if (beg->lyxCode() == code) {
786 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
788 string filename = filen;
790 if (filename.empty()) {
791 // Launch a file browser
792 string initpath = lyxrc.document_path;
795 string const trypath = owner_->buffer()->filePath();
796 // If directory is writeable, use this as default.
797 if (IsDirWriteable(trypath))
801 FileDialog fileDlg(_("Select LyX document to insert"),
803 make_pair(string(_("Documents|#o#O")),
804 string(lyxrc.document_path)),
805 make_pair(string(_("Examples|#E#e")),
806 string(AddPath(system_lyxdir(), "examples"))));
808 FileDialog::Result result =
809 fileDlg.open(initpath,
810 _("*.lyx| LyX Documents (*.lyx)"));
812 if (result.first == FileDialog::Later)
815 filename = result.second;
817 // check selected filename
818 if (filename.empty()) {
819 owner_->message(_("Canceled."));
824 // get absolute path of file and add ".lyx" to the filename if
826 filename = FileSearch(string(), filename, "lyx");
828 string const disp_fn = MakeDisplayPath(filename);
829 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
830 if (bv_->insertLyXFile(filename))
831 owner_->message(bformat(_("Document %1$s inserted."),
834 owner_->message(bformat(_("Could not insert document %1$s"),
839 void BufferView::Pimpl::trackChanges()
841 Buffer * buf(bv_->buffer());
842 bool const tracking(buf->params().tracking_changes);
845 ParIterator const end = buf->par_iterator_end();
846 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
848 buf->params().tracking_changes = true;
850 // we cannot allow undos beyond the freeze point
851 buf->undostack().clear();
854 bv_->text()->setCursor(0, 0);
855 #warning changes FIXME
856 bool found = lyx::find::findNextChange(bv_);
858 owner_->getDialogs().show("changes");
862 ParIterator const end = buf->par_iterator_end();
863 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
864 it->untrackChanges();
865 buf->params().tracking_changes = false;
868 buf->redostack().clear();
872 LCursor theTempCursor(0);
876 InsetOld * insetFromCoords(BufferView * bv, int x, int y)
878 lyxerr << "insetFromCoords" << endl;
879 LyXText * text = bv->text();
880 InsetOld * inset = 0;
881 theTempCursor = LCursor(bv);
883 InsetOld * const inset_hit = text->checkInsetHit(x, y);
885 lyxerr << "no further inset hit" << endl;
889 if (!inset->descendable()) {
890 lyxerr << "not descendable" << endl;
893 int const cell = inset->getCell(x, y + bv->top_y());
896 text = inset_hit->getText(cell);
897 lyxerr << "Hit inset: " << inset << " at x: " << x
898 << " text: " << text << " y: " << y << endl;
899 theTempCursor.push(static_cast<UpdatableInset*>(inset));
901 lyxerr << "theTempCursor: " << theTempCursor << endl;
908 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
910 switch (cmd.action) {
911 case LFUN_MOUSE_MOTION: {
914 FuncRequest cmd1(cmd, bv_);
915 UpdatableInset * inset = bv_->cursor().innerInset();
918 cmd1.x -= inset->x();
919 cmd1.y -= inset->y();
920 res = inset->dispatch(cmd1);
922 cmd1.y += bv_->top_y();
923 res = bv_->cursor().innerText()->dispatch(cmd1);
926 if (bv_->fitCursor() || res.update()) {
928 bv_->cursor().updatePos();
934 case LFUN_MOUSE_PRESS:
935 case LFUN_MOUSE_RELEASE:
936 case LFUN_MOUSE_DOUBLE:
937 case LFUN_MOUSE_TRIPLE: {
938 // We pass those directly to the Bufferview, since
939 // otherwise selection handling breaks down
941 // Doesn't go through lyxfunc, so we need to update
942 // the layout choice etc. ourselves
944 // e.g. Qt mouse press when no buffer
948 screen().hideCursor();
950 // either the inset under the cursor or the surrounding LyXText will
951 // handle this event.
953 // built temporary path to inset
954 InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y);
955 FuncRequest cmd1(cmd, bv_);
958 // try to dispatch to that inset
960 FuncRequest cmd2 = cmd1;
961 lyxerr << "dispatching action " << cmd2.action
962 << " to inset " << inset << endl;
963 cmd2.x -= inset->x();
964 cmd2.y -= inset->y();
965 res = inset->dispatch(cmd2);
968 bv_->cursor().updatePos();
977 bv_->cursor() = theTempCursor;
978 bv_->cursor().innerText()->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
979 if (bv_->fitCursor())
983 lyxerr << "not dispatched by inner inset val: " << res.val() << endl;
988 // otherwise set cursor to surrounding LyXText
989 if (!res.dispatched()) {
990 lyxerr << "temp cursor is: " << theTempCursor << endl;
991 lyxerr << "dispatching " << cmd1
992 << " to surrounding LyXText "
993 << theTempCursor.innerText() << endl;
994 bv_->cursor() = theTempCursor;
995 cmd1.y += bv_->top_y();
996 res = bv_->cursor().innerText()->dispatch(cmd1);
997 if (bv_->fitCursor() || res.update())
1000 //return DispatchResult(true, true);
1002 // see workAreaKeyPress
1003 cursor_timeout.restart();
1004 screen().showCursor(*bv_);
1006 // skip these when selecting
1007 if (cmd.action != LFUN_MOUSE_MOTION) {
1008 owner_->updateLayoutChoice();
1009 owner_->updateToolbar();
1012 // slight hack: this is only called currently when we
1013 // clicked somewhere, so we force through the display
1014 // of the new status here.
1015 owner_->clearMessage();
1020 owner_->dispatch(cmd);
1026 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1028 // Make sure that the cached BufferView is correct.
1029 FuncRequest ev = ev_in;
1032 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1033 << " action[" << ev.action << ']'
1034 << " arg[" << ev.argument << ']'
1035 << " x[" << ev.x << ']'
1036 << " y[" << ev.y << ']'
1037 << " button[" << ev.button() << ']'
1040 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1042 switch (ev.action) {
1044 case LFUN_SCROLL_INSET:
1045 // this is not handled here as this function is only active
1046 // if we have a locking_inset and that one is (or contains)
1050 case LFUN_FILE_INSERT:
1051 MenuInsertLyXFile(ev.argument);
1054 case LFUN_FILE_INSERT_ASCII_PARA:
1055 InsertAsciiFile(bv_, ev.argument, true);
1058 case LFUN_FILE_INSERT_ASCII:
1059 InsertAsciiFile(bv_, ev.argument, false);
1062 case LFUN_FONT_STATE:
1063 owner_->getLyXFunc().setMessage(currentState(bv_));
1066 case LFUN_INSERT_LABEL: {
1067 // Try and generate a valid label
1068 string const contents = ev.argument.empty() ?
1069 getPossibleLabel(*bv_) : ev.argument;
1070 InsetCommandParams icp("label", contents);
1071 string data = InsetCommandMailer::params2string("label", icp);
1072 owner_->getDialogs().show("label", data, 0);
1076 case LFUN_BOOKMARK_SAVE:
1077 savePosition(strToUnsignedInt(ev.argument));
1080 case LFUN_BOOKMARK_GOTO:
1081 restorePosition(strToUnsignedInt(ev.argument));
1084 case LFUN_REF_GOTO: {
1085 string label = ev.argument;
1086 if (label.empty()) {
1088 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1090 label = inset->getContents();
1096 bv_->gotoLabel(label);
1100 // --- accented characters ---------------------------
1103 case LFUN_CIRCUMFLEX:
1113 case LFUN_SPECIAL_CARON:
1116 case LFUN_HUNG_UMLAUT:
1119 if (ev.argument.empty()) {
1121 owner_->getLyXFunc().handleKeyFunc(ev.action);
1123 owner_->getLyXFunc().handleKeyFunc(ev.action);
1124 owner_->getIntl().getTransManager()
1125 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1130 case LFUN_MATH_MACRO:
1131 case LFUN_MATH_DELIM:
1132 case LFUN_INSERT_MATRIX:
1133 case LFUN_INSERT_MATH:
1134 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1135 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1136 case LFUN_MATH_MODE: // Open or create an inlined math inset
1140 case LFUN_INSET_INSERT: {
1142 BOOST_ASSERT(false);
1143 InsetOld * inset = createInset(ev);
1144 if (!inset || !insertInset(inset))
1149 case LFUN_FLOAT_LIST:
1150 if (tclass.floats().typeExist(ev.argument)) {
1151 InsetOld * inset = new InsetFloatList(ev.argument);
1152 if (!insertInset(inset, tclass.defaultLayoutName()))
1155 lyxerr << "Non-existent float type: "
1156 << ev.argument << endl;
1160 case LFUN_LAYOUT_PARAGRAPH: {
1162 params2string(*bv_->getLyXText()->cursorPar(), data);
1163 data = "show\n" + data;
1164 bv_->owner()->getDialogs().show("paragraph", data);
1168 case LFUN_PARAGRAPH_UPDATE:
1169 updateParagraphDialog();
1172 case LFUN_PARAGRAPH_APPLY:
1173 setParagraphParams(*bv_, ev.argument);
1176 case LFUN_THESAURUS_ENTRY: {
1177 string arg = ev.argument;
1180 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1184 if (arg.size() > 100 || arg.empty()) {
1185 // Get word or selection
1186 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1187 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1188 // FIXME: where is getLyXText()->unselect(bv_) ?
1192 bv_->owner()->getDialogs().show("thesaurus", arg);
1196 case LFUN_TRACK_CHANGES:
1200 case LFUN_MERGE_CHANGES:
1201 owner_->getDialogs().show("changes");
1204 case LFUN_ACCEPT_ALL_CHANGES: {
1205 bv_->text()->setCursor(0, 0);
1206 #warning FIXME changes
1207 while (lyx::find::findNextChange(bv_))
1208 bv_->getLyXText()->acceptChange();
1213 case LFUN_REJECT_ALL_CHANGES: {
1214 bv_->text()->setCursor(0, 0);
1215 #warning FIXME changes
1216 while (lyx::find::findNextChange(bv_))
1217 bv_->getLyXText()->rejectChange();
1222 case LFUN_ACCEPT_CHANGE: {
1223 bv_->getLyXText()->acceptChange();
1228 case LFUN_REJECT_CHANGE: {
1229 bv_->getLyXText()->rejectChange();
1234 case LFUN_UNKNOWN_ACTION:
1235 ev.errorMessage(N_("Unknown function!"));
1239 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_)).dispatched();
1246 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1248 // not quite sure if we want this...
1249 bv_->text()->recUndo(bv_->text()->cursor.par());
1252 bv_->text()->clearSelection();
1253 if (!lout.empty()) {
1254 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1256 if (!bv_->text()->cursorPar()->empty()) {
1257 bv_->text()->cursorLeft(bv_);
1258 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1262 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1263 bool hasLayout = tclass.hasLayout(lres);
1265 bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
1266 bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1268 bv_->cursor().innerText()->insertInset(inset);
1274 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1275 string const & from, string const & to)
1277 bool need_update = false;
1278 LyXCursor cursor = bv_->text()->cursor;
1279 LyXCursor tmpcursor = cursor;
1280 cursor.par(tmpcursor.par());
1281 cursor.pos(tmpcursor.pos());
1283 ParIterator end = bv_->buffer()->par_iterator_end();
1284 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1286 bool changed_inset = false;
1287 for (InsetList::iterator it2 = it->insetlist.begin();
1288 it2 != it->insetlist.end(); ++it2) {
1289 if (it2->inset->lyxCode() == code) {
1290 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1291 if (inset->getContents() == from) {
1292 inset->setContents(to);
1293 changed_inset = true;
1297 if (changed_inset) {
1302 // The test it.size()==1 was needed to prevent crashes.
1303 // How to set the cursor correctly when it.size()>1 ??
1304 if (it.size() == 1) {
1305 bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
1306 bv_->text()->redoParagraph(bv_->text()->cursorPar());
1310 bv_->text()->setCursorIntern(cursor.par(), cursor.pos());
1315 void BufferView::Pimpl::updateParagraphDialog()
1317 if (!bv_->owner()->getDialogs().visible("paragraph"))
1319 Paragraph const & par = *bv_->getLyXText()->cursorPar();
1321 params2string(par, data);
1323 // Will the paragraph accept changes from the dialog?
1324 InsetOld * const inset = par.inInset();
1326 !(inset && inset->forceDefaultParagraphs(inset));
1328 data = "update " + tostr(accept) + '\n' + data;
1329 bv_->owner()->getDialogs().update("paragraph", data);