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 ?
188 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
190 // Similarly, buffer-dependent dialogs should be updated or
191 // hidden. This should go here because some dialogs (eg ToC)
192 // require bv_->text.
193 owner_->getDialogs().updateBufferDependent(true);
195 lyxerr[Debug::INFO] << " No Buffer!" << endl;
196 owner_->getDialogs().hideBufferDependent();
198 // Also remove all remaining text's from the testcache.
199 // (there should not be any!) (if there is any it is a
201 if (lyxerr.debugging())
202 textcache.show(lyxerr, "buffer delete all");
208 owner_->updateMenubar();
209 owner_->updateToolbar();
210 owner_->updateLayoutChoice();
211 owner_->updateWindowTitle();
213 if (grfx::Previews::activated() && buffer_)
214 grfx::Previews::get().generateBufferPreviews(*buffer_);
218 bool BufferView::Pimpl::fitCursor()
222 if (bv_->theLockingInset()) {
223 bv_->theLockingInset()->fitInsetCursor(bv_);
226 ret = screen().fitCursor(bv_->text, bv_);
229 bv_->owner()->getDialogs().updateParagraph();
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(bv_, selstartpar, selstartpos);
312 bv_->text->selection.cursor = bv_->text->cursor;
313 bv_->text->setCursor(bv_, selendpar, selendpos);
314 bv_->text->setSelection(bv_);
315 bv_->text->setCursor(bv_, par, pos);
317 bv_->text->setCursor(bv_, 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->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_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 << ", first_y "
354 << t.first_y << ", default height " << defaultRowHeight() << endl;
356 workarea().setScrollbarParams(t.height, t.first_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->first_y + height));
376 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
378 if (vbt->cursor.y() < first)
379 vbt->setCursorFromCoordinates(bv_, 0, first);
380 else if (vbt->cursor.y() > last)
381 vbt->setCursorFromCoordinates(bv_, 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_first_y = t->first_y + lines * line_height;
397 // Restrict to a valid value
398 new_first_y = std::min(t->height - 4 * line_height, new_first_y);
399 new_first_y = std::max(0, new_first_y);
401 scrollDocView(new_first_y);
403 // Update the scrollbar.
404 workarea().setScrollbarParams(t->height, t->first_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 LyXText::text_status st = bv_->text->status();
504 screen().update(bv_->text, bv_);
506 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
507 bv_->text->fullRebreak(bv_);
508 st = LyXText::NEED_MORE_REFRESH;
509 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
510 bv_->text->cursor.pos());
511 if (bv_->text->selection.set()) {
512 bv_->text->setCursor(bv_, bv_->text->selection.start,
513 bv_->text->selection.start.par(),
514 bv_->text->selection.start.pos());
515 bv_->text->setCursor(bv_, bv_->text->selection.end,
516 bv_->text->selection.end.par(),
517 bv_->text->selection.end.pos());
520 bv_->text->status(bv_, st);
521 screen().update(bv_->text, bv_);
523 // do this here instead of in the screen::update because of
525 bv_->text->status(bv_, LyXText::UNCHANGED);
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;
577 text->fullRebreak(bv_);
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(bv_, 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 first_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 first_y must make sure to call
786 // updateScrollbar() currently. Never mind that this is a
787 // pretty obfuscated way of updating t->first_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(bv_, &(*buf->paragraphs.begin()), 0);
944 #warning changes FIXME
945 //moveCursorUpdate(false);
947 bool found = lyxfind::findNextChange(bv_);
949 owner_->getDialogs().showMergeChanges();
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_INSET_GRAPHICS:
994 Inset * new_inset = new InsetGraphics;
995 if (!insertInset(new_inset)) {
998 // this is need because you don't use a inset->Edit()
999 updateInset(new_inset, true);
1000 new_inset->edit(bv_);
1005 case LFUN_LAYOUT_COPY:
1006 bv_->copyEnvironment();
1009 case LFUN_LAYOUT_PASTE:
1010 bv_->pasteEnvironment();
1014 case LFUN_DEPTH_MIN:
1015 changeDepth(bv_, bv_->getLyXText(), -1);
1018 case LFUN_DEPTH_PLUS:
1019 changeDepth(bv_, bv_->getLyXText(), 1);
1023 owner_->getDialogs().setUserFreeFont();
1026 case LFUN_FILE_INSERT:
1027 MenuInsertLyXFile(ev.argument);
1030 case LFUN_FILE_INSERT_ASCII_PARA:
1031 InsertAsciiFile(bv_, ev.argument, true);
1034 case LFUN_FILE_INSERT_ASCII:
1035 InsertAsciiFile(bv_, ev.argument, false);
1039 lang(bv_, ev.argument);
1041 owner_->view_state_changed();
1046 owner_->view_state_changed();
1051 owner_->view_state_changed();
1056 owner_->view_state_changed();
1061 owner_->view_state_changed();
1066 owner_->view_state_changed();
1071 owner_->view_state_changed();
1076 owner_->view_state_changed();
1079 case LFUN_UNDERLINE:
1081 owner_->view_state_changed();
1084 case LFUN_FONT_SIZE:
1085 fontSize(bv_, ev.argument);
1086 owner_->view_state_changed();
1089 case LFUN_FONT_STATE:
1090 owner_->getLyXFunc().setMessage(currentState(bv_));
1093 case LFUN_INSERT_LABEL:
1094 MenuInsertLabel(bv_, ev.argument);
1097 case LFUN_BOOKMARK_SAVE:
1098 savePosition(strToUnsignedInt(ev.argument));
1101 case LFUN_BOOKMARK_GOTO:
1102 restorePosition(strToUnsignedInt(ev.argument));
1107 string label = ev.argument;
1108 if (label.empty()) {
1110 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1112 label = inset->getContents();
1117 if (!label.empty()) {
1118 //bv_->savePosition(0);
1119 if (!bv_->gotoLabel(label))
1120 Alert::alert(_("Error"),
1121 _("Couldn't find this label"),
1122 _("in current document."));
1127 // --- accented characters ---------------------------
1130 case LFUN_CIRCUMFLEX:
1140 case LFUN_SPECIAL_CARON:
1143 case LFUN_HUNG_UMLAUT:
1146 if (ev.argument.empty()) {
1148 owner_->getLyXFunc().handleKeyFunc(ev.action);
1150 owner_->getLyXFunc().handleKeyFunc(ev.action);
1151 owner_->getIntl().getTransManager()
1152 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1153 update(bv_->getLyXText(),
1155 | BufferView::FITCUR
1156 | BufferView::CHANGE);
1160 case LFUN_MATH_MACRO:
1161 case LFUN_MATH_DELIM:
1162 case LFUN_INSERT_MATRIX:
1163 case LFUN_INSERT_MATH:
1164 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1165 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1166 case LFUN_MATH_MODE: // Open or create an inlined math inset
1167 case LFUN_GREEK: // Insert a single greek letter
1171 case LFUN_BIBITEM_APPLY:
1172 case LFUN_BIBTEX_APPLY:
1173 case LFUN_CITATION_APPLY:
1174 case LFUN_ERT_APPLY:
1175 case LFUN_INDEX_APPLY:
1176 case LFUN_REF_APPLY:
1177 case LFUN_TOC_APPLY:
1178 case LFUN_URL_APPLY: {
1180 switch (ev.action) {
1181 case LFUN_BIBITEM_APPLY:
1184 case LFUN_BIBTEX_APPLY:
1187 case LFUN_CITATION_APPLY:
1190 case LFUN_ERT_APPLY:
1193 case LFUN_INDEX_APPLY:
1196 case LFUN_REF_APPLY:
1199 case LFUN_TOC_APPLY:
1202 case LFUN_URL_APPLY:
1209 InsetBase * base = owner_->getDialogs().getOpenInset(name);
1212 // This works both for 'original' and 'mathed' insets.
1213 // Note that the localDispatch performs updateInset
1215 base->localDispatch(ev);
1217 inset = createInset(ev);
1218 if (inset && insertInset(inset)) {
1219 updateInset(inset, true);
1225 if (ev.action == LFUN_BIBITEM_APPLY) {
1226 // We need to do a redraw because the maximum
1227 // InsetBibitem width could have changed
1228 #warning please check you mean repaint() not update(),
1229 #warning and whether the repaint() is needed at all
1236 case LFUN_CHILD_INSERT:
1238 InsetInclude::Params p;
1239 if (!ev.argument.empty())
1240 p.cparams.setFromString(ev.argument);
1241 p.masterFilename_ = buffer_->fileName();
1243 InsetInclude * inset = new InsetInclude(p);
1244 if (!insertInset(inset))
1247 updateInset(inset, true);
1248 bv_->owner()->getDialogs().showInclude(inset);
1253 case LFUN_FLOAT_LIST:
1254 if (tclass.floats().typeExist(ev.argument)) {
1255 Inset * inset = new InsetFloatList(ev.argument);
1256 if (!insertInset(inset, tclass.defaultLayoutName()))
1259 lyxerr << "Non-existent float type: "
1260 << ev.argument << endl;
1264 case LFUN_THESAURUS_ENTRY:
1266 string arg = ev.argument;
1269 arg = bv_->getLyXText()->selectionAsString(buffer_,
1273 if (arg.size() > 100 || arg.empty()) {
1274 // Get word or selection
1275 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1276 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1277 // FIXME: where is getLyXText()->unselect(bv_) ?
1281 bv_->owner()->getDialogs().showThesaurus(arg);
1285 case LFUN_TRACK_CHANGES:
1289 case LFUN_MERGE_CHANGES:
1290 owner_->getDialogs().showMergeChanges();
1293 case LFUN_ACCEPT_ALL_CHANGES: {
1294 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1295 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1296 #warning FIXME changes
1297 //moveCursorUpdate(false);
1299 while (lyxfind::findNextChange(bv_)) {
1300 bv_->getLyXText()->acceptChange(bv_);
1303 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1307 case LFUN_REJECT_ALL_CHANGES: {
1308 bv_->update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
1309 bv_->text->setCursor(bv_, &(*bv_->buffer()->paragraphs.begin()), 0);
1310 #warning FIXME changes
1311 //moveCursorUpdate(false);
1313 while (lyxfind::findNextChange(bv_)) {
1314 bv_->getLyXText()->rejectChange(bv_);
1317 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1321 case LFUN_ACCEPT_CHANGE: {
1322 bv_->getLyXText()->acceptChange(bv_);
1324 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1328 case LFUN_REJECT_CHANGE: {
1329 bv_->getLyXText()->rejectChange(bv_);
1331 BufferView::SELECT | BufferView::FITCUR | BufferView::CHANGE);
1335 case LFUN_UNKNOWN_ACTION:
1336 ev.errorMessage(N_("Unknown function!"));
1340 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1347 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1349 // if we are in a locking inset we should try to insert the
1350 // inset there otherwise this is a illegal function now
1351 if (bv_->theLockingInset()) {
1352 if (bv_->theLockingInset()->insetAllowed(inset))
1353 return bv_->theLockingInset()->insertInset(bv_, inset);
1357 // not quite sure if we want this...
1358 setCursorParUndo(bv_);
1361 beforeChange(bv_->text);
1362 if (!lout.empty()) {
1363 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1364 bv_->text->breakParagraph(bv_);
1365 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1367 if (!bv_->text->cursor.par()->empty()) {
1368 bv_->text->cursorLeft(bv_);
1370 bv_->text->breakParagraph(bv_);
1371 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1375 LyXTextClass const & tclass =
1376 buffer_->params.getLyXTextClass();
1377 bool hasLayout = tclass.hasLayout(lres);
1378 string lay = tclass.defaultLayoutName();
1380 if (hasLayout != false) {
1384 // layout not fount using default
1385 lay = tclass.defaultLayoutName();
1388 bv_->text->setLayout(bv_, lay);
1390 bv_->text->setParagraph(bv_, 0, 0,
1392 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1397 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1400 bv_->text->insertInset(bv_, inset);
1401 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1408 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1410 if (!inset || !available())
1413 // first check for locking insets
1414 if (bv_->theLockingInset()) {
1415 if (bv_->theLockingInset() == inset) {
1416 if (bv_->text->updateInset(bv_, inset)) {
1419 buffer_->markDirty();
1424 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1425 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1428 buffer_->markDirty();
1436 // then check if the inset is a top_level inset (has no owner)
1437 // if yes do the update as always otherwise we have to update the
1438 // toplevel inset where this inset is inside
1439 Inset * tl_inset = inset;
1440 while (tl_inset->owner())
1441 tl_inset = tl_inset->owner();
1443 if (tl_inset == inset) {
1444 update(bv_->text, BufferView::UPDATE);
1445 if (bv_->text->updateInset(bv_, inset)) {
1449 | BufferView::FITCUR
1450 | BufferView::CHANGE);
1452 update(bv_->text, SELECT);
1456 } else if (static_cast<UpdatableInset *>(tl_inset)
1457 ->updateInsetInInset(bv_, inset))
1459 if (bv_->text->updateInset(bv_, tl_inset)) {