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"
22 #include "bufferlist.h"
24 #include "buffer_funcs.h"
25 #include "bufferview_funcs.h"
29 #include "FloatList.h"
30 #include "funcrequest.h"
33 #include "iterators.h"
35 #include "lyx_cb.h" // added for Dispatch functions
41 #include "lastfiles.h"
42 #include "paragraph.h"
43 #include "ParagraphParameters.h"
44 #include "TextCache.h"
45 #include "undo_funcs.h"
47 #include "insets/insetfloatlist.h"
48 #include "insets/insetgraphics.h"
49 #include "insets/insetinclude.h"
50 #include "insets/insetref.h"
51 #include "insets/insettext.h"
53 #include "frontends/Alert.h"
54 #include "frontends/Dialogs.h"
55 #include "frontends/FileDialog.h"
56 #include "frontends/LyXView.h"
57 #include "frontends/LyXScreenFactory.h"
58 #include "frontends/mouse_state.h"
59 #include "frontends/screen.h"
60 #include "frontends/WorkArea.h"
61 #include "frontends/WorkAreaFactory.h"
63 #include "mathed/formulabase.h"
65 #include "graphics/Previews.h"
67 #include "support/LAssert.h"
68 #include "support/tostr.h"
69 #include "support/filetools.h"
70 #include "support/path_defines.h"
72 #include <boost/bind.hpp>
73 #include <boost/signals/connection.hpp>
88 using namespace lyx::support;
89 using namespace bv_funcs;
91 extern BufferList bufferlist;
96 unsigned int const saved_positions_num = 20;
98 // All the below connection objects are needed because of a bug in some
99 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
100 // to these connections we avoid a segfault upon startup, and also at exit.
103 boost::signals::connection dispatchcon;
104 boost::signals::connection timecon;
105 boost::signals::connection doccon;
106 boost::signals::connection resizecon;
107 boost::signals::connection kpresscon;
108 boost::signals::connection selectioncon;
109 boost::signals::connection lostcon;
115 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
116 int xpos, int ypos, int width, int height)
117 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
118 using_xterm_cursor(false)
120 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
121 screen_.reset(LyXScreenFactory::create(workarea()));
124 doccon = workarea().scrollDocView
125 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
126 resizecon = workarea().workAreaResize
127 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
128 dispatchcon = workarea().dispatch
129 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
130 kpresscon = workarea().workAreaKeyPress
131 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
132 selectioncon = workarea().selectionRequested
133 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
134 lostcon = workarea().selectionLost
135 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
137 timecon = cursor_timeout.timeout
138 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
139 cursor_timeout.start();
140 saved_positions.resize(saved_positions_num);
144 void BufferView::Pimpl::addError(ErrorItem const & ei)
146 errorlist_.push_back(ei);
150 void BufferView::Pimpl::showReadonly(bool)
152 owner_->updateWindowTitle();
153 owner_->getDialogs().updateBufferDependent(false);
157 void BufferView::Pimpl::connectBuffer(Buffer & buf)
159 if (errorConnection_.connected())
162 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
163 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
164 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
165 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
166 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
167 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
168 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
172 void BufferView::Pimpl::disconnectBuffer()
174 errorConnection_.disconnect();
175 messageConnection_.disconnect();
176 busyConnection_.disconnect();
177 titleConnection_.disconnect();
178 timerConnection_.disconnect();
179 readonlyConnection_.disconnect();
180 closingConnection_.disconnect();
184 bool BufferView::Pimpl::newFile(string const & filename,
185 string const & tname,
188 Buffer * b = ::newFile(filename, tname, isNamed);
194 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
196 // get absolute path of file and add ".lyx" to the filename if
198 string s = FileSearch(string(), filename, "lyx");
200 bool const found = !s.empty();
205 // file already open?
206 if (bufferlist.exists(s)) {
207 string const file = MakeDisplayPath(s, 20);
208 string text = bformat(_("The document %1$s is already "
209 "loaded.\n\nDo you want to revert "
210 "to the saved version?"), file);
211 int const ret = Alert::prompt(_("Revert to saved document?"),
212 text, 0, 1, _("&Revert"), _("&Switch to document"));
215 buffer(bufferlist.getBuffer(s));
218 // FIXME: should be LFUN_REVERT
219 if (!bufferlist.close(bufferlist.getBuffer(s), false))
221 // Fall through to new load. (Asger)
228 b = bufferlist.newBuffer(s);
230 if (!::loadLyXFile(b, s)) {
231 bufferlist.release(b);
235 string text = bformat(_("The document %1$s does not yet "
236 "exist.\n\nDo you want to create "
237 "a new document?"), s);
238 int const ret = Alert::prompt(_("Create new document?"),
239 text, 0, 1, _("&Create"), _("Cancel"));
242 b = ::newFile(s, string(), true);
248 bv_->showErrorList(_("Parse"));
251 lastfiles->newFile(b->fileName());
257 WorkArea & BufferView::Pimpl::workarea() const
259 return *workarea_.get();
263 LyXScreen & BufferView::Pimpl::screen() const
265 return *screen_.get();
269 Painter & BufferView::Pimpl::painter() const
271 return workarea().getPainter();
275 void BufferView::Pimpl::buffer(Buffer * b)
277 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
281 // Put the old text into the TextCache, but
282 // only if the buffer is still loaded.
283 // Also set the owner of the test to 0
284 // bv_->text->owner(0);
285 textcache.add(buffer_, workarea().workWidth(), bv_->text);
286 if (lyxerr.debugging())
287 textcache.show(lyxerr, "BufferView::buffer");
292 // set current buffer
295 // if we're quitting lyx, don't bother updating stuff
299 // if we are closing the buffer, use the first buffer as current
301 buffer_ = bufferlist.first();
304 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
305 connectBuffer(*buffer_);
307 // If we don't have a text object for this, we make one
309 resizeCurrentBuffer();
311 // FIXME: needed when ?
312 bv_->text->top_y(screen().topCursorVisible(bv_->text));
314 // Buffer-dependent dialogs should be updated or
315 // hidden. This should go here because some dialogs (eg ToC)
316 // require bv_->text.
317 owner_->getDialogs().updateBufferDependent(true);
319 lyxerr[Debug::INFO] << " No Buffer!" << endl;
320 owner_->getDialogs().hideBufferDependent();
322 // Also remove all remaining text's from the testcache.
323 // (there should not be any!) (if there is any it is a
325 if (lyxerr.debugging())
326 textcache.show(lyxerr, "buffer delete all");
332 owner_->updateMenubar();
333 owner_->updateToolbar();
334 owner_->updateLayoutChoice();
335 owner_->updateWindowTitle();
338 // Don't forget to update the Layout
339 string const layoutname =
340 bv_->text->cursor.par()->layout()->name();
341 owner_->setLayout(layoutname);
344 if (lyx::graphics::Previews::activated() && buffer_)
345 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
349 bool BufferView::Pimpl::fitCursor()
353 if (bv_->theLockingInset()) {
354 bv_->theLockingInset()->fitInsetCursor(bv_);
357 ret = screen().fitCursor(bv_->text, bv_);
360 //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
362 // We need to always update, in case we did a
363 // paste and we stayed anchored to a row, but
364 // the actual height of the doc changed ...
370 void BufferView::Pimpl::redoCurrentBuffer()
372 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
373 if (buffer_ && bv_->text) {
374 resizeCurrentBuffer();
376 owner_->updateLayoutChoice();
382 void BufferView::Pimpl::resizeCurrentBuffer()
384 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
386 ParagraphList::iterator par;
387 ParagraphList::iterator selstartpar;
388 ParagraphList::iterator selendpar;
389 UpdatableInset * the_locking_inset = 0;
392 pos_type selstartpos = 0;
393 pos_type selendpos = 0;
394 bool selection = false;
395 bool mark_set = false;
399 owner_->message(_("Formatting document..."));
402 par = bv_->text->cursor.par();
403 pos = bv_->text->cursor.pos();
404 selstartpar = bv_->text->selection.start.par();
405 selstartpos = bv_->text->selection.start.pos();
406 selendpar = bv_->text->selection.end.par();
407 selendpos = bv_->text->selection.end.pos();
408 selection = bv_->text->selection.set();
409 mark_set = bv_->text->selection.mark();
410 the_locking_inset = bv_->theLockingInset();
411 bv_->text->fullRebreak();
414 lyxerr << "text not available!" << endl;
415 // See if we have a text in TextCache that fits
416 // the new buffer_ with the correct width.
417 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
419 lyxerr << "text in cache!" << endl;
420 if (lyxerr.debugging()) {
421 lyxerr << "Found a LyXText that fits:" << endl;
422 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
424 // Set the owner of the newly found text
425 // bv_->text->owner(bv_);
426 if (lyxerr.debugging())
427 textcache.show(lyxerr, "resizeCurrentBuffer");
429 lyxerr << "no text in cache!" << endl;
430 bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs);
431 bv_->text->init(bv_);
434 par = bv_->text->ownerParagraphs().end();
435 selstartpar = bv_->text->ownerParagraphs().end();
436 selendpar = bv_->text->ownerParagraphs().end();
439 #warning does not help much
440 bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
441 bv_->text->ownerParagraphs().end());
443 if (par != bv_->text->ownerParagraphs().end()) {
444 bv_->text->selection.set(true);
445 // At this point just to avoid the Delete-Empty-Paragraph-
446 // Mechanism when setting the cursor.
447 bv_->text->selection.mark(mark_set);
449 bv_->text->setCursor(selstartpar, selstartpos);
450 bv_->text->selection.cursor = bv_->text->cursor;
451 bv_->text->setCursor(selendpar, selendpos);
452 bv_->text->setSelection();
453 bv_->text->setCursor(par, pos);
455 bv_->text->setCursor(par, pos);
456 bv_->text->selection.cursor = bv_->text->cursor;
457 bv_->text->selection.set(false);
459 // remake the inset locking
460 bv_->theLockingInset(the_locking_inset);
463 bv_->text->top_y(screen().topCursorVisible(bv_->text));
468 // reset the "Formatting..." message
469 owner_->clearMessage();
475 void BufferView::Pimpl::updateScrollbar()
478 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
479 workarea().setScrollbarParams(0, 0, 0);
483 LyXText const & t = *bv_->text;
485 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
486 << t.top_y() << ", default height " << defaultRowHeight() << endl;
488 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
492 void BufferView::Pimpl::scrollDocView(int value)
494 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
499 screen().hideCursor();
501 bv_->text->top_y(value);
504 if (!lyxrc.cursor_follows_scrollbar)
507 LyXText * vbt = bv_->text;
509 int const height = defaultRowHeight();
510 int const first = static_cast<int>((bv_->text->top_y() + height));
511 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
513 if (vbt->cursor.y() < first)
514 vbt->setCursorFromCoordinates(0, first);
515 else if (vbt->cursor.y() > last)
516 vbt->setCursorFromCoordinates(0, last);
518 owner_->updateLayoutChoice();
522 void BufferView::Pimpl::scroll(int lines)
528 LyXText const * t = bv_->text;
529 int const line_height = defaultRowHeight();
531 // The new absolute coordinate
532 int new_top_y = t->top_y() + lines * line_height;
534 // Restrict to a valid value
535 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
536 new_top_y = std::max(0, new_top_y);
538 scrollDocView(new_top_y);
540 // Update the scrollbar.
541 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
545 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
546 key_modifier::state state)
548 bv_->owner()->getLyXFunc().processKeySym(key, state);
550 /* This is perhaps a bit of a hack. When we move
551 * around, or type, it's nice to be able to see
552 * the cursor immediately after the keypress. So
553 * we reset the toggle timeout and force the visibility
554 * of the cursor. Note we cannot do this inside
555 * dispatch() itself, because that's called recursively.
558 cursor_timeout.restart();
559 screen().showCursor(*bv_);
564 void BufferView::Pimpl::selectionRequested()
571 LyXText * text = bv_->getLyXText();
573 if (text->selection.set() &&
574 (!bv_->text->xsel_cache.set() ||
575 text->selection.start != bv_->text->xsel_cache.start ||
576 text->selection.end != bv_->text->xsel_cache.end))
578 bv_->text->xsel_cache = text->selection;
579 sel = text->selectionAsString(bv_->buffer(), false);
580 } else if (!text->selection.set()) {
582 bv_->text->xsel_cache.set(false);
585 workarea().putClipboard(sel);
590 void BufferView::Pimpl::selectionLost()
593 screen().hideCursor();
594 bv_->getLyXText()->clearSelection();
595 bv_->text->xsel_cache.set(false);
600 void BufferView::Pimpl::workAreaResize()
602 static int work_area_width;
603 static int work_area_height;
605 bool const widthChange = workarea().workWidth() != work_area_width;
606 bool const heightChange = workarea().workHeight() != work_area_height;
608 // update from work area
609 work_area_width = workarea().workWidth();
610 work_area_height = workarea().workHeight();
614 // The visible LyXView need a resize
615 resizeCurrentBuffer();
617 // Remove all texts from the textcache
618 // This is not _really_ what we want to do. What
619 // we really want to do is to delete in textcache
620 // that does not have a BufferView with matching
621 // width, but as long as we have only one BufferView
622 // deleting all gives the same result.
623 if (lyxerr.debugging())
624 textcache.show(lyxerr, "Expose delete all");
629 if (widthChange || heightChange)
632 // always make sure that the scrollbar is sane.
634 owner_->updateLayoutChoice();
638 void BufferView::Pimpl::update()
640 lyxerr << "BufferView::update()" << endl;
641 // fix cursor coordinate cache in case something went wrong
642 if (bv_->getLyXText()) {
643 // check needed to survive LyX startup
644 bv_->getLyXText()->redoCursor();
647 screen().redraw(*bv_);
651 // Callback for cursor timer
652 void BufferView::Pimpl::cursorToggle()
655 cursor_timeout.restart();
659 screen().toggleCursor(*bv_);
661 cursor_timeout.restart();
665 bool BufferView::Pimpl::available() const
667 if (buffer_ && bv_->text)
673 Change const BufferView::Pimpl::getCurrentChange()
675 if (!bv_->buffer()->params.tracking_changes)
676 return Change(Change::UNCHANGED);
678 LyXText * text = bv_->getLyXText();
680 if (!text->selection.set())
681 return Change(Change::UNCHANGED);
683 LyXCursor const & cur = text->selection.start;
684 return cur.par()->lookupChangeFull(cur.pos());
688 void BufferView::Pimpl::beforeChange(LyXText * text)
690 text->clearSelection();
694 void BufferView::Pimpl::savePosition(unsigned int i)
696 if (i >= saved_positions_num)
698 saved_positions[i] = Position(buffer_->fileName(),
699 bv_->text->cursor.par()->id(),
700 bv_->text->cursor.pos());
702 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
706 void BufferView::Pimpl::restorePosition(unsigned int i)
708 if (i >= saved_positions_num)
711 string const fname = saved_positions[i].filename;
713 beforeChange(bv_->text);
715 if (fname != buffer_->fileName()) {
717 if (bufferlist.exists(fname))
718 b = bufferlist.getBuffer(fname);
720 b = bufferlist.newBuffer(fname);
721 ::loadLyXFile(b, fname); // don't ask, just load it
727 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
728 if (par == buffer_->par_iterator_end())
731 bv_->text->setCursor(par.pit(),
732 min(par->size(), saved_positions[i].par_pos));
736 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
740 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
742 if (i >= saved_positions_num)
745 return !saved_positions[i].filename.empty();
749 void BufferView::Pimpl::switchKeyMap()
751 if (!lyxrc.rtl_support)
754 LyXText * text = bv_->getLyXText();
755 if (text->real_current_font.isRightToLeft()
756 && !(bv_->theLockingInset()
757 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
759 if (owner_->getIntl().keymap == Intl::PRIMARY)
760 owner_->getIntl().KeyMapSec();
762 if (owner_->getIntl().keymap == Intl::SECONDARY)
763 owner_->getIntl().KeyMapPrim();
768 void BufferView::Pimpl::insetUnlock()
770 if (bv_->theLockingInset()) {
771 bv_->theLockingInset()->insetUnlock(bv_);
772 bv_->theLockingInset(0);
778 void BufferView::Pimpl::center()
780 LyXText * text = bv_->text;
783 int const half_height = workarea().workHeight() / 2;
786 if (text->cursor.y() > half_height)
787 new_y = text->cursor.y() - half_height;
789 // FIXME: look at this comment again ...
791 // FIXME: can we do this w/o calling screen directly ?
792 // This updates top_y() but means the fitCursor() call
793 // from the update(FITCUR) doesn't realise that we might
794 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
795 // the scrollbar to be updated as it should, so we have
796 // to do it manually. Any operation that does a center()
797 // and also might have moved top_y() must make sure to call
798 // updateScrollbar() currently. Never mind that this is a
799 // pretty obfuscated way of updating t->top_y()
806 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
808 workarea().putClipboard(stuff);
813 * Dispatch functions for actions which can be valid for BufferView->text
814 * and/or InsetText->text!!!
818 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
821 LyXCursor cursor = bv_->getLyXText()->cursor;
822 Buffer::inset_iterator it =
823 find_if(Buffer::inset_iterator(
824 cursor.par(), cursor.pos()),
825 buffer_->inset_iterator_end(),
826 lyx::compare_memfun(&Inset::lyxCode, code));
827 return it != buffer_->inset_iterator_end() ? (*it) : 0;
829 // Ok, this is a little bit too brute force but it
830 // should work for now. Better infrastructure is coming. (Lgb)
832 Buffer * b = bv_->buffer();
833 LyXCursor cursor = bv_->getLyXText()->cursor;
835 Buffer::inset_iterator beg = b->inset_iterator_begin();
836 Buffer::inset_iterator end = b->inset_iterator_end();
838 bool cursor_par_seen = false;
840 for (; beg != end; ++beg) {
841 if (beg.getPar() == cursor.par()) {
842 cursor_par_seen = true;
844 if (cursor_par_seen) {
845 if (beg.getPar() == cursor.par()
846 && beg.getPos() >= cursor.pos()) {
848 } else if (beg.getPar() != cursor.par()) {
855 // Now find the first inset that matches code.
856 for (; beg != end; ++beg) {
857 if (beg->lyxCode() == code) {
867 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
869 string filename = filen;
871 if (filename.empty()) {
872 // Launch a file browser
873 string initpath = lyxrc.document_path;
876 string const trypath = owner_->buffer()->filePath();
877 // If directory is writeable, use this as default.
878 if (IsDirWriteable(trypath))
882 FileDialog fileDlg(_("Select LyX document to insert"),
884 make_pair(string(_("Documents|#o#O")),
885 string(lyxrc.document_path)),
886 make_pair(string(_("Examples|#E#e")),
887 string(AddPath(system_lyxdir(), "examples"))));
889 FileDialog::Result result =
890 fileDlg.open(initpath,
891 _("*.lyx| LyX Documents (*.lyx)"));
893 if (result.first == FileDialog::Later)
896 filename = result.second;
898 // check selected filename
899 if (filename.empty()) {
900 owner_->message(_("Canceled."));
905 // get absolute path of file and add ".lyx" to the filename if
907 filename = FileSearch(string(), filename, "lyx");
909 string const disp_fn = MakeDisplayPath(filename);
910 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
911 if (bv_->insertLyXFile(filename))
912 owner_->message(bformat(_("Document %1$s inserted."),
915 owner_->message(bformat(_("Could not insert document %1$s"),
920 void BufferView::Pimpl::trackChanges()
922 Buffer * buf(bv_->buffer());
923 bool const tracking(buf->params.tracking_changes);
926 ParIterator const end = buf->par_iterator_end();
927 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
929 buf->params.tracking_changes = true;
931 // we cannot allow undos beyond the freeze point
932 buf->undostack.clear();
935 bv_->text->setCursor(buf->paragraphs.begin(), 0);
936 #warning changes FIXME
937 //moveCursorUpdate(false);
939 bool found = lyx::find::findNextChange(bv_);
941 owner_->getDialogs().show("changes");
945 ParIterator const end = buf->par_iterator_end();
946 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
947 it->untrackChanges();
948 buf->params.tracking_changes = false;
951 buf->redostack.clear();
955 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
958 case LFUN_MOUSE_PRESS:
959 case LFUN_MOUSE_MOTION:
960 case LFUN_MOUSE_RELEASE:
961 case LFUN_MOUSE_DOUBLE:
962 case LFUN_MOUSE_TRIPLE:
964 // We pass those directly to the Bufferview, since
965 // otherwise selection handling breaks down
967 // Doesn't go through lyxfunc, so we need to update
968 // the layout choice etc. ourselves
970 // e.g. Qt mouse press when no buffer
974 screen().hideCursor();
976 bool const res = dispatch(ev);
978 // see workAreaKeyPress
979 cursor_timeout.restart();
980 screen().showCursor(*bv_);
982 // FIXME: we should skip these when selecting
983 owner_->updateLayoutChoice();
984 owner_->updateToolbar();
987 // slight hack: this is only called currently when we
988 // clicked somewhere, so we force through the display
989 // of the new status here.
990 owner_->clearMessage();
995 owner_->dispatch(ev);
1001 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1003 // Make sure that the cached BufferView is correct.
1004 FuncRequest ev = ev_in;
1007 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1008 << " action[" << ev.action << ']'
1009 << " arg[" << ev.argument << ']'
1010 << " x[" << ev.x << ']'
1011 << " y[" << ev.y << ']'
1012 << " button[" << ev.button() << ']'
1015 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1017 switch (ev.action) {
1019 case LFUN_SCROLL_INSET:
1020 // this is not handled here as this function is only active
1021 // if we have a locking_inset and that one is (or contains)
1025 case LFUN_FILE_INSERT:
1026 MenuInsertLyXFile(ev.argument);
1029 case LFUN_FILE_INSERT_ASCII_PARA:
1030 InsertAsciiFile(bv_, ev.argument, true);
1033 case LFUN_FILE_INSERT_ASCII:
1034 InsertAsciiFile(bv_, ev.argument, false);
1038 lang(bv_, ev.argument);
1040 owner_->view_state_changed();
1045 owner_->view_state_changed();
1050 owner_->view_state_changed();
1055 owner_->view_state_changed();
1060 owner_->view_state_changed();
1065 owner_->view_state_changed();
1070 owner_->view_state_changed();
1075 owner_->view_state_changed();
1078 case LFUN_UNDERLINE:
1080 owner_->view_state_changed();
1083 case LFUN_FONT_SIZE:
1084 fontSize(bv_, ev.argument);
1085 owner_->view_state_changed();
1088 case LFUN_FONT_STATE:
1089 owner_->getLyXFunc().setMessage(currentState(bv_));
1092 case LFUN_INSERT_LABEL: {
1093 // Try and generate a valid label
1094 string const contents = ev.argument.empty() ?
1095 getPossibleLabel(*bv_) : ev.argument;
1096 InsetCommandParams icp("label", contents);
1097 string data = InsetCommandMailer::params2string("label", icp);
1098 owner_->getDialogs().show("label", data, 0);
1102 case LFUN_BOOKMARK_SAVE:
1103 savePosition(strToUnsignedInt(ev.argument));
1106 case LFUN_BOOKMARK_GOTO:
1107 restorePosition(strToUnsignedInt(ev.argument));
1110 case LFUN_REF_GOTO: {
1111 string label = ev.argument;
1112 if (label.empty()) {
1114 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1116 label = inset->getContents();
1122 bv_->gotoLabel(label);
1126 // --- accented characters ---------------------------
1129 case LFUN_CIRCUMFLEX:
1139 case LFUN_SPECIAL_CARON:
1142 case LFUN_HUNG_UMLAUT:
1145 if (ev.argument.empty()) {
1147 owner_->getLyXFunc().handleKeyFunc(ev.action);
1149 owner_->getLyXFunc().handleKeyFunc(ev.action);
1150 owner_->getIntl().getTransManager()
1151 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1156 case LFUN_MATH_MACRO:
1157 case LFUN_MATH_DELIM:
1158 case LFUN_INSERT_MATRIX:
1159 case LFUN_INSERT_MATH:
1160 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1161 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1162 case LFUN_MATH_MODE: // Open or create an inlined math inset
1166 case LFUN_INSET_APPLY: {
1167 string const name = ev.getArg(0);
1169 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1171 // This works both for 'original' and 'mathed' insets.
1172 // Note that the localDispatch performs updateInset
1174 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1175 inset->localDispatch(fr);
1177 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1183 case LFUN_INSET_INSERT: {
1184 InsetOld * inset = createInset(ev);
1185 if (inset && insertInset(inset)) {
1188 string const name = ev.getArg(0);
1189 if (name == "bibitem") {
1190 // We need to do a redraw because the maximum
1191 // InsetBibitem width could have changed
1192 #warning check whether the update() is needed at all
1201 case LFUN_FLOAT_LIST:
1202 if (tclass.floats().typeExist(ev.argument)) {
1203 InsetOld * inset = new InsetFloatList(ev.argument);
1204 if (!insertInset(inset, tclass.defaultLayoutName()))
1207 lyxerr << "Non-existent float type: "
1208 << ev.argument << endl;
1212 case LFUN_LAYOUT_PARAGRAPH: {
1214 params2string(*bv_->getLyXText()->cursor.par(), data);
1216 data = "show\n" + data;
1217 bv_->owner()->getDialogs().show("paragraph", data);
1221 case LFUN_PARAGRAPH_UPDATE: {
1222 if (!bv_->owner()->getDialogs().visible("paragraph"))
1224 Paragraph const & par = *bv_->getLyXText()->cursor.par();
1227 params2string(par, data);
1229 // Will the paragraph accept changes from the dialog?
1230 InsetOld * const inset = par.inInset();
1232 !(inset && inset->forceDefaultParagraphs(inset));
1234 data = "update " + tostr(accept) + '\n' + data;
1235 bv_->owner()->getDialogs().update("paragraph", data);
1239 case LFUN_PARAGRAPH_APPLY:
1240 setParagraphParams(*bv_, ev.argument);
1243 case LFUN_THESAURUS_ENTRY:
1245 string arg = ev.argument;
1248 arg = bv_->getLyXText()->selectionAsString(buffer_,
1252 if (arg.size() > 100 || arg.empty()) {
1253 // Get word or selection
1254 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1255 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1256 // FIXME: where is getLyXText()->unselect(bv_) ?
1260 bv_->owner()->getDialogs().show("thesaurus", arg);
1264 case LFUN_TRACK_CHANGES:
1268 case LFUN_MERGE_CHANGES:
1269 owner_->getDialogs().show("changes");
1272 case LFUN_ACCEPT_ALL_CHANGES: {
1273 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1274 #warning FIXME changes
1275 //moveCursorUpdate(false);
1277 while (lyx::find::findNextChange(bv_))
1278 bv_->getLyXText()->acceptChange();
1284 case LFUN_REJECT_ALL_CHANGES: {
1285 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1286 #warning FIXME changes
1287 //moveCursorUpdate(false);
1289 while (lyx::find::findNextChange(bv_))
1290 bv_->getLyXText()->rejectChange();
1296 case LFUN_ACCEPT_CHANGE: {
1297 bv_->getLyXText()->acceptChange();
1302 case LFUN_REJECT_CHANGE: {
1303 bv_->getLyXText()->rejectChange();
1308 case LFUN_UNKNOWN_ACTION:
1309 ev.errorMessage(N_("Unknown function!"));
1313 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1320 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1322 // if we are in a locking inset we should try to insert the
1323 // inset there otherwise this is a illegal function now
1324 if (bv_->theLockingInset()) {
1325 if (bv_->theLockingInset()->insetAllowed(inset))
1326 return bv_->theLockingInset()->insertInset(bv_, inset);
1330 // not quite sure if we want this...
1331 recordUndo(bv_, Undo::ATOMIC);
1334 beforeChange(bv_->text);
1335 if (!lout.empty()) {
1336 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1338 if (!bv_->text->cursor.par()->empty()) {
1339 bv_->text->cursorLeft(bv_);
1340 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1344 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1345 bool hasLayout = tclass.hasLayout(lres);
1346 string lay = tclass.defaultLayoutName();
1348 if (hasLayout != false) {
1352 // layout not fount using default
1353 lay = tclass.defaultLayoutName();
1356 bv_->text->setLayout(lay);
1358 bv_->text->setParagraph(0, 0,
1360 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1367 bv_->text->insertInset(inset);
1375 void BufferView::Pimpl::updateInset()
1380 // this should not be needed, but it is...
1381 bv_->text->redoParagraph(bv_->text->cursor.par());