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