]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.cpp
transfer cursor position saving (in the session) from ~WorkArea to ~BufferView.
[lyx.git] / src / frontends / WorkArea.cpp
1 /**
2  * \file WorkArea.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Splash screen code added by Angus Leeming
12  */
13
14 #include <config.h>
15
16 #include "frontends/WorkArea.h"
17
18 #include "frontends/Application.h"
19 #include "frontends/FontMetrics.h"
20 #include "frontends/LyXView.h"
21
22 #include "BufferView.h"
23 #include "Buffer.h"
24 #include "BufferParams.h"
25 #include "Color.h"
26 #include "CoordCache.h"
27 #include "Cursor.h"
28 #include "debug.h"
29 #include "Font.h"
30 #include "FuncRequest.h"
31 #include "Language.h"
32 #include "LyXFunc.h"
33 #include "LyXRC.h"
34 #include "MetricsInfo.h"
35 #include "Text.h"
36
37 #include "gettext.h"
38 #include "support/ForkedcallsController.h"
39 #include "support/FileName.h"
40
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/current_function.hpp>
44
45 using lyx::support::ForkedcallsController;
46
47 using std::endl;
48 using std::min;
49 using std::max;
50 using std::string;
51
52
53 namespace {
54
55 // All the below connection objects are needed because of a bug in some
56 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
57 // to these connections we avoid a segfault upon startup, and also at exit.
58 // (Lgb)
59
60 boost::signals::connection timecon;
61
62 } // anon namespace
63
64 namespace lyx {
65 namespace frontend {
66
67 WorkArea::WorkArea(Buffer & buffer, LyXView & lv)
68         : buffer_view_(new BufferView(buffer)), lyx_view_(&lv),
69         cursor_visible_(false), cursor_timeout_(400)
70 {
71         // Setup the signals
72         timecon = cursor_timeout_.timeout
73                 .connect(boost::bind(&WorkArea::toggleCursor, this));
74
75         bufferChangedConnection_ =
76                 buffer.changed.connect(
77                         boost::bind(&WorkArea::redraw, this));
78
79         bufferClosingConnection_ =
80                 buffer.closing.connect(
81                 boost::bind(&WorkArea::close, this));
82
83         cursor_timeout_.start();
84 }
85
86
87 WorkArea::~WorkArea()
88 {
89         bufferChangedConnection_.disconnect();
90         bufferClosingConnection_.disconnect();
91
92         delete buffer_view_;
93 }
94
95
96 void WorkArea::close()
97 {
98         lyx_view_->removeWorkArea(this);
99 }
100
101 //void WorkArea::setLyXView(LyXView * lyx_view)
102 //{
103 //      lyx_view_ = lyx_view;
104 //}
105
106
107 BufferView & WorkArea::bufferView()
108 {
109         return *buffer_view_;
110 }
111
112
113 BufferView const & WorkArea::bufferView() const
114 {
115         return *buffer_view_;
116 }
117
118
119 void WorkArea::stopBlinkingCursor()
120 {
121         cursor_timeout_.stop();
122         hideCursor();
123 }
124
125
126 void WorkArea::startBlinkingCursor()
127 {
128         showCursor();
129         cursor_timeout_.restart();
130 }
131
132
133 void WorkArea::redraw()
134 {
135         if (!isVisible())
136                 // No need to redraw in this case.
137                 return;
138
139         // No need to do anything if this is the current view. The BufferView
140         // metrics are already up to date.
141         if (lyx_view_ != theApp()->currentView()) {
142                 // FIXME: it would be nice to optimize for the off-screen case.
143                 buffer_view_->updateMetrics(false);
144                 buffer_view_->cursor().fixIfBroken();
145         }
146
147         updateScrollbar();
148
149         // update cursor position, because otherwise it has to wait until
150         // the blinking interval is over
151         if (cursor_visible_) {
152                 hideCursor();
153                 showCursor();
154         }
155         
156         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
157
158         LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
159
160         int const ymin = std::max(vi.y1, 0);
161         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
162
163         expose(0, ymin, width(), ymax - ymin);
164
165         //LYXERR(Debug::WORKAREA)
166         //<< "  ymin = " << ymin << "  width() = " << width()
167 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
168
169         if (lyxerr.debugging(Debug::WORKAREA))
170                 buffer_view_->coordCache().dump();
171 }
172
173
174 void WorkArea::processKeySym(KeySymbolPtr key, key_modifier::state state)
175 {
176         // In order to avoid bad surprise in the middle of an operation, we better stop
177         // the blinking cursor.
178         stopBlinkingCursor();
179
180         theLyXFunc().setLyXView(lyx_view_);
181         theLyXFunc().processKeySym(key, state);
182 }
183
184
185 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
186 {
187         // Handle drag&drop
188         if (cmd0.action == LFUN_FILE_OPEN) {
189                 lyx_view_->dispatch(cmd0);
190                 return;
191         }
192
193         theLyXFunc().setLyXView(lyx_view_);
194
195         FuncRequest cmd;
196
197         if (cmd0.action == LFUN_MOUSE_PRESS) {
198                 if (k == key_modifier::shift)
199                         cmd = FuncRequest(cmd0, "region-select");
200                 else if (k == key_modifier::ctrl)
201                         cmd = FuncRequest(cmd0, "paragraph-select");
202                 else
203                         cmd = cmd0;
204         }
205         else
206                 cmd = cmd0;
207
208         // In order to avoid bad surprise in the middle of an operation, we better stop
209         // the blinking cursor.
210         if (!(cmd.action == LFUN_MOUSE_MOTION
211                 && cmd.button() == mouse_button::none))
212                 stopBlinkingCursor();
213
214         bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
215
216         if (needRedraw)
217                 redraw();
218
219         // Skip these when selecting
220         if (cmd.action != LFUN_MOUSE_MOTION) {
221                 lyx_view_->updateLayoutChoice();
222                 lyx_view_->updateMenubar();
223                 lyx_view_->updateToolbars();
224         }
225
226         // GUI tweaks except with mouse motion with no button pressed.
227         if (!(cmd.action == LFUN_MOUSE_MOTION
228                 && cmd.button() == mouse_button::none)) {
229                 // Slight hack: this is only called currently when we
230                 // clicked somewhere, so we force through the display
231                 // of the new status here.
232                 lyx_view_->clearMessage();
233
234                 // Show the cursor immediately after any operation.
235                 startBlinkingCursor();
236         }
237 }
238
239
240 void WorkArea::resizeBufferView()
241 {
242         lyx_view_->busy(true);
243         lyx_view_->message(_("Formatting document..."));
244         buffer_view_->workAreaResize(width(), height());
245         lyx_view_->updateLayoutChoice();
246         lyx_view_->clearMessage();
247         lyx_view_->busy(false);
248 }
249
250
251 void WorkArea::updateScrollbar()
252 {
253         buffer_view_->updateScrollbar();
254         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
255         setScrollbarParams(scroll_.height, scroll_.position,
256                 scroll_.lineScrollHeight);
257 }
258
259
260 void WorkArea::scrollBufferView(int position)
261 {
262         stopBlinkingCursor();
263         buffer_view_->scrollDocView(position);
264         redraw();
265         if (lyxrc.cursor_follows_scrollbar) {
266                 buffer_view_->setCursorFromScrollbar();
267                 lyx_view_->updateLayoutChoice();
268         }
269         // Show the cursor immediately after any operation.
270         startBlinkingCursor();
271 }
272
273
274 void WorkArea::showCursor()
275 {
276         if (cursor_visible_)
277                 return;
278
279         CursorShape shape = BAR_SHAPE;
280
281         Text const & text = *buffer_view_->cursor().innerText();
282         Font const & realfont = text.real_current_font;
283         BufferParams const & bp = buffer_view_->buffer().params();
284         bool const samelang = realfont.language() == bp.language;
285         bool const isrtl = realfont.isVisibleRightToLeft();
286
287         if (!samelang || isrtl != bp.language->rightToLeft()) {
288                 shape = L_SHAPE;
289                 if (isrtl)
290                         shape = REVERSED_L_SHAPE;
291         }
292
293         // The ERT language hack needs fixing up
294         if (realfont.language() == latex_language)
295                 shape = BAR_SHAPE;
296
297         Font const font = buffer_view_->cursor().getFont();
298         FontMetrics const & fm = theFontMetrics(font);
299         int const asc = fm.maxAscent();
300         int const des = fm.maxDescent();
301         int h = asc + des;
302         int x = 0;
303         int y = 0;
304         buffer_view_->cursor().getPos(x, y);
305         y -= asc;
306
307         // if it doesn't touch the screen, don't try to show it
308         if (y + h < 0 || y >= height())
309                 return;
310
311         cursor_visible_ = true;
312         showCursor(x, y, h, shape);
313 }
314
315
316 void WorkArea::hideCursor()
317 {
318         if (!cursor_visible_)
319                 return;
320
321         cursor_visible_ = false;
322         removeCursor();
323 }
324
325
326 void WorkArea::toggleCursor()
327 {
328         if (cursor_visible_)
329                 hideCursor();
330         else
331                 showCursor();
332
333         // Use this opportunity to deal with any child processes that
334         // have finished but are waiting to communicate this fact
335         // to the rest of LyX.
336         ForkedcallsController & fcc = ForkedcallsController::get();
337         fcc.handleCompletedProcesses();
338
339         cursor_timeout_.restart();
340 }
341
342 } // namespace frontend
343 } // namespace lyx