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"
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 "support/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::workAreaDispatch, this, _1));
115 kpresscon = workarea().workAreaKeyPress
116 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
117 selectioncon = workarea().selectionRequested
118 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
119 lostcon = workarea().selectionLost
120 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
122 timecon = cursor_timeout.timeout
123 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
124 cursor_timeout.start();
125 saved_positions.resize(saved_positions_num);
129 WorkArea & BufferView::Pimpl::workarea() const
131 return *workarea_.get();
135 LyXScreen & BufferView::Pimpl::screen() const
137 return *screen_.get();
141 Painter & BufferView::Pimpl::painter() const
143 return workarea().getPainter();
147 void BufferView::Pimpl::buffer(Buffer * b)
149 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
152 buffer_->delUser(bv_);
154 // Put the old text into the TextCache, but
155 // only if the buffer is still loaded.
156 // Also set the owner of the test to 0
157 // bv_->text->owner(0);
158 textcache.add(buffer_, workarea().workWidth(), bv_->text);
159 if (lyxerr.debugging())
160 textcache.show(lyxerr, "BufferView::buffer");
165 // set current buffer
168 // if we're quitting lyx, don't bother updating stuff
172 // if we are closing the buffer, use the first buffer as current
174 buffer_ = bufferlist.first();
178 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
179 buffer_->addUser(bv_);
181 // If we don't have a text object for this, we make one
182 if (bv_->text == 0) {
183 resizeCurrentBuffer();
186 // FIXME: needed when ?
187 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
189 // Buffer-dependent dialogs should be updated or
190 // hidden. This should go here because some dialogs (eg ToC)
191 // require bv_->text.
192 owner_->getDialogs().updateBufferDependent(true);
194 lyxerr[Debug::INFO] << " No Buffer!" << endl;
195 owner_->getDialogs().hideBufferDependent();
197 // Also remove all remaining text's from the testcache.
198 // (there should not be any!) (if there is any it is a
200 if (lyxerr.debugging())
201 textcache.show(lyxerr, "buffer delete all");
207 owner_->updateMenubar();
208 owner_->updateToolbar();
209 owner_->updateLayoutChoice();
210 owner_->updateWindowTitle();
213 // Don't forget to update the Layout
214 string const layoutname =
215 bv_->text->cursor.par()->layout()->name();
216 owner_->setLayout(layoutname);
219 if (grfx::Previews::activated() && buffer_)
220 grfx::Previews::get().generateBufferPreviews(*buffer_);
224 bool BufferView::Pimpl::fitCursor()
228 if (bv_->theLockingInset()) {
229 bv_->theLockingInset()->fitInsetCursor(bv_);
232 ret = screen().fitCursor(bv_->text, bv_);
235 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
237 // We need to always update, in case we did a
238 // paste and we stayed anchored to a row, but
239 // the actual height of the doc changed ...
245 void BufferView::Pimpl::redoCurrentBuffer()
247 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
248 if (buffer_ && bv_->text) {
249 resizeCurrentBuffer();
251 owner_->updateLayoutChoice();
257 int BufferView::Pimpl::resizeCurrentBuffer()
259 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
262 Paragraph * selstartpar = 0;
263 Paragraph * selendpar = 0;
264 UpdatableInset * the_locking_inset = 0;
267 pos_type selstartpos = 0;
268 pos_type selendpos = 0;
269 bool selection = false;
270 bool mark_set = false;
274 owner_->message(_("Formatting document..."));
277 par = bv_->text->cursor.par();
278 pos = bv_->text->cursor.pos();
279 selstartpar = bv_->text->selection.start.par();
280 selstartpos = bv_->text->selection.start.pos();
281 selendpar = bv_->text->selection.end.par();
282 selendpos = bv_->text->selection.end.pos();
283 selection = bv_->text->selection.set();
284 mark_set = bv_->text->selection.mark();
285 the_locking_inset = bv_->theLockingInset();
286 buffer_->resizeInsets(bv_);
287 // I don't think the delete and new are necessary here we just could
288 // call only init! (Jug 20020419)
290 bv_->text = new LyXText(bv_);
291 bv_->text->init(bv_);
293 // See if we have a text in TextCache that fits
294 // the new buffer_ with the correct width.
295 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
297 if (lyxerr.debugging()) {
298 lyxerr << "Found a LyXText that fits:\n";
299 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
301 // Set the owner of the newly found text
302 // bv_->text->owner(bv_);
303 if (lyxerr.debugging())
304 textcache.show(lyxerr, "resizeCurrentBuffer");
306 buffer_->resizeInsets(bv_);
308 bv_->text = new LyXText(bv_);
309 bv_->text->init(bv_);
310 //buffer_->resizeInsets(bv_);
315 bv_->text->selection.set(true);
316 // At this point just to avoid the Delete-Empty-Paragraph-
317 // Mechanism when setting the cursor.
318 bv_->text->selection.mark(mark_set);
320 bv_->text->setCursor(selstartpar, selstartpos);
321 bv_->text->selection.cursor = bv_->text->cursor;
322 bv_->text->setCursor(selendpar, selendpos);
323 bv_->text->setSelection();
324 bv_->text->setCursor(par, pos);
326 bv_->text->setCursor(par, pos);
327 bv_->text->selection.cursor = bv_->text->cursor;
328 bv_->text->selection.set(false);
330 // remake the inset locking
331 bv_->theLockingInset(the_locking_inset);
334 bv_->text->top_y(screen().topCursorVisible(bv_->text->cursor, bv_->text->top_y()));
345 void BufferView::Pimpl::repaint()
347 // Regenerate the screen.
348 screen().redraw(bv_->text, bv_);
352 void BufferView::Pimpl::updateScrollbar()
355 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
356 workarea().setScrollbarParams(0, 0, 0);
360 LyXText const & t = *bv_->text;
362 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", top_y() "
363 << t.top_y() << ", default height " << defaultRowHeight() << endl;
365 workarea().setScrollbarParams(t.height, t.top_y(), defaultRowHeight());
369 void BufferView::Pimpl::scrollDocView(int value)
371 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
376 screen().draw(bv_->text, bv_, value);
378 if (!lyxrc.cursor_follows_scrollbar)
381 LyXText * vbt = bv_->text;
383 int const height = defaultRowHeight();
384 int const first = static_cast<int>((bv_->text->top_y() + height));
385 int const last = static_cast<int>((bv_->text->top_y() + workarea().workHeight() - height));
387 if (vbt->cursor.y() < first)
388 vbt->setCursorFromCoordinates(0, first);
389 else if (vbt->cursor.y() > last)
390 vbt->setCursorFromCoordinates(0, last);
392 owner_->updateLayoutChoice();
396 void BufferView::Pimpl::scroll(int lines)
402 LyXText const * t = bv_->text;
403 int const line_height = defaultRowHeight();
405 // The new absolute coordinate
406 int new_top_y = t->top_y() + lines * line_height;
408 // Restrict to a valid value
409 new_top_y = std::min(t->height - 4 * line_height, new_top_y);
410 new_top_y = std::max(0, new_top_y);
412 scrollDocView(new_top_y);
414 // Update the scrollbar.
415 workarea().setScrollbarParams(t->height, t->top_y(), defaultRowHeight());
419 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
420 key_modifier::state state)
422 bv_->owner()->getLyXFunc().processKeySym(key, state);
426 void BufferView::Pimpl::selectionRequested()
433 LyXText * text = bv_->getLyXText();
435 if (text->selection.set() &&
436 (!bv_->text->xsel_cache.set() ||
437 text->selection.start != bv_->text->xsel_cache.start ||
438 text->selection.end != bv_->text->xsel_cache.end))
440 bv_->text->xsel_cache = text->selection;
441 sel = text->selectionAsString(bv_->buffer(), false);
442 } else if (!text->selection.set()) {
444 bv_->text->xsel_cache.set(false);
447 workarea().putClipboard(sel);
452 void BufferView::Pimpl::selectionLost()
457 bv_->getLyXText()->clearSelection();
459 bv_->text->xsel_cache.set(false);
464 void BufferView::Pimpl::workAreaResize()
466 static int work_area_width;
467 static int work_area_height;
469 bool const widthChange = workarea().workWidth() != work_area_width;
470 bool const heightChange = workarea().workHeight() != work_area_height;
472 // update from work area
473 work_area_width = workarea().workWidth();
474 work_area_height = workarea().workHeight();
478 // The visible LyXView need a resize
479 resizeCurrentBuffer();
481 // Remove all texts from the textcache
482 // This is not _really_ what we want to do. What
483 // we really want to do is to delete in textcache
484 // that does not have a BufferView with matching
485 // width, but as long as we have only one BufferView
486 // deleting all gives the same result.
487 if (lyxerr.debugging())
488 textcache.show(lyxerr, "Expose delete all");
490 // FIXME: this is already done in resizeCurrentBuffer() ??
491 buffer_->resizeInsets(bv_);
492 } else if (heightChange) {
493 // fitCursor() ensures we don't jump back
494 // to the start of the document on vertical
500 if (widthChange || heightChange) {
504 // always make sure that the scrollbar is sane.
506 owner_->updateLayoutChoice();
511 void BufferView::Pimpl::update()
513 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
514 screen().update(*bv_);
515 bv_->text->clearPaint();
520 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
522 if (!text->selection.set() && (f & SELECT)) {
523 text->selection.cursor = text->cursor;
528 if (text->inset_owner) {
529 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
530 updateInset(text->inset_owner);
537 void BufferView::Pimpl::update(BufferView::UpdateCodes f)
539 LyXText * text = bv_->text;
541 if (!text->selection.set() && (f & SELECT)) {
542 text->selection.cursor = text->cursor;
547 if (text->inset_owner) {
548 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
549 updateInset(text->inset_owner);
556 // Callback for cursor timer
557 void BufferView::Pimpl::cursorToggle()
560 cursor_timeout.restart();
564 if (!bv_->theLockingInset()) {
565 screen().cursorToggle(bv_);
567 bv_->theLockingInset()->toggleInsetCursor(bv_);
570 cursor_timeout.restart();
574 bool BufferView::Pimpl::available() const
576 if (buffer_ && bv_->text)
582 Change const BufferView::Pimpl::getCurrentChange()
584 if (!bv_->buffer()->params.tracking_changes)
585 return Change(Change::UNCHANGED);
587 LyXText * t(bv_->getLyXText());
589 if (!t->selection.set())
590 return Change(Change::UNCHANGED);
592 LyXCursor const & cur(t->selection.start);
593 return cur.par()->lookupChangeFull(cur.pos());
597 void BufferView::Pimpl::beforeChange(LyXText * text)
600 text->clearSelection();
604 void BufferView::Pimpl::savePosition(unsigned int i)
606 if (i >= saved_positions_num)
608 saved_positions[i] = Position(buffer_->fileName(),
609 bv_->text->cursor.par()->id(),
610 bv_->text->cursor.pos());
614 str << boost::format(_("Saved bookmark %1$d")) % i;
616 str << _("Saved bookmark ") << i;
618 owner_->message(STRCONV(str.str()));
623 void BufferView::Pimpl::restorePosition(unsigned int i)
625 if (i >= saved_positions_num)
628 string const fname = saved_positions[i].filename;
630 beforeChange(bv_->text);
632 if (fname != buffer_->fileName()) {
633 Buffer * b = bufferlist.exists(fname) ?
634 bufferlist.getBuffer(fname) :
635 bufferlist.loadLyXFile(fname); // don't ask, just load it
636 if (b != 0) buffer(b);
639 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
643 bv_->text->setCursor(par,
644 min(par->size(), saved_positions[i].par_pos));
646 update(BufferView::SELECT);
650 str << boost::format(_("Moved to bookmark %1$d")) % i;
652 str << _("Moved to bookmark ") << i;
654 owner_->message(STRCONV(str.str()));
659 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
661 if (i >= saved_positions_num)
664 return !saved_positions[i].filename.empty();
668 void BufferView::Pimpl::switchKeyMap()
670 if (!lyxrc.rtl_support)
673 LyXText * text = bv_->getLyXText();
674 if (text->real_current_font.isRightToLeft()
675 && !(bv_->theLockingInset()
676 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
678 if (owner_->getIntl().keymap == Intl::PRIMARY)
679 owner_->getIntl().KeyMapSec();
681 if (owner_->getIntl().keymap == Intl::SECONDARY)
682 owner_->getIntl().KeyMapPrim();
687 void BufferView::Pimpl::insetUnlock()
689 if (bv_->theLockingInset()) {
690 bv_->theLockingInset()->insetUnlock(bv_);
691 bv_->theLockingInset(0);
697 void BufferView::Pimpl::showCursor()
699 if (bv_->theLockingInset())
700 bv_->theLockingInset()->showInsetCursor(bv_);
702 screen().showCursor(bv_->text, bv_);
706 void BufferView::Pimpl::hideCursor()
708 if (!bv_->theLockingInset())
709 screen().hideCursor();
713 void BufferView::Pimpl::toggleSelection(bool b)
715 if (bv_->theLockingInset())
716 bv_->theLockingInset()->toggleSelection(bv_, b);
717 screen().toggleSelection(bv_->text, bv_, b);
721 void BufferView::Pimpl::toggleToggle()
723 screen().toggleToggle(bv_->text, bv_);
727 void BufferView::Pimpl::center()
729 LyXText * t = bv_->text;
732 int const half_height = workarea().workHeight() / 2;
735 if (t->cursor.y() > half_height) {
736 new_y = t->cursor.y() - half_height;
739 // FIXME: look at this comment again ...
741 // FIXME: can we do this w/o calling screen directly ?
742 // This updates top_y() but means the fitCursor() call
743 // from the update(FITCUR) doesn't realise that we might
744 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
745 // the scrollbar to be updated as it should, so we have
746 // to do it manually. Any operation that does a center()
747 // and also might have moved top_y() must make sure to call
748 // updateScrollbar() currently. Never mind that this is a
749 // pretty obfuscated way of updating t->top_y()
750 screen().draw(t, bv_, new_y);
752 update(BufferView::SELECT);
756 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
758 workarea().putClipboard(stuff);
763 * Dispatch functions for actions which can be valid for BufferView->text
764 * and/or InsetText->text!!!
768 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
771 LyXCursor cursor = bv_->getLyXText()->cursor;
772 Buffer::inset_iterator it =
773 find_if(Buffer::inset_iterator(
774 cursor.par(), cursor.pos()),
775 buffer_->inset_iterator_end(),
776 lyx::compare_memfun(&Inset::lyxCode, code));
777 return it != buffer_->inset_iterator_end() ? (*it) : 0;
779 // Ok, this is a little bit too brute force but it
780 // should work for now. Better infrastructure is comming. (Lgb)
782 Buffer * b = bv_->buffer();
783 LyXCursor cursor = bv_->getLyXText()->cursor;
785 Buffer::inset_iterator beg = b->inset_iterator_begin();
786 Buffer::inset_iterator end = b->inset_iterator_end();
788 bool cursor_par_seen = false;
790 for (; beg != end; ++beg) {
791 if (beg.getPar() == cursor.par()) {
792 cursor_par_seen = true;
794 if (cursor_par_seen) {
795 if (beg.getPar() == cursor.par()
796 && beg.getPos() >= cursor.pos()) {
798 } else if (beg.getPar() != cursor.par()) {
805 // Now find the first inset that matches code.
806 for (; beg != end; ++beg) {
807 if (beg->lyxCode() == code) {
817 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
819 string filename = filen;
821 if (filename.empty()) {
822 // Launch a file browser
823 string initpath = lyxrc.document_path;
826 string const trypath = owner_->buffer()->filePath();
827 // If directory is writeable, use this as default.
828 if (IsDirWriteable(trypath))
832 FileDialog fileDlg(_("Select LyX document to insert"),
834 make_pair(string(_("Documents|#o#O")),
835 string(lyxrc.document_path)),
836 make_pair(string(_("Examples|#E#e")),
837 string(AddPath(system_lyxdir, "examples"))));
839 FileDialog::Result result =
840 fileDlg.open(initpath,
841 _("*.lyx| LyX Documents (*.lyx)"));
843 if (result.first == FileDialog::Later)
846 filename = result.second;
848 // check selected filename
849 if (filename.empty()) {
850 owner_->message(_("Canceled."));
855 // get absolute path of file and add ".lyx" to the filename if
857 filename = FileSearch(string(), filename, "lyx");
859 string const disp_fn(MakeDisplayPath(filename));
863 s1 << boost::format(_("Inserting document %1$s...")) % disp_fn;
865 s1 << _("Inserting document ") << disp_fn << _("...");
867 owner_->message(STRCONV(s1.str()));
868 bool const res = bv_->insertLyXFile(filename);
872 str << boost::format(_("Document %1$s inserted.")) % disp_fn;
874 str << _("Document ") << disp_fn << _(" inserted.");
876 owner_->message(STRCONV(str.str()));
880 str << boost::format(_("Could not insert document %1$s")) % disp_fn;
882 str << _("Could not insert document ") << disp_fn;
884 owner_->message(STRCONV(str.str()));
889 void BufferView::Pimpl::trackChanges()
891 Buffer * buf(bv_->buffer());
892 bool const tracking(buf->params.tracking_changes);
895 ParIterator const end = buf->par_iterator_end();
896 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
897 (*it)->trackChanges();
899 buf->params.tracking_changes = true;
901 // we cannot allow undos beyond the freeze point
902 buf->undostack.clear();
904 update(BufferView::SELECT);
905 bv_->text->setCursor(&(*buf->paragraphs.begin()), 0);
906 #warning changes FIXME
907 //moveCursorUpdate(false);
909 bool found = lyxfind::findNextChange(bv_);
911 owner_->getDialogs().show("changes");
915 ParIterator const end = buf->par_iterator_end();
916 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it) {
917 (*it)->untrackChanges();
919 buf->params.tracking_changes = false;
922 buf->redostack.clear();
926 // Doesn't go through lyxfunc, so we need to update the
927 // layout choice etc. ourselves
928 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & ev_in)
930 // e.g. Qt mouse press when no buffer
934 bool const res = dispatch(ev_in);
936 bv_->owner()->updateLayoutChoice();
943 bool BufferView::Pimpl::dispatch(FuncRequest const & ev_in)
945 // Make sure that the cached BufferView is correct.
946 FuncRequest ev = ev_in;
949 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
950 << " action[" << ev.action << ']'
951 << " arg[" << ev.argument << ']'
952 << " x[" << ev.x << ']'
953 << " y[" << ev.y << ']'
954 << " button[" << ev.button() << ']'
957 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
961 case LFUN_SCROLL_INSET:
962 // this is not handled here as this function is only active
963 // if we have a locking_inset and that one is (or contains)
967 case LFUN_LAYOUT_COPY:
968 bv_->copyEnvironment();
971 case LFUN_LAYOUT_PASTE:
972 bv_->pasteEnvironment();
976 case LFUN_FILE_INSERT:
977 MenuInsertLyXFile(ev.argument);
980 case LFUN_FILE_INSERT_ASCII_PARA:
981 InsertAsciiFile(bv_, ev.argument, true);
984 case LFUN_FILE_INSERT_ASCII:
985 InsertAsciiFile(bv_, ev.argument, false);
989 lang(bv_, ev.argument);
991 owner_->view_state_changed();
996 owner_->view_state_changed();
1001 owner_->view_state_changed();
1006 owner_->view_state_changed();
1011 owner_->view_state_changed();
1016 owner_->view_state_changed();
1021 owner_->view_state_changed();
1026 owner_->view_state_changed();
1029 case LFUN_UNDERLINE:
1031 owner_->view_state_changed();
1034 case LFUN_FONT_SIZE:
1035 fontSize(bv_, ev.argument);
1036 owner_->view_state_changed();
1039 case LFUN_FONT_STATE:
1040 owner_->getLyXFunc().setMessage(currentState(bv_));
1043 case LFUN_INSERT_LABEL: {
1044 // Try and generate a valid label
1045 string const contents = ev.argument.empty() ?
1046 getPossibleLabel(*bv_) : ev.argument;
1047 InsetCommandParams icp("label", contents);
1048 string data = InsetCommandMailer::params2string("label", icp);
1049 owner_->getDialogs().show("label", data, 0);
1053 case LFUN_BOOKMARK_SAVE:
1054 savePosition(strToUnsignedInt(ev.argument));
1057 case LFUN_BOOKMARK_GOTO:
1058 restorePosition(strToUnsignedInt(ev.argument));
1061 case LFUN_REF_GOTO: {
1062 string label = ev.argument;
1063 if (label.empty()) {
1065 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1067 label = inset->getContents();
1073 bv_->gotoLabel(label);
1077 // --- accented characters ---------------------------
1080 case LFUN_CIRCUMFLEX:
1090 case LFUN_SPECIAL_CARON:
1093 case LFUN_HUNG_UMLAUT:
1096 if (ev.argument.empty()) {
1098 owner_->getLyXFunc().handleKeyFunc(ev.action);
1100 owner_->getLyXFunc().handleKeyFunc(ev.action);
1101 owner_->getIntl().getTransManager()
1102 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1103 update(bv_->getLyXText(), BufferView::SELECT);
1107 case LFUN_MATH_MACRO:
1108 case LFUN_MATH_DELIM:
1109 case LFUN_INSERT_MATRIX:
1110 case LFUN_INSERT_MATH:
1111 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1112 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1113 case LFUN_MATH_MODE: // Open or create an inlined math inset
1117 case LFUN_INSET_APPLY: {
1118 string const name = ev.getArg(0);
1120 InsetBase * inset = owner_->getDialogs().getOpenInset(name);
1122 // This works both for 'original' and 'mathed' insets.
1123 // Note that the localDispatch performs updateInset
1125 FuncRequest fr(bv_, LFUN_INSET_MODIFY, ev.argument);
1126 inset->localDispatch(fr);
1128 FuncRequest fr(bv_, LFUN_INSET_INSERT, ev.argument);
1134 case LFUN_INSET_INSERT: {
1135 Inset * inset = createInset(ev);
1136 if (inset && insertInset(inset)) {
1139 string const name = ev.getArg(0);
1140 if (name == "bibitem") {
1141 // We need to do a redraw because the maximum
1142 // InsetBibitem width could have changed
1143 #warning please check you mean repaint() not update(),
1144 #warning and whether the repaint() is needed at all
1154 case LFUN_FLOAT_LIST:
1155 if (tclass.floats().typeExist(ev.argument)) {
1156 Inset * inset = new InsetFloatList(ev.argument);
1157 if (!insertInset(inset, tclass.defaultLayoutName()))
1160 lyxerr << "Non-existent float type: "
1161 << ev.argument << endl;
1165 case LFUN_LAYOUT_PARAGRAPH: {
1166 Paragraph const * par = bv_->getLyXText()->cursor.par();
1171 params2string(*par, data);
1173 data = "show\n" + data;
1174 bv_->owner()->getDialogs().show("paragraph", data);
1178 case LFUN_PARAGRAPH_UPDATE: {
1179 Paragraph const * par = bv_->getLyXText()->cursor.par();
1184 params2string(*par, data);
1186 // Will the paragraph accept changes from the dialog?
1187 Inset * const inset = par->inInset();
1189 !(inset && inset->forceDefaultParagraphs(inset));
1191 data = "update " + tostr(accept) + '\n' + data;
1192 bv_->owner()->getDialogs().update("paragraph", data);
1196 case LFUN_PARAGRAPH_APPLY:
1197 setParagraphParams(*bv_, ev.argument);
1200 case LFUN_THESAURUS_ENTRY:
1202 string arg = ev.argument;
1205 arg = bv_->getLyXText()->selectionAsString(buffer_,
1209 if (arg.size() > 100 || arg.empty()) {
1210 // Get word or selection
1211 bv_->getLyXText()->selectWordWhenUnderCursor(LyXText::WHOLE_WORD);
1212 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1213 // FIXME: where is getLyXText()->unselect(bv_) ?
1217 bv_->owner()->getDialogs().show("thesaurus", arg);
1221 case LFUN_TRACK_CHANGES:
1225 case LFUN_MERGE_CHANGES:
1226 owner_->getDialogs().show("changes");
1229 case LFUN_ACCEPT_ALL_CHANGES: {
1230 update(BufferView::SELECT);
1231 bv_->text->setCursor(&(*bv_->buffer()->paragraphs.begin()), 0);
1232 #warning FIXME changes
1233 //moveCursorUpdate(false);
1235 while (lyxfind::findNextChange(bv_)) {
1236 bv_->getLyXText()->acceptChange();
1238 update(BufferView::SELECT);
1242 case LFUN_REJECT_ALL_CHANGES: {
1243 update(BufferView::SELECT);
1244 bv_->text->setCursor(&(*bv_->buffer()->paragraphs.begin()), 0);
1245 #warning FIXME changes
1246 //moveCursorUpdate(false);
1248 while (lyxfind::findNextChange(bv_)) {
1249 bv_->getLyXText()->rejectChange();
1251 update(BufferView::SELECT);
1255 case LFUN_ACCEPT_CHANGE: {
1256 bv_->getLyXText()->acceptChange();
1257 update(BufferView::SELECT);
1261 case LFUN_REJECT_CHANGE: {
1262 bv_->getLyXText()->rejectChange();
1263 update(BufferView::SELECT);
1267 case LFUN_UNKNOWN_ACTION:
1268 ev.errorMessage(N_("Unknown function!"));
1272 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1279 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1281 // if we are in a locking inset we should try to insert the
1282 // inset there otherwise this is a illegal function now
1283 if (bv_->theLockingInset()) {
1284 if (bv_->theLockingInset()->insetAllowed(inset))
1285 return bv_->theLockingInset()->insertInset(bv_, inset);
1289 // not quite sure if we want this...
1290 setCursorParUndo(bv_);
1293 beforeChange(bv_->text);
1294 if (!lout.empty()) {
1295 update(BufferView::SELECT);
1296 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1297 update(BufferView::SELECT);
1299 if (!bv_->text->cursor.par()->empty()) {
1300 bv_->text->cursorLeft(bv_);
1302 bv_->text->breakParagraph(bv_->buffer()->paragraphs);
1303 update(BufferView::SELECT);
1307 LyXTextClass const & tclass =
1308 buffer_->params.getLyXTextClass();
1309 bool hasLayout = tclass.hasLayout(lres);
1310 string lay = tclass.defaultLayoutName();
1312 if (hasLayout != false) {
1316 // layout not fount using default
1317 lay = tclass.defaultLayoutName();
1320 bv_->text->setLayout(lay);
1322 bv_->text->setParagraph(0, 0,
1324 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1329 update(BufferView::SELECT);
1332 bv_->text->insertInset(inset);
1333 update(BufferView::SELECT);
1340 void BufferView::Pimpl::updateInset(Inset * inset)
1342 if (!inset || !available())
1345 // first check for locking insets
1346 if (bv_->theLockingInset()) {
1347 if (bv_->theLockingInset() == inset) {
1348 if (bv_->text->updateInset(inset)) {
1353 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1354 if (bv_->text->updateInset(bv_->theLockingInset())) {
1362 // then check if the inset is a top_level inset (has no owner)
1363 // if yes do the update as always otherwise we have to update the
1364 // toplevel inset where this inset is inside
1365 Inset * tl_inset = inset;
1366 while (tl_inset->owner())
1367 tl_inset = tl_inset->owner();
1369 if (tl_inset == inset) {
1370 update(BufferView::UPDATE);
1371 if (bv_->text->updateInset(inset)) {
1372 update(BufferView::SELECT);
1375 } else if (static_cast<UpdatableInset *>(tl_inset)
1376 ->updateInsetInInset(bv_, inset))
1378 if (bv_->text->updateInset(tl_inset)) {