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