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/globbing.h"
66 #include "support/path_defines.h"
67 #include "support/tostr.h"
69 #include <boost/bind.hpp>
71 using bv_funcs::currentState;
75 using lyx::support::AddPath;
76 using lyx::support::bformat;
77 using lyx::support::FileFilterList;
78 using lyx::support::FileSearch;
79 using lyx::support::IsDirWriteable;
80 using lyx::support::MakeDisplayPath;
81 using lyx::support::strToUnsignedInt;
82 using lyx::support::system_lyxdir;
90 extern BufferList bufferlist;
95 unsigned int const saved_positions_num = 20;
97 // All the below connection objects are needed because of a bug in some
98 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
99 // to these connections we avoid a segfault upon startup, and also at exit.
102 boost::signals::connection dispatchcon;
103 boost::signals::connection timecon;
104 boost::signals::connection doccon;
105 boost::signals::connection resizecon;
106 boost::signals::connection kpresscon;
107 boost::signals::connection selectioncon;
108 boost::signals::connection lostcon;
114 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
115 int xpos, int ypos, int width, int height)
116 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
117 using_xterm_cursor(false), cursor_(bv)
119 xsel_cache_.set = false;
121 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
122 screen_.reset(LyXScreenFactory::create(workarea()));
125 doccon = workarea().scrollDocView
126 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
127 resizecon = workarea().workAreaResize
128 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
129 dispatchcon = workarea().dispatch
130 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
131 kpresscon = workarea().workAreaKeyPress
132 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
133 selectioncon = workarea().selectionRequested
134 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
135 lostcon = workarea().selectionLost
136 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
138 timecon = cursor_timeout.timeout
139 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
140 cursor_timeout.start();
141 saved_positions.resize(saved_positions_num);
145 void BufferView::Pimpl::addError(ErrorItem const & ei)
147 errorlist_.push_back(ei);
151 void BufferView::Pimpl::showReadonly(bool)
153 owner_->updateWindowTitle();
154 owner_->getDialogs().updateBufferDependent(false);
158 void BufferView::Pimpl::connectBuffer(Buffer & buf)
160 if (errorConnection_.connected())
163 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
164 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
165 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
166 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
167 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
168 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
169 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
173 void BufferView::Pimpl::disconnectBuffer()
175 errorConnection_.disconnect();
176 messageConnection_.disconnect();
177 busyConnection_.disconnect();
178 titleConnection_.disconnect();
179 timerConnection_.disconnect();
180 readonlyConnection_.disconnect();
181 closingConnection_.disconnect();
185 bool BufferView::Pimpl::newFile(string const & filename,
186 string const & tname,
189 Buffer * b = ::newFile(filename, tname, isNamed);
195 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
197 // get absolute path of file and add ".lyx" to the filename if
199 string s = FileSearch(string(), filename, "lyx");
201 bool const found = !s.empty();
206 // file already open?
207 if (bufferlist.exists(s)) {
208 string const file = MakeDisplayPath(s, 20);
209 string text = bformat(_("The document %1$s is already "
210 "loaded.\n\nDo you want to revert "
211 "to the saved version?"), file);
212 int const ret = Alert::prompt(_("Revert to saved document?"),
213 text, 0, 1, _("&Revert"), _("&Switch to document"));
216 buffer(bufferlist.getBuffer(s));
219 // FIXME: should be LFUN_REVERT
220 if (!bufferlist.close(bufferlist.getBuffer(s), false))
222 // Fall through to new load. (Asger)
229 b = bufferlist.newBuffer(s);
231 if (!::loadLyXFile(b, s)) {
232 bufferlist.release(b);
236 string text = bformat(_("The document %1$s does not yet "
237 "exist.\n\nDo you want to create "
238 "a new document?"), s);
239 int const ret = Alert::prompt(_("Create new document?"),
240 text, 0, 1, _("&Create"), _("Cancel"));
243 b = ::newFile(s, string(), true);
249 bv_->showErrorList(_("Parse"));
252 LyX::ref().lastfiles().newFile(b->fileName());
258 WorkArea & BufferView::Pimpl::workarea() const
260 return *workarea_.get();
264 LyXScreen & BufferView::Pimpl::screen() const
266 return *screen_.get();
270 Painter & BufferView::Pimpl::painter() const
272 return workarea().getPainter();
276 void BufferView::Pimpl::top_y(int y)
282 int BufferView::Pimpl::top_y() const
288 void BufferView::Pimpl::buffer(Buffer * b)
290 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
294 //delete bv_->text();
298 // set current buffer
303 // if we're quitting lyx, don't bother updating stuff
307 // if we are closing the buffer, use the first buffer as current
309 buffer_ = bufferlist.first();
312 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
313 connectBuffer(*buffer_);
315 buffer_->text().init(bv_);
316 buffer_->text().textwidth_ = workarea().workWidth();
317 buffer_->text().fullRebreak();
319 // If we don't have a text object for this, we make one
320 if (bv_->text() == 0)
321 resizeCurrentBuffer();
323 // FIXME: needed when ?
326 // Buffer-dependent dialogs should be updated or
327 // hidden. This should go here because some dialogs (eg ToC)
328 // require bv_->text.
329 owner_->getDialogs().updateBufferDependent(true);
331 lyxerr[Debug::INFO] << " No Buffer!" << endl;
332 owner_->getDialogs().hideBufferDependent();
337 owner_->updateMenubar();
338 owner_->updateToolbar();
339 owner_->updateLayoutChoice();
340 owner_->updateWindowTitle();
342 // Don't forget to update the Layout
344 owner_->setLayout(bv_->text()->cursorPar()->layout()->name());
346 if (lyx::graphics::Previews::activated() && buffer_)
347 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
351 bool BufferView::Pimpl::fitCursor()
353 lyxerr << "BufferView::Pimpl::fitCursor." << endl;
354 if (screen().fitCursor(bv_)) {
362 void BufferView::Pimpl::redoCurrentBuffer()
364 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
365 if (buffer_ && bv_->text()) {
366 resizeCurrentBuffer();
368 owner_->updateLayoutChoice();
373 void BufferView::Pimpl::resizeCurrentBuffer()
375 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
378 int selstartpar = -1;
382 pos_type selstartpos = 0;
383 pos_type selendpos = 0;
385 bool mark_set = false;
389 owner_->message(_("Formatting document..."));
391 LyXText * text = bv_->text();
392 lyxerr << "### resizeCurrentBuffer: text " << text << endl;
396 par = bv_->cursor().par();
397 pos = bv_->cursor().pos();
398 selstartpar = bv_->selStart().par();
399 selstartpos = bv_->selStart().pos();
400 selendpar = bv_->selEnd().par();
401 selendpos = bv_->selEnd().pos();
402 sel = bv_->selection().set();
403 mark_set = bv_->selection().mark();
404 text->textwidth_ = bv_->workWidth();
409 bv_->selection().set(true);
410 // At this point just to avoid the Delete-Empty-Paragraph-
411 // Mechanism when setting the cursor.
412 bv_->selection().mark(mark_set);
414 text->setCursor(selstartpar, selstartpos);
416 text->setCursor(selendpar, selendpos);
417 text->setSelection();
418 text->setCursor(par, pos);
420 text->setCursor(par, pos);
422 bv_->selection().set(false);
431 // reset the "Formatting..." message
432 owner_->clearMessage();
438 void BufferView::Pimpl::updateScrollbar()
441 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
442 workarea().setScrollbarParams(0, 0, 0);
446 LyXText const & t = *bv_->text();
448 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
449 << top_y() << ", default height " << defaultRowHeight() << endl;
451 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
455 void BufferView::Pimpl::scrollDocView(int value)
457 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
462 screen().hideCursor();
465 screen().redraw(*bv_);
467 if (!lyxrc.cursor_follows_scrollbar)
470 int const height = defaultRowHeight();
471 int const first = top_y() + height;
472 int const last = top_y() + workarea().workHeight() - height;
474 LyXText * text = bv_->text();
475 if (text->cursorY() < first)
476 text->setCursorFromCoordinates(0, first);
477 else if (text->cursorY() > last)
478 text->setCursorFromCoordinates(0, last);
480 owner_->updateLayoutChoice();
484 void BufferView::Pimpl::scroll(int lines)
489 LyXText const * t = bv_->text();
490 int const line_height = defaultRowHeight();
492 // The new absolute coordinate
493 int new_top_y = top_y() + lines * line_height;
495 // Restrict to a valid value
496 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
497 new_top_y = std::max(0, new_top_y);
499 scrollDocView(new_top_y);
501 // Update the scrollbar.
502 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
506 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
507 key_modifier::state state)
509 bv_->owner()->getLyXFunc().processKeySym(key, state);
511 /* This is perhaps a bit of a hack. When we move
512 * around, or type, it's nice to be able to see
513 * the cursor immediately after the keypress. So
514 * we reset the toggle timeout and force the visibility
515 * of the cursor. Note we cannot do this inside
516 * dispatch() itself, because that's called recursively.
519 cursor_timeout.restart();
520 screen().showCursor(*bv_);
525 void BufferView::Pimpl::selectionRequested()
532 LyXText * text = bv_->getLyXText();
534 if (!bv_->selection().set()) {
535 xsel_cache_.set = false;
539 if (!xsel_cache_.set ||
540 bv_->cursor() != xsel_cache_.cursor ||
541 bv_->anchor() != xsel_cache_.anchor)
543 xsel_cache_.cursor = bv_->cursor();
544 xsel_cache_.anchor = bv_->anchor();
545 xsel_cache_.set = bv_->selection().set();
546 sel = text->selectionAsString(*bv_->buffer(), false);
548 workarea().putClipboard(sel);
553 void BufferView::Pimpl::selectionLost()
556 screen().hideCursor();
557 bv_->getLyXText()->clearSelection();
558 xsel_cache_.set = false;
563 void BufferView::Pimpl::workAreaResize()
565 static int work_area_width;
566 static int work_area_height;
568 bool const widthChange = workarea().workWidth() != work_area_width;
569 bool const heightChange = workarea().workHeight() != work_area_height;
571 // update from work area
572 work_area_width = workarea().workWidth();
573 work_area_height = workarea().workHeight();
577 // The visible LyXView need a resize
578 resizeCurrentBuffer();
582 if (widthChange || heightChange)
585 // always make sure that the scrollbar is sane.
587 owner_->updateLayoutChoice();
591 void BufferView::Pimpl::update()
593 //lyxerr << "BufferView::update()" << endl;
594 // fix cursor coordinate cache in case something went wrong
596 // check needed to survive LyX startup
597 if (bv_->getLyXText()) {
598 // update all 'visible' paragraphs
599 ParagraphList::iterator beg;
600 ParagraphList::iterator end;
601 getParsInRange(buffer_->paragraphs(),
602 top_y(), top_y() + workarea().workHeight(),
604 bv_->text()->redoParagraphs(beg, end);
607 screen().redraw(*bv_);
611 // Callback for cursor timer
612 void BufferView::Pimpl::cursorToggle()
615 cursor_timeout.restart();
619 screen().toggleCursor(*bv_);
620 cursor_timeout.restart();
624 bool BufferView::Pimpl::available() const
626 return buffer_ && bv_->text();
630 Change const BufferView::Pimpl::getCurrentChange()
632 if (!bv_->buffer()->params().tracking_changes)
633 return Change(Change::UNCHANGED);
635 LyXText * text = bv_->getLyXText();
637 if (!bv_->selection().set())
638 return Change(Change::UNCHANGED);
640 return text->getPar(bv_->selStart())
641 ->lookupChangeFull(bv_->selStart().pos());
645 void BufferView::Pimpl::savePosition(unsigned int i)
647 if (i >= saved_positions_num)
649 saved_positions[i] = Position(buffer_->fileName(),
650 bv_->text()->cursorPar()->id(),
651 bv_->text()->cursor().pos());
653 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
657 void BufferView::Pimpl::restorePosition(unsigned int i)
659 if (i >= saved_positions_num)
662 string const fname = saved_positions[i].filename;
664 bv_->text()->clearSelection();
666 if (fname != buffer_->fileName()) {
668 if (bufferlist.exists(fname))
669 b = bufferlist.getBuffer(fname);
671 b = bufferlist.newBuffer(fname);
672 ::loadLyXFile(b, fname); // don't ask, just load it
678 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
679 if (par == buffer_->par_iterator_end())
682 bv_->text()->setCursor(par.pit(),
683 min(par->size(), saved_positions[i].par_pos));
686 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
690 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
692 return i < saved_positions_num && !saved_positions[i].filename.empty();
696 void BufferView::Pimpl::switchKeyMap()
698 if (!lyxrc.rtl_support)
701 Intl & intl = owner_->getIntl();
702 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
703 if (intl.keymap == Intl::PRIMARY)
706 if (intl.keymap == Intl::SECONDARY)
712 void BufferView::Pimpl::center()
714 LyXText * text = bv_->text();
716 text->clearSelection();
717 int const half_height = workarea().workHeight() / 2;
718 int new_y = std::max(0, text->cursorY() - half_height);
720 // FIXME: look at this comment again ...
721 // This updates top_y() but means the fitCursor() call
722 // from the update(FITCUR) doesn't realise that we might
723 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
724 // the scrollbar to be updated as it should, so we have
725 // to do it manually. Any operation that does a center()
726 // and also might have moved top_y() must make sure to call
727 // updateScrollbar() currently. Never mind that this is a
728 // pretty obfuscated way of updating t->top_y()
733 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
735 workarea().putClipboard(stuff);
739 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
742 CursorSlice cursor = bv_->getLyXText()->cursor;
743 Buffer::inset_iterator it =
744 find_if(Buffer::inset_iterator(
745 cursorPar(), cursor().pos()),
746 buffer_->inset_iterator_end(),
747 lyx::compare_memfun(&Inset::lyxCode, code));
748 return it != buffer_->inset_iterator_end() ? (*it) : 0;
750 // Ok, this is a little bit too brute force but it
751 // should work for now. Better infrastructure is coming. (Lgb)
753 Buffer * b = bv_->buffer();
754 LyXText * text = bv_->getLyXText();
756 Buffer::inset_iterator beg = b->inset_iterator_begin();
757 Buffer::inset_iterator end = b->inset_iterator_end();
759 bool cursor_par_seen = false;
761 for (; beg != end; ++beg) {
762 if (beg.getPar() == text->cursorPar()) {
763 cursor_par_seen = true;
765 if (cursor_par_seen) {
766 if (beg.getPar() == text->cursorPar()
767 && beg.getPos() >= text->cursor().pos()) {
769 } else if (beg.getPar() != text->cursorPar()) {
776 // Now find the first inset that matches code.
777 for (; beg != end; ++beg) {
778 if (beg->lyxCode() == code) {
788 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
790 string filename = filen;
792 if (filename.empty()) {
793 // Launch a file browser
794 string initpath = lyxrc.document_path;
797 string const trypath = owner_->buffer()->filePath();
798 // If directory is writeable, use this as default.
799 if (IsDirWriteable(trypath))
803 FileDialog fileDlg(_("Select LyX document to insert"),
805 make_pair(string(_("Documents|#o#O")),
806 string(lyxrc.document_path)),
807 make_pair(string(_("Examples|#E#e")),
808 string(AddPath(system_lyxdir(), "examples"))));
810 FileDialog::Result result =
811 fileDlg.open(initpath,
812 FileFilterList(_("LyX Documents (*.lyx)")),
815 if (result.first == FileDialog::Later)
818 filename = result.second;
820 // check selected filename
821 if (filename.empty()) {
822 owner_->message(_("Canceled."));
827 // get absolute path of file and add ".lyx" to the filename if
829 filename = FileSearch(string(), filename, "lyx");
831 string const disp_fn = MakeDisplayPath(filename);
832 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
833 if (bv_->insertLyXFile(filename))
834 owner_->message(bformat(_("Document %1$s inserted."),
837 owner_->message(bformat(_("Could not insert document %1$s"),
842 void BufferView::Pimpl::trackChanges()
844 Buffer * buf(bv_->buffer());
845 bool const tracking(buf->params().tracking_changes);
848 ParIterator const end = buf->par_iterator_end();
849 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
851 buf->params().tracking_changes = true;
853 // we cannot allow undos beyond the freeze point
854 buf->undostack().clear();
857 bv_->text()->setCursor(0, 0);
858 #warning changes FIXME
859 bool found = lyx::find::findNextChange(bv_);
861 owner_->getDialogs().show("changes");
865 ParIterator const end = buf->par_iterator_end();
866 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
867 it->untrackChanges();
868 buf->params().tracking_changes = false;
871 buf->redostack().clear();
875 LCursor theTempCursor(0);
879 InsetOld * insetFromCoords(BufferView * bv, int x, int y)
881 lyxerr << "insetFromCoords" << endl;
882 LyXText * text = bv->text();
883 InsetOld * inset = 0;
884 theTempCursor = LCursor(bv);
886 InsetOld * const inset_hit = text->checkInsetHit(x, y);
888 lyxerr << "no further inset hit" << endl;
892 if (!inset->descendable()) {
893 lyxerr << "not descendable" << endl;
896 int const cell = inset->getCell(x, y + bv->top_y());
899 text = inset_hit->getText(cell);
900 lyxerr << "Hit inset: " << inset << " at x: " << x
901 << " text: " << text << " y: " << y << endl;
902 theTempCursor.push(static_cast<UpdatableInset*>(inset));
904 lyxerr << "theTempCursor: " << theTempCursor << endl;
911 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
913 switch (cmd.action) {
914 case LFUN_MOUSE_MOTION: {
917 FuncRequest cmd1(cmd, bv_);
918 UpdatableInset * inset = bv_->fullCursor().innerInset();
921 cmd1.x -= inset->x();
922 cmd1.y -= inset->y();
923 res = inset->dispatch(cmd1);
925 cmd1.y += bv_->top_y();
926 res = bv_->fullCursor().innerText()->dispatch(cmd1);
929 if (bv_->fitCursor() || res.update()) {
931 bv_->fullCursor().updatePos();
937 case LFUN_MOUSE_PRESS:
938 case LFUN_MOUSE_RELEASE:
939 case LFUN_MOUSE_DOUBLE:
940 case LFUN_MOUSE_TRIPLE: {
941 // We pass those directly to the Bufferview, since
942 // otherwise selection handling breaks down
944 // Doesn't go through lyxfunc, so we need to update
945 // the layout choice etc. ourselves
947 // e.g. Qt mouse press when no buffer
951 screen().hideCursor();
953 // either the inset under the cursor or the surrounding LyXText will
954 // handle this event.
956 // built temporary path to inset
957 InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y);
958 FuncRequest cmd1(cmd, bv_);
961 // try to dispatch to that inset
963 FuncRequest cmd2 = cmd1;
964 lyxerr << "dispatching action " << cmd2.action
965 << " to inset " << inset << endl;
966 cmd2.x -= inset->x();
967 cmd2.y -= inset->y();
968 res = inset->dispatch(cmd2);
971 bv_->fullCursor().updatePos();
980 bv_->fullCursor(theTempCursor);
981 bv_->fullCursor().innerText()
982 ->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
983 if (bv_->fitCursor())
987 lyxerr << "not dispatched by inner inset val: " << res.val() << endl;
992 // otherwise set cursor to surrounding LyXText
993 if (!res.dispatched()) {
994 lyxerr << "temp cursor is: " << theTempCursor << endl;
995 lyxerr << "dispatching " << cmd1
996 << " to surrounding LyXText "
997 << theTempCursor.innerText() << endl;
998 bv_->fullCursor(theTempCursor);
999 cmd1.y += bv_->top_y();
1000 res = bv_->fullCursor().innerText()->dispatch(cmd1);
1001 if (bv_->fitCursor() || res.update())
1004 //return DispatchResult(true, true);
1006 // see workAreaKeyPress
1007 cursor_timeout.restart();
1008 screen().showCursor(*bv_);
1010 // skip these when selecting
1011 if (cmd.action != LFUN_MOUSE_MOTION) {
1012 owner_->updateLayoutChoice();
1013 owner_->updateToolbar();
1016 // slight hack: this is only called currently when we
1017 // clicked somewhere, so we force through the display
1018 // of the new status here.
1019 owner_->clearMessage();
1024 owner_->dispatch(cmd);
1030 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1032 // Make sure that the cached BufferView is correct.
1033 FuncRequest ev = ev_in;
1036 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1037 << " action[" << ev.action << ']'
1038 << " arg[" << ev.argument << ']'
1039 << " x[" << ev.x << ']'
1040 << " y[" << ev.y << ']'
1041 << " button[" << ev.button() << ']'
1044 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1046 switch (ev.action) {
1048 case LFUN_SCROLL_INSET:
1049 // this is not handled here as this function is only active
1050 // if we have a locking_inset and that one is (or contains)
1054 case LFUN_FILE_INSERT:
1055 MenuInsertLyXFile(ev.argument);
1058 case LFUN_FILE_INSERT_ASCII_PARA:
1059 InsertAsciiFile(bv_, ev.argument, true);
1062 case LFUN_FILE_INSERT_ASCII:
1063 InsertAsciiFile(bv_, ev.argument, false);
1066 case LFUN_FONT_STATE:
1067 owner_->getLyXFunc().setMessage(currentState(bv_));
1070 case LFUN_INSERT_LABEL: {
1071 // Try and generate a valid label
1072 string const contents = ev.argument.empty() ?
1073 getPossibleLabel(*bv_) : ev.argument;
1074 InsetCommandParams icp("label", contents);
1075 string data = InsetCommandMailer::params2string("label", icp);
1076 owner_->getDialogs().show("label", data, 0);
1080 case LFUN_BOOKMARK_SAVE:
1081 savePosition(strToUnsignedInt(ev.argument));
1084 case LFUN_BOOKMARK_GOTO:
1085 restorePosition(strToUnsignedInt(ev.argument));
1088 case LFUN_REF_GOTO: {
1089 string label = ev.argument;
1090 if (label.empty()) {
1092 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1094 label = inset->getContents();
1100 bv_->gotoLabel(label);
1104 // --- accented characters ---------------------------
1107 case LFUN_CIRCUMFLEX:
1117 case LFUN_SPECIAL_CARON:
1120 case LFUN_HUNG_UMLAUT:
1123 if (ev.argument.empty()) {
1125 owner_->getLyXFunc().handleKeyFunc(ev.action);
1127 owner_->getLyXFunc().handleKeyFunc(ev.action);
1128 owner_->getIntl().getTransManager()
1129 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1134 case LFUN_MATH_MACRO:
1135 case LFUN_MATH_DELIM:
1136 case LFUN_INSERT_MATRIX:
1137 case LFUN_INSERT_MATH:
1138 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1139 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1140 case LFUN_MATH_MODE: // Open or create an inlined math inset
1144 case LFUN_INSET_INSERT: {
1146 BOOST_ASSERT(false);
1147 InsetOld * inset = createInset(ev);
1148 if (!inset || !insertInset(inset))
1153 case LFUN_FLOAT_LIST:
1154 if (tclass.floats().typeExist(ev.argument)) {
1155 InsetOld * inset = new InsetFloatList(ev.argument);
1156 if (!insertInset(inset, tclass.defaultLayoutName()))
1159 lyxerr << "Non-existent float type: "
1160 << ev.argument << endl;
1164 case LFUN_LAYOUT_PARAGRAPH: {
1166 params2string(*bv_->getLyXText()->cursorPar(), data);
1167 data = "show\n" + data;
1168 bv_->owner()->getDialogs().show("paragraph", data);
1172 case LFUN_PARAGRAPH_UPDATE:
1173 updateParagraphDialog();
1176 case LFUN_PARAGRAPH_APPLY:
1177 setParagraphParams(*bv_, ev.argument);
1180 case LFUN_THESAURUS_ENTRY: {
1181 string arg = ev.argument;
1184 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1188 if (arg.size() > 100 || arg.empty()) {
1189 // Get word or selection
1190 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1191 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1192 // FIXME: where is getLyXText()->unselect(bv_) ?
1196 bv_->owner()->getDialogs().show("thesaurus", arg);
1200 case LFUN_TRACK_CHANGES:
1204 case LFUN_MERGE_CHANGES:
1205 owner_->getDialogs().show("changes");
1208 case LFUN_ACCEPT_ALL_CHANGES: {
1209 bv_->text()->setCursor(0, 0);
1210 #warning FIXME changes
1211 while (lyx::find::findNextChange(bv_))
1212 bv_->getLyXText()->acceptChange();
1217 case LFUN_REJECT_ALL_CHANGES: {
1218 bv_->text()->setCursor(0, 0);
1219 #warning FIXME changes
1220 while (lyx::find::findNextChange(bv_))
1221 bv_->getLyXText()->rejectChange();
1226 case LFUN_ACCEPT_CHANGE: {
1227 bv_->getLyXText()->acceptChange();
1232 case LFUN_REJECT_CHANGE: {
1233 bv_->getLyXText()->rejectChange();
1238 case LFUN_WORD_FIND:
1239 lyx::find::find(ev);
1242 case LFUN_WORD_REPLACE:
1243 lyx::find::replace(ev);
1246 case LFUN_UNKNOWN_ACTION:
1247 ev.errorMessage(N_("Unknown function!"));
1251 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_)).dispatched();
1258 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1260 // not quite sure if we want this...
1261 bv_->text()->recUndo(bv_->text()->cursor().par());
1264 bv_->text()->clearSelection();
1265 if (!lout.empty()) {
1266 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1268 if (!bv_->text()->cursorPar()->empty()) {
1269 bv_->text()->cursorLeft(bv_);
1270 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1274 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1275 bool hasLayout = tclass.hasLayout(lres);
1277 bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
1278 bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1280 bv_->fullCursor().innerText()->insertInset(inset);
1286 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1287 string const & from, string const & to)
1289 bool need_update = false;
1290 CursorSlice cur = bv_->text()->cursor();
1292 ParIterator end = bv_->buffer()->par_iterator_end();
1293 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1295 bool changed_inset = false;
1296 for (InsetList::iterator it2 = it->insetlist.begin();
1297 it2 != it->insetlist.end(); ++it2) {
1298 if (it2->inset->lyxCode() == code) {
1299 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1300 if (inset->getContents() == from) {
1301 inset->setContents(to);
1302 changed_inset = true;
1306 if (changed_inset) {
1311 // The test it.size() == 1 was needed to prevent crashes.
1312 // How to set the cursor correctly when it.size() > 1 ??
1313 if (it.size() == 1) {
1314 bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
1315 bv_->text()->redoParagraph(bv_->text()->cursorPar());
1319 bv_->text()->setCursorIntern(cur.par(), cur.pos());
1324 void BufferView::Pimpl::updateParagraphDialog()
1326 if (!bv_->owner()->getDialogs().visible("paragraph"))
1328 Paragraph const & par = *bv_->getLyXText()->cursorPar();
1330 params2string(par, data);
1332 // Will the paragraph accept changes from the dialog?
1333 InsetOld * const inset = par.inInset();
1335 !(inset && inset->forceDefaultParagraphs(inset));
1337 data = "update " + tostr(accept) + '\n' + data;
1338 bv_->owner()->getDialogs().update("paragraph", data);