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