]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
403647da4d75b674ec9417303c71aaf385d1c7d6
[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", "xpm");
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                 // Make the screen not scroll too fast when
173                 // we have a selection.
174                 if (text->selection.set())
175                         usleep(200000);
176                 return true;
177         }
178
179         return false;
180 }
181
182
183 void LyXScreen::cursorToggle(BufferView * bv) const
184 {
185         if (cursor_visible_)
186                 bv->hideCursor();
187         else
188                 bv->showCursor();
189 }
190
191
192 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
193 {
194         int const vheight = workarea().workHeight();
195         int newtop = top_y;
196
197         Row * row = cursor.row();
198
199         // Is this a hack? Yes, probably... (Lgb)
200         if (!row)
201                 return max(newtop, 0);
202
203         if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
204                 if (row->height() < vheight
205                     && row->height() > vheight / 4) {
206                         newtop = cursor.y()
207                                 + row->height()
208                                 - row->baseline() - vheight;
209                 } else {
210                         // scroll down
211                         newtop = cursor.y()
212                                 - vheight / 2;   /* the scroll region must be so big!! */
213                 }
214
215         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
216                    top_y && top_y > 0) {
217                 if (row->height() < vheight
218                     && row->height() > vheight / 4) {
219                         newtop = cursor.y() - row->baseline();
220                 } else {
221                         // scroll up
222                         newtop = cursor.y() - vheight / 2;
223                         newtop = min(newtop, top_y);
224                 }
225         }
226
227         newtop = max(newtop, 0);
228
229         return newtop;
230 }
231
232
233 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
234 {
235         // Is a change necessary?
236         int const newtop = topCursorVisible(text->cursor, text->first_y);
237         bool const result = (newtop != text->first_y);
238         if (result) {
239                 draw(text, bv, newtop);
240                 // Make the screen not scroll too fast when
241                 // we have a selection.
242                 if (text->selection.set())
243                         usleep(200000);
244         }
245
246         return result;
247 }
248
249
250 void LyXScreen::update(LyXText * text, BufferView * bv,
251         int yo, int xo)
252 {
253         int const vwidth = workarea().workWidth();
254         int const vheight = workarea().workHeight();
255
256         workarea().getPainter().start();
257
258         switch (text->status()) {
259         case LyXText::NEED_MORE_REFRESH:
260         {
261                 int const y = max(int(text->refresh_y - text->first_y), 0);
262                 drawFromTo(text, bv, y, vheight, yo, xo);
263                 text->refresh_y = 0;
264                 // otherwise this is called ONLY from BufferView_pimpl(update)
265                 // or we should see to set this flag accordingly
266                 if (text != bv->text)
267                         text->status(bv, LyXText::UNCHANGED);
268                 expose(0, y, vwidth, vheight - y);
269         }
270         break;
271         case LyXText::NEED_VERY_LITTLE_REFRESH:
272         {
273                 // ok I will update the current cursor row
274                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
275                            yo, xo);
276                 // this because if we had a major update the refresh_row could
277                 // have been set to 0!
278                 if (text->refresh_row) {
279                         // otherwise this is called ONLY from BufferView_pimpl(update)
280                         // or we should see to set this flag accordingly
281                         if (text != bv->text)
282                                 text->status(bv, LyXText::UNCHANGED);
283                         expose(0, text->refresh_y - text->first_y + yo,
284                                    vwidth, text->refresh_row->height());
285                 }
286         }
287         break;
288         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
289         case LyXText::UNCHANGED:
290                 // Nothing needs done
291                 break;
292         }
293
294         workarea().getPainter().end();
295 }
296
297
298 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
299                                 bool kill_selection,
300                                 int yo, int xo)
301 {
302         // only if there is a selection
303         if (!text->selection.set()) return;
304
305         int const bottom = min(
306                 max(static_cast<int>(text->selection.end.y()
307                                      - text->selection.end.row()->baseline()
308                                      + text->selection.end.row()->height()),
309                     text->first_y),
310                 static_cast<int>(text->first_y + workarea().workHeight()));
311         int const top = min(
312                 max(static_cast<int>(text->selection.start.y() -
313                                      text->selection.start.row()->baseline()),
314                     text->first_y),
315                 static_cast<int>(text->first_y + workarea().workHeight()));
316
317         if (kill_selection)
318                 text->selection.set(false);
319
320         workarea().getPainter().start();
321
322         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
323                    yo, xo);
324         expose(0, top - text->first_y,
325                workarea().workWidth(),
326                bottom - text->first_y - (top - text->first_y));
327
328         workarea().getPainter().end();
329 }
330
331
332 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
333                              int yo, int xo)
334 {
335         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
336             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
337                 return;
338
339         int const top_tmp = text->toggle_cursor.y()
340                 - text->toggle_cursor.row()->baseline();
341         int const bottom_tmp = text->toggle_end_cursor.y()
342                 - text->toggle_end_cursor.row()->baseline()
343                 + text->toggle_end_cursor.row()->height();
344
345         int const offset = yo < 0 ? yo : 0;
346         int const bottom = min(max(bottom_tmp, text->first_y),
347                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
348         int const top = min(max(top_tmp, text->first_y),
349                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
350
351         workarea().getPainter().start();
352
353         drawFromTo(text, bv, top - text->first_y,
354                    bottom - text->first_y, yo,
355                    xo);
356         expose(0, top - text->first_y, workarea().workWidth(),
357                bottom - text->first_y - (top - text->first_y));
358
359         workarea().getPainter().end();
360 }
361
362
363 void LyXScreen::redraw(LyXText * text, BufferView * bv)
364 {
365         workarea().getPainter().start();
366
367         if (!text) {
368                 greyOut();
369                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
370                 workarea().getPainter().end();
371                 return;
372         }
373
374         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
375         expose(0, 0, workarea().workWidth(), workarea().workHeight());
376
377         workarea().getPainter().end();
378
379         if (cursor_visible_) {
380                 cursor_visible_ = false;
381                 bv->showCursor();
382         }
383 }
384
385
386 void LyXScreen::greyOut()
387 {
388         workarea().getPainter().fillRectangle(0, 0,
389                 workarea().workWidth(),
390                 workarea().workHeight(),
391                 LColor::bottomarea);
392
393         // Add a splash screen to the centre of the work area
394         SplashScreen const & splash = SplashScreen::get();
395         grfx::Image const * const splash_image = splash.image();
396         if (splash_image) {
397                 int const w = splash_image->getWidth();
398                 int const h = splash_image->getHeight();
399
400                 int x = (workarea().workWidth() - w) / 2;
401                 int y = (workarea().workHeight() - h) / 2;
402
403                 workarea().getPainter().image(x, y, w, h, *splash_image);
404
405                 string const & splash_text  = splash.text();
406                 LyXFont const & splash_font = splash.font();
407
408                 x += 260;
409                 y += 265;
410
411                 workarea().getPainter().text(x, y, splash_text, splash_font);
412         }
413 }
414
415
416 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
417         int y1, int y2, int yo, int xo,
418         bool internal)
419 {
420         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
421
422         int y_text = text->first_y + y1;
423
424         // get the first needed row
425         Row * row = text->getRowNearY(y_text);
426         // y_text is now the real beginning of the row
427
428         int y = y_text - text->first_y;
429         // y1 is now the real beginning of row on the screen
430
431         while (row != 0 && y < y2) {
432                 LyXText::text_status st = text->status();
433                 text->getVisibleRow(bv, y + yo,
434                                     xo, row, y + text->first_y);
435                 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
436                 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
437                         text->fullRebreak(bv);
438                         st = LyXText::NEED_MORE_REFRESH;
439                         text->setCursor(bv, text->cursor.par(), text->cursor.pos());
440                         text->status(bv, st);
441                         text->getVisibleRow(bv, y + yo,
442                                             xo, row, y + text->first_y);
443                 }
444                 y += row->height();
445                 row = row->next();
446         }
447         force_clear_ = false;
448
449         // maybe we have to clear the screen at the bottom
450         if ((y < y2) && text->bv_owner) {
451                 workarea().getPainter().fillRectangle(0, y,
452                         workarea().workWidth(), y2 - y,
453                         LColor::bottomarea);
454         }
455 }
456
457
458 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
459         int y_text, int yo, int xo)
460 {
461         int const y = y_text - text->first_y + yo;
462
463         if (((y + row->height()) > 0) &&
464             ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
465                 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
466         }
467         force_clear_ = false;
468 }