2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
12 #include "BufferView_pimpl.h"
13 #include "bufferlist.h"
15 #include "buffer_funcs.h"
16 #include "bufferview_funcs.h"
20 #include "FloatList.h"
21 #include "funcrequest.h"
24 #include "iterators.h"
26 #include "lyx_cb.h" // added for Dispatch functions
33 #include "lastfiles.h"
34 #include "paragraph.h"
35 #include "ParagraphParameters.h"
36 #include "TextCache.h"
37 #include "undo_funcs.h"
39 #include "insets/insetfloatlist.h"
40 #include "insets/insetgraphics.h"
41 #include "insets/insetinclude.h"
42 #include "insets/insetref.h"
43 #include "insets/insettext.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/FileDialog.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/LyXScreenFactory.h"
50 #include "frontends/mouse_state.h"
51 #include "frontends/screen.h"
52 #include "frontends/WorkArea.h"
53 #include "frontends/WorkAreaFactory.h"
55 #include "mathed/formulabase.h"
57 #include "graphics/Previews.h"
59 #include "support/LAssert.h"
60 #include "support/tostr.h"
61 #include "support/filetools.h"
63 #include <boost/bind.hpp>
64 #include <boost/signals/connection.hpp>
79 using namespace lyx::support;
80 using namespace bv_funcs;
82 extern BufferList bufferlist;
87 unsigned int const saved_positions_num = 20;
89 // All the below connection objects are needed because of a bug in some
90 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
91 // to these connections we avoid a segfault upon startup, and also at exit.
94 boost::signals::connection dispatchcon;
95 boost::signals::connection timecon;
96 boost::signals::connection doccon;
97 boost::signals::connection resizecon;
98 boost::signals::connection kpresscon;
99 boost::signals::connection selectioncon;
100 boost::signals::connection lostcon;
106 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
107 int xpos, int ypos, int width, int height)
108 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
109 using_xterm_cursor(false)
111 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
112 screen_.reset(LyXScreenFactory::create(workarea()));
115 doccon = workarea().scrollDocView
116 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
117 resizecon = workarea().workAreaResize
118 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
119 dispatchcon = workarea().dispatch
120 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
121 kpresscon = workarea().workAreaKeyPress
122 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
123 selectioncon = workarea().selectionRequested
124 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
125 lostcon = workarea().selectionLost
126 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
128 timecon = cursor_timeout.timeout
129 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
130 cursor_timeout.start();
131 saved_positions.resize(saved_positions_num);
135 void BufferView::Pimpl::addError(ErrorItem const & ei)
137 errorlist_.push_back(ei);
142 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
144 // get absolute path of file and add ".lyx" to the filename if
146 string s = FileSearch(string(), filename, "lyx");
151 // file already open?
152 if (bufferlist.exists(s)) {
153 string const file = MakeDisplayPath(s, 20);
154 string text = bformat(_("The document %1$s is already "
155 "loaded.\n\nDo you want to revert "
156 "to the saved version?"), file);
157 int const ret = Alert::prompt(_("Revert to saved document?"),
158 text, 0, 1, _("&Revert"), _("&Switch to document"));
161 buffer(bufferlist.getBuffer(s));
164 // FIXME: should be LFUN_REVERT
165 if (!bufferlist.close(bufferlist.getBuffer(s), false))
167 // Fall through to new load. (Asger)
170 Buffer * b = bufferlist.newBuffer(s);
172 //attach to the error signal in the buffer
173 b->parseError.connect(boost::bind(&BufferView::Pimpl::addError,
176 bool loaded = ::loadLyXFile(b, s);
179 bufferlist.release(b);
180 string text = bformat(_("The document %1$s does "
182 "Do you want to create "
183 "a new document?"), s);
184 int const ret = Alert::prompt(_("Create new document?"),
185 text, 0, 1, _("&Create"), _("Cancel"));
188 b = newFile(s, string(), true);
197 lastfiles->newFile(b->fileName());
200 bv_->showErrorList(_("Parse"));
205 WorkArea & BufferView::Pimpl::workarea() const
207 return *workarea_.get();
211 LyXScreen & BufferView::Pimpl::screen() const
213 return *screen_.get();
217 Painter & BufferView::Pimpl::painter() const
219 return workarea().getPainter();
223 void BufferView::Pimpl::buffer(Buffer * b)
225 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
228 buffer_->delUser(bv_);
230 // Put the old text into the TextCache, but
231 // only if the buffer is still loaded.
232 // Also set the owner of the test to 0
233 // bv_->text->owner(0);
234 textcache.add(buffer_, workarea().workWidth(), bv_->text);
235 if (lyxerr.debugging())
236 textcache.show(lyxerr, "BufferView::buffer");
241 // set current buffer
244 // if we're quitting lyx, don't bother updating stuff
248 // if we are closing the buffer, use the first buffer as current
250 buffer_ = bufferlist.first();
254 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
255 buffer_->addUser(bv_);
257 // If we don't have a text object for this, we make one
258 if (bv_->text == 0) {
259 resizeCurrentBuffer();
262 // FIXME: needed when ?
263 bv_->text->top_y(screen().topCursorVisible(bv_->text));
265 // Buffer-dependent dialogs should be updated or
266 // hidden. This should go here because some dialogs (eg ToC)
267 // require bv_->text.
268 owner_->getDialogs().updateBufferDependent(true);
270 lyxerr[Debug::INFO] << " No Buffer!" << endl;
271 owner_->getDialogs().hideBufferDependent();
273 // Also remove all remaining text's from the testcache.
274 // (there should not be any!) (if there is any it is a
276 if (lyxerr.debugging())
277 textcache.show(lyxerr, "buffer delete all");
283 owner_->updateMenubar();
284 owner_->updateToolbar();
285 owner_->updateLayoutChoice();
286 owner_->updateWindowTitle();
289 // Don't forget to update the Layout
290 string const layoutname =
291 bv_->text->cursor.par()->layout()->name();
292 owner_->setLayout(layoutname);
295 if (grfx::Previews::activated() && buffer_)
296 grfx::Previews::get().generateBufferPreviews(*buffer_);
300 bool BufferView::Pimpl::fitCursor()
304 if (bv_->theLockingInset()) {
305 bv_->theLockingInset()->fitInsetCursor(bv_);
308 ret = screen().fitCursor(bv_->text, bv_);
311 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
313 // We need to always update, in case we did a
314 // paste and we stayed anchored to a row, but
315 // the actual height of the doc changed ...
321 void BufferView::Pimpl::redoCurrentBuffer()
323 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
324 if (buffer_ && bv_->text) {
325 resizeCurrentBuffer();
327 owner_->updateLayoutChoice();
333 int BufferView::Pimpl::resizeCurrentBuffer()
335 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
337 ParagraphList::iterator par;
338 ParagraphList::iterator selstartpar;
339 ParagraphList::iterator selendpar;
340 UpdatableInset * the_locking_inset = 0;
343 pos_type selstartpos = 0;
344 pos_type selendpos = 0;
345 bool selection = false;
346 bool mark_set = false;
350 owner_->message(_("Formatting document..."));
353 par = bv_->text->cursor.par();
354 pos = bv_->text->cursor.pos();
355 selstartpar = bv_->text->selection.start.par();
356 selstartpos = bv_->text->selection.start.pos();
357 selendpar = bv_->text->selection.end.par();
358 selendpos = bv_->text->selection.end.pos();
359 selection = bv_->text->selection.set();
360 mark_set = bv_->text->selection.mark();
361 the_locking_inset = bv_->theLockingInset();
362 buffer_->resizeInsets(bv_);
363 // I don't think the delete and new are necessary here we
364 // just could call only init! (Jug 20020419)
366 bv_->text = new LyXText(bv_);
367 bv_->text->init(bv_);
369 // See if we have a text in TextCache that fits
370 // the new buffer_ with the correct width.
371 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
373 if (lyxerr.debugging()) {
374 lyxerr << "Found a LyXText that fits:\n";
375 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
377 // Set the owner of the newly found text
378 // bv_->text->owner(bv_);
379 if (lyxerr.debugging())
380 textcache.show(lyxerr, "resizeCurrentBuffer");
382 buffer_->resizeInsets(bv_);
384 bv_->text = new LyXText(bv_);
385 bv_->text->init(bv_);
386 //buffer_->resizeInsets(bv_);
389 par = bv_->text->ownerParagraphs().end();
390 selstartpar = bv_->text->ownerParagraphs().end();
391 selendpar = bv_->text->ownerParagraphs().end();
394 if (par != bv_->text->ownerParagraphs().end()) {
395 bv_->text->selection.set(true);
396 // At this point just to avoid the Delete-Empty-Paragraph-
397 // Mechanism when setting the cursor.
398 bv_->text->selection.mark(mark_set);
400 bv_->text->setCursor(selstartpar, selstartpos);
401 bv_->text->selection.cursor = bv_->text->cursor;
402 bv_->text->setCursor(selendpar, selendpos);
403 bv_->text->setSelection();
404 bv_->text->setCursor(par, pos);
406 bv_->text->setCursor(par, pos);
407 bv_->text->selection.cursor = bv_->text->cursor;
408 bv_->text->selection.set(false);
410 // remake the inset locking
411 bv_->theLockingInset(the_locking_inset);
414 bv_->text->top_y(screen().topCursorVisible(bv_->text));
419 // reset the "Formatting..." message
420 owner_->clearMessage();
428 void BufferView::Pimpl::repaint()
430 // Regenerate the screen.
431 screen().redraw(bv_->text, bv_);
435 void BufferView::Pimpl::updateScrollbar()
438 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
439 workarea().setScrollbarParams(0, 0, 0);
443 LyXText const & t = *bv_->text;
445 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
446 << t.top_y() << ", default height " << defaultRowHeight() << endl;
448 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
452 void BufferView::Pimpl::scrollDocView(int value)
454 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
459 screen().hideCursor();
461 screen().draw(bv_->text, bv_, value);
463 if (!lyxrc.cursor_follows_scrollbar)
466 LyXText * vbt = bv_->text;
468 int const height = defaultRowHeight();
469 int const first = static_cast<int>((bv_->text->top_y() + height));
470 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
472 if (vbt->cursor.y() < first)
473 vbt->setCursorFromCoordinates(0, first);
474 else if (vbt->cursor.y() > last)
475 vbt->setCursorFromCoordinates(0, last);
477 owner_->updateLayoutChoice();
481 void BufferView::Pimpl::scroll(int lines)
487 LyXText const * t = bv_->text;
488 int const line_height = defaultRowHeight();
490 // The new absolute coordinate
491 int new_top_y = t->top_y() + lines * line_height;
493 // Restrict to a valid value
494 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
495 new_top_y = std::max(0, new_top_y);
497 scrollDocView(new_top_y);
499 // Update the scrollbar.
500 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
504 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
505 key_modifier::state state)
507 bv_->owner()->getLyXFunc().processKeySym(key, state);
509 /* This is perhaps a bit of a hack. When we move
510 * around, or type, it's nice to be able to see
511 * the cursor immediately after the keypress. So
512 * we reset the toggle timeout and force the visibility
513 * of the cursor. Note we cannot do this inside
514 * dispatch() itself, because that's called recursively.
517 cursor_timeout.restart();
518 screen().showCursor(*bv_);
523 void BufferView::Pimpl::selectionRequested()
530 LyXText * text = bv_->getLyXText();
532 if (text->selection.set() &&
533 (!bv_->text->xsel_cache.set() ||
534 text->selection.start != bv_->text->xsel_cache.start ||
535 text->selection.end != bv_->text->xsel_cache.end))
537 bv_->text->xsel_cache = text->selection;
538 sel = text->selectionAsString(bv_->buffer(), false);
539 } else if (!text->selection.set()) {
541 bv_->text->xsel_cache.set(false);
544 workarea().putClipboard(sel);
549 void BufferView::Pimpl::selectionLost()
552 screen().hideCursor();
554 bv_->getLyXText()->clearSelection();
555 bv_->text->xsel_cache.set(false);
560 void BufferView::Pimpl::workAreaResize()
562 static int work_area_width;
563 static int work_area_height;
565 bool const widthChange = workarea().workWidth() != work_area_width;
566 bool const heightChange = workarea().workHeight() != work_area_height;
568 // update from work area
569 work_area_width = workarea().workWidth();
570 work_area_height = workarea().workHeight();
574 // The visible LyXView need a resize
575 resizeCurrentBuffer();
577 // Remove all texts from the textcache
578 // This is not _really_ what we want to do. What
579 // we really want to do is to delete in textcache
580 // that does not have a BufferView with matching
581 // width, but as long as we have only one BufferView
582 // deleting all gives the same result.
583 if (lyxerr.debugging())
584 textcache.show(lyxerr, "Expose delete all");
586 // FIXME: this is already done in resizeCurrentBuffer() ??
587 buffer_->resizeInsets(bv_);
588 } else if (heightChange) {
589 // fitCursor() ensures we don't jump back
590 // to the start of the document on vertical
596 if (widthChange || heightChange) {
600 // always make sure that the scrollbar is sane.
602 owner_->updateLayoutChoice();
607 void BufferView::Pimpl::update()
609 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
610 screen().update(*bv_);
611 bv_->text->clearPaint();
616 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
618 if (!text->selection.set() && (f & SELECT)) {
619 text->selection.cursor = text->cursor;
622 text->partialRebreak();
624 if (text->inset_owner) {
625 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
626 updateInset(text->inset_owner);
633 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
635 LyXText * text = bv_->text;
637 if (!text->selection.set() && (f & SELECT)) {
638 text->selection.cursor = text->cursor;
641 text->partialRebreak();
643 if (text->inset_owner) {
644 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
645 updateInset(text->inset_owner);
652 // Callback for cursor timer
653 void BufferView::Pimpl::cursorToggle()
656 cursor_timeout.restart();
660 screen().toggleCursor(*bv_);
662 cursor_timeout.restart();
666 bool BufferView::Pimpl::available() const
668 if (buffer_ && bv_->text)
674 Change const BufferView::Pimpl::getCurrentChange()
676 if (!bv_->buffer()->params.tracking_changes)
677 return Change(Change::UNCHANGED);
679 LyXText * t(bv_->getLyXText());
681 if (!t->selection.set())
682 return Change(Change::UNCHANGED);
684 LyXCursor const & cur(t->selection.start);
685 return cur.par()->lookupChangeFull(cur.pos());
689 void BufferView::Pimpl::beforeChange(LyXText * text)
692 text->clearSelection();
696 void BufferView::Pimpl::savePosition(unsigned int i)
698 if (i >= saved_positions_num)
700 saved_positions[i] = Position(buffer_->fileName(),
701 bv_->text->cursor.par()->id(),
702 bv_->text->cursor.pos());
704 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
708 void BufferView::Pimpl::restorePosition(unsigned int i)
710 if (i >= saved_positions_num)
713 string const fname = saved_positions[i].filename;
715 beforeChange(bv_->text);
717 if (fname != buffer_->fileName()) {
719 if (bufferlist.exists(fname))
720 b = bufferlist.getBuffer(fname);
722 b = bufferlist.newBuffer(fname);
723 ::loadLyXFile(b, fname); // don't ask, just load it
729 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
730 if (par == buffer_->par_iterator_end())
733 bv_->text->setCursor(par.pit(),
734 min(par->size(), saved_positions[i].par_pos));
736 update(BufferView::SELECT);
738 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
742 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
744 if (i >= saved_positions_num)
747 return !saved_positions[i].filename.empty();
751 void BufferView::Pimpl::switchKeyMap()
753 if (!lyxrc.rtl_support)
756 LyXText * text = bv_->getLyXText();
757 if (text->real_current_font.isRightToLeft()
758 && !(bv_->theLockingInset()
759 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
761 if (owner_->getIntl().keymap == Intl::PRIMARY)
762 owner_->getIntl().KeyMapSec();
764 if (owner_->getIntl().keymap == Intl::SECONDARY)
765 owner_->getIntl().KeyMapPrim();
770 void BufferView::Pimpl::insetUnlock()
772 if (bv_->theLockingInset()) {
773 bv_->theLockingInset()->insetUnlock(bv_);
774 bv_->theLockingInset(0);
780 void BufferView::Pimpl::toggleSelection(bool b)
782 if (bv_->theLockingInset())
783 bv_->theLockingInset()->toggleSelection(bv_, b);
784 screen().toggleSelection(bv_->text, bv_, b);
788 void BufferView::Pimpl::toggleToggle()
790 screen().toggleToggle(bv_->text, bv_);
794 void BufferView::Pimpl::center()
796 LyXText * t = bv_->text;
799 int const half_height = workarea().workHeight() / 2;
802 if (t->cursor.y() > half_height) {
803 new_y = t->cursor.y() - half_height;
806 // FIXME: look at this comment again ...
808 // FIXME: can we do this w/o calling screen directly ?
809 // This updates top_y() but means the fitCursor() call
810 // from the update(FITCUR) doesn't realise that we might
811 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
812 // the scrollbar to be updated as it should, so we have
813 // to do it manually. Any operation that does a center()
814 // and also might have moved top_y() must make sure to call
815 // updateScrollbar() currently. Never mind that this is a
816 // pretty obfuscated way of updating t->top_y()
817 screen().draw(t, bv_, new_y);
819 update(BufferView::SELECT);
823 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
825 workarea().putClipboard(stuff);
830 * Dispatch functions for actions which can be valid for BufferView->text
831 * and/or InsetText->text!!!
835 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
838 LyXCursor cursor = bv_->getLyXText()->cursor;
839 Buffer::inset_iterator it =
840 find_if(Buffer::inset_iterator(
841 cursor.par(), cursor.pos()),
842 buffer_->inset_iterator_end(),
843 lyx::compare_memfun(&Inset::lyxCode, code));
844 return it != buffer_->inset_iterator_end() ? (*it) : 0;
846 // Ok, this is a little bit too brute force but it
847 // should work for now. Better infrastructure is comming. (Lgb)
849 Buffer * b = bv_->buffer();
850 LyXCursor cursor = bv_->getLyXText()->cursor;
852 Buffer::inset_iterator beg = b->inset_iterator_begin();
853 Buffer::inset_iterator end = b->inset_iterator_end();
855 bool cursor_par_seen = false;
857 for (; beg != end; ++beg) {
858 if (beg.getPar() == cursor.par()) {
859 cursor_par_seen = true;
861 if (cursor_par_seen) {
862 if (beg.getPar() == cursor.par()
863 && beg.getPos() >= cursor.pos()) {
865 } else if (beg.getPar() != cursor.par()) {
872 // Now find the first inset that matches code.
873 for (; beg != end; ++beg) {
874 if (beg->lyxCode() == code) {
884 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
886 string filename = filen;
888 if (filename.empty()) {
889 // Launch a file browser
890 string initpath = lyxrc.document_path;
893 string const trypath = owner_->buffer()->filePath();
894 // If directory is writeable, use this as default.
895 if (IsDirWriteable(trypath))
899 FileDialog fileDlg(_("Select LyX document to insert"),
901 make_pair(string(_("Documents|#o#O")),
902 string(lyxrc.document_path)),
903 make_pair(string(_("Examples|#E#e")),
904 string(AddPath(system_lyxdir, "examples"))));
906 FileDialog::Result result =
907 fileDlg.open(initpath,
908 _("*.lyx| LyX Documents (*.lyx)"));
910 if (result.first == FileDialog::Later)
913 filename = result.second;
915 // check selected filename
916 if (filename.empty()) {
917 owner_->message(_("Canceled."));
922 // get absolute path of file and add ".lyx" to the filename if
924 filename = FileSearch(string(), filename, "lyx");
926 string const disp_fn = MakeDisplayPath(filename);
927 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
928 bool const res = bv_->insertLyXFile(filename);
930 owner_->message(bformat(_("Document %1$s inserted."), disp_fn));
932 owner_->message(bformat(_("Could not insert document %1$s"), disp_fn));
936 void BufferView::Pimpl::trackChanges()
938 Buffer * buf(bv_->buffer());
939 bool const tracking(buf->params.tracking_changes);
942 ParIterator const end = buf->par_iterator_end();
943 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
945 buf->params.tracking_changes = true;
947 // we cannot allow undos beyond the freeze point
948 buf->undostack.clear();
950 update(BufferView::SELECT);
951 bv_->text->setCursor(buf->paragraphs.begin(), 0);
952 #warning changes FIXME
953 //moveCursorUpdate(false);
955 bool found = lyxfind::findNextChange(bv_);
957 owner_->getDialogs().show("changes");
961 ParIterator const end = buf->par_iterator_end();
962 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
963 it->untrackChanges();
964 buf->params.tracking_changes = false;
967 buf->redostack.clear();
971 // Doesn't go through lyxfunc, so we need to update the
972 // layout choice etc. ourselves
973 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
975 // e.g. Qt mouse press when no buffer
979 screen().hideCursor();
981 bool const res = dispatch(ev_in);
983 // see workAreaKeyPress
984 cursor_timeout.restart();
985 screen().showCursor(*bv_);
987 // FIXME: we should skip these when selecting
988 bv_->owner()->updateLayoutChoice();
989 bv_->owner()->updateToolbar();
992 // slight hack: this is only called currently when
993 // we clicked somewhere, so we force through the display
994 // of the new status here.
995 bv_->owner()->clearMessage();
1001 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1003 // Make sure that the cached BufferView is correct.
1004 FuncRequest ev = ev_in;
1007 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1008 << " action[" << ev.action << ']'
1009 << " arg[" << ev.argument << ']'
1010 << " x[" << ev.x << ']'
1011 << " y[" << ev.y << ']'
1012 << " button[" << ev.button() << ']'
1015 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1017 switch (ev.action) {
1019 case LFUN_SCROLL_INSET:
1020 // this is not handled here as this function is only active
1021 // if we have a locking_inset and that one is (or contains)
1025 case LFUN_FILE_INSERT:
1026 MenuInsertLyXFile(ev.argument);
1029 case LFUN_FILE_INSERT_ASCII_PARA:
1030 InsertAsciiFile(bv_, ev.argument, true);
1033 case LFUN_FILE_INSERT_ASCII:
1034 InsertAsciiFile(bv_, ev.argument, false);
1038 lang(bv_, ev.argument);
1040 owner_->view_state_changed();
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();
1078 case LFUN_UNDERLINE:
1080 owner_->view_state_changed();
1083 case LFUN_FONT_SIZE:
1084 fontSize(bv_, ev.argument);
1085 owner_->view_state_changed();
1088 case LFUN_FONT_STATE:
1089 owner_->getLyXFunc().setMessage(currentState(bv_));
1092 case LFUN_INSERT_LABEL: {
1093 // Try and generate a valid label
1094 string const contents = ev.argument.empty() ?
1095 getPossibleLabel(*bv_) : ev.argument;
1096 InsetCommandParams icp("label", contents);
1097 string data = InsetCommandMailer::params2string("label", icp);
1098 owner_->getDialogs().show("label", data, 0);
1102 case LFUN_BOOKMARK_SAVE:
1103 savePosition(strToUnsignedInt(ev.argument));
1106 case LFUN_BOOKMARK_GOTO:
1107 restorePosition(strToUnsignedInt(ev.argument));
1110 case LFUN_REF_GOTO: {
1111 string label = ev.argument;
1112 if (label.empty()) {
1114 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1116 label = inset->getContents();
1122 bv_->gotoLabel(label);
1126 // --- accented characters ---------------------------
1129 case LFUN_CIRCUMFLEX:
1139 case LFUN_SPECIAL_CARON:
1142 case LFUN_HUNG_UMLAUT:
1145 if (ev.argument.empty()) {
1147 owner_->getLyXFunc().handleKeyFunc(ev.action);
1149 owner_->getLyXFunc().handleKeyFunc(ev.action);
1150 owner_->getIntl().getTransManager()
1151 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1152 update(bv_->getLyXText(), BufferView::SELECT);
1156 case LFUN_MATH_MACRO:
1157 case LFUN_MATH_DELIM:
1158 case LFUN_INSERT_MATRIX:
1159 case LFUN_INSERT_MATH:
1160 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1161 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1162 case LFUN_MATH_MODE: // Open or create an inlined math inset
1166 case LFUN_INSET_APPLY: {
1167 string const name = ev.getArg(0);
1169 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1171 // This works both for 'original' and 'mathed' insets.
1172 // Note that the localDispatch performs updateInset
1174 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1175 inset->localDispatch(fr);
1177 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1183 case LFUN_INSET_INSERT: {
1184 Inset * inset = createInset(ev);
1185 if (inset && insertInset(inset)) {
1188 string const name = ev.getArg(0);
1189 if (name == "bibitem") {
1190 // We need to do a redraw because the maximum
1191 // InsetBibitem width could have changed
1192 #warning please check you mean repaint() not update(),
1193 #warning and whether the repaint() is needed at all
1203 case LFUN_FLOAT_LIST:
1204 if (tclass.floats().typeExist(ev.argument)) {
1205 Inset * inset = new InsetFloatList(ev.argument);
1206 if (!insertInset(inset, tclass.defaultLayoutName()))
1209 lyxerr << "Non-existent float type: "
1210 << ev.argument << endl;
1214 case LFUN_LAYOUT_PARAGRAPH: {
1215 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1220 params2string(*par, data);
1222 data = "show\n" + data;
1223 bv_->owner()->getDialogs().show("paragraph", data);
1227 case LFUN_PARAGRAPH_UPDATE: {
1228 if (!bv_->owner()->getDialogs().visible("paragraph"))
1230 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1235 params2string(*par, data);
1237 // Will the paragraph accept changes from the dialog?
1238 Inset * const inset = par->inInset();
1240 !(inset && inset->forceDefaultParagraphs(inset));
1242 data = "update " + tostr(accept) + '\n' + data;
1243 bv_->owner()->getDialogs().update("paragraph", data);
1247 case LFUN_PARAGRAPH_APPLY:
1248 setParagraphParams(*bv_, ev.argument);
1251 case LFUN_THESAURUS_ENTRY:
1253 string arg = ev.argument;
1256 arg = bv_->getLyXText()->selectionAsString(buffer_,
1260 if (arg.size() > 100 || arg.empty()) {
1261 // Get word or selection
1262 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1263 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1264 // FIXME: where is getLyXText()->unselect(bv_) ?
1268 bv_->owner()->getDialogs().show("thesaurus", arg);
1272 case LFUN_TRACK_CHANGES:
1276 case LFUN_MERGE_CHANGES:
1277 owner_->getDialogs().show("changes");
1280 case LFUN_ACCEPT_ALL_CHANGES: {
1281 update(BufferView::SELECT);
1282 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1283 #warning FIXME changes
1284 //moveCursorUpdate(false);
1286 while (lyxfind::findNextChange(bv_)) {
1287 bv_->getLyXText()->acceptChange();
1289 update(BufferView::SELECT);
1293 case LFUN_REJECT_ALL_CHANGES: {
1294 update(BufferView::SELECT);
1295 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1296 #warning FIXME changes
1297 //moveCursorUpdate(false);
1299 while (lyxfind::findNextChange(bv_)) {
1300 bv_->getLyXText()->rejectChange();
1302 update(BufferView::SELECT);
1306 case LFUN_ACCEPT_CHANGE: {
1307 bv_->getLyXText()->acceptChange();
1308 update(BufferView::SELECT);
1312 case LFUN_REJECT_CHANGE: {
1313 bv_->getLyXText()->rejectChange();
1314 update(BufferView::SELECT);
1318 case LFUN_UNKNOWN_ACTION:
1319 ev.errorMessage(N_("Unknown function!"));
1323 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1330 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1332 // if we are in a locking inset we should try to insert the
1333 // inset there otherwise this is a illegal function now
1334 if (bv_->theLockingInset()) {
1335 if (bv_->theLockingInset()->insetAllowed(inset))
1336 return bv_->theLockingInset()->insertInset(bv_, inset);
1340 // not quite sure if we want this...
1341 setCursorParUndo(bv_);
1344 beforeChange(bv_->text);
1345 if (!lout.empty()) {
1346 update(BufferView::SELECT);
1347 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1348 update(BufferView::SELECT);
1350 if (!bv_->text->cursor.par()->empty()) {
1351 bv_->text->cursorLeft(bv_);
1353 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1354 update(BufferView::SELECT);
1358 LyXTextClass const & tclass =
1359 buffer_->params.getLyXTextClass();
1360 bool hasLayout = tclass.hasLayout(lres);
1361 string lay = tclass.defaultLayoutName();
1363 if (hasLayout != false) {
1367 // layout not fount using default
1368 lay = tclass.defaultLayoutName();
1371 bv_->text->setLayout(lay);
1373 bv_->text->setParagraph(0, 0,
1375 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1380 update(BufferView::SELECT);
1383 bv_->text->insertInset(inset);
1384 update(BufferView::SELECT);
1391 void BufferView::Pimpl::updateInset(Inset * inset)
1393 if (!inset || !available())
1396 // first check for locking insets
1397 if (bv_->theLockingInset()) {
1398 if (bv_->theLockingInset() == inset) {
1399 if (bv_->text->updateInset(inset)) {
1404 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1405 if (bv_->text->updateInset(bv_->theLockingInset())) {
1413 // then check if the inset is a top_level inset (has no owner)
1414 // if yes do the update as always otherwise we have to update the
1415 // toplevel inset where this inset is inside
1416 Inset * tl_inset = inset;
1417 while (tl_inset->owner())
1418 tl_inset = tl_inset->owner();
1419 if (tl_inset == inset) {
1420 update(BufferView::UPDATE);
1421 if (bv_->text->updateInset(inset)) {
1422 update(BufferView::SELECT);
1425 } else if (static_cast<UpdatableInset *>(tl_inset)
1426 ->updateInsetInInset(bv_, inset))
1428 if (bv_->text->updateInset(tl_inset)) {