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"
23 #include "bufferparams.h"
31 #include "metricsinfo.h"
32 #include "rowpainter.h"
35 #include "insets/updatableinset.h"
37 #include "graphics/GraphicsImage.h"
38 #include "graphics/GraphicsLoader.h"
40 #include "support/filetools.h" // LibFileSearch
42 #include <boost/utility.hpp>
43 #include <boost/bind.hpp>
44 #include <boost/signals/trackable.hpp>
46 using lyx::support::LibFileSearch;
55 class SplashScreen : boost::noncopyable, boost::signals::trackable {
57 /// This is a singleton class. Get the instance.
58 static SplashScreen const & get();
60 lyx::graphics::Image const * image() const { return loader_.image(); }
62 string const & text() const { return text_; }
64 LyXFont const & font() const { return font_; }
66 void connect(lyx::graphics::Loader::slot_type const & slot) const {
67 loader_.connect(slot);
70 void startLoading() const {
71 if (loader_.status() == lyx::graphics::WaitingToLoad)
72 loader_.startLoading();
76 /** Make the c-tor private so we can control how many objects
82 lyx::graphics::Loader loader_;
83 /// The text to be written on top of the pixmap
90 SplashScreen const & SplashScreen::get()
92 static SplashScreen singleton;
97 SplashScreen::SplashScreen()
98 : text_(lyx_version ? lyx_version : "unknown")
100 if (!lyxrc.show_banner)
103 string const file = LibFileSearch("images", "banner", "ppm");
107 // The font used to display the version info
108 font_.setFamily(LyXFont::SANS_FAMILY);
109 font_.setSeries(LyXFont::BOLD_SERIES);
110 font_.setSize(LyXFont::SIZE_NORMAL);
111 font_.setColor(LColor::yellow);
113 // Load up the graphics file
120 LyXScreen::LyXScreen()
121 : cursor_visible_(false), greyed_out_(true)
123 // Start loading the pixmap as soon as possible
124 if (lyxrc.show_banner) {
125 SplashScreen const & splash = SplashScreen::get();
126 splash.connect(boost::bind(&LyXScreen::greyOut, this));
127 splash.startLoading();
132 LyXScreen::~LyXScreen()
137 void LyXScreen::showCursor(BufferView & bv)
139 // this is needed to make sure we copy back the right
140 // pixmap on the hide for the Qt frontend
141 lyx_gui::sync_events();
149 Cursor_Shape shape = BAR_SHAPE;
151 LyXText const & text = *bv.getLyXText();
152 LyXFont const & realfont = text.real_current_font;
153 BufferParams const & bp = bv.buffer()->params();
154 bool const samelang = realfont.language() == bp.language;
155 bool const isrtl = realfont.isVisibleRightToLeft();
157 if (!samelang || isrtl != bp.language->RightToLeft()) {
160 shape = REVERSED_L_SHAPE;
163 // The ERT language hack needs fixing up
164 if (realfont.language() == latex_language)
167 int ascent = font_metrics::maxAscent(realfont);
168 int descent = font_metrics::maxDescent(realfont);
169 int h = ascent + descent;
172 int const top_y = bv.top_y();
174 if (bv.theLockingInset()) {
175 // Would be nice to clean this up to make some understandable sense...
176 UpdatableInset * inset = bv.theLockingInset();
177 inset->getCursor(bv, x, y);
179 // Non-obvious. The reason we have to have these
180 // extra checks is that the ->getCursor() calls rely
181 // on the inset's own knowledge of its screen position.
182 // If we scroll up or down in a big enough increment, the
183 // inset->draw() is not called: this doesn't update
184 // inset.top_baseline, so getCursor() returns an old value.
187 inset->getCursorPos(&bv, bx, by);
188 by += inset->insetInInsetY() + bv.text->cursor.y();
191 if (by > top_y + workarea().workHeight())
194 x = bv.text->cursor.x();
195 y = bv.text->cursor.y();
201 // if it doesn't fit entirely on the screen, don't try to show it
202 if (y < 0 || y + h > workarea().workHeight())
205 cursor_visible_ = true;
206 showCursor(x, y, h, shape);
210 void LyXScreen::hideCursor()
212 if (!cursor_visible_)
215 cursor_visible_ = false;
220 void LyXScreen::toggleCursor(BufferView & bv)
229 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
230 int /*x*/, int y, int asc, int desc)
232 int const vheight = workarea().workHeight();
233 int const topy = bv->top_y();
237 if (y + desc - topy >= vheight)
238 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
239 else if (y - asc < topy && topy > 0)
240 newtop = y - vheight / 4;
242 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
252 unsigned int LyXScreen::topCursorVisible(LyXText * text)
254 LyXCursor const & cursor = text->cursor;
255 int top_y = text->bv()->top_y();
257 unsigned int const vheight = workarea().workHeight();
259 RowList::iterator row = text->cursorRow();
261 if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
262 if (row->height() < vheight
263 && row->height() > vheight / 4) {
266 - row->baseline() - vheight;
268 // scroll down, the scroll region must be so big!!
269 newtop = cursor.y() - vheight / 2;
272 } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
273 if (row->height() < vheight && row->height() > vheight / 4) {
274 newtop = cursor.y() - row->baseline();
277 newtop = cursor.y() - vheight / 2;
278 newtop = min(newtop, top_y);
282 return max(newtop, 0);
286 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
288 // Is a change necessary?
289 int const newtop = topCursorVisible(text);
290 bool const result = (newtop != bv->top_y());
296 void LyXScreen::redraw(BufferView & bv)
298 greyed_out_ = !bv.text;
305 workarea().getPainter().start();
309 int const y = paintText(bv);
311 // maybe we have to clear the screen at the bottom
312 int const y2 = workarea().workHeight();
313 if (y < y2 && !bv.text->isInInset()) {
314 workarea().getPainter().fillRectangle(0, y,
315 workarea().workWidth(), y2 - y,
319 expose(0, 0, workarea().workWidth(), workarea().workHeight());
321 workarea().getPainter().end();
325 void LyXScreen::greyOut()
330 workarea().getPainter().start();
332 workarea().getPainter().fillRectangle(0, 0,
333 workarea().workWidth(),
334 workarea().workHeight(),
337 // Add a splash screen to the centre of the work area
338 SplashScreen const & splash = SplashScreen::get();
339 lyx::graphics::Image const * const splash_image = splash.image();
341 int const w = splash_image->getWidth();
342 int const h = splash_image->getHeight();
344 int x = (workarea().workWidth() - w) / 2;
345 int y = (workarea().workHeight() - h) / 2;
347 workarea().getPainter().image(x, y, w, h, *splash_image);
352 workarea().getPainter().text(x, y, splash.text(), splash.font());
354 expose(0, 0, workarea().workWidth(), workarea().workHeight());
355 workarea().getPainter().end();