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