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