]> git.lyx.org Git - lyx.git/blob - src/BufferView_pimpl.C
d80723d200b0969b40483bbc50bb09e009e4ace4
[lyx.git] / src / BufferView_pimpl.C
1 /**
2  * \file BufferView_pimpl.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Asger Alstrup
7  * \author Alfredo Braunstein
8  * \author Lars Gullik Bjønnes
9  * \author Jean-Marc Lasgouttes
10  * \author Angus Leeming
11  * \author John Levon
12  * \author André Pönitz
13  * \author Dekel Tsur
14  * \author Jürgen Vigna
15  *
16  * Full author contact details are available in file CREDITS.
17  */
18
19 #include <config.h>
20
21 #include "BufferView_pimpl.h"
22 #include "buffer.h"
23 #include "buffer_funcs.h"
24 #include "bufferlist.h"
25 #include "bufferparams.h"
26 #include "coordcache.h"
27 #include "cursor.h"
28 #include "debug.h"
29 #include "dispatchresult.h"
30 #include "factory.h"
31 #include "FloatList.h"
32 #include "funcrequest.h"
33 #include "FuncStatus.h"
34 #include "gettext.h"
35 #include "intl.h"
36 #include "insetiterator.h"
37 #include "lyx_cb.h" // added for Dispatch functions
38 #include "lyx_main.h"
39 #include "lyxfind.h"
40 #include "lyxfunc.h"
41 #include "lyxtext.h"
42 #include "lyxrc.h"
43 #include "lastfiles.h"
44 #include "paragraph.h"
45 #include "paragraph_funcs.h"
46 #include "ParagraphParameters.h"
47 #include "pariterator.h"
48 #include "rowpainter.h"
49 #include "undo.h"
50 #include "vspace.h"
51
52 #include "insets/insetref.h"
53 #include "insets/insettext.h"
54
55 #include "frontends/Alert.h"
56 #include "frontends/Dialogs.h"
57 #include "frontends/FileDialog.h"
58 #include "frontends/LyXView.h"
59 #include "frontends/LyXScreenFactory.h"
60 #include "frontends/screen.h"
61 #include "frontends/WorkArea.h"
62 #include "frontends/WorkAreaFactory.h"
63
64 #include "graphics/Previews.h"
65
66 #include "support/filetools.h"
67 #include "support/forkedcontr.h"
68 #include "support/globbing.h"
69 #include "support/path_defines.h"
70 #include "support/tostr.h"
71 #include "support/types.h"
72
73 #include <boost/bind.hpp>
74
75 using lyx::pos_type;
76
77 using lyx::support::AddPath;
78 using lyx::support::bformat;
79 using lyx::support::FileFilterList;
80 using lyx::support::FileSearch;
81 using lyx::support::ForkedcallsController;
82 using lyx::support::IsDirWriteable;
83 using lyx::support::MakeDisplayPath;
84 using lyx::support::strToUnsignedInt;
85 using lyx::support::system_lyxdir;
86
87 using std::endl;
88 using std::istringstream;
89 using std::make_pair;
90 using std::min;
91 using std::string;
92
93
94 extern BufferList bufferlist;
95
96
97 namespace {
98
99 unsigned int const saved_positions_num = 20;
100
101 // All the below connection objects are needed because of a bug in some
102 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
103 // to these connections we avoid a segfault upon startup, and also at exit.
104 // (Lgb)
105
106 boost::signals::connection dispatchcon;
107 boost::signals::connection timecon;
108 boost::signals::connection doccon;
109 boost::signals::connection resizecon;
110 boost::signals::connection kpresscon;
111 boost::signals::connection selectioncon;
112 boost::signals::connection lostcon;
113
114
115 /// Get next inset of this class from current cursor position
116 template <class T>
117 T * getInsetByCode(LCursor & cur, InsetBase::Code code)
118 {
119         T * inset = 0;
120         DocIterator it = cur;
121         if (it.nextInset() &&
122             it.nextInset()->lyxCode() == code) {
123                 inset = static_cast<T*>(it.nextInset());
124         }
125         return inset;
126 }
127
128
129 } // anon namespace
130
131
132 BufferView::Pimpl::Pimpl(BufferView & bv, LyXView * owner,
133                          int width, int height)
134         : bv_(&bv), owner_(owner), buffer_(0), cursor_timeout(400),
135           using_xterm_cursor(false), cursor_(bv)
136 {
137         xsel_cache_.set = false;
138
139         workarea_.reset(WorkAreaFactory::create(*owner_, width, height));
140         screen_.reset(LyXScreenFactory::create(workarea()));
141
142         // Setup the signals
143         doccon = workarea().scrollDocView
144                 .connect(boost::bind(&BufferView::Pimpl::scrollDocView, this, _1));
145         resizecon = workarea().workAreaResize
146                 .connect(boost::bind(&BufferView::Pimpl::workAreaResize, this));
147         dispatchcon = workarea().dispatch
148                 .connect(boost::bind(&BufferView::Pimpl::workAreaDispatch, this, _1));
149         kpresscon = workarea().workAreaKeyPress
150                 .connect(boost::bind(&BufferView::Pimpl::workAreaKeyPress, this, _1, _2));
151         selectioncon = workarea().selectionRequested
152                 .connect(boost::bind(&BufferView::Pimpl::selectionRequested, this));
153         lostcon = workarea().selectionLost
154                 .connect(boost::bind(&BufferView::Pimpl::selectionLost, this));
155
156         timecon = cursor_timeout.timeout
157                 .connect(boost::bind(&BufferView::Pimpl::cursorToggle, this));
158         cursor_timeout.start();
159         saved_positions.resize(saved_positions_num);
160 }
161
162
163 void BufferView::Pimpl::addError(ErrorItem const & ei)
164 {
165         errorlist_.push_back(ei);
166 }
167
168
169 void BufferView::Pimpl::showReadonly(bool)
170 {
171         owner_->updateWindowTitle();
172         owner_->getDialogs().updateBufferDependent(false);
173 }
174
175
176 void BufferView::Pimpl::connectBuffer(Buffer & buf)
177 {
178         if (errorConnection_.connected())
179                 disconnectBuffer();
180
181         errorConnection_ =
182                 buf.error.connect(
183                         boost::bind(&BufferView::Pimpl::addError, this, _1));
184
185         messageConnection_ =
186                 buf.message.connect(
187                         boost::bind(&LyXView::message, owner_, _1));
188
189         busyConnection_ =
190                 buf.busy.connect(
191                         boost::bind(&LyXView::busy, owner_, _1));
192
193         titleConnection_ =
194                 buf.updateTitles.connect(
195                         boost::bind(&LyXView::updateWindowTitle, owner_));
196
197         timerConnection_ =
198                 buf.resetAutosaveTimers.connect(
199                         boost::bind(&LyXView::resetAutosaveTimer, owner_));
200
201         readonlyConnection_ =
202                 buf.readonly.connect(
203                         boost::bind(&BufferView::Pimpl::showReadonly, this, _1));
204
205         closingConnection_ =
206                 buf.closing.connect(
207                         boost::bind(&BufferView::Pimpl::setBuffer, this, (Buffer *)0));
208 }
209
210
211 void BufferView::Pimpl::disconnectBuffer()
212 {
213         errorConnection_.disconnect();
214         messageConnection_.disconnect();
215         busyConnection_.disconnect();
216         titleConnection_.disconnect();
217         timerConnection_.disconnect();
218         readonlyConnection_.disconnect();
219         closingConnection_.disconnect();
220 }
221
222
223 void BufferView::Pimpl::newFile(string const & filename, string const & tname,
224         bool isNamed)
225 {
226         setBuffer(::newFile(filename, tname, isNamed));
227 }
228
229
230 bool BufferView::Pimpl::loadLyXFile(string const & filename, bool tolastfiles)
231 {
232         // get absolute path of file and add ".lyx" to the filename if
233         // necessary
234         string s = FileSearch(string(), filename, "lyx");
235
236         bool const found = !s.empty();
237
238         if (!found)
239                 s = filename;
240
241         // file already open?
242         if (bufferlist.exists(s)) {
243                 string const file = MakeDisplayPath(s, 20);
244                 string text = bformat(_("The document %1$s is already "
245                                         "loaded.\n\nDo you want to revert "
246                                         "to the saved version?"), file);
247                 int const ret = Alert::prompt(_("Revert to saved document?"),
248                         text, 0, 1,  _("&Revert"), _("&Switch to document"));
249
250                 if (ret != 0) {
251                         setBuffer(bufferlist.getBuffer(s));
252                         return true;
253                 }
254                 // FIXME: should be LFUN_REVERT
255                 if (!bufferlist.close(bufferlist.getBuffer(s), false))
256                         return false;
257                 // Fall through to new load. (Asger)
258         }
259
260         Buffer * b;
261
262         if (found) {
263                 b = bufferlist.newBuffer(s);
264                 connectBuffer(*b);
265                 if (!::loadLyXFile(b, s)) {
266                         bufferlist.release(b);
267                         return false;
268                 }
269         } else {
270                 string text = bformat(_("The document %1$s does not yet "
271                                         "exist.\n\nDo you want to create "
272                                         "a new document?"), s);
273                 int const ret = Alert::prompt(_("Create new document?"),
274                          text, 0, 1, _("&Create"), _("Cancel"));
275
276                 if (ret == 0)
277                         b = ::newFile(s, string(), true);
278                 else
279                         return false;
280         }
281
282         setBuffer(b);
283         bv_->showErrorList(_("Parse"));
284
285         if (tolastfiles)
286                 LyX::ref().lastfiles().newFile(b->fileName());
287
288         return true;
289 }
290
291
292 WorkArea & BufferView::Pimpl::workarea() const
293 {
294         return *workarea_.get();
295 }
296
297
298 LyXScreen & BufferView::Pimpl::screen() const
299 {
300         return *screen_.get();
301 }
302
303
304 Painter & BufferView::Pimpl::painter() const
305 {
306         return workarea().getPainter();
307 }
308
309
310 void BufferView::Pimpl::top_y(int y)
311 {
312         top_y_ = y;
313 }
314
315
316 int BufferView::Pimpl::top_y() const
317 {
318         return top_y_;
319 }
320
321
322 void BufferView::Pimpl::setBuffer(Buffer * b)
323 {
324         lyxerr[Debug::INFO] << "Setting buffer in BufferView ("
325                             << b << ')' << endl;
326         if (buffer_)
327                 disconnectBuffer();
328
329         // set current buffer
330         buffer_ = b;
331
332         // reset old cursor
333         top_y_ = 0;
334         cursor_ = LCursor(*bv_);
335
336         // if we're quitting lyx, don't bother updating stuff
337         if (quitting)
338                 return;
339
340         if (buffer_) {
341                 lyxerr[Debug::INFO] << "Buffer addr: " << buffer_ << endl;
342                 connectBuffer(*buffer_);
343
344                 cursor_.push(buffer_->inset());
345                 cursor_.resetAnchor();
346                 buffer_->text().init(bv_);
347                 buffer_->text().setCurrentFont(cursor_);
348
349                 // If we don't have a text object for this, we make one
350                 //if (bv_->text() == 0)
351                 //      resizeCurrentBuffer();
352
353                 // Buffer-dependent dialogs should be updated or
354                 // hidden. This should go here because some dialogs (eg ToC)
355                 // require bv_->text.
356                 owner_->getDialogs().updateBufferDependent(true);
357         } else {
358                 lyxerr[Debug::INFO] << "  No Buffer!" << endl;
359                 // we are closing the buffer, use the first buffer as current
360                 buffer_ = bufferlist.first();
361                 owner_->getDialogs().hideBufferDependent();
362         }
363
364         update();
365         updateScrollbar();
366         owner_->updateMenubar();
367         owner_->updateToolbars();
368         owner_->updateLayoutChoice();
369         owner_->updateWindowTitle();
370
371         // This is done after the layout combox has been populated
372         if (buffer_)
373                 owner_->setLayout(cursor_.paragraph().layout()->name());
374
375         if (buffer_ && lyx::graphics::Previews::status() != LyXRC::PREVIEW_OFF)
376                 lyx::graphics::Previews::get().generateBufferPreviews(*buffer_);
377 }
378
379
380 bool BufferView::Pimpl::fitCursor()
381 {
382         // to get the correct y cursor info
383         lyxerr[Debug::DEBUG] << "BufferView::fitCursor" << std::endl;
384         lyx::par_type const pit = bv_->cursor().bottom().par();
385         bv_->text()->redoParagraph(pit);
386         refreshPar(*bv_, *bv_->text(), pit);
387
388         if (!screen().fitCursor(bv_))
389                 return false;
390         updateScrollbar();
391         return true;
392 }
393
394
395 void BufferView::Pimpl::redoCurrentBuffer()
396 {
397         lyxerr[Debug::DEBUG] << "BufferView::redoCurrentBuffer" << endl;
398         if (buffer_ && bv_->text()) {
399                 resizeCurrentBuffer();
400                 updateScrollbar();
401                 owner_->updateLayoutChoice();
402         }
403 }
404
405
406 void BufferView::Pimpl::resizeCurrentBuffer()
407 {
408         lyxerr[Debug::DEBUG] << "resizeCurrentBuffer" << endl;
409         owner_->busy(true);
410         owner_->message(_("Formatting document..."));
411
412         LyXText * text = bv_->text();
413         if (!text)
414                 return;
415
416         text->init(bv_);
417         update();
418         fitCursor();
419
420         switchKeyMap();
421         owner_->busy(false);
422
423         // reset the "Formatting..." message
424         owner_->clearMessage();
425
426         updateScrollbar();
427 }
428
429
430 void BufferView::Pimpl::updateScrollbar()
431 {
432         if (!bv_->text()) {
433                 lyxerr[Debug::DEBUG] << "no text in updateScrollbar" << endl;
434                 workarea().setScrollbarParams(0, 0, 0);
435                 return;
436         }
437
438         LyXText const & t = *bv_->text();
439
440         lyxerr[Debug::GUI]
441                 << "Updating scrollbar: height: " << t.height()
442                 << " top_y: " << top_y()
443                 << " default height " << defaultRowHeight() << endl;
444
445         workarea().setScrollbarParams(t.height(), top_y(), defaultRowHeight());
446 }
447
448
449 void BufferView::Pimpl::scrollDocView(int value)
450 {
451         lyxerr[Debug::GUI] << "scrollDocView of " << value << endl;
452
453         if (!buffer_)
454                 return;
455
456         screen().hideCursor();
457
458         top_y(value);
459         screen().redraw(*bv_);
460
461         if (!lyxrc.cursor_follows_scrollbar)
462                 return;
463
464         int const height = defaultRowHeight();
465         int const first = top_y() + height;
466         int const last = top_y() + workarea().workHeight() - height;
467
468         bv_->cursor().reset(bv_->buffer()->inset());
469         LyXText * text = bv_->text();
470         int y = text->cursorY(bv_->cursor().front());
471         if (y < first)
472                 y = first;
473         if (y > last)
474                 y = last;
475         text->setCursorFromCoordinates(bv_->cursor(), 0, y);
476
477         owner_->updateLayoutChoice();
478 }
479
480
481 void BufferView::Pimpl::scroll(int lines)
482 {
483         if (!buffer_)
484                 return;
485
486         LyXText const * t = bv_->text();
487         int const line_height = defaultRowHeight();
488
489         // The new absolute coordinate
490         int new_top_y = top_y() + lines * line_height;
491
492         // Restrict to a valid value
493         new_top_y = std::min(t->height() - 4 * line_height, new_top_y);
494         new_top_y = std::max(0, new_top_y);
495
496         scrollDocView(new_top_y);
497
498         // Update the scrollbar.
499         workarea().setScrollbarParams(t->height(), top_y(), defaultRowHeight());
500 }
501
502
503 void BufferView::Pimpl::workAreaKeyPress(LyXKeySymPtr key,
504                                          key_modifier::state state)
505 {
506         bv_->owner()->getLyXFunc().processKeySym(key, state);
507
508         /* This is perhaps a bit of a hack. When we move
509          * around, or type, it's nice to be able to see
510          * the cursor immediately after the keypress. So
511          * we reset the toggle timeout and force the visibility
512          * of the cursor. Note we cannot do this inside
513          * dispatch() itself, because that's called recursively.
514          */
515         if (available()) {
516                 cursor_timeout.restart();
517                 screen().showCursor(*bv_);
518         }
519 }
520
521
522 void BufferView::Pimpl::selectionRequested()
523 {
524         static string sel;
525
526         if (!available())
527                 return;
528
529         LCursor & cur = bv_->cursor();
530
531         if (!cur.selection()) {
532                 xsel_cache_.set = false;
533                 return;
534         }
535
536         if (!xsel_cache_.set ||
537             cur.back() != xsel_cache_.cursor ||
538             cur.anchor_.back() != xsel_cache_.anchor)
539         {
540                 xsel_cache_.cursor = cur.back();
541                 xsel_cache_.anchor = cur.anchor_.back();
542                 xsel_cache_.set = cur.selection();
543                 sel = cur.selectionAsString(false);
544                 if (!sel.empty())
545                         workarea().putClipboard(sel);
546         }
547 }
548
549
550 void BufferView::Pimpl::selectionLost()
551 {
552         if (available()) {
553                 screen().hideCursor();
554                 bv_->cursor().clearSelection();
555                 xsel_cache_.set = false;
556         }
557 }
558
559
560 void BufferView::Pimpl::workAreaResize()
561 {
562         static int work_area_width;
563         static int work_area_height;
564
565         bool const widthChange = workarea().workWidth() != work_area_width;
566         bool const heightChange = workarea().workHeight() != work_area_height;
567
568         // update from work area
569         work_area_width = workarea().workWidth();
570         work_area_height = workarea().workHeight();
571
572         if (buffer_ && widthChange) {
573                 // The visible LyXView need a resize
574                 resizeCurrentBuffer();
575         }
576
577         if (widthChange || heightChange)
578                 update();
579
580         // always make sure that the scrollbar is sane.
581         updateScrollbar();
582         owner_->updateLayoutChoice();
583 }
584
585
586 void BufferView::Pimpl::update()
587 {
588         //lyxerr << "BufferView::Pimpl::update(), buffer: " << buffer_ << endl;
589         // fix cursor coordinate cache in case something went wrong
590
591         // check needed to survive LyX startup
592         if (buffer_) {
593                 // update macro store
594                 buffer_->buildMacros();
595
596                 // update all 'visible' paragraphs
597                 lyx::par_type beg, end;
598                 getParsInRange(buffer_->paragraphs(),
599                                top_y(), top_y() + workarea().workHeight(),
600                                beg, end);
601                 bv_->text()->redoParagraphs(beg, end);
602
603                 // and the scrollbar
604                 updateScrollbar();
605         }
606
607         // remove old position cache
608         theCoords.clear();
609
610         // The real, big redraw.
611         screen().redraw(*bv_);
612
613         bv_->owner()->view_state_changed();
614 }
615
616
617 // Callback for cursor timer
618 void BufferView::Pimpl::cursorToggle()
619 {
620         if (buffer_) {
621                 screen().toggleCursor(*bv_);
622
623                 // Use this opportunity to deal with any child processes that
624                 // have finished but are waiting to communicate this fact
625                 // to the rest of LyX.
626                 ForkedcallsController & fcc = ForkedcallsController::get();
627                 if (fcc.processesCompleted())
628                         fcc.handleCompletedProcesses();
629         }
630
631         cursor_timeout.restart();
632 }
633
634
635 bool BufferView::Pimpl::available() const
636 {
637         return buffer_ && bv_->text();
638 }
639
640
641 Change const BufferView::Pimpl::getCurrentChange()
642 {
643         if (!bv_->buffer()->params().tracking_changes)
644                 return Change(Change::UNCHANGED);
645
646         LyXText * text = bv_->getLyXText();
647         LCursor & cur = bv_->cursor();
648
649         if (!cur.selection())
650                 return Change(Change::UNCHANGED);
651
652         return text->getPar(cur.selBegin().par()).
653                         lookupChangeFull(cur.selBegin().pos());
654 }
655
656
657 void BufferView::Pimpl::savePosition(unsigned int i)
658 {
659         if (i >= saved_positions_num)
660                 return;
661         BOOST_ASSERT(bv_->cursor().inTexted());
662         saved_positions[i] = Position(buffer_->fileName(),
663                                       bv_->cursor().paragraph().id(),
664                                       bv_->cursor().pos());
665         if (i > 0)
666                 owner_->message(bformat(_("Saved bookmark %1$s"), tostr(i)));
667 }
668
669
670 void BufferView::Pimpl::restorePosition(unsigned int i)
671 {
672         if (i >= saved_positions_num)
673                 return;
674
675         string const fname = saved_positions[i].filename;
676
677         bv_->cursor().clearSelection();
678
679         if (fname != buffer_->fileName()) {
680                 Buffer * b = 0;
681                 if (bufferlist.exists(fname))
682                         b = bufferlist.getBuffer(fname);
683                 else {
684                         b = bufferlist.newBuffer(fname);
685                         ::loadLyXFile(b, fname); // don't ask, just load it
686                 }
687                 if (b)
688                         setBuffer(b);
689         }
690
691         ParIterator par = buffer_->getParFromID(saved_positions[i].par_id);
692         if (par == buffer_->par_iterator_end())
693                 return;
694
695         bv_->text()->setCursor(bv_->cursor(), par.pit(),
696                 min(par->size(), saved_positions[i].par_pos));
697
698         if (i > 0)
699                 owner_->message(bformat(_("Moved to bookmark %1$s"), tostr(i)));
700 }
701
702
703 bool BufferView::Pimpl::isSavedPosition(unsigned int i)
704 {
705         return i < saved_positions_num && !saved_positions[i].filename.empty();
706 }
707
708
709 void BufferView::Pimpl::switchKeyMap()
710 {
711         if (!lyxrc.rtl_support)
712                 return;
713
714         Intl & intl = owner_->getIntl();
715         if (bv_->getLyXText()->real_current_font.isRightToLeft()) {
716                 if (intl.keymap == Intl::PRIMARY)
717                         intl.KeyMapSec();
718         } else {
719                 if (intl.keymap == Intl::SECONDARY)
720                         intl.KeyMapPrim();
721         }
722 }
723
724
725 void BufferView::Pimpl::center()
726 {
727         LyXText * text = bv_->text();
728
729         bv_->cursor().clearSelection();
730         int const half_height = workarea().workHeight() / 2;
731         int new_y = text->cursorY(bv_->cursor().front()) - half_height;
732         if (new_y < 0)
733                 new_y = 0;
734
735         // FIXME: look at this comment again ...
736         // This updates top_y() but means the fitCursor() call
737         // from the update(FITCUR) doesn't realise that we might
738         // have moved (e.g. from GOTOPARAGRAPH), so doesn't cause
739         // the scrollbar to be updated as it should, so we have
740         // to do it manually. Any operation that does a center()
741         // and also might have moved top_y() must make sure to call
742         // updateScrollbar() currently. Never mind that this is a
743         // pretty obfuscated way of updating text->top_y()
744         top_y(new_y);
745 }
746
747
748 void BufferView::Pimpl::stuffClipboard(string const & stuff) const
749 {
750         workarea().putClipboard(stuff);
751 }
752
753
754 void BufferView::Pimpl::MenuInsertLyXFile(string const & filenm)
755 {
756         string filename = filenm;
757
758         if (filename.empty()) {
759                 // Launch a file browser
760                 string initpath = lyxrc.document_path;
761
762                 if (available()) {
763                         string const trypath = owner_->buffer()->filePath();
764                         // If directory is writeable, use this as default.
765                         if (IsDirWriteable(trypath))
766                                 initpath = trypath;
767                 }
768
769                 FileDialog fileDlg(_("Select LyX document to insert"),
770                         LFUN_FILE_INSERT,
771                         make_pair(string(_("Documents|#o#O")),
772                                   string(lyxrc.document_path)),
773                         make_pair(string(_("Examples|#E#e")),
774                                   string(AddPath(system_lyxdir(), "examples"))));
775
776                 FileDialog::Result result =
777                         fileDlg.open(initpath,
778                                      FileFilterList(_("LyX Documents (*.lyx)")),
779                                      string());
780
781                 if (result.first == FileDialog::Later)
782                         return;
783
784                 filename = result.second;
785
786                 // check selected filename
787                 if (filename.empty()) {
788                         owner_->message(_("Canceled."));
789                         return;
790                 }
791         }
792
793         // get absolute path of file and add ".lyx" to the filename if
794         // necessary
795         filename = FileSearch(string(), filename, "lyx");
796
797         string const disp_fn = MakeDisplayPath(filename);
798         owner_->message(bformat(_("Inserting document %1$s..."), disp_fn));
799         if (bv_->insertLyXFile(filename))
800                 owner_->message(bformat(_("Document %1$s inserted."),
801                                         disp_fn));
802         else
803                 owner_->message(bformat(_("Could not insert document %1$s"),
804                                         disp_fn));
805 }
806
807
808 void BufferView::Pimpl::trackChanges()
809 {
810         Buffer * buf = bv_->buffer();
811         bool const tracking = buf->params().tracking_changes;
812
813         if (!tracking) {
814                 ParIterator const end = buf->par_iterator_end();
815                 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
816                         it->trackChanges();
817                 buf->params().tracking_changes = true;
818
819                 // we cannot allow undos beyond the freeze point
820                 buf->undostack().clear();
821         } else {
822                 update();
823                 bv_->text()->setCursor(bv_->cursor(), 0, 0);
824 #ifdef WITH_WARNINGS
825 #warning changes FIXME
826 #endif
827                 bool found = lyx::find::findNextChange(bv_);
828                 if (found) {
829                         owner_->getDialogs().show("changes");
830                         return;
831                 }
832
833                 ParIterator const end = buf->par_iterator_end();
834                 for (ParIterator it = buf->par_iterator_begin(); it != end; ++it)
835                         it->untrackChanges();
836                 buf->params().tracking_changes = false;
837         }
838
839         buf->redostack().clear();
840 }
841
842
843 bool BufferView::Pimpl::workAreaDispatch(FuncRequest const & cmd0)
844 {
845         lyxerr << "BufferView::Pimpl::workAreaDispatch: request: "
846           << cmd0 << std::endl;
847         // this is only called for mouse related events including
848         // LFUN_FILE_OPEN generated by drag-and-drop.
849         FuncRequest cmd = cmd0;
850
851         // handle drag&drop
852         if (cmd.action == LFUN_FILE_OPEN) {
853                 owner_->dispatch(cmd);
854                 return true;
855         }
856
857         cmd.y += bv_->top_y();
858         if (!bv_->buffer())
859                 return false;
860
861         LCursor cur(*bv_);
862         cur.push(bv_->buffer()->inset());
863         cur.selection() = bv_->cursor().selection();
864
865         // Doesn't go through lyxfunc, so we need to update
866         // the layout choice etc. ourselves
867
868         // e.g. Qt mouse press when no buffer
869         if (!available())
870                 return false;
871
872         screen().hideCursor();
873
874         // Either the inset under the cursor or the
875         // surrounding LyXText will handle this event.
876
877         // Build temporary cursor.
878         InsetBase * inset = bv_->text()->editXY(cur, cmd.x, cmd.y);
879         lyxerr << "hit inset at tip: " << inset << endl;
880         lyxerr << "created temp cursor:\n" << cur << endl;
881
882         // Put anchor at the same position.
883         cur.resetAnchor();
884
885         // Try to dispatch to an non-editable inset near this position
886         // via the temp cursor. If the inset wishes to change the real
887         // cursor it has to do so explicitly by using
888         //  cur.bv().cursor() = cur;  (or similar)'
889         if (inset)
890                 inset->dispatch(cur, cmd);
891
892         // Now dispatch to the temporary cursor. If the real cursor should
893         // be modified, the inset's dispatch has to do so explicitly.
894         if (!cur.result().dispatched())
895                 cur.dispatch(cmd);
896
897         if (cur.result().dispatched()) {
898                 // Redraw if requested or necessary.
899                 if (fitCursor() || cur.result().update())
900                         update();
901         }
902
903         // see workAreaKeyPress
904         cursor_timeout.restart();
905         screen().showCursor(*bv_);
906
907         // skip these when selecting
908         if (cmd.action != LFUN_MOUSE_MOTION) {
909                 owner_->updateLayoutChoice();
910                 owner_->updateToolbars();
911         }
912
913         // slight hack: this is only called currently when we
914         // clicked somewhere, so we force through the display
915         // of the new status here.
916         owner_->clearMessage();
917         return true;
918 }
919
920
921 FuncStatus BufferView::Pimpl::getStatus(FuncRequest const & cmd)
922 {
923         Buffer * buf = bv_->buffer();
924
925         FuncStatus flag;
926
927         switch (cmd.action) {
928
929         case LFUN_UNDO:
930                 flag.enabled(!buf->undostack().empty());
931                 break;
932         case LFUN_REDO:
933                 flag.enabled(!buf->redostack().empty());
934                 break;
935         case LFUN_FILE_INSERT:
936         case LFUN_FILE_INSERT_ASCII_PARA:
937         case LFUN_FILE_INSERT_ASCII:
938         case LFUN_FONT_STATE:
939         case LFUN_INSERT_LABEL:
940         case LFUN_BOOKMARK_SAVE:
941         case LFUN_REF_GOTO:
942         case LFUN_WORD_FIND:
943         case LFUN_WORD_REPLACE:
944         case LFUN_MARK_OFF:
945         case LFUN_MARK_ON:
946         case LFUN_SETMARK:
947         case LFUN_CENTER:
948         case LFUN_BEGINNINGBUF:
949         case LFUN_ENDBUF:
950         case LFUN_BEGINNINGBUFSEL:
951         case LFUN_ENDBUFSEL:
952                 flag.enabled(true);
953                 break;
954         case LFUN_BOOKMARK_GOTO:
955                 flag.enabled(bv_->isSavedPosition(strToUnsignedInt(cmd.argument)));
956                 break;
957         case LFUN_TRACK_CHANGES:
958                 flag.enabled(true);
959                 flag.setOnOff(buf->params().tracking_changes);
960                 break;
961
962         case LFUN_MERGE_CHANGES:
963         case LFUN_ACCEPT_CHANGE: // what about these two
964         case LFUN_REJECT_CHANGE: // what about these two
965         case LFUN_ACCEPT_ALL_CHANGES:
966         case LFUN_REJECT_ALL_CHANGES:
967                 flag.enabled(buf && buf->params().tracking_changes);
968                 break;
969         default:
970                 flag.enabled(false);
971         }
972
973         return flag;
974 }
975
976
977
978 bool BufferView::Pimpl::dispatch(FuncRequest const & cmd)
979 {
980         //lyxerr << "BufferView::Pimpl::dispatch  cmd: " << cmd << std::endl;
981         // Make sure that the cached BufferView is correct.
982         lyxerr[Debug::ACTION] << "BufferView::Pimpl::Dispatch:"
983                 << " action[" << cmd.action << ']'
984                 << " arg[" << cmd.argument << ']'
985                 << " x[" << cmd.x << ']'
986                 << " y[" << cmd.y << ']'
987                 << " button[" << cmd.button() << ']'
988                 << endl;
989
990         LCursor & cur = bv_->cursor();
991
992         switch (cmd.action) {
993
994         case LFUN_UNDO:
995                 if (available()) {
996                         cur.message(_("Undo"));
997                         cur.clearSelection();
998                         if (!textUndo(*bv_))
999                                 cur.message(_("No further undo information"));
1000                         update();
1001                         switchKeyMap();
1002                 }
1003                 break;
1004
1005         case LFUN_REDO:
1006                 if (available()) {
1007                         cur.message(_("Redo"));
1008                         cur.clearSelection();
1009                         if (!textRedo(*bv_))
1010                                 cur.message(_("No further redo information"));
1011                         update();
1012                         switchKeyMap();
1013                 }
1014                 break;
1015
1016         case LFUN_FILE_INSERT:
1017                 MenuInsertLyXFile(cmd.argument);
1018                 break;
1019
1020         case LFUN_FILE_INSERT_ASCII_PARA:
1021                 InsertAsciiFile(bv_, cmd.argument, true);
1022                 break;
1023
1024         case LFUN_FILE_INSERT_ASCII:
1025                 InsertAsciiFile(bv_, cmd.argument, false);
1026                 break;
1027
1028         case LFUN_FONT_STATE:
1029                 cur.message(cur.currentState());
1030                 break;
1031
1032         case LFUN_BOOKMARK_SAVE:
1033                 savePosition(strToUnsignedInt(cmd.argument));
1034                 break;
1035
1036         case LFUN_BOOKMARK_GOTO:
1037                 restorePosition(strToUnsignedInt(cmd.argument));
1038                 break;
1039
1040         case LFUN_REF_GOTO: {
1041                 string label = cmd.argument;
1042                 if (label.empty()) {
1043                         InsetRef * inset =
1044                                 getInsetByCode<InsetRef>(bv_->cursor(),
1045                                                          InsetBase::REF_CODE);
1046                         if (inset) {
1047                                 label = inset->getContents();
1048                                 savePosition(0);
1049                         }
1050                 }
1051
1052                 if (!label.empty())
1053                         bv_->gotoLabel(label);
1054                 break;
1055         }
1056
1057         case LFUN_TRACK_CHANGES:
1058                 trackChanges();
1059                 break;
1060
1061         case LFUN_MERGE_CHANGES:
1062                 owner_->getDialogs().show("changes");
1063                 break;
1064
1065         case LFUN_ACCEPT_ALL_CHANGES: {
1066                 bv_->cursor().reset(bv_->buffer()->inset());
1067 #ifdef WITH_WARNINGS
1068 #warning FIXME changes
1069 #endif
1070                 while (lyx::find::findNextChange(bv_))
1071                         bv_->getLyXText()->acceptChange(bv_->cursor());
1072                 update();
1073                 break;
1074         }
1075
1076         case LFUN_REJECT_ALL_CHANGES: {
1077                 bv_->cursor().reset(bv_->buffer()->inset());
1078 #ifdef WITH_WARNINGS
1079 #warning FIXME changes
1080 #endif
1081                 while (lyx::find::findNextChange(bv_))
1082                         bv_->getLyXText()->rejectChange(bv_->cursor());
1083                 break;
1084         }
1085
1086         case LFUN_WORD_FIND:
1087                 lyx::find::find(bv_, cmd);
1088                 break;
1089
1090         case LFUN_WORD_REPLACE:
1091                 lyx::find::replace(bv_, cmd);
1092                 break;
1093
1094         case LFUN_MARK_OFF:
1095                 cur.clearSelection();
1096                 cur.resetAnchor();
1097                 cur.message(N_("Mark off"));
1098                 break;
1099
1100         case LFUN_MARK_ON:
1101                 cur.clearSelection();
1102                 cur.mark() = true;
1103                 cur.resetAnchor();
1104                 cur.message(N_("Mark on"));
1105                 break;
1106
1107         case LFUN_SETMARK:
1108                 cur.clearSelection();
1109                 if (cur.mark()) {
1110                         cur.mark() = false;
1111                         cur.message(N_("Mark removed"));
1112                 } else {
1113                         cur.mark() = true;
1114                         cur.message(N_("Mark set"));
1115                 }
1116                 cur.resetAnchor();
1117                 break;
1118
1119         case LFUN_CENTER:
1120                 bv_->center();
1121                 break;
1122
1123         case LFUN_BEGINNINGBUFSEL:
1124                 bv_->cursor().reset(bv_->buffer()->inset());
1125                 if (!cur.selection())
1126                         cur.resetAnchor();
1127                 bv_->text()->cursorTop(cur);
1128                 finishUndo();
1129                 break;
1130
1131         case LFUN_ENDBUFSEL:
1132                 bv_->cursor().reset(bv_->buffer()->inset());
1133                 if (!cur.selection())
1134                         cur.resetAnchor();
1135                 bv_->text()->cursorBottom(cur);
1136                 finishUndo();
1137                 break;
1138
1139         default:
1140                 return false;
1141         }
1142
1143         return true;
1144 }