2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
12 #pragma implementation
15 #include "BufferView_pimpl.h"
16 #include "frontends/WorkArea.h"
17 #include "frontends/screen.h"
18 #include "frontends/LyXScreenFactory.h"
19 #include "frontends/WorkAreaFactory.h"
20 #include "frontends/Dialogs.h"
21 #include "frontends/Alert.h"
22 #include "frontends/FileDialog.h"
23 #include "frontends/mouse_state.h"
26 #include "paragraph.h"
27 #include "frontends/LyXView.h"
28 #include "commandtags.h"
31 #include "bufferview_funcs.h"
32 #include "TextCache.h"
33 #include "bufferlist.h"
36 // added for Dispatch functions
39 #include "FloatList.h"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
43 #include "funcrequest.h"
44 #include "iterators.h"
47 #include "insets/insetbib.h"
48 #include "insets/insettext.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insettoc.h"
51 #include "insets/insetindex.h"
52 #include "insets/insetref.h"
53 #include "insets/insetinclude.h"
54 #include "insets/insetcite.h"
55 #include "insets/insetgraphics.h"
56 #include "insets/insetmarginal.h"
57 #include "insets/insetfloatlist.h"
59 #include "mathed/formulabase.h"
61 #include "graphics/Previews.h"
63 #include "support/LAssert.h"
64 #include "support/lstrings.h"
65 #include "support/filetools.h"
67 #include <boost/bind.hpp>
68 #include <boost/signals/connection.hpp>
69 #include "BoostFormat.h"
85 extern BufferList bufferlist;
90 unsigned int const saved_positions_num = 20;
92 // All the below connection objects are needed because of a bug in some
93 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
94 // to these connections we avoid a segfault upon startup, and also at exit.
97 boost::signals::connection dispatchcon;
98 boost::signals::connection timecon;
99 boost::signals::connection doccon;
100 boost::signals::connection resizecon;
101 boost::signals::connection kpresscon;
102 boost::signals::connection selectioncon;
103 boost::signals::connection lostcon;
109 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
110 int xpos, int ypos, int width, int height)
111 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
112 using_xterm_cursor(false)
114 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
115 screen_.reset(LyXScreenFactory::create(workarea()));
118 doccon = workarea().scrollDocView
119 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
120 resizecon = workarea().workAreaResize
121 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
122 dispatchcon = workarea().dispatch
123 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
124 kpresscon = workarea().workAreaKeyPress
125 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
126 selectioncon = workarea().selectionRequested
127 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
128 lostcon = workarea().selectionLost
129 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
131 timecon = cursor_timeout.timeout
132 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
133 cursor_timeout.start();
134 saved_positions.resize(saved_positions_num);
138 WorkArea & BufferView::Pimpl::workarea() const
140 return *workarea_.get();
144 LyXScreen & BufferView::Pimpl::screen() const
146 return *screen_.get();
150 Painter & BufferView::Pimpl::painter() const
152 return workarea().getPainter();
156 void BufferView::Pimpl::buffer(Buffer * b)
158 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
161 buffer_->delUser(bv_);
163 // Put the old text into the TextCache, but
164 // only if the buffer is still loaded.
165 // Also set the owner of the test to 0
166 // bv_->text->owner(0);
167 textcache.add(buffer_, workarea().workWidth(), bv_->text);
168 if (lyxerr.debugging())
169 textcache.show(lyxerr, "BufferView::buffer");
174 // set current buffer
177 if (bufferlist.getState() == BufferList::CLOSING) return;
179 // if we are closing the buffer, use the first buffer as current
181 buffer_ = bufferlist.first();
185 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
186 buffer_->addUser(bv_);
188 // If we don't have a text object for this, we make one
189 if (bv_->text == 0) {
190 resizeCurrentBuffer();
193 // FIXME: needed when ?
195 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
197 // Similarly, buffer-dependent dialogs should be updated or
198 // hidden. This should go here because some dialogs (eg ToC)
199 // require bv_->text.
200 owner_->getDialogs().updateBufferDependent(true);
202 lyxerr[Debug::INFO] << " No Buffer!" << endl;
203 owner_->getDialogs().hideBufferDependent();
205 // Also remove all remaining text's from the testcache.
206 // (there should not be any!) (if there is any it is a
208 if (lyxerr.debugging())
209 textcache.show(lyxerr, "buffer delete all");
215 owner_->updateMenubar();
216 owner_->updateToolbar();
217 owner_->updateLayoutChoice();
218 owner_->updateWindowTitle();
220 if (grfx::Previews::activated() && buffer_)
221 grfx::Previews::get().generateBufferPreviews(*buffer_);
225 bool BufferView::Pimpl::fitCursor()
229 if (bv_->theLockingInset()) {
230 bv_->theLockingInset()->fitInsetCursor(bv_);
233 ret = screen().fitCursor(bv_->text, bv_);
236 bv_->owner()->getDialogs().updateParagraph();
243 void BufferView::Pimpl::redoCurrentBuffer()
245 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
246 if (buffer_ && bv_->text) {
247 resizeCurrentBuffer();
249 owner_->updateLayoutChoice();
255 int BufferView::Pimpl::resizeCurrentBuffer()
257 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
260 Paragraph * selstartpar = 0;
261 Paragraph * selendpar = 0;
262 UpdatableInset * the_locking_inset = 0;
265 pos_type selstartpos = 0;
266 pos_type selendpos = 0;
267 bool selection = false;
268 bool mark_set = false;
270 owner_->prohibitInput();
272 owner_->message(_("Formatting document..."));
275 par = bv_->text->cursor.par();
276 pos = bv_->text->cursor.pos();
277 selstartpar = bv_->text->selection.start.par();
278 selstartpos = bv_->text->selection.start.pos();
279 selendpar = bv_->text->selection.end.par();
280 selendpos = bv_->text->selection.end.pos();
281 selection = bv_->text->selection.set();
282 mark_set = bv_->text->selection.mark();
283 the_locking_inset = bv_->theLockingInset();
284 buffer_->resizeInsets(bv_);
285 // I don't think the delete and new are necessary here we just could
286 // call only init! (Jug 20020419)
288 bv_->text = new LyXText(bv_);
289 bv_->text->init(bv_);
291 // See if we have a text in TextCache that fits
292 // the new buffer_ with the correct width.
293 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
295 if (lyxerr.debugging()) {
296 lyxerr << "Found a LyXText that fits:\n";
297 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
299 // Set the owner of the newly found text
300 // bv_->text->owner(bv_);
301 if (lyxerr.debugging())
302 textcache.show(lyxerr, "resizeCurrentBuffer");
304 buffer_->resizeInsets(bv_);
306 bv_->text = new LyXText(bv_);
307 bv_->text->init(bv_);
308 //buffer_->resizeInsets(bv_);
313 bv_->text->selection.set(true);
314 // At this point just to avoid the Delete-Empty-Paragraph-
315 // Mechanism when setting the cursor.
316 bv_->text->selection.mark(mark_set);
318 bv_->text->setCursor(bv_, selstartpar, selstartpos);
319 bv_->text->selection.cursor = bv_->text->cursor;
320 bv_->text->setCursor(bv_, selendpar, selendpos);
321 bv_->text->setSelection(bv_);
322 bv_->text->setCursor(bv_, par, pos);
324 bv_->text->setCursor(bv_, par, pos);
325 bv_->text->selection.cursor = bv_->text->cursor;
326 bv_->text->selection.set(false);
328 // remake the inset locking
329 bv_->theLockingInset(the_locking_inset);
332 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
335 owner_->allowInput();
343 void BufferView::Pimpl::repaint()
345 // Regenerate the screen.
346 screen().redraw(bv_->text, bv_);
350 void BufferView::Pimpl::updateScrollbar()
353 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
354 workarea().setScrollbarParams(0, 0, 0);
358 LyXText const & t = *bv_->text;
360 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
361 << t.first_y << ", default height " << t.defaultHeight() << endl;
363 workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
367 void BufferView::Pimpl::scrollDocView(int value)
369 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
374 screen().draw(bv_->text, bv_, value);
376 if (!lyxrc.cursor_follows_scrollbar)
379 LyXText * vbt = bv_->text;
381 int const height = vbt->defaultHeight();
382 int const first = static_cast<int>((bv_->text->first_y + height));
383 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
385 if (vbt->cursor.y() < first)
386 vbt->setCursorFromCoordinates(bv_, 0, first);
387 else if (vbt->cursor.y() > last)
388 vbt->setCursorFromCoordinates(bv_, 0, last);
392 void BufferView::Pimpl::scroll(int lines)
398 LyXText const * t = bv_->text;
399 int const line_height = t->defaultHeight();
401 // The new absolute coordinate
402 int new_first_y = t->first_y + lines * line_height;
404 // Restrict to a valid value
405 new_first_y = std::min(t->height - 4 * line_height, new_first_y);
406 new_first_y = std::max(0, new_first_y);
408 scrollDocView(new_first_y);
410 // Update the scrollbar.
411 workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
415 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
416 key_modifier::state state)
418 bv_->owner()->getLyXFunc().processKeySym(key, state);
422 void BufferView::Pimpl::selectionRequested()
429 LyXText * text = bv_->getLyXText();
431 if (text->selection.set() &&
432 (!bv_->text->xsel_cache.set() ||
433 text->selection.start != bv_->text->xsel_cache.start ||
434 text->selection.end != bv_->text->xsel_cache.end))
436 bv_->text->xsel_cache = text->selection;
437 sel = text->selectionAsString(bv_->buffer(), false);
438 } else if (!text->selection.set()) {
440 bv_->text->xsel_cache.set(false);
443 workarea().putClipboard(sel);
448 void BufferView::Pimpl::selectionLost()
453 bv_->getLyXText()->clearSelection();
455 bv_->text->xsel_cache.set(false);
460 void BufferView::Pimpl::workAreaResize()
462 static int work_area_width;
463 static int work_area_height;
465 bool const widthChange = workarea().workWidth() != work_area_width;
466 bool const heightChange = workarea().workHeight() != work_area_height;
468 // update from work area
469 work_area_width = workarea().workWidth();
470 work_area_height = workarea().workHeight();
474 // The visible LyXView need a resize
475 resizeCurrentBuffer();
477 // Remove all texts from the textcache
478 // This is not _really_ what we want to do. What
479 // we really want to do is to delete in textcache
480 // that does not have a BufferView with matching
481 // width, but as long as we have only one BufferView
482 // deleting all gives the same result.
483 if (lyxerr.debugging())
484 textcache.show(lyxerr, "Expose delete all");
486 // FIXME: this is already done in resizeCurrentBuffer() ??
487 buffer_->resizeInsets(bv_);
488 } else if (heightChange) {
489 // fitCursor() ensures we don't jump back
490 // to the start of the document on vertical
496 if (widthChange || heightChange) {
500 // always make sure that the scrollbar is sane.
502 owner_->updateLayoutChoice();
507 void BufferView::Pimpl::update()
509 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
510 LyXText::text_status st = bv_->text->status();
511 screen().update(bv_->text, bv_);
513 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
514 bv_->text->fullRebreak(bv_);
515 st = LyXText::NEED_MORE_REFRESH;
516 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
517 bv_->text->cursor.pos());
518 if (bv_->text->selection.set()) {
519 bv_->text->setCursor(bv_, bv_->text->selection.start,
520 bv_->text->selection.start.par(),
521 bv_->text->selection.start.pos());
522 bv_->text->setCursor(bv_, bv_->text->selection.end,
523 bv_->text->selection.end.par(),
524 bv_->text->selection.end.pos());
527 bv_->text->status(bv_, st);
528 screen().update(bv_->text, bv_);
530 // do this here instead of in the screen::update because of
532 bv_->text->status(bv_, LyXText::UNCHANGED);
538 // Values used when calling update:
540 // -2 - update, move sel_cursor if selection, fitcursor
541 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
542 // 0 - update, move sel_cursor if selection, fitcursor
543 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
544 // 3 - update, move sel_cursor if selection
547 // a simple redraw of the parts that need refresh
549 // move sel_cursor if selection -
550 // the text's sel_cursor is moved if there is selection is progress
553 // fitCursor() is called and the scrollbar updated
556 // the buffer is marked dirty.
565 // UPDATE_ONLY = UPDATE;
566 // UPDATE_SELECT = UPDATE | SELECT;
567 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
568 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
570 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
571 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
572 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
573 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
574 // update(3) -> update(1) -> update(1) -> update(SELECT)
576 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
578 owner_->updateLayoutChoice();
580 if (!text->selection.set() && (f & SELECT)) {
581 text->selection.cursor = text->cursor;
584 text->fullRebreak(bv_);
586 if (text->inset_owner) {
587 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
588 updateInset(text->inset_owner, false);
598 buffer_->markDirty();
603 // Callback for cursor timer
604 void BufferView::Pimpl::cursorToggle()
607 cursor_timeout.restart();
611 if (!bv_->theLockingInset()) {
612 screen().cursorToggle(bv_);
614 bv_->theLockingInset()->toggleInsetCursor(bv_);
617 cursor_timeout.restart();
621 bool BufferView::Pimpl::available() const
623 if (buffer_ && bv_->text)
629 Change const BufferView::Pimpl::getCurrentChange()
631 if (!bv_->buffer()->params.tracking_changes)
632 return Change(Change::UNCHANGED);
634 LyXText * t(bv_->getLyXText());
636 if (!t->selection.set())
637 return Change(Change::UNCHANGED);
639 LyXCursor const & cur(t->selection.start);
640 return cur.par()->lookupChangeFull(cur.pos());
644 void BufferView::Pimpl::beforeChange(LyXText * text)
647 text->clearSelection();
651 void BufferView::Pimpl::savePosition(unsigned int i)
653 if (i >= saved_positions_num)
655 saved_positions[i] = Position(buffer_->fileName(),
656 bv_->text->cursor.par()->id(),
657 bv_->text->cursor.pos());
661 str << boost::format(_("Saved bookmark %1$d")) % i;
663 str << _("Saved bookmark ") << i;
665 owner_->message(STRCONV(str.str()));
670 void BufferView::Pimpl::restorePosition(unsigned int i)
672 if (i >= saved_positions_num)
675 string const fname = saved_positions[i].filename;
677 beforeChange(bv_->text);
679 if (fname != buffer_->fileName()) {
680 Buffer * b = bufferlist.exists(fname) ?
681 bufferlist.getBuffer(fname) :
682 bufferlist.loadLyXFile(fname); // don't ask, just load it
683 if (b != 0) buffer(b);
686 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
690 bv_->text->setCursor(bv_, par,
691 min(par->size(), saved_positions[i].par_pos));
693 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
697 str << boost::format(_("Moved to bookmark %1$d")) % i;
699 str << _("Moved to bookmark ") << i;
701 owner_->message(STRCONV(str.str()));
706 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
708 if (i >= saved_positions_num)
711 return !saved_positions[i].filename.empty();
715 void BufferView::Pimpl::switchKeyMap()
717 if (!lyxrc.rtl_support)
720 LyXText * text = bv_->getLyXText();
721 if (text->real_current_font.isRightToLeft()
722 && !(bv_->theLockingInset()
723 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
725 if (owner_->getIntl().keymap == Intl::PRIMARY)
726 owner_->getIntl().KeyMapSec();
728 if (owner_->getIntl().keymap == Intl::SECONDARY)
729 owner_->getIntl().KeyMapPrim();
734 void BufferView::Pimpl::insetUnlock()
736 if (bv_->theLockingInset()) {
737 bv_->theLockingInset()->insetUnlock(bv_);
738 bv_->theLockingInset(0);
744 void BufferView::Pimpl::showCursor()
746 if (bv_->theLockingInset())
747 bv_->theLockingInset()->showInsetCursor(bv_);
749 screen().showCursor(bv_->text, bv_);
753 void BufferView::Pimpl::hideCursor()
755 if (!bv_->theLockingInset())
756 screen().hideCursor();
760 void BufferView::Pimpl::toggleSelection(bool b)
762 if (bv_->theLockingInset())
763 bv_->theLockingInset()->toggleSelection(bv_, b);
764 screen().toggleSelection(bv_->text, bv_, b);
768 void BufferView::Pimpl::toggleToggle()
770 screen().toggleToggle(bv_->text, bv_);
774 void BufferView::Pimpl::center()
776 LyXText * t = bv_->text;
779 int const half_height = workarea().workHeight() / 2;
782 if (t->cursor.y() > half_height) {
783 new_y = t->cursor.y() - half_height;
786 // FIXME: can we do this w/o calling screen directly ?
787 // This updates first_y but means the fitCursor() call
788 // from the update(FITCUR) doesn't realise that we might
789 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
790 // the scrollbar to be updated as it should, so we have
791 // to do it manually. Any operation that does a center()
792 // and also might have moved first_y must make sure to call
793 // updateScrollbar() currently. Never mind that this is a
794 // pretty obfuscated way of updating t->first_y
795 screen().draw(t, bv_, new_y);
797 update(t, BufferView::SELECT | BufferView::FITCUR);
801 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
803 workarea().putClipboard(stuff);
808 * Dispatch functions for actions which can be valid for BufferView->text
809 * and/or InsetText->text!!!
813 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
816 LyXCursor cursor = bv_->getLyXText()->cursor;
817 Buffer::inset_iterator it =
818 find_if(Buffer::inset_iterator(
819 cursor.par(), cursor.pos()),
820 buffer_->inset_iterator_end(),
821 lyx::compare_memfun(&Inset::lyxCode, code));
822 return it != buffer_->inset_iterator_end() ? (*it) : 0;
824 // Ok, this is a little bit too brute force but it
825 // should work for now. Better infrastructure is comming. (Lgb)
827 Buffer * b = bv_->buffer();
828 LyXCursor cursor = bv_->getLyXText()->cursor;
830 Buffer::inset_iterator beg = b->inset_iterator_begin();
831 Buffer::inset_iterator end = b->inset_iterator_end();
833 bool cursor_par_seen = false;
835 for (; beg != end; ++beg) {
836 if (beg.getPar() == cursor.par()) {
837 cursor_par_seen = true;
839 if (cursor_par_seen) {
840 if (beg.getPar() == cursor.par()
841 && beg.getPos() >= cursor.pos()) {
843 } else if (beg.getPar() != cursor.par()) {
850 // Now find the first inset that matches code.
851 for (; beg != end; ++beg) {
852 if (beg->lyxCode() == code) {
862 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
864 string filename = filen;
866 if (filename.empty()) {
867 // Launch a file browser
868 string initpath = lyxrc.document_path;
871 string const trypath = owner_->buffer()->filePath();
872 // If directory is writeable, use this as default.
873 if (IsDirWriteable(trypath))
877 FileDialog fileDlg(bv_->owner(),
878 _("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_REF_INSERT:
1102 if (ev.argument.empty()) {
1103 InsetCommandParams p("ref");
1104 owner_->getDialogs().createRef(p.getAsString());
1106 InsetCommandParams p;
1107 p.setFromString(ev.argument);
1109 InsetRef * inset = new InsetRef(p, *buffer_);
1110 if (!insertInset(inset))
1113 updateInset(inset, true);
1117 case LFUN_BOOKMARK_SAVE:
1118 savePosition(strToUnsignedInt(ev.argument));
1121 case LFUN_BOOKMARK_GOTO:
1122 restorePosition(strToUnsignedInt(ev.argument));
1127 string label = ev.argument;
1128 if (label.empty()) {
1130 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1132 label = inset->getContents();
1137 if (!label.empty()) {
1138 //bv_->savePosition(0);
1139 if (!bv_->gotoLabel(label))
1140 Alert::alert(_("Error"),
1141 _("Couldn't find this label"),
1142 _("in current document."));
1147 // --- accented characters ---------------------------
1150 case LFUN_CIRCUMFLEX:
1160 case LFUN_SPECIAL_CARON:
1163 case LFUN_HUNG_UMLAUT:
1166 if (ev.argument.empty()) {
1168 owner_->getLyXFunc().handleKeyFunc(ev.action);
1170 owner_->getLyXFunc().handleKeyFunc(ev.action);
1171 owner_->getIntl().getTransManager()
1172 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1173 update(bv_->getLyXText(),
1175 | BufferView::FITCUR
1176 | BufferView::CHANGE);
1180 case LFUN_MATH_MACRO:
1181 case LFUN_MATH_DELIM:
1182 case LFUN_INSERT_MATRIX:
1183 case LFUN_INSERT_MATH:
1184 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1185 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1186 case LFUN_MATH_MODE: // Open or create an inlined math inset
1187 case LFUN_GREEK: // Insert a single greek letter
1188 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1191 case LFUN_CITATION_INSERT:
1193 InsetCommandParams p;
1194 p.setFromString(ev.argument);
1196 InsetCitation * inset = new InsetCitation(p);
1197 if (!insertInset(inset))
1200 inset->setLoadingBuffer(bv_->buffer(), false);
1201 updateInset(inset, true);
1207 case LFUN_INSERT_BIBTEX:
1209 // ale970405+lasgoutt970425
1210 // The argument can be up to two tokens separated
1211 // by a space. The first one is the bibstyle.
1212 string const db = token(ev.argument, ' ', 0);
1213 string bibstyle = token(ev.argument, ' ', 1);
1214 if (bibstyle.empty())
1217 InsetCommandParams p("BibTeX", db, bibstyle);
1218 InsetBibtex * inset = new InsetBibtex(p);
1220 if (insertInset(inset)) {
1221 if (ev.argument.empty())
1228 // BibTeX data bases
1229 case LFUN_BIBDB_ADD:
1231 InsetBibtex * inset =
1232 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1234 inset->addDatabase(ev.argument);
1239 case LFUN_BIBDB_DEL:
1241 InsetBibtex * inset =
1242 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1244 inset->delDatabase(ev.argument);
1248 case LFUN_BIBTEX_STYLE:
1250 InsetBibtex * inset =
1251 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1253 inset->setOptions(ev.argument);
1257 case LFUN_CHILD_INSERT:
1259 InsetInclude::Params p;
1260 if (!ev.argument.empty())
1261 p.cparams.setFromString(ev.argument);
1262 p.masterFilename_ = buffer_->fileName();
1264 InsetInclude * inset = new InsetInclude(p);
1265 if (!insertInset(inset))
1268 updateInset(inset, true);
1269 bv_->owner()->getDialogs().showInclude(inset);
1274 case LFUN_FLOAT_LIST:
1275 if (tclass.floats().typeExist(ev.argument)) {
1276 Inset * inset = new InsetFloatList(ev.argument);
1277 if (!insertInset(inset, tclass.defaultLayoutName()))
1280 lyxerr << "Non-existent float type: "
1281 << ev.argument << endl;
1285 case LFUN_THESAURUS_ENTRY:
1287 string arg = ev.argument;
1290 arg = bv_->getLyXText()->selectionAsString(buffer_,
1294 if (arg.size() > 100 || arg.empty()) {
1295 // Get word or selection
1296 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1297 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1298 // FIXME: where is getLyXText()->unselect(bv_) ?
1302 bv_->owner()->getDialogs().showThesaurus(arg);
1306 case LFUN_TRACK_CHANGES:
1310 case LFUN_MERGE_CHANGES:
1311 owner_->getDialogs().showMergeChanges();
1314 case LFUN_ACCEPT_ALL_CHANGES: {
1315 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1316 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1317 #warning FIXME changes
1318 //moveCursorUpdate(false);
1320 while (lyxfind::findNextChange(bv_)) {
1321 bv_->getLyXText()->acceptChange(bv_);
1324 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1328 case LFUN_REJECT_ALL_CHANGES: {
1329 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1330 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1331 #warning FIXME changes
1332 //moveCursorUpdate(false);
1334 while (lyxfind::findNextChange(bv_)) {
1335 bv_->getLyXText()->rejectChange(bv_);
1338 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1342 case LFUN_ACCEPT_CHANGE: {
1343 bv_->getLyXText()->acceptChange(bv_);
1345 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1349 case LFUN_REJECT_CHANGE: {
1350 bv_->getLyXText()->rejectChange(bv_);
1352 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1356 case LFUN_UNKNOWN_ACTION:
1357 ev.errorMessage(N_("Unknown function!"));
1361 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1368 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1370 // if we are in a locking inset we should try to insert the
1371 // inset there otherwise this is a illegal function now
1372 if (bv_->theLockingInset()) {
1373 if (bv_->theLockingInset()->insetAllowed(inset))
1374 return bv_->theLockingInset()->insertInset(bv_, inset);
1378 // not quite sure if we want this...
1379 setCursorParUndo(bv_);
1382 beforeChange(bv_->text);
1383 if (!lout.empty()) {
1384 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1385 bv_->text->breakParagraph(bv_);
1386 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1388 if (!bv_->text->cursor.par()->empty()) {
1389 bv_->text->cursorLeft(bv_);
1391 bv_->text->breakParagraph(bv_);
1392 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1396 LyXTextClass const & tclass =
1397 buffer_->params.getLyXTextClass();
1398 bool hasLayout = tclass.hasLayout(lres);
1399 string lay = tclass.defaultLayoutName();
1401 if (hasLayout != false) {
1405 // layout not fount using default
1406 lay = tclass.defaultLayoutName();
1409 bv_->text->setLayout(bv_, lay);
1411 bv_->text->setParagraph(bv_, 0, 0,
1413 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1418 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1421 bv_->text->insertInset(bv_, inset);
1422 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1429 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1431 if (!inset || !available())
1434 // first check for locking insets
1435 if (bv_->theLockingInset()) {
1436 if (bv_->theLockingInset() == inset) {
1437 if (bv_->text->updateInset(bv_, inset)) {
1440 buffer_->markDirty();
1445 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1446 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1449 buffer_->markDirty();
1457 // then check if the inset is a top_level inset (has no owner)
1458 // if yes do the update as always otherwise we have to update the
1459 // toplevel inset where this inset is inside
1460 Inset * tl_inset = inset;
1461 while (tl_inset->owner())
1462 tl_inset = tl_inset->owner();
1464 if (tl_inset == inset) {
1465 update(bv_->text, BufferView::UPDATE);
1466 if (bv_->text->updateInset(bv_, inset)) {
1470 | BufferView::FITCUR
1471 | BufferView::CHANGE);
1473 update(bv_->text, SELECT);
1477 } else if (static_cast<UpdatableInset *>(tl_inset)
1478 ->updateInsetInInset(bv_, inset))
1480 if (bv_->text->updateInset(bv_, tl_inset)) {