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