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