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/path_defines.h"
66 #include "support/tostr.h"
68 #include <boost/bind.hpp>
72 using bv_funcs::currentState;
74 using bv_funcs::fontSize;
77 using bv_funcs::roman;
79 using bv_funcs::styleReset;
80 using bv_funcs::underline;
84 using lyx::support::AddPath;
85 using lyx::support::bformat;
86 using lyx::support::FileSearch;
87 using lyx::support::IsDirWriteable;
88 using lyx::support::MakeDisplayPath;
89 using lyx::support::strToUnsignedInt;
90 using lyx::support::system_lyxdir;
98 extern BufferList bufferlist;
103 unsigned int const saved_positions_num = 20;
105 // All the below connection objects are needed because of a bug in some
106 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
107 // to these connections we avoid a segfault upon startup, and also at exit.
110 boost::signals::connection dispatchcon;
111 boost::signals::connection timecon;
112 boost::signals::connection doccon;
113 boost::signals::connection resizecon;
114 boost::signals::connection kpresscon;
115 boost::signals::connection selectioncon;
116 boost::signals::connection lostcon;
122 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
123 int xpos, int ypos, int width, int height)
124 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
125 using_xterm_cursor(false)
127 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
128 screen_.reset(LyXScreenFactory::create(workarea()));
131 doccon = workarea().scrollDocView
132 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
133 resizecon = workarea().workAreaResize
134 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
135 dispatchcon = workarea().dispatch
136 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
137 kpresscon = workarea().workAreaKeyPress
138 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
139 selectioncon = workarea().selectionRequested
140 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
141 lostcon = workarea().selectionLost
142 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
144 timecon = cursor_timeout.timeout
145 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
146 cursor_timeout.start();
147 saved_positions.resize(saved_positions_num);
151 void BufferView::Pimpl::addError(ErrorItem const & ei)
153 errorlist_.push_back(ei);
157 void BufferView::Pimpl::showReadonly(bool)
159 owner_->updateWindowTitle();
160 owner_->getDialogs().updateBufferDependent(false);
164 void BufferView::Pimpl::connectBuffer(Buffer & buf)
166 if (errorConnection_.connected())
169 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
170 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
171 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
172 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
173 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
174 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
175 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
179 void BufferView::Pimpl::disconnectBuffer()
181 errorConnection_.disconnect();
182 messageConnection_.disconnect();
183 busyConnection_.disconnect();
184 titleConnection_.disconnect();
185 timerConnection_.disconnect();
186 readonlyConnection_.disconnect();
187 closingConnection_.disconnect();
191 bool BufferView::Pimpl::newFile(string const & filename,
192 string const & tname,
195 Buffer * b = ::newFile(filename, tname, isNamed);
201 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
203 // get absolute path of file and add ".lyx" to the filename if
205 string s = FileSearch(string(), filename, "lyx");
207 bool const found = !s.empty();
212 // file already open?
213 if (bufferlist.exists(s)) {
214 string const file = MakeDisplayPath(s, 20);
215 string text = bformat(_("The document %1$s is already "
216 "loaded.\n\nDo you want to revert "
217 "to the saved version?"), file);
218 int const ret = Alert::prompt(_("Revert to saved document?"),
219 text, 0, 1, _("&Revert"), _("&Switch to document"));
222 buffer(bufferlist.getBuffer(s));
225 // FIXME: should be LFUN_REVERT
226 if (!bufferlist.close(bufferlist.getBuffer(s), false))
228 // Fall through to new load. (Asger)
235 b = bufferlist.newBuffer(s);
237 if (!::loadLyXFile(b, s)) {
238 bufferlist.release(b);
242 string text = bformat(_("The document %1$s does not yet "
243 "exist.\n\nDo you want to create "
244 "a new document?"), s);
245 int const ret = Alert::prompt(_("Create new document?"),
246 text, 0, 1, _("&Create"), _("Cancel"));
249 b = ::newFile(s, string(), true);
255 bv_->showErrorList(_("Parse"));
258 LyX::ref().lastfiles().newFile(b->fileName());
264 WorkArea & BufferView::Pimpl::workarea() const
266 return *workarea_.get();
270 LyXScreen & BufferView::Pimpl::screen() const
272 return *screen_.get();
276 Painter & BufferView::Pimpl::painter() const
278 return workarea().getPainter();
282 void BufferView::Pimpl::top_y(int y)
288 int BufferView::Pimpl::top_y() const
294 void BufferView::Pimpl::buffer(Buffer * b)
296 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
304 // set current buffer
309 // if we're quitting lyx, don't bother updating stuff
313 // if we are closing the buffer, use the first buffer as current
315 buffer_ = bufferlist.first();
318 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
319 connectBuffer(*buffer_);
321 // If we don't have a text object for this, we make one
323 resizeCurrentBuffer();
325 // FIXME: needed when ?
326 top_y(screen().topCursorVisible(bv_->text));
328 // Buffer-dependent dialogs should be updated or
329 // hidden. This should go here because some dialogs (eg ToC)
330 // require bv_->text.
331 owner_->getDialogs().updateBufferDependent(true);
333 lyxerr[Debug::INFO] << " No Buffer!" << endl;
334 owner_->getDialogs().hideBufferDependent();
339 owner_->updateMenubar();
340 owner_->updateToolbar();
341 owner_->updateLayoutChoice();
342 owner_->updateWindowTitle();
345 // Don't forget to update the Layout
346 string const layoutname =
347 bv_->text->cursorPar()->layout()->name();
348 owner_->setLayout(layoutname);
351 if (lyx::graphics::Previews::activated() && buffer_)
352 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
356 bool BufferView::Pimpl::fitCursor()
360 if (bv_->theLockingInset()) {
361 bv_->theLockingInset()->fitInsetCursor(bv_);
364 ret = screen().fitCursor(bv_->text, bv_);
367 //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
369 // We need to always update, in case we did a
370 // paste and we stayed anchored to a row, but
371 // the actual height of the doc changed ...
377 void BufferView::Pimpl::redoCurrentBuffer()
379 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
380 if (buffer_ && bv_->text) {
381 resizeCurrentBuffer();
383 owner_->updateLayoutChoice();
389 void BufferView::Pimpl::resizeCurrentBuffer()
391 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
394 int selstartpar = -1;
396 UpdatableInset * the_locking_inset = 0;
399 pos_type selstartpos = 0;
400 pos_type selendpos = 0;
401 bool selection = false;
402 bool mark_set = false;
406 owner_->message(_("Formatting document..."));
409 par = bv_->text->cursor.par();
410 pos = bv_->text->cursor.pos();
411 selstartpar = bv_->text->selection.start.par();
412 selstartpos = bv_->text->selection.start.pos();
413 selendpar = bv_->text->selection.end.par();
414 selendpos = bv_->text->selection.end.pos();
415 selection = bv_->text->selection.set();
416 mark_set = bv_->text->selection.mark();
417 the_locking_inset = bv_->theLockingInset();
418 bv_->text->fullRebreak();
421 bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs());
422 bv_->text->init(bv_);
426 bv_->text->selection.set(true);
427 // At this point just to avoid the Delete-Empty-Paragraph-
428 // Mechanism when setting the cursor.
429 bv_->text->selection.mark(mark_set);
431 bv_->text->setCursor(selstartpar, selstartpos);
432 bv_->text->selection.cursor = bv_->text->cursor;
433 bv_->text->setCursor(selendpar, selendpos);
434 bv_->text->setSelection();
435 bv_->text->setCursor(par, pos);
437 bv_->text->setCursor(par, pos);
438 bv_->text->selection.cursor = bv_->text->cursor;
439 bv_->text->selection.set(false);
441 // remake the inset locking
442 bv_->theLockingInset(the_locking_inset);
445 top_y(screen().topCursorVisible(bv_->text));
450 // reset the "Formatting..." message
451 owner_->clearMessage();
457 void BufferView::Pimpl::updateScrollbar()
460 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
461 workarea().setScrollbarParams(0, 0, 0);
465 LyXText const & t = *bv_->text;
467 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
468 << top_y() << ", default height " << defaultRowHeight() << endl;
470 workarea().setScrollbarParams(t.height, top_y(), defaultRowHeight());
474 void BufferView::Pimpl::scrollDocView(int value)
476 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
481 screen().hideCursor();
484 screen().redraw(*bv_);
486 if (!lyxrc.cursor_follows_scrollbar)
489 int const height = defaultRowHeight();
490 int const first = top_y() + height;
491 int const last = top_y() + workarea().workHeight() - height;
493 LyXText * text = bv_->text;
494 if (text->cursor.y() < first)
495 text->setCursorFromCoordinates(0, first);
496 else if (text->cursor.y() > last)
497 text->setCursorFromCoordinates(0, last);
499 owner_->updateLayoutChoice();
503 void BufferView::Pimpl::scroll(int lines)
509 LyXText const * t = bv_->text;
510 int const line_height = defaultRowHeight();
512 // The new absolute coordinate
513 int new_top_y = top_y() + lines * line_height;
515 // Restrict to a valid value
516 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
517 new_top_y = std::max(0, new_top_y);
519 scrollDocView(new_top_y);
521 // Update the scrollbar.
522 workarea().setScrollbarParams(t->height, top_y(), defaultRowHeight());
526 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
527 key_modifier::state state)
529 bv_->owner()->getLyXFunc().processKeySym(key, state);
531 /* This is perhaps a bit of a hack. When we move
532 * around, or type, it's nice to be able to see
533 * the cursor immediately after the keypress. So
534 * we reset the toggle timeout and force the visibility
535 * of the cursor. Note we cannot do this inside
536 * dispatch() itself, because that's called recursively.
539 cursor_timeout.restart();
540 screen().showCursor(*bv_);
545 void BufferView::Pimpl::selectionRequested()
552 LyXText * text = bv_->getLyXText();
554 if (text->selection.set() &&
555 (!bv_->text->xsel_cache.set() ||
556 text->selection.start != bv_->text->xsel_cache.start ||
557 text->selection.end != bv_->text->xsel_cache.end))
559 bv_->text->xsel_cache = text->selection;
560 sel = text->selectionAsString(*bv_->buffer(), false);
561 } else if (!text->selection.set()) {
563 bv_->text->xsel_cache.set(false);
566 workarea().putClipboard(sel);
571 void BufferView::Pimpl::selectionLost()
574 screen().hideCursor();
575 bv_->getLyXText()->clearSelection();
576 bv_->text->xsel_cache.set(false);
581 void BufferView::Pimpl::workAreaResize()
583 static int work_area_width;
584 static int work_area_height;
586 bool const widthChange = workarea().workWidth() != work_area_width;
587 bool const heightChange = workarea().workHeight() != work_area_height;
589 // update from work area
590 work_area_width = workarea().workWidth();
591 work_area_height = workarea().workHeight();
595 // The visible LyXView need a resize
596 resizeCurrentBuffer();
600 if (widthChange || heightChange)
603 // always make sure that the scrollbar is sane.
605 owner_->updateLayoutChoice();
609 void BufferView::Pimpl::update()
611 //lyxerr << "BufferView::update()" << endl;
612 // fix cursor coordinate cache in case something went wrong
613 if (bv_->getLyXText()) {
614 // check needed to survive LyX startup
615 bv_->getLyXText()->redoCursor();
617 screen().redraw(*bv_);
621 // Callback for cursor timer
622 void BufferView::Pimpl::cursorToggle()
625 cursor_timeout.restart();
629 screen().toggleCursor(*bv_);
631 cursor_timeout.restart();
635 bool BufferView::Pimpl::available() const
637 if (buffer_ && bv_->text)
643 Change const BufferView::Pimpl::getCurrentChange()
645 if (!bv_->buffer()->params().tracking_changes)
646 return Change(Change::UNCHANGED);
648 LyXText * text = bv_->getLyXText();
650 if (!text->selection.set())
651 return Change(Change::UNCHANGED);
653 return text->getPar(text->selection.start)
654 ->lookupChangeFull(text->selection.start.pos());
658 void BufferView::Pimpl::beforeChange(LyXText * text)
660 text->clearSelection();
664 void BufferView::Pimpl::savePosition(unsigned int i)
666 if (i >= saved_positions_num)
668 saved_positions[i] = Position(buffer_->fileName(),
669 bv_->text->cursorPar()->id(),
670 bv_->text->cursor.pos());
672 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
676 void BufferView::Pimpl::restorePosition(unsigned int i)
678 if (i >= saved_positions_num)
681 string const fname = saved_positions[i].filename;
683 beforeChange(bv_->text);
685 if (fname != buffer_->fileName()) {
687 if (bufferlist.exists(fname))
688 b = bufferlist.getBuffer(fname);
690 b = bufferlist.newBuffer(fname);
691 ::loadLyXFile(b, fname); // don't ask, just load it
697 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
698 if (par == buffer_->par_iterator_end())
701 bv_->text->setCursor(par.pit(),
702 min(par->size(), saved_positions[i].par_pos));
706 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
710 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
712 if (i >= saved_positions_num)
715 return !saved_positions[i].filename.empty();
719 void BufferView::Pimpl::switchKeyMap()
721 if (!lyxrc.rtl_support)
724 LyXText * text = bv_->getLyXText();
725 if (text->real_current_font.isRightToLeft()
726 && !(bv_->theLockingInset()
727 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
729 if (owner_->getIntl().keymap == Intl::PRIMARY)
730 owner_->getIntl().KeyMapSec();
732 if (owner_->getIntl().keymap == Intl::SECONDARY)
733 owner_->getIntl().KeyMapPrim();
738 void BufferView::Pimpl::insetUnlock()
740 if (bv_->theLockingInset()) {
741 bv_->theLockingInset()->insetUnlock(bv_);
742 bv_->theLockingInset(0);
748 void BufferView::Pimpl::center()
750 LyXText * text = bv_->text;
753 int const half_height = workarea().workHeight() / 2;
754 int new_y = std::max(0, text->cursor.y() - half_height);
756 // FIXME: look at this comment again ...
758 // FIXME: can we do this w/o calling screen directly ?
759 // This updates top_y() but means the fitCursor() call
760 // from the update(FITCUR) doesn't realise that we might
761 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
762 // the scrollbar to be updated as it should, so we have
763 // to do it manually. Any operation that does a center()
764 // and also might have moved top_y() must make sure to call
765 // updateScrollbar() currently. Never mind that this is a
766 // pretty obfuscated way of updating t->top_y()
773 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
775 workarea().putClipboard(stuff);
780 * Dispatch functions for actions which can be valid for BufferView->text
781 * and/or InsetText->text!!!
785 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
788 LyXCursor cursor = bv_->getLyXText()->cursor;
789 Buffer::inset_iterator it =
790 find_if(Buffer::inset_iterator(
791 cursorPar(), cursor.pos()),
792 buffer_->inset_iterator_end(),
793 lyx::compare_memfun(&Inset::lyxCode, code));
794 return it != buffer_->inset_iterator_end() ? (*it) : 0;
796 // Ok, this is a little bit too brute force but it
797 // should work for now. Better infrastructure is coming. (Lgb)
799 Buffer * b = bv_->buffer();
800 LyXText * text = bv_->getLyXText();
802 Buffer::inset_iterator beg = b->inset_iterator_begin();
803 Buffer::inset_iterator end = b->inset_iterator_end();
805 bool cursorPar_seen = false;
807 for (; beg != end; ++beg) {
808 if (beg.getPar() == text->cursorPar()) {
809 cursorPar_seen = true;
811 if (cursorPar_seen) {
812 if (beg.getPar() == text->cursorPar()
813 && beg.getPos() >= text->cursor.pos()) {
815 } else if (beg.getPar() != text->cursorPar()) {
822 // Now find the first inset that matches code.
823 for (; beg != end; ++beg) {
824 if (beg->lyxCode() == code) {
834 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
836 string filename = filen;
838 if (filename.empty()) {
839 // Launch a file browser
840 string initpath = lyxrc.document_path;
843 string const trypath = owner_->buffer()->filePath();
844 // If directory is writeable, use this as default.
845 if (IsDirWriteable(trypath))
849 FileDialog fileDlg(_("Select LyX document to insert"),
851 make_pair(string(_("Documents|#o#O")),
852 string(lyxrc.document_path)),
853 make_pair(string(_("Examples|#E#e")),
854 string(AddPath(system_lyxdir(), "examples"))));
856 FileDialog::Result result =
857 fileDlg.open(initpath,
858 _("*.lyx| LyX Documents (*.lyx)"));
860 if (result.first == FileDialog::Later)
863 filename = result.second;
865 // check selected filename
866 if (filename.empty()) {
867 owner_->message(_("Canceled."));
872 // get absolute path of file and add ".lyx" to the filename if
874 filename = FileSearch(string(), filename, "lyx");
876 string const disp_fn = MakeDisplayPath(filename);
877 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
878 if (bv_->insertLyXFile(filename))
879 owner_->message(bformat(_("Document %1$s inserted."),
882 owner_->message(bformat(_("Could not insert document %1$s"),
887 void BufferView::Pimpl::trackChanges()
889 Buffer * buf(bv_->buffer());
890 bool const tracking(buf->params().tracking_changes);
893 ParIterator const end = buf->par_iterator_end();
894 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
896 buf->params().tracking_changes = true;
898 // we cannot allow undos beyond the freeze point
899 buf->undostack().clear();
902 bv_->text->setCursor(0, 0);
903 #warning changes FIXME
904 //moveCursorUpdate(false);
906 bool found = lyx::find::findNextChange(bv_);
908 owner_->getDialogs().show("changes");
912 ParIterator const end = buf->par_iterator_end();
913 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
914 it->untrackChanges();
915 buf->params().tracking_changes = false;
918 buf->redostack().clear();
922 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
925 case LFUN_MOUSE_PRESS:
926 case LFUN_MOUSE_MOTION:
927 case LFUN_MOUSE_RELEASE:
928 case LFUN_MOUSE_DOUBLE:
929 case LFUN_MOUSE_TRIPLE:
931 // We pass those directly to the Bufferview, since
932 // otherwise selection handling breaks down
934 // Doesn't go through lyxfunc, so we need to update
935 // the layout choice etc. ourselves
937 // e.g. Qt mouse press when no buffer
941 screen().hideCursor();
943 bool const res = dispatch(ev);
945 // see workAreaKeyPress
946 cursor_timeout.restart();
947 screen().showCursor(*bv_);
949 // FIXME: we should skip these when selecting
950 owner_->updateLayoutChoice();
951 owner_->updateToolbar();
954 // slight hack: this is only called currently when we
955 // clicked somewhere, so we force through the display
956 // of the new status here.
957 owner_->clearMessage();
962 owner_->dispatch(ev);
968 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
970 // Make sure that the cached BufferView is correct.
971 FuncRequest ev = ev_in;
974 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
975 << " action[" << ev.action << ']'
976 << " arg[" << ev.argument << ']'
977 << " x[" << ev.x << ']'
978 << " y[" << ev.y << ']'
979 << " button[" << ev.button() << ']'
982 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
986 case LFUN_SCROLL_INSET:
987 // this is not handled here as this function is only active
988 // if we have a locking_inset and that one is (or contains)
992 case LFUN_FILE_INSERT:
993 MenuInsertLyXFile(ev.argument);
996 case LFUN_FILE_INSERT_ASCII_PARA:
997 InsertAsciiFile(bv_, ev.argument, true);
1000 case LFUN_FILE_INSERT_ASCII:
1001 InsertAsciiFile(bv_, ev.argument, false);
1005 lang(bv_, ev.argument);
1007 owner_->view_state_changed();
1012 owner_->view_state_changed();
1017 owner_->view_state_changed();
1022 owner_->view_state_changed();
1027 owner_->view_state_changed();
1032 owner_->view_state_changed();
1037 owner_->view_state_changed();
1042 owner_->view_state_changed();
1045 case LFUN_UNDERLINE:
1047 owner_->view_state_changed();
1050 case LFUN_FONT_SIZE:
1051 fontSize(bv_, ev.argument);
1052 owner_->view_state_changed();
1055 case LFUN_FONT_STATE:
1056 owner_->getLyXFunc().setMessage(currentState(bv_));
1059 case LFUN_INSERT_LABEL: {
1060 // Try and generate a valid label
1061 string const contents = ev.argument.empty() ?
1062 getPossibleLabel(*bv_) : ev.argument;
1063 InsetCommandParams icp("label", contents);
1064 string data = InsetCommandMailer::params2string("label", icp);
1065 owner_->getDialogs().show("label", data, 0);
1069 case LFUN_BOOKMARK_SAVE:
1070 savePosition(strToUnsignedInt(ev.argument));
1073 case LFUN_BOOKMARK_GOTO:
1074 restorePosition(strToUnsignedInt(ev.argument));
1077 case LFUN_REF_GOTO: {
1078 string label = ev.argument;
1079 if (label.empty()) {
1081 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1083 label = inset->getContents();
1089 bv_->gotoLabel(label);
1093 // --- accented characters ---------------------------
1096 case LFUN_CIRCUMFLEX:
1106 case LFUN_SPECIAL_CARON:
1109 case LFUN_HUNG_UMLAUT:
1112 if (ev.argument.empty()) {
1114 owner_->getLyXFunc().handleKeyFunc(ev.action);
1116 owner_->getLyXFunc().handleKeyFunc(ev.action);
1117 owner_->getIntl().getTransManager()
1118 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1123 case LFUN_MATH_MACRO:
1124 case LFUN_MATH_DELIM:
1125 case LFUN_INSERT_MATRIX:
1126 case LFUN_INSERT_MATH:
1127 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1128 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1129 case LFUN_MATH_MODE: // Open or create an inlined math inset
1133 case LFUN_INSET_APPLY: {
1134 string const name = ev.getArg(0);
1136 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1138 // This works both for 'original' and 'mathed' insets.
1139 // Note that the localDispatch performs updateInset
1141 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1142 inset->dispatch(fr);
1144 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1150 case LFUN_INSET_INSERT: {
1151 InsetOld * inset = createInset(ev);
1152 if (inset && insertInset(inset)) {
1155 string const name = ev.getArg(0);
1156 if (name == "bibitem") {
1157 // We need to do a redraw because the maximum
1158 // InsetBibitem width could have changed
1159 #warning check whether the update() is needed at all
1168 case LFUN_FLOAT_LIST:
1169 if (tclass.floats().typeExist(ev.argument)) {
1170 InsetOld * inset = new InsetFloatList(ev.argument);
1171 if (!insertInset(inset, tclass.defaultLayoutName()))
1174 lyxerr << "Non-existent float type: "
1175 << ev.argument << endl;
1179 case LFUN_LAYOUT_PARAGRAPH: {
1181 params2string(*bv_->getLyXText()->cursorPar(), data);
1183 data = "show\n" + data;
1184 bv_->owner()->getDialogs().show("paragraph", data);
1188 case LFUN_PARAGRAPH_UPDATE: {
1189 if (!bv_->owner()->getDialogs().visible("paragraph"))
1191 Paragraph const & par = *bv_->getLyXText()->cursorPar();
1194 params2string(par, data);
1196 // Will the paragraph accept changes from the dialog?
1197 InsetOld * const inset = par.inInset();
1199 !(inset && inset->forceDefaultParagraphs(inset));
1201 data = "update " + tostr(accept) + '\n' + data;
1202 bv_->owner()->getDialogs().update("paragraph", data);
1206 case LFUN_PARAGRAPH_APPLY:
1207 setParagraphParams(*bv_, ev.argument);
1210 case LFUN_THESAURUS_ENTRY:
1212 string arg = ev.argument;
1215 arg = bv_->getLyXText()->selectionAsString(*buffer_,
1219 if (arg.size() > 100 || arg.empty()) {
1220 // Get word or selection
1221 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1222 arg = bv_->getLyXText()->selectionAsString(*buffer_, false);
1223 // FIXME: where is getLyXText()->unselect(bv_) ?
1227 bv_->owner()->getDialogs().show("thesaurus", arg);
1231 case LFUN_TRACK_CHANGES:
1235 case LFUN_MERGE_CHANGES:
1236 owner_->getDialogs().show("changes");
1239 case LFUN_ACCEPT_ALL_CHANGES: {
1240 bv_->text->setCursor(0, 0);
1241 #warning FIXME changes
1242 //moveCursorUpdate(false);
1244 while (lyx::find::findNextChange(bv_))
1245 bv_->getLyXText()->acceptChange();
1251 case LFUN_REJECT_ALL_CHANGES: {
1252 bv_->text->setCursor(0, 0);
1253 #warning FIXME changes
1254 //moveCursorUpdate(false);
1256 while (lyx::find::findNextChange(bv_))
1257 bv_->getLyXText()->rejectChange();
1263 case LFUN_ACCEPT_CHANGE: {
1264 bv_->getLyXText()->acceptChange();
1269 case LFUN_REJECT_CHANGE: {
1270 bv_->getLyXText()->rejectChange();
1275 case LFUN_UNKNOWN_ACTION:
1276 ev.errorMessage(N_("Unknown function!"));
1280 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_)).dispatched();
1287 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1289 // if we are in a locking inset we should try to insert the
1290 // inset there otherwise this is a illegal function now
1291 if (bv_->theLockingInset()) {
1292 if (bv_->theLockingInset()->insetAllowed(inset))
1293 return bv_->theLockingInset()->insertInset(bv_, inset);
1297 // not quite sure if we want this...
1298 bv_->text->recUndo(bv_->text->cursor.par());
1301 beforeChange(bv_->text);
1302 if (!lout.empty()) {
1303 bv_->text->breakParagraph(bv_->buffer()->paragraphs());
1305 if (!bv_->text->cursorPar()->empty()) {
1306 bv_->text->cursorLeft(bv_);
1307 bv_->text->breakParagraph(bv_->buffer()->paragraphs());
1311 LyXTextClass const & tclass = buffer_->params().getLyXTextClass();
1312 bool hasLayout = tclass.hasLayout(lres);
1313 string lay = tclass.defaultLayoutName();
1315 if (hasLayout != false) {
1319 // layout not fount using default
1320 lay = tclass.defaultLayoutName();
1323 bv_->text->setLayout(lay);
1325 bv_->text->setParagraph(
1326 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1333 bv_->text->insertInset(inset);
1341 void BufferView::Pimpl::updateInset(InsetOld const * inset)
1346 bv_->text->redoParagraph(outerPar(*bv_->buffer(), inset));
1348 // this should not be needed, but it is...
1349 // bv_->text->redoParagraph(bv_->text->cursorPar());
1350 // bv_->text->fullRebreak();
1357 bool BufferView::Pimpl::ChangeInsets(InsetOld::Code code,
1358 string const & from, string const & to)
1360 bool need_update = false;
1361 LyXCursor cursor = bv_->text->cursor;
1362 LyXCursor tmpcursor = cursor;
1363 cursor.par(tmpcursor.par());
1364 cursor.pos(tmpcursor.pos());
1366 ParIterator end = bv_->buffer()->par_iterator_end();
1367 for (ParIterator it = bv_->buffer()->par_iterator_begin();
1369 bool changed_inset = false;
1370 for (InsetList::iterator it2 = it->insetlist.begin();
1371 it2 != it->insetlist.end(); ++it2) {
1372 if (it2->inset->lyxCode() == code) {
1373 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
1374 if (inset->getContents() == from) {
1375 inset->setContents(to);
1376 changed_inset = true;
1380 if (changed_inset) {
1385 // The test it.size()==1 was needed to prevent crashes.
1386 // How to set the cursor correctly when it.size()>1 ??
1387 if (it.size() == 1) {
1388 bv_->text->setCursorIntern(bv_->text->parOffset(it.pit()), 0);
1389 bv_->text->redoParagraph(bv_->text->cursorPar());
1393 bv_->text->setCursorIntern(cursor.par(), cursor.pos());