]> git.lyx.org Git - lyx.git/blob - src/BufferView.C
change "support/std_sstream.h" to <sstream>
[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 "bufferparams.h"
22 #include "BufferView_pimpl.h"
23 #include "CutAndPaste.h"
24 #include "debug.h"
25 #include "funcrequest.h"
26 #include "FuncStatus.h"
27 #include "gettext.h"
28 #include "insetiterator.h"
29 #include "language.h"
30 #include "lyxlayout.h"
31 #include "lyxtext.h"
32 #include "lyxtextclass.h"
33 #include "paragraph.h"
34 #include "paragraph_funcs.h"
35 #include "pariterator.h"
36 #include "texrow.h"
37 #include "undo.h"
38 #include "WordLangTuple.h"
39
40 #include "frontends/Alert.h"
41 #include "frontends/Dialogs.h"
42 #include "frontends/LyXView.h"
43 #include "frontends/screen.h"
44 #include "frontends/WorkArea.h"
45
46 #include "insets/insetcommand.h" // ChangeRefs
47 #include "insets/updatableinset.h"
48 #include "insets/insettext.h"
49
50 #include "support/filetools.h"
51 #include "support/lyxalgo.h" // lyx_count
52
53 using lyx::support::bformat;
54 using lyx::support::MakeAbsPath;
55
56 using lyx::cap::setSelectionRange;
57
58 using std::distance;
59 using std::find;
60 using std::string;
61 using std::swap;
62 using std::vector;
63
64
65 extern BufferList bufferlist;
66
67
68 BufferView::BufferView(LyXView * owner, int width, int height)
69         : pimpl_(new Pimpl(*this, owner, width, height))
70 {}
71
72
73 BufferView::~BufferView()
74 {
75         delete pimpl_;
76 }
77
78
79 void BufferView::unsetXSel()
80 {
81         pimpl_->xsel_cache_.set = false;
82 }
83
84
85 Buffer * BufferView::buffer() const
86 {
87         return pimpl_->buffer_;
88 }
89
90
91 LyXScreen & BufferView::screen() const
92 {
93         return pimpl_->screen();
94 }
95
96
97 LyXView * BufferView::owner() const
98 {
99         return pimpl_->owner_;
100 }
101
102
103 Painter & BufferView::painter() const
104 {
105         return pimpl_->painter();
106 }
107
108
109 void BufferView::setBuffer(Buffer * b)
110 {
111         pimpl_->setBuffer(b);
112 }
113
114
115 void BufferView::newFile(string const & fn, string const & tn, bool named)
116 {
117         pimpl_->newFile(fn, tn, named);
118 }
119
120
121 bool BufferView::loadLyXFile(string const & fn, bool tl)
122 {
123         return pimpl_->loadLyXFile(fn, tl);
124 }
125
126
127 void BufferView::reload()
128 {
129         string const fn = buffer()->fileName();
130         if (bufferlist.close(buffer(), false))
131                 loadLyXFile(fn);
132 }
133
134
135 void BufferView::resize()
136 {
137         if (pimpl_->buffer_)
138                 pimpl_->resizeCurrentBuffer();
139 }
140
141
142 bool BufferView::fitCursor()
143 {
144         return pimpl_->fitCursor();
145 }
146
147
148 void BufferView::update()
149 {
150         pimpl_->update();
151 }
152
153
154 void BufferView::updateScrollbar()
155 {
156         pimpl_->updateScrollbar();
157 }
158
159
160 void BufferView::scrollDocView(int value)
161 {
162         pimpl_->scrollDocView(value);
163 }
164
165
166 void BufferView::redoCurrentBuffer()
167 {
168         pimpl_->redoCurrentBuffer();
169 }
170
171
172 bool BufferView::available() const
173 {
174         return pimpl_->available();
175 }
176
177
178 Change const BufferView::getCurrentChange()
179 {
180         return pimpl_->getCurrentChange();
181 }
182
183
184 void BufferView::savePosition(unsigned int i)
185 {
186         pimpl_->savePosition(i);
187 }
188
189
190 void BufferView::restorePosition(unsigned int i)
191 {
192         pimpl_->restorePosition(i);
193 }
194
195
196 bool BufferView::isSavedPosition(unsigned int i)
197 {
198         return pimpl_->isSavedPosition(i);
199 }
200
201
202 void BufferView::switchKeyMap()
203 {
204         pimpl_->switchKeyMap();
205 }
206
207
208 int BufferView::workWidth() const
209 {
210         return pimpl_->workarea().workWidth();
211 }
212
213
214 void BufferView::center()
215 {
216         pimpl_->center();
217 }
218
219
220 int BufferView::top_y() const
221 {
222         return pimpl_->top_y();
223 }
224
225
226 void BufferView::top_y(int y)
227 {
228         pimpl_->top_y(y);
229 }
230
231
232 string const BufferView::getClipboard() const
233 {
234         return pimpl_->workarea().getClipboard();
235 }
236
237
238 void BufferView::stuffClipboard(string const & stuff) const
239 {
240         pimpl_->stuffClipboard(stuff);
241 }
242
243
244 FuncStatus BufferView::getStatus(FuncRequest const & cmd)
245 {
246         return pimpl_->getStatus(cmd);
247 }
248
249
250 bool BufferView::dispatch(FuncRequest const & ev)
251 {
252         return pimpl_->dispatch(ev);
253 }
254
255
256 void BufferView::scroll(int lines)
257 {
258         pimpl_->scroll(lines);
259 }
260
261
262 // Inserts a file into current document
263 bool BufferView::insertLyXFile(string const & filen)
264         //
265         // Copyright CHT Software Service GmbH
266         // Uwe C. Schroeder
267         //
268         // Insert a LyXformat - file into current buffer
269         //
270         // Moved from lyx_cb.C (Lgb)
271 {
272         BOOST_ASSERT(!filen.empty());
273
274         string const fname = MakeAbsPath(filen);
275
276         cursor().clearSelection();
277         text()->breakParagraph(cursor());
278
279         BOOST_ASSERT(cursor().inTexted());
280         bool res = buffer()->readFile(fname, cursor().par());
281         resize();
282         return res;
283 }
284
285
286 void BufferView::showErrorList(string const & action) const
287 {
288         if (getErrorList().size()) {
289                 string const title = bformat(_("LyX: %1$s errors (%2$s)"),
290                         action, buffer()->fileName());
291                 owner()->getDialogs().show("errorlist", title);
292                 pimpl_->errorlist_.clear();
293         }
294 }
295
296
297 ErrorList const & BufferView::getErrorList() const
298 {
299         return pimpl_->errorlist_;
300 }
301
302
303 void BufferView::setCursorFromRow(int row)
304 {
305         int tmpid = -1;
306         int tmppos = -1;
307
308         buffer()->texrow().getIdFromRow(row, tmpid, tmppos);
309
310         if (tmpid == -1)
311                 text()->setCursor(cursor(), 0, 0);
312         else
313                 text()->setCursor(cursor(), buffer()->getParFromID(tmpid).pit(),
314                         tmppos);
315 }
316
317
318 void BufferView::gotoLabel(string const & label)
319 {
320         for (InsetIterator it = inset_iterator_begin(buffer()->inset()); it; ++it) {
321                 vector<string> labels;
322                 it->getLabelList(*buffer(), labels);
323                 if (find(labels.begin(),labels.end(),label) != labels.end()) {
324                         cursor().clearSelection();
325                         text()->setCursor(cursor(), it.par(), it.pos());
326                         cursor().resetAnchor();
327                         update();
328                         return;
329                 }
330         }
331 }
332
333
334 void BufferView::hideCursor()
335 {
336         screen().hideCursor();
337 }
338
339
340 LyXText * BufferView::getLyXText() const
341 {
342         LyXText * text = cursor().innerText();
343         BOOST_ASSERT(text);
344         return text;
345 }
346
347
348 Language const * BufferView::getParentLanguage(InsetOld * inset) const
349 {
350         Paragraph const & par = ownerPar(*buffer(), inset);
351         return par.getFontSettings(buffer()->params(),
352                                    par.getPositionOfInset(inset)).language();
353 }
354
355
356 void BufferView::haveSelection(bool sel)
357 {
358         pimpl_->workarea().haveSelection(sel);
359 }
360
361
362 int BufferView::workHeight() const
363 {
364         return pimpl_->workarea().workHeight();
365 }
366
367
368 LyXText * BufferView::text() const
369 {
370         return buffer() ? &buffer()->text() : 0;
371 }
372
373
374 void BufferView::setCursor(ParIterator const & par, lyx::pos_type pos)
375 {
376         int const last = par.size();
377         for (int i = 0; i < last; ++i)
378                 par[i].inset().edit(cursor(), true);
379
380         cursor().setCursor(makeDocIterator(par, pos), false);
381         par.bottom().text()->redoParagraph(par.bottom().par());
382 }
383
384
385 void BufferView::putSelectionAt(DocIterator const & cur,
386                                 int length, bool backwards)
387 {
388         ParIterator par(cur);
389
390         cursor().clearSelection();
391
392         setCursor(par, cur.pos());
393
394         if (length) {
395                 if (backwards) {
396                         cursor().setSelection(cursor(), -length);
397                         DocIterator const it = cursor();
398                         cursor().setCursor(cursor().anchor_, true);
399                         cursor().anchor_ = it;
400                 } else
401                         cursor().setSelection(cursor(), length);
402         }
403 }
404
405
406 LCursor & BufferView::cursor()
407 {
408         return pimpl_->cursor_;
409 }
410
411
412 LCursor const & BufferView::cursor() const
413 {
414         return pimpl_->cursor_;
415 }