]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
namespace grfx -> lyx::graphics
[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 #include "insets/updatableinset.h"
29 #include "mathed/formulabase.h"
30 #include "lyx_gui.h"
31
32 // Splash screen-specific stuff
33 #include "lyxfont.h"
34 #include "version.h"
35
36 #include "graphics/GraphicsLoader.h"
37 #include "graphics/GraphicsImage.h"
38
39 #include "support/filetools.h" // LibFileSearch
40
41 #include <boost/utility.hpp>
42 #include <boost/bind.hpp>
43 #include <boost/signals/trackable.hpp>
44
45 using namespace lyx::support;
46
47 using std::min;
48 using std::max;
49 using std::endl;
50
51 namespace grfx = lyx::graphics;
52
53 namespace {
54
55 class SplashScreen : boost::noncopyable, boost::signals::trackable {
56 public:
57         /// This is a singleton class. Get the instance.
58         static SplashScreen const & get();
59         ///
60         grfx::Image const * image() const { return loader_.image(); }
61         ///
62         string const & text() const { return text_; }
63         ///
64         LyXFont const & font() const { return font_; }
65         ///
66         void connect(grfx::Loader::slot_type const & slot) const {
67                 loader_.connect(slot);
68         }
69         ///
70         void startLoading() const {
71                 if (loader_.status() == grfx::WaitingToLoad)
72                         loader_.startLoading();
73         }
74
75 private:
76         /** Make the c-tor private so we can control how many objects
77          *  are instantiated.
78          */
79         SplashScreen();
80
81         ///
82         grfx::Loader loader_;
83         /// The text to be written on top of the pixmap
84         string const text_;
85         /// in this font...
86         LyXFont font_;
87 };
88
89
90 SplashScreen const & SplashScreen::get()
91 {
92         static SplashScreen singleton;
93         return singleton;
94 }
95
96
97 SplashScreen::SplashScreen()
98         : text_(lyx_version ? lyx_version : "unknown")
99 {
100         if (!lyxrc.show_banner)
101                 return;
102
103         string const file = LibFileSearch("images", "banner", "ppm");
104         if (file.empty())
105                 return;
106
107         // The font used to display the version info
108         font_.setFamily(LyXFont::SANS_FAMILY);
109         font_.setSeries(LyXFont::BOLD_SERIES);
110         font_.setSize(LyXFont::SIZE_NORMAL);
111         font_.setColor(LColor::yellow);
112
113         // Load up the graphics file
114         loader_.reset(file);
115 }
116
117 } // namespace anon
118
119
120 LyXScreen::LyXScreen()
121         : cursor_visible_(false), greyed_out_(true)
122 {
123         // Start loading the pixmap as soon as possible
124         if (lyxrc.show_banner) {
125                 SplashScreen const & splash = SplashScreen::get();
126                 splash.connect(boost::bind(&LyXScreen::greyOut, this));
127                 splash.startLoading();
128         }
129 }
130
131
132 LyXScreen::~LyXScreen()
133 {
134 }
135
136
137 void LyXScreen::showCursor(BufferView & bv)
138 {
139         // this is needed to make sure we copy back the right
140         // pixmap on the hide for the Qt frontend
141         lyx_gui::sync_events();
142
143         if (cursor_visible_)
144                 return;
145
146         if (!bv.available())
147                 return;
148
149         Cursor_Shape shape = BAR_SHAPE;
150
151         LyXText const & text = *bv.getLyXText();
152         LyXFont const & realfont(text.real_current_font);
153         BufferParams const & bp(bv.buffer()->params);
154         bool const samelang = realfont.language() == bp.language;
155         bool const isrtl = realfont.isVisibleRightToLeft();
156
157         if (!samelang || isrtl != bp.language->RightToLeft()) {
158                 shape = L_SHAPE;
159                 if (isrtl)
160                         shape = REVERSED_L_SHAPE;
161         }
162
163         int ascent = font_metrics::maxAscent(realfont);
164         int descent = font_metrics::maxDescent(realfont);
165         int h = ascent + descent;
166         int x = 0;
167         int y = 0;
168         int const top_y = bv.text->top_y();
169
170         if (bv.theLockingInset()) {
171                 // Would be nice to clean this up to make some understandable sense...
172                 UpdatableInset * inset = bv.theLockingInset();
173                 inset->getCursor(bv, x, y);
174
175                 // Non-obvious. The reason we have to have these
176                 // extra checks is that the ->getCursor() calls rely
177                 // on the inset's own knowledge of its screen position.
178                 // If we scroll up or down in a big enough increment, the
179                 // inset->draw() is not called: this doesn't update
180                 // inset.top_baseline, so getCursor() returns an old value.
181                 // Ugly as you like.
182                 int bx, by;
183                 inset->getCursorPos(&bv, bx, by);
184                 by += inset->insetInInsetY() + bv.text->cursor.iy();
185                 if (by < top_y)
186                         return;
187                 if (by > top_y + workarea().workHeight())
188                         return;
189         } else {
190                 x = bv.text->cursor.x();
191                 y = bv.text->cursor.y();
192                 y -= top_y;
193         }
194
195         y -= ascent;
196
197         // if it doesn't fit entirely on the screen, don't try to show it
198         if (y < 0 || y + h > workarea().workHeight())
199                 return;
200
201         cursor_visible_ = true;
202         showCursor(x, y, h, shape);
203 }
204
205
206 void LyXScreen::hideCursor()
207 {
208         if (!cursor_visible_)
209                 return;
210
211         cursor_visible_ = false;
212         removeCursor();
213 }
214
215
216 void LyXScreen::toggleCursor(BufferView & bv)
217 {
218         if (cursor_visible_)
219                 hideCursor();
220         else
221                 showCursor(bv);
222 }
223
224
225 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
226         int /*x*/, int y, int asc, int desc)
227 {
228         int const vheight = workarea().workHeight();
229         int newtop = text->top_y();
230
231         if (y + desc - text->top_y() >= vheight)
232                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
233         else if (y - asc < text->top_y()
234                 && text->top_y() > 0) {
235                 newtop = y - vheight / 4;
236         }
237
238         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
239
240         if (newtop != text->top_y()) {
241                 draw(text, bv, newtop);
242                 text->top_y(newtop);
243                 return true;
244         }
245
246         return false;
247 }
248
249
250 unsigned int LyXScreen::topCursorVisible(LyXText * text)
251 {
252         LyXCursor const & cursor = text->cursor;
253         int top_y = text->top_y();
254         int newtop = top_y;
255         int const vheight = workarea().workHeight();
256
257         RowList::iterator row = text->cursorRow();
258
259 #warning SUPER HACK DISABLED (Lgb)
260 #if 0
261         // Is this a hack? Yes, probably... (Lgb)
262         if (!row)
263                 return max(newtop, 0);
264 #endif
265         if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
266                 if (row->height() < vheight
267                     && row->height() > vheight / 4) {
268                         newtop = cursor.y()
269                                 + row->height()
270                                 - row->baseline() - vheight;
271                 } else {
272                         // scroll down, the scroll region must be so big!!
273                         newtop = cursor.y() - vheight / 2;
274                 }
275
276         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
277                    top_y && top_y > 0) {
278                 if (row->height() < vheight
279                     && row->height() > vheight / 4) {
280                         newtop = cursor.y() - row->baseline();
281                 } else {
282                         // scroll up
283                         newtop = cursor.y() - vheight / 2;
284                         newtop = min(newtop, top_y);
285                 }
286         }
287
288         newtop = max(newtop, 0);
289
290         return newtop;
291 }
292
293
294 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
295 {
296         // Is a change necessary?
297         int const newtop = topCursorVisible(text);
298         bool const result = (newtop != text->top_y());
299         if (result) {
300                 draw(text, bv, newtop);
301         }
302
303         return result;
304 }
305
306
307 void LyXScreen::update(BufferView & bv, int yo, int xo)
308 {
309         int const vwidth = workarea().workWidth();
310         int const vheight = workarea().workHeight();
311         LyXText * text = bv.text;
312
313         workarea().getPainter().start();
314
315         switch (text->refreshStatus()) {
316         case LyXText::REFRESH_AREA:
317         {
318                 text->updateRowPositions();
319                 int const y = max(int(text->refresh_y - text->top_y()), 0);
320                 drawFromTo(text, &bv, y, vheight, yo, xo);
321                 expose(0, y, vwidth, vheight - y);
322         }
323         break;
324         case LyXText::REFRESH_ROW:
325         {
326                 text->updateRowPositions();
327                 // ok I will update the current cursor row
328                 drawOneRow(text, &bv, text->refresh_row, text->refresh_y,
329                            yo, xo);
330                 // this because if we had a major update the refresh_row could
331                 // have been set to 0!
332                 if (text->refresh_row != text->rows().end()) {
333                         expose(0, text->refresh_y - text->top_y() + yo,
334                                    vwidth, text->refresh_row->height());
335                 }
336         }
337         break;
338         case LyXText::REFRESH_NONE:
339                 // Nothing needs done
340                 break;
341         }
342
343         workarea().getPainter().end();
344 }
345
346
347 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
348                                 bool kill_selection,
349                                 int yo, int xo)
350 {
351         // only if there is a selection
352         if (!text->selection.set()) return;
353
354         int const bottom = min(
355                 max(static_cast<int>(text->selection.end.y()
356                                      - text->getRow(text->selection.end)->baseline()
357                                      + text->getRow(text->selection.end)->height()),
358                     text->top_y()),
359                 static_cast<int>(text->top_y() + workarea().workHeight()));
360         int const top = min(
361                 max(static_cast<int>(text->selection.start.y() -
362                                      text->getRow(text->selection.start)->baseline()),
363                     text->top_y()),
364                 static_cast<int>(text->top_y() + workarea().workHeight()));
365
366         if (kill_selection)
367                 text->selection.set(false);
368
369         workarea().getPainter().start();
370
371         drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
372                    yo, xo);
373         expose(0, top - text->top_y(),
374                workarea().workWidth(),
375                bottom - text->top_y() - (top - text->top_y()));
376
377         workarea().getPainter().end();
378 }
379
380
381 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
382                              int yo, int xo)
383 {
384         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
385             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
386                 return;
387
388         int const top_tmp = text->toggle_cursor.y()
389                 - text->getRow(text->toggle_cursor)->baseline();
390         int const bottom_tmp = text->toggle_end_cursor.y()
391                 - text->getRow(text->toggle_end_cursor)->baseline()
392                 + text->getRow(text->toggle_end_cursor)->height();
393
394         int const offset = yo < 0 ? yo : 0;
395         int const bottom = min(max(bottom_tmp, text->top_y()),
396                 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
397         int const top = min(max(top_tmp, text->top_y()),
398                 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
399
400         workarea().getPainter().start();
401
402         drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
403         expose(0, top - text->top_y(), workarea().workWidth(),
404                bottom - text->top_y() - (top - text->top_y()));
405
406         workarea().getPainter().end();
407 }
408
409
410 void LyXScreen::redraw(LyXText * text, BufferView * bv)
411 {
412         greyed_out_ = !text;
413
414         if (greyed_out_) {
415                 greyOut();
416                 return;
417         }
418
419         workarea().getPainter().start();
420
421         text->updateRowPositions();
422         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
423         expose(0, 0, workarea().workWidth(), workarea().workHeight());
424
425         workarea().getPainter().end();
426 }
427
428
429 void LyXScreen::greyOut()
430 {
431         if (!greyed_out_)
432                 return;
433
434         workarea().getPainter().start();
435
436         workarea().getPainter().fillRectangle(0, 0,
437                 workarea().workWidth(),
438                 workarea().workHeight(),
439                 LColor::bottomarea);
440
441         // Add a splash screen to the centre of the work area
442         SplashScreen const & splash = SplashScreen::get();
443         grfx::Image const * const splash_image = splash.image();
444         if (splash_image) {
445                 int const w = splash_image->getWidth();
446                 int const h = splash_image->getHeight();
447
448                 int x = (workarea().workWidth() - w) / 2;
449                 int y = (workarea().workHeight() - h) / 2;
450
451                 workarea().getPainter().image(x, y, w, h, *splash_image);
452
453                 string const & splash_text  = splash.text();
454                 LyXFont const & splash_font = splash.font();
455
456                 x += 260;
457                 y += 265;
458
459                 workarea().getPainter().text(x, y, splash_text, splash_font);
460         }
461         expose(0, 0, workarea().workWidth(), workarea().workHeight());
462         workarea().getPainter().end();
463 }
464
465
466 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
467         int y1, int y2, int yo, int xo)
468 {
469         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
470
471         int const topy = text->top_y();
472         int y_text = topy + y1;
473         RowList::iterator rit = text->getRowNearY(y_text);
474         int y = y_text - topy;
475         // y1 is now the real beginning of row on the screen
476
477         hideCursor();
478
479         RowList::iterator const rend = text->rows().end();
480         while (rit != rend && y < y2) {
481                 RowPainter rp(*bv, *text, rit);
482                 rp.paint(y + yo, xo, y + topy);
483                 y += rit->height();
484                 ++rit;
485         }
486
487         // maybe we have to clear the screen at the bottom
488         if ((y < y2) && !text->isInInset()) {
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,
497                            RowList::iterator row,
498         int y_text, int yo, int xo)
499 {
500         int const y = y_text - text->top_y() + yo;
501
502         if (y + row->height() <= 0)
503                 return;
504
505         if (y - row->height() > workarea().workHeight())
506                 return;
507
508         hideCursor();
509
510         RowPainter rp(*bv, *text, row);
511         rp.paint(y, xo, y + text->top_y());
512 }