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 bv_->owner()->getDialogs().updateParagraph();
235 void BufferView::Pimpl::redoCurrentBuffer()
237 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
238 if (buffer_ && bv_->text) {
239 resizeCurrentBuffer();
241 owner_->updateLayoutChoice();
247 int BufferView::Pimpl::resizeCurrentBuffer()
249 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
252 Paragraph * selstartpar = 0;
253 Paragraph * selendpar = 0;
254 UpdatableInset * the_locking_inset = 0;
257 pos_type selstartpos = 0;
258 pos_type selendpos = 0;
259 bool selection = false;
260 bool mark_set = false;
264 owner_->message(_("Formatting document..."));
267 par = bv_->text->cursor.par();
268 pos = bv_->text->cursor.pos();
269 selstartpar = bv_->text->selection.start.par();
270 selstartpos = bv_->text->selection.start.pos();
271 selendpar = bv_->text->selection.end.par();
272 selendpos = bv_->text->selection.end.pos();
273 selection = bv_->text->selection.set();
274 mark_set = bv_->text->selection.mark();
275 the_locking_inset = bv_->theLockingInset();
276 buffer_->resizeInsets(bv_);
277 // I don't think the delete and new are necessary here we just could
278 // call only init! (Jug 20020419)
280 bv_->text = new LyXText(bv_);
281 bv_->text->init(bv_);
283 // See if we have a text in TextCache that fits
284 // the new buffer_ with the correct width.
285 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
287 if (lyxerr.debugging()) {
288 lyxerr << "Found a LyXText that fits:\n";
289 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
291 // Set the owner of the newly found text
292 // bv_->text->owner(bv_);
293 if (lyxerr.debugging())
294 textcache.show(lyxerr, "resizeCurrentBuffer");
296 buffer_->resizeInsets(bv_);
298 bv_->text = new LyXText(bv_);
299 bv_->text->init(bv_);
300 //buffer_->resizeInsets(bv_);
305 bv_->text->selection.set(true);
306 // At this point just to avoid the Delete-Empty-Paragraph-
307 // Mechanism when setting the cursor.
308 bv_->text->selection.mark(mark_set);
310 bv_->text->setCursor(bv_, selstartpar, selstartpos);
311 bv_->text->selection.cursor = bv_->text->cursor;
312 bv_->text->setCursor(bv_, selendpar, selendpos);
313 bv_->text->setSelection(bv_);
314 bv_->text->setCursor(bv_, par, pos);
316 bv_->text->setCursor(bv_, par, pos);
317 bv_->text->selection.cursor = bv_->text->cursor;
318 bv_->text->selection.set(false);
320 // remake the inset locking
321 bv_->theLockingInset(the_locking_inset);
324 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
335 void BufferView::Pimpl::repaint()
337 // Regenerate the screen.
338 screen().redraw(bv_->text, bv_);
342 void BufferView::Pimpl::updateScrollbar()
345 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
346 workarea().setScrollbarParams(0, 0, 0);
350 LyXText const & t = *bv_->text;
352 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
353 << t.top_y() << ", default height " << defaultRowHeight() << endl;
355 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
359 void BufferView::Pimpl::scrollDocView(int value)
361 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
366 screen().draw(bv_->text, bv_, value);
368 if (!lyxrc.cursor_follows_scrollbar)
371 LyXText * vbt = bv_->text;
373 int const height = defaultRowHeight();
374 int const first = static_cast<int>((bv_->text->top_y() + height));
375 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
377 if (vbt->cursor.y() < first)
378 vbt->setCursorFromCoordinates(bv_, 0, first);
379 else if (vbt->cursor.y() > last)
380 vbt->setCursorFromCoordinates(bv_, 0, last);
384 void BufferView::Pimpl::scroll(int lines)
390 LyXText const * t = bv_->text;
391 int const line_height = defaultRowHeight();
393 // The new absolute coordinate
394 int new_top_y = t->top_y() + lines * line_height;
396 // Restrict to a valid value
397 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
398 new_top_y = std::max(0, new_top_y);
400 scrollDocView(new_top_y);
402 // Update the scrollbar.
403 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
407 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
408 key_modifier::state state)
410 bv_->owner()->getLyXFunc().processKeySym(key, state);
414 void BufferView::Pimpl::selectionRequested()
421 LyXText * text = bv_->getLyXText();
423 if (text->selection.set() &&
424 (!bv_->text->xsel_cache.set() ||
425 text->selection.start != bv_->text->xsel_cache.start ||
426 text->selection.end != bv_->text->xsel_cache.end))
428 bv_->text->xsel_cache = text->selection;
429 sel = text->selectionAsString(bv_->buffer(), false);
430 } else if (!text->selection.set()) {
432 bv_->text->xsel_cache.set(false);
435 workarea().putClipboard(sel);
440 void BufferView::Pimpl::selectionLost()
445 bv_->getLyXText()->clearSelection();
447 bv_->text->xsel_cache.set(false);
452 void BufferView::Pimpl::workAreaResize()
454 static int work_area_width;
455 static int work_area_height;
457 bool const widthChange = workarea().workWidth() != work_area_width;
458 bool const heightChange = workarea().workHeight() != work_area_height;
460 // update from work area
461 work_area_width = workarea().workWidth();
462 work_area_height = workarea().workHeight();
466 // The visible LyXView need a resize
467 resizeCurrentBuffer();
469 // Remove all texts from the textcache
470 // This is not _really_ what we want to do. What
471 // we really want to do is to delete in textcache
472 // that does not have a BufferView with matching
473 // width, but as long as we have only one BufferView
474 // deleting all gives the same result.
475 if (lyxerr.debugging())
476 textcache.show(lyxerr, "Expose delete all");
478 // FIXME: this is already done in resizeCurrentBuffer() ??
479 buffer_->resizeInsets(bv_);
480 } else if (heightChange) {
481 // fitCursor() ensures we don't jump back
482 // to the start of the document on vertical
488 if (widthChange || heightChange) {
492 // always make sure that the scrollbar is sane.
494 owner_->updateLayoutChoice();
499 void BufferView::Pimpl::update()
501 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
502 LyXText::text_status st = bv_->text->status();
503 screen().update(bv_->text, bv_);
505 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
506 bv_->text->fullRebreak(bv_);
507 st = LyXText::NEED_MORE_REFRESH;
508 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
509 bv_->text->cursor.pos());
510 if (bv_->text->selection.set()) {
511 bv_->text->setCursor(bv_, bv_->text->selection.start,
512 bv_->text->selection.start.par(),
513 bv_->text->selection.start.pos());
514 bv_->text->setCursor(bv_, bv_->text->selection.end,
515 bv_->text->selection.end.par(),
516 bv_->text->selection.end.pos());
519 bv_->text->status(bv_, st);
520 screen().update(bv_->text, bv_);
522 // do this here instead of in the screen::update because of
524 bv_->text->status(bv_, LyXText::UNCHANGED);
530 // Values used when calling update:
532 // -2 - update, move sel_cursor if selection, fitcursor
533 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
534 // 0 - update, move sel_cursor if selection, fitcursor
535 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
536 // 3 - update, move sel_cursor if selection
539 // a simple redraw of the parts that need refresh
541 // move sel_cursor if selection -
542 // the text's sel_cursor is moved if there is selection is progress
545 // fitCursor() is called and the scrollbar updated
548 // the buffer is marked dirty.
557 // UPDATE_ONLY = UPDATE;
558 // UPDATE_SELECT = UPDATE | SELECT;
559 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
560 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
562 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
563 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
564 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
565 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
566 // update(3) -> update(1) -> update(1) -> update(SELECT)
568 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
570 owner_->updateLayoutChoice();
572 if (!text->selection.set() && (f & SELECT)) {
573 text->selection.cursor = text->cursor;
576 text->fullRebreak(bv_);
578 if (text->inset_owner) {
579 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
580 updateInset(text->inset_owner, false);
590 buffer_->markDirty();
595 // Callback for cursor timer
596 void BufferView::Pimpl::cursorToggle()
599 cursor_timeout.restart();
603 if (!bv_->theLockingInset()) {
604 screen().cursorToggle(bv_);
606 bv_->theLockingInset()->toggleInsetCursor(bv_);
609 cursor_timeout.restart();
613 bool BufferView::Pimpl::available() const
615 if (buffer_ && bv_->text)
621 Change const BufferView::Pimpl::getCurrentChange()
623 if (!bv_->buffer()->params.tracking_changes)
624 return Change(Change::UNCHANGED);
626 LyXText * t(bv_->getLyXText());
628 if (!t->selection.set())
629 return Change(Change::UNCHANGED);
631 LyXCursor const & cur(t->selection.start);
632 return cur.par()->lookupChangeFull(cur.pos());
636 void BufferView::Pimpl::beforeChange(LyXText * text)
639 text->clearSelection();
643 void BufferView::Pimpl::savePosition(unsigned int i)
645 if (i >= saved_positions_num)
647 saved_positions[i] = Position(buffer_->fileName(),
648 bv_->text->cursor.par()->id(),
649 bv_->text->cursor.pos());
653 str << boost::format(_("Saved bookmark %1$d")) % i;
655 str << _("Saved bookmark ") << i;
657 owner_->message(STRCONV(str.str()));
662 void BufferView::Pimpl::restorePosition(unsigned int i)
664 if (i >= saved_positions_num)
667 string const fname = saved_positions[i].filename;
669 beforeChange(bv_->text);
671 if (fname != buffer_->fileName()) {
672 Buffer * b = bufferlist.exists(fname) ?
673 bufferlist.getBuffer(fname) :
674 bufferlist.loadLyXFile(fname); // don't ask, just load it
675 if (b != 0) buffer(b);
678 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
682 bv_->text->setCursor(bv_, par,
683 min(par->size(), saved_positions[i].par_pos));
685 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
689 str << boost::format(_("Moved to bookmark %1$d")) % i;
691 str << _("Moved to bookmark ") << i;
693 owner_->message(STRCONV(str.str()));
698 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
700 if (i >= saved_positions_num)
703 return !saved_positions[i].filename.empty();
707 void BufferView::Pimpl::switchKeyMap()
709 if (!lyxrc.rtl_support)
712 LyXText * text = bv_->getLyXText();
713 if (text->real_current_font.isRightToLeft()
714 && !(bv_->theLockingInset()
715 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
717 if (owner_->getIntl().keymap == Intl::PRIMARY)
718 owner_->getIntl().KeyMapSec();
720 if (owner_->getIntl().keymap == Intl::SECONDARY)
721 owner_->getIntl().KeyMapPrim();
726 void BufferView::Pimpl::insetUnlock()
728 if (bv_->theLockingInset()) {
729 bv_->theLockingInset()->insetUnlock(bv_);
730 bv_->theLockingInset(0);
736 void BufferView::Pimpl::showCursor()
738 if (bv_->theLockingInset())
739 bv_->theLockingInset()->showInsetCursor(bv_);
741 screen().showCursor(bv_->text, bv_);
745 void BufferView::Pimpl::hideCursor()
747 if (!bv_->theLockingInset())
748 screen().hideCursor();
752 void BufferView::Pimpl::toggleSelection(bool b)
754 if (bv_->theLockingInset())
755 bv_->theLockingInset()->toggleSelection(bv_, b);
756 screen().toggleSelection(bv_->text, bv_, b);
760 void BufferView::Pimpl::toggleToggle()
762 screen().toggleToggle(bv_->text, bv_);
766 void BufferView::Pimpl::center()
768 LyXText * t = bv_->text;
771 int const half_height = workarea().workHeight() / 2;
774 if (t->cursor.y() > half_height) {
775 new_y = t->cursor.y() - half_height;
778 // FIXME: can we do this w/o calling screen directly ?
779 // This updates top_y() but means the fitCursor() call
780 // from the update(FITCUR) doesn't realise that we might
781 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
782 // the scrollbar to be updated as it should, so we have
783 // to do it manually. Any operation that does a center()
784 // and also might have moved top_y() must make sure to call
785 // updateScrollbar() currently. Never mind that this is a
786 // pretty obfuscated way of updating t->top_y()
787 screen().draw(t, bv_, new_y);
789 update(t, BufferView::SELECT | BufferView::FITCUR);
793 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
795 workarea().putClipboard(stuff);
800 * Dispatch functions for actions which can be valid for BufferView->text
801 * and/or InsetText->text!!!
805 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
808 LyXCursor cursor = bv_->getLyXText()->cursor;
809 Buffer::inset_iterator it =
810 find_if(Buffer::inset_iterator(
811 cursor.par(), cursor.pos()),
812 buffer_->inset_iterator_end(),
813 lyx::compare_memfun(&Inset::lyxCode, code));
814 return it != buffer_->inset_iterator_end() ? (*it) : 0;
816 // Ok, this is a little bit too brute force but it
817 // should work for now. Better infrastructure is comming. (Lgb)
819 Buffer * b = bv_->buffer();
820 LyXCursor cursor = bv_->getLyXText()->cursor;
822 Buffer::inset_iterator beg = b->inset_iterator_begin();
823 Buffer::inset_iterator end = b->inset_iterator_end();
825 bool cursor_par_seen = false;
827 for (; beg != end; ++beg) {
828 if (beg.getPar() == cursor.par()) {
829 cursor_par_seen = true;
831 if (cursor_par_seen) {
832 if (beg.getPar() == cursor.par()
833 && beg.getPos() >= cursor.pos()) {
835 } else if (beg.getPar() != cursor.par()) {
842 // Now find the first inset that matches code.
843 for (; beg != end; ++beg) {
844 if (beg->lyxCode() == code) {
854 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
856 string filename = filen;
858 if (filename.empty()) {
859 // Launch a file browser
860 string initpath = lyxrc.document_path;
863 string const trypath = owner_->buffer()->filePath();
864 // If directory is writeable, use this as default.
865 if (IsDirWriteable(trypath))
869 FileDialog fileDlg(_("Select LyX document to insert"),
871 make_pair(string(_("Documents|#o#O")),
872 string(lyxrc.document_path)),
873 make_pair(string(_("Examples|#E#e")),
874 string(AddPath(system_lyxdir, "examples"))));
876 FileDialog::Result result =
877 fileDlg.open(initpath,
878 _("*.lyx| LyX Documents (*.lyx)"));
880 if (result.first == FileDialog::Later)
883 filename = result.second;
885 // check selected filename
886 if (filename.empty()) {
887 owner_->message(_("Canceled."));
892 // get absolute path of file and add ".lyx" to the filename if
894 filename = FileSearch(string(), filename, "lyx");
896 string const disp_fn(MakeDisplayPath(filename));
900 s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
902 s1 << _("Inserting document ") << disp_fn << _("...");
904 owner_->message(STRCONV(s1.str()));
905 bool const res = bv_->insertLyXFile(filename);
909 str << boost::format(_("Document %1$s inserted.")) % disp_fn;
911 str << _("Document ") << disp_fn << _(" inserted.");
913 owner_->message(STRCONV(str.str()));
917 str << boost::format(_("Could not insert document %1$s")) % disp_fn;
919 str << _("Could not insert document ") << disp_fn;
921 owner_->message(STRCONV(str.str()));
926 void BufferView::Pimpl::trackChanges()
928 Buffer * buf(bv_->buffer());
929 bool const tracking(buf->params.tracking_changes);
932 ParIterator const end = buf->par_iterator_end();
933 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
934 (*it)->trackChanges();
936 buf->params.tracking_changes = true;
938 // we cannot allow undos beyond the freeze point
939 buf->undostack.clear();
941 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
942 bv_->text->setCursor(bv_, &(*buf->paragraphs.begin()), 0);
943 #warning changes FIXME
944 //moveCursorUpdate(false);
946 bool found = lyxfind::findNextChange(bv_);
948 owner_->getDialogs().show("changes");
952 ParIterator const end = buf->par_iterator_end();
953 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
954 (*it)->untrackChanges();
956 buf->params.tracking_changes = false;
959 buf->redostack.clear();
963 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
965 // Make sure that the cached BufferView is correct.
966 FuncRequest ev = ev_in;
969 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
970 << " action[" << ev.action << ']'
971 << " arg[" << ev.argument << ']'
972 << " x[" << ev.x << ']'
973 << " y[" << ev.y << ']'
974 << " button[" << ev.button() << ']'
977 // e.g. Qt mouse press when no buffer
981 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
985 case LFUN_SCROLL_INSET:
986 // this is not handled here as this function is only active
987 // if we have a locking_inset and that one is (or contains)
991 case LFUN_LAYOUT_COPY:
992 bv_->copyEnvironment();
995 case LFUN_LAYOUT_PASTE:
996 bv_->pasteEnvironment();
1000 case LFUN_FILE_INSERT:
1001 MenuInsertLyXFile(ev.argument);
1004 case LFUN_FILE_INSERT_ASCII_PARA:
1005 InsertAsciiFile(bv_, ev.argument, true);
1008 case LFUN_FILE_INSERT_ASCII:
1009 InsertAsciiFile(bv_, ev.argument, false);
1013 lang(bv_, ev.argument);
1015 owner_->view_state_changed();
1020 owner_->view_state_changed();
1025 owner_->view_state_changed();
1030 owner_->view_state_changed();
1035 owner_->view_state_changed();
1040 owner_->view_state_changed();
1045 owner_->view_state_changed();
1050 owner_->view_state_changed();
1053 case LFUN_UNDERLINE:
1055 owner_->view_state_changed();
1058 case LFUN_FONT_SIZE:
1059 fontSize(bv_, ev.argument);
1060 owner_->view_state_changed();
1063 case LFUN_FONT_STATE:
1064 owner_->getLyXFunc().setMessage(currentState(bv_));
1067 case LFUN_INSERT_LABEL: {
1068 // Try and generate a valid label
1069 string const contents = ev.argument.empty() ?
1070 getPossibleLabel(*bv_) : ev.argument;
1071 InsetCommandParams icp("label", contents);
1072 string data = InsetCommandMailer::params2string("label", icp);
1073 owner_->getDialogs().show("label", data, 0);
1077 case LFUN_BOOKMARK_SAVE:
1078 savePosition(strToUnsignedInt(ev.argument));
1081 case LFUN_BOOKMARK_GOTO:
1082 restorePosition(strToUnsignedInt(ev.argument));
1087 string label = ev.argument;
1088 if (label.empty()) {
1090 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1092 label = inset->getContents();
1097 if (!label.empty()) {
1098 //bv_->savePosition(0);
1099 if (!bv_->gotoLabel(label))
1100 Alert::alert(_("Error"),
1101 _("Couldn't find this label"),
1102 _("in current document."));
1107 // --- accented characters ---------------------------
1110 case LFUN_CIRCUMFLEX:
1120 case LFUN_SPECIAL_CARON:
1123 case LFUN_HUNG_UMLAUT:
1126 if (ev.argument.empty()) {
1128 owner_->getLyXFunc().handleKeyFunc(ev.action);
1130 owner_->getLyXFunc().handleKeyFunc(ev.action);
1131 owner_->getIntl().getTransManager()
1132 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1133 update(bv_->getLyXText(),
1135 | BufferView::FITCUR
1136 | BufferView::CHANGE);
1140 case LFUN_MATH_MACRO:
1141 case LFUN_MATH_DELIM:
1142 case LFUN_INSERT_MATRIX:
1143 case LFUN_INSERT_MATH:
1144 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1145 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1146 case LFUN_MATH_MODE: // Open or create an inlined math inset
1147 case LFUN_GREEK: // Insert a single greek letter
1151 case LFUN_INSET_APPLY: {
1152 string const name = ev.getArg(0);
1154 InsetBase * base = owner_->getDialogs().getOpenInset(name);
1156 // This works both for 'original' and 'mathed' insets.
1157 // Note that the localDispatch performs updateInset
1159 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1160 base->localDispatch(fr);
1162 Inset * inset = createInset(ev);
1163 if (inset && insertInset(inset)) {
1164 updateInset(inset, true);
1170 if (name == "bibitem") {
1171 // We need to do a redraw because the maximum
1172 // InsetBibitem width could have changed
1173 #warning please check you mean repaint() not update(),
1174 #warning and whether the repaint() is needed at all
1181 case LFUN_FLOAT_LIST:
1182 if (tclass.floats().typeExist(ev.argument)) {
1183 Inset * inset = new InsetFloatList(ev.argument);
1184 if (!insertInset(inset, tclass.defaultLayoutName()))
1187 lyxerr << "Non-existent float type: "
1188 << ev.argument << endl;
1192 case LFUN_THESAURUS_ENTRY:
1194 string arg = ev.argument;
1197 arg = bv_->getLyXText()->selectionAsString(buffer_,
1201 if (arg.size() > 100 || arg.empty()) {
1202 // Get word or selection
1203 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1204 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1205 // FIXME: where is getLyXText()->unselect(bv_) ?
1209 bv_->owner()->getDialogs().showThesaurus(arg);
1213 case LFUN_TRACK_CHANGES:
1217 case LFUN_MERGE_CHANGES:
1218 owner_->getDialogs().show("changes");
1221 case LFUN_ACCEPT_ALL_CHANGES: {
1222 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1223 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1224 #warning FIXME changes
1225 //moveCursorUpdate(false);
1227 while (lyxfind::findNextChange(bv_)) {
1228 bv_->getLyXText()->acceptChange(bv_);
1231 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1235 case LFUN_REJECT_ALL_CHANGES: {
1236 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1237 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1238 #warning FIXME changes
1239 //moveCursorUpdate(false);
1241 while (lyxfind::findNextChange(bv_)) {
1242 bv_->getLyXText()->rejectChange(bv_);
1245 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1249 case LFUN_ACCEPT_CHANGE: {
1250 bv_->getLyXText()->acceptChange(bv_);
1252 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1256 case LFUN_REJECT_CHANGE: {
1257 bv_->getLyXText()->rejectChange(bv_);
1259 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1263 case LFUN_UNKNOWN_ACTION:
1264 ev.errorMessage(N_("Unknown function!"));
1268 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1275 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1277 // if we are in a locking inset we should try to insert the
1278 // inset there otherwise this is a illegal function now
1279 if (bv_->theLockingInset()) {
1280 if (bv_->theLockingInset()->insetAllowed(inset))
1281 return bv_->theLockingInset()->insertInset(bv_, inset);
1285 // not quite sure if we want this...
1286 setCursorParUndo(bv_);
1289 beforeChange(bv_->text);
1290 if (!lout.empty()) {
1291 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1292 bv_->text->breakParagraph(bv_, bv_->buffer()->paragraphs);
1293 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1295 if (!bv_->text->cursor.par()->empty()) {
1296 bv_->text->cursorLeft(bv_);
1298 bv_->text->breakParagraph(bv_, bv_->buffer()->paragraphs);
1299 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1303 LyXTextClass const & tclass =
1304 buffer_->params.getLyXTextClass();
1305 bool hasLayout = tclass.hasLayout(lres);
1306 string lay = tclass.defaultLayoutName();
1308 if (hasLayout != false) {
1312 // layout not fount using default
1313 lay = tclass.defaultLayoutName();
1316 bv_->text->setLayout(bv_, lay);
1318 bv_->text->setParagraph(bv_, 0, 0,
1320 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1325 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1328 bv_->text->insertInset(bv_, inset);
1329 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1336 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1338 if (!inset || !available())
1341 // first check for locking insets
1342 if (bv_->theLockingInset()) {
1343 if (bv_->theLockingInset() == inset) {
1344 if (bv_->text->updateInset(bv_, inset)) {
1347 buffer_->markDirty();
1352 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1353 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1356 buffer_->markDirty();
1364 // then check if the inset is a top_level inset (has no owner)
1365 // if yes do the update as always otherwise we have to update the
1366 // toplevel inset where this inset is inside
1367 Inset * tl_inset = inset;
1368 while (tl_inset->owner())
1369 tl_inset = tl_inset->owner();
1371 if (tl_inset == inset) {
1372 update(bv_->text, BufferView::UPDATE);
1373 if (bv_->text->updateInset(bv_, inset)) {
1377 | BufferView::FITCUR
1378 | BufferView::CHANGE);
1380 update(bv_->text, SELECT);
1384 } else if (static_cast<UpdatableInset *>(tl_inset)
1385 ->updateInsetInInset(bv_, inset))
1387 if (bv_->text->updateInset(bv_, tl_inset)) {