3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
8 * Splash screen code added by Angus Leeming
12 #pragma implementation
20 #include "BufferView.h"
24 #include "font_metrics.h"
28 // Splash screen-specific stuff
32 #include "graphics/GraphicsCache.h"
33 #include "graphics/GraphicsCacheItem.h"
34 #include "graphics/GraphicsImage.h"
35 #include "graphics/GraphicsParams.h"
37 #include "support/filetools.h" // LibFileSearch
39 #include <boost/utility.hpp>
40 #include <boost/bind.hpp>
48 class SplashScreen : boost::noncopyable {
50 /// This is a singleton class. Get the instance.
51 static SplashScreen const & get();
53 grfx::GImage const * image() const { return image_.get(); }
55 string const & text() const { return text_; }
57 LyXFont const & font() const { return font_; }
60 /** Make the c-tor, d-tor private so we can control how many objects
67 /** Connected to grfx::GCacheItem::statusChanged, so will generate the
68 * pixmap as soon as the file is loaded into memory.
72 /** Must store a copy of the cached item to ensure that it is not
73 * erased unexpectedly by the cache itself.
75 grfx::GraphicPtr graphic_;
76 /** We generate a pixmap from a copy of the grfx::GImage * stored in
79 grfx::ImagePtr image_;
80 /// The loading status of the image.
81 grfx::ImageStatus status_;
82 /// The text to be written on top of the pixmap
89 SplashScreen const & SplashScreen::get()
91 static SplashScreen singleton;
96 SplashScreen::SplashScreen()
97 : status_(grfx::WaitingToLoad),
98 text_(lyx_version ? lyx_version : "unknown")
100 string const file = LibFileSearch("images", "banner", "xpm");
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
111 grfx::GCache & gc = grfx::GCache::get();
112 if (!gc.inCache(file))
114 // We /must/ make a local copy of this.
115 graphic_ = gc.graphic(file);
117 if (graphic_->status() == grfx::Loaded) {
120 graphic_->statusChanged.connect(
121 boost::bind(&SplashScreen::createPixmap, this));
122 graphic_->startLoading();
127 SplashScreen::~SplashScreen()
132 string const file = graphic_->filename();
135 // If only the cache itself now references this item, then it will be
137 grfx::GCache::get().remove(file);
141 void SplashScreen::createPixmap()
143 if (!graphic_.get() || image_.get())
146 if (graphic_->status() != grfx::Loaded)
149 if (status_ != grfx::WaitingToLoad)
152 // Strictly speaking, we need to create a copy only if we're going to
153 // modify the image (scale, etc).
154 image_.reset(graphic_->image()->clone());
156 bool const success = image_->setPixmap(grfx::GParams());
159 status_ = grfx::Loaded;
162 status_ = grfx::ErrorScalingEtc;
169 LyXScreen::LyXScreen()
170 : force_clear_(true), cursor_visible_(false)
172 // Start loading the pixmap as soon as possible
177 LyXScreen::~LyXScreen()
181 // FIXME: GUII these cursor methods need to decide
182 // whether the workarea is focused or not
184 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
189 workarea().getPainter().start();
191 Cursor_Shape shape = BAR_SHAPE;
192 BufferParams const & bp(bv->buffer()->params);
193 LyXFont const & realfont(text->real_current_font);
195 if (realfont.language() != bp.language
196 || realfont.isVisibleRightToLeft()
197 != bp.language->RightToLeft()) {
198 shape = (realfont.isVisibleRightToLeft())
199 ? REVERSED_L_SHAPE : L_SHAPE;
202 showManualCursor(text, text->cursor.x(), text->cursor.y(),
203 font_metrics::maxAscent(realfont),
204 font_metrics::maxDescent(realfont),
207 workarea().getPainter().end();
211 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
212 int /*x*/, int y, int asc, int desc)
214 int const vheight = workarea().workHeight();
215 int newtop = text->first_y;
217 if (y + desc - text->first_y >= vheight)
218 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
219 else if (y - asc < text->first_y
220 && text->first_y > 0) {
221 newtop = y - vheight / 4;
224 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
226 if (newtop != text->first_y) {
227 draw(text, bv, newtop);
228 text->first_y = newtop;
235 void LyXScreen::cursorToggle(BufferView * bv) const
244 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
246 int const vheight = workarea().workHeight();
249 Row * row = cursor.row();
251 // Is this a hack? Yes, probably... (Lgb)
253 return max(newtop, 0);
255 if (cursor.y() - row->baseline() + row->height()
256 - top_y >= vheight) {
257 if (row->height() < vheight
258 && row->height() > vheight / 4) {
261 - row->baseline() - vheight;
265 - vheight / 2; /* the scroll region must be so big!! */
268 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
269 top_y && top_y > 0) {
270 if (row->height() < vheight
271 && row->height() > vheight / 4) {
272 newtop = cursor.y() - row->baseline();
275 newtop = cursor.y() - vheight / 2;
276 newtop = min(newtop, top_y);
280 newtop = max(newtop, 0);
286 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
288 // Is a change necessary?
289 int const newtop = topCursorVisible(text->cursor, text->first_y);
290 bool const result = (newtop != text->first_y);
292 draw(text, bv, newtop);
297 void LyXScreen::update(LyXText * text, BufferView * bv,
300 int const vwidth = workarea().workWidth();
301 int const vheight = workarea().workHeight();
303 workarea().getPainter().start();
305 switch (text->status()) {
306 case LyXText::NEED_MORE_REFRESH:
308 int const y = max(int(text->refresh_y - text->first_y), 0);
309 drawFromTo(text, bv, y, vheight, yo, xo);
311 // otherwise this is called ONLY from BufferView_pimpl(update)
312 // or we should see to set this flag accordingly
313 if (text != bv->text)
314 text->status(bv, LyXText::UNCHANGED);
315 expose(0, y, vwidth, vheight - y);
318 case LyXText::NEED_VERY_LITTLE_REFRESH:
320 // ok I will update the current cursor row
321 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
323 // this because if we had a major update the refresh_row could
324 // have been set to 0!
325 if (text->refresh_row) {
326 // otherwise this is called ONLY from BufferView_pimpl(update)
327 // or we should see to set this flag accordingly
328 if (text != bv->text)
329 text->status(bv, LyXText::UNCHANGED);
330 expose(0, text->refresh_y - text->first_y + yo,
331 vwidth, text->refresh_row->height());
335 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
336 case LyXText::UNCHANGED:
337 // Nothing needs done
341 workarea().getPainter().end();
345 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
349 // only if there is a selection
350 if (!text->selection.set()) return;
352 int const bottom = min(
353 max(static_cast<int>(text->selection.end.y()
354 - text->selection.end.row()->baseline()
355 + text->selection.end.row()->height()),
357 static_cast<int>(text->first_y + workarea().workHeight()));
359 max(static_cast<int>(text->selection.start.y() -
360 text->selection.start.row()->baseline()),
362 static_cast<int>(text->first_y + workarea().workHeight()));
365 text->selection.set(false);
367 workarea().getPainter().start();
369 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
371 expose(0, top - text->first_y,
372 workarea().workWidth(),
373 bottom - text->first_y - (top - text->first_y));
375 workarea().getPainter().end();
379 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
382 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
383 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
386 int const top_tmp = text->toggle_cursor.y()
387 - text->toggle_cursor.row()->baseline();
388 int const bottom_tmp = text->toggle_end_cursor.y()
389 - text->toggle_end_cursor.row()->baseline()
390 + text->toggle_end_cursor.row()->height();
392 int const offset = yo < 0 ? yo : 0;
393 int const bottom = min(max(bottom_tmp, text->first_y),
394 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
395 int const top = min(max(top_tmp, text->first_y),
396 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
398 workarea().getPainter().start();
400 drawFromTo(text, bv, top - text->first_y,
401 bottom - text->first_y, yo,
403 expose(0, top - text->first_y, workarea().workWidth(),
404 bottom - text->first_y - (top - text->first_y));
406 workarea().getPainter().end();
410 void LyXScreen::redraw(LyXText * text, BufferView * bv)
412 workarea().getPainter().start();
416 expose(0, 0, workarea().workWidth(), workarea().workHeight());
417 workarea().getPainter().end();
421 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
422 expose(0, 0, workarea().workWidth(), workarea().workHeight());
424 workarea().getPainter().end();
426 if (cursor_visible_) {
427 cursor_visible_ = false;
433 void LyXScreen::greyOut()
435 workarea().getPainter().fillRectangle(0, 0,
436 workarea().workWidth(),
437 workarea().workHeight(),
440 // Add a splash screen to the centre of the work area
441 SplashScreen const & splash = SplashScreen::get();
442 grfx::GImage const * const splash_image = splash.image();
444 int const w = splash_image->getWidth();
445 int const h = splash_image->getHeight();
447 int x = 0.5 * (workarea().workWidth() - w);
448 int y = 0.5 * (workarea().workHeight() - h);
450 workarea().getPainter().image(x, y, w, h, *splash_image);
452 string const & splash_text = splash.text();
453 LyXFont const & splash_font = splash.font();
458 workarea().getPainter().text(x, y, splash_text, splash_font);
463 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
464 int y1, int y2, int yo, int xo,
467 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
469 int y_text = text->first_y + y1;
471 // get the first needed row
472 Row * row = text->getRowNearY(y_text);
473 // y_text is now the real beginning of the row
475 int y = y_text - text->first_y;
476 // y1 is now the real beginning of row on the screen
478 while (row != 0 && y < y2) {
479 LyXText::text_status st = text->status();
480 text->getVisibleRow(bv, y + yo,
481 xo, row, y + text->first_y);
482 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
483 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
484 text->fullRebreak(bv);
485 st = LyXText::NEED_MORE_REFRESH;
486 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
487 text->status(bv, st);
488 text->getVisibleRow(bv, y + yo,
489 xo, row, y + text->first_y);
494 force_clear_ = false;
496 // maybe we have to clear the screen at the bottom
497 if ((y < y2) && text->bv_owner) {
498 workarea().getPainter().fillRectangle(0, y,
499 workarea().workWidth(), y2 - y,
505 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
506 int y_text, int yo, int xo)
508 int const y = y_text - text->first_y + yo;
510 if (((y + row->height()) > 0) &&
511 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
512 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
514 force_clear_ = false;