2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
11 #include "BufferView_pimpl.h"
12 #include "frontends/WorkArea.h"
13 #include "frontends/screen.h"
14 #include "frontends/LyXScreenFactory.h"
15 #include "frontends/WorkAreaFactory.h"
16 #include "frontends/Dialogs.h"
17 #include "frontends/Alert.h"
18 #include "frontends/FileDialog.h"
19 #include "frontends/mouse_state.h"
22 #include "paragraph.h"
23 #include "frontends/LyXView.h"
24 #include "commandtags.h"
27 #include "bufferview_funcs.h"
28 #include "TextCache.h"
29 #include "bufferlist.h"
32 // added for Dispatch functions
35 #include "FloatList.h"
37 #include "ParagraphParameters.h"
38 #include "undo_funcs.h"
39 #include "funcrequest.h"
40 #include "iterators.h"
43 #include "insets/insetbibtex.h"
44 #include "insets/insetcite.h"
45 #include "insets/insetfloatlist.h"
46 #include "insets/insetgraphics.h"
47 #include "insets/insetinclude.h"
48 #include "insets/insetindex.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insetmarginal.h"
51 #include "insets/insetref.h"
52 #include "insets/insettext.h"
53 #include "insets/insettoc.h"
55 #include "mathed/formulabase.h"
57 #include "graphics/Previews.h"
59 #include "support/LAssert.h"
60 #include "support/lstrings.h"
61 #include "support/filetools.h"
63 #include <boost/bind.hpp>
64 #include <boost/signals/connection.hpp>
65 #include "BoostFormat.h"
81 extern BufferList bufferlist;
86 unsigned int const saved_positions_num = 20;
88 // All the below connection objects are needed because of a bug in some
89 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
90 // to these connections we avoid a segfault upon startup, and also at exit.
93 boost::signals::connection dispatchcon;
94 boost::signals::connection timecon;
95 boost::signals::connection doccon;
96 boost::signals::connection resizecon;
97 boost::signals::connection kpresscon;
98 boost::signals::connection selectioncon;
99 boost::signals::connection lostcon;
105 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
106 int xpos, int ypos, int width, int height)
107 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
108 using_xterm_cursor(false)
110 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
111 screen_.reset(LyXScreenFactory::create(workarea()));
114 doccon = workarea().scrollDocView
115 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
116 resizecon = workarea().workAreaResize
117 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
118 dispatchcon = workarea().dispatch
119 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
120 kpresscon = workarea().workAreaKeyPress
121 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
122 selectioncon = workarea().selectionRequested
123 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
124 lostcon = workarea().selectionLost
125 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
127 timecon = cursor_timeout.timeout
128 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
129 cursor_timeout.start();
130 saved_positions.resize(saved_positions_num);
134 WorkArea & BufferView::Pimpl::workarea() const
136 return *workarea_.get();
140 LyXScreen & BufferView::Pimpl::screen() const
142 return *screen_.get();
146 Painter & BufferView::Pimpl::painter() const
148 return workarea().getPainter();
152 void BufferView::Pimpl::buffer(Buffer * b)
154 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
157 buffer_->delUser(bv_);
159 // Put the old text into the TextCache, but
160 // only if the buffer is still loaded.
161 // Also set the owner of the test to 0
162 // bv_->text->owner(0);
163 textcache.add(buffer_, workarea().workWidth(), bv_->text);
164 if (lyxerr.debugging())
165 textcache.show(lyxerr, "BufferView::buffer");
170 // set current buffer
173 // if we're quitting lyx, don't bother updating stuff
177 // if we are closing the buffer, use the first buffer as current
179 buffer_ = bufferlist.first();
183 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
184 buffer_->addUser(bv_);
186 // If we don't have a text object for this, we make one
187 if (bv_->text == 0) {
188 resizeCurrentBuffer();
191 // FIXME: needed when ?
193 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
195 // Similarly, buffer-dependent dialogs should be updated or
196 // hidden. This should go here because some dialogs (eg ToC)
197 // require bv_->text.
198 owner_->getDialogs().updateBufferDependent(true);
200 lyxerr[Debug::INFO] << " No Buffer!" << endl;
201 owner_->getDialogs().hideBufferDependent();
203 // Also remove all remaining text's from the testcache.
204 // (there should not be any!) (if there is any it is a
206 if (lyxerr.debugging())
207 textcache.show(lyxerr, "buffer delete all");
213 owner_->updateMenubar();
214 owner_->updateToolbar();
215 owner_->updateLayoutChoice();
216 owner_->updateWindowTitle();
218 if (grfx::Previews::activated() && buffer_)
219 grfx::Previews::get().generateBufferPreviews(*buffer_);
223 bool BufferView::Pimpl::fitCursor()
227 if (bv_->theLockingInset()) {
228 bv_->theLockingInset()->fitInsetCursor(bv_);
231 ret = screen().fitCursor(bv_->text, bv_);
234 bv_->owner()->getDialogs().updateParagraph();
241 void BufferView::Pimpl::redoCurrentBuffer()
243 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
244 if (buffer_ && bv_->text) {
245 resizeCurrentBuffer();
247 owner_->updateLayoutChoice();
253 int BufferView::Pimpl::resizeCurrentBuffer()
255 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
258 Paragraph * selstartpar = 0;
259 Paragraph * selendpar = 0;
260 UpdatableInset * the_locking_inset = 0;
263 pos_type selstartpos = 0;
264 pos_type selendpos = 0;
265 bool selection = false;
266 bool mark_set = false;
270 owner_->message(_("Formatting document..."));
273 par = bv_->text->cursor.par();
274 pos = bv_->text->cursor.pos();
275 selstartpar = bv_->text->selection.start.par();
276 selstartpos = bv_->text->selection.start.pos();
277 selendpar = bv_->text->selection.end.par();
278 selendpos = bv_->text->selection.end.pos();
279 selection = bv_->text->selection.set();
280 mark_set = bv_->text->selection.mark();
281 the_locking_inset = bv_->theLockingInset();
282 buffer_->resizeInsets(bv_);
283 // I don't think the delete and new are necessary here we just could
284 // call only init! (Jug 20020419)
286 bv_->text = new LyXText(bv_);
287 bv_->text->init(bv_);
289 // See if we have a text in TextCache that fits
290 // the new buffer_ with the correct width.
291 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
293 if (lyxerr.debugging()) {
294 lyxerr << "Found a LyXText that fits:\n";
295 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
297 // Set the owner of the newly found text
298 // bv_->text->owner(bv_);
299 if (lyxerr.debugging())
300 textcache.show(lyxerr, "resizeCurrentBuffer");
302 buffer_->resizeInsets(bv_);
304 bv_->text = new LyXText(bv_);
305 bv_->text->init(bv_);
306 //buffer_->resizeInsets(bv_);
311 bv_->text->selection.set(true);
312 // At this point just to avoid the Delete-Empty-Paragraph-
313 // Mechanism when setting the cursor.
314 bv_->text->selection.mark(mark_set);
316 bv_->text->setCursor(bv_, selstartpar, selstartpos);
317 bv_->text->selection.cursor = bv_->text->cursor;
318 bv_->text->setCursor(bv_, selendpar, selendpos);
319 bv_->text->setSelection(bv_);
320 bv_->text->setCursor(bv_, par, pos);
322 bv_->text->setCursor(bv_, par, pos);
323 bv_->text->selection.cursor = bv_->text->cursor;
324 bv_->text->selection.set(false);
326 // remake the inset locking
327 bv_->theLockingInset(the_locking_inset);
330 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
341 void BufferView::Pimpl::repaint()
343 // Regenerate the screen.
344 screen().redraw(bv_->text, bv_);
348 void BufferView::Pimpl::updateScrollbar()
351 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
352 workarea().setScrollbarParams(0, 0, 0);
356 LyXText const & t = *bv_->text;
358 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
359 << t.first_y << ", default height " << defaultRowHeight() << endl;
361 workarea().setScrollbarParams(t.height, t.first_y, defaultRowHeight());
365 void BufferView::Pimpl::scrollDocView(int value)
367 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
372 screen().draw(bv_->text, bv_, value);
374 if (!lyxrc.cursor_follows_scrollbar)
377 LyXText * vbt = bv_->text;
379 int const height = defaultRowHeight();
380 int const first = static_cast<int>((bv_->text->first_y + height));
381 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
383 if (vbt->cursor.y() < first)
384 vbt->setCursorFromCoordinates(bv_, 0, first);
385 else if (vbt->cursor.y() > last)
386 vbt->setCursorFromCoordinates(bv_, 0, last);
390 void BufferView::Pimpl::scroll(int lines)
396 LyXText const * t = bv_->text;
397 int const line_height = defaultRowHeight();
399 // The new absolute coordinate
400 int new_first_y = t->first_y + lines * line_height;
402 // Restrict to a valid value
403 new_first_y = std::min(t->height - 4 * line_height, new_first_y);
404 new_first_y = std::max(0, new_first_y);
406 scrollDocView(new_first_y);
408 // Update the scrollbar.
409 workarea().setScrollbarParams(t->height, t->first_y, defaultRowHeight());
413 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
414 key_modifier::state state)
416 bv_->owner()->getLyXFunc().processKeySym(key, state);
420 void BufferView::Pimpl::selectionRequested()
427 LyXText * text = bv_->getLyXText();
429 if (text->selection.set() &&
430 (!bv_->text->xsel_cache.set() ||
431 text->selection.start != bv_->text->xsel_cache.start ||
432 text->selection.end != bv_->text->xsel_cache.end))
434 bv_->text->xsel_cache = text->selection;
435 sel = text->selectionAsString(bv_->buffer(), false);
436 } else if (!text->selection.set()) {
438 bv_->text->xsel_cache.set(false);
441 workarea().putClipboard(sel);
446 void BufferView::Pimpl::selectionLost()
451 bv_->getLyXText()->clearSelection();
453 bv_->text->xsel_cache.set(false);
458 void BufferView::Pimpl::workAreaResize()
460 static int work_area_width;
461 static int work_area_height;
463 bool const widthChange = workarea().workWidth() != work_area_width;
464 bool const heightChange = workarea().workHeight() != work_area_height;
466 // update from work area
467 work_area_width = workarea().workWidth();
468 work_area_height = workarea().workHeight();
472 // The visible LyXView need a resize
473 resizeCurrentBuffer();
475 // Remove all texts from the textcache
476 // This is not _really_ what we want to do. What
477 // we really want to do is to delete in textcache
478 // that does not have a BufferView with matching
479 // width, but as long as we have only one BufferView
480 // deleting all gives the same result.
481 if (lyxerr.debugging())
482 textcache.show(lyxerr, "Expose delete all");
484 // FIXME: this is already done in resizeCurrentBuffer() ??
485 buffer_->resizeInsets(bv_);
486 } else if (heightChange) {
487 // fitCursor() ensures we don't jump back
488 // to the start of the document on vertical
494 if (widthChange || heightChange) {
498 // always make sure that the scrollbar is sane.
500 owner_->updateLayoutChoice();
505 void BufferView::Pimpl::update()
507 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
508 LyXText::text_status st = bv_->text->status();
509 screen().update(bv_->text, bv_);
511 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
512 bv_->text->fullRebreak(bv_);
513 st = LyXText::NEED_MORE_REFRESH;
514 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
515 bv_->text->cursor.pos());
516 if (bv_->text->selection.set()) {
517 bv_->text->setCursor(bv_, bv_->text->selection.start,
518 bv_->text->selection.start.par(),
519 bv_->text->selection.start.pos());
520 bv_->text->setCursor(bv_, bv_->text->selection.end,
521 bv_->text->selection.end.par(),
522 bv_->text->selection.end.pos());
525 bv_->text->status(bv_, st);
526 screen().update(bv_->text, bv_);
528 // do this here instead of in the screen::update because of
530 bv_->text->status(bv_, LyXText::UNCHANGED);
536 // Values used when calling update:
538 // -2 - update, move sel_cursor if selection, fitcursor
539 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
540 // 0 - update, move sel_cursor if selection, fitcursor
541 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
542 // 3 - update, move sel_cursor if selection
545 // a simple redraw of the parts that need refresh
547 // move sel_cursor if selection -
548 // the text's sel_cursor is moved if there is selection is progress
551 // fitCursor() is called and the scrollbar updated
554 // the buffer is marked dirty.
563 // UPDATE_ONLY = UPDATE;
564 // UPDATE_SELECT = UPDATE | SELECT;
565 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
566 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
568 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
569 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
570 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
571 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
572 // update(3) -> update(1) -> update(1) -> update(SELECT)
574 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
576 owner_->updateLayoutChoice();
578 if (!text->selection.set() && (f & SELECT)) {
579 text->selection.cursor = text->cursor;
582 text->fullRebreak(bv_);
584 if (text->inset_owner) {
585 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
586 updateInset(text->inset_owner, false);
596 buffer_->markDirty();
601 // Callback for cursor timer
602 void BufferView::Pimpl::cursorToggle()
605 cursor_timeout.restart();
609 if (!bv_->theLockingInset()) {
610 screen().cursorToggle(bv_);
612 bv_->theLockingInset()->toggleInsetCursor(bv_);
615 cursor_timeout.restart();
619 bool BufferView::Pimpl::available() const
621 if (buffer_ && bv_->text)
627 Change const BufferView::Pimpl::getCurrentChange()
629 if (!bv_->buffer()->params.tracking_changes)
630 return Change(Change::UNCHANGED);
632 LyXText * t(bv_->getLyXText());
634 if (!t->selection.set())
635 return Change(Change::UNCHANGED);
637 LyXCursor const & cur(t->selection.start);
638 return cur.par()->lookupChangeFull(cur.pos());
642 void BufferView::Pimpl::beforeChange(LyXText * text)
645 text->clearSelection();
649 void BufferView::Pimpl::savePosition(unsigned int i)
651 if (i >= saved_positions_num)
653 saved_positions[i] = Position(buffer_->fileName(),
654 bv_->text->cursor.par()->id(),
655 bv_->text->cursor.pos());
659 str << boost::format(_("Saved bookmark %1$d")) % i;
661 str << _("Saved bookmark ") << i;
663 owner_->message(STRCONV(str.str()));
668 void BufferView::Pimpl::restorePosition(unsigned int i)
670 if (i >= saved_positions_num)
673 string const fname = saved_positions[i].filename;
675 beforeChange(bv_->text);
677 if (fname != buffer_->fileName()) {
678 Buffer * b = bufferlist.exists(fname) ?
679 bufferlist.getBuffer(fname) :
680 bufferlist.loadLyXFile(fname); // don't ask, just load it
681 if (b != 0) buffer(b);
684 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
688 bv_->text->setCursor(bv_, par,
689 min(par->size(), saved_positions[i].par_pos));
691 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
695 str << boost::format(_("Moved to bookmark %1$d")) % i;
697 str << _("Moved to bookmark ") << i;
699 owner_->message(STRCONV(str.str()));
704 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
706 if (i >= saved_positions_num)
709 return !saved_positions[i].filename.empty();
713 void BufferView::Pimpl::switchKeyMap()
715 if (!lyxrc.rtl_support)
718 LyXText * text = bv_->getLyXText();
719 if (text->real_current_font.isRightToLeft()
720 && !(bv_->theLockingInset()
721 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
723 if (owner_->getIntl().keymap == Intl::PRIMARY)
724 owner_->getIntl().KeyMapSec();
726 if (owner_->getIntl().keymap == Intl::SECONDARY)
727 owner_->getIntl().KeyMapPrim();
732 void BufferView::Pimpl::insetUnlock()
734 if (bv_->theLockingInset()) {
735 bv_->theLockingInset()->insetUnlock(bv_);
736 bv_->theLockingInset(0);
742 void BufferView::Pimpl::showCursor()
744 if (bv_->theLockingInset())
745 bv_->theLockingInset()->showInsetCursor(bv_);
747 screen().showCursor(bv_->text, bv_);
751 void BufferView::Pimpl::hideCursor()
753 if (!bv_->theLockingInset())
754 screen().hideCursor();
758 void BufferView::Pimpl::toggleSelection(bool b)
760 if (bv_->theLockingInset())
761 bv_->theLockingInset()->toggleSelection(bv_, b);
762 screen().toggleSelection(bv_->text, bv_, b);
766 void BufferView::Pimpl::toggleToggle()
768 screen().toggleToggle(bv_->text, bv_);
772 void BufferView::Pimpl::center()
774 LyXText * t = bv_->text;
777 int const half_height = workarea().workHeight() / 2;
780 if (t->cursor.y() > half_height) {
781 new_y = t->cursor.y() - half_height;
784 // FIXME: can we do this w/o calling screen directly ?
785 // This updates first_y but means the fitCursor() call
786 // from the update(FITCUR) doesn't realise that we might
787 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
788 // the scrollbar to be updated as it should, so we have
789 // to do it manually. Any operation that does a center()
790 // and also might have moved first_y must make sure to call
791 // updateScrollbar() currently. Never mind that this is a
792 // pretty obfuscated way of updating t->first_y
793 screen().draw(t, bv_, new_y);
795 update(t, BufferView::SELECT | BufferView::FITCUR);
799 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
801 workarea().putClipboard(stuff);
806 * Dispatch functions for actions which can be valid for BufferView->text
807 * and/or InsetText->text!!!
811 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
814 LyXCursor cursor = bv_->getLyXText()->cursor;
815 Buffer::inset_iterator it =
816 find_if(Buffer::inset_iterator(
817 cursor.par(), cursor.pos()),
818 buffer_->inset_iterator_end(),
819 lyx::compare_memfun(&Inset::lyxCode, code));
820 return it != buffer_->inset_iterator_end() ? (*it) : 0;
822 // Ok, this is a little bit too brute force but it
823 // should work for now. Better infrastructure is comming. (Lgb)
825 Buffer * b = bv_->buffer();
826 LyXCursor cursor = bv_->getLyXText()->cursor;
828 Buffer::inset_iterator beg = b->inset_iterator_begin();
829 Buffer::inset_iterator end = b->inset_iterator_end();
831 bool cursor_par_seen = false;
833 for (; beg != end; ++beg) {
834 if (beg.getPar() == cursor.par()) {
835 cursor_par_seen = true;
837 if (cursor_par_seen) {
838 if (beg.getPar() == cursor.par()
839 && beg.getPos() >= cursor.pos()) {
841 } else if (beg.getPar() != cursor.par()) {
848 // Now find the first inset that matches code.
849 for (; beg != end; ++beg) {
850 if (beg->lyxCode() == code) {
860 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
862 string filename = filen;
864 if (filename.empty()) {
865 // Launch a file browser
866 string initpath = lyxrc.document_path;
869 string const trypath = owner_->buffer()->filePath();
870 // If directory is writeable, use this as default.
871 if (IsDirWriteable(trypath))
875 FileDialog fileDlg(_("Select LyX document to insert"),
877 make_pair(string(_("Documents|#o#O")),
878 string(lyxrc.document_path)),
879 make_pair(string(_("Examples|#E#e")),
880 string(AddPath(system_lyxdir, "examples"))));
882 FileDialog::Result result =
883 fileDlg.open(initpath,
884 _("*.lyx| LyX Documents (*.lyx)"));
886 if (result.first == FileDialog::Later)
889 filename = result.second;
891 // check selected filename
892 if (filename.empty()) {
893 owner_->message(_("Canceled."));
898 // get absolute path of file and add ".lyx" to the filename if
900 filename = FileSearch(string(), filename, "lyx");
902 string const disp_fn(MakeDisplayPath(filename));
906 s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
908 s1 << _("Inserting document ") << disp_fn << _("...");
910 owner_->message(STRCONV(s1.str()));
911 bool const res = bv_->insertLyXFile(filename);
915 str << boost::format(_("Document %1$s inserted.")) % disp_fn;
917 str << _("Document ") << disp_fn << _(" inserted.");
919 owner_->message(STRCONV(str.str()));
923 str << boost::format(_("Could not insert document %1$s")) % disp_fn;
925 str << _("Could not insert document ") << disp_fn;
927 owner_->message(STRCONV(str.str()));
932 void BufferView::Pimpl::trackChanges()
934 Buffer * buf(bv_->buffer());
935 bool const tracking(buf->params.tracking_changes);
938 ParIterator const end = buf->par_iterator_end();
939 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
940 (*it)->trackChanges();
942 buf->params.tracking_changes = true;
944 // we cannot allow undos beyond the freeze point
945 buf->undostack.clear();
947 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
948 bv_->text->setCursor(bv_, &(*buf->paragraphs.begin()), 0);
949 #warning changes FIXME
950 //moveCursorUpdate(false);
952 bool found = lyxfind::findNextChange(bv_);
954 owner_->getDialogs().showMergeChanges();
958 ParIterator const end = buf->par_iterator_end();
959 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
960 (*it)->untrackChanges();
962 buf->params.tracking_changes = false;
965 buf->redostack.clear();
969 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
971 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
972 << " action[" << ev.action << ']'
973 << " arg[" << ev.argument << ']'
974 << " x[" << ev.x << ']'
975 << " y[" << ev.y << ']'
976 << " button[" << ev.button() << ']'
979 // e.g. Qt mouse press when no buffer
983 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
987 case LFUN_SCROLL_INSET:
988 // this is not handled here as this function is only active
989 // if we have a locking_inset and that one is (or contains)
993 case LFUN_INSET_GRAPHICS:
995 Inset * new_inset = new InsetGraphics;
996 if (!insertInset(new_inset)) {
999 // this is need because you don't use a inset->Edit()
1000 updateInset(new_inset, true);
1001 new_inset->edit(bv_);
1006 case LFUN_LAYOUT_COPY:
1007 bv_->copyEnvironment();
1010 case LFUN_LAYOUT_PASTE:
1011 bv_->pasteEnvironment();
1015 case LFUN_DEPTH_MIN:
1016 changeDepth(bv_, bv_->getLyXText(), -1);
1019 case LFUN_DEPTH_PLUS:
1020 changeDepth(bv_, bv_->getLyXText(), 1);
1024 owner_->getDialogs().setUserFreeFont();
1027 case LFUN_FILE_INSERT:
1028 MenuInsertLyXFile(ev.argument);
1031 case LFUN_FILE_INSERT_ASCII_PARA:
1032 InsertAsciiFile(bv_, ev.argument, true);
1035 case LFUN_FILE_INSERT_ASCII:
1036 InsertAsciiFile(bv_, ev.argument, false);
1040 lang(bv_, ev.argument);
1042 owner_->view_state_changed();
1047 owner_->view_state_changed();
1052 owner_->view_state_changed();
1057 owner_->view_state_changed();
1062 owner_->view_state_changed();
1067 owner_->view_state_changed();
1072 owner_->view_state_changed();
1077 owner_->view_state_changed();
1080 case LFUN_UNDERLINE:
1082 owner_->view_state_changed();
1085 case LFUN_FONT_SIZE:
1086 fontSize(bv_, ev.argument);
1087 owner_->view_state_changed();
1090 case LFUN_FONT_STATE:
1091 owner_->getLyXFunc().setMessage(currentState(bv_));
1094 case LFUN_INSERT_LABEL:
1095 MenuInsertLabel(bv_, ev.argument);
1098 case LFUN_REF_INSERT:
1099 if (ev.argument.empty()) {
1100 InsetCommandParams p("ref");
1101 owner_->getDialogs().createRef(p.getAsString());
1103 InsetCommandParams p;
1104 p.setFromString(ev.argument);
1106 InsetRef * inset = new InsetRef(p, *buffer_);
1107 if (!insertInset(inset))
1110 updateInset(inset, true);
1114 case LFUN_BOOKMARK_SAVE:
1115 savePosition(strToUnsignedInt(ev.argument));
1118 case LFUN_BOOKMARK_GOTO:
1119 restorePosition(strToUnsignedInt(ev.argument));
1124 string label = ev.argument;
1125 if (label.empty()) {
1127 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1129 label = inset->getContents();
1134 if (!label.empty()) {
1135 //bv_->savePosition(0);
1136 if (!bv_->gotoLabel(label))
1137 Alert::alert(_("Error"),
1138 _("Couldn't find this label"),
1139 _("in current document."));
1144 // --- accented characters ---------------------------
1147 case LFUN_CIRCUMFLEX:
1157 case LFUN_SPECIAL_CARON:
1160 case LFUN_HUNG_UMLAUT:
1163 if (ev.argument.empty()) {
1165 owner_->getLyXFunc().handleKeyFunc(ev.action);
1167 owner_->getLyXFunc().handleKeyFunc(ev.action);
1168 owner_->getIntl().getTransManager()
1169 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1170 update(bv_->getLyXText(),
1172 | BufferView::FITCUR
1173 | BufferView::CHANGE);
1177 case LFUN_MATH_MACRO:
1178 case LFUN_MATH_DELIM:
1179 case LFUN_INSERT_MATRIX:
1180 case LFUN_INSERT_MATH:
1181 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1182 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1183 case LFUN_MATH_MODE: // Open or create an inlined math inset
1184 case LFUN_GREEK: // Insert a single greek letter
1185 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1188 case LFUN_CITATION_INSERT:
1190 InsetCommandParams p;
1191 p.setFromString(ev.argument);
1193 InsetCitation * inset = new InsetCitation(p);
1194 if (!insertInset(inset))
1197 inset->setLoadingBuffer(bv_->buffer(), false);
1198 updateInset(inset, true);
1204 case LFUN_INSERT_BIBTEX:
1206 // ale970405+lasgoutt970425
1207 // The argument can be up to two tokens separated
1208 // by a space. The first one is the bibstyle.
1209 string const db = token(ev.argument, ' ', 0);
1210 string bibstyle = token(ev.argument, ' ', 1);
1211 if (bibstyle.empty())
1214 InsetCommandParams p("BibTeX", db, bibstyle);
1215 InsetBibtex * inset = new InsetBibtex(p);
1217 if (insertInset(inset)) {
1218 if (ev.argument.empty())
1225 // BibTeX data bases
1226 case LFUN_BIBDB_ADD:
1228 InsetBibtex * inset =
1229 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1231 inset->addDatabase(ev.argument);
1236 case LFUN_BIBDB_DEL:
1238 InsetBibtex * inset =
1239 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1241 inset->delDatabase(ev.argument);
1245 case LFUN_BIBTEX_STYLE:
1247 InsetBibtex * inset =
1248 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1250 inset->setOptions(ev.argument);
1254 case LFUN_CHILD_INSERT:
1256 InsetInclude::Params p;
1257 if (!ev.argument.empty())
1258 p.cparams.setFromString(ev.argument);
1259 p.masterFilename_ = buffer_->fileName();
1261 InsetInclude * inset = new InsetInclude(p);
1262 if (!insertInset(inset))
1265 updateInset(inset, true);
1266 bv_->owner()->getDialogs().showInclude(inset);
1271 case LFUN_FLOAT_LIST:
1272 if (tclass.floats().typeExist(ev.argument)) {
1273 Inset * inset = new InsetFloatList(ev.argument);
1274 if (!insertInset(inset, tclass.defaultLayoutName()))
1277 lyxerr << "Non-existent float type: "
1278 << ev.argument << endl;
1282 case LFUN_THESAURUS_ENTRY:
1284 string arg = ev.argument;
1287 arg = bv_->getLyXText()->selectionAsString(buffer_,
1291 if (arg.size() > 100 || arg.empty()) {
1292 // Get word or selection
1293 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1294 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1295 // FIXME: where is getLyXText()->unselect(bv_) ?
1299 bv_->owner()->getDialogs().showThesaurus(arg);
1303 case LFUN_TRACK_CHANGES:
1307 case LFUN_MERGE_CHANGES:
1308 owner_->getDialogs().showMergeChanges();
1311 case LFUN_ACCEPT_ALL_CHANGES: {
1312 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1313 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1314 #warning FIXME changes
1315 //moveCursorUpdate(false);
1317 while (lyxfind::findNextChange(bv_)) {
1318 bv_->getLyXText()->acceptChange(bv_);
1321 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1325 case LFUN_REJECT_ALL_CHANGES: {
1326 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1327 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1328 #warning FIXME changes
1329 //moveCursorUpdate(false);
1331 while (lyxfind::findNextChange(bv_)) {
1332 bv_->getLyXText()->rejectChange(bv_);
1335 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1339 case LFUN_ACCEPT_CHANGE: {
1340 bv_->getLyXText()->acceptChange(bv_);
1342 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1346 case LFUN_REJECT_CHANGE: {
1347 bv_->getLyXText()->rejectChange(bv_);
1349 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1353 case LFUN_UNKNOWN_ACTION:
1354 ev.errorMessage(N_("Unknown function!"));
1358 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1365 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1367 // if we are in a locking inset we should try to insert the
1368 // inset there otherwise this is a illegal function now
1369 if (bv_->theLockingInset()) {
1370 if (bv_->theLockingInset()->insetAllowed(inset))
1371 return bv_->theLockingInset()->insertInset(bv_, inset);
1375 // not quite sure if we want this...
1376 setCursorParUndo(bv_);
1379 beforeChange(bv_->text);
1380 if (!lout.empty()) {
1381 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1382 bv_->text->breakParagraph(bv_);
1383 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1385 if (!bv_->text->cursor.par()->empty()) {
1386 bv_->text->cursorLeft(bv_);
1388 bv_->text->breakParagraph(bv_);
1389 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1393 LyXTextClass const & tclass =
1394 buffer_->params.getLyXTextClass();
1395 bool hasLayout = tclass.hasLayout(lres);
1396 string lay = tclass.defaultLayoutName();
1398 if (hasLayout != false) {
1402 // layout not fount using default
1403 lay = tclass.defaultLayoutName();
1406 bv_->text->setLayout(bv_, lay);
1408 bv_->text->setParagraph(bv_, 0, 0,
1410 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1415 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1418 bv_->text->insertInset(bv_, inset);
1419 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1426 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1428 if (!inset || !available())
1431 // first check for locking insets
1432 if (bv_->theLockingInset()) {
1433 if (bv_->theLockingInset() == inset) {
1434 if (bv_->text->updateInset(bv_, inset)) {
1437 buffer_->markDirty();
1442 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1443 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1446 buffer_->markDirty();
1454 // then check if the inset is a top_level inset (has no owner)
1455 // if yes do the update as always otherwise we have to update the
1456 // toplevel inset where this inset is inside
1457 Inset * tl_inset = inset;
1458 while (tl_inset->owner())
1459 tl_inset = tl_inset->owner();
1461 if (tl_inset == inset) {
1462 update(bv_->text, BufferView::UPDATE);
1463 if (bv_->text->updateInset(bv_, inset)) {
1467 | BufferView::FITCUR
1468 | BufferView::CHANGE);
1470 update(bv_->text, SELECT);
1474 } else if (static_cast<UpdatableInset *>(tl_inset)
1475 ->updateInsetInInset(bv_, inset))
1477 if (bv_->text->updateInset(bv_, tl_inset)) {