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
33 #include "lastfiles.h"
34 #include "paragraph.h"
35 #include "ParagraphParameters.h"
36 #include "TextCache.h"
37 #include "undo_funcs.h"
39 #include "insets/insetfloatlist.h"
40 #include "insets/insetgraphics.h"
41 #include "insets/insetinclude.h"
42 #include "insets/insetref.h"
43 #include "insets/insettext.h"
45 #include "frontends/Alert.h"
46 #include "frontends/Dialogs.h"
47 #include "frontends/FileDialog.h"
48 #include "frontends/LyXView.h"
49 #include "frontends/LyXScreenFactory.h"
50 #include "frontends/mouse_state.h"
51 #include "frontends/screen.h"
52 #include "frontends/WorkArea.h"
53 #include "frontends/WorkAreaFactory.h"
55 #include "mathed/formulabase.h"
57 #include "graphics/Previews.h"
59 #include "support/LAssert.h"
60 #include "support/tostr.h"
61 #include "support/filetools.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");
194 // file already open?
195 if (bufferlist.exists(s)) {
196 string const file = MakeDisplayPath(s, 20);
197 string text = bformat(_("The document %1$s is already "
198 "loaded.\n\nDo you want to revert "
199 "to the saved version?"), file);
200 int const ret = Alert::prompt(_("Revert to saved document?"),
201 text, 0, 1, _("&Revert"), _("&Switch to document"));
204 buffer(bufferlist.getBuffer(s));
207 // FIXME: should be LFUN_REVERT
208 if (!bufferlist.close(bufferlist.getBuffer(s), false))
210 // Fall through to new load. (Asger)
213 Buffer * b = bufferlist.newBuffer(s);
217 if (! ::loadLyXFile(b, s)) {
218 bufferlist.release(b);
219 string text = bformat(_("The document %1$s does not yet "
220 "exist.\n\nDo you want to create "
221 "a new document?"), s);
222 int const ret = Alert::prompt(_("Create new document?"),
223 text, 0, 1, _("&Create"), _("Cancel"));
232 lastfiles->newFile(b->fileName());
234 bv_->showErrorList(_("Parse"));
240 WorkArea & BufferView::Pimpl::workarea() const
242 return *workarea_.get();
246 LyXScreen & BufferView::Pimpl::screen() const
248 return *screen_.get();
252 Painter & BufferView::Pimpl::painter() const
254 return workarea().getPainter();
258 void BufferView::Pimpl::buffer(Buffer * b)
260 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
264 // Put the old text into the TextCache, but
265 // only if the buffer is still loaded.
266 // Also set the owner of the test to 0
267 // bv_->text->owner(0);
268 textcache.add(buffer_, workarea().workWidth(), bv_->text);
269 if (lyxerr.debugging())
270 textcache.show(lyxerr, "BufferView::buffer");
275 // set current buffer
278 // if we're quitting lyx, don't bother updating stuff
282 // if we are closing the buffer, use the first buffer as current
284 buffer_ = bufferlist.first();
288 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
289 connectBuffer(*buffer_);
291 // If we don't have a text object for this, we make one
292 if (bv_->text == 0) {
293 resizeCurrentBuffer();
296 // FIXME: needed when ?
297 bv_->text->top_y(screen().topCursorVisible(bv_->text));
299 // Buffer-dependent dialogs should be updated or
300 // hidden. This should go here because some dialogs (eg ToC)
301 // require bv_->text.
302 owner_->getDialogs().updateBufferDependent(true);
304 lyxerr[Debug::INFO] << " No Buffer!" << endl;
305 owner_->getDialogs().hideBufferDependent();
307 // Also remove all remaining text's from the testcache.
308 // (there should not be any!) (if there is any it is a
310 if (lyxerr.debugging())
311 textcache.show(lyxerr, "buffer delete all");
317 owner_->updateMenubar();
318 owner_->updateToolbar();
319 owner_->updateLayoutChoice();
320 owner_->updateWindowTitle();
323 // Don't forget to update the Layout
324 string const layoutname =
325 bv_->text->cursor.par()->layout()->name();
326 owner_->setLayout(layoutname);
329 if (grfx::Previews::activated() && buffer_)
330 grfx::Previews::get().generateBufferPreviews(*buffer_);
334 bool BufferView::Pimpl::fitCursor()
338 if (bv_->theLockingInset()) {
339 bv_->theLockingInset()->fitInsetCursor(bv_);
342 ret = screen().fitCursor(bv_->text, bv_);
345 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
347 // We need to always update, in case we did a
348 // paste and we stayed anchored to a row, but
349 // the actual height of the doc changed ...
355 void BufferView::Pimpl::redoCurrentBuffer()
357 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
358 if (buffer_ && bv_->text) {
359 resizeCurrentBuffer();
361 owner_->updateLayoutChoice();
367 int BufferView::Pimpl::resizeCurrentBuffer()
369 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
371 ParagraphList::iterator par;
372 ParagraphList::iterator selstartpar;
373 ParagraphList::iterator selendpar;
374 UpdatableInset * the_locking_inset = 0;
377 pos_type selstartpos = 0;
378 pos_type selendpos = 0;
379 bool selection = false;
380 bool mark_set = false;
384 owner_->message(_("Formatting document..."));
387 par = bv_->text->cursor.par();
388 pos = bv_->text->cursor.pos();
389 selstartpar = bv_->text->selection.start.par();
390 selstartpos = bv_->text->selection.start.pos();
391 selendpar = bv_->text->selection.end.par();
392 selendpos = bv_->text->selection.end.pos();
393 selection = bv_->text->selection.set();
394 mark_set = bv_->text->selection.mark();
395 the_locking_inset = bv_->theLockingInset();
397 bv_->text->fullRebreak();
400 lyxerr << "text not available!\n";
401 // See if we have a text in TextCache that fits
402 // the new buffer_ with the correct width.
403 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
405 lyxerr << "text in cache!\n";
406 if (lyxerr.debugging()) {
407 lyxerr << "Found a LyXText that fits:\n";
408 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
410 // Set the owner of the newly found text
411 // bv_->text->owner(bv_);
412 if (lyxerr.debugging())
413 textcache.show(lyxerr, "resizeCurrentBuffer");
417 lyxerr << "no text in cache!\n";
418 bv_->text = new LyXText(bv_);
420 bv_->text->init(bv_);
423 par = bv_->text->ownerParagraphs().end();
424 selstartpar = bv_->text->ownerParagraphs().end();
425 selendpar = bv_->text->ownerParagraphs().end();
428 if (par != bv_->text->ownerParagraphs().end()) {
429 bv_->text->selection.set(true);
430 // At this point just to avoid the Delete-Empty-Paragraph-
431 // Mechanism when setting the cursor.
432 bv_->text->selection.mark(mark_set);
434 bv_->text->setCursor(selstartpar, selstartpos);
435 bv_->text->selection.cursor = bv_->text->cursor;
436 bv_->text->setCursor(selendpar, selendpos);
437 bv_->text->setSelection();
438 bv_->text->setCursor(par, pos);
440 bv_->text->setCursor(par, pos);
441 bv_->text->selection.cursor = bv_->text->cursor;
442 bv_->text->selection.set(false);
444 // remake the inset locking
445 bv_->theLockingInset(the_locking_inset);
448 bv_->text->top_y(screen().topCursorVisible(bv_->text));
453 // reset the "Formatting..." message
454 owner_->clearMessage();
462 void BufferView::Pimpl::repaint()
464 // Regenerate the screen.
465 screen().redraw(bv_->text, bv_);
469 void BufferView::Pimpl::updateScrollbar()
472 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
473 workarea().setScrollbarParams(0, 0, 0);
477 LyXText const & t = *bv_->text;
479 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
480 << t.top_y() << ", default height " << defaultRowHeight() << endl;
482 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
486 void BufferView::Pimpl::scrollDocView(int value)
488 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
493 screen().hideCursor();
495 screen().draw(bv_->text, bv_, value);
497 if (!lyxrc.cursor_follows_scrollbar)
500 LyXText * vbt = bv_->text;
502 int const height = defaultRowHeight();
503 int const first = static_cast<int>((bv_->text->top_y() + height));
504 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
506 if (vbt->cursor.y() < first)
507 vbt->setCursorFromCoordinates(0, first);
508 else if (vbt->cursor.y() > last)
509 vbt->setCursorFromCoordinates(0, last);
511 owner_->updateLayoutChoice();
515 void BufferView::Pimpl::scroll(int lines)
521 LyXText const * t = bv_->text;
522 int const line_height = defaultRowHeight();
524 // The new absolute coordinate
525 int new_top_y = t->top_y() + lines * line_height;
527 // Restrict to a valid value
528 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
529 new_top_y = std::max(0, new_top_y);
531 scrollDocView(new_top_y);
533 // Update the scrollbar.
534 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
538 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
539 key_modifier::state state)
541 bv_->owner()->getLyXFunc().processKeySym(key, state);
543 /* This is perhaps a bit of a hack. When we move
544 * around, or type, it's nice to be able to see
545 * the cursor immediately after the keypress. So
546 * we reset the toggle timeout and force the visibility
547 * of the cursor. Note we cannot do this inside
548 * dispatch() itself, because that's called recursively.
551 cursor_timeout.restart();
552 screen().showCursor(*bv_);
557 void BufferView::Pimpl::selectionRequested()
564 LyXText * text = bv_->getLyXText();
566 if (text->selection.set() &&
567 (!bv_->text->xsel_cache.set() ||
568 text->selection.start != bv_->text->xsel_cache.start ||
569 text->selection.end != bv_->text->xsel_cache.end))
571 bv_->text->xsel_cache = text->selection;
572 sel = text->selectionAsString(bv_->buffer(), false);
573 } else if (!text->selection.set()) {
575 bv_->text->xsel_cache.set(false);
578 workarea().putClipboard(sel);
583 void BufferView::Pimpl::selectionLost()
586 screen().hideCursor();
588 bv_->getLyXText()->clearSelection();
589 bv_->text->xsel_cache.set(false);
594 void BufferView::Pimpl::workAreaResize()
596 static int work_area_width;
597 static int work_area_height;
599 bool const widthChange = workarea().workWidth() != work_area_width;
600 bool const heightChange = workarea().workHeight() != work_area_height;
602 // update from work area
603 work_area_width = workarea().workWidth();
604 work_area_height = workarea().workHeight();
608 // The visible LyXView need a resize
609 resizeCurrentBuffer();
611 // Remove all texts from the textcache
612 // This is not _really_ what we want to do. What
613 // we really want to do is to delete in textcache
614 // that does not have a BufferView with matching
615 // width, but as long as we have only one BufferView
616 // deleting all gives the same result.
617 if (lyxerr.debugging())
618 textcache.show(lyxerr, "Expose delete all");
620 // FIXME: this is already done in resizeCurrentBuffer() ??
622 } else if (heightChange) {
623 // fitCursor() ensures we don't jump back
624 // to the start of the document on vertical
630 if (widthChange || heightChange) {
634 // always make sure that the scrollbar is sane.
636 owner_->updateLayoutChoice();
641 void BufferView::Pimpl::update()
643 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
644 screen().update(*bv_);
645 bv_->text->clearPaint();
650 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
652 if (!text->selection.set() && (f & SELECT)) {
653 text->selection.cursor = text->cursor;
656 text->partialRebreak();
658 if (text->inset_owner) {
659 text->inset_owner->setUpdateStatus(InsetText::NONE);
660 updateInset(text->inset_owner);
667 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
669 LyXText * text = bv_->text;
671 if (!text->selection.set() && (f & SELECT)) {
672 text->selection.cursor = text->cursor;
675 text->partialRebreak();
677 if (text->inset_owner) {
678 text->inset_owner->setUpdateStatus(InsetText::NONE);
679 updateInset(text->inset_owner);
686 // Callback for cursor timer
687 void BufferView::Pimpl::cursorToggle()
690 cursor_timeout.restart();
694 screen().toggleCursor(*bv_);
696 cursor_timeout.restart();
700 bool BufferView::Pimpl::available() const
702 if (buffer_ && bv_->text)
708 Change const BufferView::Pimpl::getCurrentChange()
710 if (!bv_->buffer()->params.tracking_changes)
711 return Change(Change::UNCHANGED);
713 LyXText * t(bv_->getLyXText());
715 if (!t->selection.set())
716 return Change(Change::UNCHANGED);
718 LyXCursor const & cur(t->selection.start);
719 return cur.par()->lookupChangeFull(cur.pos());
723 void BufferView::Pimpl::beforeChange(LyXText * text)
726 text->clearSelection();
730 void BufferView::Pimpl::savePosition(unsigned int i)
732 if (i >= saved_positions_num)
734 saved_positions[i] = Position(buffer_->fileName(),
735 bv_->text->cursor.par()->id(),
736 bv_->text->cursor.pos());
738 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
742 void BufferView::Pimpl::restorePosition(unsigned int i)
744 if (i >= saved_positions_num)
747 string const fname = saved_positions[i].filename;
749 beforeChange(bv_->text);
751 if (fname != buffer_->fileName()) {
753 if (bufferlist.exists(fname))
754 b = bufferlist.getBuffer(fname);
756 b = bufferlist.newBuffer(fname);
757 ::loadLyXFile(b, fname); // don't ask, just load it
763 ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
764 if (par == buffer_->par_iterator_end())
767 bv_->text->setCursor(par.pit(),
768 min(par->size(), saved_positions[i].par_pos));
770 update(BufferView::SELECT);
772 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
776 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
778 if (i >= saved_positions_num)
781 return !saved_positions[i].filename.empty();
785 void BufferView::Pimpl::switchKeyMap()
787 if (!lyxrc.rtl_support)
790 LyXText * text = bv_->getLyXText();
791 if (text->real_current_font.isRightToLeft()
792 && !(bv_->theLockingInset()
793 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
795 if (owner_->getIntl().keymap == Intl::PRIMARY)
796 owner_->getIntl().KeyMapSec();
798 if (owner_->getIntl().keymap == Intl::SECONDARY)
799 owner_->getIntl().KeyMapPrim();
804 void BufferView::Pimpl::insetUnlock()
806 if (bv_->theLockingInset()) {
807 bv_->theLockingInset()->insetUnlock(bv_);
808 bv_->theLockingInset(0);
814 void BufferView::Pimpl::toggleSelection(bool b)
816 if (bv_->theLockingInset())
817 bv_->theLockingInset()->toggleSelection(bv_, b);
818 screen().toggleSelection(bv_->text, bv_, b);
822 void BufferView::Pimpl::toggleToggle()
824 screen().toggleToggle(bv_->text, bv_);
828 void BufferView::Pimpl::center()
830 LyXText * t = bv_->text;
833 int const half_height = workarea().workHeight() / 2;
836 if (t->cursor.y() > half_height) {
837 new_y = t->cursor.y() - half_height;
840 // FIXME: look at this comment again ...
842 // FIXME: can we do this w/o calling screen directly ?
843 // This updates top_y() but means the fitCursor() call
844 // from the update(FITCUR) doesn't realise that we might
845 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
846 // the scrollbar to be updated as it should, so we have
847 // to do it manually. Any operation that does a center()
848 // and also might have moved top_y() must make sure to call
849 // updateScrollbar() currently. Never mind that this is a
850 // pretty obfuscated way of updating t->top_y()
851 screen().draw(t, bv_, new_y);
853 update(BufferView::SELECT);
857 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
859 workarea().putClipboard(stuff);
864 * Dispatch functions for actions which can be valid for BufferView->text
865 * and/or InsetText->text!!!
869 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
872 LyXCursor cursor = bv_->getLyXText()->cursor;
873 Buffer::inset_iterator it =
874 find_if(Buffer::inset_iterator(
875 cursor.par(), cursor.pos()),
876 buffer_->inset_iterator_end(),
877 lyx::compare_memfun(&Inset::lyxCode, code));
878 return it != buffer_->inset_iterator_end() ? (*it) : 0;
880 // Ok, this is a little bit too brute force but it
881 // should work for now. Better infrastructure is comming. (Lgb)
883 Buffer * b = bv_->buffer();
884 LyXCursor cursor = bv_->getLyXText()->cursor;
886 Buffer::inset_iterator beg = b->inset_iterator_begin();
887 Buffer::inset_iterator end = b->inset_iterator_end();
889 bool cursor_par_seen = false;
891 for (; beg != end; ++beg) {
892 if (beg.getPar() == cursor.par()) {
893 cursor_par_seen = true;
895 if (cursor_par_seen) {
896 if (beg.getPar() == cursor.par()
897 && beg.getPos() >= cursor.pos()) {
899 } else if (beg.getPar() != cursor.par()) {
906 // Now find the first inset that matches code.
907 for (; beg != end; ++beg) {
908 if (beg->lyxCode() == code) {
918 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
920 string filename = filen;
922 if (filename.empty()) {
923 // Launch a file browser
924 string initpath = lyxrc.document_path;
927 string const trypath = owner_->buffer()->filePath();
928 // If directory is writeable, use this as default.
929 if (IsDirWriteable(trypath))
933 FileDialog fileDlg(_("Select LyX document to insert"),
935 make_pair(string(_("Documents|#o#O")),
936 string(lyxrc.document_path)),
937 make_pair(string(_("Examples|#E#e")),
938 string(AddPath(system_lyxdir, "examples"))));
940 FileDialog::Result result =
941 fileDlg.open(initpath,
942 _("*.lyx| LyX Documents (*.lyx)"));
944 if (result.first == FileDialog::Later)
947 filename = result.second;
949 // check selected filename
950 if (filename.empty()) {
951 owner_->message(_("Canceled."));
956 // get absolute path of file and add ".lyx" to the filename if
958 filename = FileSearch(string(), filename, "lyx");
960 string const disp_fn = MakeDisplayPath(filename);
961 owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
962 if (bv_->insertLyXFile(filename))
963 owner_->message(bformat(_("Document %1$s inserted."),
966 owner_->message(bformat(_("Could not insert document %1$s"),
969 #warning remove this if update() is gone
970 bv_->text->fullRebreak();
974 void BufferView::Pimpl::trackChanges()
976 Buffer * buf(bv_->buffer());
977 bool const tracking(buf->params.tracking_changes);
980 ParIterator const end = buf->par_iterator_end();
981 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
983 buf->params.tracking_changes = true;
985 // we cannot allow undos beyond the freeze point
986 buf->undostack.clear();
988 update(BufferView::SELECT);
989 bv_->text->setCursor(buf->paragraphs.begin(), 0);
990 #warning changes FIXME
991 //moveCursorUpdate(false);
993 bool found = lyxfind::findNextChange(bv_);
995 owner_->getDialogs().show("changes");
999 ParIterator const end = buf->par_iterator_end();
1000 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
1001 it->untrackChanges();
1002 buf->params.tracking_changes = false;
1005 buf->redostack.clear();
1009 // Doesn't go through lyxfunc, so we need to update the
1010 // layout choice etc. ourselves
1011 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
1013 // e.g. Qt mouse press when no buffer
1017 screen().hideCursor();
1019 // Make sure that the cached BufferView is correct.
1020 FuncRequest ev = ev_in;
1023 bool const res = dispatch(ev);
1025 // see workAreaKeyPress
1026 cursor_timeout.restart();
1027 screen().showCursor(*bv_);
1029 // FIXME: we should skip these when selecting
1030 bv_->owner()->updateLayoutChoice();
1031 bv_->owner()->updateToolbar();
1034 // slight hack: this is only called currently when
1035 // we clicked somewhere, so we force through the display
1036 // of the new status here.
1037 bv_->owner()->clearMessage();
1043 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
1045 // Make sure that the cached BufferView is correct.
1046 FuncRequest ev = ev_in;
1049 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1050 << " action[" << ev.action << ']'
1051 << " arg[" << ev.argument << ']'
1052 << " x[" << ev.x << ']'
1053 << " y[" << ev.y << ']'
1054 << " button[" << ev.button() << ']'
1057 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1059 switch (ev.action) {
1061 case LFUN_SCROLL_INSET:
1062 // this is not handled here as this function is only active
1063 // if we have a locking_inset and that one is (or contains)
1067 case LFUN_FILE_INSERT:
1068 MenuInsertLyXFile(ev.argument);
1071 case LFUN_FILE_INSERT_ASCII_PARA:
1072 InsertAsciiFile(bv_, ev.argument, true);
1075 case LFUN_FILE_INSERT_ASCII:
1076 InsertAsciiFile(bv_, ev.argument, false);
1080 lang(bv_, ev.argument);
1082 owner_->view_state_changed();
1087 owner_->view_state_changed();
1092 owner_->view_state_changed();
1097 owner_->view_state_changed();
1102 owner_->view_state_changed();
1107 owner_->view_state_changed();
1112 owner_->view_state_changed();
1117 owner_->view_state_changed();
1120 case LFUN_UNDERLINE:
1122 owner_->view_state_changed();
1125 case LFUN_FONT_SIZE:
1126 fontSize(bv_, ev.argument);
1127 owner_->view_state_changed();
1130 case LFUN_FONT_STATE:
1131 owner_->getLyXFunc().setMessage(currentState(bv_));
1134 case LFUN_INSERT_LABEL: {
1135 // Try and generate a valid label
1136 string const contents = ev.argument.empty() ?
1137 getPossibleLabel(*bv_) : ev.argument;
1138 InsetCommandParams icp("label", contents);
1139 string data = InsetCommandMailer::params2string("label", icp);
1140 owner_->getDialogs().show("label", data, 0);
1144 case LFUN_BOOKMARK_SAVE:
1145 savePosition(strToUnsignedInt(ev.argument));
1148 case LFUN_BOOKMARK_GOTO:
1149 restorePosition(strToUnsignedInt(ev.argument));
1152 case LFUN_REF_GOTO: {
1153 string label = ev.argument;
1154 if (label.empty()) {
1156 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1158 label = inset->getContents();
1164 bv_->gotoLabel(label);
1168 // --- accented characters ---------------------------
1171 case LFUN_CIRCUMFLEX:
1181 case LFUN_SPECIAL_CARON:
1184 case LFUN_HUNG_UMLAUT:
1187 if (ev.argument.empty()) {
1189 owner_->getLyXFunc().handleKeyFunc(ev.action);
1191 owner_->getLyXFunc().handleKeyFunc(ev.action);
1192 owner_->getIntl().getTransManager()
1193 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1194 update(bv_->getLyXText(), BufferView::SELECT);
1198 case LFUN_MATH_MACRO:
1199 case LFUN_MATH_DELIM:
1200 case LFUN_INSERT_MATRIX:
1201 case LFUN_INSERT_MATH:
1202 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1203 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1204 case LFUN_MATH_MODE: // Open or create an inlined math inset
1208 case LFUN_INSET_APPLY: {
1209 string const name = ev.getArg(0);
1211 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1213 // This works both for 'original' and 'mathed' insets.
1214 // Note that the localDispatch performs updateInset
1216 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1217 inset->localDispatch(fr);
1219 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1225 case LFUN_INSET_INSERT: {
1226 Inset * inset = createInset(ev);
1227 if (inset && insertInset(inset)) {
1230 string const name = ev.getArg(0);
1231 if (name == "bibitem") {
1232 // We need to do a redraw because the maximum
1233 // InsetBibitem width could have changed
1234 #warning please check you mean repaint() not update(),
1235 #warning and whether the repaint() is needed at all
1245 case LFUN_FLOAT_LIST:
1246 if (tclass.floats().typeExist(ev.argument)) {
1247 Inset * inset = new InsetFloatList(ev.argument);
1248 if (!insertInset(inset, tclass.defaultLayoutName()))
1251 lyxerr << "Non-existent float type: "
1252 << ev.argument << endl;
1256 case LFUN_LAYOUT_PARAGRAPH: {
1257 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1262 params2string(*par, data);
1264 data = "show\n" + data;
1265 bv_->owner()->getDialogs().show("paragraph", data);
1269 case LFUN_PARAGRAPH_UPDATE: {
1270 if (!bv_->owner()->getDialogs().visible("paragraph"))
1272 Paragraph const * par = &*bv_->getLyXText()->cursor.par();
1277 params2string(*par, data);
1279 // Will the paragraph accept changes from the dialog?
1280 Inset * const inset = par->inInset();
1282 !(inset && inset->forceDefaultParagraphs(inset));
1284 data = "update " + tostr(accept) + '\n' + data;
1285 bv_->owner()->getDialogs().update("paragraph", data);
1289 case LFUN_PARAGRAPH_APPLY:
1290 setParagraphParams(*bv_, ev.argument);
1293 case LFUN_THESAURUS_ENTRY:
1295 string arg = ev.argument;
1298 arg = bv_->getLyXText()->selectionAsString(buffer_,
1302 if (arg.size() > 100 || arg.empty()) {
1303 // Get word or selection
1304 bv_->getLyXText()->selectWordWhenUnderCursor(lyx::WHOLE_WORD);
1305 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1306 // FIXME: where is getLyXText()->unselect(bv_) ?
1310 bv_->owner()->getDialogs().show("thesaurus", arg);
1314 case LFUN_TRACK_CHANGES:
1318 case LFUN_MERGE_CHANGES:
1319 owner_->getDialogs().show("changes");
1322 case LFUN_ACCEPT_ALL_CHANGES: {
1323 update(BufferView::SELECT);
1324 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1325 #warning FIXME changes
1326 //moveCursorUpdate(false);
1328 while (lyxfind::findNextChange(bv_)) {
1329 bv_->getLyXText()->acceptChange();
1331 update(BufferView::SELECT);
1335 case LFUN_REJECT_ALL_CHANGES: {
1336 update(BufferView::SELECT);
1337 bv_->text->setCursor(bv_->buffer()->paragraphs.begin(), 0);
1338 #warning FIXME changes
1339 //moveCursorUpdate(false);
1341 while (lyxfind::findNextChange(bv_)) {
1342 bv_->getLyXText()->rejectChange();
1344 update(BufferView::SELECT);
1348 case LFUN_ACCEPT_CHANGE: {
1349 bv_->getLyXText()->acceptChange();
1350 update(BufferView::SELECT);
1354 case LFUN_REJECT_CHANGE: {
1355 bv_->getLyXText()->rejectChange();
1356 update(BufferView::SELECT);
1360 case LFUN_UNKNOWN_ACTION:
1361 ev.errorMessage(N_("Unknown function!"));
1365 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1372 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1374 // if we are in a locking inset we should try to insert the
1375 // inset there otherwise this is a illegal function now
1376 if (bv_->theLockingInset()) {
1377 if (bv_->theLockingInset()->insetAllowed(inset))
1378 return bv_->theLockingInset()->insertInset(bv_, inset);
1382 // not quite sure if we want this...
1383 setCursorParUndo(bv_);
1386 beforeChange(bv_->text);
1387 if (!lout.empty()) {
1388 update(BufferView::SELECT);
1389 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1390 update(BufferView::SELECT);
1392 if (!bv_->text->cursor.par()->empty()) {
1393 bv_->text->cursorLeft(bv_);
1395 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1396 update(BufferView::SELECT);
1400 LyXTextClass const & tclass =
1401 buffer_->params.getLyXTextClass();
1402 bool hasLayout = tclass.hasLayout(lres);
1403 string lay = tclass.defaultLayoutName();
1405 if (hasLayout != false) {
1409 // layout not fount using default
1410 lay = tclass.defaultLayoutName();
1413 bv_->text->setLayout(lay);
1415 bv_->text->setParagraph(0, 0,
1417 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1422 update(BufferView::SELECT);
1425 bv_->text->insertInset(inset);
1426 update(BufferView::SELECT);
1433 void BufferView::Pimpl::updateInset(Inset * inset)
1435 if (!inset || !available())
1438 // first check for locking insets
1439 if (bv_->theLockingInset()) {
1440 if (bv_->theLockingInset() == inset) {
1441 if (bv_->text->updateInset(inset)) {
1446 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1447 if (bv_->text->updateInset(bv_->theLockingInset())) {
1455 // then check if the inset is a top_level inset (has no owner)
1456 // if yes do the update as always otherwise we have to update the
1457 // toplevel inset where this inset is inside
1458 Inset * tl_inset = inset;
1459 while (tl_inset->owner())
1460 tl_inset = tl_inset->owner();
1461 if (tl_inset == inset) {
1462 update(BufferView::UPDATE);
1463 if (bv_->text->updateInset(inset)) {
1464 update(BufferView::SELECT);
1467 } else if (static_cast<UpdatableInset *>(tl_inset)
1468 ->updateInsetInInset(bv_, inset))
1470 if (bv_->text->updateInset(tl_inset)) {