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