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"
13 #include "bufferview_funcs.h"
14 #include "commandtags.h"
17 #include "FloatList.h"
18 #include "funcrequest.h"
21 #include "iterators.h"
22 #include "lyx_cb.h" // added for Dispatch functions
29 #include "paragraph.h"
30 #include "ParagraphParameters.h"
31 #include "TextCache.h"
32 #include "undo_funcs.h"
34 #include "insets/insetfloatlist.h"
35 #include "insets/insetgraphics.h"
36 #include "insets/insetinclude.h"
37 #include "insets/insetref.h"
38 #include "insets/insettext.h"
40 #include "frontends/Alert.h"
41 #include "frontends/Dialogs.h"
42 #include "frontends/FileDialog.h"
43 #include "frontends/LyXView.h"
44 #include "frontends/LyXScreenFactory.h"
45 #include "frontends/mouse_state.h"
46 #include "frontends/screen.h"
47 #include "frontends/WorkArea.h"
48 #include "frontends/WorkAreaFactory.h"
50 #include "mathed/formulabase.h"
52 #include "graphics/Previews.h"
54 #include "support/LAssert.h"
55 #include "support/lstrings.h"
56 #include "support/filetools.h"
58 #include <boost/bind.hpp>
59 #include <boost/signals/connection.hpp>
60 #include "BoostFormat.h"
76 extern BufferList bufferlist;
81 unsigned int const saved_positions_num = 20;
83 // All the below connection objects are needed because of a bug in some
84 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
85 // to these connections we avoid a segfault upon startup, and also at exit.
88 boost::signals::connection dispatchcon;
89 boost::signals::connection timecon;
90 boost::signals::connection doccon;
91 boost::signals::connection resizecon;
92 boost::signals::connection kpresscon;
93 boost::signals::connection selectioncon;
94 boost::signals::connection lostcon;
100 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
101 int xpos, int ypos, int width, int height)
102 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
103 using_xterm_cursor(false)
105 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
106 screen_.reset(LyXScreenFactory::create(workarea()));
109 doccon = workarea().scrollDocView
110 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
111 resizecon = workarea().workAreaResize
112 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
113 dispatchcon = workarea().dispatch
114 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
115 kpresscon = workarea().workAreaKeyPress
116 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
117 selectioncon = workarea().selectionRequested
118 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
119 lostcon = workarea().selectionLost
120 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
122 timecon = cursor_timeout.timeout
123 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
124 cursor_timeout.start();
125 saved_positions.resize(saved_positions_num);
129 WorkArea & BufferView::Pimpl::workarea() const
131 return *workarea_.get();
135 LyXScreen & BufferView::Pimpl::screen() const
137 return *screen_.get();
141 Painter & BufferView::Pimpl::painter() const
143 return workarea().getPainter();
147 void BufferView::Pimpl::buffer(Buffer * b)
149 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
152 buffer_->delUser(bv_);
154 // Put the old text into the TextCache, but
155 // only if the buffer is still loaded.
156 // Also set the owner of the test to 0
157 // bv_->text->owner(0);
158 textcache.add(buffer_, workarea().workWidth(), bv_->text);
159 if (lyxerr.debugging())
160 textcache.show(lyxerr, "BufferView::buffer");
165 // set current buffer
168 // if we're quitting lyx, don't bother updating stuff
172 // if we are closing the buffer, use the first buffer as current
174 buffer_ = bufferlist.first();
178 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
179 buffer_->addUser(bv_);
181 // If we don't have a text object for this, we make one
182 if (bv_->text == 0) {
183 resizeCurrentBuffer();
186 // FIXME: needed when ?
187 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
189 // Similarly, buffer-dependent dialogs should be updated or
190 // hidden. This should go here because some dialogs (eg ToC)
191 // require bv_->text.
192 owner_->getDialogs().updateBufferDependent(true);
194 lyxerr[Debug::INFO] << " No Buffer!" << endl;
195 owner_->getDialogs().hideBufferDependent();
197 // Also remove all remaining text's from the testcache.
198 // (there should not be any!) (if there is any it is a
200 if (lyxerr.debugging())
201 textcache.show(lyxerr, "buffer delete all");
207 owner_->updateMenubar();
208 owner_->updateToolbar();
209 owner_->updateLayoutChoice();
210 owner_->updateWindowTitle();
212 if (grfx::Previews::activated() && buffer_)
213 grfx::Previews::get().generateBufferPreviews(*buffer_);
217 bool BufferView::Pimpl::fitCursor()
221 if (bv_->theLockingInset()) {
222 bv_->theLockingInset()->fitInsetCursor(bv_);
225 ret = screen().fitCursor(bv_->text, bv_);
228 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
236 void BufferView::Pimpl::redoCurrentBuffer()
238 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
239 if (buffer_ && bv_->text) {
240 resizeCurrentBuffer();
242 owner_->updateLayoutChoice();
248 int BufferView::Pimpl::resizeCurrentBuffer()
250 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
253 Paragraph * selstartpar = 0;
254 Paragraph * selendpar = 0;
255 UpdatableInset * the_locking_inset = 0;
258 pos_type selstartpos = 0;
259 pos_type selendpos = 0;
260 bool selection = false;
261 bool mark_set = false;
265 owner_->message(_("Formatting document..."));
268 par = bv_->text->cursor.par();
269 pos = bv_->text->cursor.pos();
270 selstartpar = bv_->text->selection.start.par();
271 selstartpos = bv_->text->selection.start.pos();
272 selendpar = bv_->text->selection.end.par();
273 selendpos = bv_->text->selection.end.pos();
274 selection = bv_->text->selection.set();
275 mark_set = bv_->text->selection.mark();
276 the_locking_inset = bv_->theLockingInset();
277 buffer_->resizeInsets(bv_);
278 // I don't think the delete and new are necessary here we just could
279 // call only init! (Jug 20020419)
281 bv_->text = new LyXText(bv_);
282 bv_->text->init(bv_);
284 // See if we have a text in TextCache that fits
285 // the new buffer_ with the correct width.
286 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
288 if (lyxerr.debugging()) {
289 lyxerr << "Found a LyXText that fits:\n";
290 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
292 // Set the owner of the newly found text
293 // bv_->text->owner(bv_);
294 if (lyxerr.debugging())
295 textcache.show(lyxerr, "resizeCurrentBuffer");
297 buffer_->resizeInsets(bv_);
299 bv_->text = new LyXText(bv_);
300 bv_->text->init(bv_);
301 //buffer_->resizeInsets(bv_);
306 bv_->text->selection.set(true);
307 // At this point just to avoid the Delete-Empty-Paragraph-
308 // Mechanism when setting the cursor.
309 bv_->text->selection.mark(mark_set);
311 bv_->text->setCursor(selstartpar, selstartpos);
312 bv_->text->selection.cursor = bv_->text->cursor;
313 bv_->text->setCursor(selendpar, selendpos);
314 bv_->text->setSelection();
315 bv_->text->setCursor(par, pos);
317 bv_->text->setCursor(par, pos);
318 bv_->text->selection.cursor = bv_->text->cursor;
319 bv_->text->selection.set(false);
321 // remake the inset locking
322 bv_->theLockingInset(the_locking_inset);
325 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
336 void BufferView::Pimpl::repaint()
338 // Regenerate the screen.
339 screen().redraw(bv_->text, bv_);
343 void BufferView::Pimpl::updateScrollbar()
346 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
347 workarea().setScrollbarParams(0, 0, 0);
351 LyXText const & t = *bv_->text;
353 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
354 << t.top_y() << ", default height " << defaultRowHeight() << endl;
356 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
360 void BufferView::Pimpl::scrollDocView(int value)
362 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
367 screen().draw(bv_->text, bv_, value);
369 if (!lyxrc.cursor_follows_scrollbar)
372 LyXText * vbt = bv_->text;
374 int const height = defaultRowHeight();
375 int const first = static_cast<int>((bv_->text->top_y() + height));
376 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
378 if (vbt->cursor.y() < first)
379 vbt->setCursorFromCoordinates(0, first);
380 else if (vbt->cursor.y() > last)
381 vbt->setCursorFromCoordinates(0, last);
385 void BufferView::Pimpl::scroll(int lines)
391 LyXText const * t = bv_->text;
392 int const line_height = defaultRowHeight();
394 // The new absolute coordinate
395 int new_top_y = t->top_y() + lines * line_height;
397 // Restrict to a valid value
398 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
399 new_top_y = std::max(0, new_top_y);
401 scrollDocView(new_top_y);
403 // Update the scrollbar.
404 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
408 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
409 key_modifier::state state)
411 bv_->owner()->getLyXFunc().processKeySym(key, state);
415 void BufferView::Pimpl::selectionRequested()
422 LyXText * text = bv_->getLyXText();
424 if (text->selection.set() &&
425 (!bv_->text->xsel_cache.set() ||
426 text->selection.start != bv_->text->xsel_cache.start ||
427 text->selection.end != bv_->text->xsel_cache.end))
429 bv_->text->xsel_cache = text->selection;
430 sel = text->selectionAsString(bv_->buffer(), false);
431 } else if (!text->selection.set()) {
433 bv_->text->xsel_cache.set(false);
436 workarea().putClipboard(sel);
441 void BufferView::Pimpl::selectionLost()
446 bv_->getLyXText()->clearSelection();
448 bv_->text->xsel_cache.set(false);
453 void BufferView::Pimpl::workAreaResize()
455 static int work_area_width;
456 static int work_area_height;
458 bool const widthChange = workarea().workWidth() != work_area_width;
459 bool const heightChange = workarea().workHeight() != work_area_height;
461 // update from work area
462 work_area_width = workarea().workWidth();
463 work_area_height = workarea().workHeight();
467 // The visible LyXView need a resize
468 resizeCurrentBuffer();
470 // Remove all texts from the textcache
471 // This is not _really_ what we want to do. What
472 // we really want to do is to delete in textcache
473 // that does not have a BufferView with matching
474 // width, but as long as we have only one BufferView
475 // deleting all gives the same result.
476 if (lyxerr.debugging())
477 textcache.show(lyxerr, "Expose delete all");
479 // FIXME: this is already done in resizeCurrentBuffer() ??
480 buffer_->resizeInsets(bv_);
481 } else if (heightChange) {
482 // fitCursor() ensures we don't jump back
483 // to the start of the document on vertical
489 if (widthChange || heightChange) {
493 // always make sure that the scrollbar is sane.
495 owner_->updateLayoutChoice();
500 void BufferView::Pimpl::update()
502 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
503 screen().update(*bv_);
505 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
506 bv_->text->fullRebreak();
507 bv_->text->setCursor(bv_->text->cursor.par(),
508 bv_->text->cursor.pos());
509 if (bv_->text->selection.set()) {
510 bv_->text->setCursor(bv_->text->selection.start,
511 bv_->text->selection.start.par(),
512 bv_->text->selection.start.pos());
513 bv_->text->setCursor(bv_->text->selection.end,
514 bv_->text->selection.end.par(),
515 bv_->text->selection.end.pos());
518 bv_->text->postPaint(0);
519 screen().update(*bv_);
522 // do this here instead of in the screen::update because of
524 bv_->text->clearPaint();
531 // Values used when calling update:
533 // -2 - update, move sel_cursor if selection, fitcursor
534 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
535 // 0 - update, move sel_cursor if selection, fitcursor
536 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
537 // 3 - update, move sel_cursor if selection
540 // a simple redraw of the parts that need refresh
542 // move sel_cursor if selection -
543 // the text's sel_cursor is moved if there is selection is progress
546 // fitCursor() is called and the scrollbar updated
549 // the buffer is marked dirty.
558 // UPDATE_ONLY = UPDATE;
559 // UPDATE_SELECT = UPDATE | SELECT;
560 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
561 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
563 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
564 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
565 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
566 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
567 // update(3) -> update(1) -> update(1) -> update(SELECT)
569 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
571 owner_->updateLayoutChoice();
573 if (!text->selection.set() && (f & SELECT)) {
574 text->selection.cursor = text->cursor;
579 if (text->inset_owner) {
580 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
581 updateInset(text->inset_owner, false);
591 buffer_->markDirty();
596 // Callback for cursor timer
597 void BufferView::Pimpl::cursorToggle()
600 cursor_timeout.restart();
604 if (!bv_->theLockingInset()) {
605 screen().cursorToggle(bv_);
607 bv_->theLockingInset()->toggleInsetCursor(bv_);
610 cursor_timeout.restart();
614 bool BufferView::Pimpl::available() const
616 if (buffer_ && bv_->text)
622 Change const BufferView::Pimpl::getCurrentChange()
624 if (!bv_->buffer()->params.tracking_changes)
625 return Change(Change::UNCHANGED);
627 LyXText * t(bv_->getLyXText());
629 if (!t->selection.set())
630 return Change(Change::UNCHANGED);
632 LyXCursor const & cur(t->selection.start);
633 return cur.par()->lookupChangeFull(cur.pos());
637 void BufferView::Pimpl::beforeChange(LyXText * text)
640 text->clearSelection();
644 void BufferView::Pimpl::savePosition(unsigned int i)
646 if (i >= saved_positions_num)
648 saved_positions[i] = Position(buffer_->fileName(),
649 bv_->text->cursor.par()->id(),
650 bv_->text->cursor.pos());
654 str << boost::format(_("Saved bookmark %1$d")) % i;
656 str << _("Saved bookmark ") << i;
658 owner_->message(STRCONV(str.str()));
663 void BufferView::Pimpl::restorePosition(unsigned int i)
665 if (i >= saved_positions_num)
668 string const fname = saved_positions[i].filename;
670 beforeChange(bv_->text);
672 if (fname != buffer_->fileName()) {
673 Buffer * b = bufferlist.exists(fname) ?
674 bufferlist.getBuffer(fname) :
675 bufferlist.loadLyXFile(fname); // don't ask, just load it
676 if (b != 0) buffer(b);
679 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
683 bv_->text->setCursor(par,
684 min(par->size(), saved_positions[i].par_pos));
686 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
690 str << boost::format(_("Moved to bookmark %1$d")) % i;
692 str << _("Moved to bookmark ") << i;
694 owner_->message(STRCONV(str.str()));
699 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
701 if (i >= saved_positions_num)
704 return !saved_positions[i].filename.empty();
708 void BufferView::Pimpl::switchKeyMap()
710 if (!lyxrc.rtl_support)
713 LyXText * text = bv_->getLyXText();
714 if (text->real_current_font.isRightToLeft()
715 && !(bv_->theLockingInset()
716 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
718 if (owner_->getIntl().keymap == Intl::PRIMARY)
719 owner_->getIntl().KeyMapSec();
721 if (owner_->getIntl().keymap == Intl::SECONDARY)
722 owner_->getIntl().KeyMapPrim();
727 void BufferView::Pimpl::insetUnlock()
729 if (bv_->theLockingInset()) {
730 bv_->theLockingInset()->insetUnlock(bv_);
731 bv_->theLockingInset(0);
737 void BufferView::Pimpl::showCursor()
739 if (bv_->theLockingInset())
740 bv_->theLockingInset()->showInsetCursor(bv_);
742 screen().showCursor(bv_->text, bv_);
746 void BufferView::Pimpl::hideCursor()
748 if (!bv_->theLockingInset())
749 screen().hideCursor();
753 void BufferView::Pimpl::toggleSelection(bool b)
755 if (bv_->theLockingInset())
756 bv_->theLockingInset()->toggleSelection(bv_, b);
757 screen().toggleSelection(bv_->text, bv_, b);
761 void BufferView::Pimpl::toggleToggle()
763 screen().toggleToggle(bv_->text, bv_);
767 void BufferView::Pimpl::center()
769 LyXText * t = bv_->text;
772 int const half_height = workarea().workHeight() / 2;
775 if (t->cursor.y() > half_height) {
776 new_y = t->cursor.y() - half_height;
779 // FIXME: can we do this w/o calling screen directly ?
780 // This updates top_y() but means the fitCursor() call
781 // from the update(FITCUR) doesn't realise that we might
782 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
783 // the scrollbar to be updated as it should, so we have
784 // to do it manually. Any operation that does a center()
785 // and also might have moved top_y() must make sure to call
786 // updateScrollbar() currently. Never mind that this is a
787 // pretty obfuscated way of updating t->top_y()
788 screen().draw(t, bv_, new_y);
790 update(t, BufferView::SELECT | BufferView::FITCUR);
794 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
796 workarea().putClipboard(stuff);
801 * Dispatch functions for actions which can be valid for BufferView->text
802 * and/or InsetText->text!!!
806 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
809 LyXCursor cursor = bv_->getLyXText()->cursor;
810 Buffer::inset_iterator it =
811 find_if(Buffer::inset_iterator(
812 cursor.par(), cursor.pos()),
813 buffer_->inset_iterator_end(),
814 lyx::compare_memfun(&Inset::lyxCode, code));
815 return it != buffer_->inset_iterator_end() ? (*it) : 0;
817 // Ok, this is a little bit too brute force but it
818 // should work for now. Better infrastructure is comming. (Lgb)
820 Buffer * b = bv_->buffer();
821 LyXCursor cursor = bv_->getLyXText()->cursor;
823 Buffer::inset_iterator beg = b->inset_iterator_begin();
824 Buffer::inset_iterator end = b->inset_iterator_end();
826 bool cursor_par_seen = false;
828 for (; beg != end; ++beg) {
829 if (beg.getPar() == cursor.par()) {
830 cursor_par_seen = true;
832 if (cursor_par_seen) {
833 if (beg.getPar() == cursor.par()
834 && beg.getPos() >= cursor.pos()) {
836 } else if (beg.getPar() != cursor.par()) {
843 // Now find the first inset that matches code.
844 for (; beg != end; ++beg) {
845 if (beg->lyxCode() == code) {
855 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
857 string filename = filen;
859 if (filename.empty()) {
860 // Launch a file browser
861 string initpath = lyxrc.document_path;
864 string const trypath = owner_->buffer()->filePath();
865 // If directory is writeable, use this as default.
866 if (IsDirWriteable(trypath))
870 FileDialog fileDlg(_("Select LyX document to insert"),
872 make_pair(string(_("Documents|#o#O")),
873 string(lyxrc.document_path)),
874 make_pair(string(_("Examples|#E#e")),
875 string(AddPath(system_lyxdir, "examples"))));
877 FileDialog::Result result =
878 fileDlg.open(initpath,
879 _("*.lyx| LyX Documents (*.lyx)"));
881 if (result.first == FileDialog::Later)
884 filename = result.second;
886 // check selected filename
887 if (filename.empty()) {
888 owner_->message(_("Canceled."));
893 // get absolute path of file and add ".lyx" to the filename if
895 filename = FileSearch(string(), filename, "lyx");
897 string const disp_fn(MakeDisplayPath(filename));
901 s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
903 s1 << _("Inserting document ") << disp_fn << _("...");
905 owner_->message(STRCONV(s1.str()));
906 bool const res = bv_->insertLyXFile(filename);
910 str << boost::format(_("Document %1$s inserted.")) % disp_fn;
912 str << _("Document ") << disp_fn << _(" inserted.");
914 owner_->message(STRCONV(str.str()));
918 str << boost::format(_("Could not insert document %1$s")) % disp_fn;
920 str << _("Could not insert document ") << disp_fn;
922 owner_->message(STRCONV(str.str()));
927 void BufferView::Pimpl::trackChanges()
929 Buffer * buf(bv_->buffer());
930 bool const tracking(buf->params.tracking_changes);
933 ParIterator const end = buf->par_iterator_end();
934 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
935 (*it)->trackChanges();
937 buf->params.tracking_changes = true;
939 // we cannot allow undos beyond the freeze point
940 buf->undostack.clear();
942 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
943 bv_->text->setCursor(&(*buf->paragraphs.begin()), 0);
944 #warning changes FIXME
945 //moveCursorUpdate(false);
947 bool found = lyxfind::findNextChange(bv_);
949 owner_->getDialogs().show("changes");
953 ParIterator const end = buf->par_iterator_end();
954 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
955 (*it)->untrackChanges();
957 buf->params.tracking_changes = false;
960 buf->redostack.clear();
964 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
966 // Make sure that the cached BufferView is correct.
967 FuncRequest ev = ev_in;
970 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
971 << " action[" << ev.action << ']'
972 << " arg[" << ev.argument << ']'
973 << " x[" << ev.x << ']'
974 << " y[" << ev.y << ']'
975 << " button[" << ev.button() << ']'
978 // e.g. Qt mouse press when no buffer
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_LAYOUT_COPY:
993 bv_->copyEnvironment();
996 case LFUN_LAYOUT_PASTE:
997 bv_->pasteEnvironment();
1001 case LFUN_FILE_INSERT:
1002 MenuInsertLyXFile(ev.argument);
1005 case LFUN_FILE_INSERT_ASCII_PARA:
1006 InsertAsciiFile(bv_, ev.argument, true);
1009 case LFUN_FILE_INSERT_ASCII:
1010 InsertAsciiFile(bv_, ev.argument, false);
1014 lang(bv_, ev.argument);
1016 owner_->view_state_changed();
1021 owner_->view_state_changed();
1026 owner_->view_state_changed();
1031 owner_->view_state_changed();
1036 owner_->view_state_changed();
1041 owner_->view_state_changed();
1046 owner_->view_state_changed();
1051 owner_->view_state_changed();
1054 case LFUN_UNDERLINE:
1056 owner_->view_state_changed();
1059 case LFUN_FONT_SIZE:
1060 fontSize(bv_, ev.argument);
1061 owner_->view_state_changed();
1064 case LFUN_FONT_STATE:
1065 owner_->getLyXFunc().setMessage(currentState(bv_));
1068 case LFUN_INSERT_LABEL: {
1069 // Try and generate a valid label
1070 string const contents = ev.argument.empty() ?
1071 getPossibleLabel(*bv_) : ev.argument;
1072 InsetCommandParams icp("label", contents);
1073 string data = InsetCommandMailer::params2string("label", icp);
1074 owner_->getDialogs().show("label", data, 0);
1078 case LFUN_BOOKMARK_SAVE:
1079 savePosition(strToUnsignedInt(ev.argument));
1082 case LFUN_BOOKMARK_GOTO:
1083 restorePosition(strToUnsignedInt(ev.argument));
1088 string label = ev.argument;
1089 if (label.empty()) {
1091 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1093 label = inset->getContents();
1098 if (!label.empty()) {
1099 //bv_->savePosition(0);
1100 if (!bv_->gotoLabel(label))
1101 Alert::alert(_("Error"),
1102 _("Couldn't find this label"),
1103 _("in current document."));
1108 // --- accented characters ---------------------------
1111 case LFUN_CIRCUMFLEX:
1121 case LFUN_SPECIAL_CARON:
1124 case LFUN_HUNG_UMLAUT:
1127 if (ev.argument.empty()) {
1129 owner_->getLyXFunc().handleKeyFunc(ev.action);
1131 owner_->getLyXFunc().handleKeyFunc(ev.action);
1132 owner_->getIntl().getTransManager()
1133 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1134 update(bv_->getLyXText(),
1136 | BufferView::FITCUR
1137 | BufferView::CHANGE);
1141 case LFUN_MATH_MACRO:
1142 case LFUN_MATH_DELIM:
1143 case LFUN_INSERT_MATRIX:
1144 case LFUN_INSERT_MATH:
1145 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1146 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1147 case LFUN_MATH_MODE: // Open or create an inlined math inset
1148 case LFUN_GREEK: // Insert a single greek letter
1152 case LFUN_INSET_APPLY: {
1153 string const name = ev.getArg(0);
1155 InsetBase * base = owner_->getDialogs().getOpenInset(name);
1157 // This works both for 'original' and 'mathed' insets.
1158 // Note that the localDispatch performs updateInset
1160 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1161 base->localDispatch(fr);
1163 Inset * inset = createInset(ev);
1164 if (inset && insertInset(inset)) {
1165 updateInset(inset, true);
1171 if (name == "bibitem") {
1172 // We need to do a redraw because the maximum
1173 // InsetBibitem width could have changed
1174 #warning please check you mean repaint() not update(),
1175 #warning and whether the repaint() is needed at all
1182 case LFUN_FLOAT_LIST:
1183 if (tclass.floats().typeExist(ev.argument)) {
1184 Inset * inset = new InsetFloatList(ev.argument);
1185 if (!insertInset(inset, tclass.defaultLayoutName()))
1188 lyxerr << "Non-existent float type: "
1189 << ev.argument << endl;
1193 case LFUN_LAYOUT_PARAGRAPH: {
1194 Paragraph const * par = bv_->getLyXText()->cursor.par();
1199 params2string(*par, data);
1201 data = "show\n" + data;
1202 bv_->owner()->getDialogs().show("paragraph", data);
1206 case LFUN_PARAGRAPH_UPDATE: {
1207 Paragraph const * par = bv_->getLyXText()->cursor.par();
1212 params2string(*par, data);
1214 // Will the paragraph accept changes from the dialog?
1215 Inset * const inset = par->inInset();
1217 !(inset && inset->forceDefaultParagraphs(inset));
1219 data = "update " + tostr(accept) + '\n' + data;
1220 bv_->owner()->getDialogs().update("paragraph", data);
1224 case LFUN_PARAGRAPH_APPLY:
1225 setParagraphParams(*bv_, ev.argument);
1228 case LFUN_THESAURUS_ENTRY:
1230 string arg = ev.argument;
1233 arg = bv_->getLyXText()->selectionAsString(buffer_,
1237 if (arg.size() > 100 || arg.empty()) {
1238 // Get word or selection
1239 bv_->getLyXText()->selectWordWhenUnderCursor(LyXText::WHOLE_WORD);
1240 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1241 // FIXME: where is getLyXText()->unselect(bv_) ?
1245 bv_->owner()->getDialogs().showThesaurus(arg);
1249 case LFUN_TRACK_CHANGES:
1253 case LFUN_MERGE_CHANGES:
1254 owner_->getDialogs().show("changes");
1257 case LFUN_ACCEPT_ALL_CHANGES: {
1258 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1259 bv_->text->setCursor(&(*bv_->buffer()->paragraphs.begin()), 0);
1260 #warning FIXME changes
1261 //moveCursorUpdate(false);
1263 while (lyxfind::findNextChange(bv_)) {
1264 bv_->getLyXText()->acceptChange();
1267 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1271 case LFUN_REJECT_ALL_CHANGES: {
1272 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1273 bv_->text->setCursor(&(*bv_->buffer()->paragraphs.begin()), 0);
1274 #warning FIXME changes
1275 //moveCursorUpdate(false);
1277 while (lyxfind::findNextChange(bv_)) {
1278 bv_->getLyXText()->rejectChange();
1281 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1285 case LFUN_ACCEPT_CHANGE: {
1286 bv_->getLyXText()->acceptChange();
1288 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1292 case LFUN_REJECT_CHANGE: {
1293 bv_->getLyXText()->rejectChange();
1295 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1299 case LFUN_UNKNOWN_ACTION:
1300 ev.errorMessage(N_("Unknown function!"));
1304 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1311 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1313 // if we are in a locking inset we should try to insert the
1314 // inset there otherwise this is a illegal function now
1315 if (bv_->theLockingInset()) {
1316 if (bv_->theLockingInset()->insetAllowed(inset))
1317 return bv_->theLockingInset()->insertInset(bv_, inset);
1321 // not quite sure if we want this...
1322 setCursorParUndo(bv_);
1325 beforeChange(bv_->text);
1326 if (!lout.empty()) {
1327 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1328 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1329 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1331 if (!bv_->text->cursor.par()->empty()) {
1332 bv_->text->cursorLeft(bv_);
1334 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1335 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1339 LyXTextClass const & tclass =
1340 buffer_->params.getLyXTextClass();
1341 bool hasLayout = tclass.hasLayout(lres);
1342 string lay = tclass.defaultLayoutName();
1344 if (hasLayout != false) {
1348 // layout not fount using default
1349 lay = tclass.defaultLayoutName();
1352 bv_->text->setLayout(lay);
1354 bv_->text->setParagraph(0, 0,
1356 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1361 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1364 bv_->text->insertInset(inset);
1365 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1372 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1374 if (!inset || !available())
1377 // first check for locking insets
1378 if (bv_->theLockingInset()) {
1379 if (bv_->theLockingInset() == inset) {
1380 if (bv_->text->updateInset(inset)) {
1383 buffer_->markDirty();
1388 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1389 if (bv_->text->updateInset(bv_->theLockingInset())) {
1392 buffer_->markDirty();
1400 // then check if the inset is a top_level inset (has no owner)
1401 // if yes do the update as always otherwise we have to update the
1402 // toplevel inset where this inset is inside
1403 Inset * tl_inset = inset;
1404 while (tl_inset->owner())
1405 tl_inset = tl_inset->owner();
1407 if (tl_inset == inset) {
1408 update(bv_->text, BufferView::UPDATE);
1409 if (bv_->text->updateInset(inset)) {
1413 | BufferView::FITCUR
1414 | BufferView::CHANGE);
1416 update(bv_->text, SELECT);
1420 } else if (static_cast<UpdatableInset *>(tl_inset)
1421 ->updateInsetInInset(bv_, inset))
1423 if (bv_->text->updateInset(tl_inset)) {