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
16 #include "frontends/WorkArea.h"
18 #include "frontends/Application.h"
19 #include "frontends/FontMetrics.h"
21 #include "funcrequest.h"
26 #include "BufferView.h"
28 #include "bufferparams.h"
29 #include "coordcache.h"
39 #include "metricsinfo.h"
40 #include "paragraph.h"
41 #include "rowpainter.h"
44 #include "graphics/GraphicsImage.h"
45 #include "graphics/GraphicsLoader.h"
48 #include "support/filetools.h" // LibFileSearch
49 #include "support/forkedcontr.h"
51 #include <boost/utility.hpp>
52 #include <boost/bind.hpp>
53 #include <boost/current_function.hpp>
55 using lyx::support::libFileSearch;
56 using lyx::support::ForkedcallsController;
67 // FIXME: The SplashScreen should be transfered to the
68 // LyXView and create a WorkArea only when a new buffer exists. This
69 // will allow to call WorkArea::redraw() in the constructor.
70 class SplashScreen : boost::noncopyable, boost::signals::trackable {
72 /// This is a singleton class. Get the instance.
73 static SplashScreen const & get();
75 lyx::graphics::Image const * image() const { return loader_.image(); }
77 string const & text() const { return text_; }
79 LyXFont const & font() const { return font_; }
81 void connect(lyx::graphics::Loader::slot_type const & slot) const {
82 loader_.connect(slot);
85 void startLoading() const {
86 if (loader_.status() == lyx::graphics::WaitingToLoad)
87 loader_.startLoading();
91 /** Make the c-tor private so we can control how many objects
97 lyx::graphics::Loader loader_;
98 /// The text to be written on top of the pixmap
105 SplashScreen const & SplashScreen::get()
107 static SplashScreen singleton;
112 SplashScreen::SplashScreen()
113 : text_(lyx_version ? lyx_version : "unknown")
115 if (!lyxrc.show_banner)
118 string const file = libFileSearch("images", "banner", "ppm");
122 // The font used to display the version info
123 font_.setFamily(LyXFont::SANS_FAMILY);
124 font_.setSeries(LyXFont::BOLD_SERIES);
125 font_.setSize(LyXFont::SIZE_NORMAL);
126 font_.setColor(LColor::yellow);
128 // Load up the graphics file
134 // All the below connection objects are needed because of a bug in some
135 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
136 // to these connections we avoid a segfault upon startup, and also at exit.
139 boost::signals::connection timecon;
143 WorkArea::WorkArea(LyXView & lyx_view)
144 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
145 cursor_visible_(false), cursor_timeout_(400)
147 // Start loading the pixmap as soon as possible
148 if (lyxrc.show_banner) {
149 SplashScreen const & splash = SplashScreen::get();
150 splash.connect(boost::bind(&WorkArea::checkAndGreyOut, this));
151 splash.startLoading();
155 timecon = cursor_timeout_.timeout
156 .connect(boost::bind(&WorkArea::toggleCursor, this));
158 cursor_timeout_.start();
162 void WorkArea::setBufferView(BufferView * buffer_view)
165 message_connection_.disconnect();
166 lyx_view_.disconnectBufferView();
169 theApp->setBufferView(buffer_view);
172 buffer_view_ = buffer_view;
175 message_connection_ = buffer_view_->message.connect(
176 boost::bind(&WorkArea::displayMessage, this, _1));
178 lyx_view_.connectBufferView(*buffer_view);
182 BufferView & WorkArea::bufferView()
184 return *buffer_view_;
188 BufferView const & WorkArea::bufferView() const
190 return *buffer_view_;
194 void WorkArea::checkAndGreyOut()
201 void WorkArea::redraw()
206 if (!buffer_view_->buffer()) {
212 buffer_view_->updateMetrics(false);
216 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
218 getPainter().start();
219 paintText(*buffer_view_, vi, getPainter());
220 lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
221 int const ymin = std::max(vi.y1, 0);
223 ( vi.p2 < vi.size - 1 ? vi.y2 : height() );
224 expose(0, ymin, width(), ymax - ymin);
228 << " ymin = " << ymin << " width() = " << width()
229 << " ymax-ymin = " << ymax-ymin << std::endl;
233 void WorkArea::processKeySym(LyXKeySymPtr key,
234 key_modifier::state state)
237 theLyXFunc().processKeySym(key, state);
239 /* This is perhaps a bit of a hack. When we move
240 * around, or type, it's nice to be able to see
241 * the cursor immediately after the keypress. So
242 * we reset the toggle timeout and force the visibility
243 * of the cursor. Note we cannot do this inside
244 * dispatch() itself, because that's called recursively.
246 // if (buffer_view_->buffer())
249 // uneeded "redraw()" call commented out for now.
250 // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
251 // is not needed anymore, this line should be uncommented out
256 void WorkArea::dispatch(FuncRequest const & cmd0)
259 if (cmd0.action == LFUN_FILE_OPEN) {
260 lyx_view_.dispatch(cmd0);
264 buffer_view_->workAreaDispatch(cmd0);
266 // Skip these when selecting
267 if (cmd0.action != LFUN_MOUSE_MOTION) {
268 lyx_view_.updateLayoutChoice();
269 lyx_view_.updateMenubar();
270 lyx_view_.updateToolbars();
273 // Slight hack: this is only called currently when we
274 // clicked somewhere, so we force through the display
275 // of the new status here.
276 lyx_view_.clearMessage();
282 void WorkArea::resizeBufferView()
284 lyx_view_.busy(true);
285 lyx_view_.message(_("Formatting document..."));
286 buffer_view_->workAreaResize(width(), height());
287 lyx_view_.updateLayoutChoice();
289 lyx_view_.busy(false);
290 lyx_view_.clearMessage();
294 void WorkArea::updateScrollbar()
296 buffer_view_->updateScrollbar();
297 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
298 setScrollbarParams(scroll_.height, scroll_.position,
299 scroll_.lineScrollHeight);
303 void WorkArea::scrollBufferView(int position)
305 buffer_view_->scrollDocView(position);
308 if (lyxrc.cursor_follows_scrollbar) {
309 buffer_view_->setCursorFromScrollbar();
310 lyx_view_.updateLayoutChoice();
316 void WorkArea::greyOut()
319 getPainter().start();
321 getPainter().fillRectangle(0, 0,
326 // Add a splash screen to the centre of the work area
327 SplashScreen const & splash = SplashScreen::get();
328 lyx::graphics::Image const * const splash_image = splash.image();
330 int const w = splash_image->getWidth();
331 int const h = splash_image->getHeight();
333 int x = (width() - w) / 2;
334 int y = (height() - h) / 2;
336 getPainter().image(x, y, w, h, *splash_image);
341 string stext = splash.text();
342 docstring dstext(stext.begin(), stext.end());
343 getPainter().text(x, y, dstext, splash.font());
345 expose(0, 0, width(), height());
350 void WorkArea::showCursor()
355 if (!buffer_view_->buffer())
358 CursorShape shape = BAR_SHAPE;
360 LyXText const & text = *buffer_view_->getLyXText();
361 LyXFont const & realfont = text.real_current_font;
362 BufferParams const & bp = buffer_view_->buffer()->params();
363 bool const samelang = realfont.language() == bp.language;
364 bool const isrtl = realfont.isVisibleRightToLeft();
366 if (!samelang || isrtl != bp.language->rightToLeft()) {
369 shape = REVERSED_L_SHAPE;
372 // The ERT language hack needs fixing up
373 if (realfont.language() == latex_language)
376 LyXFont const font = buffer_view_->cursor().getFont();
377 FontMetrics const & fm = theFontMetrics(font);
378 int const asc = fm.maxAscent();
379 int const des = fm.maxDescent();
383 buffer_view_->cursor().getPos(x, y);
386 // if it doesn't touch the screen, don't try to show it
387 if (y + h < 0 || y >= height())
390 cursor_visible_ = true;
391 showCursor(x, y, h, shape);
395 void WorkArea::hideCursor()
397 if (!cursor_visible_)
400 cursor_visible_ = false;
405 void WorkArea::toggleCursor()
407 if (buffer_view_->buffer()) {
414 // Use this opportunity to deal with any child processes that
415 // have finished but are waiting to communicate this fact
416 // to the rest of LyX.
417 ForkedcallsController & fcc = ForkedcallsController::get();
418 fcc.handleCompletedProcesses();
421 cursor_timeout_.restart();
425 void WorkArea::displayMessage(lyx::docstring const & message)
427 lyx_view_.message(message);
430 } // namespace frontend