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