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