]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
partial fix for anchor_row problems
[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         fitCursor();
448         update();
449 }
450
451
452 bool BufferView::lockInset(UpdatableInset * inset)
453 {
454         if (!inset)
455                 return false;
456         // don't relock if we're already locked
457         if (theLockingInset() == inset)
458                 return true;
459         if (!theLockingInset()) {
460                 // first check if it's the inset under the cursor we want lock
461                 // should be most of the time
462                 if (text->cursor.pos() < text->cursor.par()->size()
463                     && text->cursor.par()->getChar(text->cursor.pos()) ==
464                     Paragraph::META_INSET) {
465                         InsetOld * in = text->cursor.par()->getInset(text->cursor.pos());
466                         if (inset == in) {
467                                 theLockingInset(inset);
468                                 return true;
469                         }
470                 }
471                 // Then do a deep look of the inset and lock the right one
472                 int const id = inset->id();
473                 ParagraphList::iterator pit = buffer()->paragraphs.begin();
474                 ParagraphList::iterator pend = buffer()->paragraphs.end();
475                 for (; pit != pend; ++pit) {
476                         InsetList::iterator it = pit->insetlist.begin();
477                         InsetList::iterator end = pit->insetlist.end();
478                         for (; it != end; ++it) {
479                                 if (it->inset == inset) {
480                                         text->setCursorIntern(pit, it->pos);
481                                         theLockingInset(inset);
482                                         return true;
483                                 }
484                                 if (it->inset->getInsetFromID(id)) {
485                                         text->setCursorIntern(pit, it->pos);
486                                         FuncRequest cmd(this, LFUN_INSET_EDIT, "left");
487                                         it->inset->localDispatch(cmd);
488                                         return theLockingInset()->lockInsetInInset(this, inset);
489                                 }
490                         }
491                 }
492                 return false;
493         }
494         return theLockingInset()->lockInsetInInset(this, inset);
495 }
496
497
498 bool BufferView::fitLockedInsetCursor(int x, int y, int asc, int desc)
499 {
500         if (theLockingInset() && available()) {
501                 y += text->cursor.iy() + theLockingInset()->insetInInsetY();
502                 if (screen().fitManualCursor(this, text, x, y, asc, desc)) {
503                         updateScrollbar();
504                         return true;
505                 }
506         }
507         return false;
508 }
509
510
511 void BufferView::hideCursor()
512 {
513         screen().hideCursor();
514 }
515
516
517 int BufferView::unlockInset(UpdatableInset * inset)
518 {
519         if (!inset)
520                 return 0;
521         if (inset && theLockingInset() == inset) {
522                 inset->insetUnlock(this);
523                 theLockingInset(0);
524                 // make sure we update the combo !
525                 owner()->setLayout(getLyXText()->cursor.par()->layout()->name());
526                 // Tell the paragraph dialog that we changed paragraph
527                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
528                 finishUndo();
529                 return 0;
530         } else if (inset && theLockingInset() &&
531                    theLockingInset()->unlockInsetInInset(this, inset)) {
532                 // Tell the paragraph dialog that we changed paragraph
533                 dispatch(FuncRequest(LFUN_PARAGRAPH_UPDATE));
534                 // owner inset has updated the layout combo
535                 finishUndo();
536                 return 0;
537         }
538         return 1;
539 }
540
541
542 void BufferView::updateInset()
543 {
544         pimpl_->updateInset();
545 }
546
547
548 bool BufferView::ChangeInsets(InsetOld::Code code,
549                               string const & from, string const & to)
550 {
551         bool need_update = false;
552         LyXCursor cursor = text->cursor;
553         LyXCursor tmpcursor = cursor;
554         cursor.par(tmpcursor.par());
555         cursor.pos(tmpcursor.pos());
556
557         ParIterator end = buffer()->par_iterator_end();
558         for (ParIterator it = buffer()->par_iterator_begin();
559              it != end; ++it) {
560                 bool changed_inset = false;
561                 for (InsetList::iterator it2 = it->insetlist.begin();
562                      it2 != it->insetlist.end(); ++it2) {
563                         if (it2->inset->lyxCode() == code) {
564                                 InsetCommand * inset = static_cast<InsetCommand *>(it2->inset);
565                                 if (inset->getContents() == from) {
566                                         inset->setContents(to);
567                                         changed_inset = true;
568                                 }
569                         }
570                 }
571                 if (changed_inset) {
572                         need_update = true;
573
574                         // FIXME
575
576                         // The test it.size()==1 was needed to prevent crashes.
577                         // How to set the cursor corretly when it.size()>1 ??
578                         if (it.size() == 1) {
579                                 text->setCursorIntern(it.pit(), 0);
580                                 text->redoParagraph(text->cursor.par());
581                         }
582                 }
583         }
584         text->setCursorIntern(cursor.par(), cursor.pos());
585         return need_update;
586 }
587
588
589 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
590 {
591         // Check if the label 'from' appears more than once
592         vector<string> labels;
593         buffer()->getLabelList(labels);
594
595         if (lyx::count(labels.begin(), labels.end(), from) > 1)
596                 return false;
597
598         return ChangeInsets(InsetOld::REF_CODE, from, to);
599 }
600
601
602 UpdatableInset * BufferView::theLockingInset() const
603 {
604         // If NULL is not allowed we should put an Assert here. (Lgb)
605         if (text)
606                 return text->the_locking_inset;
607         return 0;
608 }
609
610
611 void BufferView::theLockingInset(UpdatableInset * inset)
612 {
613         text->the_locking_inset = inset;
614 }
615
616
617 LyXText * BufferView::getLyXText() const
618 {
619         if (theLockingInset()) {
620                 LyXText * txt = theLockingInset()->getLyXText(this, true);
621                 if (txt)
622                         return txt;
623         }
624         return text;
625 }
626
627
628 LyXText * BufferView::getParentText(InsetOld * inset) const
629 {
630         if (inset->owner()) {
631                 LyXText * txt = inset->getLyXText(this);
632                 inset = inset->owner();
633                 while (inset && inset->getLyXText(this) == txt)
634                         inset = inset->owner();
635                 if (inset)
636                         return inset->getLyXText(this);
637         }
638         return text;
639 }
640
641
642 Language const * BufferView::getParentLanguage(InsetOld * inset) const
643 {
644         LyXText * text = getParentText(inset);
645         return text->cursor.par()->getFontSettings(buffer()->params,
646                                                    text->cursor.pos()).language();
647 }
648
649
650 Encoding const * BufferView::getEncoding() const
651 {
652         LyXText * t = getLyXText();
653         if (!t)
654                 return 0;
655
656         LyXCursor const & c = t->cursor;
657         LyXFont const font = c.par()->getFont(buffer()->params, c.pos(),
658                                               outerFont(c.par(), t->ownerParagraphs()));
659         return font.language()->encoding();
660 }
661
662
663 void BufferView::haveSelection(bool sel)
664 {
665         pimpl_->workarea().haveSelection(sel);
666 }
667
668
669 int BufferView::workHeight() const
670 {
671         return pimpl_->workarea().workHeight();
672 }