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