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 "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
45 #include "TextCache.h"
46 #include "undo_funcs.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetgraphics.h"
50 #include "insets/insetinclude.h"
51 #include "insets/insetref.h"
52 #include "insets/insettext.h"
54 #include "frontends/Alert.h"
55 #include "frontends/Dialogs.h"
56 #include "frontends/FileDialog.h"
57 #include "frontends/LyXView.h"
58 #include "frontends/LyXScreenFactory.h"
59 #include "frontends/mouse_state.h"
60 #include "frontends/screen.h"
61 #include "frontends/WorkArea.h"
62 #include "frontends/WorkAreaFactory.h"
64 #include "mathed/formulabase.h"
66 #include "graphics/Previews.h"
68 #include "support/LAssert.h"
69 #include "support/tostr.h"
70 #include "support/filetools.h"
71 #include "support/path_defines.h"
73 #include <boost/bind.hpp>
74 #include <boost/signals/connection.hpp>
89 using namespace lyx::support;
90 using namespace bv_funcs;
92 extern BufferList bufferlist;
97 unsigned int const saved_positions_num = 20;
99 // All the below connection objects are needed because of a bug in some
100 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
101 // to these connections we avoid a segfault upon startup, and also at exit.
104 boost::signals::connection dispatchcon;
105 boost::signals::connection timecon;
106 boost::signals::connection doccon;
107 boost::signals::connection resizecon;
108 boost::signals::connection kpresscon;
109 boost::signals::connection selectioncon;
110 boost::signals::connection lostcon;
116 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
117 int xpos, int ypos, int width, int height)
118 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
119 using_xterm_cursor(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 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 // Put the old text into the TextCache, but
295 // only if the buffer is still loaded.
296 // Also set the owner of the test to 0
297 // bv_->text->owner(0);
298 textcache.add(buffer_, workarea().workWidth(), bv_->text);
299 if (lyxerr.debugging())
300 textcache.show(lyxerr, "BufferView::buffer");
305 // set current buffer
310 // if we're quitting lyx, don't bother updating stuff
314 // if we are closing the buffer, use the first buffer as current
316 buffer_ = bufferlist.first();
319 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
320 connectBuffer(*buffer_);
322 // If we don't have a text object for this, we make one
324 resizeCurrentBuffer();
326 // FIXME: needed when ?
327 top_y(screen().topCursorVisible(bv_->text));
329 // Buffer-dependent dialogs should be updated or
330 // hidden. This should go here because some dialogs (eg ToC)
331 // require bv_->text.
332 owner_->getDialogs().updateBufferDependent(true);
334 lyxerr[Debug::INFO] << " No Buffer!" << endl;
335 owner_->getDialogs().hideBufferDependent();
337 // Also remove all remaining text's from the testcache.
338 // (there should not be any!) (if there is any it is a
340 if (lyxerr.debugging())
341 textcache.show(lyxerr, "buffer delete all");
347 owner_->updateMenubar();
348 owner_->updateToolbar();
349 owner_->updateLayoutChoice();
350 owner_->updateWindowTitle();
353 // Don't forget to update the Layout
354 string const layoutname =
355 bv_->text->cursor.par()->layout()->name();
356 owner_->setLayout(layoutname);
359 if (lyx::graphics::Previews::activated() && buffer_)
360 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
364 bool BufferView::Pimpl::fitCursor()
368 if (bv_->theLockingInset()) {
369 bv_->theLockingInset()->fitInsetCursor(bv_);
372 ret = screen().fitCursor(bv_->text, bv_);
375 //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
377 // We need to always update, in case we did a
378 // paste and we stayed anchored to a row, but
379 // the actual height of the doc changed ...
385 void BufferView::Pimpl::redoCurrentBuffer()
387 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
388 if (buffer_ && bv_->text) {
389 resizeCurrentBuffer();
391 owner_->updateLayoutChoice();
397 void BufferView::Pimpl::resizeCurrentBuffer()
399 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
401 ParagraphList::iterator par;
402 ParagraphList::iterator selstartpar;
403 ParagraphList::iterator selendpar;
404 UpdatableInset * the_locking_inset = 0;
407 pos_type selstartpos = 0;
408 pos_type selendpos = 0;
409 bool selection = false;
410 bool mark_set = false;
414 owner_->message(_("Formatting document..."));
417 par = bv_->text->cursor.par();
418 pos = bv_->text->cursor.pos();
419 selstartpar = bv_->text->selection.start.par();
420 selstartpos = bv_->text->selection.start.pos();
421 selendpar = bv_->text->selection.end.par();
422 selendpos = bv_->text->selection.end.pos();
423 selection = bv_->text->selection.set();
424 mark_set = bv_->text->selection.mark();
425 the_locking_inset = bv_->theLockingInset();
426 bv_->text->fullRebreak();
429 lyxerr << "text not available!" << endl;
430 // See if we have a text in TextCache that fits
431 // the new buffer_ with the correct width.
432 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
434 lyxerr << "text in cache!" << endl;
435 if (lyxerr.debugging()) {
436 lyxerr << "Found a LyXText that fits:" << endl;
437 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
439 // Set the owner of the newly found text
440 // bv_->text->owner(bv_);
441 if (lyxerr.debugging())
442 textcache.show(lyxerr, "resizeCurrentBuffer");
444 lyxerr << "no text in cache!" << endl;
445 bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs);
446 bv_->text->init(bv_);
449 par = bv_->text->ownerParagraphs().end();
450 selstartpar = bv_->text->ownerParagraphs().end();
451 selendpar = bv_->text->ownerParagraphs().end();
454 #warning does not help much
455 bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
456 bv_->text->ownerParagraphs().end());
458 if (par != bv_->text->ownerParagraphs().end()) {
459 bv_->text->selection.set(true);
460 // At this point just to avoid the Delete-Empty-Paragraph-
461 // Mechanism when setting the cursor.
462 bv_->text->selection.mark(mark_set);
464 bv_->text->setCursor(selstartpar, selstartpos);
465 bv_->text->selection.cursor = bv_->text->cursor;
466 bv_->text->setCursor(selendpar, selendpos);
467 bv_->text->setSelection();
468 bv_->text->setCursor(par, pos);
470 bv_->text->setCursor(par, pos);
471 bv_->text->selection.cursor = bv_->text->cursor;
472 bv_->text->selection.set(false);
474 // remake the inset locking
475 bv_->theLockingInset(the_locking_inset);
478 top_y(screen().topCursorVisible(bv_->text));
483 // reset the "Formatting..." message
484 owner_->clearMessage();
490 void BufferView::Pimpl::updateScrollbar()
493 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
494 workarea().setScrollbarParams(0, 0, 0);
498 LyXText const & t = *bv_->text;
500 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
501 << top_y() << ", default height " << defaultRowHeight() << endl;
503 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
507 void BufferView::Pimpl::scrollDocView(int value)
509 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
514 screen().hideCursor();
517 screen().redraw(*bv_);
519 if (!lyxrc.cursor_follows_scrollbar)
522 int const height = defaultRowHeight();
523 int const first = top_y() + height;
524 int const last = top_y() + workarea().workHeight() - height;
526 LyXText * text = bv_->text;
527 if (text->cursor.y() < first)
528 text->setCursorFromCoordinates(0, first);
529 else if (text->cursor.y() > last)
530 text->setCursorFromCoordinates(0, last);
532 owner_->updateLayoutChoice();
536 void BufferView::Pimpl::scroll(int lines)
542 LyXText const * t = bv_->text;
543 int const line_height = defaultRowHeight();
545 // The new absolute coordinate
546 int new_top_y = top_y() + lines * line_height;
548 // Restrict to a valid value
549 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
550 new_top_y = std::max(0, new_top_y);
552 scrollDocView(new_top_y);
554 // Update the scrollbar.
555 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
559 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
560 key_modifier::state state)
562 bv_->owner()->getLyXFunc().processKeySym(key, state);
564 /* This is perhaps a bit of a hack. When we move
565 * around, or type, it's nice to be able to see
566 * the cursor immediately after the keypress. So
567 * we reset the toggle timeout and force the visibility
568 * of the cursor. Note we cannot do this inside
569 * dispatch() itself, because that's called recursively.
572 cursor_timeout.restart();
573 screen().showCursor(*bv_);
578 void BufferView::Pimpl::selectionRequested()
585 LyXText * text = bv_->getLyXText();
587 if (text->selection.set() &&
588 (!bv_->text->xsel_cache.set() ||
589 text->selection.start != bv_->text->xsel_cache.start ||
590 text->selection.end != bv_->text->xsel_cache.end))
592 bv_->text->xsel_cache = text->selection;
593 sel = text->selectionAsString(*bv_->buffer(), false);
594 } else if (!text->selection.set()) {
596 bv_->text->xsel_cache.set(false);
599 workarea().putClipboard(sel);
604 void BufferView::Pimpl::selectionLost()
607 screen().hideCursor();
608 bv_->getLyXText()->clearSelection();
609 bv_->text->xsel_cache.set(false);
614 void BufferView::Pimpl::workAreaResize()
616 static int work_area_width;
617 static int work_area_height;
619 bool const widthChange = workarea().workWidth() != work_area_width;
620 bool const heightChange = workarea().workHeight() != work_area_height;
622 // update from work area
623 work_area_width = workarea().workWidth();
624 work_area_height = workarea().workHeight();
628 // The visible LyXView need a resize
629 resizeCurrentBuffer();
631 // Remove all texts from the textcache
632 // This is not _really_ what we want to do. What
633 // we really want to do is to delete in textcache
634 // that does not have a BufferView with matching
635 // width, but as long as we have only one BufferView
636 // deleting all gives the same result.
637 if (lyxerr.debugging())
638 textcache.show(lyxerr, "Expose delete all");
643 if (widthChange || heightChange)
646 // always make sure that the scrollbar is sane.
648 owner_->updateLayoutChoice();
652 void BufferView::Pimpl::update()
654 lyxerr << "BufferView::update()" << endl;
655 // fix cursor coordinate cache in case something went wrong
656 if (bv_->getLyXText()) {
657 // check needed to survive LyX startup
658 bv_->getLyXText()->redoCursor();
661 screen().redraw(*bv_);
665 // Callback for cursor timer
666 void BufferView::Pimpl::cursorToggle()
669 cursor_timeout.restart();
673 screen().toggleCursor(*bv_);
675 cursor_timeout.restart();
679 bool BufferView::Pimpl::available() const
681 if (buffer_ && bv_->text)
687 Change const BufferView::Pimpl::getCurrentChange()
689 if (!bv_->buffer()->params.tracking_changes)
690 return Change(Change::UNCHANGED);
692 LyXText * text = bv_->getLyXText();
694 if (!text->selection.set())
695 return Change(Change::UNCHANGED);
697 LyXCursor const & cur = text->selection.start;
698 return cur.par()->lookupChangeFull(cur.pos());
702 void BufferView::Pimpl::beforeChange(LyXText * text)
704 text->clearSelection();
708 void BufferView::Pimpl::savePosition(unsigned int i)
710 if (i >= saved_positions_num)
712 saved_positions[i] = Position(buffer_->fileName(),
713 bv_->text->cursor.par()->id(),
714 bv_->text->cursor.pos());
716 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
720 void BufferView::Pimpl::restorePosition(unsigned int i)
722 if (i >= saved_positions_num)
725 string const fname = saved_positions[i].filename;
727 beforeChange(bv_->text);
729 if (fname != buffer_->fileName()) {
731 if (bufferlist.exists(fname))
732 b = bufferlist.getBuffer(fname);
734 b = bufferlist.newBuffer(fname);
735 ::loadLyXFile(b, fname); // don't ask, just load it
741 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
742 if (par == buffer_->par_iterator_end())
745 bv_->text->setCursor(par.pit(),
746 min(par->size(), saved_positions[i].par_pos));
750 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
754 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
756 if (i >= saved_positions_num)
759 return !saved_positions[i].filename.empty();
763 void BufferView::Pimpl::switchKeyMap()
765 if (!lyxrc.rtl_support)
768 LyXText * text = bv_->getLyXText();
769 if (text->real_current_font.isRightToLeft()
770 && !(bv_->theLockingInset()
771 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
773 if (owner_->getIntl().keymap == Intl::PRIMARY)
774 owner_->getIntl().KeyMapSec();
776 if (owner_->getIntl().keymap == Intl::SECONDARY)
777 owner_->getIntl().KeyMapPrim();
782 void BufferView::Pimpl::insetUnlock()
784 if (bv_->theLockingInset()) {
785 bv_->theLockingInset()->insetUnlock(bv_);
786 bv_->theLockingInset(0);
792 void BufferView::Pimpl::center()
794 LyXText * text = bv_->text;
797 int const half_height = workarea().workHeight() / 2;
800 if (text->cursor.y() > half_height)
801 new_y = text->cursor.y() - half_height;
803 // FIXME: look at this comment again ...
805 // FIXME: can we do this w/o calling screen directly ?
806 // This updates top_y() but means the fitCursor() call
807 // from the update(FITCUR) doesn't realise that we might
808 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
809 // the scrollbar to be updated as it should, so we have
810 // to do it manually. Any operation that does a center()
811 // and also might have moved top_y() must make sure to call
812 // updateScrollbar() currently. Never mind that this is a
813 // pretty obfuscated way of updating t->top_y()
820 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
822 workarea().putClipboard(stuff);
827 * Dispatch functions for actions which can be valid for BufferView->text
828 * and/or InsetText->text!!!
832 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
835 LyXCursor cursor = bv_->getLyXText()->cursor;
836 Buffer::inset_iterator it =
837 find_if(Buffer::inset_iterator(
838 cursor.par(), cursor.pos()),
839 buffer_->inset_iterator_end(),
840 lyx::compare_memfun(&Inset::lyxCode, code));
841 return it != buffer_->inset_iterator_end() ? (*it) : 0;
843 // Ok, this is a little bit too brute force but it
844 // should work for now. Better infrastructure is coming. (Lgb)
846 Buffer * b = bv_->buffer();
847 LyXCursor cursor = bv_->getLyXText()->cursor;
849 Buffer::inset_iterator beg = b->inset_iterator_begin();
850 Buffer::inset_iterator end = b->inset_iterator_end();
852 bool cursor_par_seen = false;
854 for (; beg != end; ++beg) {
855 if (beg.getPar() == cursor.par()) {
856 cursor_par_seen = true;
858 if (cursor_par_seen) {
859 if (beg.getPar() == cursor.par()
860 && beg.getPos() >= cursor.pos()) {
862 } else if (beg.getPar() != cursor.par()) {
869 // Now find the first inset that matches code.
870 for (; beg != end; ++beg) {
871 if (beg->lyxCode() == code) {
881 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
883 string filename = filen;
885 if (filename.empty()) {
886 // Launch a file browser
887 string initpath = lyxrc.document_path;
890 string const trypath = owner_->buffer()->filePath();
891 // If directory is writeable, use this as default.
892 if (IsDirWriteable(trypath))
896 FileDialog fileDlg(_("Select LyX document to insert"),
898 make_pair(string(_("Documents|#o#O")),
899 string(lyxrc.document_path)),
900 make_pair(string(_("Examples|#E#e")),
901 string(AddPath(system_lyxdir(), "examples"))));
903 FileDialog::Result result =
904 fileDlg.open(initpath,
905 _("*.lyx| LyX Documents (*.lyx)"));
907 if (result.first == FileDialog::Later)
910 filename = result.second;
912 // check selected filename
913 if (filename.empty()) {
914 owner_->message(_("Canceled."));
919 // get absolute path of file and add ".lyx" to the filename if
921 filename = FileSearch(string(), filename, "lyx");
923 string const disp_fn = MakeDisplayPath(filename);
924 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
925 if (bv_->insertLyXFile(filename))
926 owner_->message(bformat(_("Document %1$s inserted."),
929 owner_->message(bformat(_("Could not insert document %1$s"),
934 void BufferView::Pimpl::trackChanges()
936 Buffer * buf(bv_->buffer());
937 bool const tracking(buf->params.tracking_changes);
940 ParIterator const end = buf->par_iterator_end();
941 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
943 buf->params.tracking_changes = true;
945 // we cannot allow undos beyond the freeze point
946 buf->undostack.clear();
949 bv_->text->setCursor(buf->paragraphs.begin(), 0);
950 #warning changes FIXME
951 //moveCursorUpdate(false);
953 bool found = lyx::find::findNextChange(bv_);
955 owner_->getDialogs().show("changes");
959 ParIterator const end = buf->par_iterator_end();
960 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
961 it->untrackChanges();
962 buf->params.tracking_changes = false;
965 buf->redostack.clear();
969 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
972 case LFUN_MOUSE_PRESS:
973 case LFUN_MOUSE_MOTION:
974 case LFUN_MOUSE_RELEASE:
975 case LFUN_MOUSE_DOUBLE:
976 case LFUN_MOUSE_TRIPLE:
978 // We pass those directly to the Bufferview, since
979 // otherwise selection handling breaks down
981 // Doesn't go through lyxfunc, so we need to update
982 // the layout choice etc. ourselves
984 // e.g. Qt mouse press when no buffer
988 screen().hideCursor();
990 bool const res = dispatch(ev);
992 // see workAreaKeyPress
993 cursor_timeout.restart();
994 screen().showCursor(*bv_);
996 // FIXME: we should skip these when selecting
997 owner_->updateLayoutChoice();
998 owner_->updateToolbar();
1001 // slight hack: this is only called currently when we
1002 // clicked somewhere, so we force through the display
1003 // of the new status here.
1004 owner_->clearMessage();
1009 owner_->dispatch(ev);
1015 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1017 // Make sure that the cached BufferView is correct.
1018 FuncRequest ev = ev_in;
1021 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1022 << " action[" << ev.action << ']'
1023 << " arg[" << ev.argument << ']'
1024 << " x[" << ev.x << ']'
1025 << " y[" << ev.y << ']'
1026 << " button[" << ev.button() << ']'
1029 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1031 switch (ev.action) {
1033 case LFUN_SCROLL_INSET:
1034 // this is not handled here as this function is only active
1035 // if we have a locking_inset and that one is (or contains)
1039 case LFUN_FILE_INSERT:
1040 MenuInsertLyXFile(ev.argument);
1043 case LFUN_FILE_INSERT_ASCII_PARA:
1044 InsertAsciiFile(bv_, ev.argument, true);
1047 case LFUN_FILE_INSERT_ASCII:
1048 InsertAsciiFile(bv_, ev.argument, false);
1052 lang(bv_, ev.argument);
1054 owner_->view_state_changed();
1059 owner_->view_state_changed();
1064 owner_->view_state_changed();
1069 owner_->view_state_changed();
1074 owner_->view_state_changed();
1079 owner_->view_state_changed();
1084 owner_->view_state_changed();
1089 owner_->view_state_changed();
1092 case LFUN_UNDERLINE:
1094 owner_->view_state_changed();
1097 case LFUN_FONT_SIZE:
1098 fontSize(bv_, ev.argument);
1099 owner_->view_state_changed();
1102 case LFUN_FONT_STATE:
1103 owner_->getLyXFunc().setMessage(currentState(bv_));
1106 case LFUN_INSERT_LABEL: {
1107 // Try and generate a valid label
1108 string const contents = ev.argument.empty() ?
1109 getPossibleLabel(*bv_) : ev.argument;
1110 InsetCommandParams icp("label", contents);
1111 string data = InsetCommandMailer::params2string("label", icp);
1112 owner_->getDialogs().show("label", data, 0);
1116 case LFUN_BOOKMARK_SAVE:
1117 savePosition(strToUnsignedInt(ev.argument));
1120 case LFUN_BOOKMARK_GOTO:
1121 restorePosition(strToUnsignedInt(ev.argument));
1124 case LFUN_REF_GOTO: {
1125 string label = ev.argument;
1126 if (label.empty()) {
1128 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1130 label = inset->getContents();
1136 bv_->gotoLabel(label);
1140 // --- accented characters ---------------------------
1143 case LFUN_CIRCUMFLEX:
1153 case LFUN_SPECIAL_CARON:
1156 case LFUN_HUNG_UMLAUT:
1159 if (ev.argument.empty()) {
1161 owner_->getLyXFunc().handleKeyFunc(ev.action);
1163 owner_->getLyXFunc().handleKeyFunc(ev.action);
1164 owner_->getIntl().getTransManager()
1165 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1170 case LFUN_MATH_MACRO:
1171 case LFUN_MATH_DELIM:
1172 case LFUN_INSERT_MATRIX:
1173 case LFUN_INSERT_MATH:
1174 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1175 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1176 case LFUN_MATH_MODE: // Open or create an inlined math inset
1180 case LFUN_INSET_APPLY: {
1181 string const name = ev.getArg(0);
1183 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1185 // This works both for 'original' and 'mathed' insets.
1186 // Note that the localDispatch performs updateInset
1188 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1189 inset->localDispatch(fr);
1191 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1197 case LFUN_INSET_INSERT: {
1198 InsetOld * inset = createInset(ev);
1199 if (inset && insertInset(inset)) {
1202 string const name = ev.getArg(0);
1203 if (name == "bibitem") {
1204 // We need to do a redraw because the maximum
1205 // InsetBibitem width could have changed
1206 #warning check whether the update() is needed at all
1215 case LFUN_FLOAT_LIST:
1216 if (tclass.floats().typeExist(ev.argument)) {
1217 InsetOld * inset = new InsetFloatList(ev.argument);
1218 if (!insertInset(inset, tclass.defaultLayoutName()))
1221 lyxerr << "Non-existent float type: "
1222 << ev.argument << endl;
1226 case LFUN_LAYOUT_PARAGRAPH: {
1228 params2string(*bv_->getLyXText()->cursor.par(), data);
1230 data = "show\n" + data;
1231 bv_->owner()->getDialogs().show("paragraph", data);
1235 case LFUN_PARAGRAPH_UPDATE: {
1236 if (!bv_->owner()->getDialogs().visible("paragraph"))
1238 Paragraph const & par = *bv_->getLyXText()->cursor.par();
1241 params2string(par, data);
1243 // Will the paragraph accept changes from the dialog?
1244 InsetOld * const inset = par.inInset();
1246 !(inset && inset->forceDefaultParagraphs(inset));
1248 data = "update " + tostr(accept) + '\n' + data;
1249 bv_->owner()->getDialogs().update("paragraph", data);
1253 case LFUN_PARAGRAPH_APPLY:
1254 setParagraphParams(*bv_, ev.argument);
1257 case LFUN_THESAURUS_ENTRY:
1259 string arg = ev.argument;
1262 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1266 if (arg.size() > 100 || arg.empty()) {
1267 // Get word or selection
1268 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1269 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1270 // FIXME: where is getLyXText()->unselect(bv_) ?
1274 bv_->owner()->getDialogs().show("thesaurus", arg);
1278 case LFUN_TRACK_CHANGES:
1282 case LFUN_MERGE_CHANGES:
1283 owner_->getDialogs().show("changes");
1286 case LFUN_ACCEPT_ALL_CHANGES: {
1287 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1288 #warning FIXME changes
1289 //moveCursorUpdate(false);
1291 while (lyx::find::findNextChange(bv_))
1292 bv_->getLyXText()->acceptChange();
1298 case LFUN_REJECT_ALL_CHANGES: {
1299 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1300 #warning FIXME changes
1301 //moveCursorUpdate(false);
1303 while (lyx::find::findNextChange(bv_))
1304 bv_->getLyXText()->rejectChange();
1310 case LFUN_ACCEPT_CHANGE: {
1311 bv_->getLyXText()->acceptChange();
1316 case LFUN_REJECT_CHANGE: {
1317 bv_->getLyXText()->rejectChange();
1322 case LFUN_UNKNOWN_ACTION:
1323 ev.errorMessage(N_("Unknown function!"));
1327 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1334 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1336 // if we are in a locking inset we should try to insert the
1337 // inset there otherwise this is a illegal function now
1338 if (bv_->theLockingInset()) {
1339 if (bv_->theLockingInset()->insetAllowed(inset))
1340 return bv_->theLockingInset()->insertInset(bv_, inset);
1344 // not quite sure if we want this...
1345 recordUndo(bv_, Undo::ATOMIC);
1348 beforeChange(bv_->text);
1349 if (!lout.empty()) {
1350 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1352 if (!bv_->text->cursor.par()->empty()) {
1353 bv_->text->cursorLeft(bv_);
1354 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1358 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1359 bool hasLayout = tclass.hasLayout(lres);
1360 string lay = tclass.defaultLayoutName();
1362 if (hasLayout != false) {
1366 // layout not fount using default
1367 lay = tclass.defaultLayoutName();
1370 bv_->text->setLayout(lay);
1372 bv_->text->setParagraph(0, 0,
1374 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1381 bv_->text->insertInset(inset);
1389 void BufferView::Pimpl::updateInset(InsetOld const * inset)
1394 bv_->text->redoParagraph(outerPar(*bv_->buffer(), inset));
1396 // this should not be needed, but it is...
1397 // bv_->text->redoParagraph(bv_->text->cursor.par());
1398 // bv_->text->fullRebreak();