2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
11 #include "BufferView_pimpl.h"
12 #include "bufferlist.h"
14 #include "bufferview_funcs.h"
18 #include "FloatList.h"
19 #include "funcrequest.h"
22 #include "iterators.h"
24 #include "lyx_cb.h" // added for Dispatch functions
31 #include "paragraph.h"
32 #include "ParagraphParameters.h"
33 #include "TextCache.h"
34 #include "undo_funcs.h"
36 #include "insets/insetfloatlist.h"
37 #include "insets/insetgraphics.h"
38 #include "insets/insetinclude.h"
39 #include "insets/insetref.h"
40 #include "insets/insettext.h"
42 #include "frontends/Alert.h"
43 #include "frontends/Dialogs.h"
44 #include "frontends/FileDialog.h"
45 #include "frontends/LyXView.h"
46 #include "frontends/LyXScreenFactory.h"
47 #include "frontends/mouse_state.h"
48 #include "frontends/screen.h"
49 #include "frontends/WorkArea.h"
50 #include "frontends/WorkAreaFactory.h"
52 #include "mathed/formulabase.h"
54 #include "graphics/Previews.h"
56 #include "support/LAssert.h"
57 #include "support/tostr.h"
58 #include "support/filetools.h"
60 #include <boost/bind.hpp>
61 #include <boost/signals/connection.hpp>
76 using namespace bv_funcs;
78 extern BufferList bufferlist;
83 unsigned int const saved_positions_num = 20;
85 // All the below connection objects are needed because of a bug in some
86 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
87 // to these connections we avoid a segfault upon startup, and also at exit.
90 boost::signals::connection dispatchcon;
91 boost::signals::connection timecon;
92 boost::signals::connection doccon;
93 boost::signals::connection resizecon;
94 boost::signals::connection kpresscon;
95 boost::signals::connection selectioncon;
96 boost::signals::connection lostcon;
102 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
103 int xpos, int ypos, int width, int height)
104 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
105 using_xterm_cursor(false)
107 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
108 screen_.reset(LyXScreenFactory::create(workarea()));
111 doccon = workarea().scrollDocView
112 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
113 resizecon = workarea().workAreaResize
114 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
115 dispatchcon = workarea().dispatch
116 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
117 kpresscon = workarea().workAreaKeyPress
118 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
119 selectioncon = workarea().selectionRequested
120 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
121 lostcon = workarea().selectionLost
122 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
124 timecon = cursor_timeout.timeout
125 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
126 cursor_timeout.start();
127 saved_positions.resize(saved_positions_num);
131 WorkArea & BufferView::Pimpl::workarea() const
133 return *workarea_.get();
137 LyXScreen & BufferView::Pimpl::screen() const
139 return *screen_.get();
143 Painter & BufferView::Pimpl::painter() const
145 return workarea().getPainter();
149 void BufferView::Pimpl::buffer(Buffer * b)
151 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
154 buffer_->delUser(bv_);
156 // Put the old text into the TextCache, but
157 // only if the buffer is still loaded.
158 // Also set the owner of the test to 0
159 // bv_->text->owner(0);
160 textcache.add(buffer_, workarea().workWidth(), bv_->text);
161 if (lyxerr.debugging())
162 textcache.show(lyxerr, "BufferView::buffer");
167 // set current buffer
170 // if we're quitting lyx, don't bother updating stuff
174 // if we are closing the buffer, use the first buffer as current
176 buffer_ = bufferlist.first();
180 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
181 buffer_->addUser(bv_);
183 // If we don't have a text object for this, we make one
184 if (bv_->text == 0) {
185 resizeCurrentBuffer();
188 // FIXME: needed when ?
189 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
191 // Buffer-dependent dialogs should be updated or
192 // hidden. This should go here because some dialogs (eg ToC)
193 // require bv_->text.
194 owner_->getDialogs().updateBufferDependent(true);
196 lyxerr[Debug::INFO] << " No Buffer!" << endl;
197 owner_->getDialogs().hideBufferDependent();
199 // Also remove all remaining text's from the testcache.
200 // (there should not be any!) (if there is any it is a
202 if (lyxerr.debugging())
203 textcache.show(lyxerr, "buffer delete all");
209 owner_->updateMenubar();
210 owner_->updateToolbar();
211 owner_->updateLayoutChoice();
212 owner_->updateWindowTitle();
215 // Don't forget to update the Layout
216 string const layoutname =
217 bv_->text->cursor.par()->layout()->name();
218 owner_->setLayout(layoutname);
221 if (grfx::Previews::activated() && buffer_)
222 grfx::Previews::get().generateBufferPreviews(*buffer_);
226 bool BufferView::Pimpl::fitCursor()
230 if (bv_->theLockingInset()) {
231 bv_->theLockingInset()->fitInsetCursor(bv_);
234 ret = screen().fitCursor(bv_->text, bv_);
237 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
239 // We need to always update, in case we did a
240 // paste and we stayed anchored to a row, but
241 // the actual height of the doc changed ...
247 void BufferView::Pimpl::redoCurrentBuffer()
249 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
250 if (buffer_ && bv_->text) {
251 resizeCurrentBuffer();
253 owner_->updateLayoutChoice();
259 int BufferView::Pimpl::resizeCurrentBuffer()
261 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
263 ParagraphList::iterator par;
264 ParagraphList::iterator selstartpar;
265 ParagraphList::iterator selendpar;
266 UpdatableInset * the_locking_inset = 0;
269 pos_type selstartpos = 0;
270 pos_type selendpos = 0;
271 bool selection = false;
272 bool mark_set = false;
276 owner_->message(_("Formatting document..."));
279 par = bv_->text->cursor.par();
280 pos = bv_->text->cursor.pos();
281 selstartpar = bv_->text->selection.start.par();
282 selstartpos = bv_->text->selection.start.pos();
283 selendpar = bv_->text->selection.end.par();
284 selendpos = bv_->text->selection.end.pos();
285 selection = bv_->text->selection.set();
286 mark_set = bv_->text->selection.mark();
287 the_locking_inset = bv_->theLockingInset();
288 buffer_->resizeInsets(bv_);
289 // I don't think the delete and new are necessary here we just could
290 // call only init! (Jug 20020419)
292 bv_->text = new LyXText(bv_);
293 bv_->text->init(bv_);
295 // See if we have a text in TextCache that fits
296 // the new buffer_ with the correct width.
297 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
299 if (lyxerr.debugging()) {
300 lyxerr << "Found a LyXText that fits:\n";
301 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
303 // Set the owner of the newly found text
304 // bv_->text->owner(bv_);
305 if (lyxerr.debugging())
306 textcache.show(lyxerr, "resizeCurrentBuffer");
308 buffer_->resizeInsets(bv_);
310 bv_->text = new LyXText(bv_);
311 bv_->text->init(bv_);
312 //buffer_->resizeInsets(bv_);
315 par = bv_->text->ownerParagraphs().end();
316 selstartpar = bv_->text->ownerParagraphs().end();
317 selendpar = bv_->text->ownerParagraphs().end();
320 if (par != bv_->text->ownerParagraphs().end()) {
321 bv_->text->selection.set(true);
322 // At this point just to avoid the Delete-Empty-Paragraph-
323 // Mechanism when setting the cursor.
324 bv_->text->selection.mark(mark_set);
326 bv_->text->setCursor(selstartpar, selstartpos);
327 bv_->text->selection.cursor = bv_->text->cursor;
328 bv_->text->setCursor(selendpar, selendpos);
329 bv_->text->setSelection();
330 bv_->text->setCursor(par, pos);
332 bv_->text->setCursor(par, pos);
333 bv_->text->selection.cursor = bv_->text->cursor;
334 bv_->text->selection.set(false);
336 // remake the inset locking
337 bv_->theLockingInset(the_locking_inset);
340 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
345 // reset the "Formatting..." message
346 owner_->clearMessage();
354 void BufferView::Pimpl::repaint()
356 // Regenerate the screen.
357 screen().redraw(bv_->text, bv_);
361 void BufferView::Pimpl::updateScrollbar()
364 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
365 workarea().setScrollbarParams(0, 0, 0);
369 LyXText const & t = *bv_->text;
371 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
372 << t.top_y() << ", default height " << defaultRowHeight() << endl;
374 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
378 void BufferView::Pimpl::scrollDocView(int value)
380 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
385 screen().hideCursor();
387 screen().draw(bv_->text, bv_, value);
389 if (!lyxrc.cursor_follows_scrollbar)
392 LyXText * vbt = bv_->text;
394 int const height = defaultRowHeight();
395 int const first = static_cast<int>((bv_->text->top_y() + height));
396 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
398 if (vbt->cursor.y() < first)
399 vbt->setCursorFromCoordinates(0, first);
400 else if (vbt->cursor.y() > last)
401 vbt->setCursorFromCoordinates(0, last);
403 owner_->updateLayoutChoice();
407 void BufferView::Pimpl::scroll(int lines)
413 LyXText const * t = bv_->text;
414 int const line_height = defaultRowHeight();
416 // The new absolute coordinate
417 int new_top_y = t->top_y() + lines * line_height;
419 // Restrict to a valid value
420 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
421 new_top_y = std::max(0, new_top_y);
423 scrollDocView(new_top_y);
425 // Update the scrollbar.
426 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
430 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
431 key_modifier::state state)
433 bv_->owner()->getLyXFunc().processKeySym(key, state);
435 /* This is perhaps a bit of a hack. When we move
436 * around, or type, it's nice to be able to see
437 * the cursor immediately after the keypress. So
438 * we reset the toggle timeout and force the visibility
439 * of the cursor. Note we cannot do this inside
440 * dispatch() itself, because that's called recursively.
443 cursor_timeout.restart();
444 screen().showCursor(*bv_);
449 void BufferView::Pimpl::selectionRequested()
456 LyXText * text = bv_->getLyXText();
458 if (text->selection.set() &&
459 (!bv_->text->xsel_cache.set() ||
460 text->selection.start != bv_->text->xsel_cache.start ||
461 text->selection.end != bv_->text->xsel_cache.end))
463 bv_->text->xsel_cache = text->selection;
464 sel = text->selectionAsString(bv_->buffer(), false);
465 } else if (!text->selection.set()) {
467 bv_->text->xsel_cache.set(false);
470 workarea().putClipboard(sel);
475 void BufferView::Pimpl::selectionLost()
478 screen().hideCursor();
480 bv_->getLyXText()->clearSelection();
481 bv_->text->xsel_cache.set(false);
486 void BufferView::Pimpl::workAreaResize()
488 static int work_area_width;
489 static int work_area_height;
491 bool const widthChange = workarea().workWidth() != work_area_width;
492 bool const heightChange = workarea().workHeight() != work_area_height;
494 // update from work area
495 work_area_width = workarea().workWidth();
496 work_area_height = workarea().workHeight();
500 // The visible LyXView need a resize
501 resizeCurrentBuffer();
503 // Remove all texts from the textcache
504 // This is not _really_ what we want to do. What
505 // we really want to do is to delete in textcache
506 // that does not have a BufferView with matching
507 // width, but as long as we have only one BufferView
508 // deleting all gives the same result.
509 if (lyxerr.debugging())
510 textcache.show(lyxerr, "Expose delete all");
512 // FIXME: this is already done in resizeCurrentBuffer() ??
513 buffer_->resizeInsets(bv_);
514 } else if (heightChange) {
515 // fitCursor() ensures we don't jump back
516 // to the start of the document on vertical
522 if (widthChange || heightChange) {
526 // always make sure that the scrollbar is sane.
528 owner_->updateLayoutChoice();
533 void BufferView::Pimpl::update()
535 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
536 screen().update(*bv_);
537 bv_->text->clearPaint();
542 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
544 if (!text->selection.set() && (f & SELECT)) {
545 text->selection.cursor = text->cursor;
550 if (text->inset_owner) {
551 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
552 updateInset(text->inset_owner);
559 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
561 LyXText * text = bv_->text;
563 if (!text->selection.set() && (f & SELECT)) {
564 text->selection.cursor = text->cursor;
569 if (text->inset_owner) {
570 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
571 updateInset(text->inset_owner);
578 // Callback for cursor timer
579 void BufferView::Pimpl::cursorToggle()
582 cursor_timeout.restart();
586 screen().toggleCursor(*bv_);
588 cursor_timeout.restart();
592 bool BufferView::Pimpl::available() const
594 if (buffer_ && bv_->text)
600 Change const BufferView::Pimpl::getCurrentChange()
602 if (!bv_->buffer()->params.tracking_changes)
603 return Change(Change::UNCHANGED);
605 LyXText * t(bv_->getLyXText());
607 if (!t->selection.set())
608 return Change(Change::UNCHANGED);
610 LyXCursor const & cur(t->selection.start);
611 return cur.par()->lookupChangeFull(cur.pos());
615 void BufferView::Pimpl::beforeChange(LyXText * text)
618 text->clearSelection();
622 void BufferView::Pimpl::savePosition(unsigned int i)
624 if (i >= saved_positions_num)
626 saved_positions[i] = Position(buffer_->fileName(),
627 bv_->text->cursor.par()->id(),
628 bv_->text->cursor.pos());
630 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
634 void BufferView::Pimpl::restorePosition(unsigned int i)
636 if (i >= saved_positions_num)
639 string const fname = saved_positions[i].filename;
641 beforeChange(bv_->text);
643 if (fname != buffer_->fileName()) {
644 Buffer * b = bufferlist.exists(fname) ?
645 bufferlist.getBuffer(fname) :
646 bufferlist.loadLyXFile(fname); // don't ask, just load it
647 if (b != 0) buffer(b);
650 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
651 if (par == buffer_->par_iterator_end())
654 bv_->text->setCursor(par.pit(),
655 min(par->size(), saved_positions[i].par_pos));
657 update(BufferView::SELECT);
659 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
663 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
665 if (i >= saved_positions_num)
668 return !saved_positions[i].filename.empty();
672 void BufferView::Pimpl::switchKeyMap()
674 if (!lyxrc.rtl_support)
677 LyXText * text = bv_->getLyXText();
678 if (text->real_current_font.isRightToLeft()
679 && !(bv_->theLockingInset()
680 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
682 if (owner_->getIntl().keymap == Intl::PRIMARY)
683 owner_->getIntl().KeyMapSec();
685 if (owner_->getIntl().keymap == Intl::SECONDARY)
686 owner_->getIntl().KeyMapPrim();
691 void BufferView::Pimpl::insetUnlock()
693 if (bv_->theLockingInset()) {
694 bv_->theLockingInset()->insetUnlock(bv_);
695 bv_->theLockingInset(0);
701 void BufferView::Pimpl::toggleSelection(bool b)
703 if (bv_->theLockingInset())
704 bv_->theLockingInset()->toggleSelection(bv_, b);
705 screen().toggleSelection(bv_->text, bv_, b);
709 void BufferView::Pimpl::toggleToggle()
711 screen().toggleToggle(bv_->text, bv_);
715 void BufferView::Pimpl::center()
717 LyXText * t = bv_->text;
720 int const half_height = workarea().workHeight() / 2;
723 if (t->cursor.y() > half_height) {
724 new_y = t->cursor.y() - half_height;
727 // FIXME: look at this comment again ...
729 // FIXME: can we do this w/o calling screen directly ?
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()
738 screen().draw(t, bv_, new_y);
740 update(BufferView::SELECT);
744 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
746 workarea().putClipboard(stuff);
751 * Dispatch functions for actions which can be valid for BufferView->text
752 * and/or InsetText->text!!!
756 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
759 LyXCursor cursor = bv_->getLyXText()->cursor;
760 Buffer::inset_iterator it =
761 find_if(Buffer::inset_iterator(
762 cursor.par(), cursor.pos()),
763 buffer_->inset_iterator_end(),
764 lyx::compare_memfun(&Inset::lyxCode, code));
765 return it != buffer_->inset_iterator_end() ? (*it) : 0;
767 // Ok, this is a little bit too brute force but it
768 // should work for now. Better infrastructure is comming. (Lgb)
770 Buffer * b = bv_->buffer();
771 LyXCursor cursor = bv_->getLyXText()->cursor;
773 Buffer::inset_iterator beg = b->inset_iterator_begin();
774 Buffer::inset_iterator end = b->inset_iterator_end();
776 bool cursor_par_seen = false;
778 for (; beg != end; ++beg) {
779 if (beg.getPar() == cursor.par()) {
780 cursor_par_seen = true;
782 if (cursor_par_seen) {
783 if (beg.getPar() == cursor.par()
784 && beg.getPos() >= cursor.pos()) {
786 } else if (beg.getPar() != cursor.par()) {
793 // Now find the first inset that matches code.
794 for (; beg != end; ++beg) {
795 if (beg->lyxCode() == code) {
805 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
807 string filename = filen;
809 if (filename.empty()) {
810 // Launch a file browser
811 string initpath = lyxrc.document_path;
814 string const trypath = owner_->buffer()->filePath();
815 // If directory is writeable, use this as default.
816 if (IsDirWriteable(trypath))
820 FileDialog fileDlg(_("Select LyX document to insert"),
822 make_pair(string(_("Documents|#o#O")),
823 string(lyxrc.document_path)),
824 make_pair(string(_("Examples|#E#e")),
825 string(AddPath(system_lyxdir, "examples"))));
827 FileDialog::Result result =
828 fileDlg.open(initpath,
829 _("*.lyx| LyX Documents (*.lyx)"));
831 if (result.first == FileDialog::Later)
834 filename = result.second;
836 // check selected filename
837 if (filename.empty()) {
838 owner_->message(_("Canceled."));
843 // get absolute path of file and add ".lyx" to the filename if
845 filename = FileSearch(string(), filename, "lyx");
847 string const disp_fn = MakeDisplayPath(filename);
848 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
849 bool const res = bv_->insertLyXFile(filename);
851 owner_->message(bformat(_("Document %1$s inserted."), disp_fn));
853 owner_->message(bformat(_("Could not insert document %1$s"), disp_fn));
857 void BufferView::Pimpl::trackChanges()
859 Buffer * buf(bv_->buffer());
860 bool const tracking(buf->params.tracking_changes);
863 ParIterator const end = buf->par_iterator_end();
864 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
866 buf->params.tracking_changes = true;
868 // we cannot allow undos beyond the freeze point
869 buf->undostack.clear();
871 update(BufferView::SELECT);
872 bv_->text->setCursor(buf->paragraphs.begin(), 0);
873 #warning changes FIXME
874 //moveCursorUpdate(false);
876 bool found = lyxfind::findNextChange(bv_);
878 owner_->getDialogs().show("changes");
882 ParIterator const end = buf->par_iterator_end();
883 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
884 it->untrackChanges();
885 buf->params.tracking_changes = false;
888 buf->redostack.clear();
892 // Doesn't go through lyxfunc, so we need to update the
893 // layout choice etc. ourselves
894 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
896 // e.g. Qt mouse press when no buffer
900 screen().hideCursor();
902 bool const res = dispatch(ev_in);
904 // see workAreaKeyPress
905 cursor_timeout.restart();
906 screen().showCursor(*bv_);
908 // FIXME: we should skip these when selecting
909 bv_->owner()->updateLayoutChoice();
910 bv_->owner()->updateToolbar();
913 // slight hack: this is only called currently when
914 // we clicked somewhere, so we force through the display
915 // of the new status here.
916 bv_->owner()->clearMessage();
922 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
924 // Make sure that the cached BufferView is correct.
925 FuncRequest ev = ev_in;
928 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
929 << " action[" << ev.action << ']'
930 << " arg[" << ev.argument << ']'
931 << " x[" << ev.x << ']'
932 << " y[" << ev.y << ']'
933 << " button[" << ev.button() << ']'
936 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
940 case LFUN_SCROLL_INSET:
941 // this is not handled here as this function is only active
942 // if we have a locking_inset and that one is (or contains)
946 case LFUN_FILE_INSERT:
947 MenuInsertLyXFile(ev.argument);
950 case LFUN_FILE_INSERT_ASCII_PARA:
951 InsertAsciiFile(bv_, ev.argument, true);
954 case LFUN_FILE_INSERT_ASCII:
955 InsertAsciiFile(bv_, ev.argument, false);
959 lang(bv_, ev.argument);
961 owner_->view_state_changed();
966 owner_->view_state_changed();
971 owner_->view_state_changed();
976 owner_->view_state_changed();
981 owner_->view_state_changed();
986 owner_->view_state_changed();
991 owner_->view_state_changed();
996 owner_->view_state_changed();
1001 owner_->view_state_changed();
1004 case LFUN_FONT_SIZE:
1005 fontSize(bv_, ev.argument);
1006 owner_->view_state_changed();
1009 case LFUN_FONT_STATE:
1010 owner_->getLyXFunc().setMessage(currentState(bv_));
1013 case LFUN_INSERT_LABEL: {
1014 // Try and generate a valid label
1015 string const contents = ev.argument.empty() ?
1016 getPossibleLabel(*bv_) : ev.argument;
1017 InsetCommandParams icp("label", contents);
1018 string data = InsetCommandMailer::params2string("label", icp);
1019 owner_->getDialogs().show("label", data, 0);
1023 case LFUN_BOOKMARK_SAVE:
1024 savePosition(strToUnsignedInt(ev.argument));
1027 case LFUN_BOOKMARK_GOTO:
1028 restorePosition(strToUnsignedInt(ev.argument));
1031 case LFUN_REF_GOTO: {
1032 string label = ev.argument;
1033 if (label.empty()) {
1035 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1037 label = inset->getContents();
1043 bv_->gotoLabel(label);
1047 // --- accented characters ---------------------------
1050 case LFUN_CIRCUMFLEX:
1060 case LFUN_SPECIAL_CARON:
1063 case LFUN_HUNG_UMLAUT:
1066 if (ev.argument.empty()) {
1068 owner_->getLyXFunc().handleKeyFunc(ev.action);
1070 owner_->getLyXFunc().handleKeyFunc(ev.action);
1071 owner_->getIntl().getTransManager()
1072 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1073 update(bv_->getLyXText(), BufferView::SELECT);
1077 case LFUN_MATH_MACRO:
1078 case LFUN_MATH_DELIM:
1079 case LFUN_INSERT_MATRIX:
1080 case LFUN_INSERT_MATH:
1081 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1082 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1083 case LFUN_MATH_MODE: // Open or create an inlined math inset
1087 case LFUN_INSET_APPLY: {
1088 string const name = ev.getArg(0);
1090 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1092 // This works both for 'original' and 'mathed' insets.
1093 // Note that the localDispatch performs updateInset
1095 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1096 inset->localDispatch(fr);
1098 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1104 case LFUN_INSET_INSERT: {
1105 Inset * inset = createInset(ev);
1106 if (inset && insertInset(inset)) {
1109 string const name = ev.getArg(0);
1110 if (name == "bibitem") {
1111 // We need to do a redraw because the maximum
1112 // InsetBibitem width could have changed
1113 #warning please check you mean repaint() not update(),
1114 #warning and whether the repaint() is needed at all
1124 case LFUN_FLOAT_LIST:
1125 if (tclass.floats().typeExist(ev.argument)) {
1126 Inset * inset = new InsetFloatList(ev.argument);
1127 if (!insertInset(inset, tclass.defaultLayoutName()))
1130 lyxerr << "Non-existent float type: "
1131 << ev.argument << endl;
1135 case LFUN_LAYOUT_PARAGRAPH: {
1136 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1141 params2string(*par, data);
1143 data = "show\n" + data;
1144 bv_->owner()->getDialogs().show("paragraph", data);
1148 case LFUN_PARAGRAPH_UPDATE: {
1149 if (!bv_->owner()->getDialogs().visible("paragraph"))
1151 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1156 params2string(*par, data);
1158 // Will the paragraph accept changes from the dialog?
1159 Inset * const inset = par->inInset();
1161 !(inset && inset->forceDefaultParagraphs(inset));
1163 data = "update " + tostr(accept) + '\n' + data;
1164 bv_->owner()->getDialogs().update("paragraph", data);
1168 case LFUN_PARAGRAPH_APPLY:
1169 setParagraphParams(*bv_, ev.argument);
1172 case LFUN_THESAURUS_ENTRY:
1174 string arg = ev.argument;
1177 arg = bv_->getLyXText()->selectionAsString(buffer_,
1181 if (arg.size() > 100 || arg.empty()) {
1182 // Get word or selection
1183 bv_->getLyXText()->selectWordWhenUnderCursor(LyXText::WHOLE_WORD);
1184 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1185 // FIXME: where is getLyXText()->unselect(bv_) ?
1189 bv_->owner()->getDialogs().show("thesaurus", arg);
1193 case LFUN_TRACK_CHANGES:
1197 case LFUN_MERGE_CHANGES:
1198 owner_->getDialogs().show("changes");
1201 case LFUN_ACCEPT_ALL_CHANGES: {
1202 update(BufferView::SELECT);
1203 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1204 #warning FIXME changes
1205 //moveCursorUpdate(false);
1207 while (lyxfind::findNextChange(bv_)) {
1208 bv_->getLyXText()->acceptChange();
1210 update(BufferView::SELECT);
1214 case LFUN_REJECT_ALL_CHANGES: {
1215 update(BufferView::SELECT);
1216 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1217 #warning FIXME changes
1218 //moveCursorUpdate(false);
1220 while (lyxfind::findNextChange(bv_)) {
1221 bv_->getLyXText()->rejectChange();
1223 update(BufferView::SELECT);
1227 case LFUN_ACCEPT_CHANGE: {
1228 bv_->getLyXText()->acceptChange();
1229 update(BufferView::SELECT);
1233 case LFUN_REJECT_CHANGE: {
1234 bv_->getLyXText()->rejectChange();
1235 update(BufferView::SELECT);
1239 case LFUN_UNKNOWN_ACTION:
1240 ev.errorMessage(N_("Unknown function!"));
1244 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1251 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1253 // if we are in a locking inset we should try to insert the
1254 // inset there otherwise this is a illegal function now
1255 if (bv_->theLockingInset()) {
1256 if (bv_->theLockingInset()->insetAllowed(inset))
1257 return bv_->theLockingInset()->insertInset(bv_, inset);
1261 // not quite sure if we want this...
1262 setCursorParUndo(bv_);
1265 beforeChange(bv_->text);
1266 if (!lout.empty()) {
1267 update(BufferView::SELECT);
1268 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1269 update(BufferView::SELECT);
1271 if (!bv_->text->cursor.par()->empty()) {
1272 bv_->text->cursorLeft(bv_);
1274 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1275 update(BufferView::SELECT);
1279 LyXTextClass const & tclass =
1280 buffer_->params.getLyXTextClass();
1281 bool hasLayout = tclass.hasLayout(lres);
1282 string lay = tclass.defaultLayoutName();
1284 if (hasLayout != false) {
1288 // layout not fount using default
1289 lay = tclass.defaultLayoutName();
1292 bv_->text->setLayout(lay);
1294 bv_->text->setParagraph(0, 0,
1296 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1301 update(BufferView::SELECT);
1304 bv_->text->insertInset(inset);
1305 update(BufferView::SELECT);
1312 void BufferView::Pimpl::updateInset(Inset * inset)
1314 if (!inset || !available())
1317 // first check for locking insets
1318 if (bv_->theLockingInset()) {
1319 if (bv_->theLockingInset() == inset) {
1320 if (bv_->text->updateInset(inset)) {
1325 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1326 if (bv_->text->updateInset(bv_->theLockingInset())) {
1334 // then check if the inset is a top_level inset (has no owner)
1335 // if yes do the update as always otherwise we have to update the
1336 // toplevel inset where this inset is inside
1337 Inset * tl_inset = inset;
1338 while (tl_inset->owner())
1339 tl_inset = tl_inset->owner();
1340 if (tl_inset == inset) {
1341 update(BufferView::UPDATE);
1342 if (bv_->text->updateInset(inset)) {
1343 update(BufferView::SELECT);
1346 } else if (static_cast<UpdatableInset *>(tl_inset)
1347 ->updateInsetInInset(bv_, inset))
1349 if (bv_->text->updateInset(tl_inset)) {