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);
134 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
136 // get absolute path of file and add ".lyx" to the filename if
138 string s = FileSearch(string(), filename, "lyx");
143 // file already open?
144 if (bufferlist.exists(s)) {
145 string const file = MakeDisplayPath(s, 20);
146 string text = bformat(_("The document %1$s is already "
147 "loaded.\n\nDo you want to revert "
148 "to the saved version?"), file);
149 int const ret = Alert::prompt(_("Revert to saved document?"),
150 text, 0, 1, _("&Revert"), _("&Switch to document"));
153 buffer(bufferlist.getBuffer(s));
156 // FIXME: should be LFUN_REVERT
157 if (!bufferlist.close(bufferlist.getBuffer(s), false))
159 // Fall through to new load. (Asger)
162 Buffer * b = bufferlist.newBuffer(s);
164 //this is the point to attach to the error signal in the buffer
166 if (!::loadLyXFile(b, s)) {
167 bufferlist.release(b);
168 string text = bformat(_("The document %1$s does "
170 "Do you want to create "
171 "a new document?"), s);
172 int const ret = Alert::prompt(_("Create new document?"),
173 text, 0, 1, _("&Create"), _("Cancel"));
176 bufferlist.close(buffer_, false);
177 buffer(newFile(s, string(), true));
184 lastfiles->newFile(b->fileName());
189 WorkArea & BufferView::Pimpl::workarea() const
191 return *workarea_.get();
195 LyXScreen & BufferView::Pimpl::screen() const
197 return *screen_.get();
201 Painter & BufferView::Pimpl::painter() const
203 return workarea().getPainter();
207 void BufferView::Pimpl::buffer(Buffer * b)
209 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
212 buffer_->delUser(bv_);
214 // Put the old text into the TextCache, but
215 // only if the buffer is still loaded.
216 // Also set the owner of the test to 0
217 // bv_->text->owner(0);
218 textcache.add(buffer_, workarea().workWidth(), bv_->text);
219 if (lyxerr.debugging())
220 textcache.show(lyxerr, "BufferView::buffer");
225 // set current buffer
228 // if we're quitting lyx, don't bother updating stuff
232 // if we are closing the buffer, use the first buffer as current
234 buffer_ = bufferlist.first();
238 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
239 buffer_->addUser(bv_);
241 // If we don't have a text object for this, we make one
242 if (bv_->text == 0) {
243 resizeCurrentBuffer();
246 // FIXME: needed when ?
247 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
249 // Buffer-dependent dialogs should be updated or
250 // hidden. This should go here because some dialogs (eg ToC)
251 // require bv_->text.
252 owner_->getDialogs().updateBufferDependent(true);
254 lyxerr[Debug::INFO] << " No Buffer!" << endl;
255 owner_->getDialogs().hideBufferDependent();
257 // Also remove all remaining text's from the testcache.
258 // (there should not be any!) (if there is any it is a
260 if (lyxerr.debugging())
261 textcache.show(lyxerr, "buffer delete all");
267 owner_->updateMenubar();
268 owner_->updateToolbar();
269 owner_->updateLayoutChoice();
270 owner_->updateWindowTitle();
273 // Don't forget to update the Layout
274 string const layoutname =
275 bv_->text->cursor.par()->layout()->name();
276 owner_->setLayout(layoutname);
279 if (grfx::Previews::activated() && buffer_)
280 grfx::Previews::get().generateBufferPreviews(*buffer_);
284 bool BufferView::Pimpl::fitCursor()
288 if (bv_->theLockingInset()) {
289 bv_->theLockingInset()->fitInsetCursor(bv_);
292 ret = screen().fitCursor(bv_->text, bv_);
295 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
297 // We need to always update, in case we did a
298 // paste and we stayed anchored to a row, but
299 // the actual height of the doc changed ...
305 void BufferView::Pimpl::redoCurrentBuffer()
307 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
308 if (buffer_ && bv_->text) {
309 resizeCurrentBuffer();
311 owner_->updateLayoutChoice();
317 int BufferView::Pimpl::resizeCurrentBuffer()
319 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
321 ParagraphList::iterator par;
322 ParagraphList::iterator selstartpar;
323 ParagraphList::iterator selendpar;
324 UpdatableInset * the_locking_inset = 0;
327 pos_type selstartpos = 0;
328 pos_type selendpos = 0;
329 bool selection = false;
330 bool mark_set = false;
334 owner_->message(_("Formatting document..."));
337 par = bv_->text->cursor.par();
338 pos = bv_->text->cursor.pos();
339 selstartpar = bv_->text->selection.start.par();
340 selstartpos = bv_->text->selection.start.pos();
341 selendpar = bv_->text->selection.end.par();
342 selendpos = bv_->text->selection.end.pos();
343 selection = bv_->text->selection.set();
344 mark_set = bv_->text->selection.mark();
345 the_locking_inset = bv_->theLockingInset();
346 buffer_->resizeInsets(bv_);
347 // I don't think the delete and new are necessary here we
348 // just could call only init! (Jug 20020419)
350 bv_->text = new LyXText(bv_);
351 bv_->text->init(bv_);
353 // See if we have a text in TextCache that fits
354 // the new buffer_ with the correct width.
355 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
357 if (lyxerr.debugging()) {
358 lyxerr << "Found a LyXText that fits:\n";
359 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
361 // Set the owner of the newly found text
362 // bv_->text->owner(bv_);
363 if (lyxerr.debugging())
364 textcache.show(lyxerr, "resizeCurrentBuffer");
366 buffer_->resizeInsets(bv_);
368 bv_->text = new LyXText(bv_);
369 bv_->text->init(bv_);
370 //buffer_->resizeInsets(bv_);
373 par = bv_->text->ownerParagraphs().end();
374 selstartpar = bv_->text->ownerParagraphs().end();
375 selendpar = bv_->text->ownerParagraphs().end();
378 if (par != bv_->text->ownerParagraphs().end()) {
379 bv_->text->selection.set(true);
380 // At this point just to avoid the Delete-Empty-Paragraph-
381 // Mechanism when setting the cursor.
382 bv_->text->selection.mark(mark_set);
384 bv_->text->setCursor(selstartpar, selstartpos);
385 bv_->text->selection.cursor = bv_->text->cursor;
386 bv_->text->setCursor(selendpar, selendpos);
387 bv_->text->setSelection();
388 bv_->text->setCursor(par, pos);
390 bv_->text->setCursor(par, pos);
391 bv_->text->selection.cursor = bv_->text->cursor;
392 bv_->text->selection.set(false);
394 // remake the inset locking
395 bv_->theLockingInset(the_locking_inset);
398 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
403 // reset the "Formatting..." message
404 owner_->clearMessage();
412 void BufferView::Pimpl::repaint()
414 // Regenerate the screen.
415 screen().redraw(bv_->text, bv_);
419 void BufferView::Pimpl::updateScrollbar()
422 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
423 workarea().setScrollbarParams(0, 0, 0);
427 LyXText const & t = *bv_->text;
429 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
430 << t.top_y() << ", default height " << defaultRowHeight() << endl;
432 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
436 void BufferView::Pimpl::scrollDocView(int value)
438 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
443 screen().hideCursor();
445 screen().draw(bv_->text, bv_, value);
447 if (!lyxrc.cursor_follows_scrollbar)
450 LyXText * vbt = bv_->text;
452 int const height = defaultRowHeight();
453 int const first = static_cast<int>((bv_->text->top_y() + height));
454 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
456 if (vbt->cursor.y() < first)
457 vbt->setCursorFromCoordinates(0, first);
458 else if (vbt->cursor.y() > last)
459 vbt->setCursorFromCoordinates(0, last);
461 owner_->updateLayoutChoice();
465 void BufferView::Pimpl::scroll(int lines)
471 LyXText const * t = bv_->text;
472 int const line_height = defaultRowHeight();
474 // The new absolute coordinate
475 int new_top_y = t->top_y() + lines * line_height;
477 // Restrict to a valid value
478 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
479 new_top_y = std::max(0, new_top_y);
481 scrollDocView(new_top_y);
483 // Update the scrollbar.
484 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
488 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
489 key_modifier::state state)
491 bv_->owner()->getLyXFunc().processKeySym(key, state);
493 /* This is perhaps a bit of a hack. When we move
494 * around, or type, it's nice to be able to see
495 * the cursor immediately after the keypress. So
496 * we reset the toggle timeout and force the visibility
497 * of the cursor. Note we cannot do this inside
498 * dispatch() itself, because that's called recursively.
501 cursor_timeout.restart();
502 screen().showCursor(*bv_);
507 void BufferView::Pimpl::selectionRequested()
514 LyXText * text = bv_->getLyXText();
516 if (text->selection.set() &&
517 (!bv_->text->xsel_cache.set() ||
518 text->selection.start != bv_->text->xsel_cache.start ||
519 text->selection.end != bv_->text->xsel_cache.end))
521 bv_->text->xsel_cache = text->selection;
522 sel = text->selectionAsString(bv_->buffer(), false);
523 } else if (!text->selection.set()) {
525 bv_->text->xsel_cache.set(false);
528 workarea().putClipboard(sel);
533 void BufferView::Pimpl::selectionLost()
536 screen().hideCursor();
538 bv_->getLyXText()->clearSelection();
539 bv_->text->xsel_cache.set(false);
544 void BufferView::Pimpl::workAreaResize()
546 static int work_area_width;
547 static int work_area_height;
549 bool const widthChange = workarea().workWidth() != work_area_width;
550 bool const heightChange = workarea().workHeight() != work_area_height;
552 // update from work area
553 work_area_width = workarea().workWidth();
554 work_area_height = workarea().workHeight();
558 // The visible LyXView need a resize
559 resizeCurrentBuffer();
561 // Remove all texts from the textcache
562 // This is not _really_ what we want to do. What
563 // we really want to do is to delete in textcache
564 // that does not have a BufferView with matching
565 // width, but as long as we have only one BufferView
566 // deleting all gives the same result.
567 if (lyxerr.debugging())
568 textcache.show(lyxerr, "Expose delete all");
570 // FIXME: this is already done in resizeCurrentBuffer() ??
571 buffer_->resizeInsets(bv_);
572 } else if (heightChange) {
573 // fitCursor() ensures we don't jump back
574 // to the start of the document on vertical
580 if (widthChange || heightChange) {
584 // always make sure that the scrollbar is sane.
586 owner_->updateLayoutChoice();
591 void BufferView::Pimpl::update()
593 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
594 screen().update(*bv_);
595 bv_->text->clearPaint();
600 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
602 if (!text->selection.set() && (f & SELECT)) {
603 text->selection.cursor = text->cursor;
608 if (text->inset_owner) {
609 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
610 updateInset(text->inset_owner);
617 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
619 LyXText * text = bv_->text;
621 if (!text->selection.set() && (f & SELECT)) {
622 text->selection.cursor = text->cursor;
627 if (text->inset_owner) {
628 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
629 updateInset(text->inset_owner);
636 // Callback for cursor timer
637 void BufferView::Pimpl::cursorToggle()
640 cursor_timeout.restart();
644 screen().toggleCursor(*bv_);
646 cursor_timeout.restart();
650 bool BufferView::Pimpl::available() const
652 if (buffer_ && bv_->text)
658 Change const BufferView::Pimpl::getCurrentChange()
660 if (!bv_->buffer()->params.tracking_changes)
661 return Change(Change::UNCHANGED);
663 LyXText * t(bv_->getLyXText());
665 if (!t->selection.set())
666 return Change(Change::UNCHANGED);
668 LyXCursor const & cur(t->selection.start);
669 return cur.par()->lookupChangeFull(cur.pos());
673 void BufferView::Pimpl::beforeChange(LyXText * text)
676 text->clearSelection();
680 void BufferView::Pimpl::savePosition(unsigned int i)
682 if (i >= saved_positions_num)
684 saved_positions[i] = Position(buffer_->fileName(),
685 bv_->text->cursor.par()->id(),
686 bv_->text->cursor.pos());
688 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
692 void BufferView::Pimpl::restorePosition(unsigned int i)
694 if (i >= saved_positions_num)
697 string const fname = saved_positions[i].filename;
699 beforeChange(bv_->text);
701 if (fname != buffer_->fileName()) {
703 if (bufferlist.exists(fname))
704 b = bufferlist.getBuffer(fname);
706 b = bufferlist.newBuffer(fname);
707 ::loadLyXFile(b, fname); // don't ask, just load it
713 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
714 if (par == buffer_->par_iterator_end())
717 bv_->text->setCursor(par.pit(),
718 min(par->size(), saved_positions[i].par_pos));
720 update(BufferView::SELECT);
722 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
726 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
728 if (i >= saved_positions_num)
731 return !saved_positions[i].filename.empty();
735 void BufferView::Pimpl::switchKeyMap()
737 if (!lyxrc.rtl_support)
740 LyXText * text = bv_->getLyXText();
741 if (text->real_current_font.isRightToLeft()
742 && !(bv_->theLockingInset()
743 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
745 if (owner_->getIntl().keymap == Intl::PRIMARY)
746 owner_->getIntl().KeyMapSec();
748 if (owner_->getIntl().keymap == Intl::SECONDARY)
749 owner_->getIntl().KeyMapPrim();
754 void BufferView::Pimpl::insetUnlock()
756 if (bv_->theLockingInset()) {
757 bv_->theLockingInset()->insetUnlock(bv_);
758 bv_->theLockingInset(0);
764 void BufferView::Pimpl::toggleSelection(bool b)
766 if (bv_->theLockingInset())
767 bv_->theLockingInset()->toggleSelection(bv_, b);
768 screen().toggleSelection(bv_->text, bv_, b);
772 void BufferView::Pimpl::toggleToggle()
774 screen().toggleToggle(bv_->text, bv_);
778 void BufferView::Pimpl::center()
780 LyXText * t = bv_->text;
783 int const half_height = workarea().workHeight() / 2;
786 if (t->cursor.y() > half_height) {
787 new_y = t->cursor.y() - half_height;
790 // FIXME: look at this comment again ...
792 // FIXME: can we do this w/o calling screen directly ?
793 // This updates top_y() but means the fitCursor() call
794 // from the update(FITCUR) doesn't realise that we might
795 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
796 // the scrollbar to be updated as it should, so we have
797 // to do it manually. Any operation that does a center()
798 // and also might have moved top_y() must make sure to call
799 // updateScrollbar() currently. Never mind that this is a
800 // pretty obfuscated way of updating t->top_y()
801 screen().draw(t, bv_, new_y);
803 update(BufferView::SELECT);
807 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
809 workarea().putClipboard(stuff);
814 * Dispatch functions for actions which can be valid for BufferView->text
815 * and/or InsetText->text!!!
819 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
822 LyXCursor cursor = bv_->getLyXText()->cursor;
823 Buffer::inset_iterator it =
824 find_if(Buffer::inset_iterator(
825 cursor.par(), cursor.pos()),
826 buffer_->inset_iterator_end(),
827 lyx::compare_memfun(&Inset::lyxCode, code));
828 return it != buffer_->inset_iterator_end() ? (*it) : 0;
830 // Ok, this is a little bit too brute force but it
831 // should work for now. Better infrastructure is comming. (Lgb)
833 Buffer * b = bv_->buffer();
834 LyXCursor cursor = bv_->getLyXText()->cursor;
836 Buffer::inset_iterator beg = b->inset_iterator_begin();
837 Buffer::inset_iterator end = b->inset_iterator_end();
839 bool cursor_par_seen = false;
841 for (; beg != end; ++beg) {
842 if (beg.getPar() == cursor.par()) {
843 cursor_par_seen = true;
845 if (cursor_par_seen) {
846 if (beg.getPar() == cursor.par()
847 && beg.getPos() >= cursor.pos()) {
849 } else if (beg.getPar() != cursor.par()) {
856 // Now find the first inset that matches code.
857 for (; beg != end; ++beg) {
858 if (beg->lyxCode() == code) {
868 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
870 string filename = filen;
872 if (filename.empty()) {
873 // Launch a file browser
874 string initpath = lyxrc.document_path;
877 string const trypath = owner_->buffer()->filePath();
878 // If directory is writeable, use this as default.
879 if (IsDirWriteable(trypath))
883 FileDialog fileDlg(_("Select LyX document to insert"),
885 make_pair(string(_("Documents|#o#O")),
886 string(lyxrc.document_path)),
887 make_pair(string(_("Examples|#E#e")),
888 string(AddPath(system_lyxdir, "examples"))));
890 FileDialog::Result result =
891 fileDlg.open(initpath,
892 _("*.lyx| LyX Documents (*.lyx)"));
894 if (result.first == FileDialog::Later)
897 filename = result.second;
899 // check selected filename
900 if (filename.empty()) {
901 owner_->message(_("Canceled."));
906 // get absolute path of file and add ".lyx" to the filename if
908 filename = FileSearch(string(), filename, "lyx");
910 string const disp_fn = MakeDisplayPath(filename);
911 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
912 bool const res = bv_->insertLyXFile(filename);
914 owner_->message(bformat(_("Document %1$s inserted."), disp_fn));
916 owner_->message(bformat(_("Could not insert document %1$s"), disp_fn));
920 void BufferView::Pimpl::trackChanges()
922 Buffer * buf(bv_->buffer());
923 bool const tracking(buf->params.tracking_changes);
926 ParIterator const end = buf->par_iterator_end();
927 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
929 buf->params.tracking_changes = true;
931 // we cannot allow undos beyond the freeze point
932 buf->undostack.clear();
934 update(BufferView::SELECT);
935 bv_->text->setCursor(buf->paragraphs.begin(), 0);
936 #warning changes FIXME
937 //moveCursorUpdate(false);
939 bool found = lyxfind::findNextChange(bv_);
941 owner_->getDialogs().show("changes");
945 ParIterator const end = buf->par_iterator_end();
946 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
947 it->untrackChanges();
948 buf->params.tracking_changes = false;
951 buf->redostack.clear();
955 // Doesn't go through lyxfunc, so we need to update the
956 // layout choice etc. ourselves
957 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
959 // e.g. Qt mouse press when no buffer
963 screen().hideCursor();
965 bool const res = dispatch(ev_in);
967 // see workAreaKeyPress
968 cursor_timeout.restart();
969 screen().showCursor(*bv_);
971 // FIXME: we should skip these when selecting
972 bv_->owner()->updateLayoutChoice();
973 bv_->owner()->updateToolbar();
976 // slight hack: this is only called currently when
977 // we clicked somewhere, so we force through the display
978 // of the new status here.
979 bv_->owner()->clearMessage();
985 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
987 // Make sure that the cached BufferView is correct.
988 FuncRequest ev = ev_in;
991 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
992 << " action[" << ev.action << ']'
993 << " arg[" << ev.argument << ']'
994 << " x[" << ev.x << ']'
995 << " y[" << ev.y << ']'
996 << " button[" << ev.button() << ']'
999 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1001 switch (ev.action) {
1003 case LFUN_SCROLL_INSET:
1004 // this is not handled here as this function is only active
1005 // if we have a locking_inset and that one is (or contains)
1009 case LFUN_FILE_INSERT:
1010 MenuInsertLyXFile(ev.argument);
1013 case LFUN_FILE_INSERT_ASCII_PARA:
1014 InsertAsciiFile(bv_, ev.argument, true);
1017 case LFUN_FILE_INSERT_ASCII:
1018 InsertAsciiFile(bv_, ev.argument, false);
1022 lang(bv_, ev.argument);
1024 owner_->view_state_changed();
1029 owner_->view_state_changed();
1034 owner_->view_state_changed();
1039 owner_->view_state_changed();
1044 owner_->view_state_changed();
1049 owner_->view_state_changed();
1054 owner_->view_state_changed();
1059 owner_->view_state_changed();
1062 case LFUN_UNDERLINE:
1064 owner_->view_state_changed();
1067 case LFUN_FONT_SIZE:
1068 fontSize(bv_, ev.argument);
1069 owner_->view_state_changed();
1072 case LFUN_FONT_STATE:
1073 owner_->getLyXFunc().setMessage(currentState(bv_));
1076 case LFUN_INSERT_LABEL: {
1077 // Try and generate a valid label
1078 string const contents = ev.argument.empty() ?
1079 getPossibleLabel(*bv_) : ev.argument;
1080 InsetCommandParams icp("label", contents);
1081 string data = InsetCommandMailer::params2string("label", icp);
1082 owner_->getDialogs().show("label", data, 0);
1086 case LFUN_BOOKMARK_SAVE:
1087 savePosition(strToUnsignedInt(ev.argument));
1090 case LFUN_BOOKMARK_GOTO:
1091 restorePosition(strToUnsignedInt(ev.argument));
1094 case LFUN_REF_GOTO: {
1095 string label = ev.argument;
1096 if (label.empty()) {
1098 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1100 label = inset->getContents();
1106 bv_->gotoLabel(label);
1110 // --- accented characters ---------------------------
1113 case LFUN_CIRCUMFLEX:
1123 case LFUN_SPECIAL_CARON:
1126 case LFUN_HUNG_UMLAUT:
1129 if (ev.argument.empty()) {
1131 owner_->getLyXFunc().handleKeyFunc(ev.action);
1133 owner_->getLyXFunc().handleKeyFunc(ev.action);
1134 owner_->getIntl().getTransManager()
1135 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1136 update(bv_->getLyXText(), BufferView::SELECT);
1140 case LFUN_MATH_MACRO:
1141 case LFUN_MATH_DELIM:
1142 case LFUN_INSERT_MATRIX:
1143 case LFUN_INSERT_MATH:
1144 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1145 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1146 case LFUN_MATH_MODE: // Open or create an inlined math inset
1150 case LFUN_INSET_APPLY: {
1151 string const name = ev.getArg(0);
1153 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1155 // This works both for 'original' and 'mathed' insets.
1156 // Note that the localDispatch performs updateInset
1158 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1159 inset->localDispatch(fr);
1161 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1167 case LFUN_INSET_INSERT: {
1168 Inset * inset = createInset(ev);
1169 if (inset && insertInset(inset)) {
1172 string const name = ev.getArg(0);
1173 if (name == "bibitem") {
1174 // We need to do a redraw because the maximum
1175 // InsetBibitem width could have changed
1176 #warning please check you mean repaint() not update(),
1177 #warning and whether the repaint() is needed at all
1187 case LFUN_FLOAT_LIST:
1188 if (tclass.floats().typeExist(ev.argument)) {
1189 Inset * inset = new InsetFloatList(ev.argument);
1190 if (!insertInset(inset, tclass.defaultLayoutName()))
1193 lyxerr << "Non-existent float type: "
1194 << ev.argument << endl;
1198 case LFUN_LAYOUT_PARAGRAPH: {
1199 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1204 params2string(*par, data);
1206 data = "show\n" + data;
1207 bv_->owner()->getDialogs().show("paragraph", data);
1211 case LFUN_PARAGRAPH_UPDATE: {
1212 if (!bv_->owner()->getDialogs().visible("paragraph"))
1214 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1219 params2string(*par, data);
1221 // Will the paragraph accept changes from the dialog?
1222 Inset * const inset = par->inInset();
1224 !(inset && inset->forceDefaultParagraphs(inset));
1226 data = "update " + tostr(accept) + '\n' + data;
1227 bv_->owner()->getDialogs().update("paragraph", data);
1231 case LFUN_PARAGRAPH_APPLY:
1232 setParagraphParams(*bv_, ev.argument);
1235 case LFUN_THESAURUS_ENTRY:
1237 string arg = ev.argument;
1240 arg = bv_->getLyXText()->selectionAsString(buffer_,
1244 if (arg.size() > 100 || arg.empty()) {
1245 // Get word or selection
1246 bv_->getLyXText()->selectWordWhenUnderCursor(LyXText::WHOLE_WORD);
1247 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1248 // FIXME: where is getLyXText()->unselect(bv_) ?
1252 bv_->owner()->getDialogs().show("thesaurus", arg);
1256 case LFUN_TRACK_CHANGES:
1260 case LFUN_MERGE_CHANGES:
1261 owner_->getDialogs().show("changes");
1264 case LFUN_ACCEPT_ALL_CHANGES: {
1265 update(BufferView::SELECT);
1266 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1267 #warning FIXME changes
1268 //moveCursorUpdate(false);
1270 while (lyxfind::findNextChange(bv_)) {
1271 bv_->getLyXText()->acceptChange();
1273 update(BufferView::SELECT);
1277 case LFUN_REJECT_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()->rejectChange();
1286 update(BufferView::SELECT);
1290 case LFUN_ACCEPT_CHANGE: {
1291 bv_->getLyXText()->acceptChange();
1292 update(BufferView::SELECT);
1296 case LFUN_REJECT_CHANGE: {
1297 bv_->getLyXText()->rejectChange();
1298 update(BufferView::SELECT);
1302 case LFUN_UNKNOWN_ACTION:
1303 ev.errorMessage(N_("Unknown function!"));
1307 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1314 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1316 // if we are in a locking inset we should try to insert the
1317 // inset there otherwise this is a illegal function now
1318 if (bv_->theLockingInset()) {
1319 if (bv_->theLockingInset()->insetAllowed(inset))
1320 return bv_->theLockingInset()->insertInset(bv_, inset);
1324 // not quite sure if we want this...
1325 setCursorParUndo(bv_);
1328 beforeChange(bv_->text);
1329 if (!lout.empty()) {
1330 update(BufferView::SELECT);
1331 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1332 update(BufferView::SELECT);
1334 if (!bv_->text->cursor.par()->empty()) {
1335 bv_->text->cursorLeft(bv_);
1337 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1338 update(BufferView::SELECT);
1342 LyXTextClass const & tclass =
1343 buffer_->params.getLyXTextClass();
1344 bool hasLayout = tclass.hasLayout(lres);
1345 string lay = tclass.defaultLayoutName();
1347 if (hasLayout != false) {
1351 // layout not fount using default
1352 lay = tclass.defaultLayoutName();
1355 bv_->text->setLayout(lay);
1357 bv_->text->setParagraph(0, 0,
1359 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1364 update(BufferView::SELECT);
1367 bv_->text->insertInset(inset);
1368 update(BufferView::SELECT);
1375 void BufferView::Pimpl::updateInset(Inset * inset)
1377 if (!inset || !available())
1380 // first check for locking insets
1381 if (bv_->theLockingInset()) {
1382 if (bv_->theLockingInset() == inset) {
1383 if (bv_->text->updateInset(inset)) {
1388 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1389 if (bv_->text->updateInset(bv_->theLockingInset())) {
1397 // then check if the inset is a top_level inset (has no owner)
1398 // if yes do the update as always otherwise we have to update the
1399 // toplevel inset where this inset is inside
1400 Inset * tl_inset = inset;
1401 while (tl_inset->owner())
1402 tl_inset = tl_inset->owner();
1403 if (tl_inset == inset) {
1404 update(BufferView::UPDATE);
1405 if (bv_->text->updateInset(inset)) {
1406 update(BufferView::SELECT);
1409 } else if (static_cast<UpdatableInset *>(tl_inset)
1410 ->updateInsetInInset(bv_, inset))
1412 if (bv_->text->updateInset(tl_inset)) {