]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
This commit moves the busy cursor and formatting message handling from BufferView...
[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 "WorkArea.h"
17
18 #include "font_metrics.h"
19 #include "funcrequest.h"
20 #include "lyx_gui.h"
21 #include "lyxfunc.h"
22 #include "Painter.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 "LColor.h"
32 #include "lyxfont.h"
33 #include "lyxrc.h"
34 #include "lyxrow.h"
35 #include "lyxtext.h"
36 #include "LyXView.h"
37 #include "metricsinfo.h"
38 #include "paragraph.h"
39 #include "rowpainter.h"
40 #include "version.h"
41
42 #include "graphics/GraphicsImage.h"
43 #include "graphics/GraphicsLoader.h"
44
45 #include "gettext.h"
46 #include "support/filetools.h" // LibFileSearch
47 #include "support/forkedcontr.h"
48
49 #include <boost/utility.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/current_function.hpp>
52 #include <boost/signals/trackable.hpp>
53
54 using lyx::support::libFileSearch;
55 using lyx::support::ForkedcallsController;
56
57 using std::endl;
58 using std::min;
59 using std::max;
60 using std::string;
61
62
63 namespace lyx {
64 namespace frontend {
65
66 // FIXME: The SplashScreen should be transfered to the
67 // LyXView and create a WorkArea only when a new buffer exists. This
68 // will allow to call WorkArea::redraw() in the constructor.
69 class SplashScreen : boost::noncopyable, boost::signals::trackable {
70 public:
71         /// This is a singleton class. Get the instance.
72         static SplashScreen const & get();
73         ///
74         lyx::graphics::Image const * image() const { return loader_.image(); }
75         ///
76         string const & text() const { return text_; }
77         ///
78         LyXFont const & font() const { return font_; }
79         ///
80         void connect(lyx::graphics::Loader::slot_type const & slot) const {
81                 loader_.connect(slot);
82         }
83         ///
84         void startLoading() const {
85                 if (loader_.status() == lyx::graphics::WaitingToLoad)
86                         loader_.startLoading();
87         }
88
89 private:
90         /** Make the c-tor private so we can control how many objects
91          *  are instantiated.
92          */
93         SplashScreen();
94
95         ///
96         lyx::graphics::Loader loader_;
97         /// The text to be written on top of the pixmap
98         string const text_;
99         /// in this font...
100         LyXFont font_;
101 };
102
103
104 SplashScreen const & SplashScreen::get()
105 {
106         static SplashScreen singleton;
107         return singleton;
108 }
109
110
111 SplashScreen::SplashScreen()
112         : text_(lyx_version ? lyx_version : "unknown")
113 {
114         if (!lyxrc.show_banner)
115                 return;
116
117         string const file = libFileSearch("images", "banner", "ppm");
118         if (file.empty())
119                 return;
120
121         // The font used to display the version info
122         font_.setFamily(LyXFont::SANS_FAMILY);
123         font_.setSeries(LyXFont::BOLD_SERIES);
124         font_.setSize(LyXFont::SIZE_NORMAL);
125         font_.setColor(LColor::yellow);
126
127         // Load up the graphics file
128         loader_.reset(file);
129 }
130
131 namespace {
132
133 // All the below connection objects are needed because of a bug in some
134 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
135 // to these connections we avoid a segfault upon startup, and also at exit.
136 // (Lgb)
137
138 boost::signals::connection timecon;
139
140 } // anon namespace
141
142 WorkArea::WorkArea(LyXView & lyx_view)
143         :  buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
144         cursor_visible_(false), cursor_timeout_(400)
145 {
146         // Start loading the pixmap as soon as possible
147         if (lyxrc.show_banner) {
148                 SplashScreen const & splash = SplashScreen::get();
149                 splash.connect(boost::bind(&WorkArea::checkAndGreyOut, this));
150                 splash.startLoading();
151         }
152
153         // Setup the signals
154         timecon = cursor_timeout_.timeout
155                 .connect(boost::bind(&WorkArea::toggleCursor, this));
156
157         cursor_timeout_.start();
158 }
159
160
161 void WorkArea::setBufferView(BufferView * buffer_view)
162 {
163         hideCursor();
164         buffer_view_ = buffer_view;
165         toggleCursor();
166 }
167
168
169 BufferView & WorkArea::bufferView()
170 {
171         return *buffer_view_;
172 }
173
174
175 BufferView const & WorkArea::bufferView() const
176 {
177         return *buffer_view_;
178 }
179
180
181 void WorkArea::checkAndGreyOut()
182 {
183         if (greyed_out_)
184                 greyOut();
185 }
186
187
188 void WorkArea::redraw()
189 {
190         if (!buffer_view_)
191                 return;
192
193         if (!buffer_view_->buffer()) {
194                 greyOut();
195                 return;
196         }
197
198         buffer_view_->updateMetrics(false);
199
200         updateScrollbar();
201
202         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
203         greyed_out_ = false;
204         getPainter().start();
205         paintText(*buffer_view_, vi, getPainter());
206         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
207         int const ymin = std::max(vi.y1, 0);
208         int const ymax =
209                 ( vi.p2 < vi.size - 1 ?  vi.y2 : height() );
210         expose(0, ymin, width(), ymax - ymin);
211         getPainter().end();
212
213         lyxerr[Debug::DEBUG]
214         << "  ymin = " << ymin << "  width() = " << width()
215                 << "  ymax-ymin = " << ymax-ymin << std::endl;
216 }
217
218
219 void WorkArea::processKeySym(LyXKeySymPtr key,
220                                                          key_modifier::state state)
221 {
222         hideCursor();
223         lyx_view_.getLyXFunc().processKeySym(key, state);
224
225         /* This is perhaps a bit of a hack. When we move
226          * around, or type, it's nice to be able to see
227          * the cursor immediately after the keypress. So
228          * we reset the toggle timeout and force the visibility
229          * of the cursor. Note we cannot do this inside
230          * dispatch() itself, because that's called recursively.
231          */
232 //      if (buffer_view_->available())
233         toggleCursor();
234         
235         // uneeded "redraw()" call commented out for now.
236         // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
237         // is not needed anymore, this line should be uncommented out
238         //redraw();
239 }
240
241
242 void WorkArea::dispatch(FuncRequest const & cmd0)
243 {
244         // Handle drag&drop
245         if (cmd0.action == LFUN_FILE_OPEN) {
246                 lyx_view_.dispatch(cmd0);
247                 return;
248         }
249
250         buffer_view_->workAreaDispatch(cmd0);
251
252         // Skip these when selecting
253         if (cmd0.action != LFUN_MOUSE_MOTION) {
254                 lyx_view_.updateLayoutChoice();
255                 lyx_view_.updateToolbars();
256         }
257
258         // Slight hack: this is only called currently when we
259         // clicked somewhere, so we force through the display
260         // of the new status here.
261         lyx_view_.clearMessage();
262
263         redraw();
264 }
265
266
267 void WorkArea::resizeBufferView()
268 {
269         lyx_view_.busy(true);
270         lyx_view_.message(_("Formatting document..."));
271         buffer_view_->workAreaResize(width(), height());
272         lyx_view_.updateLayoutChoice();
273         redraw();
274         lyx_view_.busy(false);
275         lyx_view_.clearMessage();
276 }
277
278
279 void WorkArea::updateScrollbar()
280 {
281         buffer_view_->updateScrollbar(); 
282         ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
283         setScrollbarParams(scroll_.height, scroll_.position,
284                 scroll_.lineScrollHeight);
285 }
286
287
288 void WorkArea::scrollBufferView(int position)
289 {
290         buffer_view_->scrollDocView(position);
291         redraw();
292         hideCursor();
293         if (lyxrc.cursor_follows_scrollbar) {
294                 buffer_view_->setCursorFromScrollbar();
295                 lyx_view_.updateLayoutChoice();
296         }
297         toggleCursor();
298 }
299
300
301 void WorkArea::greyOut()
302 {
303         greyed_out_ = true;
304         getPainter().start();
305
306         getPainter().fillRectangle(0, 0,
307                 width(),
308                 height(),
309                 LColor::bottomarea);
310
311         // Add a splash screen to the centre of the work area
312         SplashScreen const & splash = SplashScreen::get();
313         lyx::graphics::Image const * const splash_image = splash.image();
314         if (splash_image) {
315                 int const w = splash_image->getWidth();
316                 int const h = splash_image->getHeight();
317
318                 int x = (width() - w) / 2;
319                 int y = (height() - h) / 2;
320
321                 getPainter().image(x, y, w, h, *splash_image);
322
323                 x += 260;
324                 y += 265;
325
326                 string stext = splash.text();
327                 docstring dstext(stext.begin(), stext.end());
328                 getPainter().text(x, y, dstext, splash.font());
329         }
330         expose(0, 0, width(), height());
331         getPainter().end();
332 }
333
334
335 void WorkArea::showCursor()
336 {
337         if (cursor_visible_)
338                 return;
339
340         if (!buffer_view_->available())
341                 return;
342
343         CursorShape shape = BAR_SHAPE;
344
345         LyXText const & text = *buffer_view_->getLyXText();
346         LyXFont const & realfont = text.real_current_font;
347         BufferParams const & bp = buffer_view_->buffer()->params();
348         bool const samelang = realfont.language() == bp.language;
349         bool const isrtl = realfont.isVisibleRightToLeft();
350
351         if (!samelang || isrtl != bp.language->rightToLeft()) {
352                 shape = L_SHAPE;
353                 if (isrtl)
354                         shape = REVERSED_L_SHAPE;
355         }
356
357         // The ERT language hack needs fixing up
358         if (realfont.language() == latex_language)
359                 shape = BAR_SHAPE;
360
361         LyXFont const font = buffer_view_->cursor().getFont();
362         int const asc = font_metrics::maxAscent(font);
363         int const des = font_metrics::maxDescent(font);
364         int h = asc + des;
365         int x = 0;
366         int y = 0;
367         buffer_view_->cursor().getPos(x, y);
368         y -= asc;
369
370         // if it doesn't touch the screen, don't try to show it
371         if (y + h < 0 || y >= height())
372                 return;
373
374         cursor_visible_ = true;
375         showCursor(x, y, h, shape);
376 }
377
378
379 void WorkArea::hideCursor()
380 {
381         if (!cursor_visible_)
382                 return;
383
384         cursor_visible_ = false;
385         removeCursor();
386 }
387
388
389 void WorkArea::toggleCursor()
390 {
391         if (buffer_view_->buffer()) {
392
393                 if (cursor_visible_)
394                         hideCursor();
395                 else
396                         showCursor();
397
398                 // Use this opportunity to deal with any child processes that
399                 // have finished but are waiting to communicate this fact
400                 // to the rest of LyX.
401                 ForkedcallsController & fcc = ForkedcallsController::get();
402                 fcc.handleCompletedProcesses();
403         }
404
405         cursor_timeout_.restart();
406 }
407
408 } // namespace frontend
409 } // namespace lyx