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"
25 #include "BufferView.h"
27 #include "bufferparams.h"
28 #include "coordcache.h"
38 #include "metricsinfo.h"
39 #include "paragraph.h"
40 #include "rowpainter.h"
43 #include "graphics/GraphicsImage.h"
44 #include "graphics/GraphicsLoader.h"
47 #include "support/filetools.h" // LibFileSearch
48 #include "support/forkedcontr.h"
50 #include <boost/utility.hpp>
51 #include <boost/bind.hpp>
52 #include <boost/current_function.hpp>
54 using lyx::support::libFileSearch;
55 using lyx::support::ForkedcallsController;
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 {
71 /// This is a singleton class. Get the instance.
72 static SplashScreen const & get();
74 lyx::graphics::Image const * image() const { return loader_.image(); }
76 string const & text() const { return text_; }
78 LyXFont const & font() const { return font_; }
80 void connect(lyx::graphics::Loader::slot_type const & slot) const {
81 loader_.connect(slot);
84 void startLoading() const {
85 if (loader_.status() == lyx::graphics::WaitingToLoad)
86 loader_.startLoading();
90 /** Make the c-tor private so we can control how many objects
96 lyx::graphics::Loader loader_;
97 /// The text to be written on top of the pixmap
104 SplashScreen const & SplashScreen::get()
106 static SplashScreen singleton;
111 SplashScreen::SplashScreen()
112 : text_(lyx_version ? lyx_version : "unknown")
114 if (!lyxrc.show_banner)
117 string const file = libFileSearch("images", "banner", "ppm");
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);
127 // Load up the graphics file
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.
138 boost::signals::connection timecon;
142 WorkArea::WorkArea(LyXView & lyx_view)
143 : buffer_view_(0), lyx_view_(lyx_view), greyed_out_(true),
144 cursor_visible_(false), cursor_timeout_(400)
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();
154 timecon = cursor_timeout_.timeout
155 .connect(boost::bind(&WorkArea::toggleCursor, this));
157 cursor_timeout_.start();
161 void WorkArea::setBufferView(BufferView * buffer_view)
164 message_connection_.disconnect();
165 lyx_view_.disconnectBufferView();
168 theApp->setBufferView(buffer_view);
171 buffer_view_ = buffer_view;
174 message_connection_ = buffer_view_->message.connect(
175 boost::bind(&WorkArea::displayMessage, this, _1));
177 lyx_view_.connectBufferView(*buffer_view);
181 BufferView & WorkArea::bufferView()
183 return *buffer_view_;
187 BufferView const & WorkArea::bufferView() const
189 return *buffer_view_;
193 void WorkArea::checkAndGreyOut()
200 void WorkArea::redraw()
205 if (!buffer_view_->buffer()) {
211 buffer_view_->updateMetrics(false);
215 ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
217 getPainter().start();
218 paintText(*buffer_view_, vi, getPainter());
219 lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
220 int const ymin = std::max(vi.y1, 0);
222 ( vi.p2 < vi.size - 1 ? vi.y2 : height() );
223 expose(0, ymin, width(), ymax - ymin);
227 << " ymin = " << ymin << " width() = " << width()
228 << " ymax-ymin = " << ymax-ymin << std::endl;
232 void WorkArea::processKeySym(LyXKeySymPtr key,
233 key_modifier::state state)
236 theLyXFunc().processKeySym(key, state);
238 /* This is perhaps a bit of a hack. When we move
239 * around, or type, it's nice to be able to see
240 * the cursor immediately after the keypress. So
241 * we reset the toggle timeout and force the visibility
242 * of the cursor. Note we cannot do this inside
243 * dispatch() itself, because that's called recursively.
245 // if (buffer_view_->buffer())
248 // uneeded "redraw()" call commented out for now.
249 // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
250 // is not needed anymore, this line should be uncommented out
255 void WorkArea::dispatch(FuncRequest const & cmd0)
258 if (cmd0.action == LFUN_FILE_OPEN) {
259 lyx_view_.dispatch(cmd0);
263 buffer_view_->workAreaDispatch(cmd0);
265 // Skip these when selecting
266 if (cmd0.action != LFUN_MOUSE_MOTION) {
267 lyx_view_.updateLayoutChoice();
268 lyx_view_.updateMenubar();
269 lyx_view_.updateToolbars();
272 // Slight hack: this is only called currently when we
273 // clicked somewhere, so we force through the display
274 // of the new status here.
275 lyx_view_.clearMessage();
281 void WorkArea::resizeBufferView()
283 lyx_view_.busy(true);
284 lyx_view_.message(_("Formatting document..."));
285 buffer_view_->workAreaResize(width(), height());
286 lyx_view_.updateLayoutChoice();
288 lyx_view_.busy(false);
289 lyx_view_.clearMessage();
293 void WorkArea::updateScrollbar()
295 buffer_view_->updateScrollbar();
296 ScrollbarParameters const & scroll_ = buffer_view_->scrollbarParameters();
297 setScrollbarParams(scroll_.height, scroll_.position,
298 scroll_.lineScrollHeight);
302 void WorkArea::scrollBufferView(int position)
304 buffer_view_->scrollDocView(position);
307 if (lyxrc.cursor_follows_scrollbar) {
308 buffer_view_->setCursorFromScrollbar();
309 lyx_view_.updateLayoutChoice();
315 void WorkArea::greyOut()
318 getPainter().start();
320 getPainter().fillRectangle(0, 0,
325 // Add a splash screen to the centre of the work area
326 SplashScreen const & splash = SplashScreen::get();
327 lyx::graphics::Image const * const splash_image = splash.image();
329 int const w = splash_image->getWidth();
330 int const h = splash_image->getHeight();
332 int x = (width() - w) / 2;
333 int y = (height() - h) / 2;
335 getPainter().image(x, y, w, h, *splash_image);
340 string stext = splash.text();
341 docstring dstext(stext.begin(), stext.end());
342 getPainter().text(x, y, dstext, splash.font());
344 expose(0, 0, width(), height());
349 void WorkArea::showCursor()
354 if (!buffer_view_->buffer())
357 CursorShape shape = BAR_SHAPE;
359 LyXText const & text = *buffer_view_->getLyXText();
360 LyXFont const & realfont = text.real_current_font;
361 BufferParams const & bp = buffer_view_->buffer()->params();
362 bool const samelang = realfont.language() == bp.language;
363 bool const isrtl = realfont.isVisibleRightToLeft();
365 if (!samelang || isrtl != bp.language->rightToLeft()) {
368 shape = REVERSED_L_SHAPE;
371 // The ERT language hack needs fixing up
372 if (realfont.language() == latex_language)
375 LyXFont const font = buffer_view_->cursor().getFont();
376 FontMetrics const & fm = theFontMetrics(font);
377 int const asc = fm.maxAscent();
378 int const des = fm.maxDescent();
382 buffer_view_->cursor().getPos(x, y);
385 // if it doesn't touch the screen, don't try to show it
386 if (y + h < 0 || y >= height())
389 cursor_visible_ = true;
390 showCursor(x, y, h, shape);
394 void WorkArea::hideCursor()
396 if (!cursor_visible_)
399 cursor_visible_ = false;
404 void WorkArea::toggleCursor()
406 if (buffer_view_->buffer()) {
413 // Use this opportunity to deal with any child processes that
414 // have finished but are waiting to communicate this fact
415 // to the rest of LyX.
416 ForkedcallsController & fcc = ForkedcallsController::get();
417 fcc.handleCompletedProcesses();
420 cursor_timeout_.restart();
424 void WorkArea::displayMessage(lyx::docstring const & message)
426 lyx_view_.message(message);
429 } // namespace frontend