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
20 #include "BufferView.h"
24 #include "font_metrics.h"
27 #include "rowpainter.h"
28 #include "insets/updatableinset.h"
30 #include "metricsinfo.h"
32 // Splash screen-specific stuff
36 #include "graphics/GraphicsLoader.h"
37 #include "graphics/GraphicsImage.h"
39 #include "support/filetools.h" // LibFileSearch
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/signals/trackable.hpp>
45 using namespace lyx::support;
53 class SplashScreen : boost::noncopyable, boost::signals::trackable {
55 /// This is a singleton class. Get the instance.
56 static SplashScreen const & get();
58 lyx::graphics::Image const * image() const { return loader_.image(); }
60 string const & text() const { return text_; }
62 LyXFont const & font() const { return font_; }
64 void connect(lyx::graphics::Loader::slot_type const & slot) const {
65 loader_.connect(slot);
68 void startLoading() const {
69 if (loader_.status() == lyx::graphics::WaitingToLoad)
70 loader_.startLoading();
74 /** Make the c-tor private so we can control how many objects
80 lyx::graphics::Loader loader_;
81 /// The text to be written on top of the pixmap
88 SplashScreen const & SplashScreen::get()
90 static SplashScreen singleton;
95 SplashScreen::SplashScreen()
96 : text_(lyx_version ? lyx_version : "unknown")
98 if (!lyxrc.show_banner)
101 string const file = LibFileSearch("images", "banner", "ppm");
105 // The font used to display the version info
106 font_.setFamily(LyXFont::SANS_FAMILY);
107 font_.setSeries(LyXFont::BOLD_SERIES);
108 font_.setSize(LyXFont::SIZE_NORMAL);
109 font_.setColor(LColor::yellow);
111 // Load up the graphics file
118 LyXScreen::LyXScreen()
119 : cursor_visible_(false), greyed_out_(true)
121 // Start loading the pixmap as soon as possible
122 if (lyxrc.show_banner) {
123 SplashScreen const & splash = SplashScreen::get();
124 splash.connect(boost::bind(&LyXScreen::greyOut, this));
125 splash.startLoading();
130 LyXScreen::~LyXScreen()
135 void LyXScreen::showCursor(BufferView & bv)
137 // this is needed to make sure we copy back the right
138 // pixmap on the hide for the Qt frontend
139 lyx_gui::sync_events();
147 Cursor_Shape shape = BAR_SHAPE;
149 LyXText const & text = *bv.getLyXText();
150 LyXFont const & realfont = text.real_current_font;
151 BufferParams const & bp = bv.buffer()->params;
152 bool const samelang = realfont.language() == bp.language;
153 bool const isrtl = realfont.isVisibleRightToLeft();
155 if (!samelang || isrtl != bp.language->RightToLeft()) {
158 shape = REVERSED_L_SHAPE;
161 int ascent = font_metrics::maxAscent(realfont);
162 int descent = font_metrics::maxDescent(realfont);
163 int h = ascent + descent;
166 int const top_y = bv.text->top_y();
168 if (bv.theLockingInset()) {
169 // Would be nice to clean this up to make some understandable sense...
170 UpdatableInset * inset = bv.theLockingInset();
171 inset->getCursor(bv, x, y);
173 // Non-obvious. The reason we have to have these
174 // extra checks is that the ->getCursor() calls rely
175 // on the inset's own knowledge of its screen position.
176 // If we scroll up or down in a big enough increment, the
177 // inset->draw() is not called: this doesn't update
178 // inset.top_baseline, so getCursor() returns an old value.
181 inset->getCursorPos(&bv, bx, by);
182 by += inset->insetInInsetY() + bv.text->cursor.iy();
185 if (by > top_y + workarea().workHeight())
188 x = bv.text->cursor.x();
189 y = bv.text->cursor.y();
195 // if it doesn't fit entirely on the screen, don't try to show it
196 if (y < 0 || y + h > workarea().workHeight())
199 cursor_visible_ = true;
200 showCursor(x, y, h, shape);
204 void LyXScreen::hideCursor()
206 if (!cursor_visible_)
209 cursor_visible_ = false;
214 void LyXScreen::toggleCursor(BufferView & bv)
223 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
224 int /*x*/, int y, int asc, int desc)
226 int const vheight = workarea().workHeight();
227 int newtop = text->top_y();
229 if (y + desc - text->top_y() >= vheight)
230 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
231 else if (y - asc < text->top_y() && text->top_y() > 0)
232 newtop = y - vheight / 4;
234 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
236 if (newtop == text->top_y())
239 draw(text, bv, newtop);
245 unsigned int LyXScreen::topCursorVisible(LyXText * text)
247 LyXCursor const & cursor = text->cursor;
248 int top_y = text->top_y();
250 int const vheight = workarea().workHeight();
252 RowList::iterator row = text->cursorRow();
254 #warning SUPER HACK DISABLED (Lgb)
256 // Is this a hack? Yes, probably... (Lgb)
258 return max(newtop, 0);
260 if (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 newtop = 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 != text->top_y());
293 draw(text, bv, newtop);
300 void LyXScreen::update(BufferView & bv, int yo, int xo)
302 LyXText * text = bv.text;
304 workarea().getPainter().start();
306 if (text->needRefresh()) {
307 int const vwidth = workarea().workWidth();
308 int const vheight = workarea().workHeight();
309 text->updateRowPositions();
311 drawFromTo(text, &bv, y, vheight, yo, xo);
312 expose(0, y, vwidth, vheight - y);
315 workarea().getPainter().end();
319 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
323 // only if there is a selection
324 if (!text->selection.set()) return;
326 int const bottom = min(
327 max(static_cast<int>(text->selection.end.y()
328 - text->getRow(text->selection.end)->baseline()
329 + text->getRow(text->selection.end)->height()),
331 static_cast<int>(text->top_y() + workarea().workHeight()));
333 max(static_cast<int>(text->selection.start.y() -
334 text->getRow(text->selection.start)->baseline()),
336 static_cast<int>(text->top_y() + workarea().workHeight()));
339 text->selection.set(false);
341 workarea().getPainter().start();
343 drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
345 expose(0, top - text->top_y(),
346 workarea().workWidth(),
347 bottom - text->top_y() - (top - text->top_y()));
349 workarea().getPainter().end();
353 void LyXScreen::redraw(BufferView * bv, LyXText * text)
362 workarea().getPainter().start();
364 text->updateRowPositions();
365 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
366 expose(0, 0, workarea().workWidth(), workarea().workHeight());
368 workarea().getPainter().end();
372 void LyXScreen::greyOut()
377 workarea().getPainter().start();
379 workarea().getPainter().fillRectangle(0, 0,
380 workarea().workWidth(),
381 workarea().workHeight(),
384 // Add a splash screen to the centre of the work area
385 SplashScreen const & splash = SplashScreen::get();
386 lyx::graphics::Image const * const splash_image = splash.image();
388 int const w = splash_image->getWidth();
389 int const h = splash_image->getHeight();
391 int x = (workarea().workWidth() - w) / 2;
392 int y = (workarea().workHeight() - h) / 2;
394 workarea().getPainter().image(x, y, w, h, *splash_image);
396 string const & splash_text = splash.text();
397 LyXFont const & splash_font = splash.font();
402 workarea().getPainter().text(x, y, splash_text, splash_font);
404 expose(0, 0, workarea().workWidth(), workarea().workHeight());
405 workarea().getPainter().end();
409 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
410 int y1, int y2, int yo, int xo)
412 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
415 int const topy = text->top_y();
416 int y_text = topy + y1;
417 RowList::iterator rit = text->getRowNearY(y_text);
418 int y = y_text - topy;
420 y = paintRows(*bv, *text, rit, xo, y, y, y2, yo);
422 // maybe we have to clear the screen at the bottom
423 if (y < y2 && !text->isInInset()) {
424 workarea().getPainter().fillRectangle(0, y,
425 workarea().workWidth(), y2 - y,