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