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::GImage 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()
193 - top_y >= vheight) {
194 if (row->height() < vheight
195 && row->height() > vheight / 4) {
198 - row->baseline() - vheight;
202 - vheight / 2; /* the scroll region must be so big!! */
205 } else if (static_cast<int>((cursor.y()) - row->baseline()) <
206 top_y && top_y > 0) {
207 if (row->height() < vheight
208 && row->height() > vheight / 4) {
209 newtop = cursor.y() - row->baseline();
212 newtop = cursor.y() - vheight / 2;
213 newtop = min(newtop, top_y);
217 newtop = max(newtop, 0);
223 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
225 // Is a change necessary?
226 int const newtop = topCursorVisible(text->cursor, text->first_y);
227 bool const result = (newtop != text->first_y);
229 draw(text, bv, newtop);
234 void LyXScreen::update(LyXText * text, BufferView * bv,
237 int const vwidth = workarea().workWidth();
238 int const vheight = workarea().workHeight();
240 workarea().getPainter().start();
242 switch (text->status()) {
243 case LyXText::NEED_MORE_REFRESH:
245 int const y = max(int(text->refresh_y - text->first_y), 0);
246 drawFromTo(text, bv, y, vheight, yo, xo);
248 // otherwise this is called ONLY from BufferView_pimpl(update)
249 // or we should see to set this flag accordingly
250 if (text != bv->text)
251 text->status(bv, LyXText::UNCHANGED);
252 expose(0, y, vwidth, vheight - y);
255 case LyXText::NEED_VERY_LITTLE_REFRESH:
257 // ok I will update the current cursor row
258 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
260 // this because if we had a major update the refresh_row could
261 // have been set to 0!
262 if (text->refresh_row) {
263 // otherwise this is called ONLY from BufferView_pimpl(update)
264 // or we should see to set this flag accordingly
265 if (text != bv->text)
266 text->status(bv, LyXText::UNCHANGED);
267 expose(0, text->refresh_y - text->first_y + yo,
268 vwidth, text->refresh_row->height());
272 case LyXText::CHANGED_IN_DRAW: // just to remove the warning
273 case LyXText::UNCHANGED:
274 // Nothing needs done
278 workarea().getPainter().end();
282 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
286 // only if there is a selection
287 if (!text->selection.set()) return;
289 int const bottom = min(
290 max(static_cast<int>(text->selection.end.y()
291 - text->selection.end.row()->baseline()
292 + text->selection.end.row()->height()),
294 static_cast<int>(text->first_y + workarea().workHeight()));
296 max(static_cast<int>(text->selection.start.y() -
297 text->selection.start.row()->baseline()),
299 static_cast<int>(text->first_y + workarea().workHeight()));
302 text->selection.set(false);
304 workarea().getPainter().start();
306 drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
308 expose(0, top - text->first_y,
309 workarea().workWidth(),
310 bottom - text->first_y - (top - text->first_y));
312 workarea().getPainter().end();
316 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
319 if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
320 && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
323 int const top_tmp = text->toggle_cursor.y()
324 - text->toggle_cursor.row()->baseline();
325 int const bottom_tmp = text->toggle_end_cursor.y()
326 - text->toggle_end_cursor.row()->baseline()
327 + text->toggle_end_cursor.row()->height();
329 int const offset = yo < 0 ? yo : 0;
330 int const bottom = min(max(bottom_tmp, text->first_y),
331 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
332 int const top = min(max(top_tmp, text->first_y),
333 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
335 workarea().getPainter().start();
337 drawFromTo(text, bv, top - text->first_y,
338 bottom - text->first_y, yo,
340 expose(0, top - text->first_y, workarea().workWidth(),
341 bottom - text->first_y - (top - text->first_y));
343 workarea().getPainter().end();
347 void LyXScreen::redraw(LyXText * text, BufferView * bv)
349 workarea().getPainter().start();
353 expose(0, 0, workarea().workWidth(), workarea().workHeight());
354 workarea().getPainter().end();
358 drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
359 expose(0, 0, workarea().workWidth(), workarea().workHeight());
361 workarea().getPainter().end();
363 if (cursor_visible_) {
364 cursor_visible_ = false;
370 void LyXScreen::greyOut()
372 workarea().getPainter().fillRectangle(0, 0,
373 workarea().workWidth(),
374 workarea().workHeight(),
377 // Add a splash screen to the centre of the work area
378 SplashScreen const & splash = SplashScreen::get();
379 grfx::GImage const * const splash_image = splash.image();
381 int const w = splash_image->getWidth();
382 int const h = splash_image->getHeight();
384 int x = 0.5 * (workarea().workWidth() - w);
385 int y = 0.5 * (workarea().workHeight() - h);
387 workarea().getPainter().image(x, y, w, h, *splash_image);
389 string const & splash_text = splash.text();
390 LyXFont const & splash_font = splash.font();
395 workarea().getPainter().text(x, y, splash_text, splash_font);
400 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
401 int y1, int y2, int yo, int xo,
404 lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
406 int y_text = text->first_y + y1;
408 // get the first needed row
409 Row * row = text->getRowNearY(y_text);
410 // y_text is now the real beginning of the row
412 int y = y_text - text->first_y;
413 // y1 is now the real beginning of row on the screen
415 while (row != 0 && y < y2) {
416 LyXText::text_status st = text->status();
417 text->getVisibleRow(bv, y + yo,
418 xo, row, y + text->first_y);
419 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
420 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
421 text->fullRebreak(bv);
422 st = LyXText::NEED_MORE_REFRESH;
423 text->setCursor(bv, text->cursor.par(), text->cursor.pos());
424 text->status(bv, st);
425 text->getVisibleRow(bv, y + yo,
426 xo, row, y + text->first_y);
431 force_clear_ = false;
433 // maybe we have to clear the screen at the bottom
434 if ((y < y2) && text->bv_owner) {
435 workarea().getPainter().fillRectangle(0, y,
436 workarea().workWidth(), y2 - y,
442 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
443 int y_text, int yo, int xo)
445 int const y = y_text - text->first_y + yo;
447 if (((y + row->height()) > 0) &&
448 ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
449 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
451 force_clear_ = false;