2 * \file BufferView_pimpl.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Asger Alstrup
7 * \author Alfredo Braustein
8 * \author Lars Gullik Bjønnes
9 * \author Jean-Marc Lasgouttes
10 * \author Angus Leeming
12 * \author André Pönitz
14 * \author Jürgen Vigna
16 * Full author contact details are available in file CREDITS.
21 #include "BufferView_pimpl.h"
23 #include "buffer_funcs.h"
24 #include "bufferlist.h"
25 #include "bufferparams.h"
28 #include "dispatchresult.h"
30 #include "FloatList.h"
31 #include "funcrequest.h"
34 #include "iterators.h"
35 #include "lyx_cb.h" // added for Dispatch functions
41 #include "lastfiles.h"
42 #include "paragraph.h"
43 #include "paragraph_funcs.h"
44 #include "ParagraphParameters.h"
48 #include "insets/insetfloatlist.h"
49 #include "insets/insetref.h"
51 #include "frontends/Alert.h"
52 #include "frontends/Dialogs.h"
53 #include "frontends/FileDialog.h"
54 #include "frontends/LyXView.h"
55 #include "frontends/LyXScreenFactory.h"
56 #include "frontends/screen.h"
57 #include "frontends/WorkArea.h"
58 #include "frontends/WorkAreaFactory.h"
60 #include "graphics/Previews.h"
62 #include "mathed/formulabase.h"
64 #include "support/filetools.h"
65 #include "support/globbing.h"
66 #include "support/path_defines.h"
67 #include "support/tostr.h"
69 #include <boost/bind.hpp>
71 using bv_funcs::currentState;
75 using lyx::support::AddPath;
76 using lyx::support::bformat;
77 using lyx::support::FileFilterList;
78 using lyx::support::FileSearch;
79 using lyx::support::IsDirWriteable;
80 using lyx::support::MakeDisplayPath;
81 using lyx::support::strToUnsignedInt;
82 using lyx::support::system_lyxdir;
90 extern BufferList bufferlist;
95 unsigned int const saved_positions_num = 20;
97 // All the below connection objects are needed because of a bug in some
98 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
99 // to these connections we avoid a segfault upon startup, and also at exit.
102 boost::signals::connection dispatchcon;
103 boost::signals::connection timecon;
104 boost::signals::connection doccon;
105 boost::signals::connection resizecon;
106 boost::signals::connection kpresscon;
107 boost::signals::connection selectioncon;
108 boost::signals::connection lostcon;
114 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
115 int xpos, int ypos, int width, int height)
116 : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
117 using_xterm_cursor(false), cursor_(bv)
119 xsel_cache_.set = false;
121 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
122 screen_.reset(LyXScreenFactory::create(workarea()));
125 doccon = workarea().scrollDocView
126 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
127 resizecon = workarea().workAreaResize
128 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
129 dispatchcon = workarea().dispatch
130 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
131 kpresscon = workarea().workAreaKeyPress
132 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
133 selectioncon = workarea().selectionRequested
134 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
135 lostcon = workarea().selectionLost
136 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
138 timecon = cursor_timeout.timeout
139 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
140 cursor_timeout.start();
141 saved_positions.resize(saved_positions_num);
145 void BufferView::Pimpl::addError(ErrorItem const & ei)
147 errorlist_.push_back(ei);
151 void BufferView::Pimpl::showReadonly(bool)
153 owner_->updateWindowTitle();
154 owner_->getDialogs().updateBufferDependent(false);
158 void BufferView::Pimpl::connectBuffer(Buffer & buf)
160 if (errorConnection_.connected())
164 buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
166 buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
168 buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
170 buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
172 buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
173 readonlyConnection_ =
174 buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
176 buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
180 void BufferView::Pimpl::disconnectBuffer()
182 errorConnection_.disconnect();
183 messageConnection_.disconnect();
184 busyConnection_.disconnect();
185 titleConnection_.disconnect();
186 timerConnection_.disconnect();
187 readonlyConnection_.disconnect();
188 closingConnection_.disconnect();
192 bool BufferView::Pimpl::newFile(string const & filename,
193 string const & tname,
196 Buffer * b = ::newFile(filename, tname, isNamed);
202 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
204 // get absolute path of file and add ".lyx" to the filename if
206 string s = FileSearch(string(), filename, "lyx");
208 bool const found = !s.empty();
213 // file already open?
214 if (bufferlist.exists(s)) {
215 string const file = MakeDisplayPath(s, 20);
216 string text = bformat(_("The document %1$s is already "
217 "loaded.\n\nDo you want to revert "
218 "to the saved version?"), file);
219 int const ret = Alert::prompt(_("Revert to saved document?"),
220 text, 0, 1, _("&Revert"), _("&Switch to document"));
223 buffer(bufferlist.getBuffer(s));
226 // FIXME: should be LFUN_REVERT
227 if (!bufferlist.close(bufferlist.getBuffer(s), false))
229 // Fall through to new load. (Asger)
236 b = bufferlist.newBuffer(s);
238 if (!::loadLyXFile(b, s)) {
239 bufferlist.release(b);
243 string text = bformat(_("The document %1$s does not yet "
244 "exist.\n\nDo you want to create "
245 "a new document?"), s);
246 int const ret = Alert::prompt(_("Create new document?"),
247 text, 0, 1, _("&Create"), _("Cancel"));
250 b = ::newFile(s, string(), true);
256 bv_->showErrorList(_("Parse"));
259 LyX::ref().lastfiles().newFile(b->fileName());
265 WorkArea & BufferView::Pimpl::workarea() const
267 return *workarea_.get();
271 LyXScreen & BufferView::Pimpl::screen() const
273 return *screen_.get();
277 Painter & BufferView::Pimpl::painter() const
279 return workarea().getPainter();
283 void BufferView::Pimpl::top_y(int y)
289 int BufferView::Pimpl::top_y() const
295 void BufferView::Pimpl::buffer(Buffer * b)
297 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
301 //delete bv_->text();
305 // set current buffer
310 // if we're quitting lyx, don't bother updating stuff
314 // if we are closing the buffer, use the first buffer as current
316 buffer_ = bufferlist.first();
319 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
320 connectBuffer(*buffer_);
322 buffer_->text().init(bv_);
323 buffer_->text().textwidth_ = workarea().workWidth();
324 buffer_->text().fullRebreak();
326 // If we don't have a text object for this, we make one
327 if (bv_->text() == 0)
328 resizeCurrentBuffer();
330 // FIXME: needed when ?
333 // Buffer-dependent dialogs should be updated or
334 // hidden. This should go here because some dialogs (eg ToC)
335 // require bv_->text.
336 owner_->getDialogs().updateBufferDependent(true);
338 lyxerr[Debug::INFO] << " No Buffer!" << endl;
339 owner_->getDialogs().hideBufferDependent();
344 owner_->updateMenubar();
345 owner_->updateToolbar();
346 owner_->updateLayoutChoice();
347 owner_->updateWindowTitle();
349 // Don't forget to update the Layout
351 owner_->setLayout(bv_->text()->cursorPar()->layout()->name());
353 if (lyx::graphics::Previews::activated() && buffer_)
354 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
358 bool BufferView::Pimpl::fitCursor()
360 lyxerr << "BufferView::Pimpl::fitCursor." << endl;
361 if (screen().fitCursor(bv_)) {
369 void BufferView::Pimpl::redoCurrentBuffer()
371 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
372 if (buffer_ && bv_->text()) {
373 resizeCurrentBuffer();
375 owner_->updateLayoutChoice();
380 void BufferView::Pimpl::resizeCurrentBuffer()
382 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
385 int selstartpar = -1;
389 pos_type selstartpos = 0;
390 pos_type selendpos = 0;
392 bool mark_set = false;
396 owner_->message(_("Formatting document..."));
398 LyXText * text = bv_->text();
399 lyxerr << "### resizeCurrentBuffer: text " << text << endl;
403 LCursor & cur = bv_->cursor();
406 selstartpar = cur.selStart().par();
407 selstartpos = cur.selStart().pos();
408 selendpar = cur.selEnd().par();
409 selendpos = cur.selEnd().pos();
410 sel = cur.selection();
411 mark_set = cur.mark();
412 text->textwidth_ = bv_->workWidth();
417 cur.selection() = true;
418 // At this point just to avoid the Delete-Empty-Paragraph-
419 // Mechanism when setting the cursor.
420 cur.mark() = mark_set;
422 text->setCursor(selstartpar, selstartpos);
424 text->setCursor(selendpar, selendpos);
426 text->setCursor(par, pos);
428 text->setCursor(par, pos);
430 cur.selection() = false;
439 // reset the "Formatting..." message
440 owner_->clearMessage();
446 void BufferView::Pimpl::updateScrollbar()
449 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
450 workarea().setScrollbarParams(0, 0, 0);
454 LyXText const & t = *bv_->text();
456 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
457 << top_y() << ", default height " << defaultRowHeight() << endl;
459 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
463 void BufferView::Pimpl::scrollDocView(int value)
465 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
470 screen().hideCursor();
473 screen().redraw(*bv_);
475 if (!lyxrc.cursor_follows_scrollbar)
478 int const height = defaultRowHeight();
479 int const first = top_y() + height;
480 int const last = top_y() + workarea().workHeight() - height;
482 LyXText * text = bv_->text();
483 if (text->cursorY() < first)
484 text->setCursorFromCoordinates(0, first);
485 else if (text->cursorY() > last)
486 text->setCursorFromCoordinates(0, last);
488 owner_->updateLayoutChoice();
492 void BufferView::Pimpl::scroll(int lines)
497 LyXText const * t = bv_->text();
498 int const line_height = defaultRowHeight();
500 // The new absolute coordinate
501 int new_top_y = top_y() + lines * line_height;
503 // Restrict to a valid value
504 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
505 new_top_y = std::max(0, new_top_y);
507 scrollDocView(new_top_y);
509 // Update the scrollbar.
510 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
514 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
515 key_modifier::state state)
517 bv_->owner()->getLyXFunc().processKeySym(key, state);
519 /* This is perhaps a bit of a hack. When we move
520 * around, or type, it's nice to be able to see
521 * the cursor immediately after the keypress. So
522 * we reset the toggle timeout and force the visibility
523 * of the cursor. Note we cannot do this inside
524 * dispatch() itself, because that's called recursively.
527 cursor_timeout.restart();
528 screen().showCursor(*bv_);
533 void BufferView::Pimpl::selectionRequested()
540 LCursor & cur = bv_->cursor();
542 if (!cur.selection()) {
543 xsel_cache_.set = false;
547 if (!xsel_cache_.set ||
548 cur.cursor_.back() != xsel_cache_.cursor ||
549 cur.anchor_.back() != xsel_cache_.anchor)
551 xsel_cache_.cursor = cur.cursor_.back();
552 xsel_cache_.anchor = cur.anchor_.back();
553 xsel_cache_.set = cur.selection();
554 sel = bv_->getLyXText()->selectionAsString(*bv_->buffer(), false);
556 workarea().putClipboard(sel);
561 void BufferView::Pimpl::selectionLost()
564 screen().hideCursor();
565 bv_->cursor().clearSelection();
566 xsel_cache_.set = false;
571 void BufferView::Pimpl::workAreaResize()
573 static int work_area_width;
574 static int work_area_height;
576 bool const widthChange = workarea().workWidth() != work_area_width;
577 bool const heightChange = workarea().workHeight() != work_area_height;
579 // update from work area
580 work_area_width = workarea().workWidth();
581 work_area_height = workarea().workHeight();
585 // The visible LyXView need a resize
586 resizeCurrentBuffer();
590 if (widthChange || heightChange)
593 // always make sure that the scrollbar is sane.
595 owner_->updateLayoutChoice();
599 void BufferView::Pimpl::update()
601 //lyxerr << "BufferView::update()" << endl;
602 // fix cursor coordinate cache in case something went wrong
604 // check needed to survive LyX startup
605 if (bv_->getLyXText()) {
606 // update all 'visible' paragraphs
607 ParagraphList::iterator beg;
608 ParagraphList::iterator end;
609 getParsInRange(buffer_->paragraphs(),
610 top_y(), top_y() + workarea().workHeight(),
612 bv_->text()->redoParagraphs(beg, end);
615 screen().redraw(*bv_);
619 // Callback for cursor timer
620 void BufferView::Pimpl::cursorToggle()
623 cursor_timeout.restart();
627 screen().toggleCursor(*bv_);
628 cursor_timeout.restart();
632 bool BufferView::Pimpl::available() const
634 return buffer_ && bv_->text();
638 Change const BufferView::Pimpl::getCurrentChange()
640 if (!bv_->buffer()->params().tracking_changes)
641 return Change(Change::UNCHANGED);
643 LyXText * text = bv_->getLyXText();
644 LCursor & cur = bv_->cursor();
646 if (!cur.selection())
647 return Change(Change::UNCHANGED);
649 return text->getPar(cur.selStart())
650 ->lookupChangeFull(cur.selStart().pos());
654 void BufferView::Pimpl::savePosition(unsigned int i)
656 if (i >= saved_positions_num)
658 saved_positions[i] = Position(buffer_->fileName(),
659 bv_->text()->cursorPar()->id(),
660 bv_->text()->cursor().pos());
662 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
666 void BufferView::Pimpl::restorePosition(unsigned int i)
668 if (i >= saved_positions_num)
671 string const fname = saved_positions[i].filename;
673 bv_->cursor().clearSelection();
675 if (fname != buffer_->fileName()) {
677 if (bufferlist.exists(fname))
678 b = bufferlist.getBuffer(fname);
680 b = bufferlist.newBuffer(fname);
681 ::loadLyXFile(b, fname); // don't ask, just load it
687 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
688 if (par == buffer_->par_iterator_end())
691 bv_->text()->setCursor(par.pit(),
692 min(par->size(), saved_positions[i].par_pos));
695 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
699 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
701 return i < saved_positions_num && !saved_positions[i].filename.empty();
705 void BufferView::Pimpl::switchKeyMap()
707 if (!lyxrc.rtl_support)
710 Intl & intl = owner_->getIntl();
711 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
712 if (intl.keymap == Intl::PRIMARY)
715 if (intl.keymap == Intl::SECONDARY)
721 void BufferView::Pimpl::center()
723 LyXText * text = bv_->text();
725 bv_->cursor().clearSelection();
726 int const half_height = workarea().workHeight() / 2;
727 int new_y = std::max(0, text->cursorY() - half_height);
729 // FIXME: look at this comment again ...
730 // This updates top_y() but means the fitCursor() call
731 // from the update(FITCUR) doesn't realise that we might
732 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
733 // the scrollbar to be updated as it should, so we have
734 // to do it manually. Any operation that does a center()
735 // and also might have moved top_y() must make sure to call
736 // updateScrollbar() currently. Never mind that this is a
737 // pretty obfuscated way of updating t->top_y()
742 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
744 workarea().putClipboard(stuff);
748 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
751 CursorSlice cursor = bv_->getLyXText()->cursor;
752 Buffer::inset_iterator it =
753 find_if(Buffer::inset_iterator(
754 cursorPar(), cursor().pos()),
755 buffer_->inset_iterator_end(),
756 lyx::compare_memfun(&Inset::lyxCode, code));
757 return it != buffer_->inset_iterator_end() ? (*it) : 0;
759 // Ok, this is a little bit too brute force but it
760 // should work for now. Better infrastructure is coming. (Lgb)
762 Buffer * b = bv_->buffer();
763 LyXText * text = bv_->getLyXText();
765 Buffer::inset_iterator beg = b->inset_iterator_begin();
766 Buffer::inset_iterator end = b->inset_iterator_end();
768 bool cursor_par_seen = false;
770 for (; beg != end; ++beg) {
771 if (beg.getPar() == text->cursorPar()) {
772 cursor_par_seen = true;
774 if (cursor_par_seen) {
775 if (beg.getPar() == text->cursorPar()
776 && beg.getPos() >= text->cursor().pos()) {
778 } else if (beg.getPar() != text->cursorPar()) {
785 // Now find the first inset that matches code.
786 for (; beg != end; ++beg) {
787 if (beg->lyxCode() == code) {
797 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
799 string filename = filen;
801 if (filename.empty()) {
802 // Launch a file browser
803 string initpath = lyxrc.document_path;
806 string const trypath = owner_->buffer()->filePath();
807 // If directory is writeable, use this as default.
808 if (IsDirWriteable(trypath))
812 FileDialog fileDlg(_("Select LyX document to insert"),
814 make_pair(string(_("Documents|#o#O")),
815 string(lyxrc.document_path)),
816 make_pair(string(_("Examples|#E#e")),
817 string(AddPath(system_lyxdir(), "examples"))));
819 FileDialog::Result result =
820 fileDlg.open(initpath,
821 FileFilterList(_("LyX Documents (*.lyx)")),
824 if (result.first == FileDialog::Later)
827 filename = result.second;
829 // check selected filename
830 if (filename.empty()) {
831 owner_->message(_("Canceled."));
836 // get absolute path of file and add ".lyx" to the filename if
838 filename = FileSearch(string(), filename, "lyx");
840 string const disp_fn = MakeDisplayPath(filename);
841 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
842 if (bv_->insertLyXFile(filename))
843 owner_->message(bformat(_("Document %1$s inserted."),
846 owner_->message(bformat(_("Could not insert document %1$s"),
851 void BufferView::Pimpl::trackChanges()
853 Buffer * buf(bv_->buffer());
854 bool const tracking(buf->params().tracking_changes);
857 ParIterator const end = buf->par_iterator_end();
858 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
860 buf->params().tracking_changes = true;
862 // we cannot allow undos beyond the freeze point
863 buf->undostack().clear();
866 bv_->text()->setCursor(0, 0);
867 #warning changes FIXME
868 bool found = lyx::find::findNextChange(bv_);
870 owner_->getDialogs().show("changes");
874 ParIterator const end = buf->par_iterator_end();
875 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
876 it->untrackChanges();
877 buf->params().tracking_changes = false;
880 buf->redostack().clear();
884 CursorBase theTempCursor;
888 InsetOld * insetFromCoords(BufferView * bv, int x, int y)
890 lyxerr << "insetFromCoords" << endl;
891 LyXText * text = bv->text();
892 InsetOld * inset = 0;
893 theTempCursor.clear();
895 InsetOld * const inset_hit = text->checkInsetHit(x, y);
897 lyxerr << "no further inset hit" << endl;
901 if (!inset->descendable()) {
902 lyxerr << "not descendable" << endl;
905 int const cell = inset->getCell(x, y + bv->top_y());
908 text = inset_hit->getText(cell);
909 lyxerr << "Hit inset: " << inset << " at x: " << x
910 << " text: " << text << " y: " << y << endl;
911 theTempCursor.push_back(CursorSlice(inset));
913 //lyxerr << "theTempCursor: " << theTempCursor << endl;
920 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd)
922 LCursor & cur = bv_->cursor();
923 switch (cmd.action) {
924 case LFUN_MOUSE_MOTION: {
927 FuncRequest cmd1 = cmd;
928 InsetBase * inset = cur.inset();
931 cmd1.x -= inset->x();
932 cmd1.y -= inset->y();
933 res = inset->dispatch(cur, cmd1);
935 cmd1.y += bv_->top_y();
936 res = cur.innerText()->dispatch(cur, cmd1);
939 if (bv_->fitCursor() || res.update()) {
947 case LFUN_MOUSE_PRESS:
948 case LFUN_MOUSE_RELEASE:
949 case LFUN_MOUSE_DOUBLE:
950 case LFUN_MOUSE_TRIPLE: {
951 // We pass those directly to the Bufferview, since
952 // otherwise selection handling breaks down
954 // Doesn't go through lyxfunc, so we need to update
955 // the layout choice etc. ourselves
957 // e.g. Qt mouse press when no buffer
961 screen().hideCursor();
963 // either the inset under the cursor or the surrounding LyXText will
964 // handle this event.
966 // built temporary path to inset
967 InsetOld * inset = insetFromCoords(bv_, cmd.x, cmd.y);
970 // try to dispatch to that inset
972 FuncRequest cmd2 = cmd;
973 lyxerr << "dispatching action " << cmd2.action
974 << " to inset " << inset << endl;
975 cmd2.x -= inset->x();
976 cmd2.y -= inset->y();
977 res = inset->dispatch(cur, cmd2);
988 theTempCursor.pop_back();
989 cur.cursor_ = theTempCursor;
991 ->setCursorFromCoordinates(cmd.x, top_y() + cmd.y);
992 if (bv_->fitCursor())
996 lyxerr << "not dispatched by inner inset val: " << res.val() << endl;
1001 // otherwise set cursor to surrounding LyXText
1002 if (!res.dispatched()) {
1003 //lyxerr << "temp cursor is: " << theTempCursor << endl;
1004 //lyxerr << "dispatching " << cmd
1005 // << " to surrounding LyXText "
1006 // << theTempCursor.innerText() << endl;
1007 cur.cursor_ = theTempCursor;
1008 FuncRequest cmd1 = cmd;
1009 cmd1.y += bv_->top_y();
1010 res = cur.innerText()->dispatch(cur, cmd1);
1011 if (bv_->fitCursor() || res.update())
1014 //return DispatchResult(true, true);
1016 // see workAreaKeyPress
1017 cursor_timeout.restart();
1018 screen().showCursor(*bv_);
1020 // skip these when selecting
1021 if (cmd.action != LFUN_MOUSE_MOTION) {
1022 owner_->updateLayoutChoice();
1023 owner_->updateToolbar();
1026 // slight hack: this is only called currently when we
1027 // clicked somewhere, so we force through the display
1028 // of the new status here.
1029 owner_->clearMessage();
1034 owner_->dispatch(cmd);
1040 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
1042 // Make sure that the cached BufferView is correct.
1043 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1044 << " action[" << ev.action << ']'
1045 << " arg[" << ev.argument << ']'
1046 << " x[" << ev.x << ']'
1047 << " y[" << ev.y << ']'
1048 << " button[" << ev.button() << ']'
1051 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1052 LCursor & cur = bv_->cursor();
1054 switch (ev.action) {
1056 case LFUN_SCROLL_INSET:
1057 // this is not handled here as this function is only active
1058 // if we have a locking_inset and that one is (or contains)
1062 case LFUN_FILE_INSERT:
1063 MenuInsertLyXFile(ev.argument);
1066 case LFUN_FILE_INSERT_ASCII_PARA:
1067 InsertAsciiFile(bv_, ev.argument, true);
1070 case LFUN_FILE_INSERT_ASCII:
1071 InsertAsciiFile(bv_, ev.argument, false);
1074 case LFUN_FONT_STATE:
1075 owner_->getLyXFunc().setMessage(currentState(bv_));
1078 case LFUN_INSERT_LABEL: {
1079 // Try and generate a valid label
1080 string const contents = ev.argument.empty() ?
1081 getPossibleLabel(*bv_) : ev.argument;
1082 InsetCommandParams icp("label", contents);
1083 string data = InsetCommandMailer::params2string("label", icp);
1084 owner_->getDialogs().show("label", data, 0);
1088 case LFUN_BOOKMARK_SAVE:
1089 savePosition(strToUnsignedInt(ev.argument));
1092 case LFUN_BOOKMARK_GOTO:
1093 restorePosition(strToUnsignedInt(ev.argument));
1096 case LFUN_REF_GOTO: {
1097 string label = ev.argument;
1098 if (label.empty()) {
1100 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1102 label = inset->getContents();
1108 bv_->gotoLabel(label);
1112 // --- accented characters ---------------------------
1115 case LFUN_CIRCUMFLEX:
1125 case LFUN_SPECIAL_CARON:
1128 case LFUN_HUNG_UMLAUT:
1131 if (ev.argument.empty()) {
1133 owner_->getLyXFunc().handleKeyFunc(ev.action);
1135 owner_->getLyXFunc().handleKeyFunc(ev.action);
1136 owner_->getIntl().getTransManager()
1137 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1142 case LFUN_MATH_MACRO:
1143 case LFUN_MATH_DELIM:
1144 case LFUN_INSERT_MATRIX:
1145 case LFUN_INSERT_MATH:
1146 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1147 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1148 case LFUN_MATH_MODE: // Open or create an inlined math inset
1149 mathDispatch(cur, ev);
1152 case LFUN_INSET_INSERT: {
1154 BOOST_ASSERT(false);
1155 InsetOld * inset = createInset(bv_, ev);
1156 if (!inset || !insertInset(inset))
1161 case LFUN_FLOAT_LIST:
1162 if (tclass.floats().typeExist(ev.argument)) {
1163 InsetOld * inset = new InsetFloatList(ev.argument);
1164 if (!insertInset(inset, tclass.defaultLayoutName()))
1167 lyxerr << "Non-existent float type: "
1168 << ev.argument << endl;
1172 case LFUN_LAYOUT_PARAGRAPH: {
1174 params2string(*bv_->getLyXText()->cursorPar(), data);
1175 data = "show\n" + data;
1176 bv_->owner()->getDialogs().show("paragraph", data);
1180 case LFUN_PARAGRAPH_UPDATE:
1181 updateParagraphDialog();
1184 case LFUN_PARAGRAPH_APPLY:
1185 setParagraphParams(*bv_, ev.argument);
1188 case LFUN_THESAURUS_ENTRY: {
1189 string arg = ev.argument;
1192 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1196 if (arg.size() > 100 || arg.empty()) {
1197 // Get word or selection
1198 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1199 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1200 // FIXME: where is getLyXText()->unselect(bv_) ?
1204 bv_->owner()->getDialogs().show("thesaurus", arg);
1208 case LFUN_TRACK_CHANGES:
1212 case LFUN_MERGE_CHANGES:
1213 owner_->getDialogs().show("changes");
1216 case LFUN_ACCEPT_ALL_CHANGES: {
1217 bv_->text()->setCursor(0, 0);
1218 #warning FIXME changes
1219 while (lyx::find::findNextChange(bv_))
1220 bv_->getLyXText()->acceptChange();
1225 case LFUN_REJECT_ALL_CHANGES: {
1226 bv_->text()->setCursor(0, 0);
1227 #warning FIXME changes
1228 while (lyx::find::findNextChange(bv_))
1229 bv_->getLyXText()->rejectChange();
1234 case LFUN_ACCEPT_CHANGE: {
1235 bv_->getLyXText()->acceptChange();
1240 case LFUN_REJECT_CHANGE: {
1241 bv_->getLyXText()->rejectChange();
1246 case LFUN_WORD_FIND:
1247 lyx::find::find(bv_, ev);
1250 case LFUN_WORD_REPLACE:
1251 lyx::find::replace(bv_, ev);
1255 cur.clearSelection();
1258 ev.message(N_("Mark off"));
1262 cur.clearSelection();
1266 ev.message(N_("Mark on"));
1270 cur.clearSelection();
1272 ev.message(N_("Mark removed"));
1275 ev.message(N_("Mark set"));
1281 case LFUN_UNKNOWN_ACTION:
1282 ev.errorMessage(N_("Unknown function!"));
1286 return cur.dispatch(ev).dispatched();
1293 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1295 // not quite sure if we want this...
1296 bv_->text()->recUndo(bv_->text()->cursor().par());
1299 bv_->cursor().clearSelection();
1300 if (!lout.empty()) {
1301 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1303 if (!bv_->text()->cursorPar()->empty()) {
1304 bv_->text()->cursorLeft(bv_);
1305 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1309 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1310 bool hasLayout = tclass.hasLayout(lres);
1312 bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
1313 bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1315 bv_->cursor().innerText()->insertInset(inset);
1321 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1322 string const & from, string const & to)
1324 bool need_update = false;
1325 CursorSlice cur = bv_->text()->cursor();
1327 ParIterator end = bv_->buffer()->par_iterator_end();
1328 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1330 bool changed_inset = false;
1331 for (InsetList::iterator it2 = it->insetlist.begin();
1332 it2 != it->insetlist.end(); ++it2) {
1333 if (it2->inset->lyxCode() == code) {
1334 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1335 if (inset->getContents() == from) {
1336 inset->setContents(to);
1337 changed_inset = true;
1341 if (changed_inset) {
1346 // The test it.size() == 1 was needed to prevent crashes.
1347 // How to set the cursor correctly when it.size() > 1 ??
1348 if (it.size() == 1) {
1349 bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
1350 bv_->text()->redoParagraph(bv_->text()->cursorPar());
1354 bv_->text()->setCursorIntern(cur.par(), cur.pos());
1359 void BufferView::Pimpl::updateParagraphDialog()
1361 if (!bv_->owner()->getDialogs().visible("paragraph"))
1363 Paragraph const & par = *bv_->getLyXText()->cursorPar();
1365 params2string(par, data);
1367 // Will the paragraph accept changes from the dialog?
1368 InsetOld * const inset = par.inInset();
1370 !(inset && inset->forceDefaultParagraphs(inset));
1372 data = "update " + tostr(accept) + '\n' + data;
1373 bv_->owner()->getDialogs().update("paragraph", data);