2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
12 #include "BufferView_pimpl.h"
13 #include "bufferlist.h"
15 #include "buffer_funcs.h"
16 #include "bufferview_funcs.h"
20 #include "FloatList.h"
21 #include "funcrequest.h"
24 #include "iterators.h"
26 #include "lyx_cb.h" // added for Dispatch functions
33 #include "lastfiles.h"
34 #include "paragraph.h"
35 #include "ParagraphParameters.h"
36 #include "TextCache.h"
37 #include "undo_funcs.h"
39 #include "insets/insetfloatlist.h"
40 #include "insets/insetgraphics.h"
41 #include "insets/insetinclude.h"
42 #include "insets/insetref.h"
43 #include "insets/insettext.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/FileDialog.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/LyXScreenFactory.h"
50 #include "frontends/mouse_state.h"
51 #include "frontends/screen.h"
52 #include "frontends/WorkArea.h"
53 #include "frontends/WorkAreaFactory.h"
55 #include "mathed/formulabase.h"
57 #include "graphics/Previews.h"
59 #include "support/LAssert.h"
60 #include "support/tostr.h"
61 #include "support/filetools.h"
63 #include <boost/bind.hpp>
64 #include <boost/signals/connection.hpp>
79 using namespace lyx::support;
80 using namespace bv_funcs;
82 extern BufferList bufferlist;
87 unsigned int const saved_positions_num = 20;
89 // All the below connection objects are needed because of a bug in some
90 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
91 // to these connections we avoid a segfault upon startup, and also at exit.
94 boost::signals::connection dispatchcon;
95 boost::signals::connection timecon;
96 boost::signals::connection doccon;
97 boost::signals::connection resizecon;
98 boost::signals::connection kpresscon;
99 boost::signals::connection selectioncon;
100 boost::signals::connection lostcon;
106 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
107 int xpos, int ypos, int width, int height)
108 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
109 using_xterm_cursor(false)
111 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
112 screen_.reset(LyXScreenFactory::create(workarea()));
115 doccon = workarea().scrollDocView
116 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
117 resizecon = workarea().workAreaResize
118 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
119 dispatchcon = workarea().dispatch
120 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
121 kpresscon = workarea().workAreaKeyPress
122 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
123 selectioncon = workarea().selectionRequested
124 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
125 lostcon = workarea().selectionLost
126 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
128 timecon = cursor_timeout.timeout
129 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
130 cursor_timeout.start();
131 saved_positions.resize(saved_positions_num);
135 void BufferView::Pimpl::addError(ErrorItem const & ei)
137 errorlist_.push_back(ei);
141 void BufferView::Pimpl::connectBuffer(Buffer & buf)
143 if (errorConnection_.connected())
146 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
147 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
148 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
152 void BufferView::Pimpl::disconnectBuffer()
154 errorConnection_.disconnect();
155 messageConnection_.disconnect();
156 busyConnection_.disconnect();
160 bool BufferView::Pimpl::newFile(string const & filename,
161 string const & tname,
164 Buffer * b = ::newFile(filename, tname, isNamed);
170 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
172 // get absolute path of file and add ".lyx" to the filename if
174 string s = FileSearch(string(), filename, "lyx");
179 // file already open?
180 if (bufferlist.exists(s)) {
181 string const file = MakeDisplayPath(s, 20);
182 string text = bformat(_("The document %1$s is already "
183 "loaded.\n\nDo you want to revert "
184 "to the saved version?"), file);
185 int const ret = Alert::prompt(_("Revert to saved document?"),
186 text, 0, 1, _("&Revert"), _("&Switch to document"));
189 buffer(bufferlist.getBuffer(s));
192 // FIXME: should be LFUN_REVERT
193 if (!bufferlist.close(bufferlist.getBuffer(s), false))
195 // Fall through to new load. (Asger)
198 Buffer * b = bufferlist.newBuffer(s);
202 if (! ::loadLyXFile(b, s)) {
203 bufferlist.release(b);
204 string text = bformat(_("The document %1$s does not yet "
205 "exist.\n\nDo you want to create "
206 "a new document?"), s);
207 int const ret = Alert::prompt(_("Create new document?"),
208 text, 0, 1, _("&Create"), _("Cancel"));
217 lastfiles->newFile(b->fileName());
219 bv_->showErrorList(_("Parse"));
225 WorkArea & BufferView::Pimpl::workarea() const
227 return *workarea_.get();
231 LyXScreen & BufferView::Pimpl::screen() const
233 return *screen_.get();
237 Painter & BufferView::Pimpl::painter() const
239 return workarea().getPainter();
243 void BufferView::Pimpl::buffer(Buffer * b)
245 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
249 buffer_->delUser(bv_);
251 // Put the old text into the TextCache, but
252 // only if the buffer is still loaded.
253 // Also set the owner of the test to 0
254 // bv_->text->owner(0);
255 textcache.add(buffer_, workarea().workWidth(), bv_->text);
256 if (lyxerr.debugging())
257 textcache.show(lyxerr, "BufferView::buffer");
262 // set current buffer
265 // if we're quitting lyx, don't bother updating stuff
269 // if we are closing the buffer, use the first buffer as current
271 buffer_ = bufferlist.first();
275 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
276 buffer_->addUser(bv_);
277 connectBuffer(*buffer_);
279 // If we don't have a text object for this, we make one
280 if (bv_->text == 0) {
281 resizeCurrentBuffer();
284 // FIXME: needed when ?
285 bv_->text->top_y(screen().topCursorVisible(bv_->text));
287 // Buffer-dependent dialogs should be updated or
288 // hidden. This should go here because some dialogs (eg ToC)
289 // require bv_->text.
290 owner_->getDialogs().updateBufferDependent(true);
292 lyxerr[Debug::INFO] << " No Buffer!" << endl;
293 owner_->getDialogs().hideBufferDependent();
295 // Also remove all remaining text's from the testcache.
296 // (there should not be any!) (if there is any it is a
298 if (lyxerr.debugging())
299 textcache.show(lyxerr, "buffer delete all");
305 owner_->updateMenubar();
306 owner_->updateToolbar();
307 owner_->updateLayoutChoice();
308 owner_->updateWindowTitle();
311 // Don't forget to update the Layout
312 string const layoutname =
313 bv_->text->cursor.par()->layout()->name();
314 owner_->setLayout(layoutname);
317 if (grfx::Previews::activated() && buffer_)
318 grfx::Previews::get().generateBufferPreviews(*buffer_);
322 bool BufferView::Pimpl::fitCursor()
326 if (bv_->theLockingInset()) {
327 bv_->theLockingInset()->fitInsetCursor(bv_);
330 ret = screen().fitCursor(bv_->text, bv_);
333 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
335 // We need to always update, in case we did a
336 // paste and we stayed anchored to a row, but
337 // the actual height of the doc changed ...
343 void BufferView::Pimpl::redoCurrentBuffer()
345 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
346 if (buffer_ && bv_->text) {
347 resizeCurrentBuffer();
349 owner_->updateLayoutChoice();
355 int BufferView::Pimpl::resizeCurrentBuffer()
357 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
359 ParagraphList::iterator par;
360 ParagraphList::iterator selstartpar;
361 ParagraphList::iterator selendpar;
362 UpdatableInset * the_locking_inset = 0;
365 pos_type selstartpos = 0;
366 pos_type selendpos = 0;
367 bool selection = false;
368 bool mark_set = false;
372 owner_->message(_("Formatting document..."));
375 par = bv_->text->cursor.par();
376 pos = bv_->text->cursor.pos();
377 selstartpar = bv_->text->selection.start.par();
378 selstartpos = bv_->text->selection.start.pos();
379 selendpar = bv_->text->selection.end.par();
380 selendpos = bv_->text->selection.end.pos();
381 selection = bv_->text->selection.set();
382 mark_set = bv_->text->selection.mark();
383 the_locking_inset = bv_->theLockingInset();
384 buffer_->resizeInsets(bv_);
385 bv_->text->init(bv_);
387 lyxerr << "text not available!\n";
388 // See if we have a text in TextCache that fits
389 // the new buffer_ with the correct width.
390 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
392 lyxerr << "text in cache!\n";
393 if (lyxerr.debugging()) {
394 lyxerr << "Found a LyXText that fits:\n";
395 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
397 // Set the owner of the newly found text
398 // bv_->text->owner(bv_);
399 if (lyxerr.debugging())
400 textcache.show(lyxerr, "resizeCurrentBuffer");
402 buffer_->resizeInsets(bv_);
404 lyxerr << "no text in cache!\n";
405 bv_->text = new LyXText(bv_);
406 buffer_->resizeInsets(bv_);
407 bv_->text->init(bv_);
410 par = bv_->text->ownerParagraphs().end();
411 selstartpar = bv_->text->ownerParagraphs().end();
412 selendpar = bv_->text->ownerParagraphs().end();
415 if (par != bv_->text->ownerParagraphs().end()) {
416 bv_->text->selection.set(true);
417 // At this point just to avoid the Delete-Empty-Paragraph-
418 // Mechanism when setting the cursor.
419 bv_->text->selection.mark(mark_set);
421 bv_->text->setCursor(selstartpar, selstartpos);
422 bv_->text->selection.cursor = bv_->text->cursor;
423 bv_->text->setCursor(selendpar, selendpos);
424 bv_->text->setSelection();
425 bv_->text->setCursor(par, pos);
427 bv_->text->setCursor(par, pos);
428 bv_->text->selection.cursor = bv_->text->cursor;
429 bv_->text->selection.set(false);
431 // remake the inset locking
432 bv_->theLockingInset(the_locking_inset);
435 bv_->text->top_y(screen().topCursorVisible(bv_->text));
440 // reset the "Formatting..." message
441 owner_->clearMessage();
449 void BufferView::Pimpl::repaint()
451 // Regenerate the screen.
452 screen().redraw(bv_->text, bv_);
456 void BufferView::Pimpl::updateScrollbar()
459 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
460 workarea().setScrollbarParams(0, 0, 0);
464 LyXText const & t = *bv_->text;
466 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
467 << t.top_y() << ", default height " << defaultRowHeight() << endl;
469 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
473 void BufferView::Pimpl::scrollDocView(int value)
475 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
480 screen().hideCursor();
482 screen().draw(bv_->text, bv_, value);
484 if (!lyxrc.cursor_follows_scrollbar)
487 LyXText * vbt = bv_->text;
489 int const height = defaultRowHeight();
490 int const first = static_cast<int>((bv_->text->top_y() + height));
491 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
493 if (vbt->cursor.y() < first)
494 vbt->setCursorFromCoordinates(0, first);
495 else if (vbt->cursor.y() > last)
496 vbt->setCursorFromCoordinates(0, last);
498 owner_->updateLayoutChoice();
502 void BufferView::Pimpl::scroll(int lines)
508 LyXText const * t = bv_->text;
509 int const line_height = defaultRowHeight();
511 // The new absolute coordinate
512 int new_top_y = t->top_y() + lines * line_height;
514 // Restrict to a valid value
515 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
516 new_top_y = std::max(0, new_top_y);
518 scrollDocView(new_top_y);
520 // Update the scrollbar.
521 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
525 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
526 key_modifier::state state)
528 bv_->owner()->getLyXFunc().processKeySym(key, state);
530 /* This is perhaps a bit of a hack. When we move
531 * around, or type, it's nice to be able to see
532 * the cursor immediately after the keypress. So
533 * we reset the toggle timeout and force the visibility
534 * of the cursor. Note we cannot do this inside
535 * dispatch() itself, because that's called recursively.
538 cursor_timeout.restart();
539 screen().showCursor(*bv_);
544 void BufferView::Pimpl::selectionRequested()
551 LyXText * text = bv_->getLyXText();
553 if (text->selection.set() &&
554 (!bv_->text->xsel_cache.set() ||
555 text->selection.start != bv_->text->xsel_cache.start ||
556 text->selection.end != bv_->text->xsel_cache.end))
558 bv_->text->xsel_cache = text->selection;
559 sel = text->selectionAsString(bv_->buffer(), false);
560 } else if (!text->selection.set()) {
562 bv_->text->xsel_cache.set(false);
565 workarea().putClipboard(sel);
570 void BufferView::Pimpl::selectionLost()
573 screen().hideCursor();
575 bv_->getLyXText()->clearSelection();
576 bv_->text->xsel_cache.set(false);
581 void BufferView::Pimpl::workAreaResize()
583 static int work_area_width;
584 static int work_area_height;
586 bool const widthChange = workarea().workWidth() != work_area_width;
587 bool const heightChange = workarea().workHeight() != work_area_height;
589 // update from work area
590 work_area_width = workarea().workWidth();
591 work_area_height = workarea().workHeight();
595 // The visible LyXView need a resize
596 resizeCurrentBuffer();
598 // Remove all texts from the textcache
599 // This is not _really_ what we want to do. What
600 // we really want to do is to delete in textcache
601 // that does not have a BufferView with matching
602 // width, but as long as we have only one BufferView
603 // deleting all gives the same result.
604 if (lyxerr.debugging())
605 textcache.show(lyxerr, "Expose delete all");
607 // FIXME: this is already done in resizeCurrentBuffer() ??
608 buffer_->resizeInsets(bv_);
609 } else if (heightChange) {
610 // fitCursor() ensures we don't jump back
611 // to the start of the document on vertical
617 if (widthChange || heightChange) {
621 // always make sure that the scrollbar is sane.
623 owner_->updateLayoutChoice();
628 void BufferView::Pimpl::update()
630 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
631 screen().update(*bv_);
632 bv_->text->clearPaint();
637 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
639 if (!text->selection.set() && (f & SELECT)) {
640 text->selection.cursor = text->cursor;
643 text->partialRebreak();
645 if (text->inset_owner) {
646 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
647 updateInset(text->inset_owner);
654 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
656 LyXText * text = bv_->text;
658 if (!text->selection.set() && (f & SELECT)) {
659 text->selection.cursor = text->cursor;
662 text->partialRebreak();
664 if (text->inset_owner) {
665 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
666 updateInset(text->inset_owner);
673 // Callback for cursor timer
674 void BufferView::Pimpl::cursorToggle()
677 cursor_timeout.restart();
681 screen().toggleCursor(*bv_);
683 cursor_timeout.restart();
687 bool BufferView::Pimpl::available() const
689 if (buffer_ && bv_->text)
695 Change const BufferView::Pimpl::getCurrentChange()
697 if (!bv_->buffer()->params.tracking_changes)
698 return Change(Change::UNCHANGED);
700 LyXText * t(bv_->getLyXText());
702 if (!t->selection.set())
703 return Change(Change::UNCHANGED);
705 LyXCursor const & cur(t->selection.start);
706 return cur.par()->lookupChangeFull(cur.pos());
710 void BufferView::Pimpl::beforeChange(LyXText * text)
713 text->clearSelection();
717 void BufferView::Pimpl::savePosition(unsigned int i)
719 if (i >= saved_positions_num)
721 saved_positions[i] = Position(buffer_->fileName(),
722 bv_->text->cursor.par()->id(),
723 bv_->text->cursor.pos());
725 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
729 void BufferView::Pimpl::restorePosition(unsigned int i)
731 if (i >= saved_positions_num)
734 string const fname = saved_positions[i].filename;
736 beforeChange(bv_->text);
738 if (fname != buffer_->fileName()) {
740 if (bufferlist.exists(fname))
741 b = bufferlist.getBuffer(fname);
743 b = bufferlist.newBuffer(fname);
744 ::loadLyXFile(b, fname); // don't ask, just load it
750 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
751 if (par == buffer_->par_iterator_end())
754 bv_->text->setCursor(par.pit(),
755 min(par->size(), saved_positions[i].par_pos));
757 update(BufferView::SELECT);
759 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
763 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
765 if (i >= saved_positions_num)
768 return !saved_positions[i].filename.empty();
772 void BufferView::Pimpl::switchKeyMap()
774 if (!lyxrc.rtl_support)
777 LyXText * text = bv_->getLyXText();
778 if (text->real_current_font.isRightToLeft()
779 && !(bv_->theLockingInset()
780 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
782 if (owner_->getIntl().keymap == Intl::PRIMARY)
783 owner_->getIntl().KeyMapSec();
785 if (owner_->getIntl().keymap == Intl::SECONDARY)
786 owner_->getIntl().KeyMapPrim();
791 void BufferView::Pimpl::insetUnlock()
793 if (bv_->theLockingInset()) {
794 bv_->theLockingInset()->insetUnlock(bv_);
795 bv_->theLockingInset(0);
801 void BufferView::Pimpl::toggleSelection(bool b)
803 if (bv_->theLockingInset())
804 bv_->theLockingInset()->toggleSelection(bv_, b);
805 screen().toggleSelection(bv_->text, bv_, b);
809 void BufferView::Pimpl::toggleToggle()
811 screen().toggleToggle(bv_->text, bv_);
815 void BufferView::Pimpl::center()
817 LyXText * t = bv_->text;
820 int const half_height = workarea().workHeight() / 2;
823 if (t->cursor.y() > half_height) {
824 new_y = t->cursor.y() - half_height;
827 // FIXME: look at this comment again ...
829 // FIXME: can we do this w/o calling screen directly ?
830 // This updates top_y() but means the fitCursor() call
831 // from the update(FITCUR) doesn't realise that we might
832 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
833 // the scrollbar to be updated as it should, so we have
834 // to do it manually. Any operation that does a center()
835 // and also might have moved top_y() must make sure to call
836 // updateScrollbar() currently. Never mind that this is a
837 // pretty obfuscated way of updating t->top_y()
838 screen().draw(t, bv_, new_y);
840 update(BufferView::SELECT);
844 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
846 workarea().putClipboard(stuff);
851 * Dispatch functions for actions which can be valid for BufferView->text
852 * and/or InsetText->text!!!
856 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
859 LyXCursor cursor = bv_->getLyXText()->cursor;
860 Buffer::inset_iterator it =
861 find_if(Buffer::inset_iterator(
862 cursor.par(), cursor.pos()),
863 buffer_->inset_iterator_end(),
864 lyx::compare_memfun(&Inset::lyxCode, code));
865 return it != buffer_->inset_iterator_end() ? (*it) : 0;
867 // Ok, this is a little bit too brute force but it
868 // should work for now. Better infrastructure is comming. (Lgb)
870 Buffer * b = bv_->buffer();
871 LyXCursor cursor = bv_->getLyXText()->cursor;
873 Buffer::inset_iterator beg = b->inset_iterator_begin();
874 Buffer::inset_iterator end = b->inset_iterator_end();
876 bool cursor_par_seen = false;
878 for (; beg != end; ++beg) {
879 if (beg.getPar() == cursor.par()) {
880 cursor_par_seen = true;
882 if (cursor_par_seen) {
883 if (beg.getPar() == cursor.par()
884 && beg.getPos() >= cursor.pos()) {
886 } else if (beg.getPar() != cursor.par()) {
893 // Now find the first inset that matches code.
894 for (; beg != end; ++beg) {
895 if (beg->lyxCode() == code) {
905 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
907 string filename = filen;
909 if (filename.empty()) {
910 // Launch a file browser
911 string initpath = lyxrc.document_path;
914 string const trypath = owner_->buffer()->filePath();
915 // If directory is writeable, use this as default.
916 if (IsDirWriteable(trypath))
920 FileDialog fileDlg(_("Select LyX document to insert"),
922 make_pair(string(_("Documents|#o#O")),
923 string(lyxrc.document_path)),
924 make_pair(string(_("Examples|#E#e")),
925 string(AddPath(system_lyxdir, "examples"))));
927 FileDialog::Result result =
928 fileDlg.open(initpath,
929 _("*.lyx| LyX Documents (*.lyx)"));
931 if (result.first == FileDialog::Later)
934 filename = result.second;
936 // check selected filename
937 if (filename.empty()) {
938 owner_->message(_("Canceled."));
943 // get absolute path of file and add ".lyx" to the filename if
945 filename = FileSearch(string(), filename, "lyx");
947 string const disp_fn = MakeDisplayPath(filename);
948 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
949 bool const res = bv_->insertLyXFile(filename);
951 owner_->message(bformat(_("Document %1$s inserted."), disp_fn));
953 owner_->message(bformat(_("Could not insert document %1$s"), disp_fn));
955 #warning remove this if update() is gone
956 bv_->text->fullRebreak();
960 void BufferView::Pimpl::trackChanges()
962 Buffer * buf(bv_->buffer());
963 bool const tracking(buf->params.tracking_changes);
966 ParIterator const end = buf->par_iterator_end();
967 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
969 buf->params.tracking_changes = true;
971 // we cannot allow undos beyond the freeze point
972 buf->undostack.clear();
974 update(BufferView::SELECT);
975 bv_->text->setCursor(buf->paragraphs.begin(), 0);
976 #warning changes FIXME
977 //moveCursorUpdate(false);
979 bool found = lyxfind::findNextChange(bv_);
981 owner_->getDialogs().show("changes");
985 ParIterator const end = buf->par_iterator_end();
986 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
987 it->untrackChanges();
988 buf->params.tracking_changes = false;
991 buf->redostack.clear();
995 // Doesn't go through lyxfunc, so we need to update the
996 // layout choice etc. ourselves
997 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
999 // e.g. Qt mouse press when no buffer
1003 screen().hideCursor();
1005 // Make sure that the cached BufferView is correct.
1006 FuncRequest ev = ev_in;
1009 bool const res = dispatch(ev);
1011 // see workAreaKeyPress
1012 cursor_timeout.restart();
1013 screen().showCursor(*bv_);
1015 // FIXME: we should skip these when selecting
1016 bv_->owner()->updateLayoutChoice();
1017 bv_->owner()->updateToolbar();
1020 // slight hack: this is only called currently when
1021 // we clicked somewhere, so we force through the display
1022 // of the new status here.
1023 bv_->owner()->clearMessage();
1029 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1031 // Make sure that the cached BufferView is correct.
1032 FuncRequest ev = ev_in;
1035 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1036 << " action[" << ev.action << ']'
1037 << " arg[" << ev.argument << ']'
1038 << " x[" << ev.x << ']'
1039 << " y[" << ev.y << ']'
1040 << " button[" << ev.button() << ']'
1043 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1045 switch (ev.action) {
1047 case LFUN_SCROLL_INSET:
1048 // this is not handled here as this function is only active
1049 // if we have a locking_inset and that one is (or contains)
1053 case LFUN_FILE_INSERT:
1054 MenuInsertLyXFile(ev.argument);
1057 case LFUN_FILE_INSERT_ASCII_PARA:
1058 InsertAsciiFile(bv_, ev.argument, true);
1061 case LFUN_FILE_INSERT_ASCII:
1062 InsertAsciiFile(bv_, ev.argument, false);
1066 lang(bv_, ev.argument);
1068 owner_->view_state_changed();
1073 owner_->view_state_changed();
1078 owner_->view_state_changed();
1083 owner_->view_state_changed();
1088 owner_->view_state_changed();
1093 owner_->view_state_changed();
1098 owner_->view_state_changed();
1103 owner_->view_state_changed();
1106 case LFUN_UNDERLINE:
1108 owner_->view_state_changed();
1111 case LFUN_FONT_SIZE:
1112 fontSize(bv_, ev.argument);
1113 owner_->view_state_changed();
1116 case LFUN_FONT_STATE:
1117 owner_->getLyXFunc().setMessage(currentState(bv_));
1120 case LFUN_INSERT_LABEL: {
1121 // Try and generate a valid label
1122 string const contents = ev.argument.empty() ?
1123 getPossibleLabel(*bv_) : ev.argument;
1124 InsetCommandParams icp("label", contents);
1125 string data = InsetCommandMailer::params2string("label", icp);
1126 owner_->getDialogs().show("label", data, 0);
1130 case LFUN_BOOKMARK_SAVE:
1131 savePosition(strToUnsignedInt(ev.argument));
1134 case LFUN_BOOKMARK_GOTO:
1135 restorePosition(strToUnsignedInt(ev.argument));
1138 case LFUN_REF_GOTO: {
1139 string label = ev.argument;
1140 if (label.empty()) {
1142 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1144 label = inset->getContents();
1150 bv_->gotoLabel(label);
1154 // --- accented characters ---------------------------
1157 case LFUN_CIRCUMFLEX:
1167 case LFUN_SPECIAL_CARON:
1170 case LFUN_HUNG_UMLAUT:
1173 if (ev.argument.empty()) {
1175 owner_->getLyXFunc().handleKeyFunc(ev.action);
1177 owner_->getLyXFunc().handleKeyFunc(ev.action);
1178 owner_->getIntl().getTransManager()
1179 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1180 update(bv_->getLyXText(), BufferView::SELECT);
1184 case LFUN_MATH_MACRO:
1185 case LFUN_MATH_DELIM:
1186 case LFUN_INSERT_MATRIX:
1187 case LFUN_INSERT_MATH:
1188 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1189 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1190 case LFUN_MATH_MODE: // Open or create an inlined math inset
1194 case LFUN_INSET_APPLY: {
1195 string const name = ev.getArg(0);
1197 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1199 // This works both for 'original' and 'mathed' insets.
1200 // Note that the localDispatch performs updateInset
1202 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1203 inset->localDispatch(fr);
1205 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1211 case LFUN_INSET_INSERT: {
1212 Inset * inset = createInset(ev);
1213 if (inset && insertInset(inset)) {
1216 string const name = ev.getArg(0);
1217 if (name == "bibitem") {
1218 // We need to do a redraw because the maximum
1219 // InsetBibitem width could have changed
1220 #warning please check you mean repaint() not update(),
1221 #warning and whether the repaint() is needed at all
1231 case LFUN_FLOAT_LIST:
1232 if (tclass.floats().typeExist(ev.argument)) {
1233 Inset * inset = new InsetFloatList(ev.argument);
1234 if (!insertInset(inset, tclass.defaultLayoutName()))
1237 lyxerr << "Non-existent float type: "
1238 << ev.argument << endl;
1242 case LFUN_LAYOUT_PARAGRAPH: {
1243 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1248 params2string(*par, data);
1250 data = "show\n" + data;
1251 bv_->owner()->getDialogs().show("paragraph", data);
1255 case LFUN_PARAGRAPH_UPDATE: {
1256 if (!bv_->owner()->getDialogs().visible("paragraph"))
1258 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1263 params2string(*par, data);
1265 // Will the paragraph accept changes from the dialog?
1266 Inset * const inset = par->inInset();
1268 !(inset && inset->forceDefaultParagraphs(inset));
1270 data = "update " + tostr(accept) + '\n' + data;
1271 bv_->owner()->getDialogs().update("paragraph", data);
1275 case LFUN_PARAGRAPH_APPLY:
1276 setParagraphParams(*bv_, ev.argument);
1279 case LFUN_THESAURUS_ENTRY:
1281 string arg = ev.argument;
1284 arg = bv_->getLyXText()->selectionAsString(buffer_,
1288 if (arg.size() > 100 || arg.empty()) {
1289 // Get word or selection
1290 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1291 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1292 // FIXME: where is getLyXText()->unselect(bv_) ?
1296 bv_->owner()->getDialogs().show("thesaurus", arg);
1300 case LFUN_TRACK_CHANGES:
1304 case LFUN_MERGE_CHANGES:
1305 owner_->getDialogs().show("changes");
1308 case LFUN_ACCEPT_ALL_CHANGES: {
1309 update(BufferView::SELECT);
1310 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1311 #warning FIXME changes
1312 //moveCursorUpdate(false);
1314 while (lyxfind::findNextChange(bv_)) {
1315 bv_->getLyXText()->acceptChange();
1317 update(BufferView::SELECT);
1321 case LFUN_REJECT_ALL_CHANGES: {
1322 update(BufferView::SELECT);
1323 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1324 #warning FIXME changes
1325 //moveCursorUpdate(false);
1327 while (lyxfind::findNextChange(bv_)) {
1328 bv_->getLyXText()->rejectChange();
1330 update(BufferView::SELECT);
1334 case LFUN_ACCEPT_CHANGE: {
1335 bv_->getLyXText()->acceptChange();
1336 update(BufferView::SELECT);
1340 case LFUN_REJECT_CHANGE: {
1341 bv_->getLyXText()->rejectChange();
1342 update(BufferView::SELECT);
1346 case LFUN_UNKNOWN_ACTION:
1347 ev.errorMessage(N_("Unknown function!"));
1351 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1358 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1360 // if we are in a locking inset we should try to insert the
1361 // inset there otherwise this is a illegal function now
1362 if (bv_->theLockingInset()) {
1363 if (bv_->theLockingInset()->insetAllowed(inset))
1364 return bv_->theLockingInset()->insertInset(bv_, inset);
1368 // not quite sure if we want this...
1369 setCursorParUndo(bv_);
1372 beforeChange(bv_->text);
1373 if (!lout.empty()) {
1374 update(BufferView::SELECT);
1375 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1376 update(BufferView::SELECT);
1378 if (!bv_->text->cursor.par()->empty()) {
1379 bv_->text->cursorLeft(bv_);
1381 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1382 update(BufferView::SELECT);
1386 LyXTextClass const & tclass =
1387 buffer_->params.getLyXTextClass();
1388 bool hasLayout = tclass.hasLayout(lres);
1389 string lay = tclass.defaultLayoutName();
1391 if (hasLayout != false) {
1395 // layout not fount using default
1396 lay = tclass.defaultLayoutName();
1399 bv_->text->setLayout(lay);
1401 bv_->text->setParagraph(0, 0,
1403 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1408 update(BufferView::SELECT);
1411 bv_->text->insertInset(inset);
1412 update(BufferView::SELECT);
1419 void BufferView::Pimpl::updateInset(Inset * inset)
1421 if (!inset || !available())
1424 // first check for locking insets
1425 if (bv_->theLockingInset()) {
1426 if (bv_->theLockingInset() == inset) {
1427 if (bv_->text->updateInset(inset)) {
1432 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1433 if (bv_->text->updateInset(bv_->theLockingInset())) {
1441 // then check if the inset is a top_level inset (has no owner)
1442 // if yes do the update as always otherwise we have to update the
1443 // toplevel inset where this inset is inside
1444 Inset * tl_inset = inset;
1445 while (tl_inset->owner())
1446 tl_inset = tl_inset->owner();
1447 if (tl_inset == inset) {
1448 update(BufferView::UPDATE);
1449 if (bv_->text->updateInset(inset)) {
1450 update(BufferView::SELECT);
1453 } else if (static_cast<UpdatableInset *>(tl_inset)
1454 ->updateInsetInInset(bv_, inset))
1456 if (bv_->text->updateInset(tl_inset)) {