]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
476c6a207389b16d71562d4ed2f810f6a8266c68
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * Copyright 1995-2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Lars Gullik Bjønnes
7  * \author John Levon <moz@compsoc.man.ac.uk>
8  */
9
10 #include <config.h>
11
12 #include "BufferView.h"
13 #include "BufferView_pimpl.h"
14
15 #include "LaTeX.h"
16 #include "ParagraphParameters.h"
17 #include "WordLangTuple.h"
18 #include "buffer.h"
19 #include "bufferlist.h"
20 #include "debug.h"
21 #include "gettext.h"
22 #include "errorlist.h"
23 #include "iterators.h"
24 #include "language.h"
25 #include "lyxcursor.h"
26 #include "lyxlex.h"
27 #include "lyxtext.h"
28 #include "undo_funcs.h"
29 #include "changes.h"
30 #include "paragraph_funcs.h"
31
32 #include "frontends/Alert.h"
33 #include "frontends/Dialogs.h"
34 #include "frontends/LyXView.h"
35 #include "frontends/WorkArea.h"
36 #include "frontends/screen.h"
37
38 #include "insets/insetcommand.h" // ChangeRefs
39 #include "insets/updatableinset.h"
40
41 #include "support/FileInfo.h"
42 #include "support/filetools.h"
43 #include "support/types.h"
44 #include "support/lyxalgo.h" // lyx_count
45
46 #include <fstream>
47
48 extern BufferList bufferlist;
49
50 using lyx::pos_type;
51 using namespace lyx::support;
52
53 using std::pair;
54 using std::endl;
55 using std::ifstream;
56 using std::vector;
57 using std::find;
58 using std::count_if;
59
60
61 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
62                        int width, int height)
63         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height))
64 {
65         text = 0;
66 }
67
68
69 BufferView::~BufferView()
70 {
71         delete text;
72         delete pimpl_;
73 }
74
75
76 Buffer * BufferView::buffer() const
77 {
78         return pimpl_->buffer_;
79 }
80
81
82 LyXScreen & BufferView::screen() const
83 {
84         return pimpl_->screen();
85 }
86
87
88 LyXView * BufferView::owner() const
89 {
90         return pimpl_->owner_;
91 }
92
93
94 Painter & BufferView::painter() const
95 {
96         return pimpl_->painter();
97 }
98
99
100 void BufferView::buffer(Buffer * b)
101 {
102         pimpl_->buffer(b);
103 }
104
105
106 bool BufferView::newFile(string const & fn, string const & tn, bool named)
107 {
108         return pimpl_->newFile(fn, tn, named);
109 }
110
111
112 bool BufferView::loadLyXFile(string const & fn, bool tl)
113 {
114         return pimpl_->loadLyXFile(fn, tl);
115 }
116
117
118 void BufferView::reload()
119 {
120         string const fn = buffer()->fileName();
121         if (bufferlist.close(buffer(), false))
122                 loadLyXFile(fn);
123 }
124
125
126 void BufferView::resize()
127 {
128         if (pimpl_->buffer_)
129                 pimpl_->resizeCurrentBuffer();
130 }
131
132
133 bool BufferView::fitCursor()
134 {
135         return pimpl_->fitCursor();
136 }
137
138
139 void BufferView::update()
140 {
141         pimpl_->update();
142 }
143
144
145 void BufferView::updateScrollbar()
146 {
147         pimpl_->updateScrollbar();
148 }
149
150
151 void BufferView::scrollDocView(int value)
152 {
153         pimpl_->scrollDocView(value);
154 }
155
156
157 void BufferView::redoCurrentBuffer()
158 {
159         pimpl_->redoCurrentBuffer();
160 }
161
162
163 bool BufferView::available() const
164 {
165         return pimpl_->available();
166 }
167
168
169 Change const BufferView::getCurrentChange()
170 {
171         return pimpl_->getCurrentChange();
172 }
173
174
175 void BufferView::beforeChange(LyXText * text)
176 {
177         pimpl_->beforeChange(text);
178 }
179
180
181 void BufferView::savePosition(unsigned int i)
182 {
183         pimpl_->savePosition(i);
184 }
185
186
187 void BufferView::restorePosition(unsigned int i)
188 {
189         pimpl_->restorePosition(i);
190 }
191
192
193 bool BufferView::isSavedPosition(unsigned int i)
194 {
195         return pimpl_->isSavedPosition(i);
196 }
197
198
199 void BufferView::switchKeyMap()
200 {
201         pimpl_->switchKeyMap();
202 }
203
204
205 void BufferView::insetUnlock()
206 {
207         pimpl_->insetUnlock();
208 }
209
210
211 int BufferView::workWidth() const
212 {
213         return pimpl_->workarea().workWidth();
214 }
215
216
217 void BufferView::toggleSelection(bool b)
218 {
219         pimpl_->toggleSelection(b);
220 }
221
222
223 void BufferView::center()
224 {
225         pimpl_->center();
226 }
227
228
229 string const BufferView::getClipboard() const
230 {
231         return pimpl_->workarea().getClipboard();
232 }
233
234
235 void BufferView::stuffClipboard(string const & stuff) const
236 {
237         pimpl_->stuffClipboard(stuff);
238 }
239
240
241 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
242                                   BufferView::UpdateCodes uc2)
243 {
244         return static_cast<BufferView::UpdateCodes>
245                 (static_cast<int>(uc1) | static_cast<int>(uc2));
246 }
247
248
249 bool BufferView::dispatch(FuncRequest const & ev)
250 {
251         return pimpl_->dispatch(ev);
252 }
253
254
255 void BufferView::scroll(int lines)
256 {
257         pimpl_->scroll(lines);
258 }
259
260
261 // Inserts a file into current document
262 bool BufferView::insertLyXFile(string const & filen)
263         //
264         // Copyright CHT Software Service GmbH
265         // Uwe C. Schroeder
266         //
267         // Insert a LyXformat - file into current buffer
268         //
269         // Moved from lyx_cb.C (Lgb)
270 {
271         if (filen.empty())
272                 return false;
273
274         string const fname = MakeAbsPath(filen);
275
276         // check if file exist
277         FileInfo const fi(fname);
278
279         if (!fi.readable()) {
280                 string const file = MakeDisplayPath(fname, 50);
281                 string const text =
282                         bformat(_("The specified document\n%1$s\ncould not be read."), file);
283                 Alert::error(_("Could not read document"), text);
284                 return false;
285         }
286
287         beforeChange(text);
288
289         ifstream ifs(fname.c_str());
290         if (!ifs) {
291                 string const file = MakeDisplayPath(fname, 50);
292                 string const text =
293                         bformat(_("Could not open the specified document %1$s\n"), file);
294                 Alert::error(_("Could not open file"), text);
295                 return false;
296         }
297
298         int const c = ifs.peek();
299
300         LyXLex lex(0, 0);
301         lex.setStream(ifs);
302
303         bool res = true;
304
305         text->breakParagraph(buffer()->paragraphs);
306
307         if (c == '#') {
308                 // FIXME: huh ? No we won't !
309                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
310                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
311         } else {
312                 lyxerr[Debug::INFO] << "Will insert file without header"
313                                     << endl;
314                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
315         }
316
317         resize();
318         return res;
319 }
320
321
322 void BufferView::showErrorList(string const & action) const
323 {
324         if (getErrorList().size()) {
325                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
326                 owner()->getDialogs().show("errorlist", title);
327                 pimpl_->errorlist_.clear();
328         }
329 }
330
331
332 ErrorList const &
333 BufferView::getErrorList() const
334 {
335         return pimpl_->errorlist_;
336 }
337
338
339 void BufferView::setCursorFromRow(int row)
340 {
341         int tmpid = -1;
342         int tmppos = -1;
343
344         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
345
346         ParagraphList::iterator texrowpar;
347
348         if (tmpid == -1) {
349                 texrowpar = text->ownerParagraphs().begin();
350                 tmppos = 0;
351         } else {
352                 texrowpar = buffer()->getParFromID(tmpid).pit();
353         }
354         text->setCursor(texrowpar, tmppos);
355 }
356
357
358 bool BufferView::insertInset(InsetOld * inset, string const & lout)
359 {
360         return pimpl_->insertInset(inset, lout);
361 }
362
363
364 void BufferView::gotoLabel(string const & label)
365 {
366         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
367              it != buffer()->inset_iterator_end(); ++it) {
368                 vector<string> labels;
369                 it->getLabelList(labels);
370                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
371                         beforeChange(text);
372                         text->setCursor(it.getPar(), it.getPos());
373                         text->selection.cursor = text->cursor;
374                         update();
375                         return;
376                 }
377         }
378 }
379
380
381 void BufferView::undo()
382 {
383         if (!available())
384                 return;
385
386         owner()->message(_("Undo"));
387         beforeChange(text);
388         if (!textUndo(this))
389                 owner()->message(_("No further undo information"));
390         update();
391         switchKeyMap();
392 }
393
394
395 void BufferView::redo()
396 {
397         if (!available())
398                 return;
399
400         owner()->message(_("Redo"));
401         beforeChange(text);
402         if (!textRedo(this))
403                 owner()->message(_("No further redo information"));
404         update();
405         switchKeyMap();
406 }
407
408
409 // these functions are for the spellchecker
410 WordLangTuple const BufferView::nextWord(float & value)
411 {
412         if (!available()) {
413                 value = 1;
414                 return WordLangTuple();
415         }
416
417         return text->selectNextWordToSpellcheck(value);
418 }
419
420
421 void BufferView::selectLastWord()
422 {
423         if (!available())
424                 return;
425
426         LyXCursor cur = text->selection.cursor;
427         beforeChange(text);
428         text->selection.cursor = cur;
429         text->selectSelectedWord();
430         toggleSelection(false);
431         update();
432 }
433
434
435 void BufferView::endOfSpellCheck()
436 {
437         if (!available()) return;
438
439         beforeChange(text);
440         text->selectSelectedWord();
441         text->clearSelection();
442         update();
443 }
444
445
446 void BufferView::replaceWord(string const & replacestring)
447 {
448         if (!available())
449                 return;
450
451         LyXText * tt = getLyXText();
452
453         // clear the selection (if there is any)
454         toggleSelection(false);
455
456         // clear the selection (if there is any)
457         toggleSelection(false);
458         tt->replaceSelectionWithString(replacestring);
459
460         tt->setSelectionRange(replacestring.length());
461
462         // Go back so that replacement string is also spellchecked
463         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) 
464                 tt->cursorLeft(this);
465
466         // FIXME: should be done through LFUN
467         buffer()->markDirty();
468         fitCursor();
469         update();
470 }
471
472
473 bool BufferView::lockInset(UpdatableInset * inset)
474 {
475         if (!inset)
476                 return false;
477         // don't relock if we're already locked
478         if (theLockingInset() == inset)
479                 return true;
480         if (!theLockingInset()) {
481                 // first check if it's the inset under the cursor we want lock
482                 // should be most of the time
483                 if (text->cursor.pos() < text->cursor.par()->size()
484                     && text->cursor.par()->getChar(text->cursor.pos()) ==
485                     Paragraph::META_INSET) {
486                         InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
487                         if (inset == in) {
488                                 theLockingInset(inset);
489                                 return true;
490                         }
491                 }
492                 // Then do a deep look of the inset and lock the right one
493                 int const id = inset->id();
494                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
495                 ParagraphList::iterator pend = buffer()->paragraphs.end();
496                 for (; pit != pend; ++pit) {
497                         InsetList::iterator it = pit->insetlist.begin();
498                         InsetList::iterator end = pit->insetlist.end();
499                         for (; it != end; ++it) {
500                                 if (it->inset == inset) {
501                                         text->setCursorIntern(pit, it->pos);
502                                         theLockingInset(inset);
503                                         return true;
504                                 }
505                                 if (it->inset->getInsetFromID(id)) {
506                                         text->setCursorIntern(pit, it->pos);
507                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
508                                         it->inset->localDispatch(cmd);
509                                         return theLockingInset()->lockInsetInInset(this, inset);
510                                 }
511                         }
512                 }
513                 return false;
514         }
515         return theLockingInset()->lockInsetInInset(this, inset);
516 }
517
518
519 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
520 {
521         if (theLockingInset() && available()) {
522                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
523                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
524                         updateScrollbar();
525                         return true;
526                 }
527         }
528         return false;
529 }
530
531
532 void BufferView::hideCursor()
533 {
534         screen().hideCursor();
535 }
536
537
538 int BufferView::unlockInset(UpdatableInset * inset)
539 {
540         if (!inset)
541                 return 0;
542         if (inset && theLockingInset() == inset) {
543                 inset->insetUnlock(this);
544                 theLockingInset(0);
545                 // make sure we update the combo !
546                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
547                 // Tell the paragraph dialog that we changed paragraph
548                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
549                 finishUndo();
550                 return 0;
551         } else if (inset && theLockingInset() &&
552                    theLockingInset()->unlockInsetInInset(this, inset)) {
553                 // Tell the paragraph dialog that we changed paragraph
554                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
555                 // owner inset has updated the layout combo
556                 finishUndo();
557                 return 0;
558         }
559         return 1;
560 }
561
562
563 void BufferView::updateInset(InsetOld * inset)
564 {
565         pimpl_->updateInset(inset);
566 }
567
568
569 bool BufferView::ChangeInsets(InsetOld::Code code,
570                               string const & from, string const & to)
571 {
572         bool need_update = false;
573         LyXCursor cursor = text->cursor;
574         LyXCursor tmpcursor = cursor;
575         cursor.par(tmpcursor.par());
576         cursor.pos(tmpcursor.pos());
577
578         ParIterator end = buffer()->par_iterator_end();
579         for (ParIterator it = buffer()->par_iterator_begin();
580              it != end; ++it) {
581                 bool changed_inset = false;
582                 for (InsetList::iterator it2 = it->insetlist.begin();
583                      it2 != it->insetlist.end(); ++it2) {
584                         if (it2->inset->lyxCode() == code) {
585                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
586                                 if (inset->getContents() == from) {
587                                         inset->setContents(to);
588                                         changed_inset = true;
589                                 }
590                         }
591                 }
592                 if (changed_inset) {
593                         need_update = true;
594
595                         // FIXME
596
597                         // The test it.size()==1 was needed to prevent crashes.
598                         // How to set the cursor corretly when it.size()>1 ??
599                         if (it.size() == 1) {
600                                 text->setCursorIntern(it.pit(), 0);
601                                 text->redoParagraph(text->cursor.par());
602                         }
603                 }
604         }
605         text->setCursorIntern(cursor.par(), cursor.pos());
606         return need_update;
607 }
608
609
610 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
611 {
612         // Check if the label 'from' appears more than once
613         vector<string> labels;
614         buffer()->getLabelList(labels);
615
616         if (lyx::count(labels.begin(), labels.end(), from) > 1)
617                 return false;
618
619         return ChangeInsets(InsetOld::REF_CODE, from, to);
620 }
621
622
623 UpdatableInset * BufferView::theLockingInset() const
624 {
625         // If NULL is not allowed we should put an Assert here. (Lgb)
626         if (text)
627                 return text->the_locking_inset;
628         return 0;
629 }
630
631
632 void BufferView::theLockingInset(UpdatableInset * inset)
633 {
634         text->the_locking_inset = inset;
635 }
636
637
638 LyXText * BufferView::getLyXText() const
639 {
640         if (theLockingInset()) {
641                 LyXText * txt = theLockingInset()->getLyXText(this, true);
642                 if (txt)
643                         return txt;
644         }
645         return text;
646 }
647
648
649 LyXText * BufferView::getParentText(InsetOld * inset) const
650 {
651         if (inset->owner()) {
652                 LyXText * txt = inset->getLyXText(this);
653                 inset = inset->owner();
654                 while (inset && inset->getLyXText(this) == txt)
655                         inset = inset->owner();
656                 if (inset)
657                         return inset->getLyXText(this);
658         }
659         return text;
660 }
661
662
663 Language const * BufferView::getParentLanguage(InsetOld * inset) const
664 {
665         LyXText * text = getParentText(inset);
666         return text->cursor.par()->getFontSettings(buffer()->params,
667                                                    text->cursor.pos()).language();
668 }
669
670
671 Encoding const * BufferView::getEncoding() const
672 {
673         LyXText * t = getLyXText();
674         if (!t)
675                 return 0;
676
677         LyXCursor const & c = t->cursor;
678         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
679                                               outerFont(c.par(), t->ownerParagraphs()));
680         return font.language()->encoding();
681 }
682
683
684 void BufferView::haveSelection(bool sel)
685 {
686         pimpl_->workarea().haveSelection(sel);
687 }
688
689
690 int BufferView::workHeight() const
691 {
692         return pimpl_->workarea().workHeight();
693 }