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