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"
62 #include "support/path_defines.h"
64 #include <boost/bind.hpp>
65 #include <boost/signals/connection.hpp>
80 using namespace lyx::support;
81 using namespace bv_funcs;
83 extern BufferList bufferlist;
88 unsigned int const saved_positions_num = 20;
90 // All the below connection objects are needed because of a bug in some
91 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
92 // to these connections we avoid a segfault upon startup, and also at exit.
95 boost::signals::connection dispatchcon;
96 boost::signals::connection timecon;
97 boost::signals::connection doccon;
98 boost::signals::connection resizecon;
99 boost::signals::connection kpresscon;
100 boost::signals::connection selectioncon;
101 boost::signals::connection lostcon;
107 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
108 int xpos, int ypos, int width, int height)
109 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
110 using_xterm_cursor(false)
112 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
113 screen_.reset(LyXScreenFactory::create(workarea()));
116 doccon = workarea().scrollDocView
117 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
118 resizecon = workarea().workAreaResize
119 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
120 dispatchcon = workarea().dispatch
121 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
122 kpresscon = workarea().workAreaKeyPress
123 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
124 selectioncon = workarea().selectionRequested
125 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
126 lostcon = workarea().selectionLost
127 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
129 timecon = cursor_timeout.timeout
130 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
131 cursor_timeout.start();
132 saved_positions.resize(saved_positions_num);
136 void BufferView::Pimpl::addError(ErrorItem const & ei)
138 errorlist_.push_back(ei);
142 void BufferView::Pimpl::showReadonly(bool)
144 owner_->updateWindowTitle();
145 owner_->getDialogs().updateBufferDependent(false);
149 void BufferView::Pimpl::connectBuffer(Buffer & buf)
151 if (errorConnection_.connected())
154 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
155 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
156 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
157 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
158 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
159 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
160 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
164 void BufferView::Pimpl::disconnectBuffer()
166 errorConnection_.disconnect();
167 messageConnection_.disconnect();
168 busyConnection_.disconnect();
169 titleConnection_.disconnect();
170 timerConnection_.disconnect();
171 readonlyConnection_.disconnect();
172 closingConnection_.disconnect();
176 bool BufferView::Pimpl::newFile(string const & filename,
177 string const & tname,
180 Buffer * b = ::newFile(filename, tname, isNamed);
186 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
188 // get absolute path of file and add ".lyx" to the filename if
190 string s = FileSearch(string(), filename, "lyx");
192 bool const found = !s.empty();
197 // file already open?
198 if (bufferlist.exists(s)) {
199 string const file = MakeDisplayPath(s, 20);
200 string text = bformat(_("The document %1$s is already "
201 "loaded.\n\nDo you want to revert "
202 "to the saved version?"), file);
203 int const ret = Alert::prompt(_("Revert to saved document?"),
204 text, 0, 1, _("&Revert"), _("&Switch to document"));
207 buffer(bufferlist.getBuffer(s));
210 // FIXME: should be LFUN_REVERT
211 if (!bufferlist.close(bufferlist.getBuffer(s), false))
213 // Fall through to new load. (Asger)
220 b = bufferlist.newBuffer(s);
222 if (!::loadLyXFile(b, s)) {
223 bufferlist.release(b);
227 string text = bformat(_("The document %1$s does not yet "
228 "exist.\n\nDo you want to create "
229 "a new document?"), s);
230 int const ret = Alert::prompt(_("Create new document?"),
231 text, 0, 1, _("&Create"), _("Cancel"));
234 b = ::newFile(s, string(), true);
240 bv_->showErrorList(_("Parse"));
243 lastfiles->newFile(b->fileName());
249 WorkArea & BufferView::Pimpl::workarea() const
251 return *workarea_.get();
255 LyXScreen & BufferView::Pimpl::screen() const
257 return *screen_.get();
261 Painter & BufferView::Pimpl::painter() const
263 return workarea().getPainter();
267 void BufferView::Pimpl::buffer(Buffer * b)
269 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
273 // Put the old text into the TextCache, but
274 // only if the buffer is still loaded.
275 // Also set the owner of the test to 0
276 // bv_->text->owner(0);
277 textcache.add(buffer_, workarea().workWidth(), bv_->text);
278 if (lyxerr.debugging())
279 textcache.show(lyxerr, "BufferView::buffer");
284 // set current buffer
287 // if we're quitting lyx, don't bother updating stuff
291 // if we are closing the buffer, use the first buffer as current
293 buffer_ = bufferlist.first();
296 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
297 connectBuffer(*buffer_);
299 // If we don't have a text object for this, we make one
301 resizeCurrentBuffer();
303 // FIXME: needed when ?
304 bv_->text->top_y(screen().topCursorVisible(bv_->text));
306 // Buffer-dependent dialogs should be updated or
307 // hidden. This should go here because some dialogs (eg ToC)
308 // require bv_->text.
309 owner_->getDialogs().updateBufferDependent(true);
311 lyxerr[Debug::INFO] << " No Buffer!" << endl;
312 owner_->getDialogs().hideBufferDependent();
314 // Also remove all remaining text's from the testcache.
315 // (there should not be any!) (if there is any it is a
317 if (lyxerr.debugging())
318 textcache.show(lyxerr, "buffer delete all");
324 owner_->updateMenubar();
325 owner_->updateToolbar();
326 owner_->updateLayoutChoice();
327 owner_->updateWindowTitle();
330 // Don't forget to update the Layout
331 string const layoutname =
332 bv_->text->cursor.par()->layout()->name();
333 owner_->setLayout(layoutname);
336 if (lyx::graphics::Previews::activated() && buffer_)
337 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
341 bool BufferView::Pimpl::fitCursor()
345 if (bv_->theLockingInset()) {
346 bv_->theLockingInset()->fitInsetCursor(bv_);
349 ret = screen().fitCursor(bv_->text, bv_);
352 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
354 // We need to always update, in case we did a
355 // paste and we stayed anchored to a row, but
356 // the actual height of the doc changed ...
362 void BufferView::Pimpl::redoCurrentBuffer()
364 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
365 if (buffer_ && bv_->text) {
366 resizeCurrentBuffer();
368 owner_->updateLayoutChoice();
374 int BufferView::Pimpl::resizeCurrentBuffer()
376 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
378 ParagraphList::iterator par;
379 ParagraphList::iterator selstartpar;
380 ParagraphList::iterator selendpar;
381 UpdatableInset * the_locking_inset = 0;
384 pos_type selstartpos = 0;
385 pos_type selendpos = 0;
386 bool selection = false;
387 bool mark_set = false;
391 owner_->message(_("Formatting document..."));
394 par = bv_->text->cursor.par();
395 pos = bv_->text->cursor.pos();
396 selstartpar = bv_->text->selection.start.par();
397 selstartpos = bv_->text->selection.start.pos();
398 selendpar = bv_->text->selection.end.par();
399 selendpos = bv_->text->selection.end.pos();
400 selection = bv_->text->selection.set();
401 mark_set = bv_->text->selection.mark();
402 the_locking_inset = bv_->theLockingInset();
404 bv_->text->fullRebreak();
407 lyxerr << "text not available!" << endl;
408 // See if we have a text in TextCache that fits
409 // the new buffer_ with the correct width.
410 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
412 lyxerr << "text in cache!" << endl;
413 if (lyxerr.debugging()) {
414 lyxerr << "Found a LyXText that fits:" << endl;
415 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
417 // Set the owner of the newly found text
418 // bv_->text->owner(bv_);
419 if (lyxerr.debugging())
420 textcache.show(lyxerr, "resizeCurrentBuffer");
424 lyxerr << "no text in cache!" << endl;
425 bv_->text = new LyXText(bv_);
427 bv_->text->init(bv_);
430 par = bv_->text->ownerParagraphs().end();
431 selstartpar = bv_->text->ownerParagraphs().end();
432 selendpar = bv_->text->ownerParagraphs().end();
435 if (par != bv_->text->ownerParagraphs().end()) {
436 bv_->text->selection.set(true);
437 // At this point just to avoid the Delete-Empty-Paragraph-
438 // Mechanism when setting the cursor.
439 bv_->text->selection.mark(mark_set);
441 bv_->text->setCursor(selstartpar, selstartpos);
442 bv_->text->selection.cursor = bv_->text->cursor;
443 bv_->text->setCursor(selendpar, selendpos);
444 bv_->text->setSelection();
445 bv_->text->setCursor(par, pos);
447 bv_->text->setCursor(par, pos);
448 bv_->text->selection.cursor = bv_->text->cursor;
449 bv_->text->selection.set(false);
451 // remake the inset locking
452 bv_->theLockingInset(the_locking_inset);
455 bv_->text->top_y(screen().topCursorVisible(bv_->text));
460 // reset the "Formatting..." message
461 owner_->clearMessage();
469 void BufferView::Pimpl::updateScrollbar()
472 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
473 workarea().setScrollbarParams(0, 0, 0);
477 LyXText const & t = *bv_->text;
479 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
480 << t.top_y() << ", default height " << defaultRowHeight() << endl;
482 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
486 void BufferView::Pimpl::scrollDocView(int value)
488 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
493 screen().hideCursor();
495 screen().draw(bv_->text, bv_, value);
497 if (!lyxrc.cursor_follows_scrollbar)
500 LyXText * vbt = bv_->text;
502 int const height = defaultRowHeight();
503 int const first = static_cast<int>((bv_->text->top_y() + height));
504 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
506 if (vbt->cursor.y() < first)
507 vbt->setCursorFromCoordinates(0, first);
508 else if (vbt->cursor.y() > last)
509 vbt->setCursorFromCoordinates(0, last);
511 owner_->updateLayoutChoice();
515 void BufferView::Pimpl::scroll(int lines)
521 LyXText const * t = bv_->text;
522 int const line_height = defaultRowHeight();
524 // The new absolute coordinate
525 int new_top_y = t->top_y() + lines * line_height;
527 // Restrict to a valid value
528 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
529 new_top_y = std::max(0, new_top_y);
531 scrollDocView(new_top_y);
533 // Update the scrollbar.
534 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
538 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
539 key_modifier::state state)
541 bv_->owner()->getLyXFunc().processKeySym(key, state);
543 /* This is perhaps a bit of a hack. When we move
544 * around, or type, it's nice to be able to see
545 * the cursor immediately after the keypress. So
546 * we reset the toggle timeout and force the visibility
547 * of the cursor. Note we cannot do this inside
548 * dispatch() itself, because that's called recursively.
551 cursor_timeout.restart();
552 screen().showCursor(*bv_);
557 void BufferView::Pimpl::selectionRequested()
564 LyXText * text = bv_->getLyXText();
566 if (text->selection.set() &&
567 (!bv_->text->xsel_cache.set() ||
568 text->selection.start != bv_->text->xsel_cache.start ||
569 text->selection.end != bv_->text->xsel_cache.end))
571 bv_->text->xsel_cache = text->selection;
572 sel = text->selectionAsString(bv_->buffer(), false);
573 } else if (!text->selection.set()) {
575 bv_->text->xsel_cache.set(false);
578 workarea().putClipboard(sel);
583 void BufferView::Pimpl::selectionLost()
586 screen().hideCursor();
588 bv_->getLyXText()->clearSelection();
589 bv_->text->xsel_cache.set(false);
594 void BufferView::Pimpl::workAreaResize()
596 static int work_area_width;
597 static int work_area_height;
599 bool const widthChange = workarea().workWidth() != work_area_width;
600 bool const heightChange = workarea().workHeight() != work_area_height;
602 // update from work area
603 work_area_width = workarea().workWidth();
604 work_area_height = workarea().workHeight();
608 // The visible LyXView need a resize
609 resizeCurrentBuffer();
611 // Remove all texts from the textcache
612 // This is not _really_ what we want to do. What
613 // we really want to do is to delete in textcache
614 // that does not have a BufferView with matching
615 // width, but as long as we have only one BufferView
616 // deleting all gives the same result.
617 if (lyxerr.debugging())
618 textcache.show(lyxerr, "Expose delete all");
620 // FIXME: this is already done in resizeCurrentBuffer() ??
622 } else if (heightChange) {
623 // fitCursor() ensures we don't jump back
624 // to the start of the document on vertical
630 if (widthChange || heightChange)
633 // always make sure that the scrollbar is sane.
635 owner_->updateLayoutChoice();
640 void BufferView::Pimpl::update()
642 lyxerr << "BufferView::update()" << endl;
643 screen().redraw(*bv_);
647 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
649 if (!text->selection.set() && (f & SELECT)) {
650 text->selection.cursor = text->cursor;
653 text->partialRebreak();
655 if (text->inset_owner) {
656 text->inset_owner->setUpdateStatus(InsetText::NONE);
657 updateInset(text->inset_owner);
664 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
666 LyXText * text = bv_->text;
668 if (!text->selection.set() && (f & SELECT)) {
669 text->selection.cursor = text->cursor;
672 text->partialRebreak();
674 if (text->inset_owner) {
675 text->inset_owner->setUpdateStatus(InsetText::NONE);
676 updateInset(text->inset_owner);
683 // Callback for cursor timer
684 void BufferView::Pimpl::cursorToggle()
687 cursor_timeout.restart();
691 screen().toggleCursor(*bv_);
693 cursor_timeout.restart();
697 bool BufferView::Pimpl::available() const
699 if (buffer_ && bv_->text)
705 Change const BufferView::Pimpl::getCurrentChange()
707 if (!bv_->buffer()->params.tracking_changes)
708 return Change(Change::UNCHANGED);
710 LyXText * t(bv_->getLyXText());
712 if (!t->selection.set())
713 return Change(Change::UNCHANGED);
715 LyXCursor const & cur(t->selection.start);
716 return cur.par()->lookupChangeFull(cur.pos());
720 void BufferView::Pimpl::beforeChange(LyXText * text)
723 text->clearSelection();
727 void BufferView::Pimpl::savePosition(unsigned int i)
729 if (i >= saved_positions_num)
731 saved_positions[i] = Position(buffer_->fileName(),
732 bv_->text->cursor.par()->id(),
733 bv_->text->cursor.pos());
735 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
739 void BufferView::Pimpl::restorePosition(unsigned int i)
741 if (i >= saved_positions_num)
744 string const fname = saved_positions[i].filename;
746 beforeChange(bv_->text);
748 if (fname != buffer_->fileName()) {
750 if (bufferlist.exists(fname))
751 b = bufferlist.getBuffer(fname);
753 b = bufferlist.newBuffer(fname);
754 ::loadLyXFile(b, fname); // don't ask, just load it
760 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
761 if (par == buffer_->par_iterator_end())
764 bv_->text->setCursor(par.pit(),
765 min(par->size(), saved_positions[i].par_pos));
767 update(BufferView::SELECT);
769 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
773 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
775 if (i >= saved_positions_num)
778 return !saved_positions[i].filename.empty();
782 void BufferView::Pimpl::switchKeyMap()
784 if (!lyxrc.rtl_support)
787 LyXText * text = bv_->getLyXText();
788 if (text->real_current_font.isRightToLeft()
789 && !(bv_->theLockingInset()
790 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
792 if (owner_->getIntl().keymap == Intl::PRIMARY)
793 owner_->getIntl().KeyMapSec();
795 if (owner_->getIntl().keymap == Intl::SECONDARY)
796 owner_->getIntl().KeyMapPrim();
801 void BufferView::Pimpl::insetUnlock()
803 if (bv_->theLockingInset()) {
804 bv_->theLockingInset()->insetUnlock(bv_);
805 bv_->theLockingInset(0);
811 void BufferView::Pimpl::toggleSelection(bool b)
813 screen().toggleSelection(bv_->text, bv_, b);
817 void BufferView::Pimpl::center()
819 LyXText * t = bv_->text;
822 int const half_height = workarea().workHeight() / 2;
825 if (t->cursor.y() > half_height) {
826 new_y = t->cursor.y() - half_height;
829 // FIXME: look at this comment again ...
831 // FIXME: can we do this w/o calling screen directly ?
832 // This updates top_y() but means the fitCursor() call
833 // from the update(FITCUR) doesn't realise that we might
834 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
835 // the scrollbar to be updated as it should, so we have
836 // to do it manually. Any operation that does a center()
837 // and also might have moved top_y() must make sure to call
838 // updateScrollbar() currently. Never mind that this is a
839 // pretty obfuscated way of updating t->top_y()
840 screen().draw(t, bv_, new_y);
842 update(BufferView::SELECT);
846 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
848 workarea().putClipboard(stuff);
853 * Dispatch functions for actions which can be valid for BufferView->text
854 * and/or InsetText->text!!!
858 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
861 LyXCursor cursor = bv_->getLyXText()->cursor;
862 Buffer::inset_iterator it =
863 find_if(Buffer::inset_iterator(
864 cursor.par(), cursor.pos()),
865 buffer_->inset_iterator_end(),
866 lyx::compare_memfun(&Inset::lyxCode, code));
867 return it != buffer_->inset_iterator_end() ? (*it) : 0;
869 // Ok, this is a little bit too brute force but it
870 // should work for now. Better infrastructure is comming. (Lgb)
872 Buffer * b = bv_->buffer();
873 LyXCursor cursor = bv_->getLyXText()->cursor;
875 Buffer::inset_iterator beg = b->inset_iterator_begin();
876 Buffer::inset_iterator end = b->inset_iterator_end();
878 bool cursor_par_seen = false;
880 for (; beg != end; ++beg) {
881 if (beg.getPar() == cursor.par()) {
882 cursor_par_seen = true;
884 if (cursor_par_seen) {
885 if (beg.getPar() == cursor.par()
886 && beg.getPos() >= cursor.pos()) {
888 } else if (beg.getPar() != cursor.par()) {
895 // Now find the first inset that matches code.
896 for (; beg != end; ++beg) {
897 if (beg->lyxCode() == code) {
907 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
909 string filename = filen;
911 if (filename.empty()) {
912 // Launch a file browser
913 string initpath = lyxrc.document_path;
916 string const trypath = owner_->buffer()->filePath();
917 // If directory is writeable, use this as default.
918 if (IsDirWriteable(trypath))
922 FileDialog fileDlg(_("Select LyX document to insert"),
924 make_pair(string(_("Documents|#o#O")),
925 string(lyxrc.document_path)),
926 make_pair(string(_("Examples|#E#e")),
927 string(AddPath(system_lyxdir(), "examples"))));
929 FileDialog::Result result =
930 fileDlg.open(initpath,
931 _("*.lyx| LyX Documents (*.lyx)"));
933 if (result.first == FileDialog::Later)
936 filename = result.second;
938 // check selected filename
939 if (filename.empty()) {
940 owner_->message(_("Canceled."));
945 // get absolute path of file and add ".lyx" to the filename if
947 filename = FileSearch(string(), filename, "lyx");
949 string const disp_fn = MakeDisplayPath(filename);
950 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
951 if (bv_->insertLyXFile(filename))
952 owner_->message(bformat(_("Document %1$s inserted."),
955 owner_->message(bformat(_("Could not insert document %1$s"),
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 = lyx::find::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(InsetOld::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 InsetOld * 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 check whether the update() is needed at all
1230 case LFUN_FLOAT_LIST:
1231 if (tclass.floats().typeExist(ev.argument)) {
1232 InsetOld * inset = new InsetFloatList(ev.argument);
1233 if (!insertInset(inset, tclass.defaultLayoutName()))
1236 lyxerr << "Non-existent float type: "
1237 << ev.argument << endl;
1241 case LFUN_LAYOUT_PARAGRAPH: {
1242 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1247 params2string(*par, data);
1249 data = "show\n" + data;
1250 bv_->owner()->getDialogs().show("paragraph", data);
1254 case LFUN_PARAGRAPH_UPDATE: {
1255 if (!bv_->owner()->getDialogs().visible("paragraph"))
1257 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1262 params2string(*par, data);
1264 // Will the paragraph accept changes from the dialog?
1265 InsetOld * const inset = par->inInset();
1267 !(inset && inset->forceDefaultParagraphs(inset));
1269 data = "update " + tostr(accept) + '\n' + data;
1270 bv_->owner()->getDialogs().update("paragraph", data);
1274 case LFUN_PARAGRAPH_APPLY:
1275 setParagraphParams(*bv_, ev.argument);
1278 case LFUN_THESAURUS_ENTRY:
1280 string arg = ev.argument;
1283 arg = bv_->getLyXText()->selectionAsString(buffer_,
1287 if (arg.size() > 100 || arg.empty()) {
1288 // Get word or selection
1289 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1290 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1291 // FIXME: where is getLyXText()->unselect(bv_) ?
1295 bv_->owner()->getDialogs().show("thesaurus", arg);
1299 case LFUN_TRACK_CHANGES:
1303 case LFUN_MERGE_CHANGES:
1304 owner_->getDialogs().show("changes");
1307 case LFUN_ACCEPT_ALL_CHANGES: {
1308 update(BufferView::SELECT);
1309 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1310 #warning FIXME changes
1311 //moveCursorUpdate(false);
1313 while (lyx::find::findNextChange(bv_)) {
1314 bv_->getLyXText()->acceptChange();
1316 update(BufferView::SELECT);
1320 case LFUN_REJECT_ALL_CHANGES: {
1321 update(BufferView::SELECT);
1322 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1323 #warning FIXME changes
1324 //moveCursorUpdate(false);
1326 while (lyx::find::findNextChange(bv_)) {
1327 bv_->getLyXText()->rejectChange();
1329 update(BufferView::SELECT);
1333 case LFUN_ACCEPT_CHANGE: {
1334 bv_->getLyXText()->acceptChange();
1335 update(BufferView::SELECT);
1339 case LFUN_REJECT_CHANGE: {
1340 bv_->getLyXText()->rejectChange();
1341 update(BufferView::SELECT);
1345 case LFUN_UNKNOWN_ACTION:
1346 ev.errorMessage(N_("Unknown function!"));
1350 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1357 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1359 // if we are in a locking inset we should try to insert the
1360 // inset there otherwise this is a illegal function now
1361 if (bv_->theLockingInset()) {
1362 if (bv_->theLockingInset()->insetAllowed(inset))
1363 return bv_->theLockingInset()->insertInset(bv_, inset);
1367 // not quite sure if we want this...
1368 recordUndo(bv_, Undo::ATOMIC);
1371 beforeChange(bv_->text);
1372 if (!lout.empty()) {
1373 update(BufferView::SELECT);
1374 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1375 update(BufferView::SELECT);
1377 if (!bv_->text->cursor.par()->empty()) {
1378 bv_->text->cursorLeft(bv_);
1380 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1381 update(BufferView::SELECT);
1385 LyXTextClass const & tclass =
1386 buffer_->params.getLyXTextClass();
1387 bool hasLayout = tclass.hasLayout(lres);
1388 string lay = tclass.defaultLayoutName();
1390 if (hasLayout != false) {
1394 // layout not fount using default
1395 lay = tclass.defaultLayoutName();
1398 bv_->text->setLayout(lay);
1400 bv_->text->setParagraph(0, 0,
1402 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1407 update(BufferView::SELECT);
1410 bv_->text->insertInset(inset);
1411 update(BufferView::SELECT);
1418 void BufferView::Pimpl::updateInset(InsetOld * inset)
1420 if (!inset || !available())
1423 // first check for locking insets
1424 if (bv_->theLockingInset()) {
1425 if (bv_->theLockingInset() == inset) {
1426 if (bv_->text->updateInset(inset)) {
1431 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1432 if (bv_->text->updateInset(bv_->theLockingInset())) {
1440 // then check if the inset is a top_level inset (has no owner)
1441 // if yes do the update as always otherwise we have to update the
1442 // toplevel inset where this inset is inside
1443 InsetOld * tl_inset = inset;
1444 while (tl_inset->owner())
1445 tl_inset = tl_inset->owner();
1446 if (tl_inset == inset) {
1447 update(BufferView::UPDATE);
1448 if (bv_->text->updateInset(inset)) {
1449 update(BufferView::SELECT);
1452 } else if (static_cast<UpdatableInset *>(tl_inset)
1453 ->updateInsetInInset(bv_, inset))
1455 if (bv_->text->updateInset(tl_inset)) {