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;
56 class SplashScreen : boost::noncopyable, boost::signals::trackable {
58 /// This is a singleton class. Get the instance.
59 static SplashScreen const & get();
61 lyx::graphics::Image const * image() const { return loader_.image(); }
63 string const & text() const { return text_; }
65 LyXFont const & font() const { return font_; }
67 void connect(lyx::graphics::Loader::slot_type const & slot) const {
68 loader_.connect(slot);
71 void startLoading() const {
72 if (loader_.status() == lyx::graphics::WaitingToLoad)
73 loader_.startLoading();
77 /** Make the c-tor private so we can control how many objects
83 lyx::graphics::Loader loader_;
84 /// The text to be written on top of the pixmap
91 SplashScreen const & SplashScreen::get()
93 static SplashScreen singleton;
98 SplashScreen::SplashScreen()
99 : text_(lyx_version ? lyx_version : "unknown")
101 if (!lyxrc.show_banner)
104 string const file = LibFileSearch("images", "banner", "ppm");
108 // The font used to display the version info
109 font_.setFamily(LyXFont::SANS_FAMILY);
110 font_.setSeries(LyXFont::BOLD_SERIES);
111 font_.setSize(LyXFont::SIZE_NORMAL);
112 font_.setColor(LColor::yellow);
114 // Load up the graphics file
121 LyXScreen::LyXScreen()
122 : cursor_visible_(false), greyed_out_(true)
124 // Start loading the pixmap as soon as possible
125 if (lyxrc.show_banner) {
126 SplashScreen const & splash = SplashScreen::get();
127 splash.connect(boost::bind(&LyXScreen::greyOut, this));
128 splash.startLoading();
133 LyXScreen::~LyXScreen()
138 void LyXScreen::showCursor(BufferView & bv)
140 // this is needed to make sure we copy back the right
141 // pixmap on the hide for the Qt frontend
142 lyx_gui::sync_events();
150 Cursor_Shape shape = BAR_SHAPE;
152 LyXText const & text = *bv.getLyXText();
153 LyXFont const & realfont = text.real_current_font;
154 BufferParams const & bp = bv.buffer()->params();
155 bool const samelang = realfont.language() == bp.language;
156 bool const isrtl = realfont.isVisibleRightToLeft();
158 if (!samelang || isrtl != bp.language->RightToLeft()) {
161 shape = REVERSED_L_SHAPE;
164 // The ERT language hack needs fixing up
165 if (realfont.language() == latex_language)
168 int ascent = font_metrics::maxAscent(realfont);
169 int descent = font_metrics::maxDescent(realfont);
170 int h = ascent + descent;
173 int const top_y = bv.top_y();
175 if (bv.theLockingInset()) {
176 // Would be nice to clean this up to make some understandable sense...
177 UpdatableInset * inset = bv.theLockingInset();
178 inset->getCursor(bv, x, y);
180 // Non-obvious. The reason we have to have these
181 // extra checks is that the ->getCursor() calls rely
182 // on the inset's own knowledge of its screen position.
183 // If we scroll up or down in a big enough increment, the
184 // inset->draw() is not called: this doesn't update
185 // inset.top_baseline, so getCursor() returns an old value.
188 inset->getCursorPos(&bv, bx, by);
189 by += inset->insetInInsetY() + bv.text->cursor.y();
192 if (by > top_y + workarea().workHeight())
195 x = bv.text->cursor.x();
196 y = bv.text->cursor.y();
202 // if it doesn't fit entirely on the screen, don't try to show it
203 if (y < 0 || y + h > workarea().workHeight())
206 cursor_visible_ = true;
207 showCursor(x, y, h, shape);
211 void LyXScreen::hideCursor()
213 if (!cursor_visible_)
216 cursor_visible_ = false;
221 void LyXScreen::toggleCursor(BufferView & bv)
230 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
231 int /*x*/, int y, int asc, int desc)
233 int const vheight = workarea().workHeight();
234 int const topy = bv->top_y();
238 if (y + desc - topy >= vheight)
239 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
240 else if (y - asc < topy && topy > 0)
241 newtop = y - vheight / 4;
243 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
253 unsigned int LyXScreen::topCursorVisible(LyXText * text)
255 LyXCursor const & cursor = text->cursor;
256 int top_y = text->bv()->top_y();
258 unsigned int const vheight = workarea().workHeight();
260 RowList::iterator row = text->cursorRow();
262 if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
263 if (row->height() < vheight
264 && row->height() > vheight / 4) {
267 - row->baseline() - vheight;
269 // scroll down, the scroll region must be so big!!
270 newtop = cursor.y() - vheight / 2;
273 } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
274 if (row->height() < vheight && row->height() > vheight / 4) {
275 newtop = cursor.y() - row->baseline();
278 newtop = cursor.y() - vheight / 2;
279 newtop = min(newtop, top_y);
283 return max(newtop, 0);
287 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
289 // Is a change necessary?
290 int const newtop = topCursorVisible(text);
291 bool const result = (newtop != bv->top_y());
297 void LyXScreen::redraw(BufferView & bv)
299 greyed_out_ = !bv.text;
306 workarea().getPainter().start();
310 int const y = paintText(bv);
312 // maybe we have to clear the screen at the bottom
313 int const y2 = workarea().workHeight();
314 if (y < y2 && !bv.text->isInInset()) {
315 workarea().getPainter().fillRectangle(0, y,
316 workarea().workWidth(), y2 - y,
320 lyxerr << "Redraw screen" << endl;
322 expose(0, 0, workarea().workWidth(), workarea().workHeight());
324 workarea().getPainter().end();
328 void LyXScreen::greyOut()
333 workarea().getPainter().start();
335 workarea().getPainter().fillRectangle(0, 0,
336 workarea().workWidth(),
337 workarea().workHeight(),
340 // Add a splash screen to the centre of the work area
341 SplashScreen const & splash = SplashScreen::get();
342 lyx::graphics::Image const * const splash_image = splash.image();
344 int const w = splash_image->getWidth();
345 int const h = splash_image->getHeight();
347 int x = (workarea().workWidth() - w) / 2;
348 int y = (workarea().workHeight() - h) / 2;
350 workarea().getPainter().image(x, y, w, h, *splash_image);
355 workarea().getPainter().text(x, y, splash.text(), splash.font());
357 expose(0, 0, workarea().workWidth(), workarea().workHeight());
358 workarea().getPainter().end();