]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.cpp
header cleanup.
[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         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
148         greyed_out_ = false;
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(KeySymbolPtr key, key_modifier::state state)
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, state);
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, key_modifier::state k)
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 (k == key_modifier::shift)
196                         cmd = FuncRequest(cmd0, "region-select");
197                 else if (k == key_modifier::ctrl)
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         bool const needRedraw = buffer_view_->workAreaDispatch(cmd);
212
213         if (needRedraw)
214                 redraw();
215
216         // Skip these when selecting
217         if (cmd.action != LFUN_MOUSE_MOTION) {
218                 lyx_view_.updateLayoutChoice();
219                 lyx_view_.updateMenubar();
220                 lyx_view_.updateToolbars();
221         }
222
223         // GUI tweaks except with mouse motion with no button pressed.
224         if (!(cmd.action == LFUN_MOUSE_MOTION
225                 && cmd.button() == mouse_button::none)) {
226                 // Slight hack: this is only called currently when we
227                 // clicked somewhere, so we force through the display
228                 // of the new status here.
229                 lyx_view_.clearMessage();
230
231                 // Show the cursor immediately after any operation.
232                 startBlinkingCursor();
233         }
234 }
235
236
237 void WorkArea::resizeBufferView()
238 {
239         lyx_view_.busy(true);
240         lyx_view_.message(_("Formatting document..."));
241         buffer_view_->workAreaResize(width(), height());
242         lyx_view_.updateLayoutChoice();
243         lyx_view_.clearMessage();
244         lyx_view_.busy(false);
245 }
246
247
248 void WorkArea::updateScrollbar()
249 {
250         buffer_view_->updateScrollbar();
251         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
252         setScrollbarParams(scroll_.height, scroll_.position,
253                 scroll_.lineScrollHeight);
254 }
255
256
257 void WorkArea::scrollBufferView(int position)
258 {
259         stopBlinkingCursor();
260         buffer_view_->scrollDocView(position);
261         redraw();
262         if (lyxrc.cursor_follows_scrollbar) {
263                 buffer_view_->setCursorFromScrollbar();
264                 lyx_view_.updateLayoutChoice();
265         }
266         // Show the cursor immediately after any operation.
267         startBlinkingCursor();
268 }
269
270
271 void WorkArea::showCursor()
272 {
273         if (cursor_visible_)
274                 return;
275
276         if (!buffer_view_->buffer())
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 (buffer_view_->buffer()) {
329
330                 if (cursor_visible_)
331                         hideCursor();
332                 else
333                         showCursor();
334
335                 // Use this opportunity to deal with any child processes that
336                 // have finished but are waiting to communicate this fact
337                 // to the rest of LyX.
338                 ForkedcallsController & fcc = ForkedcallsController::get();
339                 fcc.handleCompletedProcesses();
340         }
341
342         cursor_timeout_.restart();
343 }
344
345
346 void WorkArea::displayMessage(lyx::docstring const & message)
347 {
348         lyx_view_.message(message);
349 }
350
351 } // namespace frontend
352 } // namespace lyx