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