]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
86afacc6ee0f7d8af7fdef7b9b31b3b0e9a4a528
[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 #include <config.h>
14
15 #include "screen.h"
16 #include "font_metrics.h"
17 #include "lyx_gui.h"
18 #include "Painter.h"
19 #include "WorkArea.h"
20
21 #include "BufferView.h"
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "debug.h"
25 #include "language.h"
26 #include "LColor.h"
27 #include "lyxfont.h"
28 #include "lyxrc.h"
29 #include "lyxrow.h"
30 #include "lyxtext.h"
31 #include "metricsinfo.h"
32 #include "rowpainter.h"
33 #include "version.h"
34
35 #include "insets/updatableinset.h"
36
37 #include "graphics/GraphicsImage.h"
38 #include "graphics/GraphicsLoader.h"
39
40 #include "support/filetools.h" // LibFileSearch
41
42 #include <boost/utility.hpp>
43 #include <boost/bind.hpp>
44 #include <boost/signals/trackable.hpp>
45
46 using lyx::support::LibFileSearch;
47
48 using std::min;
49 using std::max;
50
51
52 namespace {
53
54 class SplashScreen : boost::noncopyable, boost::signals::trackable {
55 public:
56         /// This is a singleton class. Get the instance.
57         static SplashScreen const & get();
58         ///
59         lyx::graphics::Image const * image() const { return loader_.image(); }
60         ///
61         string const & text() const { return text_; }
62         ///
63         LyXFont const & font() const { return font_; }
64         ///
65         void connect(lyx::graphics::Loader::slot_type const & slot) const {
66                 loader_.connect(slot);
67         }
68         ///
69         void startLoading() const {
70                 if (loader_.status() == lyx::graphics::WaitingToLoad)
71                         loader_.startLoading();
72         }
73
74 private:
75         /** Make the c-tor private so we can control how many objects
76          *  are instantiated.
77          */
78         SplashScreen();
79
80         ///
81         lyx::graphics::Loader loader_;
82         /// The text to be written on top of the pixmap
83         string const text_;
84         /// in this font...
85         LyXFont font_;
86 };
87
88
89 SplashScreen const & SplashScreen::get()
90 {
91         static SplashScreen singleton;
92         return singleton;
93 }
94
95
96 SplashScreen::SplashScreen()
97         : text_(lyx_version ? lyx_version : "unknown")
98 {
99         if (!lyxrc.show_banner)
100                 return;
101
102         string const file = LibFileSearch("images", "banner", "ppm");
103         if (file.empty())
104                 return;
105
106         // The font used to display the version info
107         font_.setFamily(LyXFont::SANS_FAMILY);
108         font_.setSeries(LyXFont::BOLD_SERIES);
109         font_.setSize(LyXFont::SIZE_NORMAL);
110         font_.setColor(LColor::yellow);
111
112         // Load up the graphics file
113         loader_.reset(file);
114 }
115
116 } // namespace anon
117
118
119 LyXScreen::LyXScreen()
120         : cursor_visible_(false), greyed_out_(true)
121 {
122         // Start loading the pixmap as soon as possible
123         if (lyxrc.show_banner) {
124                 SplashScreen const & splash = SplashScreen::get();
125                 splash.connect(boost::bind(&LyXScreen::greyOut, this));
126                 splash.startLoading();
127         }
128 }
129
130
131 LyXScreen::~LyXScreen()
132 {
133 }
134
135
136 void LyXScreen::showCursor(BufferView & bv)
137 {
138         // this is needed to make sure we copy back the right
139         // pixmap on the hide for the Qt frontend
140         lyx_gui::sync_events();
141
142         if (cursor_visible_)
143                 return;
144
145         if (!bv.available())
146                 return;
147
148         Cursor_Shape shape = BAR_SHAPE;
149
150         LyXText const & text = *bv.getLyXText();
151         LyXFont const & realfont = text.real_current_font;
152         BufferParams const & bp = bv.buffer()->params();
153         bool const samelang = realfont.language() == bp.language;
154         bool const isrtl = realfont.isVisibleRightToLeft();
155
156         if (!samelang || isrtl != bp.language->RightToLeft()) {
157                 shape = L_SHAPE;
158                 if (isrtl)
159                         shape = REVERSED_L_SHAPE;
160         }
161
162         // The ERT language hack needs fixing up
163         if (realfont.language() == latex_language)
164                 shape = BAR_SHAPE;
165
166         int ascent = font_metrics::maxAscent(realfont);
167         int descent = font_metrics::maxDescent(realfont);
168         int h = ascent + descent;
169         int x = 0;
170         int y = 0;
171         int const top_y = bv.top_y();
172
173         if (bv.theLockingInset()) {
174                 // Would be nice to clean this up to make some understandable sense...
175                 UpdatableInset * inset = bv.theLockingInset();
176                 inset->getCursor(bv, x, y);
177
178                 // Non-obvious. The reason we have to have these
179                 // extra checks is that the ->getCursor() calls rely
180                 // on the inset's own knowledge of its screen position.
181                 // If we scroll up or down in a big enough increment, the
182                 // inset->draw() is not called: this doesn't update
183                 // inset.top_baseline, so getCursor() returns an old value.
184                 // Ugly as you like.
185                 int bx, by;
186                 inset->getCursorPos(&bv, bx, by);
187                 by += inset->insetInInsetY() + bv.text->cursor.y();
188                 if (by < top_y)
189                         return;
190                 if (by > top_y + workarea().workHeight())
191                         return;
192         } else {
193                 x = bv.text->cursor.x();
194                 y = bv.text->cursor.y();
195                 y -= top_y;
196         }
197
198         y -= ascent;
199
200         // if it doesn't fit entirely on the screen, don't try to show it
201         if (y < 0 || y + h > workarea().workHeight())
202                 return;
203
204         cursor_visible_ = true;
205         showCursor(x, y, h, shape);
206 }
207
208
209 void LyXScreen::hideCursor()
210 {
211         if (!cursor_visible_)
212                 return;
213
214         cursor_visible_ = false;
215         removeCursor();
216 }
217
218
219 void LyXScreen::toggleCursor(BufferView & bv)
220 {
221         if (cursor_visible_)
222                 hideCursor();
223         else
224                 showCursor(bv);
225 }
226
227
228 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText *,
229         int /*x*/, int y, int asc, int desc)
230 {
231         int const vheight = workarea().workHeight();
232         int const topy = bv->top_y();
233         int newtop = topy;
234
235
236         if (y + desc - topy >= vheight)
237                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
238         else if (y - asc < topy && topy > 0)
239                 newtop = y - vheight / 4;
240
241         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
242
243         if (newtop == topy)
244                 return false;
245
246         bv->top_y(newtop);
247         return true;
248 }
249
250
251 unsigned int LyXScreen::topCursorVisible(LyXText * text)
252 {
253         LyXCursor const & cursor = text->cursor;
254         int top_y = text->bv()->top_y();
255         int newtop = top_y;
256         unsigned int const vheight = workarea().workHeight();
257
258         RowList::iterator row = text->cursorRow();
259
260         if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
261                 if (row->height() < vheight
262                     && row->height() > vheight / 4) {
263                         newtop = cursor.y()
264                                 + row->height()
265                                 - row->baseline() - vheight;
266                 } else {
267                         // scroll down, the scroll region must be so big!!
268                         newtop = cursor.y() - vheight / 2;
269                 }
270
271         } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
272                 if (row->height() < vheight && row->height() > vheight / 4) {
273                         newtop = cursor.y() - row->baseline();
274                 } else {
275                         // scroll up
276                         newtop = cursor.y() - vheight / 2;
277                         newtop = min(newtop, top_y);
278                 }
279         }
280
281         return max(newtop, 0);
282 }
283
284
285 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
286 {
287         // Is a change necessary?
288         int const newtop = topCursorVisible(text);
289         bool const result = (newtop != bv->top_y());
290         bv->top_y(newtop);
291         return result;
292 }
293
294
295 void LyXScreen::redraw(BufferView & bv)
296 {
297         greyed_out_ = !bv.text;
298
299         if (greyed_out_) {
300                 greyOut();
301                 return;
302         }
303
304         workarea().getPainter().start();
305
306         hideCursor();
307
308         int const y = paintText(bv);
309
310         // maybe we have to clear the screen at the bottom
311         int const y2 = workarea().workHeight();
312         if (y < y2 && !bv.text->isInInset()) {
313                 workarea().getPainter().fillRectangle(0, y,
314                         workarea().workWidth(), y2 - y,
315                         LColor::bottomarea);
316         }
317
318         expose(0, 0, workarea().workWidth(), workarea().workHeight());
319
320         workarea().getPainter().end();
321 }
322
323
324 void LyXScreen::greyOut()
325 {
326         if (!greyed_out_)
327                 return;
328
329         workarea().getPainter().start();
330
331         workarea().getPainter().fillRectangle(0, 0,
332                 workarea().workWidth(),
333                 workarea().workHeight(),
334                 LColor::bottomarea);
335
336         // Add a splash screen to the centre of the work area
337         SplashScreen const & splash = SplashScreen::get();
338         lyx::graphics::Image const * const splash_image = splash.image();
339         if (splash_image) {
340                 int const w = splash_image->getWidth();
341                 int const h = splash_image->getHeight();
342
343                 int x = (workarea().workWidth() - w) / 2;
344                 int y = (workarea().workHeight() - h) / 2;
345
346                 workarea().getPainter().image(x, y, w, h, *splash_image);
347
348                 x += 260;
349                 y += 265;
350
351                 workarea().getPainter().text(x, y, splash.text(), splash.font());
352         }
353         expose(0, 0, workarea().workWidth(), workarea().workHeight());
354         workarea().getPainter().end();
355 }