2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
11 #include "BufferView_pimpl.h"
12 #include "bufferlist.h"
14 #include "buffer_funcs.h"
15 #include "bufferview_funcs.h"
19 #include "FloatList.h"
20 #include "funcrequest.h"
23 #include "iterators.h"
25 #include "lyx_cb.h" // added for Dispatch functions
32 #include "lastfiles.h"
33 #include "paragraph.h"
34 #include "ParagraphParameters.h"
35 #include "TextCache.h"
36 #include "undo_funcs.h"
38 #include "insets/insetfloatlist.h"
39 #include "insets/insetgraphics.h"
40 #include "insets/insetinclude.h"
41 #include "insets/insetref.h"
42 #include "insets/insettext.h"
44 #include "frontends/Alert.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/FileDialog.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/LyXScreenFactory.h"
49 #include "frontends/mouse_state.h"
50 #include "frontends/screen.h"
51 #include "frontends/WorkArea.h"
52 #include "frontends/WorkAreaFactory.h"
54 #include "mathed/formulabase.h"
56 #include "graphics/Previews.h"
58 #include "support/LAssert.h"
59 #include "support/tostr.h"
60 #include "support/filetools.h"
62 #include <boost/bind.hpp>
63 #include <boost/signals/connection.hpp>
78 using namespace bv_funcs;
80 extern BufferList bufferlist;
85 unsigned int const saved_positions_num = 20;
87 // All the below connection objects are needed because of a bug in some
88 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
89 // to these connections we avoid a segfault upon startup, and also at exit.
92 boost::signals::connection dispatchcon;
93 boost::signals::connection timecon;
94 boost::signals::connection doccon;
95 boost::signals::connection resizecon;
96 boost::signals::connection kpresscon;
97 boost::signals::connection selectioncon;
98 boost::signals::connection lostcon;
104 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
105 int xpos, int ypos, int width, int height)
106 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
107 using_xterm_cursor(false)
109 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
110 screen_.reset(LyXScreenFactory::create(workarea()));
113 doccon = workarea().scrollDocView
114 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
115 resizecon = workarea().workAreaResize
116 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
117 dispatchcon = workarea().dispatch
118 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
119 kpresscon = workarea().workAreaKeyPress
120 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
121 selectioncon = workarea().selectionRequested
122 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
123 lostcon = workarea().selectionLost
124 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
126 timecon = cursor_timeout.timeout
127 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
128 cursor_timeout.start();
129 saved_positions.resize(saved_positions_num);
133 void BufferView::Pimpl::addError(ErrorItem const & ei)
135 errorlist_.push_back(ei);
140 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
142 // get absolute path of file and add ".lyx" to the filename if
144 string s = FileSearch(string(), filename, "lyx");
149 // file already open?
150 if (bufferlist.exists(s)) {
151 string const file = MakeDisplayPath(s, 20);
152 string text = bformat(_("The document %1$s is already "
153 "loaded.\n\nDo you want to revert "
154 "to the saved version?"), file);
155 int const ret = Alert::prompt(_("Revert to saved document?"),
156 text, 0, 1, _("&Revert"), _("&Switch to document"));
159 buffer(bufferlist.getBuffer(s));
162 // FIXME: should be LFUN_REVERT
163 if (!bufferlist.close(bufferlist.getBuffer(s), false))
165 // Fall through to new load. (Asger)
168 Buffer * b = bufferlist.newBuffer(s);
170 //attach to the error signal in the buffer
171 b->parseError.connect(boost::bind(&BufferView::Pimpl::addError,
174 bool loaded = ::loadLyXFile(b, s);
177 bufferlist.release(b);
178 string text = bformat(_("The document %1$s does "
180 "Do you want to create "
181 "a new document?"), s);
182 int const ret = Alert::prompt(_("Create new document?"),
183 text, 0, 1, _("&Create"), _("Cancel"));
186 bufferlist.close(buffer_, false);
187 buffer(newFile(s, string(), true));
194 lastfiles->newFile(b->fileName());
197 bv_->showErrorList(_("Parse"));
202 WorkArea & BufferView::Pimpl::workarea() const
204 return *workarea_.get();
208 LyXScreen & BufferView::Pimpl::screen() const
210 return *screen_.get();
214 Painter & BufferView::Pimpl::painter() const
216 return workarea().getPainter();
220 void BufferView::Pimpl::buffer(Buffer * b)
222 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
225 buffer_->delUser(bv_);
227 // Put the old text into the TextCache, but
228 // only if the buffer is still loaded.
229 // Also set the owner of the test to 0
230 // bv_->text->owner(0);
231 textcache.add(buffer_, workarea().workWidth(), bv_->text);
232 if (lyxerr.debugging())
233 textcache.show(lyxerr, "BufferView::buffer");
238 // set current buffer
241 // if we're quitting lyx, don't bother updating stuff
245 // if we are closing the buffer, use the first buffer as current
247 buffer_ = bufferlist.first();
251 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
252 buffer_->addUser(bv_);
254 // If we don't have a text object for this, we make one
255 if (bv_->text == 0) {
256 resizeCurrentBuffer();
259 // FIXME: needed when ?
260 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
262 // Buffer-dependent dialogs should be updated or
263 // hidden. This should go here because some dialogs (eg ToC)
264 // require bv_->text.
265 owner_->getDialogs().updateBufferDependent(true);
267 lyxerr[Debug::INFO] << " No Buffer!" << endl;
268 owner_->getDialogs().hideBufferDependent();
270 // Also remove all remaining text's from the testcache.
271 // (there should not be any!) (if there is any it is a
273 if (lyxerr.debugging())
274 textcache.show(lyxerr, "buffer delete all");
280 owner_->updateMenubar();
281 owner_->updateToolbar();
282 owner_->updateLayoutChoice();
283 owner_->updateWindowTitle();
286 // Don't forget to update the Layout
287 string const layoutname =
288 bv_->text->cursor.par()->layout()->name();
289 owner_->setLayout(layoutname);
292 if (grfx::Previews::activated() && buffer_)
293 grfx::Previews::get().generateBufferPreviews(*buffer_);
297 bool BufferView::Pimpl::fitCursor()
301 if (bv_->theLockingInset()) {
302 bv_->theLockingInset()->fitInsetCursor(bv_);
305 ret = screen().fitCursor(bv_->text, bv_);
308 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
310 // We need to always update, in case we did a
311 // paste and we stayed anchored to a row, but
312 // the actual height of the doc changed ...
318 void BufferView::Pimpl::redoCurrentBuffer()
320 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
321 if (buffer_ && bv_->text) {
322 resizeCurrentBuffer();
324 owner_->updateLayoutChoice();
330 int BufferView::Pimpl::resizeCurrentBuffer()
332 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
334 ParagraphList::iterator par;
335 ParagraphList::iterator selstartpar;
336 ParagraphList::iterator selendpar;
337 UpdatableInset * the_locking_inset = 0;
340 pos_type selstartpos = 0;
341 pos_type selendpos = 0;
342 bool selection = false;
343 bool mark_set = false;
347 owner_->message(_("Formatting document..."));
350 par = bv_->text->cursor.par();
351 pos = bv_->text->cursor.pos();
352 selstartpar = bv_->text->selection.start.par();
353 selstartpos = bv_->text->selection.start.pos();
354 selendpar = bv_->text->selection.end.par();
355 selendpos = bv_->text->selection.end.pos();
356 selection = bv_->text->selection.set();
357 mark_set = bv_->text->selection.mark();
358 the_locking_inset = bv_->theLockingInset();
359 buffer_->resizeInsets(bv_);
360 // I don't think the delete and new are necessary here we
361 // just could call only init! (Jug 20020419)
363 bv_->text = new LyXText(bv_);
364 bv_->text->init(bv_);
366 // See if we have a text in TextCache that fits
367 // the new buffer_ with the correct width.
368 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
370 if (lyxerr.debugging()) {
371 lyxerr << "Found a LyXText that fits:\n";
372 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
374 // Set the owner of the newly found text
375 // bv_->text->owner(bv_);
376 if (lyxerr.debugging())
377 textcache.show(lyxerr, "resizeCurrentBuffer");
379 buffer_->resizeInsets(bv_);
381 bv_->text = new LyXText(bv_);
382 bv_->text->init(bv_);
383 //buffer_->resizeInsets(bv_);
386 par = bv_->text->ownerParagraphs().end();
387 selstartpar = bv_->text->ownerParagraphs().end();
388 selendpar = bv_->text->ownerParagraphs().end();
391 if (par != bv_->text->ownerParagraphs().end()) {
392 bv_->text->selection.set(true);
393 // At this point just to avoid the Delete-Empty-Paragraph-
394 // Mechanism when setting the cursor.
395 bv_->text->selection.mark(mark_set);
397 bv_->text->setCursor(selstartpar, selstartpos);
398 bv_->text->selection.cursor = bv_->text->cursor;
399 bv_->text->setCursor(selendpar, selendpos);
400 bv_->text->setSelection();
401 bv_->text->setCursor(par, pos);
403 bv_->text->setCursor(par, pos);
404 bv_->text->selection.cursor = bv_->text->cursor;
405 bv_->text->selection.set(false);
407 // remake the inset locking
408 bv_->theLockingInset(the_locking_inset);
411 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
416 // reset the "Formatting..." message
417 owner_->clearMessage();
425 void BufferView::Pimpl::repaint()
427 // Regenerate the screen.
428 screen().redraw(bv_->text, bv_);
432 void BufferView::Pimpl::updateScrollbar()
435 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
436 workarea().setScrollbarParams(0, 0, 0);
440 LyXText const & t = *bv_->text;
442 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
443 << t.top_y() << ", default height " << defaultRowHeight() << endl;
445 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
449 void BufferView::Pimpl::scrollDocView(int value)
451 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
456 screen().hideCursor();
458 screen().draw(bv_->text, bv_, value);
460 if (!lyxrc.cursor_follows_scrollbar)
463 LyXText * vbt = bv_->text;
465 int const height = defaultRowHeight();
466 int const first = static_cast<int>((bv_->text->top_y() + height));
467 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
469 if (vbt->cursor.y() < first)
470 vbt->setCursorFromCoordinates(0, first);
471 else if (vbt->cursor.y() > last)
472 vbt->setCursorFromCoordinates(0, last);
474 owner_->updateLayoutChoice();
478 void BufferView::Pimpl::scroll(int lines)
484 LyXText const * t = bv_->text;
485 int const line_height = defaultRowHeight();
487 // The new absolute coordinate
488 int new_top_y = t->top_y() + lines * line_height;
490 // Restrict to a valid value
491 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
492 new_top_y = std::max(0, new_top_y);
494 scrollDocView(new_top_y);
496 // Update the scrollbar.
497 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
501 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
502 key_modifier::state state)
504 bv_->owner()->getLyXFunc().processKeySym(key, state);
506 /* This is perhaps a bit of a hack. When we move
507 * around, or type, it's nice to be able to see
508 * the cursor immediately after the keypress. So
509 * we reset the toggle timeout and force the visibility
510 * of the cursor. Note we cannot do this inside
511 * dispatch() itself, because that's called recursively.
514 cursor_timeout.restart();
515 screen().showCursor(*bv_);
520 void BufferView::Pimpl::selectionRequested()
527 LyXText * text = bv_->getLyXText();
529 if (text->selection.set() &&
530 (!bv_->text->xsel_cache.set() ||
531 text->selection.start != bv_->text->xsel_cache.start ||
532 text->selection.end != bv_->text->xsel_cache.end))
534 bv_->text->xsel_cache = text->selection;
535 sel = text->selectionAsString(bv_->buffer(), false);
536 } else if (!text->selection.set()) {
538 bv_->text->xsel_cache.set(false);
541 workarea().putClipboard(sel);
546 void BufferView::Pimpl::selectionLost()
549 screen().hideCursor();
551 bv_->getLyXText()->clearSelection();
552 bv_->text->xsel_cache.set(false);
557 void BufferView::Pimpl::workAreaResize()
559 static int work_area_width;
560 static int work_area_height;
562 bool const widthChange = workarea().workWidth() != work_area_width;
563 bool const heightChange = workarea().workHeight() != work_area_height;
565 // update from work area
566 work_area_width = workarea().workWidth();
567 work_area_height = workarea().workHeight();
571 // The visible LyXView need a resize
572 resizeCurrentBuffer();
574 // Remove all texts from the textcache
575 // This is not _really_ what we want to do. What
576 // we really want to do is to delete in textcache
577 // that does not have a BufferView with matching
578 // width, but as long as we have only one BufferView
579 // deleting all gives the same result.
580 if (lyxerr.debugging())
581 textcache.show(lyxerr, "Expose delete all");
583 // FIXME: this is already done in resizeCurrentBuffer() ??
584 buffer_->resizeInsets(bv_);
585 } else if (heightChange) {
586 // fitCursor() ensures we don't jump back
587 // to the start of the document on vertical
593 if (widthChange || heightChange) {
597 // always make sure that the scrollbar is sane.
599 owner_->updateLayoutChoice();
604 void BufferView::Pimpl::update()
606 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
607 screen().update(*bv_);
608 bv_->text->clearPaint();
613 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
615 if (!text->selection.set() && (f & SELECT)) {
616 text->selection.cursor = text->cursor;
621 if (text->inset_owner) {
622 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
623 updateInset(text->inset_owner);
630 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
632 LyXText * text = bv_->text;
634 if (!text->selection.set() && (f & SELECT)) {
635 text->selection.cursor = text->cursor;
640 if (text->inset_owner) {
641 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
642 updateInset(text->inset_owner);
649 // Callback for cursor timer
650 void BufferView::Pimpl::cursorToggle()
653 cursor_timeout.restart();
657 screen().toggleCursor(*bv_);
659 cursor_timeout.restart();
663 bool BufferView::Pimpl::available() const
665 if (buffer_ && bv_->text)
671 Change const BufferView::Pimpl::getCurrentChange()
673 if (!bv_->buffer()->params.tracking_changes)
674 return Change(Change::UNCHANGED);
676 LyXText * t(bv_->getLyXText());
678 if (!t->selection.set())
679 return Change(Change::UNCHANGED);
681 LyXCursor const & cur(t->selection.start);
682 return cur.par()->lookupChangeFull(cur.pos());
686 void BufferView::Pimpl::beforeChange(LyXText * text)
689 text->clearSelection();
693 void BufferView::Pimpl::savePosition(unsigned int i)
695 if (i >= saved_positions_num)
697 saved_positions[i] = Position(buffer_->fileName(),
698 bv_->text->cursor.par()->id(),
699 bv_->text->cursor.pos());
701 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
705 void BufferView::Pimpl::restorePosition(unsigned int i)
707 if (i >= saved_positions_num)
710 string const fname = saved_positions[i].filename;
712 beforeChange(bv_->text);
714 if (fname != buffer_->fileName()) {
716 if (bufferlist.exists(fname))
717 b = bufferlist.getBuffer(fname);
719 b = bufferlist.newBuffer(fname);
720 ::loadLyXFile(b, fname); // don't ask, just load it
726 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
727 if (par == buffer_->par_iterator_end())
730 bv_->text->setCursor(par.pit(),
731 min(par->size(), saved_positions[i].par_pos));
733 update(BufferView::SELECT);
735 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
739 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
741 if (i >= saved_positions_num)
744 return !saved_positions[i].filename.empty();
748 void BufferView::Pimpl::switchKeyMap()
750 if (!lyxrc.rtl_support)
753 LyXText * text = bv_->getLyXText();
754 if (text->real_current_font.isRightToLeft()
755 && !(bv_->theLockingInset()
756 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
758 if (owner_->getIntl().keymap == Intl::PRIMARY)
759 owner_->getIntl().KeyMapSec();
761 if (owner_->getIntl().keymap == Intl::SECONDARY)
762 owner_->getIntl().KeyMapPrim();
767 void BufferView::Pimpl::insetUnlock()
769 if (bv_->theLockingInset()) {
770 bv_->theLockingInset()->insetUnlock(bv_);
771 bv_->theLockingInset(0);
777 void BufferView::Pimpl::toggleSelection(bool b)
779 if (bv_->theLockingInset())
780 bv_->theLockingInset()->toggleSelection(bv_, b);
781 screen().toggleSelection(bv_->text, bv_, b);
785 void BufferView::Pimpl::toggleToggle()
787 screen().toggleToggle(bv_->text, bv_);
791 void BufferView::Pimpl::center()
793 LyXText * t = bv_->text;
796 int const half_height = workarea().workHeight() / 2;
799 if (t->cursor.y() > half_height) {
800 new_y = t->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()
814 screen().draw(t, bv_, new_y);
816 update(BufferView::SELECT);
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 Inset * BufferView::Pimpl::getInsetByCode(Inset::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 comming. (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 bool const res = bv_->insertLyXFile(filename);
927 owner_->message(bformat(_("Document %1$s inserted."), disp_fn));
929 owner_->message(bformat(_("Could not insert document %1$s"), disp_fn));
933 void BufferView::Pimpl::trackChanges()
935 Buffer * buf(bv_->buffer());
936 bool const tracking(buf->params.tracking_changes);
939 ParIterator const end = buf->par_iterator_end();
940 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
942 buf->params.tracking_changes = true;
944 // we cannot allow undos beyond the freeze point
945 buf->undostack.clear();
947 update(BufferView::SELECT);
948 bv_->text->setCursor(buf->paragraphs.begin(), 0);
949 #warning changes FIXME
950 //moveCursorUpdate(false);
952 bool found = lyxfind::findNextChange(bv_);
954 owner_->getDialogs().show("changes");
958 ParIterator const end = buf->par_iterator_end();
959 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
960 it->untrackChanges();
961 buf->params.tracking_changes = false;
964 buf->redostack.clear();
968 // Doesn't go through lyxfunc, so we need to update the
969 // layout choice etc. ourselves
970 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
972 // e.g. Qt mouse press when no buffer
976 screen().hideCursor();
978 bool const res = dispatch(ev_in);
980 // see workAreaKeyPress
981 cursor_timeout.restart();
982 screen().showCursor(*bv_);
984 // FIXME: we should skip these when selecting
985 bv_->owner()->updateLayoutChoice();
986 bv_->owner()->updateToolbar();
989 // slight hack: this is only called currently when
990 // we clicked somewhere, so we force through the display
991 // of the new status here.
992 bv_->owner()->clearMessage();
998 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1000 // Make sure that the cached BufferView is correct.
1001 FuncRequest ev = ev_in;
1004 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1005 << " action[" << ev.action << ']'
1006 << " arg[" << ev.argument << ']'
1007 << " x[" << ev.x << ']'
1008 << " y[" << ev.y << ']'
1009 << " button[" << ev.button() << ']'
1012 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1014 switch (ev.action) {
1016 case LFUN_SCROLL_INSET:
1017 // this is not handled here as this function is only active
1018 // if we have a locking_inset and that one is (or contains)
1022 case LFUN_FILE_INSERT:
1023 MenuInsertLyXFile(ev.argument);
1026 case LFUN_FILE_INSERT_ASCII_PARA:
1027 InsertAsciiFile(bv_, ev.argument, true);
1030 case LFUN_FILE_INSERT_ASCII:
1031 InsertAsciiFile(bv_, ev.argument, false);
1035 lang(bv_, ev.argument);
1037 owner_->view_state_changed();
1042 owner_->view_state_changed();
1047 owner_->view_state_changed();
1052 owner_->view_state_changed();
1057 owner_->view_state_changed();
1062 owner_->view_state_changed();
1067 owner_->view_state_changed();
1072 owner_->view_state_changed();
1075 case LFUN_UNDERLINE:
1077 owner_->view_state_changed();
1080 case LFUN_FONT_SIZE:
1081 fontSize(bv_, ev.argument);
1082 owner_->view_state_changed();
1085 case LFUN_FONT_STATE:
1086 owner_->getLyXFunc().setMessage(currentState(bv_));
1089 case LFUN_INSERT_LABEL: {
1090 // Try and generate a valid label
1091 string const contents = ev.argument.empty() ?
1092 getPossibleLabel(*bv_) : ev.argument;
1093 InsetCommandParams icp("label", contents);
1094 string data = InsetCommandMailer::params2string("label", icp);
1095 owner_->getDialogs().show("label", data, 0);
1099 case LFUN_BOOKMARK_SAVE:
1100 savePosition(strToUnsignedInt(ev.argument));
1103 case LFUN_BOOKMARK_GOTO:
1104 restorePosition(strToUnsignedInt(ev.argument));
1107 case LFUN_REF_GOTO: {
1108 string label = ev.argument;
1109 if (label.empty()) {
1111 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1113 label = inset->getContents();
1119 bv_->gotoLabel(label);
1123 // --- accented characters ---------------------------
1126 case LFUN_CIRCUMFLEX:
1136 case LFUN_SPECIAL_CARON:
1139 case LFUN_HUNG_UMLAUT:
1142 if (ev.argument.empty()) {
1144 owner_->getLyXFunc().handleKeyFunc(ev.action);
1146 owner_->getLyXFunc().handleKeyFunc(ev.action);
1147 owner_->getIntl().getTransManager()
1148 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1149 update(bv_->getLyXText(), BufferView::SELECT);
1153 case LFUN_MATH_MACRO:
1154 case LFUN_MATH_DELIM:
1155 case LFUN_INSERT_MATRIX:
1156 case LFUN_INSERT_MATH:
1157 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1158 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1159 case LFUN_MATH_MODE: // Open or create an inlined math inset
1163 case LFUN_INSET_APPLY: {
1164 string const name = ev.getArg(0);
1166 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1168 // This works both for 'original' and 'mathed' insets.
1169 // Note that the localDispatch performs updateInset
1171 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1172 inset->localDispatch(fr);
1174 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1180 case LFUN_INSET_INSERT: {
1181 Inset * inset = createInset(ev);
1182 if (inset && insertInset(inset)) {
1185 string const name = ev.getArg(0);
1186 if (name == "bibitem") {
1187 // We need to do a redraw because the maximum
1188 // InsetBibitem width could have changed
1189 #warning please check you mean repaint() not update(),
1190 #warning and whether the repaint() is needed at all
1200 case LFUN_FLOAT_LIST:
1201 if (tclass.floats().typeExist(ev.argument)) {
1202 Inset * inset = new InsetFloatList(ev.argument);
1203 if (!insertInset(inset, tclass.defaultLayoutName()))
1206 lyxerr << "Non-existent float type: "
1207 << ev.argument << endl;
1211 case LFUN_LAYOUT_PARAGRAPH: {
1212 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1217 params2string(*par, data);
1219 data = "show\n" + data;
1220 bv_->owner()->getDialogs().show("paragraph", data);
1224 case LFUN_PARAGRAPH_UPDATE: {
1225 if (!bv_->owner()->getDialogs().visible("paragraph"))
1227 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1232 params2string(*par, data);
1234 // Will the paragraph accept changes from the dialog?
1235 Inset * const inset = par->inInset();
1237 !(inset && inset->forceDefaultParagraphs(inset));
1239 data = "update " + tostr(accept) + '\n' + data;
1240 bv_->owner()->getDialogs().update("paragraph", data);
1244 case LFUN_PARAGRAPH_APPLY:
1245 setParagraphParams(*bv_, ev.argument);
1248 case LFUN_THESAURUS_ENTRY:
1250 string arg = ev.argument;
1253 arg = bv_->getLyXText()->selectionAsString(buffer_,
1257 if (arg.size() > 100 || arg.empty()) {
1258 // Get word or selection
1259 bv_->getLyXText()->selectWordWhenUnderCursor(LyXText::WHOLE_WORD);
1260 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1261 // FIXME: where is getLyXText()->unselect(bv_) ?
1265 bv_->owner()->getDialogs().show("thesaurus", arg);
1269 case LFUN_TRACK_CHANGES:
1273 case LFUN_MERGE_CHANGES:
1274 owner_->getDialogs().show("changes");
1277 case LFUN_ACCEPT_ALL_CHANGES: {
1278 update(BufferView::SELECT);
1279 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1280 #warning FIXME changes
1281 //moveCursorUpdate(false);
1283 while (lyxfind::findNextChange(bv_)) {
1284 bv_->getLyXText()->acceptChange();
1286 update(BufferView::SELECT);
1290 case LFUN_REJECT_ALL_CHANGES: {
1291 update(BufferView::SELECT);
1292 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1293 #warning FIXME changes
1294 //moveCursorUpdate(false);
1296 while (lyxfind::findNextChange(bv_)) {
1297 bv_->getLyXText()->rejectChange();
1299 update(BufferView::SELECT);
1303 case LFUN_ACCEPT_CHANGE: {
1304 bv_->getLyXText()->acceptChange();
1305 update(BufferView::SELECT);
1309 case LFUN_REJECT_CHANGE: {
1310 bv_->getLyXText()->rejectChange();
1311 update(BufferView::SELECT);
1315 case LFUN_UNKNOWN_ACTION:
1316 ev.errorMessage(N_("Unknown function!"));
1320 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1327 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1329 // if we are in a locking inset we should try to insert the
1330 // inset there otherwise this is a illegal function now
1331 if (bv_->theLockingInset()) {
1332 if (bv_->theLockingInset()->insetAllowed(inset))
1333 return bv_->theLockingInset()->insertInset(bv_, inset);
1337 // not quite sure if we want this...
1338 setCursorParUndo(bv_);
1341 beforeChange(bv_->text);
1342 if (!lout.empty()) {
1343 update(BufferView::SELECT);
1344 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1345 update(BufferView::SELECT);
1347 if (!bv_->text->cursor.par()->empty()) {
1348 bv_->text->cursorLeft(bv_);
1350 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1351 update(BufferView::SELECT);
1355 LyXTextClass const & tclass =
1356 buffer_->params.getLyXTextClass();
1357 bool hasLayout = tclass.hasLayout(lres);
1358 string lay = tclass.defaultLayoutName();
1360 if (hasLayout != false) {
1364 // layout not fount using default
1365 lay = tclass.defaultLayoutName();
1368 bv_->text->setLayout(lay);
1370 bv_->text->setParagraph(0, 0,
1372 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1377 update(BufferView::SELECT);
1380 bv_->text->insertInset(inset);
1381 update(BufferView::SELECT);
1388 void BufferView::Pimpl::updateInset(Inset * inset)
1390 if (!inset || !available())
1393 // first check for locking insets
1394 if (bv_->theLockingInset()) {
1395 if (bv_->theLockingInset() == inset) {
1396 if (bv_->text->updateInset(inset)) {
1401 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1402 if (bv_->text->updateInset(bv_->theLockingInset())) {
1410 // then check if the inset is a top_level inset (has no owner)
1411 // if yes do the update as always otherwise we have to update the
1412 // toplevel inset where this inset is inside
1413 Inset * tl_inset = inset;
1414 while (tl_inset->owner())
1415 tl_inset = tl_inset->owner();
1416 if (tl_inset == inset) {
1417 update(BufferView::UPDATE);
1418 if (bv_->text->updateInset(inset)) {
1419 update(BufferView::SELECT);
1422 } else if (static_cast<UpdatableInset *>(tl_inset)
1423 ->updateInsetInInset(bv_, inset))
1425 if (bv_->text->updateInset(tl_inset)) {