]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
1d38beafcc0aa1b4a3b588820808c734a645058f
[lyx.git] / src / frontends / screen.C
1 /**
2  * \file screen.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author John Levon <moz@compsoc.man.ac.uk>
7  *
8  * Splash screen code added by Angus Leeming
9  */
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include <config.h>
16
17 #include "screen.h"
18 #include "lyxtext.h"
19 #include "lyxrow.h"
20 #include "BufferView.h"
21 #include "buffer.h"
22 #include "WorkArea.h"
23 #include "Painter.h"
24 #include "font_metrics.h"
25 #include "language.h"
26 #include "debug.h"
27
28 // Splash screen-specific stuff
29 #include "lyxfont.h"
30 #include "version.h"
31
32 #include "graphics/GraphicsCache.h"
33 #include "graphics/GraphicsCacheItem.h"
34 #include "graphics/GraphicsImage.h"
35 #include "graphics/GraphicsParams.h"
36
37 #include "support/filetools.h" // LibFileSearch
38
39 #include <boost/utility.hpp>
40 #include <boost/bind.hpp>
41
42 using std::min;
43 using std::max;
44 using std::endl;
45
46 namespace {
47
48 class SplashScreen : boost::noncopyable {
49 public:
50         /// This is a singleton class. Get the instance.
51         static SplashScreen const & get();
52         ///
53         grfx::GImage const * image() const { return graphic_->image().get(); }
54         ///
55         string const & text() const { return text_; }
56         ///
57         LyXFont const & font() const { return font_; }
58
59 private:
60         /** Make the c-tor, d-tor private so we can control how many objects
61          *  are instantiated.
62          */
63         SplashScreen();
64         ///
65         ~SplashScreen();
66
67         /** Connected to grfx::GCacheItem::statusChanged, so will generate the
68          *  pixmap as soon as the file is loaded into memory.
69          */
70         void createPixmap();
71         
72         /** Must store a copy of the cached item to ensure that it is not
73          *  erased unexpectedly by the cache itself.
74          */
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
79         string const text_;
80         /// in this font...
81         LyXFont font_;
82 };
83
84
85 SplashScreen const & SplashScreen::get()
86 {
87         static SplashScreen singleton;
88         return singleton;
89 }
90
91
92 SplashScreen::SplashScreen()
93         : status_(grfx::WaitingToLoad),
94           text_(lyx_version ? lyx_version : "unknown")
95 {
96         string const file = LibFileSearch("images", "banner", "xpm");
97         if (file.empty())
98                 return;
99
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);
105
106         // Load up the graphics file
107         grfx::GCache & gc = grfx::GCache::get();
108         if (!gc.inCache(file))
109                 gc.add(file);
110         // We /must/ make a local copy of this.
111         graphic_ = gc.graphic(file);
112
113         if (graphic_->status() == grfx::Loaded) {
114                 createPixmap();
115         } else {
116                 graphic_->statusChanged.connect(
117                         boost::bind(&SplashScreen::createPixmap, this));
118                 graphic_->startLoading();
119         }
120 }
121
122
123 SplashScreen::~SplashScreen()
124 {
125         if (!graphic_.get())
126                 return;
127
128         string const file = graphic_->filename();
129         graphic_.reset();
130
131         // If only the cache itself now references this item, then it will be
132         // removed.
133         grfx::GCache::get().remove(file);
134 }
135
136
137 void SplashScreen::createPixmap()
138 {
139         if (!graphic_.get() || status_ != grfx::WaitingToLoad)
140                 return;
141
142         // We aren't going to modify the image, so don't bother making a
143         // local copy
144         grfx::GImage * const image = graphic_->image().get();
145         if (!image)
146                 return;
147
148         if (image->getPixmap()) {
149                 status_ = grfx::Loaded;
150                 return;
151         }
152
153         bool const success = image->setPixmap(grfx::GParams());
154         status_ = success ? grfx::Loaded : grfx::ErrorLoading;
155 }
156
157 } // namespace anon
158
159
160 LyXScreen::LyXScreen()
161         : force_clear_(true), cursor_visible_(false)
162 {
163         // Start loading the pixmap as soon as possible
164         SplashScreen::get();
165 }
166
167
168 LyXScreen::~LyXScreen()
169 {
170 }
171
172 // FIXME: GUII these cursor methods need to decide
173 // whether the workarea is focused or not
174
175 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
176 {
177         if (cursor_visible_)
178                 return;
179
180         workarea().getPainter().start();
181
182         Cursor_Shape shape = BAR_SHAPE;
183         BufferParams const & bp(bv->buffer()->params);
184         LyXFont const & realfont(text->real_current_font);
185
186         if (realfont.language() != bp.language
187                 || realfont.isVisibleRightToLeft()
188                 != bp.language->RightToLeft()) {
189                 shape = (realfont.isVisibleRightToLeft())
190                         ? REVERSED_L_SHAPE : L_SHAPE;
191         }
192
193         showManualCursor(text, text->cursor.x(), text->cursor.y(),
194                 font_metrics::maxAscent(realfont),
195                 font_metrics::maxDescent(realfont),
196                 shape);
197
198         workarea().getPainter().end();
199 }
200
201
202 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
203         int /*x*/, int y, int asc, int desc)
204 {
205         int const vheight = workarea().workHeight();
206         int newtop = text->first_y;
207
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;
213         }
214
215         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
216
217         if (newtop != text->first_y) {
218                 draw(text, bv, newtop);
219                 text->first_y = newtop;
220                 return true;
221         }
222         return false;
223 }
224
225
226 void LyXScreen::cursorToggle(BufferView * bv) const
227 {
228         if (cursor_visible_)
229                 bv->hideCursor();
230         else
231                 bv->showCursor();
232 }
233
234
235 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
236 {
237         int const vheight = workarea().workHeight();
238         int newtop = top_y;
239
240         Row * row = cursor.row();
241
242         // Is this a hack? Yes, probably... (Lgb)
243         if (!row)
244                 return max(newtop, 0);
245
246         if (cursor.y() - row->baseline() + row->height()
247             - top_y >= vheight) {
248                 if (row->height() < vheight
249                     && row->height() > vheight / 4) {
250                         newtop = cursor.y()
251                                 + row->height()
252                                 - row->baseline() - vheight;
253                 } else {
254                         // scroll down
255                         newtop = cursor.y()
256                                 - vheight / 2;   /* the scroll region must be so big!! */
257                 }
258
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();
264                 } else {
265                         // scroll up
266                         newtop = cursor.y() - vheight / 2;
267                         newtop = min(newtop, top_y);
268                 }
269         }
270
271         newtop = max(newtop, 0);
272
273         return newtop;
274 }
275
276
277 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
278 {
279         // Is a change necessary?
280         int const newtop = topCursorVisible(text->cursor, text->first_y);
281         bool const result = (newtop != text->first_y);
282         if (result)
283                 draw(text, bv, newtop);
284         return result;
285 }
286
287
288 void LyXScreen::update(LyXText * text, BufferView * bv,
289         int yo, int xo)
290 {
291         int const vwidth = workarea().workWidth();
292         int const vheight = workarea().workHeight();
293
294         workarea().getPainter().start();
295
296         switch (text->status()) {
297         case LyXText::NEED_MORE_REFRESH:
298         {
299                 int const y = max(int(text->refresh_y - text->first_y), 0);
300                 drawFromTo(text, bv, y, vheight, yo, xo);
301                 text->refresh_y = 0;
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);
307         }
308         break;
309         case LyXText::NEED_VERY_LITTLE_REFRESH:
310         {
311                 // ok I will update the current cursor row
312                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
313                            yo, xo);
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());
323                 }
324         }
325         break;
326         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
327         case LyXText::UNCHANGED:
328                 // Nothing needs done
329                 break;
330         }
331
332         workarea().getPainter().end();
333 }
334
335
336 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
337                                 bool kill_selection,
338                                 int yo, int xo)
339 {
340         // only if there is a selection
341         if (!text->selection.set()) return;
342
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()),
347                     text->first_y),
348                 static_cast<int>(text->first_y + workarea().workHeight()));
349         int const top = min(
350                 max(static_cast<int>(text->selection.start.y() -
351                                      text->selection.start.row()->baseline()),
352                     text->first_y),
353                 static_cast<int>(text->first_y + workarea().workHeight()));
354
355         if (kill_selection)
356                 text->selection.set(false);
357
358         workarea().getPainter().start();
359
360         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
361                    yo, xo);
362         expose(0, top - text->first_y,
363                workarea().workWidth(),
364                bottom - text->first_y - (top - text->first_y));
365
366         workarea().getPainter().end();
367 }
368
369
370 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
371                              int yo, int xo)
372 {
373         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
374             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
375                 return;
376
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();
382
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;
388
389         workarea().getPainter().start();
390
391         drawFromTo(text, bv, top - text->first_y,
392                    bottom - text->first_y, yo,
393                    xo);
394         expose(0, top - text->first_y, workarea().workWidth(),
395                bottom - text->first_y - (top - text->first_y));
396
397         workarea().getPainter().end();
398 }
399
400
401 void LyXScreen::redraw(LyXText * text, BufferView * bv)
402 {
403         workarea().getPainter().start();
404
405         if (!text) {
406                 greyOut();
407                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
408                 workarea().getPainter().end();
409                 return;
410         }
411
412         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
413         expose(0, 0, workarea().workWidth(), workarea().workHeight());
414
415         workarea().getPainter().end();
416
417         if (cursor_visible_) {
418                 cursor_visible_ = false;
419                 bv->showCursor();
420         }
421 }
422
423
424 void LyXScreen::greyOut()
425 {
426         workarea().getPainter().fillRectangle(0, 0,
427                 workarea().workWidth(),
428                 workarea().workHeight(),
429                 LColor::bottomarea);
430
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();
434         if (splash_image) {
435                 int const w = splash_image->getWidth();
436                 int const h = splash_image->getHeight();
437
438                 int x = 0.5 * (workarea().workWidth() - w);
439                 int y = 0.5 * (workarea().workHeight() - h);
440
441                 workarea().getPainter().image(x, y, w, h, *splash_image);
442
443                 string const & splash_text  = splash.text();
444                 LyXFont const & splash_font = splash.font();
445                 
446                 x += 260;
447                 y += 265;
448
449                 workarea().getPainter().text(x, y, splash_text, splash_font);
450         }
451 }
452
453
454 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
455         int y1, int y2, int yo, int xo,
456         bool internal)
457 {
458         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
459
460         int y_text = text->first_y + y1;
461
462         // get the first needed row
463         Row * row = text->getRowNearY(y_text);
464         // y_text is now the real beginning of the row
465
466         int y = y_text - text->first_y;
467         // y1 is now the real beginning of row on the screen
468
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);
481                 }
482                 y += row->height();
483                 row = row->next();
484         }
485         force_clear_ = false;
486
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,
491                         LColor::bottomarea);
492         }
493 }
494
495
496 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
497         int y_text, int yo, int xo)
498 {
499         int const y = y_text - text->first_y + yo;
500
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);
504         }
505         force_clear_ = false;
506 }