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;
54 class SplashScreen : boost::noncopyable, boost::signals::trackable {
56 /// This is a singleton class. Get the instance.
57 static SplashScreen const & get();
59 lyx::graphics::Image const * image() const { return loader_.image(); }
61 string const & text() const { return text_; }
63 LyXFont const & font() const { return font_; }
65 void connect(lyx::graphics::Loader::slot_type const & slot) const {
66 loader_.connect(slot);
69 void startLoading() const {
70 if (loader_.status() == lyx::graphics::WaitingToLoad)
71 loader_.startLoading();
75 /** Make the c-tor private so we can control how many objects
81 lyx::graphics::Loader loader_;
82 /// The text to be written on top of the pixmap
89 SplashScreen const & SplashScreen::get()
91 static SplashScreen singleton;
96 SplashScreen::SplashScreen()
97 : text_(lyx_version ? lyx_version : "unknown")
99 if (!lyxrc.show_banner)
102 string const file = LibFileSearch("images", "banner", "ppm");
106 // The font used to display the version info
107 font_.setFamily(LyXFont::SANS_FAMILY);
108 font_.setSeries(LyXFont::BOLD_SERIES);
109 font_.setSize(LyXFont::SIZE_NORMAL);
110 font_.setColor(LColor::yellow);
112 // Load up the graphics file
119 LyXScreen::LyXScreen()
120 : cursor_visible_(false), greyed_out_(true)
122 // Start loading the pixmap as soon as possible
123 if (lyxrc.show_banner) {
124 SplashScreen const & splash = SplashScreen::get();
125 splash.connect(boost::bind(&LyXScreen::greyOut, this));
126 splash.startLoading();
131 LyXScreen::~LyXScreen()
136 void LyXScreen::showCursor(BufferView & bv)
138 // this is needed to make sure we copy back the right
139 // pixmap on the hide for the Qt frontend
140 lyx_gui::sync_events();
148 Cursor_Shape shape = BAR_SHAPE;
150 LyXText const & text = *bv.getLyXText();
151 LyXFont const & realfont = text.real_current_font;
152 BufferParams const & bp = bv.buffer()->params();
153 bool const samelang = realfont.language() == bp.language;
154 bool const isrtl = realfont.isVisibleRightToLeft();
156 if (!samelang || isrtl != bp.language->RightToLeft()) {
159 shape = REVERSED_L_SHAPE;
162 // The ERT language hack needs fixing up
163 if (realfont.language() == latex_language)
166 int ascent = font_metrics::maxAscent(realfont);
167 int descent = font_metrics::maxDescent(realfont);
168 int h = ascent + descent;
171 int const top_y = bv.top_y();
173 if (bv.theLockingInset()) {
174 // Would be nice to clean this up to make some understandable sense...
175 UpdatableInset * inset = bv.theLockingInset();
176 inset->getCursor(bv, x, y);
178 // Non-obvious. The reason we have to have these
179 // extra checks is that the ->getCursor() calls rely
180 // on the inset's own knowledge of its screen position.
181 // If we scroll up or down in a big enough increment, the
182 // inset->draw() is not called: this doesn't update
183 // inset.top_baseline, so getCursor() returns an old value.
186 inset->getCursorPos(&bv, bx, by);
187 by += inset->insetInInsetY() + bv.text->cursor.y();
190 if (by > top_y + workarea().workHeight())
193 x = bv.text->cursor.x();
194 y = bv.text->cursor.y();
200 // if it doesn't fit entirely on the screen, don't try to show it
201 if (y < 0 || y + h > workarea().workHeight())
204 cursor_visible_ = true;
205 showCursor(x, y, h, shape);
209 void LyXScreen::hideCursor()
211 if (!cursor_visible_)
214 cursor_visible_ = false;
219 void LyXScreen::toggleCursor(BufferView & bv)
228 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
229 int /*x*/, int y, int asc, int desc)
231 int const vheight = workarea().workHeight();
232 int const topy = bv->top_y();
236 if (y + desc - topy >= vheight)
237 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
238 else if (y - asc < topy && topy > 0)
239 newtop = y - vheight / 4;
241 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
251 unsigned int LyXScreen::topCursorVisible(LyXText * text)
253 LyXCursor const & cursor = text->cursor;
254 int top_y = text->bv()->top_y();
256 unsigned int const vheight = workarea().workHeight();
258 RowList::iterator row = text->cursorRow();
260 if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
261 if (row->height() < vheight
262 && row->height() > vheight / 4) {
265 - row->baseline() - vheight;
267 // scroll down, the scroll region must be so big!!
268 newtop = cursor.y() - vheight / 2;
271 } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
272 if (row->height() < vheight && row->height() > vheight / 4) {
273 newtop = cursor.y() - row->baseline();
276 newtop = cursor.y() - vheight / 2;
277 newtop = min(newtop, top_y);
281 return max(newtop, 0);
285 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
287 // Is a change necessary?
288 int const newtop = topCursorVisible(text);
289 bool const result = (newtop != bv->top_y());
295 void LyXScreen::redraw(BufferView & bv)
297 greyed_out_ = !bv.text;
304 workarea().getPainter().start();
308 int const y = paintText(bv);
310 // maybe we have to clear the screen at the bottom
311 int const y2 = workarea().workHeight();
312 if (y < y2 && !bv.text->isInInset()) {
313 workarea().getPainter().fillRectangle(0, y,
314 workarea().workWidth(), y2 - y,
318 expose(0, 0, workarea().workWidth(), workarea().workHeight());
320 workarea().getPainter().end();
324 void LyXScreen::greyOut()
329 workarea().getPainter().start();
331 workarea().getPainter().fillRectangle(0, 0,
332 workarea().workWidth(),
333 workarea().workHeight(),
336 // Add a splash screen to the centre of the work area
337 SplashScreen const & splash = SplashScreen::get();
338 lyx::graphics::Image const * const splash_image = splash.image();
340 int const w = splash_image->getWidth();
341 int const h = splash_image->getHeight();
343 int x = (workarea().workWidth() - w) / 2;
344 int y = (workarea().workHeight() - h) / 2;
346 workarea().getPainter().image(x, y, w, h, *splash_image);
351 workarea().getPainter().text(x, y, splash.text(), splash.font());
353 expose(0, 0, workarea().workWidth(), workarea().workHeight());
354 workarea().getPainter().end();