3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
10 * Splash screen code added by Angus Leeming
16 #include "font_metrics.h"
21 #include "BufferView.h"
29 #include "metricsinfo.h"
30 #include "rowpainter.h"
33 #include "insets/updatableinset.h"
35 #include "graphics/GraphicsImage.h"
36 #include "graphics/GraphicsLoader.h"
38 #include "support/filetools.h" // LibFileSearch
40 #include <boost/utility.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/signals/trackable.hpp>
44 using namespace lyx::support;
52 class SplashScreen : boost::noncopyable, boost::signals::trackable {
54 /// This is a singleton class. Get the instance.
55 static SplashScreen const & get();
57 lyx::graphics::Image const * image() const { return loader_.image(); }
59 string const & text() const { return text_; }
61 LyXFont const & font() const { return font_; }
63 void connect(lyx::graphics::Loader::slot_type const & slot) const {
64 loader_.connect(slot);
67 void startLoading() const {
68 if (loader_.status() == lyx::graphics::WaitingToLoad)
69 loader_.startLoading();
73 /** Make the c-tor private so we can control how many objects
79 lyx::graphics::Loader loader_;
80 /// The text to be written on top of the pixmap
87 SplashScreen const & SplashScreen::get()
89 static SplashScreen singleton;
94 SplashScreen::SplashScreen()
95 : text_(lyx_version ? lyx_version : "unknown")
97 if (!lyxrc.show_banner)
100 string const file = LibFileSearch("images", "banner", "ppm");
104 // The font used to display the version info
105 font_.setFamily(LyXFont::SANS_FAMILY);
106 font_.setSeries(LyXFont::BOLD_SERIES);
107 font_.setSize(LyXFont::SIZE_NORMAL);
108 font_.setColor(LColor::yellow);
110 // Load up the graphics file
117 LyXScreen::LyXScreen()
118 : cursor_visible_(false), greyed_out_(true)
120 // Start loading the pixmap as soon as possible
121 if (lyxrc.show_banner) {
122 SplashScreen const & splash = SplashScreen::get();
123 splash.connect(boost::bind(&LyXScreen::greyOut, this));
124 splash.startLoading();
129 LyXScreen::~LyXScreen()
134 void LyXScreen::showCursor(BufferView & bv)
136 // this is needed to make sure we copy back the right
137 // pixmap on the hide for the Qt frontend
138 lyx_gui::sync_events();
146 Cursor_Shape shape = BAR_SHAPE;
148 LyXText const & text = *bv.getLyXText();
149 LyXFont const & realfont = text.real_current_font;
150 BufferParams const & bp = bv.buffer()->params();
151 bool const samelang = realfont.language() == bp.language;
152 bool const isrtl = realfont.isVisibleRightToLeft();
154 if (!samelang || isrtl != bp.language->RightToLeft()) {
157 shape = REVERSED_L_SHAPE;
160 // The ERT language hack needs fixing up
161 if (realfont.language() == latex_language)
164 int ascent = font_metrics::maxAscent(realfont);
165 int descent = font_metrics::maxDescent(realfont);
166 int h = ascent + descent;
169 int const top_y = bv.top_y();
171 if (bv.theLockingInset()) {
172 // Would be nice to clean this up to make some understandable sense...
173 UpdatableInset * inset = bv.theLockingInset();
174 inset->getCursor(bv, x, y);
176 // Non-obvious. The reason we have to have these
177 // extra checks is that the ->getCursor() calls rely
178 // on the inset's own knowledge of its screen position.
179 // If we scroll up or down in a big enough increment, the
180 // inset->draw() is not called: this doesn't update
181 // inset.top_baseline, so getCursor() returns an old value.
184 inset->getCursorPos(&bv, bx, by);
185 by += inset->insetInInsetY() + bv.text->cursor.y();
188 if (by > top_y + workarea().workHeight())
191 x = bv.text->cursor.x();
192 y = bv.text->cursor.y();
198 // if it doesn't fit entirely on the screen, don't try to show it
199 if (y < 0 || y + h > workarea().workHeight())
202 cursor_visible_ = true;
203 showCursor(x, y, h, shape);
207 void LyXScreen::hideCursor()
209 if (!cursor_visible_)
212 cursor_visible_ = false;
217 void LyXScreen::toggleCursor(BufferView & bv)
226 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
227 int /*x*/, int y, int asc, int desc)
229 int const vheight = workarea().workHeight();
230 int const topy = bv->top_y();
234 if (y + desc - topy >= vheight)
235 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
236 else if (y - asc < topy && topy > 0)
237 newtop = y - vheight / 4;
239 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
249 unsigned int LyXScreen::topCursorVisible(LyXText * text)
251 LyXCursor const & cursor = text->cursor;
252 int top_y = text->bv()->top_y();
254 unsigned int const vheight = workarea().workHeight();
256 RowList::iterator row = text->cursorRow();
258 if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
259 if (row->height() < vheight
260 && row->height() > vheight / 4) {
263 - row->baseline() - vheight;
265 // scroll down, the scroll region must be so big!!
266 newtop = cursor.y() - vheight / 2;
269 } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
270 if (row->height() < vheight && row->height() > vheight / 4) {
271 newtop = cursor.y() - row->baseline();
274 newtop = cursor.y() - vheight / 2;
275 newtop = min(newtop, top_y);
279 return max(newtop, 0);
283 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
285 // Is a change necessary?
286 int const newtop = topCursorVisible(text);
287 bool const result = (newtop != bv->top_y());
293 void LyXScreen::redraw(BufferView & bv)
295 greyed_out_ = !bv.text;
302 workarea().getPainter().start();
306 int const y = paintText(bv);
308 // maybe we have to clear the screen at the bottom
309 int const y2 = workarea().workHeight();
310 if (y < y2 && !bv.text->isInInset()) {
311 workarea().getPainter().fillRectangle(0, y,
312 workarea().workWidth(), y2 - y,
316 expose(0, 0, workarea().workWidth(), workarea().workHeight());
318 workarea().getPainter().end();
322 void LyXScreen::greyOut()
327 workarea().getPainter().start();
329 workarea().getPainter().fillRectangle(0, 0,
330 workarea().workWidth(),
331 workarea().workHeight(),
334 // Add a splash screen to the centre of the work area
335 SplashScreen const & splash = SplashScreen::get();
336 lyx::graphics::Image const * const splash_image = splash.image();
338 int const w = splash_image->getWidth();
339 int const h = splash_image->getHeight();
341 int x = (workarea().workWidth() - w) / 2;
342 int y = (workarea().workHeight() - h) / 2;
344 workarea().getPainter().image(x, y, w, h, *splash_image);
349 workarea().getPainter().text(x, y, splash.text(), splash.font());
351 expose(0, 0, workarea().workWidth(), workarea().workHeight());
352 workarea().getPainter().end();