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 graphic_->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 /// The loading status of the image.
77 grfx::ImageStatus status_;
78 /// The text to be written on top of the pixmap
85 SplashScreen const & SplashScreen::get()
87 static SplashScreen singleton;
92 SplashScreen::SplashScreen()
93 : status_(grfx::WaitingToLoad),
94 text_(lyx_version ? lyx_version : "unknown")
96 string const file = LibFileSearch("images", "banner", "xpm");
100 // The font used to display the version info
101 font_.setFamily(LyXFont::SANS_FAMILY);
102 font_.setSeries(LyXFont::BOLD_SERIES);
103 font_.setSize(LyXFont::SIZE_NORMAL);
104 font_.setColor(LColor::yellow);
106 // Load up the graphics file
107 grfx::GCache & gc = grfx::GCache::get();
108 if (!gc.inCache(file))
110 // We /must/ make a local copy of this.
111 graphic_ = gc.graphic(file);
113 if (graphic_->status() == grfx::Loaded) {
116 graphic_->statusChanged.connect(
117 boost::bind(&SplashScreen::createPixmap, this));
118 graphic_->startLoading();
123 SplashScreen::~SplashScreen()
128 string const file = graphic_->filename();
131 // If only the cache itself now references this item, then it will be
133 grfx::GCache::get().remove(file);
137 void SplashScreen::createPixmap()
139 if (!graphic_.get() || status_ != grfx::WaitingToLoad)
142 // We aren't going to modify the image, so don't bother making a
144 grfx::GImage * const image = graphic_->image().get();
148 if (image->getPixmap()) {
149 status_ = grfx::Loaded;
153 bool const success = image->setPixmap(grfx::GParams());
154 status_ = success ? grfx::Loaded : grfx::ErrorLoading;
160 LyXScreen::LyXScreen()
161 : force_clear_(true), cursor_visible_(false)
163 // Start loading the pixmap as soon as possible
168 LyXScreen::~LyXScreen()
172 // FIXME: GUII these cursor methods need to decide
173 // whether the workarea is focused or not
175 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
180 workarea().getPainter().start();
182 Cursor_Shape shape = BAR_SHAPE;
183 BufferParams const & bp(bv->buffer()->params);
184 LyXFont const & realfont(text->real_current_font);
186 if (realfont.language() != bp.language
187 || realfont.isVisibleRightToLeft()
188 != bp.language->RightToLeft()) {
189 shape = (realfont.isVisibleRightToLeft())
190 ? REVERSED_L_SHAPE : L_SHAPE;
193 showManualCursor(text, text->cursor.x(), text->cursor.y(),
194 font_metrics::maxAscent(realfont),
195 font_metrics::maxDescent(realfont),
198 workarea().getPainter().end();
202 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
203 int /*x*/, int y, int asc, int desc)
205 int const vheight = workarea().workHeight();
206 int newtop = text->first_y;
208 if (y + desc - text->first_y >= vheight)
209 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
210 else if (y - asc < text->first_y
211 && text->first_y > 0) {
212 newtop = y - vheight / 4;
215 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
217 if (newtop != text->first_y) {
218 draw(text, bv, newtop);
219 text->first_y = newtop;
226 void LyXScreen::cursorToggle(BufferView * bv) const
235 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
237 int const vheight = workarea().workHeight();
240 Row * row = cursor.row();
242 // Is this a hack? Yes, probably... (Lgb)
244 return max(newtop, 0);
246 if (cursor.y() - row->baseline() + row->height()
247 - top_y >= vheight) {
248 if (row->height() < vheight
249 && row->height() > vheight / 4) {
252 - row->baseline() - vheight;
256 - vheight / 2; /* the scroll region must be so big!! */
259 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
260 top_y && top_y > 0) {
261 if (row->height() < vheight
262 && row->height() > vheight / 4) {
263 newtop = cursor.y() - row->baseline();
266 newtop = cursor.y() - vheight / 2;
267 newtop = min(newtop, top_y);
271 newtop = max(newtop, 0);
277 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
279 // Is a change necessary?
280 int const newtop = topCursorVisible(text->cursor, text->first_y);
281 bool const result = (newtop != text->first_y);
283 draw(text, bv, newtop);
288 void LyXScreen::update(LyXText * text, BufferView * bv,
291 int const vwidth = workarea().workWidth();
292 int const vheight = workarea().workHeight();
294 workarea().getPainter().start();
296 switch (text->status()) {
297 case LyXText::NEED_MORE_REFRESH:
299 int const y = max(int(text->refresh_y - text->first_y), 0);
300 drawFromTo(text, bv, y, vheight, yo, xo);
302 // otherwise this is called ONLY from BufferView_pimpl(update)
303 // or we should see to set this flag accordingly
304 if (text != bv->text)
305 text->status(bv, LyXText::UNCHANGED);
306 expose(0, y, vwidth, vheight - y);
309 case LyXText::NEED_VERY_LITTLE_REFRESH:
311 // ok I will update the current cursor row
312 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
314 // this because if we had a major update the refresh_row could
315 // have been set to 0!
316 if (text->refresh_row) {
317 // otherwise this is called ONLY from BufferView_pimpl(update)
318 // or we should see to set this flag accordingly
319 if (text != bv->text)
320 text->status(bv, LyXText::UNCHANGED);
321 expose(0, text->refresh_y - text->first_y + yo,
322 vwidth, text->refresh_row->height());
326 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
327 case LyXText::UNCHANGED:
328 // Nothing needs done
332 workarea().getPainter().end();
336 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
340 // only if there is a selection
341 if (!text->selection.set()) return;
343 int const bottom = min(
344 max(static_cast<int>(text->selection.end.y()
345 - text->selection.end.row()->baseline()
346 + text->selection.end.row()->height()),
348 static_cast<int>(text->first_y + workarea().workHeight()));
350 max(static_cast<int>(text->selection.start.y() -
351 text->selection.start.row()->baseline()),
353 static_cast<int>(text->first_y + workarea().workHeight()));
356 text->selection.set(false);
358 workarea().getPainter().start();
360 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
362 expose(0, top - text->first_y,
363 workarea().workWidth(),
364 bottom - text->first_y - (top - text->first_y));
366 workarea().getPainter().end();
370 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
373 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
374 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
377 int const top_tmp = text->toggle_cursor.y()
378 - text->toggle_cursor.row()->baseline();
379 int const bottom_tmp = text->toggle_end_cursor.y()
380 - text->toggle_end_cursor.row()->baseline()
381 + text->toggle_end_cursor.row()->height();
383 int const offset = yo < 0 ? yo : 0;
384 int const bottom = min(max(bottom_tmp, text->first_y),
385 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
386 int const top = min(max(top_tmp, text->first_y),
387 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
389 workarea().getPainter().start();
391 drawFromTo(text, bv, top - text->first_y,
392 bottom - text->first_y, yo,
394 expose(0, top - text->first_y, workarea().workWidth(),
395 bottom - text->first_y - (top - text->first_y));
397 workarea().getPainter().end();
401 void LyXScreen::redraw(LyXText * text, BufferView * bv)
403 workarea().getPainter().start();
407 expose(0, 0, workarea().workWidth(), workarea().workHeight());
408 workarea().getPainter().end();
412 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
413 expose(0, 0, workarea().workWidth(), workarea().workHeight());
415 workarea().getPainter().end();
417 if (cursor_visible_) {
418 cursor_visible_ = false;
424 void LyXScreen::greyOut()
426 workarea().getPainter().fillRectangle(0, 0,
427 workarea().workWidth(),
428 workarea().workHeight(),
431 // Add a splash screen to the centre of the work area
432 SplashScreen const & splash = SplashScreen::get();
433 grfx::GImage const * const splash_image = splash.image();
435 int const w = splash_image->getWidth();
436 int const h = splash_image->getHeight();
438 int x = 0.5 * (workarea().workWidth() - w);
439 int y = 0.5 * (workarea().workHeight() - h);
441 workarea().getPainter().image(x, y, w, h, *splash_image);
443 string const & splash_text = splash.text();
444 LyXFont const & splash_font = splash.font();
449 workarea().getPainter().text(x, y, splash_text, splash_font);
454 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
455 int y1, int y2, int yo, int xo,
458 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
460 int y_text = text->first_y + y1;
462 // get the first needed row
463 Row * row = text->getRowNearY(y_text);
464 // y_text is now the real beginning of the row
466 int y = y_text - text->first_y;
467 // y1 is now the real beginning of row on the screen
469 while (row != 0 && y < y2) {
470 LyXText::text_status st = text->status();
471 text->getVisibleRow(bv, y + yo,
472 xo, row, y + text->first_y);
473 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
474 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
475 text->fullRebreak(bv);
476 st = LyXText::NEED_MORE_REFRESH;
477 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
478 text->status(bv, st);
479 text->getVisibleRow(bv, y + yo,
480 xo, row, y + text->first_y);
485 force_clear_ = false;
487 // maybe we have to clear the screen at the bottom
488 if ((y < y2) && text->bv_owner) {
489 workarea().getPainter().fillRectangle(0, y,
490 workarea().workWidth(), y2 - y,
496 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
497 int y_text, int yo, int xo)
499 int const y = y_text - text->first_y + yo;
501 if (((y + row->height()) > 0) &&
502 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
503 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
505 force_clear_ = false;