2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
11 #include "BufferView_pimpl.h"
12 #include "frontends/WorkArea.h"
13 #include "frontends/screen.h"
14 #include "frontends/LyXScreenFactory.h"
15 #include "frontends/WorkAreaFactory.h"
16 #include "frontends/Dialogs.h"
17 #include "frontends/Alert.h"
18 #include "frontends/FileDialog.h"
19 #include "frontends/mouse_state.h"
22 #include "paragraph.h"
23 #include "frontends/LyXView.h"
24 #include "commandtags.h"
27 #include "bufferview_funcs.h"
28 #include "TextCache.h"
29 #include "bufferlist.h"
32 // added for Dispatch functions
35 #include "FloatList.h"
37 #include "ParagraphParameters.h"
38 #include "undo_funcs.h"
39 #include "funcrequest.h"
40 #include "iterators.h"
43 #include "insets/insetbibitem.h"
44 #include "insets/insetbibtex.h"
45 #include "insets/insetcite.h"
46 #include "insets/insetert.h"
47 #include "insets/insetfloatlist.h"
48 #include "insets/insetgraphics.h"
49 #include "insets/insetinclude.h"
50 #include "insets/insetindex.h"
51 #include "insets/insetlatexaccent.h"
52 #include "insets/insetmarginal.h"
53 #include "insets/insetref.h"
54 #include "insets/insettext.h"
55 #include "insets/insettoc.h"
56 #include "insets/inseturl.h"
58 #include "mathed/formulabase.h"
60 #include "graphics/Previews.h"
62 #include "support/LAssert.h"
63 #include "support/lstrings.h"
64 #include "support/filetools.h"
66 #include <boost/bind.hpp>
67 #include <boost/signals/connection.hpp>
68 #include "BoostFormat.h"
84 extern BufferList bufferlist;
89 unsigned int const saved_positions_num = 20;
91 // All the below connection objects are needed because of a bug in some
92 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
93 // to these connections we avoid a segfault upon startup, and also at exit.
96 boost::signals::connection dispatchcon;
97 boost::signals::connection timecon;
98 boost::signals::connection doccon;
99 boost::signals::connection resizecon;
100 boost::signals::connection kpresscon;
101 boost::signals::connection selectioncon;
102 boost::signals::connection lostcon;
108 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
109 int xpos, int ypos, int width, int height)
110 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
111 using_xterm_cursor(false)
113 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
114 screen_.reset(LyXScreenFactory::create(workarea()));
117 doccon = workarea().scrollDocView
118 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
119 resizecon = workarea().workAreaResize
120 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
121 dispatchcon = workarea().dispatch
122 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
123 kpresscon = workarea().workAreaKeyPress
124 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
125 selectioncon = workarea().selectionRequested
126 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
127 lostcon = workarea().selectionLost
128 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
130 timecon = cursor_timeout.timeout
131 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
132 cursor_timeout.start();
133 saved_positions.resize(saved_positions_num);
137 WorkArea & BufferView::Pimpl::workarea() const
139 return *workarea_.get();
143 LyXScreen & BufferView::Pimpl::screen() const
145 return *screen_.get();
149 Painter & BufferView::Pimpl::painter() const
151 return workarea().getPainter();
155 void BufferView::Pimpl::buffer(Buffer * b)
157 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
160 buffer_->delUser(bv_);
162 // Put the old text into the TextCache, but
163 // only if the buffer is still loaded.
164 // Also set the owner of the test to 0
165 // bv_->text->owner(0);
166 textcache.add(buffer_, workarea().workWidth(), bv_->text);
167 if (lyxerr.debugging())
168 textcache.show(lyxerr, "BufferView::buffer");
173 // set current buffer
176 // if we're quitting lyx, don't bother updating stuff
180 // if we are closing the buffer, use the first buffer as current
182 buffer_ = bufferlist.first();
186 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
187 buffer_->addUser(bv_);
189 // If we don't have a text object for this, we make one
190 if (bv_->text == 0) {
191 resizeCurrentBuffer();
194 // FIXME: needed when ?
196 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
198 // Similarly, buffer-dependent dialogs should be updated or
199 // hidden. This should go here because some dialogs (eg ToC)
200 // require bv_->text.
201 owner_->getDialogs().updateBufferDependent(true);
203 lyxerr[Debug::INFO] << " No Buffer!" << endl;
204 owner_->getDialogs().hideBufferDependent();
206 // Also remove all remaining text's from the testcache.
207 // (there should not be any!) (if there is any it is a
209 if (lyxerr.debugging())
210 textcache.show(lyxerr, "buffer delete all");
216 owner_->updateMenubar();
217 owner_->updateToolbar();
218 owner_->updateLayoutChoice();
219 owner_->updateWindowTitle();
221 if (grfx::Previews::activated() && buffer_)
222 grfx::Previews::get().generateBufferPreviews(*buffer_);
226 bool BufferView::Pimpl::fitCursor()
230 if (bv_->theLockingInset()) {
231 bv_->theLockingInset()->fitInsetCursor(bv_);
234 ret = screen().fitCursor(bv_->text, bv_);
237 bv_->owner()->getDialogs().updateParagraph();
244 void BufferView::Pimpl::redoCurrentBuffer()
246 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
247 if (buffer_ && bv_->text) {
248 resizeCurrentBuffer();
250 owner_->updateLayoutChoice();
256 int BufferView::Pimpl::resizeCurrentBuffer()
258 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
261 Paragraph * selstartpar = 0;
262 Paragraph * selendpar = 0;
263 UpdatableInset * the_locking_inset = 0;
266 pos_type selstartpos = 0;
267 pos_type selendpos = 0;
268 bool selection = false;
269 bool mark_set = false;
273 owner_->message(_("Formatting document..."));
276 par = bv_->text->cursor.par();
277 pos = bv_->text->cursor.pos();
278 selstartpar = bv_->text->selection.start.par();
279 selstartpos = bv_->text->selection.start.pos();
280 selendpar = bv_->text->selection.end.par();
281 selendpos = bv_->text->selection.end.pos();
282 selection = bv_->text->selection.set();
283 mark_set = bv_->text->selection.mark();
284 the_locking_inset = bv_->theLockingInset();
285 buffer_->resizeInsets(bv_);
286 // I don't think the delete and new are necessary here we just could
287 // call only init! (Jug 20020419)
289 bv_->text = new LyXText(bv_);
290 bv_->text->init(bv_);
292 // See if we have a text in TextCache that fits
293 // the new buffer_ with the correct width.
294 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
296 if (lyxerr.debugging()) {
297 lyxerr << "Found a LyXText that fits:\n";
298 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
300 // Set the owner of the newly found text
301 // bv_->text->owner(bv_);
302 if (lyxerr.debugging())
303 textcache.show(lyxerr, "resizeCurrentBuffer");
305 buffer_->resizeInsets(bv_);
307 bv_->text = new LyXText(bv_);
308 bv_->text->init(bv_);
309 //buffer_->resizeInsets(bv_);
314 bv_->text->selection.set(true);
315 // At this point just to avoid the Delete-Empty-Paragraph-
316 // Mechanism when setting the cursor.
317 bv_->text->selection.mark(mark_set);
319 bv_->text->setCursor(bv_, selstartpar, selstartpos);
320 bv_->text->selection.cursor = bv_->text->cursor;
321 bv_->text->setCursor(bv_, selendpar, selendpos);
322 bv_->text->setSelection(bv_);
323 bv_->text->setCursor(bv_, par, pos);
325 bv_->text->setCursor(bv_, par, pos);
326 bv_->text->selection.cursor = bv_->text->cursor;
327 bv_->text->selection.set(false);
329 // remake the inset locking
330 bv_->theLockingInset(the_locking_inset);
333 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
344 void BufferView::Pimpl::repaint()
346 // Regenerate the screen.
347 screen().redraw(bv_->text, bv_);
351 void BufferView::Pimpl::updateScrollbar()
354 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
355 workarea().setScrollbarParams(0, 0, 0);
359 LyXText const & t = *bv_->text;
361 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
362 << t.first_y << ", default height " << defaultRowHeight() << endl;
364 workarea().setScrollbarParams(t.height, t.first_y, defaultRowHeight());
368 void BufferView::Pimpl::scrollDocView(int value)
370 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
375 screen().draw(bv_->text, bv_, value);
377 if (!lyxrc.cursor_follows_scrollbar)
380 LyXText * vbt = bv_->text;
382 int const height = defaultRowHeight();
383 int const first = static_cast<int>((bv_->text->first_y + height));
384 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
386 if (vbt->cursor.y() < first)
387 vbt->setCursorFromCoordinates(bv_, 0, first);
388 else if (vbt->cursor.y() > last)
389 vbt->setCursorFromCoordinates(bv_, 0, last);
393 void BufferView::Pimpl::scroll(int lines)
399 LyXText const * t = bv_->text;
400 int const line_height = defaultRowHeight();
402 // The new absolute coordinate
403 int new_first_y = t->first_y + lines * line_height;
405 // Restrict to a valid value
406 new_first_y = std::min(t->height - 4 * line_height, new_first_y);
407 new_first_y = std::max(0, new_first_y);
409 scrollDocView(new_first_y);
411 // Update the scrollbar.
412 workarea().setScrollbarParams(t->height, t->first_y, defaultRowHeight());
416 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
417 key_modifier::state state)
419 bv_->owner()->getLyXFunc().processKeySym(key, state);
423 void BufferView::Pimpl::selectionRequested()
430 LyXText * text = bv_->getLyXText();
432 if (text->selection.set() &&
433 (!bv_->text->xsel_cache.set() ||
434 text->selection.start != bv_->text->xsel_cache.start ||
435 text->selection.end != bv_->text->xsel_cache.end))
437 bv_->text->xsel_cache = text->selection;
438 sel = text->selectionAsString(bv_->buffer(), false);
439 } else if (!text->selection.set()) {
441 bv_->text->xsel_cache.set(false);
444 workarea().putClipboard(sel);
449 void BufferView::Pimpl::selectionLost()
454 bv_->getLyXText()->clearSelection();
456 bv_->text->xsel_cache.set(false);
461 void BufferView::Pimpl::workAreaResize()
463 static int work_area_width;
464 static int work_area_height;
466 bool const widthChange = workarea().workWidth() != work_area_width;
467 bool const heightChange = workarea().workHeight() != work_area_height;
469 // update from work area
470 work_area_width = workarea().workWidth();
471 work_area_height = workarea().workHeight();
475 // The visible LyXView need a resize
476 resizeCurrentBuffer();
478 // Remove all texts from the textcache
479 // This is not _really_ what we want to do. What
480 // we really want to do is to delete in textcache
481 // that does not have a BufferView with matching
482 // width, but as long as we have only one BufferView
483 // deleting all gives the same result.
484 if (lyxerr.debugging())
485 textcache.show(lyxerr, "Expose delete all");
487 // FIXME: this is already done in resizeCurrentBuffer() ??
488 buffer_->resizeInsets(bv_);
489 } else if (heightChange) {
490 // fitCursor() ensures we don't jump back
491 // to the start of the document on vertical
497 if (widthChange || heightChange) {
501 // always make sure that the scrollbar is sane.
503 owner_->updateLayoutChoice();
508 void BufferView::Pimpl::update()
510 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
511 LyXText::text_status st = bv_->text->status();
512 screen().update(bv_->text, bv_);
514 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
515 bv_->text->fullRebreak(bv_);
516 st = LyXText::NEED_MORE_REFRESH;
517 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
518 bv_->text->cursor.pos());
519 if (bv_->text->selection.set()) {
520 bv_->text->setCursor(bv_, bv_->text->selection.start,
521 bv_->text->selection.start.par(),
522 bv_->text->selection.start.pos());
523 bv_->text->setCursor(bv_, bv_->text->selection.end,
524 bv_->text->selection.end.par(),
525 bv_->text->selection.end.pos());
528 bv_->text->status(bv_, st);
529 screen().update(bv_->text, bv_);
531 // do this here instead of in the screen::update because of
533 bv_->text->status(bv_, LyXText::UNCHANGED);
539 // Values used when calling update:
541 // -2 - update, move sel_cursor if selection, fitcursor
542 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
543 // 0 - update, move sel_cursor if selection, fitcursor
544 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
545 // 3 - update, move sel_cursor if selection
548 // a simple redraw of the parts that need refresh
550 // move sel_cursor if selection -
551 // the text's sel_cursor is moved if there is selection is progress
554 // fitCursor() is called and the scrollbar updated
557 // the buffer is marked dirty.
566 // UPDATE_ONLY = UPDATE;
567 // UPDATE_SELECT = UPDATE | SELECT;
568 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
569 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
571 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
572 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
573 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
574 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
575 // update(3) -> update(1) -> update(1) -> update(SELECT)
577 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
579 owner_->updateLayoutChoice();
581 if (!text->selection.set() && (f & SELECT)) {
582 text->selection.cursor = text->cursor;
585 text->fullRebreak(bv_);
587 if (text->inset_owner) {
588 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
589 updateInset(text->inset_owner, false);
599 buffer_->markDirty();
604 // Callback for cursor timer
605 void BufferView::Pimpl::cursorToggle()
608 cursor_timeout.restart();
612 if (!bv_->theLockingInset()) {
613 screen().cursorToggle(bv_);
615 bv_->theLockingInset()->toggleInsetCursor(bv_);
618 cursor_timeout.restart();
622 bool BufferView::Pimpl::available() const
624 if (buffer_ && bv_->text)
630 Change const BufferView::Pimpl::getCurrentChange()
632 if (!bv_->buffer()->params.tracking_changes)
633 return Change(Change::UNCHANGED);
635 LyXText * t(bv_->getLyXText());
637 if (!t->selection.set())
638 return Change(Change::UNCHANGED);
640 LyXCursor const & cur(t->selection.start);
641 return cur.par()->lookupChangeFull(cur.pos());
645 void BufferView::Pimpl::beforeChange(LyXText * text)
648 text->clearSelection();
652 void BufferView::Pimpl::savePosition(unsigned int i)
654 if (i >= saved_positions_num)
656 saved_positions[i] = Position(buffer_->fileName(),
657 bv_->text->cursor.par()->id(),
658 bv_->text->cursor.pos());
662 str << boost::format(_("Saved bookmark %1$d")) % i;
664 str << _("Saved bookmark ") << i;
666 owner_->message(STRCONV(str.str()));
671 void BufferView::Pimpl::restorePosition(unsigned int i)
673 if (i >= saved_positions_num)
676 string const fname = saved_positions[i].filename;
678 beforeChange(bv_->text);
680 if (fname != buffer_->fileName()) {
681 Buffer * b = bufferlist.exists(fname) ?
682 bufferlist.getBuffer(fname) :
683 bufferlist.loadLyXFile(fname); // don't ask, just load it
684 if (b != 0) buffer(b);
687 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
691 bv_->text->setCursor(bv_, par,
692 min(par->size(), saved_positions[i].par_pos));
694 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
698 str << boost::format(_("Moved to bookmark %1$d")) % i;
700 str << _("Moved to bookmark ") << i;
702 owner_->message(STRCONV(str.str()));
707 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
709 if (i >= saved_positions_num)
712 return !saved_positions[i].filename.empty();
716 void BufferView::Pimpl::switchKeyMap()
718 if (!lyxrc.rtl_support)
721 LyXText * text = bv_->getLyXText();
722 if (text->real_current_font.isRightToLeft()
723 && !(bv_->theLockingInset()
724 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
726 if (owner_->getIntl().keymap == Intl::PRIMARY)
727 owner_->getIntl().KeyMapSec();
729 if (owner_->getIntl().keymap == Intl::SECONDARY)
730 owner_->getIntl().KeyMapPrim();
735 void BufferView::Pimpl::insetUnlock()
737 if (bv_->theLockingInset()) {
738 bv_->theLockingInset()->insetUnlock(bv_);
739 bv_->theLockingInset(0);
745 void BufferView::Pimpl::showCursor()
747 if (bv_->theLockingInset())
748 bv_->theLockingInset()->showInsetCursor(bv_);
750 screen().showCursor(bv_->text, bv_);
754 void BufferView::Pimpl::hideCursor()
756 if (!bv_->theLockingInset())
757 screen().hideCursor();
761 void BufferView::Pimpl::toggleSelection(bool b)
763 if (bv_->theLockingInset())
764 bv_->theLockingInset()->toggleSelection(bv_, b);
765 screen().toggleSelection(bv_->text, bv_, b);
769 void BufferView::Pimpl::toggleToggle()
771 screen().toggleToggle(bv_->text, bv_);
775 void BufferView::Pimpl::center()
777 LyXText * t = bv_->text;
780 int const half_height = workarea().workHeight() / 2;
783 if (t->cursor.y() > half_height) {
784 new_y = t->cursor.y() - half_height;
787 // FIXME: can we do this w/o calling screen directly ?
788 // This updates first_y but means the fitCursor() call
789 // from the update(FITCUR) doesn't realise that we might
790 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
791 // the scrollbar to be updated as it should, so we have
792 // to do it manually. Any operation that does a center()
793 // and also might have moved first_y must make sure to call
794 // updateScrollbar() currently. Never mind that this is a
795 // pretty obfuscated way of updating t->first_y
796 screen().draw(t, bv_, new_y);
798 update(t, BufferView::SELECT | BufferView::FITCUR);
802 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
804 workarea().putClipboard(stuff);
809 * Dispatch functions for actions which can be valid for BufferView->text
810 * and/or InsetText->text!!!
814 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
817 LyXCursor cursor = bv_->getLyXText()->cursor;
818 Buffer::inset_iterator it =
819 find_if(Buffer::inset_iterator(
820 cursor.par(), cursor.pos()),
821 buffer_->inset_iterator_end(),
822 lyx::compare_memfun(&Inset::lyxCode, code));
823 return it != buffer_->inset_iterator_end() ? (*it) : 0;
825 // Ok, this is a little bit too brute force but it
826 // should work for now. Better infrastructure is comming. (Lgb)
828 Buffer * b = bv_->buffer();
829 LyXCursor cursor = bv_->getLyXText()->cursor;
831 Buffer::inset_iterator beg = b->inset_iterator_begin();
832 Buffer::inset_iterator end = b->inset_iterator_end();
834 bool cursor_par_seen = false;
836 for (; beg != end; ++beg) {
837 if (beg.getPar() == cursor.par()) {
838 cursor_par_seen = true;
840 if (cursor_par_seen) {
841 if (beg.getPar() == cursor.par()
842 && beg.getPos() >= cursor.pos()) {
844 } else if (beg.getPar() != cursor.par()) {
851 // Now find the first inset that matches code.
852 for (; beg != end; ++beg) {
853 if (beg->lyxCode() == code) {
863 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
865 string filename = filen;
867 if (filename.empty()) {
868 // Launch a file browser
869 string initpath = lyxrc.document_path;
872 string const trypath = owner_->buffer()->filePath();
873 // If directory is writeable, use this as default.
874 if (IsDirWriteable(trypath))
878 FileDialog fileDlg(_("Select LyX document to insert"),
880 make_pair(string(_("Documents|#o#O")),
881 string(lyxrc.document_path)),
882 make_pair(string(_("Examples|#E#e")),
883 string(AddPath(system_lyxdir, "examples"))));
885 FileDialog::Result result =
886 fileDlg.open(initpath,
887 _("*.lyx| LyX Documents (*.lyx)"));
889 if (result.first == FileDialog::Later)
892 filename = result.second;
894 // check selected filename
895 if (filename.empty()) {
896 owner_->message(_("Canceled."));
901 // get absolute path of file and add ".lyx" to the filename if
903 filename = FileSearch(string(), filename, "lyx");
905 string const disp_fn(MakeDisplayPath(filename));
909 s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
911 s1 << _("Inserting document ") << disp_fn << _("...");
913 owner_->message(STRCONV(s1.str()));
914 bool const res = bv_->insertLyXFile(filename);
918 str << boost::format(_("Document %1$s inserted.")) % disp_fn;
920 str << _("Document ") << disp_fn << _(" inserted.");
922 owner_->message(STRCONV(str.str()));
926 str << boost::format(_("Could not insert document %1$s")) % disp_fn;
928 str << _("Could not insert document ") << disp_fn;
930 owner_->message(STRCONV(str.str()));
935 void BufferView::Pimpl::trackChanges()
937 Buffer * buf(bv_->buffer());
938 bool const tracking(buf->params.tracking_changes);
941 ParIterator const end = buf->par_iterator_end();
942 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
943 (*it)->trackChanges();
945 buf->params.tracking_changes = true;
947 // we cannot allow undos beyond the freeze point
948 buf->undostack.clear();
950 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
951 bv_->text->setCursor(bv_, &(*buf->paragraphs.begin()), 0);
952 #warning changes FIXME
953 //moveCursorUpdate(false);
955 bool found = lyxfind::findNextChange(bv_);
957 owner_->getDialogs().showMergeChanges();
961 ParIterator const end = buf->par_iterator_end();
962 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
963 (*it)->untrackChanges();
965 buf->params.tracking_changes = false;
968 buf->redostack.clear();
972 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
974 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
975 << " action[" << ev.action << ']'
976 << " arg[" << ev.argument << ']'
977 << " x[" << ev.x << ']'
978 << " y[" << ev.y << ']'
979 << " button[" << ev.button() << ']'
982 // e.g. Qt mouse press when no buffer
986 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
990 case LFUN_SCROLL_INSET:
991 // this is not handled here as this function is only active
992 // if we have a locking_inset and that one is (or contains)
996 case LFUN_INSET_GRAPHICS:
998 Inset * new_inset = new InsetGraphics;
999 if (!insertInset(new_inset)) {
1002 // this is need because you don't use a inset->Edit()
1003 updateInset(new_inset, true);
1004 new_inset->edit(bv_);
1009 case LFUN_LAYOUT_COPY:
1010 bv_->copyEnvironment();
1013 case LFUN_LAYOUT_PASTE:
1014 bv_->pasteEnvironment();
1018 case LFUN_DEPTH_MIN:
1019 changeDepth(bv_, bv_->getLyXText(), -1);
1022 case LFUN_DEPTH_PLUS:
1023 changeDepth(bv_, bv_->getLyXText(), 1);
1027 owner_->getDialogs().setUserFreeFont();
1030 case LFUN_FILE_INSERT:
1031 MenuInsertLyXFile(ev.argument);
1034 case LFUN_FILE_INSERT_ASCII_PARA:
1035 InsertAsciiFile(bv_, ev.argument, true);
1038 case LFUN_FILE_INSERT_ASCII:
1039 InsertAsciiFile(bv_, ev.argument, false);
1043 lang(bv_, ev.argument);
1045 owner_->view_state_changed();
1050 owner_->view_state_changed();
1055 owner_->view_state_changed();
1060 owner_->view_state_changed();
1065 owner_->view_state_changed();
1070 owner_->view_state_changed();
1075 owner_->view_state_changed();
1080 owner_->view_state_changed();
1083 case LFUN_UNDERLINE:
1085 owner_->view_state_changed();
1088 case LFUN_FONT_SIZE:
1089 fontSize(bv_, ev.argument);
1090 owner_->view_state_changed();
1093 case LFUN_FONT_STATE:
1094 owner_->getLyXFunc().setMessage(currentState(bv_));
1097 case LFUN_INSERT_LABEL:
1098 MenuInsertLabel(bv_, ev.argument);
1101 case LFUN_BOOKMARK_SAVE:
1102 savePosition(strToUnsignedInt(ev.argument));
1105 case LFUN_BOOKMARK_GOTO:
1106 restorePosition(strToUnsignedInt(ev.argument));
1111 string label = ev.argument;
1112 if (label.empty()) {
1114 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1116 label = inset->getContents();
1121 if (!label.empty()) {
1122 //bv_->savePosition(0);
1123 if (!bv_->gotoLabel(label))
1124 Alert::alert(_("Error"),
1125 _("Couldn't find this label"),
1126 _("in current document."));
1131 // --- accented characters ---------------------------
1134 case LFUN_CIRCUMFLEX:
1144 case LFUN_SPECIAL_CARON:
1147 case LFUN_HUNG_UMLAUT:
1150 if (ev.argument.empty()) {
1152 owner_->getLyXFunc().handleKeyFunc(ev.action);
1154 owner_->getLyXFunc().handleKeyFunc(ev.action);
1155 owner_->getIntl().getTransManager()
1156 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1157 update(bv_->getLyXText(),
1159 | BufferView::FITCUR
1160 | BufferView::CHANGE);
1164 case LFUN_MATH_MACRO:
1165 case LFUN_MATH_DELIM:
1166 case LFUN_INSERT_MATRIX:
1167 case LFUN_INSERT_MATH:
1168 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1169 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1170 case LFUN_MATH_MODE: // Open or create an inlined math inset
1171 case LFUN_GREEK: // Insert a single greek letter
1172 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1175 case LFUN_BIBITEM_APPLY: {
1176 InsetCommandParams params;
1177 InsetCommandMailer::string2params(ev.argument, params);
1180 owner_->getDialogs().getOpenInset("bibitem");
1181 InsetBibitem * inset = 0;
1183 inset = dynamic_cast<InsetBibitem *>(base);
1187 if (params.getContents() !=
1188 inset->params().getContents()) {
1189 bv_->ChangeCitationsIfUnique(
1190 inset->params().getContents(),
1191 params.getContents());
1193 inset->setParams(params);
1195 inset = new InsetBibitem(params);
1196 if (!insertInset(inset)) {
1201 updateInset(inset, true);
1203 // We need to do a redraw because the maximum
1204 // InsetBibitem width could have changed
1205 #warning please check you mean repaint() not update(),
1206 #warning and whether the repaint() is needed at all
1212 case LFUN_BIBTEX_APPLY: {
1213 InsetCommandParams params;
1214 InsetCommandMailer::string2params(ev.argument, params);
1217 owner_->getDialogs().getOpenInset("bibtex");
1218 InsetBibtex * inset = 0;
1220 inset = dynamic_cast<InsetBibtex *>(base);
1224 if (params.getContents() !=
1225 inset->params().getContents()) {
1226 bv_->ChangeCitationsIfUnique(
1227 inset->params().getContents(),
1228 params.getContents());
1230 inset->setParams(params);
1232 inset = new InsetBibtex(params);
1233 if (!insertInset(inset)) {
1238 updateInset(inset, true);
1242 case LFUN_CITATION_APPLY: {
1243 InsetCommandParams params;
1244 InsetCommandMailer::string2params(ev.argument, params);
1247 owner_->getDialogs().getOpenInset("citation");
1248 InsetCitation * inset = 0;
1250 inset = dynamic_cast<InsetCitation *>(base);
1254 inset->setParams(params);
1256 inset = new InsetCitation(params);
1257 if (!insertInset(inset)) {
1261 inset->setLoadingBuffer(bv_->buffer(), false);
1264 updateInset(inset, true);
1268 case LFUN_ERT_APPLY: {
1269 InsetBase * base = owner_->getDialogs().getOpenInset("ert");
1270 InsetERT * inset = 0;
1272 inset = dynamic_cast<InsetERT *>(base);
1276 inset = new InsetERT(bv_->buffer()->params);
1277 if (!insertInset(inset)) {
1283 InsetERT::ERTStatus status;
1284 InsetERTMailer::string2params(ev.argument, status);
1286 inset->status(bv_, status);
1287 updateInset(inset, true);
1291 case LFUN_INDEX_APPLY: {
1292 InsetCommandParams params;
1293 InsetCommandMailer::string2params(ev.argument, params);
1295 InsetBase * base = owner_->getDialogs().getOpenInset("index");
1296 InsetIndex * inset = 0;
1298 inset = dynamic_cast<InsetIndex *>(base);
1302 inset->setParams(params);
1304 InsetIndex * inset = new InsetIndex(params);
1305 if (!insertInset(inset)) {
1310 updateInset(inset, true);
1314 case LFUN_REF_APPLY: {
1315 dispatch_result result = UNDISPATCHED;
1316 InsetBase * base = owner_->getDialogs().getOpenInset("ref");
1319 // This works both for 'original' and 'mathed' insets.
1320 result = base->localDispatch(ev);
1322 InsetCommandParams params;
1323 InsetCommandMailer::string2params(ev.argument, params);
1325 inset = new InsetRef(params, *buffer_);
1326 if (!insertInset(inset)) {
1328 result = UNDISPATCHED;
1330 updateInset(inset, true);
1335 case LFUN_TOC_APPLY: {
1336 InsetCommandParams params;
1337 InsetCommandMailer::string2params(ev.argument, params);
1339 InsetBase * base = owner_->getDialogs().getOpenInset("toc");
1340 InsetTOC * inset = 0;
1342 InsetTOC * inset = dynamic_cast<InsetTOC *>(base);
1346 inset->setParams(params);
1348 InsetTOC * inset = new InsetTOC(params);
1349 if (!insertInset(inset)) {
1354 updateInset(inset, true);
1358 case LFUN_URL_APPLY: {
1359 InsetCommandParams params;
1360 InsetCommandMailer::string2params(ev.argument, params);
1362 InsetBase * base = owner_->getDialogs().getOpenInset("url");
1363 InsetUrl * inset = 0;
1365 inset = dynamic_cast<InsetUrl *>(base);
1369 inset->setParams(params);
1371 InsetUrl * inset = new InsetUrl(params);
1372 if (!insertInset(inset)) {
1377 updateInset(inset, true);
1381 case LFUN_CHILD_INSERT:
1383 InsetInclude::Params p;
1384 if (!ev.argument.empty())
1385 p.cparams.setFromString(ev.argument);
1386 p.masterFilename_ = buffer_->fileName();
1388 InsetInclude * inset = new InsetInclude(p);
1389 if (!insertInset(inset))
1392 updateInset(inset, true);
1393 bv_->owner()->getDialogs().showInclude(inset);
1398 case LFUN_FLOAT_LIST:
1399 if (tclass.floats().typeExist(ev.argument)) {
1400 Inset * inset = new InsetFloatList(ev.argument);
1401 if (!insertInset(inset, tclass.defaultLayoutName()))
1404 lyxerr << "Non-existent float type: "
1405 << ev.argument << endl;
1409 case LFUN_THESAURUS_ENTRY:
1411 string arg = ev.argument;
1414 arg = bv_->getLyXText()->selectionAsString(buffer_,
1418 if (arg.size() > 100 || arg.empty()) {
1419 // Get word or selection
1420 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1421 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1422 // FIXME: where is getLyXText()->unselect(bv_) ?
1426 bv_->owner()->getDialogs().showThesaurus(arg);
1430 case LFUN_TRACK_CHANGES:
1434 case LFUN_MERGE_CHANGES:
1435 owner_->getDialogs().showMergeChanges();
1438 case LFUN_ACCEPT_ALL_CHANGES: {
1439 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1440 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1441 #warning FIXME changes
1442 //moveCursorUpdate(false);
1444 while (lyxfind::findNextChange(bv_)) {
1445 bv_->getLyXText()->acceptChange(bv_);
1448 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1452 case LFUN_REJECT_ALL_CHANGES: {
1453 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1454 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1455 #warning FIXME changes
1456 //moveCursorUpdate(false);
1458 while (lyxfind::findNextChange(bv_)) {
1459 bv_->getLyXText()->rejectChange(bv_);
1462 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1466 case LFUN_ACCEPT_CHANGE: {
1467 bv_->getLyXText()->acceptChange(bv_);
1469 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1473 case LFUN_REJECT_CHANGE: {
1474 bv_->getLyXText()->rejectChange(bv_);
1476 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1480 case LFUN_UNKNOWN_ACTION:
1481 ev.errorMessage(N_("Unknown function!"));
1485 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1492 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1494 // if we are in a locking inset we should try to insert the
1495 // inset there otherwise this is a illegal function now
1496 if (bv_->theLockingInset()) {
1497 if (bv_->theLockingInset()->insetAllowed(inset))
1498 return bv_->theLockingInset()->insertInset(bv_, inset);
1502 // not quite sure if we want this...
1503 setCursorParUndo(bv_);
1506 beforeChange(bv_->text);
1507 if (!lout.empty()) {
1508 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1509 bv_->text->breakParagraph(bv_);
1510 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1512 if (!bv_->text->cursor.par()->empty()) {
1513 bv_->text->cursorLeft(bv_);
1515 bv_->text->breakParagraph(bv_);
1516 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1520 LyXTextClass const & tclass =
1521 buffer_->params.getLyXTextClass();
1522 bool hasLayout = tclass.hasLayout(lres);
1523 string lay = tclass.defaultLayoutName();
1525 if (hasLayout != false) {
1529 // layout not fount using default
1530 lay = tclass.defaultLayoutName();
1533 bv_->text->setLayout(bv_, lay);
1535 bv_->text->setParagraph(bv_, 0, 0,
1537 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1542 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1545 bv_->text->insertInset(bv_, inset);
1546 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1553 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1555 if (!inset || !available())
1558 // first check for locking insets
1559 if (bv_->theLockingInset()) {
1560 if (bv_->theLockingInset() == inset) {
1561 if (bv_->text->updateInset(bv_, inset)) {
1564 buffer_->markDirty();
1569 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1570 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1573 buffer_->markDirty();
1581 // then check if the inset is a top_level inset (has no owner)
1582 // if yes do the update as always otherwise we have to update the
1583 // toplevel inset where this inset is inside
1584 Inset * tl_inset = inset;
1585 while (tl_inset->owner())
1586 tl_inset = tl_inset->owner();
1588 if (tl_inset == inset) {
1589 update(bv_->text, BufferView::UPDATE);
1590 if (bv_->text->updateInset(bv_, inset)) {
1594 | BufferView::FITCUR
1595 | BufferView::CHANGE);
1597 update(bv_->text, SELECT);
1601 } else if (static_cast<UpdatableInset *>(tl_inset)
1602 ->updateInsetInInset(bv_, inset))
1604 if (bv_->text->updateInset(bv_, tl_inset)) {