2 * \file BufferView_pimpl.C
3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
12 #pragma implementation
15 #include "BufferView_pimpl.h"
16 #include "frontends/WorkArea.h"
17 #include "frontends/screen.h"
18 #include "frontends/LyXScreenFactory.h"
19 #include "frontends/WorkAreaFactory.h"
20 #include "frontends/Dialogs.h"
21 #include "frontends/Alert.h"
22 #include "frontends/FileDialog.h"
23 #include "frontends/mouse_state.h"
26 #include "paragraph.h"
27 #include "frontends/LyXView.h"
28 #include "commandtags.h"
31 #include "bufferview_funcs.h"
32 #include "TextCache.h"
33 #include "bufferlist.h"
36 // added for Dispatch functions
39 #include "FloatList.h"
41 #include "ParagraphParameters.h"
42 #include "undo_funcs.h"
43 #include "funcrequest.h"
46 #include "insets/insetbib.h"
47 #include "insets/insettext.h"
48 #include "insets/inseturl.h"
49 #include "insets/insetlatexaccent.h"
50 #include "insets/insettoc.h"
51 #include "insets/insetref.h"
52 #include "insets/insetparent.h"
53 #include "insets/insetindex.h"
54 #include "insets/insetinclude.h"
55 #include "insets/insetcite.h"
56 #include "insets/insetgraphics.h"
57 #include "insets/insetmarginal.h"
58 #include "insets/insetcaption.h"
59 #include "insets/insetfloatlist.h"
61 #include "mathed/formulabase.h"
63 #include "graphics/Previews.h"
65 #include "support/LAssert.h"
66 #include "support/lstrings.h"
67 #include "support/filetools.h"
69 #include <boost/bind.hpp>
70 #include <boost/signals/connection.hpp>
86 /* the selection possible is needed, that only motion events are
87 * used, where the bottom press event was on the drawing area too */
88 bool selection_possible = false;
90 extern BufferList bufferlist;
91 extern int bibitemMaxWidth(BufferView *, LyXFont const &);
96 unsigned int const saved_positions_num = 20;
98 // All the below connection objects are needed because of a bug in some
99 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
100 // to these connections we avoid a segfault upon startup, and also at exit.
103 boost::signals::connection dispatchcon;
104 boost::signals::connection timecon;
105 boost::signals::connection doccon;
106 boost::signals::connection resizecon;
107 boost::signals::connection kpresscon;
108 boost::signals::connection selectioncon;
109 boost::signals::connection lostcon;
113 * Return the on-screen dimensions of the inset at the cursor.
114 * Pre-condition: the cursor must be at an inset.
116 Box insetDimensions(BufferView * bv, LyXText const & text,
117 LyXCursor const & cursor)
119 Paragraph /*const*/ & par = *cursor.par();
120 pos_type const pos = cursor.pos();
122 lyx::Assert(par.getInset(pos));
124 Inset const & inset(*par.getInset(pos));
126 LyXFont const & font = text.getFont(bv->buffer(), &par, pos);
128 int const width = inset.width(bv, font);
129 int const inset_x = font.isVisibleRightToLeft()
130 ? (cursor.ix() - width) : cursor.ix();
133 inset_x + inset.scroll(),
135 cursor.iy() - inset.ascent(bv, font),
136 cursor.iy() + inset.descent(bv, font));
141 * check if the given co-ordinates are inside an inset at the
142 * given cursor, if one exists. If so, the inset is returned,
143 * and the co-ordinates are made relative. Otherwise, 0 is returned.
145 Inset * checkInset(BufferView * bv, LyXText const & text,
146 LyXCursor const & cursor, int & x, int & y)
148 pos_type const pos = cursor.pos();
149 Paragraph /*const*/ & par(*cursor.par());
151 if (pos >= par.size() || !par.isInset(pos)) {
155 Inset /*const*/ * inset = par.getInset(pos);
157 if (!isEditableInset(inset))
160 Box b = insetDimensions(bv, text, cursor);
162 if (!b.contained(x, y)) {
163 lyxerr[Debug::GUI] << "Missed inset at x,y " << x << "," << y
164 << " box " << b << endl;
168 text.setCursor(bv, &par, pos, true);
171 // The origin of an inset is on the baseline
172 y -= text.cursor.iy();
180 BufferView::Pimpl::Pimpl(BufferView * bv, LyXView * owner,
181 int xpos, int ypos, int width, int height)
182 : bv_(bv), owner_(owner), buffer_(0), cursor_timeout(400),
183 using_xterm_cursor(false)
185 workarea_.reset(WorkAreaFactory::create(xpos, ypos, width, height));
186 screen_.reset(LyXScreenFactory::create(workarea()));
189 doccon = workarea().scrollDocView
190 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
191 resizecon = workarea().workAreaResize
192 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
193 dispatchcon = workarea().dispatch
194 .connect(boost::bind(&BufferView::Pimpl::dispatch, this, _1));
195 kpresscon = workarea().workAreaKeyPress
196 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
197 selectioncon = workarea().selectionRequested
198 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
199 lostcon = workarea().selectionLost
200 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
202 timecon = cursor_timeout.timeout
203 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
204 cursor_timeout.start();
205 saved_positions.resize(saved_positions_num);
209 WorkArea & BufferView::Pimpl::workarea() const
211 return *workarea_.get();
215 LyXScreen & BufferView::Pimpl::screen() const
217 return *screen_.get();
221 Painter & BufferView::Pimpl::painter() const
223 return workarea().getPainter();
227 void BufferView::Pimpl::buffer(Buffer * b)
229 lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
232 buffer_->delUser(bv_);
234 // Put the old text into the TextCache, but
235 // only if the buffer is still loaded.
236 // Also set the owner of the test to 0
237 // bv_->text->owner(0);
238 textcache.add(buffer_, workarea().workWidth(), bv_->text);
239 if (lyxerr.debugging())
240 textcache.show(lyxerr, "BufferView::buffer");
245 // set current buffer
248 if (bufferlist.getState() == BufferList::CLOSING) return;
250 // if we are closing the buffer, use the first buffer as current
252 buffer_ = bufferlist.first();
256 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
257 buffer_->addUser(bv_);
259 // If we don't have a text object for this, we make one
260 if (bv_->text == 0) {
261 resizeCurrentBuffer();
264 // FIXME: needed when ?
266 screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
268 // Similarly, buffer-dependent dialogs should be updated or
269 // hidden. This should go here because some dialogs (eg ToC)
270 // require bv_->text.
271 owner_->getDialogs().updateBufferDependent(true);
273 lyxerr[Debug::INFO] << " No Buffer!" << endl;
274 owner_->getDialogs().hideBufferDependent();
276 // Also remove all remaining text's from the testcache.
277 // (there should not be any!) (if there is any it is a
279 if (lyxerr.debugging())
280 textcache.show(lyxerr, "buffer delete all");
286 owner_->updateMenubar();
287 owner_->updateToolbar();
288 owner_->updateLayoutChoice();
289 owner_->updateWindowTitle();
291 if (grfx::Previews::activated() && buffer_)
292 grfx::Previews::get().generateBufferPreviews(*buffer_);
296 bool BufferView::Pimpl::fitCursor()
300 if (bv_->theLockingInset()) {
301 bv_->theLockingInset()->fitInsetCursor(bv_);
304 ret = screen().fitCursor(bv_->text, bv_);
307 bv_->owner()->getDialogs().updateParagraph();
314 void BufferView::Pimpl::redoCurrentBuffer()
316 lyxerr[Debug::INFO] << "BufferView::redoCurrentBuffer" << endl;
317 if (buffer_ && bv_->text) {
318 resizeCurrentBuffer();
320 owner_->updateLayoutChoice();
326 int BufferView::Pimpl::resizeCurrentBuffer()
328 lyxerr[Debug::INFO] << "resizeCurrentBuffer" << endl;
331 Paragraph * selstartpar = 0;
332 Paragraph * selendpar = 0;
333 UpdatableInset * the_locking_inset = 0;
336 pos_type selstartpos = 0;
337 pos_type selendpos = 0;
338 bool selection = false;
339 bool mark_set = false;
341 owner_->prohibitInput();
343 owner_->message(_("Formatting document..."));
346 par = bv_->text->cursor.par();
347 pos = bv_->text->cursor.pos();
348 selstartpar = bv_->text->selection.start.par();
349 selstartpos = bv_->text->selection.start.pos();
350 selendpar = bv_->text->selection.end.par();
351 selendpos = bv_->text->selection.end.pos();
352 selection = bv_->text->selection.set();
353 mark_set = bv_->text->selection.mark();
354 the_locking_inset = bv_->theLockingInset();
355 buffer_->resizeInsets(bv_);
356 // I don't think the delete and new are necessary here we just could
357 // call only init! (Jug 20020419)
359 bv_->text = new LyXText(bv_);
360 bv_->text->init(bv_);
362 // See if we have a text in TextCache that fits
363 // the new buffer_ with the correct width.
364 bv_->text = textcache.findFit(buffer_, workarea().workWidth());
366 if (lyxerr.debugging()) {
367 lyxerr << "Found a LyXText that fits:\n";
368 textcache.show(lyxerr, make_pair(buffer_, make_pair(workarea().workWidth(), bv_->text)));
370 // Set the owner of the newly found text
371 // bv_->text->owner(bv_);
372 if (lyxerr.debugging())
373 textcache.show(lyxerr, "resizeCurrentBuffer");
375 bv_->text = new LyXText(bv_);
376 bv_->text->init(bv_);
377 //buffer_->resizeInsets(bv_);
382 bv_->text->selection.set(true);
383 // At this point just to avoid the Delete-Empty-Paragraph-
384 // Mechanism when setting the cursor.
385 bv_->text->selection.mark(mark_set);
387 bv_->text->setCursor(bv_, selstartpar, selstartpos);
388 bv_->text->selection.cursor = bv_->text->cursor;
389 bv_->text->setCursor(bv_, selendpar, selendpos);
390 bv_->text->setSelection(bv_);
391 bv_->text->setCursor(bv_, par, pos);
393 bv_->text->setCursor(bv_, par, pos);
394 bv_->text->selection.cursor = bv_->text->cursor;
395 bv_->text->selection.set(false);
397 // remake the inset locking
398 bv_->theLockingInset(the_locking_inset);
401 bv_->text->first_y = screen().topCursorVisible(bv_->text->cursor, bv_->text->first_y);
404 owner_->allowInput();
412 void BufferView::Pimpl::repaint()
414 // Regenerate the screen.
415 screen().redraw(bv_->text, bv_);
419 void BufferView::Pimpl::updateScrollbar()
422 lyxerr[Debug::GUI] << "no text in updateScrollbar" << endl;
423 workarea().setScrollbarParams(0, 0, 0);
427 LyXText const & t = *bv_->text;
429 lyxerr[Debug::GUI] << "Updating scrollbar: h " << t.height << ", first_y "
430 << t.first_y << ", default height " << t.defaultHeight() << endl;
432 workarea().setScrollbarParams(t.height, t.first_y, t.defaultHeight());
436 void BufferView::Pimpl::scrollDocView(int value)
438 lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
440 if (!buffer_) return;
442 screen().draw(bv_->text, bv_, value);
444 if (!lyxrc.cursor_follows_scrollbar) {
448 LyXText * vbt = bv_->text;
450 int const height = vbt->defaultHeight();
451 int const first = static_cast<int>((bv_->text->first_y + height));
452 int const last = static_cast<int>((bv_->text->first_y + workarea().workHeight() - height));
454 if (vbt->cursor.y() < first)
455 vbt->setCursorFromCoordinates(bv_, 0, first);
456 else if (vbt->cursor.y() > last)
457 vbt->setCursorFromCoordinates(bv_, 0, last);
461 int BufferView::Pimpl::scroll(long time)
466 LyXText const * t = bv_->text;
468 double const diff = t->defaultHeight()
469 + double(time) * double(time) * 0.125;
471 scrollDocView(int(diff));
472 workarea().setScrollbarParams(t->height, t->first_y, t->defaultHeight());
477 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
478 key_modifier::state state)
480 bv_->owner()->getLyXFunc().processKeySym(key, state);
484 void BufferView::Pimpl::selectionRequested()
491 LyXText * text = bv_->getLyXText();
493 if (text->selection.set() &&
494 (!bv_->text->xsel_cache.set() ||
495 text->selection.start != bv_->text->xsel_cache.start ||
496 text->selection.end != bv_->text->xsel_cache.end))
498 bv_->text->xsel_cache = text->selection;
499 sel = text->selectionAsString(bv_->buffer(), false);
500 } else if (!text->selection.set()) {
502 bv_->text->xsel_cache.set(false);
505 workarea().putClipboard(sel);
510 void BufferView::Pimpl::selectionLost()
515 bv_->getLyXText()->clearSelection();
517 bv_->text->xsel_cache.set(false);
523 Inset * BufferView::Pimpl::checkInsetHit(LyXText * text, int & x, int & y)
525 int y_tmp = y + text->first_y;
528 text->setCursorFromCoordinates(bv_, cursor, x, y_tmp);
530 Inset * inset = checkInset(bv_, *text, cursor, x, y_tmp);
537 // look at previous position
538 if (cursor.pos() == 0) {
543 text->setCursor(bv_, cursor, cursor.par(), cursor.pos() - 1, true);
545 inset = checkInset(bv_, *text, cursor, x, y_tmp);
553 void BufferView::Pimpl::workAreaResize()
555 static int work_area_width;
556 static int work_area_height;
558 bool const widthChange = workarea().workWidth() != work_area_width;
559 bool const heightChange = workarea().workHeight() != work_area_height;
561 // update from work area
562 work_area_width = workarea().workWidth();
563 work_area_height = workarea().workHeight();
567 // The visible LyXView need a resize
568 resizeCurrentBuffer();
570 // Remove all texts from the textcache
571 // This is not _really_ what we want to do. What
572 // we really want to do is to delete in textcache
573 // that does not have a BufferView with matching
574 // width, but as long as we have only one BufferView
575 // deleting all gives the same result.
576 if (lyxerr.debugging())
577 textcache.show(lyxerr, "Expose delete all");
579 // FIXME: this is already done in resizeCurrentBuffer() ??
580 buffer_->resizeInsets(bv_);
581 } else if (heightChange) {
582 // fitCursor() ensures we don't jump back
583 // to the start of the document on vertical
589 if (widthChange || heightChange) {
593 // always make sure that the scrollbar is sane.
595 owner_->updateLayoutChoice();
600 void BufferView::Pimpl::update()
602 if (!bv_->theLockingInset() || !bv_->theLockingInset()->nodraw()) {
603 LyXText::text_status st = bv_->text->status();
604 screen().update(bv_->text, bv_);
606 while (bv_->text->status() == LyXText::CHANGED_IN_DRAW) {
607 bv_->text->fullRebreak(bv_);
608 st = LyXText::NEED_MORE_REFRESH;
609 bv_->text->setCursor(bv_, bv_->text->cursor.par(),
610 bv_->text->cursor.pos());
611 if (bv_->text->selection.set()) {
612 bv_->text->setCursor(bv_, bv_->text->selection.start,
613 bv_->text->selection.start.par(),
614 bv_->text->selection.start.pos());
615 bv_->text->setCursor(bv_, bv_->text->selection.end,
616 bv_->text->selection.end.par(),
617 bv_->text->selection.end.pos());
620 bv_->text->status(bv_, st);
621 screen().update(bv_->text, bv_);
623 // do this here instead of in the screen::update because of
625 bv_->text->status(bv_, LyXText::UNCHANGED);
631 // Values used when calling update:
633 // -2 - update, move sel_cursor if selection, fitcursor
634 // -1 - update, move sel_cursor if selection, fitcursor, mark dirty
635 // 0 - update, move sel_cursor if selection, fitcursor
636 // 1 - update, move sel_cursor if selection, fitcursor, mark dirty
637 // 3 - update, move sel_cursor if selection
640 // a simple redraw of the parts that need refresh
642 // move sel_cursor if selection -
643 // the text's sel_cursor is moved if there is selection is progress
646 // fitCursor() is called and the scrollbar updated
649 // the buffer is marked dirty.
658 // UPDATE_ONLY = UPDATE;
659 // UPDATE_SELECT = UPDATE | SELECT;
660 // UPDATE_SELECT_MOVE = UPDATE | SELECT | FITCUR;
661 // UPDATE_SELECT_MOVE_AFTER_CHANGE = UPDATE | SELECT | FITCUR | CHANGE;
663 // update(-3) -> update(0) -> update(0) -> update(UPDATE)
664 // update(-2) -> update(1 + 2) -> update(3) -> update(SELECT|FITCUR)
665 // update(-1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
666 // update(1) -> update(1 + 2 + 4) -> update(7) -> update(SELECT|FITCUR|CHANGE)
667 // update(3) -> update(1) -> update(1) -> update(SELECT)
669 void BufferView::Pimpl::update(LyXText * text, BufferView::UpdateCodes f)
671 owner_->updateLayoutChoice();
673 if (!text->selection.set() && (f & SELECT)) {
674 text->selection.cursor = text->cursor;
677 text->fullRebreak(bv_);
679 if (text->inset_owner) {
680 text->inset_owner->setUpdateStatus(bv_, InsetText::NONE);
681 updateInset(text->inset_owner, false);
691 buffer_->markDirty();
696 // Callback for cursor timer
697 void BufferView::Pimpl::cursorToggle()
700 cursor_timeout.restart();
705 extern void reapSpellchecker(void);
708 if (!bv_->theLockingInset()) {
709 screen().cursorToggle(bv_);
711 bv_->theLockingInset()->toggleInsetCursor(bv_);
714 cursor_timeout.restart();
718 bool BufferView::Pimpl::available() const
720 if (buffer_ && bv_->text)
726 void BufferView::Pimpl::beforeChange(LyXText * text)
729 text->clearSelection();
733 void BufferView::Pimpl::savePosition(unsigned int i)
735 if (i >= saved_positions_num)
737 saved_positions[i] = Position(buffer_->fileName(),
738 bv_->text->cursor.par()->id(),
739 bv_->text->cursor.pos());
742 str << _("Saved bookmark") << ' ' << i;
743 owner_->message(str.str().c_str());
748 void BufferView::Pimpl::restorePosition(unsigned int i)
750 if (i >= saved_positions_num)
753 string const fname = saved_positions[i].filename;
755 beforeChange(bv_->text);
757 if (fname != buffer_->fileName()) {
758 Buffer * b = bufferlist.exists(fname) ?
759 bufferlist.getBuffer(fname) :
760 bufferlist.loadLyXFile(fname); // don't ask, just load it
761 if (b != 0) buffer(b);
764 Paragraph * par = buffer_->getParFromID(saved_positions[i].par_id);
768 bv_->text->setCursor(bv_, par,
769 min(par->size(), saved_positions[i].par_pos));
771 update(bv_->text, BufferView::SELECT | BufferView::FITCUR);
774 str << _("Moved to bookmark") << ' ' << i;
775 owner_->message(str.str().c_str());
780 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
782 if (i >= saved_positions_num)
785 return !saved_positions[i].filename.empty();
789 void BufferView::Pimpl::switchKeyMap()
791 if (!lyxrc.rtl_support)
794 LyXText * text = bv_->getLyXText();
795 if (text->real_current_font.isRightToLeft()
796 && !(bv_->theLockingInset()
797 && bv_->theLockingInset()->lyxCode() == Inset::ERT_CODE))
799 if (owner_->getIntl().keymap == Intl::PRIMARY)
800 owner_->getIntl().KeyMapSec();
802 if (owner_->getIntl().keymap == Intl::SECONDARY)
803 owner_->getIntl().KeyMapPrim();
808 void BufferView::Pimpl::insetUnlock()
810 if (bv_->theLockingInset()) {
811 bv_->theLockingInset()->insetUnlock(bv_);
812 bv_->theLockingInset(0);
818 void BufferView::Pimpl::showCursor()
820 if (bv_->theLockingInset())
821 bv_->theLockingInset()->showInsetCursor(bv_);
823 screen().showCursor(bv_->text, bv_);
827 void BufferView::Pimpl::hideCursor()
829 if (!bv_->theLockingInset())
830 screen().hideCursor();
834 void BufferView::Pimpl::toggleSelection(bool b)
836 if (bv_->theLockingInset())
837 bv_->theLockingInset()->toggleSelection(bv_, b);
838 screen().toggleSelection(bv_->text, bv_, b);
842 void BufferView::Pimpl::toggleToggle()
844 screen().toggleToggle(bv_->text, bv_);
848 void BufferView::Pimpl::center()
850 LyXText * t = bv_->text;
853 int const half_height = workarea().workHeight() / 2;
856 if (t->cursor.y() > half_height) {
857 new_y = t->cursor.y() - half_height;
860 // FIXME: can we do this w/o calling screen directly ?
861 // This updates first_y but means the fitCursor() call
862 // from the update(FITCUR) doesn't realise that we might
863 // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
864 // the scrollbar to be updated as it should, so we have
865 // to do it manually. Any operation that does a center()
866 // and also might have moved first_y must make sure to call
867 // updateScrollbar() currently. Never mind that this is a
868 // pretty obfuscated way of updating t->first_y
869 screen().draw(t, bv_, new_y);
871 update(t, BufferView::SELECT | BufferView::FITCUR);
875 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
877 workarea().putClipboard(stuff);
882 * Dispatch functions for actions which can be valid for BufferView->text
883 * and/or InsetText->text!!!
887 Inset * BufferView::Pimpl::getInsetByCode(Inset::Code code)
890 LyXCursor cursor = bv_->getLyXText()->cursor;
891 Buffer::inset_iterator it =
892 find_if(Buffer::inset_iterator(
893 cursor.par(), cursor.pos()),
894 buffer_->inset_iterator_end(),
895 lyx::compare_memfun(&Inset::lyxCode, code));
896 return it != buffer_->inset_iterator_end() ? (*it) : 0;
898 // Ok, this is a little bit too brute force but it
899 // should work for now. Better infrastructure is comming. (Lgb)
901 Buffer * b = bv_->buffer();
902 LyXCursor cursor = bv_->getLyXText()->cursor;
904 Buffer::inset_iterator beg = b->inset_iterator_begin();
905 Buffer::inset_iterator end = b->inset_iterator_end();
907 bool cursor_par_seen = false;
909 for (; beg != end; ++beg) {
910 if (beg.getPar() == cursor.par()) {
911 cursor_par_seen = true;
913 if (cursor_par_seen) {
914 if (beg.getPar() == cursor.par()
915 && beg.getPos() >= cursor.pos()) {
917 } else if (beg.getPar() != cursor.par()) {
924 // Now find the first inset that matches code.
925 for (; beg != end; ++beg) {
926 if (beg->lyxCode() == code) {
936 void BufferView::Pimpl::MenuInsertLyXFile(string const & filen)
938 string filename = filen;
940 if (filename.empty()) {
941 // Launch a file browser
942 string initpath = lyxrc.document_path;
945 string const trypath = owner_->buffer()->filePath();
946 // If directory is writeable, use this as default.
947 if (IsDirWriteable(trypath))
951 FileDialog fileDlg(bv_->owner(),
952 _("Select LyX document to insert"),
954 make_pair(string(_("Documents|#o#O")),
955 string(lyxrc.document_path)),
956 make_pair(string(_("Examples|#E#e")),
957 string(AddPath(system_lyxdir, "examples"))));
959 FileDialog::Result result =
960 fileDlg.Select(initpath,
961 _("*.lyx| LyX Documents (*.lyx)"));
963 if (result.first == FileDialog::Later)
966 filename = result.second;
968 // check selected filename
969 if (filename.empty()) {
970 owner_->message(_("Canceled."));
975 // get absolute path of file and add ".lyx" to the filename if
977 filename = FileSearch(string(), filename, "lyx");
979 string const disp_fn(MakeDisplayPath(filename));
982 s1 << _("Inserting document") << ' '
983 << disp_fn << " ...";
984 owner_->message(s1.str().c_str());
985 bool const res = bv_->insertLyXFile(filename);
988 str << _("Document") << ' ' << disp_fn
989 << ' ' << _("inserted.");
990 owner_->message(str.str().c_str());
993 str << _("Could not insert document") << ' '
995 owner_->message(str.str().c_str());
1000 bool BufferView::Pimpl::dispatch(FuncRequest const & ev)
1002 lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
1003 << " action[" << ev.action <<"]"
1004 << " arg[" << ev.argument << "]"
1005 << " x[" << ev.x << "]"
1006 << " y[" << ev.y << "]"
1007 << " button[" << ev.button() << "]"
1010 LyXTextClass const & tclass = buffer_->params.getLyXTextClass();
1012 switch (ev.action) {
1014 case LFUN_TOC_INSERT:
1016 InsetCommandParams p;
1017 p.setCmdName("tableofcontents");
1018 Inset * inset = new InsetTOC(p);
1019 if (!insertInset(inset, tclass.defaultLayoutName()))
1024 case LFUN_SCROLL_INSET:
1025 // this is not handled here as this function is only active
1026 // if we have a locking_inset and that one is (or contains)
1030 case LFUN_INSET_GRAPHICS:
1032 Inset * new_inset = new InsetGraphics;
1033 if (!insertInset(new_inset)) {
1036 // this is need because you don't use a inset->Edit()
1037 updateInset(new_inset, true);
1038 new_inset->edit(bv_);
1043 case LFUN_LAYOUT_COPY:
1044 bv_->copyEnvironment();
1047 case LFUN_LAYOUT_PASTE:
1048 bv_->pasteEnvironment();
1052 case LFUN_DEPTH_MIN:
1053 changeDepth(bv_, bv_->getLyXText(), -1);
1056 case LFUN_DEPTH_PLUS:
1057 changeDepth(bv_, bv_->getLyXText(), 1);
1061 owner_->getDialogs().setUserFreeFont();
1064 case LFUN_FILE_INSERT:
1065 MenuInsertLyXFile(ev.argument);
1068 case LFUN_FILE_INSERT_ASCII_PARA:
1069 InsertAsciiFile(bv_, ev.argument, true);
1072 case LFUN_FILE_INSERT_ASCII:
1073 InsertAsciiFile(bv_, ev.argument, false);
1077 lang(bv_, ev.argument);
1079 owner_->view_state_changed();
1084 owner_->view_state_changed();
1089 owner_->view_state_changed();
1094 owner_->view_state_changed();
1099 owner_->view_state_changed();
1104 owner_->view_state_changed();
1109 owner_->view_state_changed();
1114 owner_->view_state_changed();
1117 case LFUN_UNDERLINE:
1119 owner_->view_state_changed();
1122 case LFUN_FONT_SIZE:
1123 fontSize(bv_, ev.argument);
1124 owner_->view_state_changed();
1127 case LFUN_FONT_STATE:
1128 owner_->getLyXFunc().setMessage(currentState(bv_));
1131 case LFUN_INSERT_LABEL:
1132 MenuInsertLabel(bv_, ev.argument);
1135 case LFUN_REF_INSERT:
1136 if (ev.argument.empty()) {
1137 InsetCommandParams p("ref");
1138 owner_->getDialogs().createRef(p.getAsString());
1140 InsetCommandParams p;
1141 p.setFromString(ev.argument);
1143 InsetRef * inset = new InsetRef(p, *buffer_);
1144 if (!insertInset(inset))
1147 updateInset(inset, true);
1151 case LFUN_BOOKMARK_SAVE:
1152 savePosition(strToUnsignedInt(ev.argument));
1155 case LFUN_BOOKMARK_GOTO:
1156 restorePosition(strToUnsignedInt(ev.argument));
1161 string label = ev.argument;
1162 if (label.empty()) {
1164 static_cast<InsetRef*>(getInsetByCode(Inset::REF_CODE));
1166 label = inset->getContents();
1171 if (!label.empty()) {
1172 //bv_->savePosition(0);
1173 if (!bv_->gotoLabel(label))
1174 Alert::alert(_("Error"),
1175 _("Couldn't find this label"),
1176 _("in current document."));
1184 InsetCommandParams p;
1185 if (ev.action == LFUN_HTMLURL)
1186 p.setCmdName("htmlurl");
1188 p.setCmdName("url");
1189 owner_->getDialogs().createUrl(p.getAsString());
1193 case LFUN_INSERT_URL:
1195 InsetCommandParams p;
1196 p.setFromString(ev.argument);
1198 InsetUrl * inset = new InsetUrl(p);
1199 if (!insertInset(inset))
1202 updateInset(inset, true);
1206 case LFUN_INSET_CAPTION:
1208 // Do we have a locking inset...
1209 if (bv_->theLockingInset()) {
1210 lyxerr << "Locking inset code: "
1211 << static_cast<int>(bv_->theLockingInset()->lyxCode());
1212 InsetCaption * new_inset =
1213 new InsetCaption(buffer_->params);
1214 new_inset->setOwner(bv_->theLockingInset());
1215 new_inset->setAutoBreakRows(true);
1216 new_inset->setDrawFrame(0, InsetText::LOCKED);
1217 new_inset->setFrameColor(0, LColor::captionframe);
1218 if (insertInset(new_inset))
1219 new_inset->edit(bv_);
1227 // --- accented characters ---------------------------
1230 case LFUN_CIRCUMFLEX:
1240 case LFUN_SPECIAL_CARON:
1243 case LFUN_HUNG_UMLAUT:
1246 if (ev.argument.empty()) {
1248 owner_->getLyXFunc().handleKeyFunc(ev.action);
1250 owner_->getLyXFunc().handleKeyFunc(ev.action);
1251 owner_->getIntl().getTransManager()
1252 .TranslateAndInsert(ev.argument[0], bv_->getLyXText());
1253 update(bv_->getLyXText(),
1255 | BufferView::FITCUR
1256 | BufferView::CHANGE);
1260 case LFUN_MATH_MACRO:
1261 case LFUN_MATH_DELIM:
1262 case LFUN_INSERT_MATRIX:
1263 case LFUN_INSERT_MATH:
1264 case LFUN_MATH_IMPORT_SELECTION: // Imports LaTeX from the X selection
1265 case LFUN_MATH_DISPLAY: // Open or create a displayed math inset
1266 case LFUN_MATH_MODE: // Open or create an inlined math inset
1267 case LFUN_GREEK: // Insert a single greek letter
1268 mathDispatch(FuncRequest(bv_, ev.action, ev.argument));
1271 case LFUN_CITATION_INSERT:
1273 InsetCommandParams p;
1274 p.setFromString(ev.argument);
1276 InsetCitation * inset = new InsetCitation(p);
1277 if (!insertInset(inset))
1280 updateInset(inset, true);
1284 case LFUN_INSERT_BIBTEX:
1286 // ale970405+lasgoutt970425
1287 // The argument can be up to two tokens separated
1288 // by a space. The first one is the bibstyle.
1289 string const db = token(ev.argument, ' ', 0);
1290 string bibstyle = token(ev.argument, ' ', 1);
1291 if (bibstyle.empty())
1294 InsetCommandParams p("BibTeX", db, bibstyle);
1295 InsetBibtex * inset = new InsetBibtex(p);
1297 if (insertInset(inset)) {
1298 if (ev.argument.empty())
1305 // BibTeX data bases
1306 case LFUN_BIBDB_ADD:
1308 InsetBibtex * inset =
1309 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1311 inset->addDatabase(ev.argument);
1316 case LFUN_BIBDB_DEL:
1318 InsetBibtex * inset =
1319 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1321 inset->delDatabase(ev.argument);
1326 case LFUN_BIBTEX_STYLE:
1328 InsetBibtex * inset =
1329 static_cast<InsetBibtex*>(getInsetByCode(Inset::BIBTEX_CODE));
1331 inset->setOptions(ev.argument);
1336 case LFUN_INDEX_INSERT:
1338 string entry = ev.argument;
1340 entry = bv_->getLyXText()->getStringToIndex(bv_);
1342 if (entry.empty()) {
1343 owner_->getDialogs().createIndex();
1347 InsetIndex * inset = new InsetIndex(InsetCommandParams("index", entry));
1349 if (!insertInset(inset)) {
1352 updateInset(inset, true);
1357 case LFUN_INDEX_PRINT:
1359 InsetCommandParams p("printindex");
1360 Inset * inset = new InsetPrintIndex(p);
1361 if (!insertInset(inset, tclass.defaultLayoutName()))
1366 case LFUN_PARENTINSERT:
1368 InsetCommandParams p("lyxparent", ev.argument);
1369 Inset * inset = new InsetParent(p, *buffer_);
1370 if (!insertInset(inset, tclass.defaultLayoutName()))
1376 case LFUN_CHILD_INSERT:
1378 InsetInclude::Params p;
1379 p.cparams.setFromString(ev.argument);
1380 p.masterFilename_ = buffer_->fileName();
1382 InsetInclude * inset = new InsetInclude(p);
1383 if (!insertInset(inset))
1386 updateInset(inset, true);
1387 bv_->owner()->getDialogs().showInclude(inset);
1392 case LFUN_FLOAT_LIST:
1393 if (tclass.floats().typeExist(ev.argument)) {
1394 Inset * inset = new InsetFloatList(ev.argument);
1395 if (!insertInset(inset, tclass.defaultLayoutName()))
1398 lyxerr << "Non-existent float type: "
1399 << ev.argument << endl;
1403 case LFUN_THESAURUS_ENTRY:
1405 string arg = ev.argument;
1408 arg = bv_->getLyXText()->selectionAsString(buffer_,
1412 if (arg.size() > 100 || arg.empty()) {
1413 // Get word or selection
1414 bv_->getLyXText()->selectWordWhenUnderCursor(bv_, LyXText::WHOLE_WORD);
1415 arg = bv_->getLyXText()->selectionAsString(buffer_, false);
1416 // FIXME: where is getLyXText()->unselect(bv_) ?
1420 bv_->owner()->getDialogs().showThesaurus(arg);
1424 case LFUN_UNKNOWN_ACTION:
1425 ev.errorMessage(N_("Unknown function!"));
1428 case LFUN_MOUSE_TRIPLE:
1433 LyXText * text = bv_->getLyXText();
1435 if (text->bv_owner && bv_->theLockingInset())
1438 if (ev.button() == mouse_button::button1) {
1439 if (text->bv_owner) {
1440 screen().hideCursor();
1441 screen().toggleSelection(text, bv_);
1443 text->cursorHome(bv_);
1444 text->selection.cursor = text->cursor;
1445 text->cursorEnd(bv_);
1446 text->setSelection(bv_);
1448 screen().toggleSelection(text, bv_, false);
1449 // This will fit the cursor on the screen if necessary
1450 update(text, BufferView::SELECT|BufferView::FITCUR);
1451 workarea().haveSelection(bv_->getLyXText()->selection.set());
1456 case LFUN_MOUSE_DOUBLE:
1461 LyXText * text = bv_->getLyXText();
1463 if (text->bv_owner && bv_->theLockingInset())
1466 if (ev.button() == mouse_button::button1) {
1467 if (text->bv_owner) {
1468 screen().hideCursor();
1469 screen().toggleSelection(text, bv_);
1470 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
1471 screen().toggleSelection(text, bv_, false);
1473 text->selectWord(bv_, LyXText::WHOLE_WORD_STRICT);
1475 // This will fit the cursor on the screen if necessary
1476 update(text, BufferView::SELECT|BufferView::FITCUR);
1477 workarea().haveSelection(bv_->getLyXText()->selection.set());
1482 case LFUN_MOUSE_MOTION:
1484 // Only use motion with button 1
1485 //if (ev.button() != mouse_button::button1)
1491 // Check for inset locking
1492 if (bv_->theLockingInset()) {
1493 LyXCursor cursor = bv_->text->cursor;
1494 LyXFont font = bv_->text->getFont(buffer_,
1495 cursor.par(), cursor.pos());
1496 int width = bv_->theLockingInset()->width(bv_, font);
1497 int inset_x = font.isVisibleRightToLeft()
1498 ? cursor.ix() - width : cursor.ix();
1499 int start_x = inset_x + bv_->theLockingInset()->scroll();
1501 FuncRequest cmd(bv_, LFUN_MOUSE_MOTION,
1502 ev.x - start_x, ev.y - cursor.iy() + bv_->text->first_y, ev.button());
1503 bv_->theLockingInset()->localDispatch(cmd);
1507 // The test for not selection possible is needed, that only motion
1508 // events are used, where the bottom press event was on
1509 // the drawing area too
1510 if (!selection_possible) {
1511 lyxerr[Debug::ACTION]
1512 << "BufferView::Pimpl::Dispatch: no selection possible\n";
1516 screen().hideCursor();
1518 Row * cursorrow = bv_->text->cursor.row();
1519 bv_->text->setCursorFromCoordinates(bv_, ev.x, ev.y + bv_->text->first_y);
1521 // sorry for this but I have a strange error that the y value jumps at
1522 // a certain point. This seems like an error in my xforms library or
1523 // in some other local environment, but I would like to leave this here
1524 // for the moment until I can remove this (Jug 20020418)
1525 if (y_before < bv_->text->cursor.y())
1526 lyxerr << y_before << ":" << bv_->text->cursor.y() << endl;
1528 // This is to allow jumping over large insets
1529 if (cursorrow == bv_->text->cursor.row()) {
1530 if (ev.y >= int(workarea().workHeight())) {
1531 bv_->text->cursorDown(bv_, false);
1532 } else if (ev.y < 0) {
1533 bv_->text->cursorUp(bv_, false);
1537 if (!bv_->text->selection.set())
1538 update(bv_->text, BufferView::UPDATE); // Maybe an empty line was deleted
1540 bv_->text->setSelection(bv_);
1541 screen().toggleToggle(bv_->text, bv_);
1547 // Single-click on work area
1548 case LFUN_MOUSE_PRESS:
1553 // ok ok, this is a hack (for xforms)
1554 if (ev.button() == mouse_button::button4) {
1555 scroll(-lyxrc.wheel_jump);
1556 // We shouldn't go further down as we really should only do the
1557 // scrolling and be done with this. Otherwise we may open some
1558 // dialogs (Jug 20020424).
1561 if (ev.button() == mouse_button::button5) {
1562 scroll(lyxrc.wheel_jump);
1563 // We shouldn't go further down as we really should only do the
1564 // scrolling and be done with this. Otherwise we may open some
1565 // dialogs (Jug 20020424).
1571 Inset * inset_hit = checkInsetHit(bv_->text, x, y);
1573 // Middle button press pastes if we have a selection
1574 // We do this here as if the selection was inside an inset
1575 // it could get cleared on the unlocking of the inset so
1576 // we have to check this first
1577 bool paste_internally = false;
1578 if (ev.button() == mouse_button::button2
1579 && bv_->getLyXText()->selection.set())
1581 owner_->dispatch(FuncRequest(LFUN_COPY));
1582 paste_internally = true;
1585 int const screen_first = bv_->text->first_y;
1587 if (bv_->theLockingInset()) {
1588 // We are in inset locking mode
1590 // Check whether the inset was hit. If not reset mode,
1591 // otherwise give the event to the inset
1592 if (inset_hit == bv_->theLockingInset()) {
1593 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, x, y, ev.button());
1594 bv_->theLockingInset()->localDispatch(cmd);
1597 bv_->unlockInset(bv_->theLockingInset());
1601 selection_possible = true;
1602 screen().hideCursor();
1604 // Clear the selection
1605 screen().toggleSelection(bv_->text, bv_);
1606 bv_->text->clearSelection();
1607 bv_->text->fullRebreak(bv_);
1611 // Single left click in math inset?
1612 if (isHighlyEditableInset(inset_hit)) {
1613 // Highly editable inset, like math
1614 UpdatableInset * inset = static_cast<UpdatableInset *>(inset_hit);
1615 selection_possible = false;
1616 owner_->updateLayoutChoice();
1617 owner_->message(inset->editMessage());
1618 //inset->edit(bv_, x, y, ev.button());
1619 // We just have to lock the inset before calling a PressEvent on it!
1620 // we don't need the edit() call here! (Jug20020329)
1621 if (!bv_->lockInset(inset)) {
1622 lyxerr[Debug::INSETS] << "Cannot lock inset" << endl;
1624 FuncRequest cmd(bv_, LFUN_MOUSE_PRESS, x, y, ev.button());
1625 inset->localDispatch(cmd);
1628 // I'm not sure we should continue here if we hit an inset (Jug20020403)
1630 // Right click on a footnote flag opens float menu
1631 if (ev.button() == mouse_button::button3) {
1632 selection_possible = false;
1636 if (!inset_hit) // otherwise it was already set in checkInsetHit(...)
1637 bv_->text->setCursorFromCoordinates(bv_, x, y + screen_first);
1639 bv_->text->selection.cursor = bv_->text->cursor;
1640 bv_->text->cursor.x_fix(bv_->text->cursor.x());
1642 owner_->updateLayoutChoice();
1644 selection_possible = false;
1647 // Insert primary selection with middle mouse
1648 // if there is a local selection in the current buffer,
1650 if (ev.button() == mouse_button::button2) {
1651 if (paste_internally)
1652 owner_->dispatch(FuncRequest(LFUN_PASTE));
1654 owner_->dispatch(FuncRequest(LFUN_PASTESELECTION, "paragraph"));
1655 selection_possible = false;
1661 case LFUN_MOUSE_RELEASE:
1663 // do nothing if we used the mouse wheel
1665 || ev.button() == mouse_button::button4
1666 || ev.button() == mouse_button::button5)
1669 // If we hit an inset, we have the inset coordinates in these
1670 // and inset_hit points to the inset. If we do not hit an
1671 // inset, inset_hit is 0, and inset_x == x, inset_y == y.
1674 Inset * inset_hit = checkInsetHit(bv_->text, x, y);
1676 if (bv_->theLockingInset()) {
1677 // We are in inset locking mode.
1679 // LyX does a kind of work-area grabbing for insets.
1680 // Only a ButtonPress FuncRequest outside the inset will
1681 // force a insetUnlock.
1682 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, ev.button());
1683 bv_->theLockingInset()->localDispatch(cmd);
1687 selection_possible = false;
1689 if (ev.button() == mouse_button::button2)
1693 if (ev.button() == mouse_button::button1) {
1694 workarea().haveSelection(bv_->getLyXText()->selection.set());
1698 owner_->view_state_changed();
1699 owner_->updateMenubar();
1700 owner_->updateToolbar();
1702 // Did we hit an editable inset?
1704 selection_possible = false;
1706 // if we reach this point with a selection, it
1707 // must mean we are currently selecting.
1708 // But we don't want to open the inset
1709 // because that is annoying for the user.
1710 // So just pretend we didn't hit it.
1711 // this is OK because a "kosher" ButtonRelease
1712 // will follow a ButtonPress that clears
1714 // Note this also fixes selection drawing
1715 // problems if we end up opening an inset
1716 if (bv_->getLyXText()->selection.set())
1719 // CHECK fix this proper in 0.13
1720 // well, maybe 13.0 !!!!!!!!!
1722 // Following a ref shouldn't issue
1723 // a push on the undo-stack
1724 // anylonger, now that we have
1725 // keybindings for following
1726 // references and returning from
1727 // references. IMHO though, it
1728 // should be the inset's own business
1729 // to push or not push on the undo
1730 // stack. They don't *have* to
1731 // alter the document...
1733 // ...or maybe the SetCursorParUndo()
1734 // below isn't necessary at all anylonger?
1735 if (inset_hit->lyxCode() == Inset::REF_CODE) {
1736 setCursorParUndo(bv_);
1739 owner_->message(inset_hit->editMessage());
1741 if (isHighlyEditableInset(inset_hit)) {
1742 // Highly editable inset, like math
1743 UpdatableInset *inset = (UpdatableInset *)inset_hit;
1744 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, ev.button());
1745 inset->localDispatch(cmd);
1747 FuncRequest cmd(bv_, LFUN_MOUSE_RELEASE, x, y, ev.button());
1748 inset_hit->localDispatch(cmd);
1749 // IMO this is a grosshack! Inset's should be changed so that
1750 // they call the actions they have to do with the insetButtonRel.
1751 // function and not in the edit(). This should be changed
1753 #ifdef WITH_WARNINGS
1754 #warning Please remove donot call inset->edit() here (Jug 20020812)
1756 inset_hit->edit(bv_, x, y, ev.button());
1761 // Maybe we want to edit a bibitem ale970302
1762 if (bv_->text->cursor.par()->bibkey) {
1763 bool const is_rtl = bv_->text->cursor.par()->isRightToLeftPar(buffer_->params);
1764 int const width = bibitemMaxWidth(bv_, buffer_->params.getLyXTextClass().defaultfont());
1765 if ((is_rtl && x > bv_->text->workWidth(bv_)-20-width) ||
1766 (!is_rtl && x < 20+width)) {
1767 bv_->text->cursor.par()->bibkey->edit(bv_, 0, 0, mouse_button::none);
1774 return bv_->getLyXText()->dispatch(FuncRequest(ev, bv_));
1781 bool BufferView::Pimpl::insertInset(Inset * inset, string const & lout)
1783 // if we are in a locking inset we should try to insert the
1784 // inset there otherwise this is a illegal function now
1785 if (bv_->theLockingInset()) {
1786 if (bv_->theLockingInset()->insetAllowed(inset))
1787 return bv_->theLockingInset()->insertInset(bv_, inset);
1791 // not quite sure if we want this...
1792 setCursorParUndo(bv_);
1795 beforeChange(bv_->text);
1796 if (!lout.empty()) {
1797 update(bv_->text, BufferView::SELECT|BufferView::FITCUR);
1798 bv_->text->breakParagraph(bv_);
1799 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1801 if (!bv_->text->cursor.par()->empty()) {
1802 bv_->text->cursorLeft(bv_);
1804 bv_->text->breakParagraph(bv_);
1805 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1809 LyXTextClass const & tclass =
1810 buffer_->params.getLyXTextClass();
1811 bool hasLayout = tclass.hasLayout(lres);
1812 string lay = tclass.defaultLayoutName();
1814 if (hasLayout != false) {
1818 // layout not fount using default
1819 lay = tclass.defaultLayoutName();
1822 bv_->text->setLayout(bv_, lay);
1824 bv_->text->setParagraph(bv_, 0, 0,
1826 VSpace(VSpace::NONE), VSpace(VSpace::NONE),
1831 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1834 bv_->text->insertInset(bv_, inset);
1835 update(bv_->text, BufferView::SELECT|BufferView::FITCUR|BufferView::CHANGE);
1842 void BufferView::Pimpl::updateInset(Inset * inset, bool mark_dirty)
1844 if (!inset || !available())
1847 // first check for locking insets
1848 if (bv_->theLockingInset()) {
1849 if (bv_->theLockingInset() == inset) {
1850 if (bv_->text->updateInset(bv_, inset)) {
1853 buffer_->markDirty();
1858 } else if (bv_->theLockingInset()->updateInsetInInset(bv_, inset)) {
1859 if (bv_->text->updateInset(bv_, bv_->theLockingInset())) {
1862 buffer_->markDirty();
1870 // then check if the inset is a top_level inset (has no owner)
1871 // if yes do the update as always otherwise we have to update the
1872 // toplevel inset where this inset is inside
1873 Inset * tl_inset = inset;
1874 while (tl_inset->owner())
1875 tl_inset = tl_inset->owner();
1877 if (tl_inset == inset) {
1878 update(bv_->text, BufferView::UPDATE);
1879 if (bv_->text->updateInset(bv_, inset)) {
1883 | BufferView::FITCUR
1884 | BufferView::CHANGE);
1886 update(bv_->text, SELECT);
1890 } else if (static_cast<UpdatableInset *>(tl_inset)
1891 ->updateInsetInInset(bv_, inset))
1893 if (bv_->text->updateInset(bv_, tl_inset)) {