3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
11 * Splash screen code added by Angus Leeming
18 #include "font_metrics.h"
19 #include "funcrequest.h"
24 #include "BufferView.h"
26 #include "bufferparams.h"
27 #include "coordcache.h"
37 #include "metricsinfo.h"
38 #include "paragraph.h"
39 #include "rowpainter.h"
42 #include "graphics/GraphicsImage.h"
43 #include "graphics/GraphicsLoader.h"
46 #include "support/filetools.h" // LibFileSearch
47 #include "support/forkedcontr.h"
49 #include <boost/utility.hpp>
50 #include <boost/bind.hpp>
51 #include <boost/current_function.hpp>
53 using lyx::support::libFileSearch;
54 using lyx::support::ForkedcallsController;
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 {
70 /// This is a singleton class. Get the instance.
71 static SplashScreen const & get();
73 lyx::graphics::Image const * image() const { return loader_.image(); }
75 string const & text() const { return text_; }
77 LyXFont const & font() const { return font_; }
79 void connect(lyx::graphics::Loader::slot_type const & slot) const {
80 loader_.connect(slot);
83 void startLoading() const {
84 if (loader_.status() == lyx::graphics::WaitingToLoad)
85 loader_.startLoading();
89 /** Make the c-tor private so we can control how many objects
95 lyx::graphics::Loader loader_;
96 /// The text to be written on top of the pixmap
103 SplashScreen const & SplashScreen::get()
105 static SplashScreen singleton;
110 SplashScreen::SplashScreen()
111 : text_(lyx_version ? lyx_version : "unknown")
113 if (!lyxrc.show_banner)
116 string const file = libFileSearch("images", "banner", "ppm");
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);
126 // Load up the graphics file
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.
137 boost::signals::connection timecon;
141 WorkArea::WorkArea(LyXView & lyx_view)
142 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
143 cursor_visible_(false), cursor_timeout_(400)
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();
153 timecon = cursor_timeout_.timeout
154 .connect(boost::bind(&WorkArea::toggleCursor, this));
156 cursor_timeout_.start();
160 void WorkArea::setBufferView(BufferView * buffer_view)
163 message_connection_.disconnect();
166 buffer_view_ = buffer_view;
169 message_connection_ = buffer_view_->message.connect(
170 boost::bind(&WorkArea::displayMessage, this, _1));
174 BufferView & WorkArea::bufferView()
176 return *buffer_view_;
180 BufferView const & WorkArea::bufferView() const
182 return *buffer_view_;
186 void WorkArea::checkAndGreyOut()
193 void WorkArea::redraw()
198 if (!buffer_view_->buffer()) {
204 buffer_view_->updateMetrics(false);
208 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
210 getPainter().start();
211 paintText(*buffer_view_, vi, getPainter());
212 lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
213 int const ymin = std::max(vi.y1, 0);
215 ( vi.p2 < vi.size - 1 ? vi.y2 : height() );
216 expose(0, ymin, width(), ymax - ymin);
220 << " ymin = " << ymin << " width() = " << width()
221 << " ymax-ymin = " << ymax-ymin << std::endl;
225 void WorkArea::processKeySym(LyXKeySymPtr key,
226 key_modifier::state state)
229 lyx_view_.getLyXFunc().processKeySym(key, state);
231 /* This is perhaps a bit of a hack. When we move
232 * around, or type, it's nice to be able to see
233 * the cursor immediately after the keypress. So
234 * we reset the toggle timeout and force the visibility
235 * of the cursor. Note we cannot do this inside
236 * dispatch() itself, because that's called recursively.
238 // if (buffer_view_->available())
241 // uneeded "redraw()" call commented out for now.
242 // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
243 // is not needed anymore, this line should be uncommented out
248 void WorkArea::dispatch(FuncRequest const & cmd0)
251 if (cmd0.action == LFUN_FILE_OPEN) {
252 lyx_view_.dispatch(cmd0);
256 buffer_view_->workAreaDispatch(cmd0);
258 // Skip these when selecting
259 if (cmd0.action != LFUN_MOUSE_MOTION) {
260 lyx_view_.updateLayoutChoice();
261 lyx_view_.updateToolbars();
264 // Slight hack: this is only called currently when we
265 // clicked somewhere, so we force through the display
266 // of the new status here.
267 lyx_view_.clearMessage();
273 void WorkArea::resizeBufferView()
275 lyx_view_.busy(true);
276 lyx_view_.message(_("Formatting document..."));
277 buffer_view_->workAreaResize(width(), height());
278 lyx_view_.updateLayoutChoice();
280 lyx_view_.busy(false);
281 lyx_view_.clearMessage();
285 void WorkArea::updateScrollbar()
287 buffer_view_->updateScrollbar();
288 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
289 setScrollbarParams(scroll_.height, scroll_.position,
290 scroll_.lineScrollHeight);
294 void WorkArea::scrollBufferView(int position)
296 buffer_view_->scrollDocView(position);
299 if (lyxrc.cursor_follows_scrollbar) {
300 buffer_view_->setCursorFromScrollbar();
301 lyx_view_.updateLayoutChoice();
307 void WorkArea::greyOut()
310 getPainter().start();
312 getPainter().fillRectangle(0, 0,
317 // Add a splash screen to the centre of the work area
318 SplashScreen const & splash = SplashScreen::get();
319 lyx::graphics::Image const * const splash_image = splash.image();
321 int const w = splash_image->getWidth();
322 int const h = splash_image->getHeight();
324 int x = (width() - w) / 2;
325 int y = (height() - h) / 2;
327 getPainter().image(x, y, w, h, *splash_image);
332 string stext = splash.text();
333 docstring dstext(stext.begin(), stext.end());
334 getPainter().text(x, y, dstext, splash.font());
336 expose(0, 0, width(), height());
341 void WorkArea::showCursor()
346 if (!buffer_view_->available())
349 CursorShape shape = BAR_SHAPE;
351 LyXText const & text = *buffer_view_->getLyXText();
352 LyXFont const & realfont = text.real_current_font;
353 BufferParams const & bp = buffer_view_->buffer()->params();
354 bool const samelang = realfont.language() == bp.language;
355 bool const isrtl = realfont.isVisibleRightToLeft();
357 if (!samelang || isrtl != bp.language->rightToLeft()) {
360 shape = REVERSED_L_SHAPE;
363 // The ERT language hack needs fixing up
364 if (realfont.language() == latex_language)
367 LyXFont const font = buffer_view_->cursor().getFont();
368 int const asc = font_metrics::maxAscent(font);
369 int const des = font_metrics::maxDescent(font);
373 buffer_view_->cursor().getPos(x, y);
376 // if it doesn't touch the screen, don't try to show it
377 if (y + h < 0 || y >= height())
380 cursor_visible_ = true;
381 showCursor(x, y, h, shape);
385 void WorkArea::hideCursor()
387 if (!cursor_visible_)
390 cursor_visible_ = false;
395 void WorkArea::toggleCursor()
397 if (buffer_view_->buffer()) {
404 // Use this opportunity to deal with any child processes that
405 // have finished but are waiting to communicate this fact
406 // to the rest of LyX.
407 ForkedcallsController & fcc = ForkedcallsController::get();
408 fcc.handleCompletedProcesses();
411 cursor_timeout_.restart();
415 void WorkArea::displayMessage(std::string const & message)
417 lyx_view_.message(message);
420 } // namespace frontend