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 Braunstein
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 "support/filetools.h"
63 #include "support/globbing.h"
64 #include "support/path_defines.h"
65 #include "support/tostr.h"
67 #include <boost/bind.hpp>
69 using bv_funcs::currentState;
73 using lyx::support::AddPath;
74 using lyx::support::bformat;
75 using lyx::support::FileFilterList;
76 using lyx::support::FileSearch;
77 using lyx::support::IsDirWriteable;
78 using lyx::support::MakeDisplayPath;
79 using lyx::support::strToUnsignedInt;
80 using lyx::support::system_lyxdir;
88 extern BufferList bufferlist;
93 unsigned int const saved_positions_num = 20;
95 // All the below connection objects are needed because of a bug in some
96 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
97 // to these connections we avoid a segfault upon startup, and also at exit.
100 boost::signals::connection dispatchcon;
101 boost::signals::connection timecon;
102 boost::signals::connection doccon;
103 boost::signals::connection resizecon;
104 boost::signals::connection kpresscon;
105 boost::signals::connection selectioncon;
106 boost::signals::connection lostcon;
112 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
113 int xpos, int ypos, int width, int height)
114 : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
115 using_xterm_cursor(false), cursor_(bv)
117 xsel_cache_.set = false;
119 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
120 screen_.reset(LyXScreenFactory::create(workarea()));
123 doccon = workarea().scrollDocView
124 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
125 resizecon = workarea().workAreaResize
126 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
127 dispatchcon = workarea().dispatch
128 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
129 kpresscon = workarea().workAreaKeyPress
130 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
131 selectioncon = workarea().selectionRequested
132 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
133 lostcon = workarea().selectionLost
134 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
136 timecon = cursor_timeout.timeout
137 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
138 cursor_timeout.start();
139 saved_positions.resize(saved_positions_num);
143 void BufferView::Pimpl::addError(ErrorItem const & ei)
145 errorlist_.push_back(ei);
149 void BufferView::Pimpl::showReadonly(bool)
151 owner_->updateWindowTitle();
152 owner_->getDialogs().updateBufferDependent(false);
156 void BufferView::Pimpl::connectBuffer(Buffer & buf)
158 if (errorConnection_.connected())
162 buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
164 buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
166 buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
168 buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
170 buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
171 readonlyConnection_ =
172 buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
174 buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
178 void BufferView::Pimpl::disconnectBuffer()
180 errorConnection_.disconnect();
181 messageConnection_.disconnect();
182 busyConnection_.disconnect();
183 titleConnection_.disconnect();
184 timerConnection_.disconnect();
185 readonlyConnection_.disconnect();
186 closingConnection_.disconnect();
190 bool BufferView::Pimpl::newFile(string const & filename,
191 string const & tname,
194 Buffer * b = ::newFile(filename, tname, isNamed);
200 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
202 // get absolute path of file and add ".lyx" to the filename if
204 string s = FileSearch(string(), filename, "lyx");
206 bool const found = !s.empty();
211 // file already open?
212 if (bufferlist.exists(s)) {
213 string const file = MakeDisplayPath(s, 20);
214 string text = bformat(_("The document %1$s is already "
215 "loaded.\n\nDo you want to revert "
216 "to the saved version?"), file);
217 int const ret = Alert::prompt(_("Revert to saved document?"),
218 text, 0, 1, _("&Revert"), _("&Switch to document"));
221 buffer(bufferlist.getBuffer(s));
224 // FIXME: should be LFUN_REVERT
225 if (!bufferlist.close(bufferlist.getBuffer(s), false))
227 // Fall through to new load. (Asger)
234 b = bufferlist.newBuffer(s);
236 if (!::loadLyXFile(b, s)) {
237 bufferlist.release(b);
241 string text = bformat(_("The document %1$s does not yet "
242 "exist.\n\nDo you want to create "
243 "a new document?"), s);
244 int const ret = Alert::prompt(_("Create new document?"),
245 text, 0, 1, _("&Create"), _("Cancel"));
248 b = ::newFile(s, string(), true);
254 bv_->showErrorList(_("Parse"));
257 LyX::ref().lastfiles().newFile(b->fileName());
263 WorkArea & BufferView::Pimpl::workarea() const
265 return *workarea_.get();
269 LyXScreen & BufferView::Pimpl::screen() const
271 return *screen_.get();
275 Painter & BufferView::Pimpl::painter() const
277 return workarea().getPainter();
281 void BufferView::Pimpl::top_y(int y)
287 int BufferView::Pimpl::top_y() const
293 void BufferView::Pimpl::buffer(Buffer * b)
295 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
299 //delete bv_->text();
306 // set current buffer
311 // if we're quitting lyx, don't bother updating stuff
315 // if we are closing the buffer, use the first buffer as current
317 buffer_ = bufferlist.first();
320 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
321 connectBuffer(*buffer_);
323 buffer_->text().init(bv_);
324 buffer_->text().textwidth_ = workarea().workWidth();
325 buffer_->text().fullRebreak();
327 // If we don't have a text object for this, we make one
328 if (bv_->text() == 0)
329 resizeCurrentBuffer();
331 // FIXME: needed when ?
334 // Buffer-dependent dialogs should be updated or
335 // hidden. This should go here because some dialogs (eg ToC)
336 // require bv_->text.
337 owner_->getDialogs().updateBufferDependent(true);
339 lyxerr[Debug::INFO] << " No Buffer!" << endl;
340 owner_->getDialogs().hideBufferDependent();
345 owner_->updateMenubar();
346 owner_->updateToolbar();
347 owner_->updateLayoutChoice();
348 owner_->updateWindowTitle();
350 // Don't forget to update the Layout
352 owner_->setLayout(bv_->text()->cursorPar()->layout()->name());
354 if (lyx::graphics::Previews::activated() && buffer_)
355 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
359 bool BufferView::Pimpl::fitCursor()
361 lyxerr << "BufferView::Pimpl::fitCursor." << endl;
362 if (screen().fitCursor(bv_)) {
370 void BufferView::Pimpl::redoCurrentBuffer()
372 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
373 if (buffer_ && bv_->text()) {
374 resizeCurrentBuffer();
376 owner_->updateLayoutChoice();
381 void BufferView::Pimpl::resizeCurrentBuffer()
383 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
386 int selstartpar = -1;
390 pos_type selstartpos = 0;
391 pos_type selendpos = 0;
393 bool mark_set = false;
397 owner_->message(_("Formatting document..."));
399 LyXText * text = bv_->text();
400 lyxerr << "### resizeCurrentBuffer: text " << text << endl;
404 LCursor & cur = bv_->cursor();
407 selstartpar = cur.selBegin().par();
408 selstartpos = cur.selBegin().pos();
409 selendpar = cur.selEnd().par();
410 selendpos = cur.selEnd().pos();
411 sel = cur.selection();
412 mark_set = cur.mark();
413 text->textwidth_ = bv_->workWidth();
418 cur.selection() = true;
419 // At this point just to avoid the Delete-Empty-Paragraph-
420 // Mechanism when setting the cursor.
421 cur.mark() = mark_set;
423 text->setCursor(selstartpar, selstartpos);
425 text->setCursor(selendpar, selendpos);
427 text->setCursor(par, pos);
429 text->setCursor(par, pos);
431 cur.selection() = false;
440 // reset the "Formatting..." message
441 owner_->clearMessage();
447 void BufferView::Pimpl::updateScrollbar()
450 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
451 workarea().setScrollbarParams(0, 0, 0);
455 LyXText const & t = *bv_->text();
457 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
458 << top_y() << ", default height " << defaultRowHeight() << endl;
460 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
464 void BufferView::Pimpl::scrollDocView(int value)
466 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
471 screen().hideCursor();
474 screen().redraw(*bv_);
476 if (!lyxrc.cursor_follows_scrollbar)
479 int const height = defaultRowHeight();
480 int const first = top_y() + height;
481 int const last = top_y() + workarea().workHeight() - height;
483 LyXText * text = bv_->text();
484 int y = text->cursorY(bv_->cursor().cursor_.front());
486 text->setCursorFromCoordinates(0, first);
488 text->setCursorFromCoordinates(0, last);
490 owner_->updateLayoutChoice();
494 void BufferView::Pimpl::scroll(int lines)
499 LyXText const * t = bv_->text();
500 int const line_height = defaultRowHeight();
502 // The new absolute coordinate
503 int new_top_y = top_y() + lines * line_height;
505 // Restrict to a valid value
506 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
507 new_top_y = std::max(0, new_top_y);
509 scrollDocView(new_top_y);
511 // Update the scrollbar.
512 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
516 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
517 key_modifier::state state)
519 bv_->owner()->getLyXFunc().processKeySym(key, state);
521 /* This is perhaps a bit of a hack. When we move
522 * around, or type, it's nice to be able to see
523 * the cursor immediately after the keypress. So
524 * we reset the toggle timeout and force the visibility
525 * of the cursor. Note we cannot do this inside
526 * dispatch() itself, because that's called recursively.
529 cursor_timeout.restart();
530 screen().showCursor(*bv_);
535 void BufferView::Pimpl::selectionRequested()
542 LCursor & cur = bv_->cursor();
544 if (!cur.selection()) {
545 xsel_cache_.set = false;
549 if (!xsel_cache_.set ||
550 cur.cursor_.back() != xsel_cache_.cursor ||
551 cur.anchor_.back() != xsel_cache_.anchor)
553 xsel_cache_.cursor = cur.cursor_.back();
554 xsel_cache_.anchor = cur.anchor_.back();
555 xsel_cache_.set = cur.selection();
556 sel = bv_->getLyXText()->selectionAsString(*bv_->buffer(), false);
558 workarea().putClipboard(sel);
563 void BufferView::Pimpl::selectionLost()
566 screen().hideCursor();
567 bv_->cursor().clearSelection();
568 xsel_cache_.set = false;
573 void BufferView::Pimpl::workAreaResize()
575 static int work_area_width;
576 static int work_area_height;
578 bool const widthChange = workarea().workWidth() != work_area_width;
579 bool const heightChange = workarea().workHeight() != work_area_height;
581 // update from work area
582 work_area_width = workarea().workWidth();
583 work_area_height = workarea().workHeight();
587 // The visible LyXView need a resize
588 resizeCurrentBuffer();
592 if (widthChange || heightChange)
595 // always make sure that the scrollbar is sane.
597 owner_->updateLayoutChoice();
601 void BufferView::Pimpl::update()
603 //lyxerr << "BufferView::update()" << endl;
604 // fix cursor coordinate cache in case something went wrong
606 // check needed to survive LyX startup
607 if (bv_->getLyXText()) {
608 // update all 'visible' paragraphs
609 ParagraphList::iterator beg;
610 ParagraphList::iterator end;
611 getParsInRange(buffer_->paragraphs(),
612 top_y(), top_y() + workarea().workHeight(),
614 bv_->text()->redoParagraphs(beg, end);
617 screen().redraw(*bv_);
621 // Callback for cursor timer
622 void BufferView::Pimpl::cursorToggle()
625 cursor_timeout.restart();
629 screen().toggleCursor(*bv_);
630 cursor_timeout.restart();
634 bool BufferView::Pimpl::available() const
636 return buffer_ && bv_->text();
640 Change const BufferView::Pimpl::getCurrentChange()
642 if (!bv_->buffer()->params().tracking_changes)
643 return Change(Change::UNCHANGED);
645 LyXText * text = bv_->getLyXText();
646 LCursor & cur = bv_->cursor();
648 if (!cur.selection())
649 return Change(Change::UNCHANGED);
651 return text->getPar(cur.selBegin())
652 ->lookupChangeFull(cur.selBegin().pos());
656 void BufferView::Pimpl::savePosition(unsigned int i)
658 if (i >= saved_positions_num)
660 saved_positions[i] = Position(buffer_->fileName(),
661 bv_->text()->cursorPar()->id(),
662 bv_->text()->cursor().pos());
664 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
668 void BufferView::Pimpl::restorePosition(unsigned int i)
670 if (i >= saved_positions_num)
673 string const fname = saved_positions[i].filename;
675 bv_->cursor().clearSelection();
677 if (fname != buffer_->fileName()) {
679 if (bufferlist.exists(fname))
680 b = bufferlist.getBuffer(fname);
682 b = bufferlist.newBuffer(fname);
683 ::loadLyXFile(b, fname); // don't ask, just load it
689 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
690 if (par == buffer_->par_iterator_end())
693 bv_->text()->setCursor(par.pit(),
694 min(par->size(), saved_positions[i].par_pos));
697 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
701 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
703 return i < saved_positions_num && !saved_positions[i].filename.empty();
707 void BufferView::Pimpl::switchKeyMap()
709 if (!lyxrc.rtl_support)
712 Intl & intl = owner_->getIntl();
713 if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
714 if (intl.keymap == Intl::PRIMARY)
717 if (intl.keymap == Intl::SECONDARY)
723 void BufferView::Pimpl::center()
725 LyXText * text = bv_->text();
727 bv_->cursor().clearSelection();
728 int const half_height = workarea().workHeight() / 2;
729 int new_y = text->cursorY(bv_->cursor().cursor_.front()) - half_height;
733 // FIXME: look at this comment again ...
734 // This updates top_y() but means the fitCursor() call
735 // from the update(FITCUR) doesn't realise that we might
736 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
737 // the scrollbar to be updated as it should, so we have
738 // to do it manually. Any operation that does a center()
739 // and also might have moved top_y() must make sure to call
740 // updateScrollbar() currently. Never mind that this is a
741 // pretty obfuscated way of updating text->top_y()
746 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
748 workarea().putClipboard(stuff);
752 InsetBase * BufferView::Pimpl::getInsetByCode(InsetBase::Code code)
755 CursorSlice cursor = bv_->getLyXText()->cursor;
756 Buffer::inset_iterator it =
757 find_if(Buffer::inset_iterator(
758 cursorPar(), cursor().pos()),
759 buffer_->inset_iterator_end(),
760 lyx::compare_memfun(&Inset::lyxCode, code));
761 return it != buffer_->inset_iterator_end() ? (*it) : 0;
763 // Ok, this is a little bit too brute force but it
764 // should work for now. Better infrastructure is coming. (Lgb)
766 Buffer * b = bv_->buffer();
767 LyXText * text = bv_->getLyXText();
769 Buffer::inset_iterator beg = b->inset_iterator_begin();
770 Buffer::inset_iterator end = b->inset_iterator_end();
772 bool cursor_par_seen = false;
774 for (; beg != end; ++beg) {
775 if (beg.getPar() == text->cursorPar()) {
776 cursor_par_seen = true;
778 if (cursor_par_seen) {
779 if (beg.getPar() == text->cursorPar()
780 && beg.getPos() >= text->cursor().pos()) {
783 if (beg.getPar() != text->cursorPar()) {
790 // Now find the first inset that matches code.
791 for (; beg != end; ++beg) {
792 if (beg->lyxCode() == code)
801 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
803 string filename = filenm;
805 if (filename.empty()) {
806 // Launch a file browser
807 string initpath = lyxrc.document_path;
810 string const trypath = owner_->buffer()->filePath();
811 // If directory is writeable, use this as default.
812 if (IsDirWriteable(trypath))
816 FileDialog fileDlg(_("Select LyX document to insert"),
818 make_pair(string(_("Documents|#o#O")),
819 string(lyxrc.document_path)),
820 make_pair(string(_("Examples|#E#e")),
821 string(AddPath(system_lyxdir(), "examples"))));
823 FileDialog::Result result =
824 fileDlg.open(initpath,
825 FileFilterList(_("LyX Documents (*.lyx)")),
828 if (result.first == FileDialog::Later)
831 filename = result.second;
833 // check selected filename
834 if (filename.empty()) {
835 owner_->message(_("Canceled."));
840 // get absolute path of file and add ".lyx" to the filename if
842 filename = FileSearch(string(), filename, "lyx");
844 string const disp_fn = MakeDisplayPath(filename);
845 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
846 if (bv_->insertLyXFile(filename))
847 owner_->message(bformat(_("Document %1$s inserted."),
850 owner_->message(bformat(_("Could not insert document %1$s"),
855 void BufferView::Pimpl::trackChanges()
857 Buffer * buf = bv_->buffer();
858 bool const tracking(buf->params().tracking_changes);
861 ParIterator const end = buf->par_iterator_end();
862 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
864 buf->params().tracking_changes = true;
866 // we cannot allow undos beyond the freeze point
867 buf->undostack().clear();
870 bv_->text()->setCursor(0, 0);
871 #warning changes FIXME
872 bool found = lyx::find::findNextChange(bv_);
874 owner_->getDialogs().show("changes");
878 ParIterator const end = buf->par_iterator_end();
879 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
880 it->untrackChanges();
881 buf->params().tracking_changes = false;
884 buf->redostack().clear();
888 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
891 // this is only called for mouse related events.
893 FuncRequest cmd = cmd0;
894 cmd.y += bv_->top_y();
895 lyxerr << "*** workAreaDispatch: request: " << cmd << std::endl;
897 switch (cmd.action) {
899 case LFUN_MOUSE_MOTION: {
902 FuncRequest cmd1 = cmd;
903 InsetBase * inset = cur.inset();
906 res = inset->dispatch(cur, cmd);
908 res = cur.innerText()->dispatch(cur, cmd);
911 if (bv_->fitCursor() || res.update()) {
919 case LFUN_MOUSE_MOTION:
920 case LFUN_MOUSE_PRESS:
921 case LFUN_MOUSE_RELEASE:
922 case LFUN_MOUSE_DOUBLE:
923 case LFUN_MOUSE_TRIPLE: {
924 // We pass those directly to the Bufferview, since
925 // otherwise selection handling breaks down
927 // Doesn't go through lyxfunc, so we need to update
928 // the layout choice etc. ourselves
930 // e.g. Qt mouse press when no buffer
934 screen().hideCursor();
936 // either the inset under the cursor or the surrounding LyXText will
937 // handle this event.
939 // built temporary path to inset
940 LyXText * text = bv_->text();
941 InsetBase * const inset_hit = text->checkInsetHit(cmd.x, cmd.y);
943 inset_hit->edit(cur, cmd.x, cmd.y);
945 text->setCursorFromCoordinates(cur.current(), cmd.x, cmd.y);
946 lyxerr << "created temp cursor: " << cur << endl;
948 // Dispatch to the temp cursor.
949 // An inset (or LyXText) can assign this to bv->cursor()
950 // if it wishes to do so.
951 DispatchResult res = cur.dispatch(cmd);
953 if (bv_->fitCursor() || res.update())
956 // see workAreaKeyPress
957 cursor_timeout.restart();
958 screen().showCursor(*bv_);
960 // skip these when selecting
961 if (cmd.action != LFUN_MOUSE_MOTION) {
962 owner_->updateLayoutChoice();
963 owner_->updateToolbar();
966 // slight hack: this is only called currently when we
967 // clicked somewhere, so we force through the display
968 // of the new status here.
969 owner_->clearMessage();
974 lyxerr << "*** UNDISPATCHED: " << cmd;
975 //owner_->dispatch(cmd);
981 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
983 lyxerr << "*** BufferView::Pimpl: request: " << cmd << std::endl;
984 // Make sure that the cached BufferView is correct.
985 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
986 << " action[" << cmd.action << ']'
987 << " arg[" << cmd.argument << ']'
988 << " x[" << cmd.x << ']'
989 << " y[" << cmd.y << ']'
990 << " button[" << cmd.button() << ']'
993 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
994 LCursor & cur = bv_->cursor();
996 switch (cmd.action) {
998 case LFUN_SCROLL_INSET:
999 // this is not handled here as this function is only active
1000 // if we have a locking_inset and that one is (or contains)
1004 case LFUN_FILE_INSERT:
1005 MenuInsertLyXFile(cmd.argument);
1008 case LFUN_FILE_INSERT_ASCII_PARA:
1009 InsertAsciiFile(bv_, cmd.argument, true);
1012 case LFUN_FILE_INSERT_ASCII:
1013 InsertAsciiFile(bv_, cmd.argument, false);
1016 case LFUN_FONT_STATE:
1017 owner_->getLyXFunc().setMessage(currentState(bv_));
1020 case LFUN_INSERT_LABEL: {
1021 // Try and generate a valid label
1022 string const contents = cmd.argument.empty() ?
1023 getPossibleLabel(*bv_) : cmd.argument;
1024 InsetCommandParams icp("label", contents);
1025 string data = InsetCommandMailer::params2string("label", icp);
1026 owner_->getDialogs().show("label", data, 0);
1030 case LFUN_BOOKMARK_SAVE:
1031 savePosition(strToUnsignedInt(cmd.argument));
1034 case LFUN_BOOKMARK_GOTO:
1035 restorePosition(strToUnsignedInt(cmd.argument));
1038 case LFUN_REF_GOTO: {
1039 string label = cmd.argument;
1040 if (label.empty()) {
1042 static_cast<InsetRef*>(getInsetByCode(InsetBase::REF_CODE));
1044 label = inset->getContents();
1050 bv_->gotoLabel(label);
1054 // --- accented characters ---------------------------
1057 case LFUN_CIRCUMFLEX:
1067 case LFUN_SPECIAL_CARON:
1070 case LFUN_HUNG_UMLAUT:
1073 if (cmd.argument.empty()) {
1075 owner_->getLyXFunc().handleKeyFunc(cmd.action);
1077 owner_->getLyXFunc().handleKeyFunc(cmd.action);
1078 owner_->getIntl().getTransManager()
1079 .TranslateAndInsert(cmd.argument[0], bv_->getLyXText());
1084 case LFUN_INSET_INSERT: {
1086 BOOST_ASSERT(false);
1087 InsetBase * inset = createInset(bv_, cmd);
1088 if (!inset || !insertInset(inset))
1093 case LFUN_FLOAT_LIST:
1094 if (tclass.floats().typeExist(cmd.argument)) {
1095 InsetBase * inset = new InsetFloatList(cmd.argument);
1096 if (!insertInset(inset, tclass.defaultLayoutName()))
1099 lyxerr << "Non-existent float type: "
1100 << cmd.argument << endl;
1104 case LFUN_LAYOUT_PARAGRAPH: {
1106 params2string(*bv_->getLyXText()->cursorPar(), data);
1107 data = "show\n" + data;
1108 bv_->owner()->getDialogs().show("paragraph", data);
1112 case LFUN_PARAGRAPH_UPDATE:
1113 updateParagraphDialog();
1116 case LFUN_PARAGRAPH_APPLY:
1117 setParagraphParams(*bv_, cmd.argument);
1120 case LFUN_THESAURUS_ENTRY: {
1121 string arg = cmd.argument;
1124 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1128 if (arg.size() > 100 || arg.empty()) {
1129 // Get word or selection
1130 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1131 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1132 // FIXME: where is getLyXText()->unselect(bv_) ?
1136 bv_->owner()->getDialogs().show("thesaurus", arg);
1140 case LFUN_TRACK_CHANGES:
1144 case LFUN_MERGE_CHANGES:
1145 owner_->getDialogs().show("changes");
1148 case LFUN_ACCEPT_ALL_CHANGES: {
1149 bv_->text()->setCursor(0, 0);
1150 #warning FIXME changes
1151 while (lyx::find::findNextChange(bv_))
1152 bv_->getLyXText()->acceptChange();
1157 case LFUN_REJECT_ALL_CHANGES: {
1158 bv_->text()->setCursor(0, 0);
1159 #warning FIXME changes
1160 while (lyx::find::findNextChange(bv_))
1161 bv_->getLyXText()->rejectChange();
1166 case LFUN_ACCEPT_CHANGE: {
1167 bv_->getLyXText()->acceptChange();
1172 case LFUN_REJECT_CHANGE: {
1173 bv_->getLyXText()->rejectChange();
1178 case LFUN_WORD_FIND:
1179 lyx::find::find(bv_, cmd);
1182 case LFUN_WORD_REPLACE:
1183 lyx::find::replace(bv_, cmd);
1187 cur.clearSelection();
1190 cmd.message(N_("Mark off"));
1194 cur.clearSelection();
1198 cmd.message(N_("Mark on"));
1202 cur.clearSelection();
1204 cmd.message(N_("Mark removed"));
1207 cmd.message(N_("Mark set"));
1213 case LFUN_UNKNOWN_ACTION:
1214 cmd.errorMessage(N_("Unknown function!"));
1218 return cur.dispatch(cmd).dispatched();
1225 bool BufferView::Pimpl::insertInset(InsetBase * inset, string const & lout)
1227 // not quite sure if we want this...
1228 bv_->text()->recUndo(bv_->text()->cursor().par());
1231 bv_->cursor().clearSelection();
1232 if (!lout.empty()) {
1233 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1235 if (!bv_->text()->cursorPar()->empty()) {
1236 bv_->text()->cursorLeft(bv_);
1237 bv_->text()->breakParagraph(bv_->buffer()->paragraphs());
1241 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1242 bool hasLayout = tclass.hasLayout(lres);
1244 bv_->text()->setLayout(hasLayout ? lres : tclass.defaultLayoutName());
1245 bv_->text()->setParagraph(Spacing(), LYX_ALIGN_LAYOUT, string(), 0);
1247 bv_->cursor().innerText()->insertInset(inset);
1253 bool BufferView::Pimpl::ChangeInsets(InsetBase::Code code,
1254 string const & from, string const & to)
1256 bool need_update = false;
1257 CursorSlice cur = bv_->text()->cursor();
1259 ParIterator end = bv_->buffer()->par_iterator_end();
1260 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1262 bool changed_inset = false;
1263 for (InsetList::iterator it2 = it->insetlist.begin();
1264 it2 != it->insetlist.end(); ++it2) {
1265 if (it2->inset->lyxCode() == code) {
1266 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1267 if (inset->getContents() == from) {
1268 inset->setContents(to);
1269 changed_inset = true;
1273 if (changed_inset) {
1278 // The test it.size() == 1 was needed to prevent crashes.
1279 // How to set the cursor correctly when it.size() > 1 ??
1280 if (it.size() == 1) {
1281 bv_->text()->setCursorIntern(bv_->text()->parOffset(it.pit()), 0);
1282 bv_->text()->redoParagraph(bv_->text()->cursorPar());
1286 bv_->text()->setCursorIntern(cur.par(), cur.pos());
1291 void BufferView::Pimpl::updateParagraphDialog()
1293 if (!bv_->owner()->getDialogs().visible("paragraph"))
1295 CursorSlice const & cur = bv_->cursor().innerTextSlice();
1296 LyXText * text = bv_->cursor().innerText();
1297 Paragraph const & par = *text->getPar(cur.par());
1299 params2string(par, data);
1301 // Will the paragraph accept changes from the dialog?
1302 InsetBase * const inset = cur.inset();
1304 !(inset && inset->forceDefaultParagraphs(inset));
1306 data = "update " + tostr(accept) + '\n' + data;
1307 bv_->owner()->getDialogs().update("paragraph", data);