]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
bug 1341
[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
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         // The ERT language hack needs fixing up
162         if (realfont.language() == latex_language)
163                 shape = BAR_SHAPE;
164
165         int ascent = font_metrics::maxAscent(realfont);
166         int descent = font_metrics::maxDescent(realfont);
167         int h = ascent + descent;
168         int x = 0;
169         int y = 0;
170         int const top_y = bv.top_y();
171
172         if (bv.theLockingInset()) {
173                 // Would be nice to clean this up to make some understandable sense...
174                 UpdatableInset * inset = bv.theLockingInset();
175                 inset->getCursor(bv, x, y);
176
177                 // Non-obvious. The reason we have to have these
178                 // extra checks is that the ->getCursor() calls rely
179                 // on the inset's own knowledge of its screen position.
180                 // If we scroll up or down in a big enough increment, the
181                 // inset->draw() is not called: this doesn't update
182                 // inset.top_baseline, so getCursor() returns an old value.
183                 // Ugly as you like.
184                 int bx, by;
185                 inset->getCursorPos(&bv, bx, by);
186                 by += inset->insetInInsetY() + bv.text->cursor.iy();
187                 if (by < top_y)
188                         return;
189                 if (by > top_y + workarea().workHeight())
190                         return;
191         } else {
192                 x = bv.text->cursor.x();
193                 y = bv.text->cursor.y();
194                 y -= top_y;
195         }
196
197         y -= ascent;
198
199         // if it doesn't fit entirely on the screen, don't try to show it
200         if (y < 0 || y + h > workarea().workHeight())
201                 return;
202
203         cursor_visible_ = true;
204         showCursor(x, y, h, shape);
205 }
206
207
208 void LyXScreen::hideCursor()
209 {
210         if (!cursor_visible_)
211                 return;
212
213         cursor_visible_ = false;
214         removeCursor();
215 }
216
217
218 void LyXScreen::toggleCursor(BufferView & bv)
219 {
220         if (cursor_visible_)
221                 hideCursor();
222         else
223                 showCursor(bv);
224 }
225
226
227 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
228         int /*x*/, int y, int asc, int desc)
229 {
230         int const vheight = workarea().workHeight();
231         int const topy = bv->top_y();
232         int newtop = topy;
233         
234         
235         if (y + desc - topy >= vheight)
236                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
237         else if (y - asc < topy && topy > 0)
238                 newtop = y - vheight / 4;
239
240         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
241
242         if (newtop == topy)
243                 return false;
244
245         bv->top_y(newtop);
246         return true;
247 }
248
249
250 unsigned int LyXScreen::topCursorVisible(LyXText * text)
251 {
252         LyXCursor const & cursor = text->cursor;
253         int top_y = text->bv()->top_y();
254         int newtop = top_y;
255         int const vheight = workarea().workHeight();
256
257         RowList::iterator row = text->cursorRow();
258
259         if (int(cursor.y() - row->baseline() + row->height() - top_y) >= vheight) {
260                 if (row->height() < vheight
261                     && row->height() > vheight / 4) {
262                         newtop = cursor.y()
263                                 + row->height()
264                                 - row->baseline() - vheight;
265                 } else {
266                         // scroll down, the scroll region must be so big!!
267                         newtop = cursor.y() - vheight / 2;
268                 }
269
270         } else if (int(cursor.y() - row->baseline()) < top_y && top_y > 0) {
271                 if (row->height() < vheight && row->height() > vheight / 4) {
272                         newtop = cursor.y() - row->baseline();
273                 } else {
274                         // scroll up
275                         newtop = cursor.y() - vheight / 2;
276                         newtop = min(newtop, top_y);
277                 }
278         }
279
280         return max(newtop, 0);
281 }
282
283
284 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
285 {
286         // Is a change necessary?
287         int const newtop = topCursorVisible(text);
288         bool const result = (newtop != bv->top_y());
289         bv->top_y(newtop);
290         return result;
291 }
292
293
294 void LyXScreen::redraw(BufferView & bv)
295 {
296         greyed_out_ = !bv.text;
297
298         if (greyed_out_) {
299                 greyOut();
300                 return;
301         }
302
303         workarea().getPainter().start();
304
305         hideCursor();
306
307         int const y = paintText(bv);
308
309         // maybe we have to clear the screen at the bottom
310         int const y2 = workarea().workHeight();
311         if (y < y2 && !bv.text->isInInset()) {
312                 workarea().getPainter().fillRectangle(0, y,
313                         workarea().workWidth(), y2 - y,
314                         LColor::bottomarea);
315         }
316
317         expose(0, 0, workarea().workWidth(), workarea().workHeight());
318
319         workarea().getPainter().end();
320 }
321
322
323 void LyXScreen::greyOut()
324 {
325         if (!greyed_out_)
326                 return;
327
328         workarea().getPainter().start();
329
330         workarea().getPainter().fillRectangle(0, 0,
331                 workarea().workWidth(),
332                 workarea().workHeight(),
333                 LColor::bottomarea);
334
335         // Add a splash screen to the centre of the work area
336         SplashScreen const & splash = SplashScreen::get();
337         lyx::graphics::Image const * const splash_image = splash.image();
338         if (splash_image) {
339                 int const w = splash_image->getWidth();
340                 int const h = splash_image->getHeight();
341
342                 int x = (workarea().workWidth() - w) / 2;
343                 int y = (workarea().workHeight() - h) / 2;
344
345                 workarea().getPainter().image(x, y, w, h, *splash_image);
346
347                 x += 260;
348                 y += 265;
349
350                 workarea().getPainter().text(x, y, splash.text(), splash.font());
351         }
352         expose(0, 0, workarea().workWidth(), workarea().workHeight());
353         workarea().getPainter().end();
354 }