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"
29 #include "FloatList.h"
30 #include "funcrequest.h"
33 #include "iterators.h"
34 #include "lyx_cb.h" // added for Dispatch functions
40 #include "lastfiles.h"
41 #include "paragraph.h"
42 #include "paragraph_funcs.h"
43 #include "ParagraphParameters.h"
44 #include "TextCache.h"
45 #include "undo_funcs.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>
72 using bv_funcs::currentState;
74 using bv_funcs::fontSize;
77 using bv_funcs::roman;
79 using bv_funcs::styleReset;
80 using bv_funcs::underline;
84 using lyx::support::AddPath;
85 using lyx::support::bformat;
86 using lyx::support::FileSearch;
87 using lyx::support::IsDirWriteable;
88 using lyx::support::MakeDisplayPath;
89 using lyx::support::strToUnsignedInt;
90 using lyx::support::system_lyxdir;
98 extern BufferList bufferlist;
103 unsigned int const saved_positions_num = 20;
105 // All the below connection objects are needed because of a bug in some
106 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
107 // to these connections we avoid a segfault upon startup, and also at exit.
110 boost::signals::connection dispatchcon;
111 boost::signals::connection timecon;
112 boost::signals::connection doccon;
113 boost::signals::connection resizecon;
114 boost::signals::connection kpresscon;
115 boost::signals::connection selectioncon;
116 boost::signals::connection lostcon;
122 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
123 int xpos, int ypos, int width, int height)
124 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
125 using_xterm_cursor(false)
127 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
128 screen_.reset(LyXScreenFactory::create(workarea()));
131 doccon = workarea().scrollDocView
132 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
133 resizecon = workarea().workAreaResize
134 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
135 dispatchcon = workarea().dispatch
136 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
137 kpresscon = workarea().workAreaKeyPress
138 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
139 selectioncon = workarea().selectionRequested
140 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
141 lostcon = workarea().selectionLost
142 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
144 timecon = cursor_timeout.timeout
145 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
146 cursor_timeout.start();
147 saved_positions.resize(saved_positions_num);
151 void BufferView::Pimpl::addError(ErrorItem const & ei)
153 errorlist_.push_back(ei);
157 void BufferView::Pimpl::showReadonly(bool)
159 owner_->updateWindowTitle();
160 owner_->getDialogs().updateBufferDependent(false);
164 void BufferView::Pimpl::connectBuffer(Buffer & buf)
166 if (errorConnection_.connected())
169 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
170 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
171 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
172 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
173 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
174 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
175 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
179 void BufferView::Pimpl::disconnectBuffer()
181 errorConnection_.disconnect();
182 messageConnection_.disconnect();
183 busyConnection_.disconnect();
184 titleConnection_.disconnect();
185 timerConnection_.disconnect();
186 readonlyConnection_.disconnect();
187 closingConnection_.disconnect();
191 bool BufferView::Pimpl::newFile(string const & filename,
192 string const & tname,
195 Buffer * b = ::newFile(filename, tname, isNamed);
201 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
203 // get absolute path of file and add ".lyx" to the filename if
205 string s = FileSearch(string(), filename, "lyx");
207 bool const found = !s.empty();
212 // file already open?
213 if (bufferlist.exists(s)) {
214 string const file = MakeDisplayPath(s, 20);
215 string text = bformat(_("The document %1$s is already "
216 "loaded.\n\nDo you want to revert "
217 "to the saved version?"), file);
218 int const ret = Alert::prompt(_("Revert to saved document?"),
219 text, 0, 1, _("&Revert"), _("&Switch to document"));
222 buffer(bufferlist.getBuffer(s));
225 // FIXME: should be LFUN_REVERT
226 if (!bufferlist.close(bufferlist.getBuffer(s), false))
228 // Fall through to new load. (Asger)
235 b = bufferlist.newBuffer(s);
237 if (!::loadLyXFile(b, s)) {
238 bufferlist.release(b);
242 string text = bformat(_("The document %1$s does not yet "
243 "exist.\n\nDo you want to create "
244 "a new document?"), s);
245 int const ret = Alert::prompt(_("Create new document?"),
246 text, 0, 1, _("&Create"), _("Cancel"));
249 b = ::newFile(s, string(), true);
255 bv_->showErrorList(_("Parse"));
258 lastfiles->newFile(b->fileName());
264 WorkArea & BufferView::Pimpl::workarea() const
266 return *workarea_.get();
270 LyXScreen & BufferView::Pimpl::screen() const
272 return *screen_.get();
276 Painter & BufferView::Pimpl::painter() const
278 return workarea().getPainter();
282 void BufferView::Pimpl::top_y(int y)
288 int BufferView::Pimpl::top_y() const
294 void BufferView::Pimpl::buffer(Buffer * b)
296 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
300 // Put the old text into the TextCache, but
301 // only if the buffer is still loaded.
302 // Also set the owner of the test to 0
303 // bv_->text->owner(0);
304 textcache.add(buffer_, workarea().workWidth(), bv_->text);
305 if (lyxerr.debugging())
306 textcache.show(lyxerr, "BufferView::buffer");
311 // set current buffer
316 // if we're quitting lyx, don't bother updating stuff
320 // if we are closing the buffer, use the first buffer as current
322 buffer_ = bufferlist.first();
325 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
326 connectBuffer(*buffer_);
328 // If we don't have a text object for this, we make one
330 resizeCurrentBuffer();
332 // FIXME: needed when ?
333 top_y(screen().topCursorVisible(bv_->text));
335 // Buffer-dependent dialogs should be updated or
336 // hidden. This should go here because some dialogs (eg ToC)
337 // require bv_->text.
338 owner_->getDialogs().updateBufferDependent(true);
340 lyxerr[Debug::INFO] << " No Buffer!" << endl;
341 owner_->getDialogs().hideBufferDependent();
343 // Also remove all remaining text's from the testcache.
344 // (there should not be any!) (if there is any it is a
346 if (lyxerr.debugging())
347 textcache.show(lyxerr, "buffer delete all");
353 owner_->updateMenubar();
354 owner_->updateToolbar();
355 owner_->updateLayoutChoice();
356 owner_->updateWindowTitle();
359 // Don't forget to update the Layout
360 string const layoutname =
361 bv_->text->cursor.par()->layout()->name();
362 owner_->setLayout(layoutname);
365 if (lyx::graphics::Previews::activated() && buffer_)
366 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
370 bool BufferView::Pimpl::fitCursor()
374 if (bv_->theLockingInset()) {
375 bv_->theLockingInset()->fitInsetCursor(bv_);
378 ret = screen().fitCursor(bv_->text, bv_);
381 //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
383 // We need to always update, in case we did a
384 // paste and we stayed anchored to a row, but
385 // the actual height of the doc changed ...
391 void BufferView::Pimpl::redoCurrentBuffer()
393 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
394 if (buffer_ && bv_->text) {
395 resizeCurrentBuffer();
397 owner_->updateLayoutChoice();
403 void BufferView::Pimpl::resizeCurrentBuffer()
405 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
407 ParagraphList::iterator par;
408 ParagraphList::iterator selstartpar;
409 ParagraphList::iterator selendpar;
410 UpdatableInset * the_locking_inset = 0;
413 pos_type selstartpos = 0;
414 pos_type selendpos = 0;
415 bool selection = false;
416 bool mark_set = false;
420 owner_->message(_("Formatting document..."));
423 par = bv_->text->cursor.par();
424 pos = bv_->text->cursor.pos();
425 selstartpar = bv_->text->selection.start.par();
426 selstartpos = bv_->text->selection.start.pos();
427 selendpar = bv_->text->selection.end.par();
428 selendpos = bv_->text->selection.end.pos();
429 selection = bv_->text->selection.set();
430 mark_set = bv_->text->selection.mark();
431 the_locking_inset = bv_->theLockingInset();
432 bv_->text->fullRebreak();
435 lyxerr << "text not available!" << endl;
436 // See if we have a text in TextCache that fits
437 // the new buffer_ with the correct width.
438 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
440 lyxerr << "text in cache!" << endl;
441 if (lyxerr.debugging()) {
442 lyxerr << "Found a LyXText that fits:" << endl;
443 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
445 // Set the owner of the newly found text
446 // bv_->text->owner(bv_);
447 if (lyxerr.debugging())
448 textcache.show(lyxerr, "resizeCurrentBuffer");
450 lyxerr << "no text in cache!" << endl;
451 bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
452 bv_->text->init(bv_);
455 par = bv_->text->ownerParagraphs().end();
456 selstartpar = bv_->text->ownerParagraphs().end();
457 selendpar = bv_->text->ownerParagraphs().end();
460 #warning does not help much
461 //bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
462 // bv_->text->ownerParagraphs().end());
464 if (par != bv_->text->ownerParagraphs().end()) {
465 bv_->text->selection.set(true);
466 // At this point just to avoid the Delete-Empty-Paragraph-
467 // Mechanism when setting the cursor.
468 bv_->text->selection.mark(mark_set);
470 bv_->text->setCursor(selstartpar, selstartpos);
471 bv_->text->selection.cursor = bv_->text->cursor;
472 bv_->text->setCursor(selendpar, selendpos);
473 bv_->text->setSelection();
474 bv_->text->setCursor(par, pos);
476 bv_->text->setCursor(par, pos);
477 bv_->text->selection.cursor = bv_->text->cursor;
478 bv_->text->selection.set(false);
480 // remake the inset locking
481 bv_->theLockingInset(the_locking_inset);
484 top_y(screen().topCursorVisible(bv_->text));
489 // reset the "Formatting..." message
490 owner_->clearMessage();
496 void BufferView::Pimpl::updateScrollbar()
499 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
500 workarea().setScrollbarParams(0, 0, 0);
504 LyXText const & t = *bv_->text;
506 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
507 << top_y() << ", default height " << defaultRowHeight() << endl;
509 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
513 void BufferView::Pimpl::scrollDocView(int value)
515 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
520 screen().hideCursor();
523 screen().redraw(*bv_);
525 if (!lyxrc.cursor_follows_scrollbar)
528 int const height = defaultRowHeight();
529 int const first = top_y() + height;
530 int const last = top_y() + workarea().workHeight() - height;
532 LyXText * text = bv_->text;
533 if (text->cursor.y() < first)
534 text->setCursorFromCoordinates(0, first);
535 else if (text->cursor.y() > last)
536 text->setCursorFromCoordinates(0, last);
538 owner_->updateLayoutChoice();
542 void BufferView::Pimpl::scroll(int lines)
548 LyXText const * t = bv_->text;
549 int const line_height = defaultRowHeight();
551 // The new absolute coordinate
552 int new_top_y = top_y() + lines * line_height;
554 // Restrict to a valid value
555 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
556 new_top_y = std::max(0, new_top_y);
558 scrollDocView(new_top_y);
560 // Update the scrollbar.
561 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
565 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
566 key_modifier::state state)
568 bv_->owner()->getLyXFunc().processKeySym(key, state);
570 /* This is perhaps a bit of a hack. When we move
571 * around, or type, it's nice to be able to see
572 * the cursor immediately after the keypress. So
573 * we reset the toggle timeout and force the visibility
574 * of the cursor. Note we cannot do this inside
575 * dispatch() itself, because that's called recursively.
578 cursor_timeout.restart();
579 screen().showCursor(*bv_);
584 void BufferView::Pimpl::selectionRequested()
591 LyXText * text = bv_->getLyXText();
593 if (text->selection.set() &&
594 (!bv_->text->xsel_cache.set() ||
595 text->selection.start != bv_->text->xsel_cache.start ||
596 text->selection.end != bv_->text->xsel_cache.end))
598 bv_->text->xsel_cache = text->selection;
599 sel = text->selectionAsString(*bv_->buffer(), false);
600 } else if (!text->selection.set()) {
602 bv_->text->xsel_cache.set(false);
605 workarea().putClipboard(sel);
610 void BufferView::Pimpl::selectionLost()
613 screen().hideCursor();
614 bv_->getLyXText()->clearSelection();
615 bv_->text->xsel_cache.set(false);
620 void BufferView::Pimpl::workAreaResize()
622 static int work_area_width;
623 static int work_area_height;
625 bool const widthChange = workarea().workWidth() != work_area_width;
626 bool const heightChange = workarea().workHeight() != work_area_height;
628 // update from work area
629 work_area_width = workarea().workWidth();
630 work_area_height = workarea().workHeight();
634 // The visible LyXView need a resize
635 resizeCurrentBuffer();
637 // Remove all texts from the textcache
638 // This is not _really_ what we want to do. What
639 // we really want to do is to delete in textcache
640 // that does not have a BufferView with matching
641 // width, but as long as we have only one BufferView
642 // deleting all gives the same result.
643 if (lyxerr.debugging())
644 textcache.show(lyxerr, "Expose delete all");
649 if (widthChange || heightChange)
652 // always make sure that the scrollbar is sane.
654 owner_->updateLayoutChoice();
658 void BufferView::Pimpl::update()
660 //lyxerr << "BufferView::update()" << endl;
661 // fix cursor coordinate cache in case something went wrong
662 if (bv_->getLyXText()) {
663 // check needed to survive LyX startup
664 bv_->getLyXText()->redoCursor();
666 screen().redraw(*bv_);
670 // Callback for cursor timer
671 void BufferView::Pimpl::cursorToggle()
674 cursor_timeout.restart();
678 screen().toggleCursor(*bv_);
680 cursor_timeout.restart();
684 bool BufferView::Pimpl::available() const
686 if (buffer_ && bv_->text)
692 Change const BufferView::Pimpl::getCurrentChange()
694 if (!bv_->buffer()->params().tracking_changes)
695 return Change(Change::UNCHANGED);
697 LyXText * text = bv_->getLyXText();
699 if (!text->selection.set())
700 return Change(Change::UNCHANGED);
702 LyXCursor const & cur = text->selection.start;
703 return cur.par()->lookupChangeFull(cur.pos());
707 void BufferView::Pimpl::beforeChange(LyXText * text)
709 text->clearSelection();
713 void BufferView::Pimpl::savePosition(unsigned int i)
715 if (i >= saved_positions_num)
717 saved_positions[i] = Position(buffer_->fileName(),
718 bv_->text->cursor.par()->id(),
719 bv_->text->cursor.pos());
721 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
725 void BufferView::Pimpl::restorePosition(unsigned int i)
727 if (i >= saved_positions_num)
730 string const fname = saved_positions[i].filename;
732 beforeChange(bv_->text);
734 if (fname != buffer_->fileName()) {
736 if (bufferlist.exists(fname))
737 b = bufferlist.getBuffer(fname);
739 b = bufferlist.newBuffer(fname);
740 ::loadLyXFile(b, fname); // don't ask, just load it
746 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
747 if (par == buffer_->par_iterator_end())
750 bv_->text->setCursor(par.pit(),
751 min(par->size(), saved_positions[i].par_pos));
755 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
759 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
761 if (i >= saved_positions_num)
764 return !saved_positions[i].filename.empty();
768 void BufferView::Pimpl::switchKeyMap()
770 if (!lyxrc.rtl_support)
773 LyXText * text = bv_->getLyXText();
774 if (text->real_current_font.isRightToLeft()
775 && !(bv_->theLockingInset()
776 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
778 if (owner_->getIntl().keymap == Intl::PRIMARY)
779 owner_->getIntl().KeyMapSec();
781 if (owner_->getIntl().keymap == Intl::SECONDARY)
782 owner_->getIntl().KeyMapPrim();
787 void BufferView::Pimpl::insetUnlock()
789 if (bv_->theLockingInset()) {
790 bv_->theLockingInset()->insetUnlock(bv_);
791 bv_->theLockingInset(0);
797 void BufferView::Pimpl::center()
799 LyXText * text = bv_->text;
802 int const half_height = workarea().workHeight() / 2;
803 int new_y = std::max(0, text->cursor.y() - half_height);
805 // FIXME: look at this comment again ...
807 // FIXME: can we do this w/o calling screen directly ?
808 // This updates top_y() but means the fitCursor() call
809 // from the update(FITCUR) doesn't realise that we might
810 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
811 // the scrollbar to be updated as it should, so we have
812 // to do it manually. Any operation that does a center()
813 // and also might have moved top_y() must make sure to call
814 // updateScrollbar() currently. Never mind that this is a
815 // pretty obfuscated way of updating t->top_y()
822 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
824 workarea().putClipboard(stuff);
829 * Dispatch functions for actions which can be valid for BufferView->text
830 * and/or InsetText->text!!!
834 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
837 LyXCursor cursor = bv_->getLyXText()->cursor;
838 Buffer::inset_iterator it =
839 find_if(Buffer::inset_iterator(
840 cursor.par(), cursor.pos()),
841 buffer_->inset_iterator_end(),
842 lyx::compare_memfun(&Inset::lyxCode, code));
843 return it != buffer_->inset_iterator_end() ? (*it) : 0;
845 // Ok, this is a little bit too brute force but it
846 // should work for now. Better infrastructure is coming. (Lgb)
848 Buffer * b = bv_->buffer();
849 LyXCursor cursor = bv_->getLyXText()->cursor;
851 Buffer::inset_iterator beg = b->inset_iterator_begin();
852 Buffer::inset_iterator end = b->inset_iterator_end();
854 bool cursor_par_seen = false;
856 for (; beg != end; ++beg) {
857 if (beg.getPar() == cursor.par()) {
858 cursor_par_seen = true;
860 if (cursor_par_seen) {
861 if (beg.getPar() == cursor.par()
862 && beg.getPos() >= cursor.pos()) {
864 } else if (beg.getPar() != cursor.par()) {
871 // Now find the first inset that matches code.
872 for (; beg != end; ++beg) {
873 if (beg->lyxCode() == code) {
883 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
885 string filename = filen;
887 if (filename.empty()) {
888 // Launch a file browser
889 string initpath = lyxrc.document_path;
892 string const trypath = owner_->buffer()->filePath();
893 // If directory is writeable, use this as default.
894 if (IsDirWriteable(trypath))
898 FileDialog fileDlg(_("Select LyX document to insert"),
900 make_pair(string(_("Documents|#o#O")),
901 string(lyxrc.document_path)),
902 make_pair(string(_("Examples|#E#e")),
903 string(AddPath(system_lyxdir(), "examples"))));
905 FileDialog::Result result =
906 fileDlg.open(initpath,
907 _("*.lyx| LyX Documents (*.lyx)"));
909 if (result.first == FileDialog::Later)
912 filename = result.second;
914 // check selected filename
915 if (filename.empty()) {
916 owner_->message(_("Canceled."));
921 // get absolute path of file and add ".lyx" to the filename if
923 filename = FileSearch(string(), filename, "lyx");
925 string const disp_fn = MakeDisplayPath(filename);
926 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
927 if (bv_->insertLyXFile(filename))
928 owner_->message(bformat(_("Document %1$s inserted."),
931 owner_->message(bformat(_("Could not insert document %1$s"),
936 void BufferView::Pimpl::trackChanges()
938 Buffer * buf(bv_->buffer());
939 bool const tracking(buf->params().tracking_changes);
942 ParIterator const end = buf->par_iterator_end();
943 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
945 buf->params().tracking_changes = true;
947 // we cannot allow undos beyond the freeze point
948 buf->undostack().clear();
951 bv_->text->setCursor(buf->paragraphs().begin(), 0);
952 #warning changes FIXME
953 //moveCursorUpdate(false);
955 bool found = lyx::find::findNextChange(bv_);
957 owner_->getDialogs().show("changes");
961 ParIterator const end = buf->par_iterator_end();
962 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
963 it->untrackChanges();
964 buf->params().tracking_changes = false;
967 buf->redostack().clear();
971 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
974 case LFUN_MOUSE_PRESS:
975 case LFUN_MOUSE_MOTION:
976 case LFUN_MOUSE_RELEASE:
977 case LFUN_MOUSE_DOUBLE:
978 case LFUN_MOUSE_TRIPLE:
980 // We pass those directly to the Bufferview, since
981 // otherwise selection handling breaks down
983 // Doesn't go through lyxfunc, so we need to update
984 // the layout choice etc. ourselves
986 // e.g. Qt mouse press when no buffer
990 screen().hideCursor();
992 bool const res = dispatch(ev);
994 // see workAreaKeyPress
995 cursor_timeout.restart();
996 screen().showCursor(*bv_);
998 // FIXME: we should skip these when selecting
999 owner_->updateLayoutChoice();
1000 owner_->updateToolbar();
1003 // slight hack: this is only called currently when we
1004 // clicked somewhere, so we force through the display
1005 // of the new status here.
1006 owner_->clearMessage();
1011 owner_->dispatch(ev);
1017 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1019 // Make sure that the cached BufferView is correct.
1020 FuncRequest ev = ev_in;
1023 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1024 << " action[" << ev.action << ']'
1025 << " arg[" << ev.argument << ']'
1026 << " x[" << ev.x << ']'
1027 << " y[" << ev.y << ']'
1028 << " button[" << ev.button() << ']'
1031 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1033 switch (ev.action) {
1035 case LFUN_SCROLL_INSET:
1036 // this is not handled here as this function is only active
1037 // if we have a locking_inset and that one is (or contains)
1041 case LFUN_FILE_INSERT:
1042 MenuInsertLyXFile(ev.argument);
1045 case LFUN_FILE_INSERT_ASCII_PARA:
1046 InsertAsciiFile(bv_, ev.argument, true);
1049 case LFUN_FILE_INSERT_ASCII:
1050 InsertAsciiFile(bv_, ev.argument, false);
1054 lang(bv_, ev.argument);
1056 owner_->view_state_changed();
1061 owner_->view_state_changed();
1066 owner_->view_state_changed();
1071 owner_->view_state_changed();
1076 owner_->view_state_changed();
1081 owner_->view_state_changed();
1086 owner_->view_state_changed();
1091 owner_->view_state_changed();
1094 case LFUN_UNDERLINE:
1096 owner_->view_state_changed();
1099 case LFUN_FONT_SIZE:
1100 fontSize(bv_, ev.argument);
1101 owner_->view_state_changed();
1104 case LFUN_FONT_STATE:
1105 owner_->getLyXFunc().setMessage(currentState(bv_));
1108 case LFUN_INSERT_LABEL: {
1109 // Try and generate a valid label
1110 string const contents = ev.argument.empty() ?
1111 getPossibleLabel(*bv_) : ev.argument;
1112 InsetCommandParams icp("label", contents);
1113 string data = InsetCommandMailer::params2string("label", icp);
1114 owner_->getDialogs().show("label", data, 0);
1118 case LFUN_BOOKMARK_SAVE:
1119 savePosition(strToUnsignedInt(ev.argument));
1122 case LFUN_BOOKMARK_GOTO:
1123 restorePosition(strToUnsignedInt(ev.argument));
1126 case LFUN_REF_GOTO: {
1127 string label = ev.argument;
1128 if (label.empty()) {
1130 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1132 label = inset->getContents();
1138 bv_->gotoLabel(label);
1142 // --- accented characters ---------------------------
1145 case LFUN_CIRCUMFLEX:
1155 case LFUN_SPECIAL_CARON:
1158 case LFUN_HUNG_UMLAUT:
1161 if (ev.argument.empty()) {
1163 owner_->getLyXFunc().handleKeyFunc(ev.action);
1165 owner_->getLyXFunc().handleKeyFunc(ev.action);
1166 owner_->getIntl().getTransManager()
1167 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1172 case LFUN_MATH_MACRO:
1173 case LFUN_MATH_DELIM:
1174 case LFUN_INSERT_MATRIX:
1175 case LFUN_INSERT_MATH:
1176 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1177 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1178 case LFUN_MATH_MODE: // Open or create an inlined math inset
1182 case LFUN_INSET_APPLY: {
1183 string const name = ev.getArg(0);
1185 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1187 // This works both for 'original' and 'mathed' insets.
1188 // Note that the localDispatch performs updateInset
1190 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1191 inset->localDispatch(fr);
1193 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1199 case LFUN_INSET_INSERT: {
1200 InsetOld * inset = createInset(ev);
1201 if (inset && insertInset(inset)) {
1204 string const name = ev.getArg(0);
1205 if (name == "bibitem") {
1206 // We need to do a redraw because the maximum
1207 // InsetBibitem width could have changed
1208 #warning check whether the update() is needed at all
1217 case LFUN_FLOAT_LIST:
1218 if (tclass.floats().typeExist(ev.argument)) {
1219 InsetOld * inset = new InsetFloatList(ev.argument);
1220 if (!insertInset(inset, tclass.defaultLayoutName()))
1223 lyxerr << "Non-existent float type: "
1224 << ev.argument << endl;
1228 case LFUN_LAYOUT_PARAGRAPH: {
1230 params2string(*bv_->getLyXText()->cursor.par(), data);
1232 data = "show\n" + data;
1233 bv_->owner()->getDialogs().show("paragraph", data);
1237 case LFUN_PARAGRAPH_UPDATE: {
1238 if (!bv_->owner()->getDialogs().visible("paragraph"))
1240 Paragraph const & par = *bv_->getLyXText()->cursor.par();
1243 params2string(par, data);
1245 // Will the paragraph accept changes from the dialog?
1246 InsetOld * const inset = par.inInset();
1248 !(inset && inset->forceDefaultParagraphs(inset));
1250 data = "update " + tostr(accept) + '\n' + data;
1251 bv_->owner()->getDialogs().update("paragraph", data);
1255 case LFUN_PARAGRAPH_APPLY:
1256 setParagraphParams(*bv_, ev.argument);
1259 case LFUN_THESAURUS_ENTRY:
1261 string arg = ev.argument;
1264 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1268 if (arg.size() > 100 || arg.empty()) {
1269 // Get word or selection
1270 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1271 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1272 // FIXME: where is getLyXText()->unselect(bv_) ?
1276 bv_->owner()->getDialogs().show("thesaurus", arg);
1280 case LFUN_TRACK_CHANGES:
1284 case LFUN_MERGE_CHANGES:
1285 owner_->getDialogs().show("changes");
1288 case LFUN_ACCEPT_ALL_CHANGES: {
1289 bv_->text->setCursor(bv_->buffer()->paragraphs().begin(), 0);
1290 #warning FIXME changes
1291 //moveCursorUpdate(false);
1293 while (lyx::find::findNextChange(bv_))
1294 bv_->getLyXText()->acceptChange();
1300 case LFUN_REJECT_ALL_CHANGES: {
1301 bv_->text->setCursor(bv_->buffer()->paragraphs().begin(), 0);
1302 #warning FIXME changes
1303 //moveCursorUpdate(false);
1305 while (lyx::find::findNextChange(bv_))
1306 bv_->getLyXText()->rejectChange();
1312 case LFUN_ACCEPT_CHANGE: {
1313 bv_->getLyXText()->acceptChange();
1318 case LFUN_REJECT_CHANGE: {
1319 bv_->getLyXText()->rejectChange();
1324 case LFUN_UNKNOWN_ACTION:
1325 ev.errorMessage(N_("Unknown function!"));
1329 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1336 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1338 // if we are in a locking inset we should try to insert the
1339 // inset there otherwise this is a illegal function now
1340 if (bv_->theLockingInset()) {
1341 if (bv_->theLockingInset()->insetAllowed(inset))
1342 return bv_->theLockingInset()->insertInset(bv_, inset);
1346 // not quite sure if we want this...
1347 recordUndo(bv_, Undo::ATOMIC);
1350 beforeChange(bv_->text);
1351 if (!lout.empty()) {
1352 bv_->text->breakParagraph(bv_->buffer()->paragraphs());
1354 if (!bv_->text->cursor.par()->empty()) {
1355 bv_->text->cursorLeft(bv_);
1356 bv_->text->breakParagraph(bv_->buffer()->paragraphs());
1360 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1361 bool hasLayout = tclass.hasLayout(lres);
1362 string lay = tclass.defaultLayoutName();
1364 if (hasLayout != false) {
1368 // layout not fount using default
1369 lay = tclass.defaultLayoutName();
1372 bv_->text->setLayout(lay);
1374 bv_->text->setParagraph(0, 0,
1376 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1383 bv_->text->insertInset(inset);
1391 void BufferView::Pimpl::updateInset(InsetOld const * inset)
1396 bv_->text->redoParagraph(outerPar(*bv_->buffer(), inset));
1398 // this should not be needed, but it is...
1399 // bv_->text->redoParagraph(bv_->text->cursor.par());
1400 // bv_->text->fullRebreak();
1407 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1408 string const & from, string const & to)
1410 bool need_update = false;
1411 LyXCursor cursor = bv_->text->cursor;
1412 LyXCursor tmpcursor = cursor;
1413 cursor.par(tmpcursor.par());
1414 cursor.pos(tmpcursor.pos());
1416 ParIterator end = bv_->buffer()->par_iterator_end();
1417 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1419 bool changed_inset = false;
1420 for (InsetList::iterator it2 = it->insetlist.begin();
1421 it2 != it->insetlist.end(); ++it2) {
1422 if (it2->inset->lyxCode() == code) {
1423 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1424 if (inset->getContents() == from) {
1425 inset->setContents(to);
1426 changed_inset = true;
1430 if (changed_inset) {
1435 // The test it.size()==1 was needed to prevent crashes.
1436 // How to set the cursor corretly when it.size()>1 ??
1437 if (it.size() == 1) {
1438 bv_->text->setCursorIntern(it.pit(), 0);
1439 bv_->text->redoParagraph(bv_->text->cursor.par());
1443 bv_->text->setCursorIntern(cursor.par(), cursor.pos());