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