]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
ce96e909e1230509f76bc83bc1394a2e36692e2b
[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::update(LyXText * text, UpdateCodes f)
200 {
201         pimpl_->update(text, f);
202 }
203
204
205 void BufferView::update(UpdateCodes f)
206 {
207         pimpl_->update(f);
208 }
209
210
211 void BufferView::switchKeyMap()
212 {
213         pimpl_->switchKeyMap();
214 }
215
216
217 void BufferView::insetUnlock()
218 {
219         pimpl_->insetUnlock();
220 }
221
222
223 int BufferView::workWidth() const
224 {
225         return pimpl_->workarea().workWidth();
226 }
227
228
229 void BufferView::toggleSelection(bool b)
230 {
231         pimpl_->toggleSelection(b);
232 }
233
234
235 void BufferView::center()
236 {
237         pimpl_->center();
238 }
239
240
241 string const BufferView::getClipboard() const
242 {
243         return pimpl_->workarea().getClipboard();
244 }
245
246
247 void BufferView::stuffClipboard(string const & stuff) const
248 {
249         pimpl_->stuffClipboard(stuff);
250 }
251
252
253 BufferView::UpdateCodes operator|(BufferView::UpdateCodes uc1,
254                                   BufferView::UpdateCodes uc2)
255 {
256         return static_cast<BufferView::UpdateCodes>
257                 (static_cast<int>(uc1) | static_cast<int>(uc2));
258 }
259
260
261 bool BufferView::dispatch(FuncRequest const & ev)
262 {
263         return pimpl_->dispatch(ev);
264 }
265
266
267 void BufferView::scroll(int lines)
268 {
269         pimpl_->scroll(lines);
270 }
271
272
273 // Inserts a file into current document
274 bool BufferView::insertLyXFile(string const & filen)
275         //
276         // Copyright CHT Software Service GmbH
277         // Uwe C. Schroeder
278         //
279         // Insert a LyXformat - file into current buffer
280         //
281         // Moved from lyx_cb.C (Lgb)
282 {
283         if (filen.empty())
284                 return false;
285
286         string const fname = MakeAbsPath(filen);
287
288         // check if file exist
289         FileInfo const fi(fname);
290
291         if (!fi.readable()) {
292                 string const file = MakeDisplayPath(fname, 50);
293                 string const text =
294                         bformat(_("The specified document\n%1$s\ncould not be read."), file);
295                 Alert::error(_("Could not read document"), text);
296                 return false;
297         }
298
299         beforeChange(text);
300
301         ifstream ifs(fname.c_str());
302         if (!ifs) {
303                 string const file = MakeDisplayPath(fname, 50);
304                 string const text =
305                         bformat(_("Could not open the specified document %1$s\n"), file);
306                 Alert::error(_("Could not open file"), text);
307                 return false;
308         }
309
310         int const c = ifs.peek();
311
312         LyXLex lex(0, 0);
313         lex.setStream(ifs);
314
315         bool res = true;
316
317         text->breakParagraph(buffer()->paragraphs);
318
319         if (c == '#') {
320                 // FIXME: huh ? No we won't !
321                 lyxerr[Debug::INFO] << "Will insert file with header" << endl;
322                 res = buffer()->readFile(lex, fname, ParagraphList::iterator(text->cursor.par()));
323         } else {
324                 lyxerr[Debug::INFO] << "Will insert file without header"
325                                     << endl;
326                 res = buffer()->readBody(lex, ParagraphList::iterator(text->cursor.par()));
327         }
328
329         resize();
330         return res;
331 }
332
333
334 void BufferView::showErrorList(string const & action) const
335 {
336         if (getErrorList().size()) {
337                 string const title = bformat(_("LyX: %1$s errors (%2$s)"), action, buffer()->fileName());
338                 owner()->getDialogs().show("errorlist", title);
339                 pimpl_->errorlist_.clear();
340         }
341 }
342
343
344 ErrorList const &
345 BufferView::getErrorList() const
346 {
347         return pimpl_->errorlist_;
348 }
349
350
351 void BufferView::setCursorFromRow(int row)
352 {
353         int tmpid = -1;
354         int tmppos = -1;
355
356         buffer()->texrow.getIdFromRow(row, tmpid, tmppos);
357
358         ParagraphList::iterator texrowpar;
359
360         if (tmpid == -1) {
361                 texrowpar = text->ownerParagraphs().begin();
362                 tmppos = 0;
363         } else {
364                 texrowpar = buffer()->getParFromID(tmpid).pit();
365         }
366         text->setCursor(texrowpar, tmppos);
367 }
368
369
370 bool BufferView::insertInset(InsetOld * inset, string const & lout)
371 {
372         return pimpl_->insertInset(inset, lout);
373 }
374
375
376 void BufferView::gotoLabel(string const & label)
377 {
378         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
379              it != buffer()->inset_iterator_end(); ++it) {
380                 vector<string> labels;
381                 it->getLabelList(labels);
382                 if (find(labels.begin(),labels.end(),label)
383                      != labels.end()) {
384                         beforeChange(text);
385                         text->setCursor(it.getPar(), it.getPos());
386                         text->selection.cursor = text->cursor;
387                         update(text, BufferView::SELECT);
388                         return;
389                 }
390         }
391 }
392
393
394 void BufferView::undo()
395 {
396         if (!available())
397                 return;
398
399         owner()->message(_("Undo"));
400         beforeChange(text);
401         update(text, BufferView::SELECT);
402         if (!textUndo(this))
403                 owner()->message(_("No further undo information"));
404         else
405                 update(text, BufferView::SELECT);
406         switchKeyMap();
407 }
408
409
410 void BufferView::redo()
411 {
412         if (!available())
413                 return;
414
415         owner()->message(_("Redo"));
416         beforeChange(text);
417         update(text, BufferView::SELECT);
418         if (!textRedo(this))
419                 owner()->message(_("No further redo information"));
420         else
421                 update(text, BufferView::SELECT);
422         switchKeyMap();
423 }
424
425
426 // these functions are for the spellchecker
427 WordLangTuple const BufferView::nextWord(float & value)
428 {
429         if (!available()) {
430                 value = 1;
431                 return WordLangTuple();
432         }
433
434         return text->selectNextWordToSpellcheck(value);
435 }
436
437
438 void BufferView::selectLastWord()
439 {
440         if (!available())
441                 return;
442
443         LyXCursor cur = text->selection.cursor;
444         beforeChange(text);
445         text->selection.cursor = cur;
446         text->selectSelectedWord();
447         toggleSelection(false);
448         update(text, BufferView::SELECT);
449 }
450
451
452 void BufferView::endOfSpellCheck()
453 {
454         if (!available()) return;
455
456         beforeChange(text);
457         text->selectSelectedWord();
458         text->clearSelection();
459         update(text, BufferView::SELECT);
460 }
461
462
463 void BufferView::replaceWord(string const & replacestring)
464 {
465         if (!available())
466                 return;
467
468         LyXText * tt = getLyXText();
469         update(tt, BufferView::SELECT);
470
471         // clear the selection (if there is any)
472         toggleSelection(false);
473         update(tt, BufferView::SELECT);
474
475         // clear the selection (if there is any)
476         toggleSelection(false);
477         tt->replaceSelectionWithString(replacestring);
478
479         tt->setSelectionRange(replacestring.length());
480
481         // Go back so that replacement string is also spellchecked
482         for (string::size_type i = 0; i < replacestring.length() + 1; ++i) {
483                 tt->cursorLeft(this);
484         }
485         update(tt, BufferView::SELECT);
486
487         // FIXME: should be done through LFUN
488         buffer()->markDirty();
489         fitCursor();
490 }
491 // End of spellchecker stuff
492
493
494 bool BufferView::lockInset(UpdatableInset * inset)
495 {
496         if (!inset)
497                 return false;
498         // don't relock if we're already locked
499         if (theLockingInset() == inset)
500                 return true;
501         if (!theLockingInset()) {
502                 // first check if it's the inset under the cursor we want lock
503                 // should be most of the time
504                 if (text->cursor.pos() < text->cursor.par()->size()
505                     && text->cursor.par()->getChar(text->cursor.pos()) ==
506                     Paragraph::META_INSET) {
507                         InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
508                         if (inset == in) {
509                                 theLockingInset(inset);
510                                 return true;
511                         }
512                 }
513                 // Then do a deep look of the inset and lock the right one
514                 int const id = inset->id();
515                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
516                 ParagraphList::iterator pend = buffer()->paragraphs.end();
517                 for (; pit != pend; ++pit) {
518                         InsetList::iterator it = pit->insetlist.begin();
519                         InsetList::iterator end = pit->insetlist.end();
520                         for (; it != end; ++it) {
521                                 if (it->inset == inset) {
522                                         text->setCursorIntern(pit, it->pos);
523                                         theLockingInset(inset);
524                                         return true;
525                                 }
526                                 if (it->inset->getInsetFromID(id)) {
527                                         text->setCursorIntern(pit, it->pos);
528                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
529                                         it->inset->localDispatch(cmd);
530                                         return theLockingInset()->lockInsetInInset(this, inset);
531                                 }
532                         }
533                 }
534                 return false;
535         }
536         return theLockingInset()->lockInsetInInset(this, inset);
537 }
538
539
540 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
541 {
542         if (theLockingInset() && available()) {
543                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
544                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
545                         updateScrollbar();
546                         return true;
547                 }
548         }
549         return false;
550 }
551
552
553 void BufferView::hideCursor()
554 {
555         screen().hideCursor();
556 }
557
558
559 int BufferView::unlockInset(UpdatableInset * inset)
560 {
561         if (!inset)
562                 return 0;
563         if (inset && theLockingInset() == inset) {
564                 inset->insetUnlock(this);
565                 theLockingInset(0);
566                 // make sure we update the combo !
567                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
568                 // Tell the paragraph dialog that we changed paragraph
569                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
570                 finishUndo();
571                 return 0;
572         } else if (inset && theLockingInset() &&
573                    theLockingInset()->unlockInsetInInset(this, inset)) {
574                 // Tell the paragraph dialog that we changed paragraph
575                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
576                 // owner inset has updated the layout combo
577                 finishUndo();
578                 return 0;
579         }
580         return 1;
581 }
582
583
584 void BufferView::updateInset(InsetOld * inset)
585 {
586         pimpl_->updateInset(inset);
587 }
588
589
590 bool BufferView::ChangeInsets(InsetOld::Code code,
591                               string const & from, string const & to)
592 {
593         bool need_update = false;
594         LyXCursor cursor = text->cursor;
595         LyXCursor tmpcursor = cursor;
596         cursor.par(tmpcursor.par());
597         cursor.pos(tmpcursor.pos());
598
599         ParIterator end = buffer()->par_iterator_end();
600         for (ParIterator it = buffer()->par_iterator_begin();
601              it != end; ++it) {
602                 bool changed_inset = false;
603                 for (InsetList::iterator it2 = it->insetlist.begin();
604                      it2 != it->insetlist.end(); ++it2) {
605                         if (it2->inset->lyxCode() == code) {
606                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
607                                 if (inset->getContents() == from) {
608                                         inset->setContents(to);
609                                         changed_inset = true;
610                                 }
611                         }
612                 }
613                 if (changed_inset) {
614                         need_update = true;
615
616                         // FIXME
617
618                         // The test it.size()==1 was needed to prevent crashes.
619                         // How to set the cursor corretly when it.size()>1 ??
620                         if (it.size() == 1) {
621                                 text->setCursorIntern(it.pit(), 0);
622                                 text->redoParagraph(text->cursor.par());
623                                 text->partialRebreak();
624                         }
625                 }
626         }
627         text->setCursorIntern(cursor.par(), cursor.pos());
628         return need_update;
629 }
630
631
632 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
633 {
634         // Check if the label 'from' appears more than once
635         vector<string> labels;
636         buffer()->getLabelList(labels);
637
638         if (lyx::count(labels.begin(), labels.end(), from) > 1)
639                 return false;
640
641         return ChangeInsets(InsetOld::REF_CODE, from, to);
642 }
643
644
645 UpdatableInset * BufferView::theLockingInset() const
646 {
647         // If NULL is not allowed we should put an Assert here. (Lgb)
648         if (text)
649                 return text->the_locking_inset;
650         return 0;
651 }
652
653
654 void BufferView::theLockingInset(UpdatableInset * inset)
655 {
656         text->the_locking_inset = inset;
657 }
658
659
660 LyXText * BufferView::getLyXText() const
661 {
662         if (theLockingInset()) {
663                 LyXText * txt = theLockingInset()->getLyXText(this, true);
664                 if (txt)
665                         return txt;
666         }
667         return text;
668 }
669
670
671 LyXText * BufferView::getParentText(InsetOld * inset) const
672 {
673         if (inset->owner()) {
674                 LyXText * txt = inset->getLyXText(this);
675                 inset = inset->owner();
676                 while (inset && inset->getLyXText(this) == txt)
677                         inset = inset->owner();
678                 if (inset)
679                         return inset->getLyXText(this);
680         }
681         return text;
682 }
683
684
685 Language const * BufferView::getParentLanguage(InsetOld * inset) const
686 {
687         LyXText * text = getParentText(inset);
688         return text->cursor.par()->getFontSettings(buffer()->params,
689                                                    text->cursor.pos()).language();
690 }
691
692
693 Encoding const * BufferView::getEncoding() const
694 {
695         LyXText * t = getLyXText();
696         if (!t)
697                 return 0;
698
699         LyXCursor const & c = t->cursor;
700         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
701                                               outerFont(c.par(), t->ownerParagraphs()));
702         return font.language()->encoding();
703 }
704
705
706 void BufferView::haveSelection(bool sel)
707 {
708         pimpl_->workarea().haveSelection(sel);
709 }
710
711
712 int BufferView::workHeight() const
713 {
714         return pimpl_->workarea().workHeight();
715 }