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;
384 bool selection = false;
385 bool mark_set = false;
389 owner_->message(_("Formatting document..."));
391 lyxerr << "### resizeCurrentBuffer: text " << bv_->text() << endl;
395 par = bv_->text()->cursor.par();
396 pos = bv_->text()->cursor.pos();
397 selstartpar = bv_->text()->selStart().par();
398 selstartpos = bv_->text()->selStart().pos();
399 selendpar = bv_->text()->selEnd().par();
400 selendpos = bv_->text()->selEnd().pos();
401 selection = bv_->text()->selection.set();
402 mark_set = bv_->text()->selection.mark();
403 bv_->text()->textwidth_ = bv_->workWidth();
404 bv_->text()->fullRebreak();
408 bv_->text()->selection.set(true);
409 // At this point just to avoid the Delete-Empty-Paragraph-
410 // Mechanism when setting the cursor.
411 bv_->text()->selection.mark(mark_set);
413 bv_->text()->setCursor(selstartpar, selstartpos);
414 bv_->text()->selection.cursor = bv_->text()->cursor;
415 bv_->text()->setCursor(selendpar, selendpos);
416 bv_->text()->setSelection();
417 bv_->text()->setCursor(par, pos);
419 bv_->text()->setCursor(par, pos);
420 bv_->text()->selection.cursor = bv_->text()->cursor;
421 bv_->text()->selection.set(false);
430 // reset the "Formatting..." message
431 owner_->clearMessage();
437 void BufferView::Pimpl::updateScrollbar()
440 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
441 workarea().setScrollbarParams(0, 0, 0);
445 LyXText const & t = *bv_->text();
447 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
448 << top_y() << ", default height " << defaultRowHeight() << endl;
450 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
454 void BufferView::Pimpl::scrollDocView(int value)
456 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
461 screen().hideCursor();
464 screen().redraw(*bv_);
466 if (!lyxrc.cursor_follows_scrollbar)
469 int const height = defaultRowHeight();
470 int const first = top_y() + height;
471 int const last = top_y() + workarea().workHeight() - height;
473 LyXText * text = bv_->text();
474 if (text->cursorY() < first)
475 text->setCursorFromCoordinates(0, first);
476 else if (text->cursorY() > last)
477 text->setCursorFromCoordinates(0, last);
479 owner_->updateLayoutChoice();
483 void BufferView::Pimpl::scroll(int lines)
488 LyXText const * t = bv_->text();
489 int const line_height = defaultRowHeight();
491 // The new absolute coordinate
492 int new_top_y = top_y() + lines * line_height;
494 // Restrict to a valid value
495 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
496 new_top_y = std::max(0, new_top_y);
498 scrollDocView(new_top_y);
500 // Update the scrollbar.
501 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
505 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
506 key_modifier::state state)
508 bv_->owner()->getLyXFunc().processKeySym(key, state);
510 /* This is perhaps a bit of a hack. When we move
511 * around, or type, it's nice to be able to see
512 * the cursor immediately after the keypress. So
513 * we reset the toggle timeout and force the visibility
514 * of the cursor. Note we cannot do this inside
515 * dispatch() itself, because that's called recursively.
518 cursor_timeout.restart();
519 screen().showCursor(*bv_);
524 void BufferView::Pimpl::selectionRequested()
531 LyXText * text = bv_->getLyXText();
533 if (!text->selection.set()) {
534 xsel_cache_.set = false;
538 if (!xsel_cache_.set ||
539 text->cursor != xsel_cache_.cursor ||
540 text->selection.cursor != xsel_cache_.selection_cursor)
542 xsel_cache_.cursor = text->cursor;
543 xsel_cache_.selection_cursor = text->selection.cursor;
544 xsel_cache_.set = text->selection.set();
545 sel = text->selectionAsString(*bv_->buffer(), false);
547 workarea().putClipboard(sel);
552 void BufferView::Pimpl::selectionLost()
555 screen().hideCursor();
556 bv_->getLyXText()->clearSelection();
557 xsel_cache_.set = false;
562 void BufferView::Pimpl::workAreaResize()
564 static int work_area_width;
565 static int work_area_height;
567 bool const widthChange = workarea().workWidth() != work_area_width;
568 bool const heightChange = workarea().workHeight() != work_area_height;
570 // update from work area
571 work_area_width = workarea().workWidth();
572 work_area_height = workarea().workHeight();
576 // The visible LyXView need a resize
577 resizeCurrentBuffer();
581 if (widthChange || heightChange)
584 // always make sure that the scrollbar is sane.
586 owner_->updateLayoutChoice();
590 void BufferView::Pimpl::update()
592 //lyxerr << "BufferView::update()" << endl;
593 // fix cursor coordinate cache in case something went wrong
595 // check needed to survive LyX startup
596 if (bv_->getLyXText()) {
597 // update all 'visible' paragraphs
598 ParagraphList::iterator beg;
599 ParagraphList::iterator end;
600 getParsInRange(buffer_->paragraphs(),
601 top_y(), top_y() + workarea().workHeight(),
603 bv_->text()->redoParagraphs(beg, end);
606 screen().redraw(*bv_);
610 // Callback for cursor timer
611 void BufferView::Pimpl::cursorToggle()
614 cursor_timeout.restart();
618 screen().toggleCursor(*bv_);
619 cursor_timeout.restart();
623 bool BufferView::Pimpl::available() const
625 return buffer_ && bv_->text();
629 Change const BufferView::Pimpl::getCurrentChange()
631 if (!bv_->buffer()->params().tracking_changes)
632 return Change(Change::UNCHANGED);
634 LyXText * text = bv_->getLyXText();
636 if (!text->selection.set())
637 return Change(Change::UNCHANGED);
639 return text->getPar(text->selStart())
640 ->lookupChangeFull(text->selStart().pos());
644 void BufferView::Pimpl::savePosition(unsigned int i)
646 if (i >= saved_positions_num)
648 saved_positions[i] = Position(buffer_->fileName(),
649 bv_->text()->cursorPar()->id(),
650 bv_->text()->cursor.pos());
652 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
656 void BufferView::Pimpl::restorePosition(unsigned int i)
658 if (i >= saved_positions_num)
661 string const fname = saved_positions[i].filename;
663 bv_->text()->clearSelection();
665 if (fname != buffer_->fileName()) {
667 if (bufferlist.exists(fname))
668 b = bufferlist.getBuffer(fname);
670 b = bufferlist.newBuffer(fname);
671 ::loadLyXFile(b, fname); // don't ask, just load it
677 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
678 if (par == buffer_->par_iterator_end())
681 bv_->text()->setCursor(par.pit(),
682 min(par->size(), saved_positions[i].par_pos));
685 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
689 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
691 return i < saved_positions_num && !saved_positions[i].filename.empty();
695 void BufferView::Pimpl::switchKeyMap()
697 if (!lyxrc.rtl_support)
700 Intl & intl = owner_->getIntl();
701 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
702 if (intl.keymap == Intl::PRIMARY)
705 if (intl.keymap == Intl::SECONDARY)
711 void BufferView::Pimpl::center()
713 LyXText * text = bv_->text();
715 text->clearSelection();
716 int const half_height = workarea().workHeight() / 2;
717 int new_y = std::max(0, text->cursorY() - half_height);
719 // FIXME: look at this comment again ...
720 // This updates top_y() but means the fitCursor() call
721 // from the update(FITCUR) doesn't realise that we might
722 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
723 // the scrollbar to be updated as it should, so we have
724 // to do it manually. Any operation that does a center()
725 // and also might have moved top_y() must make sure to call
726 // updateScrollbar() currently. Never mind that this is a
727 // pretty obfuscated way of updating t->top_y()
732 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
734 workarea().putClipboard(stuff);
738 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
741 LyXCursor cursor = bv_->getLyXText()->cursor;
742 Buffer::inset_iterator it =
743 find_if(Buffer::inset_iterator(
744 cursorPar(), cursor.pos()),
745 buffer_->inset_iterator_end(),
746 lyx::compare_memfun(&Inset::lyxCode, code));
747 return it != buffer_->inset_iterator_end() ? (*it) : 0;
749 // Ok, this is a little bit too brute force but it
750 // should work for now. Better infrastructure is coming. (Lgb)
752 Buffer * b = bv_->buffer();
753 LyXText * text = bv_->getLyXText();
755 Buffer::inset_iterator beg = b->inset_iterator_begin();
756 Buffer::inset_iterator end = b->inset_iterator_end();
758 bool cursor_par_seen = false;
760 for (; beg != end; ++beg) {
761 if (beg.getPar() == text->cursorPar()) {
762 cursor_par_seen = true;
764 if (cursor_par_seen) {
765 if (beg.getPar() == text->cursorPar()
766 && beg.getPos() >= text->cursor.pos()) {
768 } else if (beg.getPar() != text->cursorPar()) {
775 // Now find the first inset that matches code.
776 for (; beg != end; ++beg) {
777 if (beg->lyxCode() == code) {
787 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
789 string filename = filen;
791 if (filename.empty()) {
792 // Launch a file browser
793 string initpath = lyxrc.document_path;
796 string const trypath = owner_->buffer()->filePath();
797 // If directory is writeable, use this as default.
798 if (IsDirWriteable(trypath))
802 FileDialog fileDlg(_("Select LyX document to insert"),
804 make_pair(string(_("Documents|#o#O")),
805 string(lyxrc.document_path)),
806 make_pair(string(_("Examples|#E#e")),
807 string(AddPath(system_lyxdir(), "examples"))));
809 FileDialog::Result result =
810 fileDlg.open(initpath,
811 FileFilterList(_("LyX Documents (*.lyx)")),
814 if (result.first == FileDialog::Later)
817 filename = result.second;
819 // check selected filename
820 if (filename.empty()) {
821 owner_->message(_("Canceled."));
826 // get absolute path of file and add ".lyx" to the filename if
828 filename = FileSearch(string(), filename, "lyx");
830 string const disp_fn = MakeDisplayPath(filename);
831 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
832 if (bv_->insertLyXFile(filename))
833 owner_->message(bformat(_("Document %1$s inserted."),
836 owner_->message(bformat(_("Could not insert document %1$s"),
841 void BufferView::Pimpl::trackChanges()
843 Buffer * buf(bv_->buffer());
844 bool const tracking(buf->params().tracking_changes);
847 ParIterator const end = buf->par_iterator_end();
848 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
850 buf->params().tracking_changes = true;
852 // we cannot allow undos beyond the freeze point
853 buf->undostack().clear();
856 bv_->text()->setCursor(0, 0);
857 #warning changes FIXME
858 bool found = lyx::find::findNextChange(bv_);
860 owner_->getDialogs().show("changes");
864 ParIterator const end = buf->par_iterator_end();
865 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
866 it->untrackChanges();
867 buf->params().tracking_changes = false;
870 buf->redostack().clear();
874 LCursor theTempCursor(0);
878 InsetOld * insetFromCoords(BufferView * bv, int x, int y)
880 lyxerr << "insetFromCoords" << endl;
881 LyXText * text = bv->text();
882 InsetOld * inset = 0;
883 theTempCursor = LCursor(bv);
885 InsetOld * const inset_hit = text->checkInsetHit(x, y);
887 lyxerr << "no further inset hit" << endl;
891 if (!inset->descendable()) {
892 lyxerr << "not descendable" << endl;
895 int const cell = inset->getCell(x, y + bv->top_y());
898 text = inset_hit->getText(cell);
899 lyxerr << "Hit inset: " << inset << " at x: " << x
900 << " text: " << text << " y: " << y << endl;
901 theTempCursor.push(static_cast<UpdatableInset*>(inset));
903 lyxerr << "theTempCursor: " << theTempCursor << endl;
910 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
912 switch (cmd.action) {
913 case LFUN_MOUSE_MOTION: {
916 FuncRequest cmd1(cmd, bv_);
917 UpdatableInset * inset = bv_->cursor().innerInset();
920 cmd1.x -= inset->x();
921 cmd1.y -= inset->y();
922 res = inset->dispatch(cmd1);
924 cmd1.y += bv_->top_y();
925 res = bv_->cursor().innerText()->dispatch(cmd1);
928 if (bv_->fitCursor() || res.update()) {
930 bv_->cursor().updatePos();
936 case LFUN_MOUSE_PRESS:
937 case LFUN_MOUSE_RELEASE:
938 case LFUN_MOUSE_DOUBLE:
939 case LFUN_MOUSE_TRIPLE: {
940 // We pass those directly to the Bufferview, since
941 // otherwise selection handling breaks down
943 // Doesn't go through lyxfunc, so we need to update
944 // the layout choice etc. ourselves
946 // e.g. Qt mouse press when no buffer
950 screen().hideCursor();
952 // either the inset under the cursor or the surrounding LyXText will
953 // handle this event.
955 // built temporary path to inset
956 InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y);
957 FuncRequest cmd1(cmd, bv_);
960 // try to dispatch to that inset
962 FuncRequest cmd2 = cmd1;
963 lyxerr << "dispatching action " << cmd2.action
964 << " to inset " << inset << endl;
965 cmd2.x -= inset->x();
966 cmd2.y -= inset->y();
967 res = inset->dispatch(cmd2);
970 bv_->cursor().updatePos();
979 bv_->cursor() = theTempCursor;
980 bv_->cursor().innerText()->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
981 if (bv_->fitCursor())
985 lyxerr << "not dispatched by inner inset val: " << res.val() << endl;
990 // otherwise set cursor to surrounding LyXText
991 if (!res.dispatched()) {
992 lyxerr << "temp cursor is: " << theTempCursor << endl;
993 lyxerr << "dispatching " << cmd1
994 << " to surrounding LyXText "
995 << theTempCursor.innerText() << endl;
996 bv_->cursor() = theTempCursor;
997 cmd1.y += bv_->top_y();
998 res = bv_->cursor().innerText()->dispatch(cmd1);
999 if (bv_->fitCursor() || res.update())
1002 //return DispatchResult(true, true);
1004 // see workAreaKeyPress
1005 cursor_timeout.restart();
1006 screen().showCursor(*bv_);
1008 // skip these when selecting
1009 if (cmd.action != LFUN_MOUSE_MOTION) {
1010 owner_->updateLayoutChoice();
1011 owner_->updateToolbar();
1014 // slight hack: this is only called currently when we
1015 // clicked somewhere, so we force through the display
1016 // of the new status here.
1017 owner_->clearMessage();
1022 owner_->dispatch(cmd);
1028 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1030 // Make sure that the cached BufferView is correct.
1031 FuncRequest ev = ev_in;
1034 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1035 << " action[" << ev.action << ']'
1036 << " arg[" << ev.argument << ']'
1037 << " x[" << ev.x << ']'
1038 << " y[" << ev.y << ']'
1039 << " button[" << ev.button() << ']'
1042 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1044 switch (ev.action) {
1046 case LFUN_SCROLL_INSET:
1047 // this is not handled here as this function is only active
1048 // if we have a locking_inset and that one is (or contains)
1052 case LFUN_FILE_INSERT:
1053 MenuInsertLyXFile(ev.argument);
1056 case LFUN_FILE_INSERT_ASCII_PARA:
1057 InsertAsciiFile(bv_, ev.argument, true);
1060 case LFUN_FILE_INSERT_ASCII:
1061 InsertAsciiFile(bv_, ev.argument, false);
1064 case LFUN_FONT_STATE:
1065 owner_->getLyXFunc().setMessage(currentState(bv_));
1068 case LFUN_INSERT_LABEL: {
1069 // Try and generate a valid label
1070 string const contents = ev.argument.empty() ?
1071 getPossibleLabel(*bv_) : ev.argument;
1072 InsetCommandParams icp("label", contents);
1073 string data = InsetCommandMailer::params2string("label", icp);
1074 owner_->getDialogs().show("label", data, 0);
1078 case LFUN_BOOKMARK_SAVE:
1079 savePosition(strToUnsignedInt(ev.argument));
1082 case LFUN_BOOKMARK_GOTO:
1083 restorePosition(strToUnsignedInt(ev.argument));
1086 case LFUN_REF_GOTO: {
1087 string label = ev.argument;
1088 if (label.empty()) {
1090 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1092 label = inset->getContents();
1098 bv_->gotoLabel(label);
1102 // --- accented characters ---------------------------
1105 case LFUN_CIRCUMFLEX:
1115 case LFUN_SPECIAL_CARON:
1118 case LFUN_HUNG_UMLAUT:
1121 if (ev.argument.empty()) {
1123 owner_->getLyXFunc().handleKeyFunc(ev.action);
1125 owner_->getLyXFunc().handleKeyFunc(ev.action);
1126 owner_->getIntl().getTransManager()
1127 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1132 case LFUN_MATH_MACRO:
1133 case LFUN_MATH_DELIM:
1134 case LFUN_INSERT_MATRIX:
1135 case LFUN_INSERT_MATH:
1136 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1137 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1138 case LFUN_MATH_MODE: // Open or create an inlined math inset
1142 case LFUN_INSET_INSERT: {
1144 BOOST_ASSERT(false);
1145 InsetOld * inset = createInset(ev);
1146 if (!inset || !insertInset(inset))
1151 case LFUN_FLOAT_LIST:
1152 if (tclass.floats().typeExist(ev.argument)) {
1153 InsetOld * inset = new InsetFloatList(ev.argument);
1154 if (!insertInset(inset, tclass.defaultLayoutName()))
1157 lyxerr << "Non-existent float type: "
1158 << ev.argument << endl;
1162 case LFUN_LAYOUT_PARAGRAPH: {
1164 params2string(*bv_->getLyXText()->cursorPar(), data);
1165 data = "show\n" + data;
1166 bv_->owner()->getDialogs().show("paragraph", data);
1170 case LFUN_PARAGRAPH_UPDATE:
1171 updateParagraphDialog();
1174 case LFUN_PARAGRAPH_APPLY:
1175 setParagraphParams(*bv_, ev.argument);
1178 case LFUN_THESAURUS_ENTRY: {
1179 string arg = ev.argument;
1182 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1186 if (arg.size() > 100 || arg.empty()) {
1187 // Get word or selection
1188 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1189 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1190 // FIXME: where is getLyXText()->unselect(bv_) ?
1194 bv_->owner()->getDialogs().show("thesaurus", arg);
1198 case LFUN_TRACK_CHANGES:
1202 case LFUN_MERGE_CHANGES:
1203 owner_->getDialogs().show("changes");
1206 case LFUN_ACCEPT_ALL_CHANGES: {
1207 bv_->text()->setCursor(0, 0);
1208 #warning FIXME changes
1209 while (lyx::find::findNextChange(bv_))
1210 bv_->getLyXText()->acceptChange();
1215 case LFUN_REJECT_ALL_CHANGES: {
1216 bv_->text()->setCursor(0, 0);
1217 #warning FIXME changes
1218 while (lyx::find::findNextChange(bv_))
1219 bv_->getLyXText()->rejectChange();
1224 case LFUN_ACCEPT_CHANGE: {
1225 bv_->getLyXText()->acceptChange();
1230 case LFUN_REJECT_CHANGE: {
1231 bv_->getLyXText()->rejectChange();
1236 case LFUN_WORD_FIND:
1237 lyx::find::find(ev);
1240 case LFUN_WORD_REPLACE:
1241 lyx::find::replace(ev);
1244 case LFUN_UNKNOWN_ACTION:
1245 ev.errorMessage(N_("Unknown function!"));
1249 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_)).dispatched();
1256 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1258 // not quite sure if we want this...
1259 bv_->text()->recUndo(bv_->text()->cursor.par());
1262 bv_->text()->clearSelection();
1263 if (!lout.empty()) {
1264 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1266 if (!bv_->text()->cursorPar()->empty()) {
1267 bv_->text()->cursorLeft(bv_);
1268 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1272 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1273 bool hasLayout = tclass.hasLayout(lres);
1275 bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
1276 bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1278 bv_->cursor().innerText()->insertInset(inset);
1284 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1285 string const & from, string const & to)
1287 bool need_update = false;
1288 LyXCursor cursor = bv_->text()->cursor;
1289 LyXCursor tmpcursor = cursor;
1290 cursor.par(tmpcursor.par());
1291 cursor.pos(tmpcursor.pos());
1293 ParIterator end = bv_->buffer()->par_iterator_end();
1294 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1296 bool changed_inset = false;
1297 for (InsetList::iterator it2 = it->insetlist.begin();
1298 it2 != it->insetlist.end(); ++it2) {
1299 if (it2->inset->lyxCode() == code) {
1300 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1301 if (inset->getContents() == from) {
1302 inset->setContents(to);
1303 changed_inset = true;
1307 if (changed_inset) {
1312 // The test it.size()==1 was needed to prevent crashes.
1313 // How to set the cursor correctly when it.size()>1 ??
1314 if (it.size() == 1) {
1315 bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
1316 bv_->text()->redoParagraph(bv_->text()->cursorPar());
1320 bv_->text()->setCursorIntern(cursor.par(), cursor.pos());
1325 void BufferView::Pimpl::updateParagraphDialog()
1327 if (!bv_->owner()->getDialogs().visible("paragraph"))
1329 Paragraph const & par = *bv_->getLyXText()->cursorPar();
1331 params2string(par, data);
1333 // Will the paragraph accept changes from the dialog?
1334 InsetOld * const inset = par.inInset();
1336 !(inset && inset->forceDefaultParagraphs(inset));
1338 data = "update " + tostr(accept) + '\n' + data;
1339 bv_->owner()->getDialogs().update("paragraph", data);