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