]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
ce174f8f48ad0f0048d4bc9be8cc2c7571b1d952
[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 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         /** We generate a pixmap from a copy of the grfx::GImage * stored in
77          *  the cache.
78          */
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
83         string const text_;
84         /// in this font...
85         LyXFont font_;
86 };
87
88
89 SplashScreen const & SplashScreen::get()
90 {
91         static SplashScreen singleton;
92         return singleton;
93 }
94
95
96 SplashScreen::SplashScreen()
97         : status_(grfx::WaitingToLoad),
98           text_(lyx_version ? lyx_version : "unknown")
99 {
100         string const file = LibFileSearch("images", "banner", "xpm");
101         if (file.empty())
102                 return;
103
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);
109
110         // Load up the graphics file
111         grfx::GCache & gc = grfx::GCache::get();
112         if (!gc.inCache(file))
113                 gc.add(file);
114         // We /must/ make a local copy of this.
115         graphic_ = gc.graphic(file);
116
117         if (graphic_->status() == grfx::Loaded) {
118                 createPixmap();
119         } else {
120                 graphic_->statusChanged.connect(
121                         boost::bind(&SplashScreen::createPixmap, this));
122                 graphic_->startLoading();
123         }
124 }
125
126
127 SplashScreen::~SplashScreen()
128 {
129         if (!graphic_.get())
130                 return;
131
132         string const file = graphic_->filename();
133         graphic_.reset();
134
135         // If only the cache itself now references this item, then it will be
136         // removed.
137         grfx::GCache::get().remove(file);
138 }
139
140
141 void SplashScreen::createPixmap()
142 {
143         if (!graphic_.get() || image_.get())
144                 return;
145
146         if (graphic_->status() != grfx::Loaded)
147                 return;
148
149         if (status_ != grfx::WaitingToLoad)
150                 return;
151
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());
155         
156         bool const success = image_->setPixmap(grfx::GParams());
157
158         if (success) {
159                 status_ = grfx::Loaded;
160         } else {
161                 image_.reset();
162                 status_ = grfx::ErrorScalingEtc;
163         }
164 }
165
166 } // namespace anon
167
168
169 LyXScreen::LyXScreen()
170         : force_clear_(true), cursor_visible_(false)
171 {
172         // Start loading the pixmap as soon as possible
173         SplashScreen::get();
174 }
175
176
177 LyXScreen::~LyXScreen()
178 {
179 }
180
181 // FIXME: GUII these cursor methods need to decide
182 // whether the workarea is focused or not
183
184 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
185 {
186         if (cursor_visible_)
187                 return;
188
189         workarea().getPainter().start();
190
191         Cursor_Shape shape = BAR_SHAPE;
192         BufferParams const & bp(bv->buffer()->params);
193         LyXFont const & realfont(text->real_current_font);
194
195         if (realfont.language() != bp.language
196                 || realfont.isVisibleRightToLeft()
197                 != bp.language->RightToLeft()) {
198                 shape = (realfont.isVisibleRightToLeft())
199                         ? REVERSED_L_SHAPE : L_SHAPE;
200         }
201
202         showManualCursor(text, text->cursor.x(), text->cursor.y(),
203                 font_metrics::maxAscent(realfont),
204                 font_metrics::maxDescent(realfont),
205                 shape);
206
207         workarea().getPainter().end();
208 }
209
210
211 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
212         int /*x*/, int y, int asc, int desc)
213 {
214         int const vheight = workarea().workHeight();
215         int newtop = text->first_y;
216
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;
222         }
223
224         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
225
226         if (newtop != text->first_y) {
227                 draw(text, bv, newtop);
228                 text->first_y = newtop;
229                 return true;
230         }
231         return false;
232 }
233
234
235 void LyXScreen::cursorToggle(BufferView * bv) const
236 {
237         if (cursor_visible_)
238                 bv->hideCursor();
239         else
240                 bv->showCursor();
241 }
242
243
244 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
245 {
246         int const vheight = workarea().workHeight();
247         int newtop = top_y;
248
249         Row * row = cursor.row();
250
251         // Is this a hack? Yes, probably... (Lgb)
252         if (!row)
253                 return max(newtop, 0);
254
255         if (cursor.y() - row->baseline() + row->height()
256             - top_y >= vheight) {
257                 if (row->height() < vheight
258                     && row->height() > vheight / 4) {
259                         newtop = cursor.y()
260                                 + row->height()
261                                 - row->baseline() - vheight;
262                 } else {
263                         // scroll down
264                         newtop = cursor.y()
265                                 - vheight / 2;   /* the scroll region must be so big!! */
266                 }
267
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();
273                 } else {
274                         // scroll up
275                         newtop = cursor.y() - vheight / 2;
276                         newtop = min(newtop, top_y);
277                 }
278         }
279
280         newtop = max(newtop, 0);
281
282         return newtop;
283 }
284
285
286 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
287 {
288         // Is a change necessary?
289         int const newtop = topCursorVisible(text->cursor, text->first_y);
290         bool const result = (newtop != text->first_y);
291         if (result)
292                 draw(text, bv, newtop);
293         return result;
294 }
295
296
297 void LyXScreen::update(LyXText * text, BufferView * bv,
298         int yo, int xo)
299 {
300         int const vwidth = workarea().workWidth();
301         int const vheight = workarea().workHeight();
302
303         workarea().getPainter().start();
304
305         switch (text->status()) {
306         case LyXText::NEED_MORE_REFRESH:
307         {
308                 int const y = max(int(text->refresh_y - text->first_y), 0);
309                 drawFromTo(text, bv, y, vheight, yo, xo);
310                 text->refresh_y = 0;
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);
316         }
317         break;
318         case LyXText::NEED_VERY_LITTLE_REFRESH:
319         {
320                 // ok I will update the current cursor row
321                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
322                            yo, xo);
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());
332                 }
333         }
334         break;
335         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
336         case LyXText::UNCHANGED:
337                 // Nothing needs done
338                 break;
339         }
340
341         workarea().getPainter().end();
342 }
343
344
345 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
346                                 bool kill_selection,
347                                 int yo, int xo)
348 {
349         // only if there is a selection
350         if (!text->selection.set()) return;
351
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()),
356                     text->first_y),
357                 static_cast<int>(text->first_y + workarea().workHeight()));
358         int const top = min(
359                 max(static_cast<int>(text->selection.start.y() -
360                                      text->selection.start.row()->baseline()),
361                     text->first_y),
362                 static_cast<int>(text->first_y + workarea().workHeight()));
363
364         if (kill_selection)
365                 text->selection.set(false);
366
367         workarea().getPainter().start();
368
369         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
370                    yo, xo);
371         expose(0, top - text->first_y,
372                workarea().workWidth(),
373                bottom - text->first_y - (top - text->first_y));
374
375         workarea().getPainter().end();
376 }
377
378
379 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
380                              int yo, int xo)
381 {
382         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
383             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
384                 return;
385
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();
391
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;
397
398         workarea().getPainter().start();
399
400         drawFromTo(text, bv, top - text->first_y,
401                    bottom - text->first_y, yo,
402                    xo);
403         expose(0, top - text->first_y, workarea().workWidth(),
404                bottom - text->first_y - (top - text->first_y));
405
406         workarea().getPainter().end();
407 }
408
409
410 void LyXScreen::redraw(LyXText * text, BufferView * bv)
411 {
412         workarea().getPainter().start();
413
414         if (!text) {
415                 greyOut();
416                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
417                 workarea().getPainter().end();
418                 return;
419         }
420
421         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
422         expose(0, 0, workarea().workWidth(), workarea().workHeight());
423
424         workarea().getPainter().end();
425
426         if (cursor_visible_) {
427                 cursor_visible_ = false;
428                 bv->showCursor();
429         }
430 }
431
432
433 void LyXScreen::greyOut()
434 {
435         workarea().getPainter().fillRectangle(0, 0,
436                 workarea().workWidth(),
437                 workarea().workHeight(),
438                 LColor::bottomarea);
439
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();
443         if (splash_image) {
444                 int const w = splash_image->getWidth();
445                 int const h = splash_image->getHeight();
446
447                 int x = 0.5 * (workarea().workWidth() - w);
448                 int y = 0.5 * (workarea().workHeight() - h);
449
450                 workarea().getPainter().image(x, y, w, h, *splash_image);
451
452                 string const & splash_text  = splash.text();
453                 LyXFont const & splash_font = splash.font();
454                 
455                 x += 260;
456                 y += 265;
457
458                 workarea().getPainter().text(x, y, splash_text, splash_font);
459         }
460 }
461
462
463 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
464         int y1, int y2, int yo, int xo,
465         bool internal)
466 {
467         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
468
469         int y_text = text->first_y + y1;
470
471         // get the first needed row
472         Row * row = text->getRowNearY(y_text);
473         // y_text is now the real beginning of the row
474
475         int y = y_text - text->first_y;
476         // y1 is now the real beginning of row on the screen
477
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);
490                 }
491                 y += row->height();
492                 row = row->next();
493         }
494         force_clear_ = false;
495
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,
500                         LColor::bottomarea);
501         }
502 }
503
504
505 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
506         int y_text, int yo, int xo)
507 {
508         int const y = y_text - text->first_y + yo;
509
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);
513         }
514         force_clear_ = false;
515 }