]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
Some helper functions for the augmented dialogs.
[lyx.git] / src / BufferView.C
1 /**
2  * \file BufferView.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alfredo Braunstein
7  * \author Lars Gullik Bjønnes
8  * \author John Levon
9  * \author André Pönitz
10  * \author Jürgen Vigna
11  *
12  * Full author contact details are available in file CREDITS.
13  */
14
15 #include <config.h>
16
17 #include "BufferView.h"
18
19 #include "buffer.h"
20 #include "bufferlist.h"
21 #include "BufferView_pimpl.h"
22 #include "debug.h"
23 #include "funcrequest.h"
24 #include "gettext.h"
25 #include "iterators.h"
26 #include "language.h"
27 #include "lyxlayout.h"
28 #include "lyxtext.h"
29 #include "paragraph.h"
30 #include "paragraph_funcs.h"
31 #include "texrow.h"
32 #include "undo.h"
33 #include "WordLangTuple.h"
34
35 #include "frontends/Alert.h"
36 #include "frontends/Dialogs.h"
37 #include "frontends/LyXView.h"
38 #include "frontends/screen.h"
39 #include "frontends/WorkArea.h"
40
41 #include "insets/insetcommand.h" // ChangeRefs
42 #include "insets/updatableinset.h"
43
44 #include "support/filetools.h"
45 #include "support/lyxalgo.h" // lyx_count
46
47 using lyx::support::bformat;
48 using lyx::support::MakeAbsPath;
49
50 using std::find;
51 using std::string;
52 using std::vector;
53
54
55 extern BufferList bufferlist;
56
57
58 BufferView::BufferView(LyXView * owner, int xpos, int ypos,
59                        int width, int height)
60         : pimpl_(new Pimpl(this, owner, xpos, ypos, width, height)),
61           x_target_(0)
62 {}
63
64
65 BufferView::~BufferView()
66 {
67         delete pimpl_;
68 }
69
70
71 Buffer * BufferView::buffer() const
72 {
73         return pimpl_->buffer_;
74 }
75
76
77 LyXScreen & BufferView::screen() const
78 {
79         return pimpl_->screen();
80 }
81
82
83 LyXView * BufferView::owner() const
84 {
85         return pimpl_->owner_;
86 }
87
88
89 Painter & BufferView::painter() const
90 {
91         return pimpl_->painter();
92 }
93
94
95 void BufferView::buffer(Buffer * b)
96 {
97         pimpl_->buffer(b);
98 }
99
100
101 bool BufferView::newFile(string const & fn, string const & tn, bool named)
102 {
103         return pimpl_->newFile(fn, tn, named);
104 }
105
106
107 bool BufferView::loadLyXFile(string const & fn, bool tl)
108 {
109         return pimpl_->loadLyXFile(fn, tl);
110 }
111
112
113 void BufferView::reload()
114 {
115         string const fn = buffer()->fileName();
116         if (bufferlist.close(buffer(), false))
117                 loadLyXFile(fn);
118 }
119
120
121 void BufferView::resize()
122 {
123         if (pimpl_->buffer_)
124                 pimpl_->resizeCurrentBuffer();
125 }
126
127
128 bool BufferView::fitCursor()
129 {
130         return pimpl_->fitCursor();
131 }
132
133
134 void BufferView::update()
135 {
136         pimpl_->update();
137 }
138
139
140 void BufferView::updateScrollbar()
141 {
142         pimpl_->updateScrollbar();
143 }
144
145
146 void BufferView::scrollDocView(int value)
147 {
148         pimpl_->scrollDocView(value);
149 }
150
151
152 void BufferView::redoCurrentBuffer()
153 {
154         pimpl_->redoCurrentBuffer();
155 }
156
157
158 bool BufferView::available() const
159 {
160         return pimpl_->available();
161 }
162
163
164 Change const BufferView::getCurrentChange()
165 {
166         return pimpl_->getCurrentChange();
167 }
168
169
170 void BufferView::savePosition(unsigned int i)
171 {
172         pimpl_->savePosition(i);
173 }
174
175
176 void BufferView::restorePosition(unsigned int i)
177 {
178         pimpl_->restorePosition(i);
179 }
180
181
182 bool BufferView::isSavedPosition(unsigned int i)
183 {
184         return pimpl_->isSavedPosition(i);
185 }
186
187
188 void BufferView::switchKeyMap()
189 {
190         pimpl_->switchKeyMap();
191 }
192
193
194 int BufferView::workWidth() const
195 {
196         return pimpl_->workarea().workWidth();
197 }
198
199
200 void BufferView::center()
201 {
202         pimpl_->center();
203 }
204
205
206 int BufferView::top_y() const
207 {
208         return pimpl_->top_y();
209 }
210
211
212 void BufferView::top_y(int y)
213 {
214         pimpl_->top_y(y);
215 }
216
217
218 string const BufferView::getClipboard() const
219 {
220         return pimpl_->workarea().getClipboard();
221 }
222
223
224 void BufferView::stuffClipboard(string const & stuff) const
225 {
226         pimpl_->stuffClipboard(stuff);
227 }
228
229
230 bool BufferView::dispatch(FuncRequest const & ev)
231 {
232         return pimpl_->dispatch(ev);
233 }
234
235
236 void BufferView::scroll(int lines)
237 {
238         pimpl_->scroll(lines);
239 }
240
241
242 // Inserts a file into current document
243 bool BufferView::insertLyXFile(string const & filen)
244         //
245         // Copyright CHT Software Service GmbH
246         // Uwe C. Schroeder
247         //
248         // Insert a LyXformat - file into current buffer
249         //
250         // Moved from lyx_cb.C (Lgb)
251 {
252         BOOST_ASSERT(!filen.empty());
253
254         string const fname = MakeAbsPath(filen);
255
256         text()->clearSelection();
257         text()->breakParagraph(buffer()->paragraphs());
258
259         bool res = buffer()->readFile(fname, text()->cursorPar());
260         resize();
261         return res;
262 }
263
264
265 void BufferView::showErrorList(string const & action) const
266 {
267         if (getErrorList().size()) {
268                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
269                         action, buffer()->fileName());
270                 owner()->getDialogs().show("errorlist", title);
271                 pimpl_->errorlist_.clear();
272         }
273 }
274
275
276 ErrorList const & BufferView::getErrorList() const
277 {
278         return pimpl_->errorlist_;
279 }
280
281
282 void BufferView::setCursorFromRow(int row)
283 {
284         int tmpid = -1;
285         int tmppos = -1;
286
287         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
288
289         if (tmpid == -1)
290                 text()->setCursor(0, 0);
291         else
292                 text()->setCursor(buffer()->getParFromID(tmpid).pit(), tmppos);
293 }
294
295
296 bool BufferView::insertInset(InsetOld * inset, string const & lout)
297 {
298         return pimpl_->insertInset(inset, lout);
299 }
300
301
302 void BufferView::gotoLabel(string const & label)
303 {
304         for (Buffer::inset_iterator it = buffer()->inset_iterator_begin();
305              it != buffer()->inset_iterator_end(); ++it) {
306                 vector<string> labels;
307                 it->getLabelList(*buffer(), labels);
308                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
309                         text()->clearSelection();
310                         text()->setCursor(
311                                 std::distance(text()->paragraphs().begin(), it.getPar()),
312                                 it.getPos());
313                         text()->selection.cursor = text()->cursor;
314                         update();
315                         return;
316                 }
317         }
318 }
319
320
321 void BufferView::undo()
322 {
323         if (!available())
324                 return;
325
326         owner()->message(_("Undo"));
327         text()->clearSelection();
328         if (!textUndo(this))
329                 owner()->message(_("No further undo information"));
330         update();
331         switchKeyMap();
332 }
333
334
335 void BufferView::redo()
336 {
337         if (!available())
338                 return;
339
340         owner()->message(_("Redo"));
341         text()->clearSelection();
342         if (!textRedo(this))
343                 owner()->message(_("No further redo information"));
344         update();
345         switchKeyMap();
346 }
347
348
349 void BufferView::replaceWord(string const & replacestring)
350 {
351         if (!available())
352                 return;
353
354         LyXText * t = getLyXText();
355
356         t->replaceSelectionWithString(replacestring);
357         t->setSelectionRange(replacestring.length());
358
359         // Go back so that replacement string is also spellchecked
360         for (string::size_type i = 0; i < replacestring.length() + 1; ++i)
361                 t->cursorLeft(this);
362
363         // FIXME: should be done through LFUN
364         buffer()->markDirty();
365         update();
366 }
367
368
369 void BufferView::hideCursor()
370 {
371         screen().hideCursor();
372 }
373
374
375 bool BufferView::ChangeRefsIfUnique(string const & from, string const & to)
376 {
377         // Check if the label 'from' appears more than once
378         vector<string> labels;
379         buffer()->getLabelList(labels);
380
381         if (lyx::count(labels.begin(), labels.end(), from) > 1)
382                 return false;
383
384         return pimpl_->ChangeInsets(InsetOld::REF_CODE, from, to);
385 }
386
387
388 UpdatableInset * BufferView::innerInset() const
389 {
390         return static_cast<UpdatableInset*>(cursor().innerInset());
391 }
392
393
394 LyXText * BufferView::getLyXText() const
395 {
396         return cursor().innerText();
397 }
398
399
400 Language const * BufferView::getParentLanguage(InsetOld * inset) const
401 {
402         Paragraph const & par = ownerPar(*buffer(), inset);
403         return par.getFontSettings(buffer()->params(),
404                                    par.getPositionOfInset(inset)).language();
405 }
406
407
408 Encoding const * BufferView::getEncoding() const
409 {
410         LyXText * t = getLyXText();
411         if (!t)
412                 return 0;
413         return t->cursorPar()->getFont(
414                 buffer()->params(),
415                 t->cursor.pos(),
416                 outerFont(t->cursorPar(), t->paragraphs())
417         ).language()->encoding();
418 }
419
420
421 void BufferView::haveSelection(bool sel)
422 {
423         pimpl_->workarea().haveSelection(sel);
424 }
425
426
427 int BufferView::workHeight() const
428 {
429         return pimpl_->workarea().workHeight();
430 }
431
432
433 LCursor & BufferView::cursor()
434 {
435         return pimpl_->cursor_;
436 }
437
438
439 LCursor const & BufferView::cursor() const
440 {
441         return pimpl_->cursor_;
442 }
443
444
445 void BufferView::x_target(int x)
446 {
447         x_target_ = x;
448 }
449
450
451 int BufferView::x_target() const
452 {
453         return x_target_;
454 }
455
456
457 void BufferView::updateParagraphDialog()
458 {
459         pimpl_->updateParagraphDialog();
460 }
461
462
463 LyXText * BufferView::text() const
464 {
465         return pimpl_->buffer_ ? &pimpl_->buffer_->text() : 0;
466 }