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