2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author Lars Gullik Bjønnes
12 #include "BufferView_pimpl.h"
13 #include "bufferlist.h"
15 #include "buffer_funcs.h"
16 #include "bufferview_funcs.h"
20 #include "FloatList.h"
21 #include "funcrequest.h"
24 #include "iterators.h"
26 #include "lyx_cb.h" // added for Dispatch functions
32 #include "lastfiles.h"
33 #include "paragraph.h"
34 #include "ParagraphParameters.h"
35 #include "TextCache.h"
36 #include "undo_funcs.h"
38 #include "insets/insetfloatlist.h"
39 #include "insets/insetgraphics.h"
40 #include "insets/insetinclude.h"
41 #include "insets/insetref.h"
42 #include "insets/insettext.h"
44 #include "frontends/Alert.h"
45 #include "frontends/Dialogs.h"
46 #include "frontends/FileDialog.h"
47 #include "frontends/LyXView.h"
48 #include "frontends/LyXScreenFactory.h"
49 #include "frontends/mouse_state.h"
50 #include "frontends/screen.h"
51 #include "frontends/WorkArea.h"
52 #include "frontends/WorkAreaFactory.h"
54 #include "mathed/formulabase.h"
56 #include "graphics/Previews.h"
58 #include "support/LAssert.h"
59 #include "support/tostr.h"
60 #include "support/filetools.h"
61 #include "support/path_defines.h"
63 #include <boost/bind.hpp>
64 #include <boost/signals/connection.hpp>
79 using namespace lyx::support;
80 using namespace bv_funcs;
82 extern BufferList bufferlist;
87 unsigned int const saved_positions_num = 20;
89 // All the below connection objects are needed because of a bug in some
90 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
91 // to these connections we avoid a segfault upon startup, and also at exit.
94 boost::signals::connection dispatchcon;
95 boost::signals::connection timecon;
96 boost::signals::connection doccon;
97 boost::signals::connection resizecon;
98 boost::signals::connection kpresscon;
99 boost::signals::connection selectioncon;
100 boost::signals::connection lostcon;
106 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
107 int xpos, int ypos, int width, int height)
108 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
109 using_xterm_cursor(false)
111 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
112 screen_.reset(LyXScreenFactory::create(workarea()));
115 doccon = workarea().scrollDocView
116 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
117 resizecon = workarea().workAreaResize
118 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
119 dispatchcon = workarea().dispatch
120 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
121 kpresscon = workarea().workAreaKeyPress
122 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
123 selectioncon = workarea().selectionRequested
124 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
125 lostcon = workarea().selectionLost
126 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
128 timecon = cursor_timeout.timeout
129 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
130 cursor_timeout.start();
131 saved_positions.resize(saved_positions_num);
135 void BufferView::Pimpl::addError(ErrorItem const & ei)
137 errorlist_.push_back(ei);
141 void BufferView::Pimpl::showReadonly(bool)
143 owner_->updateWindowTitle();
144 owner_->getDialogs().updateBufferDependent(false);
148 void BufferView::Pimpl::connectBuffer(Buffer & buf)
150 if (errorConnection_.connected())
153 errorConnection_ = buf.error.connect(boost::bind(&BufferView::Pimpl::addError, this, _1));
154 messageConnection_ = buf.message.connect(boost::bind(&LyXView::message, owner_, _1));
155 busyConnection_ = buf.busy.connect(boost::bind(&LyXView::busy, owner_, _1));
156 titleConnection_ = buf.updateTitles.connect(boost::bind(&LyXView::updateWindowTitle, owner_));
157 timerConnection_ = buf.resetAutosaveTimers.connect(boost::bind(&LyXView::resetAutosaveTimer, owner_));
158 readonlyConnection_ = buf.readonly.connect(boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
159 closingConnection_ = buf.closing.connect(boost::bind(&BufferView::Pimpl::buffer, this, (Buffer *)0));
163 void BufferView::Pimpl::disconnectBuffer()
165 errorConnection_.disconnect();
166 messageConnection_.disconnect();
167 busyConnection_.disconnect();
168 titleConnection_.disconnect();
169 timerConnection_.disconnect();
170 readonlyConnection_.disconnect();
171 closingConnection_.disconnect();
175 bool BufferView::Pimpl::newFile(string const & filename,
176 string const & tname,
179 Buffer * b = ::newFile(filename, tname, isNamed);
185 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
187 // get absolute path of file and add ".lyx" to the filename if
189 string s = FileSearch(string(), filename, "lyx");
191 bool const found = !s.empty();
196 // file already open?
197 if (bufferlist.exists(s)) {
198 string const file = MakeDisplayPath(s, 20);
199 string text = bformat(_("The document %1$s is already "
200 "loaded.\n\nDo you want to revert "
201 "to the saved version?"), file);
202 int const ret = Alert::prompt(_("Revert to saved document?"),
203 text, 0, 1, _("&Revert"), _("&Switch to document"));
206 buffer(bufferlist.getBuffer(s));
209 // FIXME: should be LFUN_REVERT
210 if (!bufferlist.close(bufferlist.getBuffer(s), false))
212 // Fall through to new load. (Asger)
219 b = bufferlist.newBuffer(s);
221 if (!::loadLyXFile(b, s)) {
222 bufferlist.release(b);
226 string text = bformat(_("The document %1$s does not yet "
227 "exist.\n\nDo you want to create "
228 "a new document?"), s);
229 int const ret = Alert::prompt(_("Create new document?"),
230 text, 0, 1, _("&Create"), _("Cancel"));
233 b = ::newFile(s, string(), true);
239 bv_->showErrorList(_("Parse"));
242 lastfiles->newFile(b->fileName());
248 WorkArea & BufferView::Pimpl::workarea() const
250 return *workarea_.get();
254 LyXScreen & BufferView::Pimpl::screen() const
256 return *screen_.get();
260 Painter & BufferView::Pimpl::painter() const
262 return workarea().getPainter();
266 void BufferView::Pimpl::buffer(Buffer * b)
268 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
272 // Put the old text into the TextCache, but
273 // only if the buffer is still loaded.
274 // Also set the owner of the test to 0
275 // bv_->text->owner(0);
276 textcache.add(buffer_, workarea().workWidth(), bv_->text);
277 if (lyxerr.debugging())
278 textcache.show(lyxerr, "BufferView::buffer");
283 // set current buffer
286 // if we're quitting lyx, don't bother updating stuff
290 // if we are closing the buffer, use the first buffer as current
292 buffer_ = bufferlist.first();
295 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
296 connectBuffer(*buffer_);
298 // If we don't have a text object for this, we make one
300 resizeCurrentBuffer();
302 // FIXME: needed when ?
303 bv_->text->top_y(screen().topCursorVisible(bv_->text));
305 // Buffer-dependent dialogs should be updated or
306 // hidden. This should go here because some dialogs (eg ToC)
307 // require bv_->text.
308 owner_->getDialogs().updateBufferDependent(true);
310 lyxerr[Debug::INFO] << " No Buffer!" << endl;
311 owner_->getDialogs().hideBufferDependent();
313 // Also remove all remaining text's from the testcache.
314 // (there should not be any!) (if there is any it is a
316 if (lyxerr.debugging())
317 textcache.show(lyxerr, "buffer delete all");
323 owner_->updateMenubar();
324 owner_->updateToolbar();
325 owner_->updateLayoutChoice();
326 owner_->updateWindowTitle();
329 // Don't forget to update the Layout
330 string const layoutname =
331 bv_->text->cursor.par()->layout()->name();
332 owner_->setLayout(layoutname);
335 if (lyx::graphics::Previews::activated() && buffer_)
336 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
340 bool BufferView::Pimpl::fitCursor()
344 if (bv_->theLockingInset()) {
345 bv_->theLockingInset()->fitInsetCursor(bv_);
348 ret = screen().fitCursor(bv_->text, bv_);
351 //dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
353 // We need to always update, in case we did a
354 // paste and we stayed anchored to a row, but
355 // the actual height of the doc changed ...
361 void BufferView::Pimpl::redoCurrentBuffer()
363 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
364 if (buffer_ && bv_->text) {
365 resizeCurrentBuffer();
367 owner_->updateLayoutChoice();
373 void BufferView::Pimpl::resizeCurrentBuffer()
375 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
377 ParagraphList::iterator par;
378 ParagraphList::iterator selstartpar;
379 ParagraphList::iterator selendpar;
380 UpdatableInset * the_locking_inset = 0;
383 pos_type selstartpos = 0;
384 pos_type selendpos = 0;
385 bool selection = false;
386 bool mark_set = false;
390 owner_->message(_("Formatting document..."));
393 par = bv_->text->cursor.par();
394 pos = bv_->text->cursor.pos();
395 selstartpar = bv_->text->selection.start.par();
396 selstartpos = bv_->text->selection.start.pos();
397 selendpar = bv_->text->selection.end.par();
398 selendpos = bv_->text->selection.end.pos();
399 selection = bv_->text->selection.set();
400 mark_set = bv_->text->selection.mark();
401 the_locking_inset = bv_->theLockingInset();
402 bv_->text->fullRebreak();
405 lyxerr << "text not available!" << endl;
406 // See if we have a text in TextCache that fits
407 // the new buffer_ with the correct width.
408 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
410 lyxerr << "text in cache!" << endl;
411 if (lyxerr.debugging()) {
412 lyxerr << "Found a LyXText that fits:" << endl;
413 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
415 // Set the owner of the newly found text
416 // bv_->text->owner(bv_);
417 if (lyxerr.debugging())
418 textcache.show(lyxerr, "resizeCurrentBuffer");
420 lyxerr << "no text in cache!" << endl;
421 bv_->text = new LyXText(bv_, 0, false, bv_->buffer()->paragraphs);
422 bv_->text->init(bv_);
425 par = bv_->text->ownerParagraphs().end();
426 selstartpar = bv_->text->ownerParagraphs().end();
427 selendpar = bv_->text->ownerParagraphs().end();
430 #warning does not help much
431 bv_->text->redoParagraphs(bv_->text->ownerParagraphs().begin(),
432 bv_->text->ownerParagraphs().end());
434 if (par != bv_->text->ownerParagraphs().end()) {
435 bv_->text->selection.set(true);
436 // At this point just to avoid the Delete-Empty-Paragraph-
437 // Mechanism when setting the cursor.
438 bv_->text->selection.mark(mark_set);
440 bv_->text->setCursor(selstartpar, selstartpos);
441 bv_->text->selection.cursor = bv_->text->cursor;
442 bv_->text->setCursor(selendpar, selendpos);
443 bv_->text->setSelection();
444 bv_->text->setCursor(par, pos);
446 bv_->text->setCursor(par, pos);
447 bv_->text->selection.cursor = bv_->text->cursor;
448 bv_->text->selection.set(false);
450 // remake the inset locking
451 bv_->theLockingInset(the_locking_inset);
454 bv_->text->top_y(screen().topCursorVisible(bv_->text));
459 // reset the "Formatting..." message
460 owner_->clearMessage();
466 void BufferView::Pimpl::updateScrollbar()
469 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
470 workarea().setScrollbarParams(0, 0, 0);
474 LyXText const & t = *bv_->text;
476 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
477 << t.top_y() << ", default height " << defaultRowHeight() << endl;
479 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
483 void BufferView::Pimpl::scrollDocView(int value)
485 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
490 screen().hideCursor();
492 screen().draw(bv_->text, bv_, value);
494 if (!lyxrc.cursor_follows_scrollbar)
497 LyXText * vbt = bv_->text;
499 int const height = defaultRowHeight();
500 int const first = static_cast<int>((bv_->text->top_y() + height));
501 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
503 if (vbt->cursor.y() < first)
504 vbt->setCursorFromCoordinates(0, first);
505 else if (vbt->cursor.y() > last)
506 vbt->setCursorFromCoordinates(0, last);
508 owner_->updateLayoutChoice();
512 void BufferView::Pimpl::scroll(int lines)
518 LyXText const * t = bv_->text;
519 int const line_height = defaultRowHeight();
521 // The new absolute coordinate
522 int new_top_y = t->top_y() + lines * line_height;
524 // Restrict to a valid value
525 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
526 new_top_y = std::max(0, new_top_y);
528 scrollDocView(new_top_y);
530 // Update the scrollbar.
531 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
535 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
536 key_modifier::state state)
538 bv_->owner()->getLyXFunc().processKeySym(key, state);
540 /* This is perhaps a bit of a hack. When we move
541 * around, or type, it's nice to be able to see
542 * the cursor immediately after the keypress. So
543 * we reset the toggle timeout and force the visibility
544 * of the cursor. Note we cannot do this inside
545 * dispatch() itself, because that's called recursively.
548 cursor_timeout.restart();
549 screen().showCursor(*bv_);
554 void BufferView::Pimpl::selectionRequested()
561 LyXText * text = bv_->getLyXText();
563 if (text->selection.set() &&
564 (!bv_->text->xsel_cache.set() ||
565 text->selection.start != bv_->text->xsel_cache.start ||
566 text->selection.end != bv_->text->xsel_cache.end))
568 bv_->text->xsel_cache = text->selection;
569 sel = text->selectionAsString(bv_->buffer(), false);
570 } else if (!text->selection.set()) {
572 bv_->text->xsel_cache.set(false);
575 workarea().putClipboard(sel);
580 void BufferView::Pimpl::selectionLost()
583 screen().hideCursor();
584 bv_->getLyXText()->clearSelection();
585 bv_->text->xsel_cache.set(false);
590 void BufferView::Pimpl::workAreaResize()
592 static int work_area_width;
593 static int work_area_height;
595 bool const widthChange = workarea().workWidth() != work_area_width;
596 bool const heightChange = workarea().workHeight() != work_area_height;
598 // update from work area
599 work_area_width = workarea().workWidth();
600 work_area_height = workarea().workHeight();
604 // The visible LyXView need a resize
605 resizeCurrentBuffer();
607 // Remove all texts from the textcache
608 // This is not _really_ what we want to do. What
609 // we really want to do is to delete in textcache
610 // that does not have a BufferView with matching
611 // width, but as long as we have only one BufferView
612 // deleting all gives the same result.
613 if (lyxerr.debugging())
614 textcache.show(lyxerr, "Expose delete all");
619 if (widthChange || heightChange)
622 // always make sure that the scrollbar is sane.
624 owner_->updateLayoutChoice();
628 void BufferView::Pimpl::update()
630 lyxerr << "BufferView::update()" << endl;
631 // fix cursor coordinate cache in case something went wrong
632 if (bv_->getLyXText()) {
633 // check needed to survive LyX startup
634 bv_->getLyXText()->redoCursor();
637 screen().redraw(*bv_);
641 // Callback for cursor timer
642 void BufferView::Pimpl::cursorToggle()
645 cursor_timeout.restart();
649 screen().toggleCursor(*bv_);
651 cursor_timeout.restart();
655 bool BufferView::Pimpl::available() const
657 if (buffer_ && bv_->text)
663 Change const BufferView::Pimpl::getCurrentChange()
665 if (!bv_->buffer()->params.tracking_changes)
666 return Change(Change::UNCHANGED);
668 LyXText * text = bv_->getLyXText();
670 if (!text->selection.set())
671 return Change(Change::UNCHANGED);
673 LyXCursor const & cur = text->selection.start;
674 return cur.par()->lookupChangeFull(cur.pos());
678 void BufferView::Pimpl::beforeChange(LyXText * text)
680 text->clearSelection();
684 void BufferView::Pimpl::savePosition(unsigned int i)
686 if (i >= saved_positions_num)
688 saved_positions[i] = Position(buffer_->fileName(),
689 bv_->text->cursor.par()->id(),
690 bv_->text->cursor.pos());
692 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
696 void BufferView::Pimpl::restorePosition(unsigned int i)
698 if (i >= saved_positions_num)
701 string const fname = saved_positions[i].filename;
703 beforeChange(bv_->text);
705 if (fname != buffer_->fileName()) {
707 if (bufferlist.exists(fname))
708 b = bufferlist.getBuffer(fname);
710 b = bufferlist.newBuffer(fname);
711 ::loadLyXFile(b, fname); // don't ask, just load it
717 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
718 if (par == buffer_->par_iterator_end())
721 bv_->text->setCursor(par.pit(),
722 min(par->size(), saved_positions[i].par_pos));
726 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
730 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
732 if (i >= saved_positions_num)
735 return !saved_positions[i].filename.empty();
739 void BufferView::Pimpl::switchKeyMap()
741 if (!lyxrc.rtl_support)
744 LyXText * text = bv_->getLyXText();
745 if (text->real_current_font.isRightToLeft()
746 && !(bv_->theLockingInset()
747 && bv_->theLockingInset()->lyxCode() == InsetOld::ERT_CODE))
749 if (owner_->getIntl().keymap == Intl::PRIMARY)
750 owner_->getIntl().KeyMapSec();
752 if (owner_->getIntl().keymap == Intl::SECONDARY)
753 owner_->getIntl().KeyMapPrim();
758 void BufferView::Pimpl::insetUnlock()
760 if (bv_->theLockingInset()) {
761 bv_->theLockingInset()->insetUnlock(bv_);
762 bv_->theLockingInset(0);
768 void BufferView::Pimpl::center()
770 LyXText * t = bv_->text;
773 int const half_height = workarea().workHeight() / 2;
776 if (t->cursor.y() > half_height)
777 new_y = t->cursor.y() - half_height;
779 // FIXME: look at this comment again ...
781 // FIXME: can we do this w/o calling screen directly ?
782 // This updates top_y() but means the fitCursor() call
783 // from the update(FITCUR) doesn't realise that we might
784 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
785 // the scrollbar to be updated as it should, so we have
786 // to do it manually. Any operation that does a center()
787 // and also might have moved top_y() must make sure to call
788 // updateScrollbar() currently. Never mind that this is a
789 // pretty obfuscated way of updating t->top_y()
790 screen().draw(t, bv_, new_y);
796 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
798 workarea().putClipboard(stuff);
803 * Dispatch functions for actions which can be valid for BufferView->text
804 * and/or InsetText->text!!!
808 InsetOld * BufferView::Pimpl::getInsetByCode(InsetOld::Code code)
811 LyXCursor cursor = bv_->getLyXText()->cursor;
812 Buffer::inset_iterator it =
813 find_if(Buffer::inset_iterator(
814 cursor.par(), cursor.pos()),
815 buffer_->inset_iterator_end(),
816 lyx::compare_memfun(&Inset::lyxCode, code));
817 return it != buffer_->inset_iterator_end() ? (*it) : 0;
819 // Ok, this is a little bit too brute force but it
820 // should work for now. Better infrastructure is comming. (Lgb)
822 Buffer * b = bv_->buffer();
823 LyXCursor cursor = bv_->getLyXText()->cursor;
825 Buffer::inset_iterator beg = b->inset_iterator_begin();
826 Buffer::inset_iterator end = b->inset_iterator_end();
828 bool cursor_par_seen = false;
830 for (; beg != end; ++beg) {
831 if (beg.getPar() == cursor.par()) {
832 cursor_par_seen = true;
834 if (cursor_par_seen) {
835 if (beg.getPar() == cursor.par()
836 && beg.getPos() >= cursor.pos()) {
838 } else if (beg.getPar() != cursor.par()) {
845 // Now find the first inset that matches code.
846 for (; beg != end; ++beg) {
847 if (beg->lyxCode() == code) {
857 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
859 string filename = filen;
861 if (filename.empty()) {
862 // Launch a file browser
863 string initpath = lyxrc.document_path;
866 string const trypath = owner_->buffer()->filePath();
867 // If directory is writeable, use this as default.
868 if (IsDirWriteable(trypath))
872 FileDialog fileDlg(_("Select LyX document to insert"),
874 make_pair(string(_("Documents|#o#O")),
875 string(lyxrc.document_path)),
876 make_pair(string(_("Examples|#E#e")),
877 string(AddPath(system_lyxdir(), "examples"))));
879 FileDialog::Result result =
880 fileDlg.open(initpath,
881 _("*.lyx| LyX Documents (*.lyx)"));
883 if (result.first == FileDialog::Later)
886 filename = result.second;
888 // check selected filename
889 if (filename.empty()) {
890 owner_->message(_("Canceled."));
895 // get absolute path of file and add ".lyx" to the filename if
897 filename = FileSearch(string(), filename, "lyx");
899 string const disp_fn = MakeDisplayPath(filename);
900 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
901 if (bv_->insertLyXFile(filename))
902 owner_->message(bformat(_("Document %1$s inserted."),
905 owner_->message(bformat(_("Could not insert document %1$s"),
910 void BufferView::Pimpl::trackChanges()
912 Buffer * buf(bv_->buffer());
913 bool const tracking(buf->params.tracking_changes);
916 ParIterator const end = buf->par_iterator_end();
917 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
919 buf->params.tracking_changes = true;
921 // we cannot allow undos beyond the freeze point
922 buf->undostack.clear();
925 bv_->text->setCursor(buf->paragraphs.begin(), 0);
926 #warning changes FIXME
927 //moveCursorUpdate(false);
929 bool found = lyx::find::findNextChange(bv_);
931 owner_->getDialogs().show("changes");
935 ParIterator const end = buf->par_iterator_end();
936 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
937 it->untrackChanges();
938 buf->params.tracking_changes = false;
941 buf->redostack.clear();
945 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev)
948 case LFUN_MOUSE_PRESS:
949 case LFUN_MOUSE_MOTION:
950 case LFUN_MOUSE_RELEASE:
951 case LFUN_MOUSE_DOUBLE:
952 case LFUN_MOUSE_TRIPLE:
954 // We pass those directly to the Bufferview, since
955 // otherwise selection handling breaks down
957 // Doesn't go through lyxfunc, so we need to update
958 // the layout choice etc. ourselves
960 // e.g. Qt mouse press when no buffer
964 screen().hideCursor();
966 bool const res = dispatch(ev);
968 // see workAreaKeyPress
969 cursor_timeout.restart();
970 screen().showCursor(*bv_);
972 // FIXME: we should skip these when selecting
973 owner_->updateLayoutChoice();
974 owner_->updateToolbar();
977 // slight hack: this is only called currently when we
978 // clicked somewhere, so we force through the display
979 // of the new status here.
980 owner_->clearMessage();
985 owner_->dispatch(ev);
991 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
993 // Make sure that the cached BufferView is correct.
994 FuncRequest ev = ev_in;
997 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
998 << " action[" << ev.action << ']'
999 << " arg[" << ev.argument << ']'
1000 << " x[" << ev.x << ']'
1001 << " y[" << ev.y << ']'
1002 << " button[" << ev.button() << ']'
1005 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1007 switch (ev.action) {
1009 case LFUN_SCROLL_INSET:
1010 // this is not handled here as this function is only active
1011 // if we have a locking_inset and that one is (or contains)
1015 case LFUN_FILE_INSERT:
1016 MenuInsertLyXFile(ev.argument);
1019 case LFUN_FILE_INSERT_ASCII_PARA:
1020 InsertAsciiFile(bv_, ev.argument, true);
1023 case LFUN_FILE_INSERT_ASCII:
1024 InsertAsciiFile(bv_, ev.argument, false);
1028 lang(bv_, ev.argument);
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();
1055 owner_->view_state_changed();
1060 owner_->view_state_changed();
1065 owner_->view_state_changed();
1068 case LFUN_UNDERLINE:
1070 owner_->view_state_changed();
1073 case LFUN_FONT_SIZE:
1074 fontSize(bv_, ev.argument);
1075 owner_->view_state_changed();
1078 case LFUN_FONT_STATE:
1079 owner_->getLyXFunc().setMessage(currentState(bv_));
1082 case LFUN_INSERT_LABEL: {
1083 // Try and generate a valid label
1084 string const contents = ev.argument.empty() ?
1085 getPossibleLabel(*bv_) : ev.argument;
1086 InsetCommandParams icp("label", contents);
1087 string data = InsetCommandMailer::params2string("label", icp);
1088 owner_->getDialogs().show("label", data, 0);
1092 case LFUN_BOOKMARK_SAVE:
1093 savePosition(strToUnsignedInt(ev.argument));
1096 case LFUN_BOOKMARK_GOTO:
1097 restorePosition(strToUnsignedInt(ev.argument));
1100 case LFUN_REF_GOTO: {
1101 string label = ev.argument;
1102 if (label.empty()) {
1104 static_cast<InsetRef*>(getInsetByCode(InsetOld::REF_CODE));
1106 label = inset->getContents();
1112 bv_->gotoLabel(label);
1116 // --- accented characters ---------------------------
1119 case LFUN_CIRCUMFLEX:
1129 case LFUN_SPECIAL_CARON:
1132 case LFUN_HUNG_UMLAUT:
1135 if (ev.argument.empty()) {
1137 owner_->getLyXFunc().handleKeyFunc(ev.action);
1139 owner_->getLyXFunc().handleKeyFunc(ev.action);
1140 owner_->getIntl().getTransManager()
1141 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1146 case LFUN_MATH_MACRO:
1147 case LFUN_MATH_DELIM:
1148 case LFUN_INSERT_MATRIX:
1149 case LFUN_INSERT_MATH:
1150 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1151 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1152 case LFUN_MATH_MODE: // Open or create an inlined math inset
1156 case LFUN_INSET_APPLY: {
1157 string const name = ev.getArg(0);
1159 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1161 // This works both for 'original' and 'mathed' insets.
1162 // Note that the localDispatch performs updateInset
1164 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1165 inset->localDispatch(fr);
1167 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1173 case LFUN_INSET_INSERT: {
1174 InsetOld * inset = createInset(ev);
1175 if (inset && insertInset(inset)) {
1178 string const name = ev.getArg(0);
1179 if (name == "bibitem") {
1180 // We need to do a redraw because the maximum
1181 // InsetBibitem width could have changed
1183 #warning check whether the update() is needed at all
1192 case LFUN_FLOAT_LIST:
1193 if (tclass.floats().typeExist(ev.argument)) {
1194 InsetOld * inset = new InsetFloatList(ev.argument);
1195 if (!insertInset(inset, tclass.defaultLayoutName()))
1198 lyxerr << "Non-existent float type: "
1199 << ev.argument << endl;
1203 case LFUN_LAYOUT_PARAGRAPH: {
1204 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1209 params2string(*par, data);
1211 data = "show\n" + data;
1212 bv_->owner()->getDialogs().show("paragraph", data);
1216 case LFUN_PARAGRAPH_UPDATE: {
1217 if (!bv_->owner()->getDialogs().visible("paragraph"))
1219 Paragraph const & par = *bv_->getLyXText()->cursor.par();
1222 params2string(par, data);
1224 // Will the paragraph accept changes from the dialog?
1225 InsetOld * const inset = par.inInset();
1227 !(inset && inset->forceDefaultParagraphs(inset));
1229 data = "update " + tostr(accept) + '\n' + data;
1230 bv_->owner()->getDialogs().update("paragraph", data);
1234 case LFUN_PARAGRAPH_APPLY:
1235 setParagraphParams(*bv_, ev.argument);
1238 case LFUN_THESAURUS_ENTRY:
1240 string arg = ev.argument;
1243 arg = bv_->getLyXText()->selectionAsString(buffer_,
1247 if (arg.size() > 100 || arg.empty()) {
1248 // Get word or selection
1249 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1250 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1251 // FIXME: where is getLyXText()->unselect(bv_) ?
1255 bv_->owner()->getDialogs().show("thesaurus", arg);
1259 case LFUN_TRACK_CHANGES:
1263 case LFUN_MERGE_CHANGES:
1264 owner_->getDialogs().show("changes");
1267 case LFUN_ACCEPT_ALL_CHANGES: {
1268 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1269 #warning FIXME changes
1270 //moveCursorUpdate(false);
1272 while (lyx::find::findNextChange(bv_))
1273 bv_->getLyXText()->acceptChange();
1279 case LFUN_REJECT_ALL_CHANGES: {
1280 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1281 #warning FIXME changes
1282 //moveCursorUpdate(false);
1284 while (lyx::find::findNextChange(bv_))
1285 bv_->getLyXText()->rejectChange();
1291 case LFUN_ACCEPT_CHANGE: {
1292 bv_->getLyXText()->acceptChange();
1297 case LFUN_REJECT_CHANGE: {
1298 bv_->getLyXText()->rejectChange();
1303 case LFUN_UNKNOWN_ACTION:
1304 ev.errorMessage(N_("Unknown function!"));
1308 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1315 bool BufferView::Pimpl::insertInset(InsetOld * inset, string const & lout)
1317 // if we are in a locking inset we should try to insert the
1318 // inset there otherwise this is a illegal function now
1319 if (bv_->theLockingInset()) {
1320 if (bv_->theLockingInset()->insetAllowed(inset))
1321 return bv_->theLockingInset()->insertInset(bv_, inset);
1325 // not quite sure if we want this...
1326 recordUndo(bv_, Undo::ATOMIC);
1329 beforeChange(bv_->text);
1330 if (!lout.empty()) {
1331 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1333 if (!bv_->text->cursor.par()->empty()) {
1334 bv_->text->cursorLeft(bv_);
1335 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1339 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1340 bool hasLayout = tclass.hasLayout(lres);
1341 string lay = tclass.defaultLayoutName();
1343 if (hasLayout != false) {
1347 // layout not fount using default
1348 lay = tclass.defaultLayoutName();
1351 bv_->text->setLayout(lay);
1353 bv_->text->setParagraph(0, 0,
1355 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1362 bv_->text->insertInset(inset);
1370 void BufferView::Pimpl::updateInset()
1375 // this should not be needed, but it is...
1376 bv_->text->redoParagraph(bv_->text->cursor.par());