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/GraphicsLoader.h"
33 #include "graphics/GraphicsImage.h"
35 #include "support/filetools.h" // LibFileSearch
37 #include <boost/utility.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/signals/trackable.hpp>
47 class SplashScreen : boost::noncopyable, boost::signals::trackable {
49 /// This is a singleton class. Get the instance.
50 static SplashScreen const & get();
52 grfx::Image const * image() const { return loader_.image(); }
54 string const & text() const { return text_; }
56 LyXFont const & font() const { return font_; }
59 /** Make the c-tor private so we can control how many objects
66 /// The text to be written on top of the pixmap
73 SplashScreen const & SplashScreen::get()
75 static SplashScreen singleton;
80 SplashScreen::SplashScreen()
81 : text_(lyx_version ? lyx_version : "unknown")
83 string const file = LibFileSearch("images", "banner", "xpm");
87 // The font used to display the version info
88 font_.setFamily(LyXFont::SANS_FAMILY);
89 font_.setSeries(LyXFont::BOLD_SERIES);
90 font_.setSize(LyXFont::SIZE_NORMAL);
91 font_.setColor(LColor::yellow);
93 // Load up the graphics file
95 // We aren't interested here in when the image is loaded.
96 // If it isn't ready when we want it, then we ignore it.
97 // loader_->statusChanged.connect(
98 // boost::bind(&SplashScreen::statusChanged, this));
99 if (loader_.status() == grfx::WaitingToLoad)
100 loader_.startLoading();
106 LyXScreen::LyXScreen()
107 : force_clear_(true), cursor_visible_(false)
109 // Start loading the pixmap as soon as possible
114 LyXScreen::~LyXScreen()
118 // FIXME: GUII these cursor methods need to decide
119 // whether the workarea is focused or not
121 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
126 workarea().getPainter().start();
128 Cursor_Shape shape = BAR_SHAPE;
129 BufferParams const & bp(bv->buffer()->params);
130 LyXFont const & realfont(text->real_current_font);
132 if (realfont.language() != bp.language
133 || realfont.isVisibleRightToLeft()
134 != bp.language->RightToLeft()) {
135 shape = (realfont.isVisibleRightToLeft())
136 ? REVERSED_L_SHAPE : L_SHAPE;
139 showManualCursor(text, text->cursor.x(), text->cursor.y(),
140 font_metrics::maxAscent(realfont),
141 font_metrics::maxDescent(realfont),
144 workarea().getPainter().end();
148 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
149 int /*x*/, int y, int asc, int desc)
151 int const vheight = workarea().workHeight();
152 int newtop = text->first_y;
154 if (y + desc - text->first_y >= vheight)
155 newtop = y - 3 * vheight / 4; // the scroll region must be so big!!
156 else if (y - asc < text->first_y
157 && text->first_y > 0) {
158 newtop = y - vheight / 4;
161 newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
163 if (newtop != text->first_y) {
164 draw(text, bv, newtop);
165 text->first_y = newtop;
172 void LyXScreen::cursorToggle(BufferView * bv) const
181 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
183 int const vheight = workarea().workHeight();
186 Row * row = cursor.row();
188 // Is this a hack? Yes, probably... (Lgb)
190 return max(newtop, 0);
192 if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
193 if (row->height() < vheight
194 && row->height() > vheight / 4) {
197 - row->baseline() - vheight;
201 - vheight / 2; /* the scroll region must be so big!! */
204 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
205 top_y && top_y > 0) {
206 if (row->height() < vheight
207 && row->height() > vheight / 4) {
208 newtop = cursor.y() - row->baseline();
211 newtop = cursor.y() - vheight / 2;
212 newtop = min(newtop, top_y);
216 newtop = max(newtop, 0);
222 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
224 // Is a change necessary?
225 int const newtop = topCursorVisible(text->cursor, text->first_y);
226 bool const result = (newtop != text->first_y);
228 draw(text, bv, newtop);
233 void LyXScreen::update(LyXText * text, BufferView * bv,
236 int const vwidth = workarea().workWidth();
237 int const vheight = workarea().workHeight();
239 workarea().getPainter().start();
241 switch (text->status()) {
242 case LyXText::NEED_MORE_REFRESH:
244 int const y = max(int(text->refresh_y - text->first_y), 0);
245 drawFromTo(text, bv, y, vheight, yo, xo);
247 // otherwise this is called ONLY from BufferView_pimpl(update)
248 // or we should see to set this flag accordingly
249 if (text != bv->text)
250 text->status(bv, LyXText::UNCHANGED);
251 expose(0, y, vwidth, vheight - y);
254 case LyXText::NEED_VERY_LITTLE_REFRESH:
256 // ok I will update the current cursor row
257 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
259 // this because if we had a major update the refresh_row could
260 // have been set to 0!
261 if (text->refresh_row) {
262 // otherwise this is called ONLY from BufferView_pimpl(update)
263 // or we should see to set this flag accordingly
264 if (text != bv->text)
265 text->status(bv, LyXText::UNCHANGED);
266 expose(0, text->refresh_y - text->first_y + yo,
267 vwidth, text->refresh_row->height());
271 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
272 case LyXText::UNCHANGED:
273 // Nothing needs done
277 workarea().getPainter().end();
281 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
285 // only if there is a selection
286 if (!text->selection.set()) return;
288 int const bottom = min(
289 max(static_cast<int>(text->selection.end.y()
290 - text->selection.end.row()->baseline()
291 + text->selection.end.row()->height()),
293 static_cast<int>(text->first_y + workarea().workHeight()));
295 max(static_cast<int>(text->selection.start.y() -
296 text->selection.start.row()->baseline()),
298 static_cast<int>(text->first_y + workarea().workHeight()));
301 text->selection.set(false);
303 workarea().getPainter().start();
305 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
307 expose(0, top - text->first_y,
308 workarea().workWidth(),
309 bottom - text->first_y - (top - text->first_y));
311 workarea().getPainter().end();
315 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
318 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
319 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
322 int const top_tmp = text->toggle_cursor.y()
323 - text->toggle_cursor.row()->baseline();
324 int const bottom_tmp = text->toggle_end_cursor.y()
325 - text->toggle_end_cursor.row()->baseline()
326 + text->toggle_end_cursor.row()->height();
328 int const offset = yo < 0 ? yo : 0;
329 int const bottom = min(max(bottom_tmp, text->first_y),
330 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
331 int const top = min(max(top_tmp, text->first_y),
332 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
334 workarea().getPainter().start();
336 drawFromTo(text, bv, top - text->first_y,
337 bottom - text->first_y, yo,
339 expose(0, top - text->first_y, workarea().workWidth(),
340 bottom - text->first_y - (top - text->first_y));
342 workarea().getPainter().end();
346 void LyXScreen::redraw(LyXText * text, BufferView * bv)
348 workarea().getPainter().start();
352 expose(0, 0, workarea().workWidth(), workarea().workHeight());
353 workarea().getPainter().end();
357 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
358 expose(0, 0, workarea().workWidth(), workarea().workHeight());
360 workarea().getPainter().end();
362 if (cursor_visible_) {
363 cursor_visible_ = false;
369 void LyXScreen::greyOut()
371 workarea().getPainter().fillRectangle(0, 0,
372 workarea().workWidth(),
373 workarea().workHeight(),
376 // Add a splash screen to the centre of the work area
377 SplashScreen const & splash = SplashScreen::get();
378 grfx::Image const * const splash_image = splash.image();
380 int const w = splash_image->getWidth();
381 int const h = splash_image->getHeight();
383 int x = (workarea().workWidth() - w) / 2;
384 int y = (workarea().workHeight() - h) / 2;
386 workarea().getPainter().image(x, y, w, h, *splash_image);
388 string const & splash_text = splash.text();
389 LyXFont const & splash_font = splash.font();
394 workarea().getPainter().text(x, y, splash_text, splash_font);
399 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
400 int y1, int y2, int yo, int xo,
403 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
405 int y_text = text->first_y + y1;
407 // get the first needed row
408 Row * row = text->getRowNearY(y_text);
409 // y_text is now the real beginning of the row
411 int y = y_text - text->first_y;
412 // y1 is now the real beginning of row on the screen
414 while (row != 0 && y < y2) {
415 LyXText::text_status st = text->status();
416 text->getVisibleRow(bv, y + yo,
417 xo, row, y + text->first_y);
418 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
419 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
420 text->fullRebreak(bv);
421 st = LyXText::NEED_MORE_REFRESH;
422 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
423 text->status(bv, st);
424 text->getVisibleRow(bv, y + yo,
425 xo, row, y + text->first_y);
430 force_clear_ = false;
432 // maybe we have to clear the screen at the bottom
433 if ((y < y2) && text->bv_owner) {
434 workarea().getPainter().fillRectangle(0, y,
435 workarea().workWidth(), y2 - y,
441 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
442 int y_text, int yo, int xo)
444 int const y = y_text - text->first_y + yo;
446 if (((y + row->height()) > 0) &&
447 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
448 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
450 force_clear_ = false;