]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.cpp
merge controllers/Makefile.am and controllers/tests/Makefile.am
[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
21 #include "FuncRequest.h"
22 #include "LyXFunc.h"
23
24 #include "BufferView.h"
25 #include "Buffer.h"
26 #include "BufferParams.h"
27 #include "CoordCache.h"
28 #include "Cursor.h"
29 #include "debug.h"
30 #include "Language.h"
31 #include "Color.h"
32 #include "Font.h"
33 #include "LyXRC.h"
34 #include "Text.h"
35 #include "LyXView.h"
36 #include "MetricsInfo.h"
37
38 #include "gettext.h"
39 #include "support/ForkedcallsController.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(int id, LyXView & lyx_view)
68         : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
69           id_(id), cursor_visible_(false), cursor_timeout_(400)
70 {
71         // Start loading the pixmap as soon as possible
72         //if (lyxrc.show_banner) {
73         //      showBanner();
74         //}
75
76         // Setup the signals
77         timecon = cursor_timeout_.timeout
78                 .connect(boost::bind(&WorkArea::toggleCursor, this));
79
80         cursor_timeout_.start();
81 }
82
83
84 void WorkArea::setBufferView(BufferView * buffer_view)
85 {
86         if (buffer_view_) {
87                 message_connection_.disconnect();
88                 lyx_view_.disconnectBufferView();
89         }
90
91         hideCursor();
92         buffer_view_ = buffer_view;
93         toggleCursor();
94
95         message_connection_ = buffer_view_->message.connect(
96                         boost::bind(&WorkArea::displayMessage, this, _1));
97
98         lyx_view_.connectBufferView(*buffer_view);
99 }
100
101
102 BufferView & WorkArea::bufferView()
103 {
104         return *buffer_view_;
105 }
106
107
108 BufferView const & WorkArea::bufferView() const
109 {
110         return *buffer_view_;
111 }
112
113
114 void WorkArea::stopBlinkingCursor()
115 {
116         cursor_timeout_.stop();
117         hideCursor();
118 }
119
120
121 void WorkArea::startBlinkingCursor()
122 {
123         showCursor();
124         cursor_timeout_.restart();
125 }
126
127
128 void WorkArea::redraw()
129 {
130         if (!buffer_view_ || !buffer_view_->buffer()) {
131                 greyed_out_ = true;
132                 // The argument here are meaningless.
133                 expose(1,1,1,1);
134                 return;
135         }
136
137         // No need to do anything if this is the current view. The BufferView
138         // metrics are already up to date.
139         if (&lyx_view_ != theApp()->currentView()) {
140                 // FIXME: it would be nice to optimize for the off-screen case.
141                 buffer_view_->updateMetrics(false);
142                 buffer_view_->cursor().fixIfBroken();
143         }
144
145         updateScrollbar();
146
147         // update cursor position, because otherwise it has to wait until
148         // the blinking interval is over
149         if (cursor_visible_) {
150                 hideCursor();
151                 showCursor();
152         }
153         
154         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
155         greyed_out_ = false;
156
157         LYXERR(Debug::WORKAREA) << "WorkArea::redraw screen" << endl;
158
159         int const ymin = std::max(vi.y1, 0);
160         int const ymax = vi.p2 < vi.size - 1 ? vi.y2 : height();
161
162         expose(0, ymin, width(), ymax - ymin);
163
164         //LYXERR(Debug::WORKAREA)
165         //<< "  ymin = " << ymin << "  width() = " << width()
166 //              << "  ymax-ymin = " << ymax-ymin << std::endl;
167
168         if (lyxerr.debugging(Debug::WORKAREA))
169                 buffer_view_->coordCache().dump();
170 }
171
172
173 void WorkArea::processKeySym(KeySymbolPtr key, key_modifier::state state)
174 {
175         // In order to avoid bad surprise in the middle of an operation, we better stop
176         // the blinking cursor.
177         stopBlinkingCursor();
178
179         theLyXFunc().setLyXView(&lyx_view_);
180         theLyXFunc().processKeySym(key, state);
181
182         /* When we move around, or type, it's nice to be able to see
183          * the cursor immediately after the keypress.
184          */
185         startBlinkingCursor();
186 }
187
188
189 void WorkArea::dispatch(FuncRequest const & cmd0, key_modifier::state k)
190 {
191         // Handle drag&drop
192         if (cmd0.action == LFUN_FILE_OPEN) {
193                 lyx_view_.dispatch(cmd0);
194                 return;
195         }
196
197         theLyXFunc().setLyXView(&lyx_view_);
198
199         FuncRequest cmd;
200
201         if (cmd0.action == LFUN_MOUSE_PRESS) {
202                 if (k == key_modifier::shift)
203                         cmd = FuncRequest(cmd0, "region-select");
204                 else if (k == key_modifier::ctrl)
205                         cmd = FuncRequest(cmd0, "paragraph-select");
206                 else
207                         cmd = cmd0;
208         }
209         else
210                 cmd = cmd0;
211
212         // In order to avoid bad surprise in the middle of an operation, we better stop
213         // the blinking cursor.
214         if (!(cmd.action == LFUN_MOUSE_MOTION
215                 && cmd.button() == mouse_button::none))
216                 stopBlinkingCursor();
217
218         bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
219
220         if (needRedraw)
221                 redraw();
222
223         // Skip these when selecting
224         if (cmd.action != LFUN_MOUSE_MOTION) {
225                 lyx_view_.updateLayoutChoice();
226                 lyx_view_.updateMenubar();
227                 lyx_view_.updateToolbars();
228         }
229
230         // GUI tweaks except with mouse motion with no button pressed.
231         if (!(cmd.action == LFUN_MOUSE_MOTION
232                 && cmd.button() == mouse_button::none)) {
233                 // Slight hack: this is only called currently when we
234                 // clicked somewhere, so we force through the display
235                 // of the new status here.
236                 lyx_view_.clearMessage();
237
238                 // Show the cursor immediately after any operation.
239                 startBlinkingCursor();
240         }
241 }
242
243
244 void WorkArea::resizeBufferView()
245 {
246         lyx_view_.busy(true);
247         lyx_view_.message(_("Formatting document..."));
248         buffer_view_->workAreaResize(width(), height());
249         lyx_view_.updateLayoutChoice();
250         lyx_view_.clearMessage();
251         lyx_view_.busy(false);
252 }
253
254
255 void WorkArea::updateScrollbar()
256 {
257         buffer_view_->updateScrollbar();
258         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
259         setScrollbarParams(scroll_.height, scroll_.position,
260                 scroll_.lineScrollHeight);
261 }
262
263
264 void WorkArea::scrollBufferView(int position)
265 {
266         stopBlinkingCursor();
267         buffer_view_->scrollDocView(position);
268         redraw();
269         if (lyxrc.cursor_follows_scrollbar) {
270                 buffer_view_->setCursorFromScrollbar();
271                 lyx_view_.updateLayoutChoice();
272         }
273         // Show the cursor immediately after any operation.
274         startBlinkingCursor();
275 }
276
277
278 void WorkArea::showCursor()
279 {
280         if (cursor_visible_)
281                 return;
282
283         if (!buffer_view_->buffer())
284                 return;
285
286         CursorShape shape = BAR_SHAPE;
287
288         Text const & text = *buffer_view_->cursor().innerText();
289         Font const & realfont = text.real_current_font;
290         BufferParams const & bp = buffer_view_->buffer()->params();
291         bool const samelang = realfont.language() == bp.language;
292         bool const isrtl = realfont.isVisibleRightToLeft();
293
294         if (!samelang || isrtl != bp.language->rightToLeft()) {
295                 shape = L_SHAPE;
296                 if (isrtl)
297                         shape = REVERSED_L_SHAPE;
298         }
299
300         // The ERT language hack needs fixing up
301         if (realfont.language() == latex_language)
302                 shape = BAR_SHAPE;
303
304         Font const font = buffer_view_->cursor().getFont();
305         FontMetrics const & fm = theFontMetrics(font);
306         int const asc = fm.maxAscent();
307         int const des = fm.maxDescent();
308         int h = asc + des;
309         int x = 0;
310         int y = 0;
311         buffer_view_->cursor().getPos(x, y);
312         y -= asc;
313
314         // if it doesn't touch the screen, don't try to show it
315         if (y + h < 0 || y >= height())
316                 return;
317
318         cursor_visible_ = true;
319         showCursor(x, y, h, shape);
320 }
321
322
323 void WorkArea::hideCursor()
324 {
325         if (!cursor_visible_)
326                 return;
327
328         cursor_visible_ = false;
329         removeCursor();
330 }
331
332
333 void WorkArea::toggleCursor()
334 {
335         if (buffer_view_->buffer()) {
336
337                 if (cursor_visible_)
338                         hideCursor();
339                 else
340                         showCursor();
341
342                 // Use this opportunity to deal with any child processes that
343                 // have finished but are waiting to communicate this fact
344                 // to the rest of LyX.
345                 ForkedcallsController & fcc = ForkedcallsController::get();
346                 fcc.handleCompletedProcesses();
347         }
348
349         cursor_timeout_.restart();
350 }
351
352
353 void WorkArea::displayMessage(lyx::docstring const & message)
354 {
355         lyx_view_.message(message);
356 }
357
358 } // namespace frontend
359 } // namespace lyx