]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
* disable loading of xpm files by the xforms image loader.
[lyx.git] / src / frontends / screen.C
1 /**
2  * \file screen.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  *
8  * Full author contact details are available in file CREDITS
9  *
10  * Splash screen code added by Angus Leeming
11  */
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include <config.h>
18
19 #include "screen.h"
20 #include "lyxtext.h"
21 #include "lyxrc.h"
22 #include "lyxrow.h"
23 #include "BufferView.h"
24 #include "buffer.h"
25 #include "WorkArea.h"
26 #include "Painter.h"
27 #include "font_metrics.h"
28 #include "language.h"
29 #include "debug.h"
30
31 // Splash screen-specific stuff
32 #include "lyxfont.h"
33 #include "version.h"
34
35 #include "graphics/GraphicsLoader.h"
36 #include "graphics/GraphicsImage.h"
37
38 #include "support/filetools.h" // LibFileSearch
39
40 #include <boost/utility.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/signals/trackable.hpp>
43
44 using std::min;
45 using std::max;
46 using std::endl;
47
48 namespace {
49
50 class SplashScreen : boost::noncopyable, boost::signals::trackable {
51 public:
52         /// This is a singleton class. Get the instance.
53         static SplashScreen const & get();
54         ///
55         grfx::Image const * image() const { return loader_.image(); }
56         ///
57         string const & text() const { return text_; }
58         ///
59         LyXFont const & font() const { return font_; }
60
61 private:
62         /** Make the c-tor private so we can control how many objects
63          *  are instantiated.
64          */
65         SplashScreen();
66
67         ///
68         grfx::Loader loader_;
69         /// The text to be written on top of the pixmap
70         string const text_;
71         /// in this font...
72         LyXFont font_;
73 };
74
75
76 SplashScreen const & SplashScreen::get()
77 {
78         static SplashScreen singleton;
79         return singleton;
80 }
81
82
83 SplashScreen::SplashScreen()
84         : text_(lyx_version ? lyx_version : "unknown")
85 {
86         if (!lyxrc.show_banner)
87                 return;
88
89         string const file = LibFileSearch("images", "banner", "ppm");
90         if (file.empty())
91                 return;
92
93         // The font used to display the version info
94         font_.setFamily(LyXFont::SANS_FAMILY);
95         font_.setSeries(LyXFont::BOLD_SERIES);
96         font_.setSize(LyXFont::SIZE_NORMAL);
97         font_.setColor(LColor::yellow);
98
99         // Load up the graphics file
100         loader_.reset(file);
101         // We aren't interested here in when the image is loaded.
102         // If it isn't ready when we want it, then we ignore it.
103 //      loader_->statusChanged.connect(
104 //                      boost::bind(&SplashScreen::statusChanged, this));
105         if (loader_.status() == grfx::WaitingToLoad)
106                 loader_.startLoading();
107 }
108
109 } // namespace anon
110
111
112 LyXScreen::LyXScreen()
113         : force_clear_(true), cursor_visible_(false)
114 {
115         // Start loading the pixmap as soon as possible
116         SplashScreen::get();
117 }
118
119
120 LyXScreen::~LyXScreen()
121 {
122 }
123
124 // FIXME: GUII these cursor methods need to decide
125 // whether the workarea is focused or not
126
127 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
128 {
129         if (cursor_visible_)
130                 return;
131
132         workarea().getPainter().start();
133
134         Cursor_Shape shape = BAR_SHAPE;
135         BufferParams const & bp(bv->buffer()->params);
136         LyXFont const & realfont(text->real_current_font);
137
138         if (realfont.language() != bp.language
139                 || realfont.isVisibleRightToLeft()
140                 != bp.language->RightToLeft()) {
141                 shape = (realfont.isVisibleRightToLeft())
142                         ? REVERSED_L_SHAPE : L_SHAPE;
143         }
144
145         showManualCursor(text, text->cursor.x(), text->cursor.y(),
146                 font_metrics::maxAscent(realfont),
147                 font_metrics::maxDescent(realfont),
148                 shape);
149
150         workarea().getPainter().end();
151 }
152
153
154 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
155         int /*x*/, int y, int asc, int desc)
156 {
157         int const vheight = workarea().workHeight();
158         int newtop = text->first_y;
159
160         if (y + desc - text->first_y >= vheight)
161                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
162         else if (y - asc < text->first_y
163                 && text->first_y > 0) {
164                 newtop = y - vheight / 4;
165         }
166
167         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
168
169         if (newtop != text->first_y) {
170                 draw(text, bv, newtop);
171                 text->first_y = newtop;
172                 return true;
173         }
174
175         return false;
176 }
177
178
179 void LyXScreen::cursorToggle(BufferView * bv) const
180 {
181         if (cursor_visible_)
182                 bv->hideCursor();
183         else
184                 bv->showCursor();
185 }
186
187
188 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
189 {
190         int const vheight = workarea().workHeight();
191         int newtop = top_y;
192
193         Row * row = cursor.row();
194
195         // Is this a hack? Yes, probably... (Lgb)
196         if (!row)
197                 return max(newtop, 0);
198
199         if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
200                 if (row->height() < vheight
201                     && row->height() > vheight / 4) {
202                         newtop = cursor.y()
203                                 + row->height()
204                                 - row->baseline() - vheight;
205                 } else {
206                         // scroll down
207                         newtop = cursor.y()
208                                 - vheight / 2;   /* the scroll region must be so big!! */
209                 }
210
211         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
212                    top_y && top_y > 0) {
213                 if (row->height() < vheight
214                     && row->height() > vheight / 4) {
215                         newtop = cursor.y() - row->baseline();
216                 } else {
217                         // scroll up
218                         newtop = cursor.y() - vheight / 2;
219                         newtop = min(newtop, top_y);
220                 }
221         }
222
223         newtop = max(newtop, 0);
224
225         return newtop;
226 }
227
228
229 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
230 {
231         // Is a change necessary?
232         int const newtop = topCursorVisible(text->cursor, text->first_y);
233         bool const result = (newtop != text->first_y);
234         if (result) {
235                 draw(text, bv, newtop);
236         }
237
238         return result;
239 }
240
241
242 void LyXScreen::update(LyXText * text, BufferView * bv,
243         int yo, int xo)
244 {
245         int const vwidth = workarea().workWidth();
246         int const vheight = workarea().workHeight();
247
248         workarea().getPainter().start();
249
250         switch (text->status()) {
251         case LyXText::NEED_MORE_REFRESH:
252         {
253                 int const y = max(int(text->refresh_y - text->first_y), 0);
254                 drawFromTo(text, bv, y, vheight, yo, xo);
255                 text->refresh_y = 0;
256                 // otherwise this is called ONLY from BufferView_pimpl(update)
257                 // or we should see to set this flag accordingly
258                 if (text != bv->text)
259                         text->status(bv, LyXText::UNCHANGED);
260                 expose(0, y, vwidth, vheight - y);
261         }
262         break;
263         case LyXText::NEED_VERY_LITTLE_REFRESH:
264         {
265                 // ok I will update the current cursor row
266                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
267                            yo, xo);
268                 // this because if we had a major update the refresh_row could
269                 // have been set to 0!
270                 if (text->refresh_row) {
271                         // otherwise this is called ONLY from BufferView_pimpl(update)
272                         // or we should see to set this flag accordingly
273                         if (text != bv->text)
274                                 text->status(bv, LyXText::UNCHANGED);
275                         expose(0, text->refresh_y - text->first_y + yo,
276                                    vwidth, text->refresh_row->height());
277                 }
278         }
279         break;
280         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
281         case LyXText::UNCHANGED:
282                 // Nothing needs done
283                 break;
284         }
285
286         workarea().getPainter().end();
287 }
288
289
290 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
291                                 bool kill_selection,
292                                 int yo, int xo)
293 {
294         // only if there is a selection
295         if (!text->selection.set()) return;
296
297         int const bottom = min(
298                 max(static_cast<int>(text->selection.end.y()
299                                      - text->selection.end.row()->baseline()
300                                      + text->selection.end.row()->height()),
301                     text->first_y),
302                 static_cast<int>(text->first_y + workarea().workHeight()));
303         int const top = min(
304                 max(static_cast<int>(text->selection.start.y() -
305                                      text->selection.start.row()->baseline()),
306                     text->first_y),
307                 static_cast<int>(text->first_y + workarea().workHeight()));
308
309         if (kill_selection)
310                 text->selection.set(false);
311
312         workarea().getPainter().start();
313
314         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
315                    yo, xo);
316         expose(0, top - text->first_y,
317                workarea().workWidth(),
318                bottom - text->first_y - (top - text->first_y));
319
320         workarea().getPainter().end();
321 }
322
323
324 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
325                              int yo, int xo)
326 {
327         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
328             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
329                 return;
330
331         int const top_tmp = text->toggle_cursor.y()
332                 - text->toggle_cursor.row()->baseline();
333         int const bottom_tmp = text->toggle_end_cursor.y()
334                 - text->toggle_end_cursor.row()->baseline()
335                 + text->toggle_end_cursor.row()->height();
336
337         int const offset = yo < 0 ? yo : 0;
338         int const bottom = min(max(bottom_tmp, text->first_y),
339                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
340         int const top = min(max(top_tmp, text->first_y),
341                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
342
343         workarea().getPainter().start();
344
345         drawFromTo(text, bv, top - text->first_y,
346                    bottom - text->first_y, yo,
347                    xo);
348         expose(0, top - text->first_y, workarea().workWidth(),
349                bottom - text->first_y - (top - text->first_y));
350
351         workarea().getPainter().end();
352 }
353
354
355 void LyXScreen::redraw(LyXText * text, BufferView * bv)
356 {
357         workarea().getPainter().start();
358
359         if (!text) {
360                 greyOut();
361                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
362                 workarea().getPainter().end();
363                 return;
364         }
365
366         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
367         expose(0, 0, workarea().workWidth(), workarea().workHeight());
368
369         workarea().getPainter().end();
370
371         if (cursor_visible_) {
372                 cursor_visible_ = false;
373                 bv->showCursor();
374         }
375 }
376
377
378 void LyXScreen::greyOut()
379 {
380         workarea().getPainter().fillRectangle(0, 0,
381                 workarea().workWidth(),
382                 workarea().workHeight(),
383                 LColor::bottomarea);
384
385         // Add a splash screen to the centre of the work area
386         SplashScreen const & splash = SplashScreen::get();
387         grfx::Image const * const splash_image = splash.image();
388         if (splash_image) {
389                 int const w = splash_image->getWidth();
390                 int const h = splash_image->getHeight();
391
392                 int x = (workarea().workWidth() - w) / 2;
393                 int y = (workarea().workHeight() - h) / 2;
394
395                 workarea().getPainter().image(x, y, w, h, *splash_image);
396
397                 string const & splash_text  = splash.text();
398                 LyXFont const & splash_font = splash.font();
399
400                 x += 260;
401                 y += 265;
402
403                 workarea().getPainter().text(x, y, splash_text, splash_font);
404         }
405 }
406
407
408 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
409         int y1, int y2, int yo, int xo,
410         bool internal)
411 {
412         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
413
414         int y_text = text->first_y + y1;
415
416         // get the first needed row
417         Row * row = text->getRowNearY(y_text);
418         // y_text is now the real beginning of the row
419
420         int y = y_text - text->first_y;
421         // y1 is now the real beginning of row on the screen
422
423         while (row != 0 && y < y2) {
424                 LyXText::text_status st = text->status();
425                 text->getVisibleRow(bv, y + yo,
426                                     xo, row, y + text->first_y);
427                 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
428                 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
429                         text->fullRebreak(bv);
430                         st = LyXText::NEED_MORE_REFRESH;
431                         text->setCursor(bv, text->cursor.par(), text->cursor.pos());
432                         text->status(bv, st);
433                         text->getVisibleRow(bv, y + yo,
434                                             xo, row, y + text->first_y);
435                 }
436                 y += row->height();
437                 row = row->next();
438         }
439         force_clear_ = false;
440
441         // maybe we have to clear the screen at the bottom
442         if ((y < y2) && text->bv_owner) {
443                 workarea().getPainter().fillRectangle(0, y,
444                         workarea().workWidth(), y2 - y,
445                         LColor::bottomarea);
446         }
447 }
448
449
450 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
451         int y_text, int yo, int xo)
452 {
453         int const y = y_text - text->first_y + yo;
454
455         if (((y + row->height()) > 0) &&
456             ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
457                 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
458         }
459         force_clear_ = false;
460 }