]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
include sys/time.h
[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
14 #include <config.h>
15
16 #include "screen.h"
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
28 // Splash screen-specific stuff
29 #include "lyxfont.h"
30 #include "version.h"
31
32 #include "graphics/GraphicsLoader.h"
33 #include "graphics/GraphicsImage.h"
34
35 #include "support/filetools.h" // LibFileSearch
36
37 #include <boost/utility.hpp>
38 #include <boost/bind.hpp>
39 #include <boost/signals/trackable.hpp>
40
41 using std::min;
42 using std::max;
43 using std::endl;
44
45 namespace {
46
47 class SplashScreen : boost::noncopyable, boost::signals::trackable {
48 public:
49         /// This is a singleton class. Get the instance.
50         static SplashScreen const & get();
51         ///
52         grfx::Image const * image() const { return loader_.image(); }
53         ///
54         string const & text() const { return text_; }
55         ///
56         LyXFont const & font() const { return font_; }
57
58 private:
59         /** Make the c-tor private so we can control how many objects
60          *  are instantiated.
61          */
62         SplashScreen();
63
64         ///
65         grfx::Loader loader_;
66         /// The text to be written on top of the pixmap
67         string const text_;
68         /// in this font...
69         LyXFont font_;
70 };
71
72
73 SplashScreen const & SplashScreen::get()
74 {
75         static SplashScreen singleton;
76         return singleton;
77 }
78
79
80 SplashScreen::SplashScreen()
81         : text_(lyx_version ? lyx_version : "unknown")
82 {
83         if (!lyxrc.show_banner)
84                 return;
85
86         string const file = LibFileSearch("images", "banner", "ppm");
87         if (file.empty())
88                 return;
89
90         // The font used to display the version info
91         font_.setFamily(LyXFont::SANS_FAMILY);
92         font_.setSeries(LyXFont::BOLD_SERIES);
93         font_.setSize(LyXFont::SIZE_NORMAL);
94         font_.setColor(LColor::yellow);
95
96         // Load up the graphics file
97         loader_.reset(file);
98         // We aren't interested here in when the image is loaded.
99         // If it isn't ready when we want it, then we ignore it.
100 //      loader_->statusChanged.connect(
101 //                      boost::bind(&SplashScreen::statusChanged, this));
102         if (loader_.status() == grfx::WaitingToLoad)
103                 loader_.startLoading();
104 }
105
106 } // namespace anon
107
108
109 LyXScreen::LyXScreen()
110         : force_clear_(true), cursor_visible_(false)
111 {
112         // Start loading the pixmap as soon as possible
113         SplashScreen::get();
114 }
115
116
117 LyXScreen::~LyXScreen()
118 {
119 }
120
121 // FIXME: GUII these cursor methods need to decide
122 // whether the workarea is focused or not
123
124 void LyXScreen::showCursor(LyXText const * text, BufferView const * bv)
125 {
126         if (cursor_visible_)
127                 return;
128
129         workarea().getPainter().start();
130
131         Cursor_Shape shape = BAR_SHAPE;
132         BufferParams const & bp(bv->buffer()->params);
133         LyXFont const & realfont(text->real_current_font);
134
135         if (realfont.language() != bp.language
136                 || realfont.isVisibleRightToLeft()
137                 != bp.language->RightToLeft()) {
138                 shape = (realfont.isVisibleRightToLeft())
139                         ? REVERSED_L_SHAPE : L_SHAPE;
140         }
141
142         showManualCursor(text, text->cursor.x(), text->cursor.y(),
143                 font_metrics::maxAscent(realfont),
144                 font_metrics::maxDescent(realfont),
145                 shape);
146
147         workarea().getPainter().end();
148 }
149
150
151 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
152         int /*x*/, int y, int asc, int desc)
153 {
154         int const vheight = workarea().workHeight();
155         int newtop = text->first_y;
156
157         if (y + desc - text->first_y >= vheight)
158                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
159         else if (y - asc < text->first_y
160                 && text->first_y > 0) {
161                 newtop = y - vheight / 4;
162         }
163
164         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
165
166         if (newtop != text->first_y) {
167                 draw(text, bv, newtop);
168                 text->first_y = newtop;
169                 return true;
170         }
171
172         return false;
173 }
174
175
176 void LyXScreen::cursorToggle(BufferView * bv) const
177 {
178         if (cursor_visible_)
179                 bv->hideCursor();
180         else
181                 bv->showCursor();
182 }
183
184
185 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
186 {
187         int const vheight = workarea().workHeight();
188         int newtop = top_y;
189
190         Row * row = cursor.row();
191
192         // Is this a hack? Yes, probably... (Lgb)
193         if (!row)
194                 return max(newtop, 0);
195
196         if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
197                 if (row->height() < vheight
198                     && row->height() > vheight / 4) {
199                         newtop = cursor.y()
200                                 + row->height()
201                                 - row->baseline() - vheight;
202                 } else {
203                         // scroll down
204                         newtop = cursor.y()
205                                 - vheight / 2;   /* the scroll region must be so big!! */
206                 }
207
208         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
209                    top_y && top_y > 0) {
210                 if (row->height() < vheight
211                     && row->height() > vheight / 4) {
212                         newtop = cursor.y() - row->baseline();
213                 } else {
214                         // scroll up
215                         newtop = cursor.y() - vheight / 2;
216                         newtop = min(newtop, top_y);
217                 }
218         }
219
220         newtop = max(newtop, 0);
221
222         return newtop;
223 }
224
225
226 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
227 {
228         // Is a change necessary?
229         int const newtop = topCursorVisible(text->cursor, text->first_y);
230         bool const result = (newtop != text->first_y);
231         if (result) {
232                 draw(text, bv, newtop);
233         }
234
235         return result;
236 }
237
238
239 void LyXScreen::update(LyXText * text, BufferView * bv,
240         int yo, int xo)
241 {
242         int const vwidth = workarea().workWidth();
243         int const vheight = workarea().workHeight();
244
245         workarea().getPainter().start();
246
247         switch (text->status()) {
248         case LyXText::NEED_MORE_REFRESH:
249         {
250                 int const y = max(int(text->refresh_y - text->first_y), 0);
251                 drawFromTo(text, bv, y, vheight, yo, xo);
252                 text->refresh_y = 0;
253                 // otherwise this is called ONLY from BufferView_pimpl(update)
254                 // or we should see to set this flag accordingly
255                 if (text != bv->text)
256                         text->status(bv, LyXText::UNCHANGED);
257                 expose(0, y, vwidth, vheight - y);
258         }
259         break;
260         case LyXText::NEED_VERY_LITTLE_REFRESH:
261         {
262                 // ok I will update the current cursor row
263                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
264                            yo, xo);
265                 // this because if we had a major update the refresh_row could
266                 // have been set to 0!
267                 if (text->refresh_row) {
268                         // otherwise this is called ONLY from BufferView_pimpl(update)
269                         // or we should see to set this flag accordingly
270                         if (text != bv->text)
271                                 text->status(bv, LyXText::UNCHANGED);
272                         expose(0, text->refresh_y - text->first_y + yo,
273                                    vwidth, text->refresh_row->height());
274                 }
275         }
276         break;
277         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
278         case LyXText::UNCHANGED:
279                 // Nothing needs done
280                 break;
281         }
282
283         workarea().getPainter().end();
284 }
285
286
287 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
288                                 bool kill_selection,
289                                 int yo, int xo)
290 {
291         // only if there is a selection
292         if (!text->selection.set()) return;
293
294         int const bottom = min(
295                 max(static_cast<int>(text->selection.end.y()
296                                      - text->selection.end.row()->baseline()
297                                      + text->selection.end.row()->height()),
298                     text->first_y),
299                 static_cast<int>(text->first_y + workarea().workHeight()));
300         int const top = min(
301                 max(static_cast<int>(text->selection.start.y() -
302                                      text->selection.start.row()->baseline()),
303                     text->first_y),
304                 static_cast<int>(text->first_y + workarea().workHeight()));
305
306         if (kill_selection)
307                 text->selection.set(false);
308
309         workarea().getPainter().start();
310
311         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
312                    yo, xo);
313         expose(0, top - text->first_y,
314                workarea().workWidth(),
315                bottom - text->first_y - (top - text->first_y));
316
317         workarea().getPainter().end();
318 }
319
320
321 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
322                              int yo, int xo)
323 {
324         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
325             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
326                 return;
327
328         int const top_tmp = text->toggle_cursor.y()
329                 - text->toggle_cursor.row()->baseline();
330         int const bottom_tmp = text->toggle_end_cursor.y()
331                 - text->toggle_end_cursor.row()->baseline()
332                 + text->toggle_end_cursor.row()->height();
333
334         int const offset = yo < 0 ? yo : 0;
335         int const bottom = min(max(bottom_tmp, text->first_y),
336                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
337         int const top = min(max(top_tmp, text->first_y),
338                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
339
340         workarea().getPainter().start();
341
342         drawFromTo(text, bv, top - text->first_y,
343                    bottom - text->first_y, yo,
344                    xo);
345         expose(0, top - text->first_y, workarea().workWidth(),
346                bottom - text->first_y - (top - text->first_y));
347
348         workarea().getPainter().end();
349 }
350
351
352 void LyXScreen::redraw(LyXText * text, BufferView * bv)
353 {
354         workarea().getPainter().start();
355
356         if (!text) {
357                 greyOut();
358                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
359                 workarea().getPainter().end();
360                 return;
361         }
362
363         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
364         expose(0, 0, workarea().workWidth(), workarea().workHeight());
365
366         workarea().getPainter().end();
367
368         if (cursor_visible_) {
369                 cursor_visible_ = false;
370                 bv->showCursor();
371         }
372 }
373
374
375 void LyXScreen::greyOut()
376 {
377         workarea().getPainter().fillRectangle(0, 0,
378                 workarea().workWidth(),
379                 workarea().workHeight(),
380                 LColor::bottomarea);
381
382         // Add a splash screen to the centre of the work area
383         SplashScreen const & splash = SplashScreen::get();
384         grfx::Image const * const splash_image = splash.image();
385         if (splash_image) {
386                 int const w = splash_image->getWidth();
387                 int const h = splash_image->getHeight();
388
389                 int x = (workarea().workWidth() - w) / 2;
390                 int y = (workarea().workHeight() - h) / 2;
391
392                 workarea().getPainter().image(x, y, w, h, *splash_image);
393
394                 string const & splash_text  = splash.text();
395                 LyXFont const & splash_font = splash.font();
396
397                 x += 260;
398                 y += 265;
399
400                 workarea().getPainter().text(x, y, splash_text, splash_font);
401         }
402 }
403
404
405 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
406         int y1, int y2, int yo, int xo,
407         bool internal)
408 {
409         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
410
411         int y_text = text->first_y + y1;
412
413         // get the first needed row
414         Row * row = text->getRowNearY(y_text);
415         // y_text is now the real beginning of the row
416
417         int y = y_text - text->first_y;
418         // y1 is now the real beginning of row on the screen
419
420         while (row != 0 && y < y2) {
421                 LyXText::text_status st = text->status();
422                 text->getVisibleRow(bv, y + yo,
423                                     xo, row, y + text->first_y);
424                 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
425                 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
426                         text->fullRebreak(bv);
427                         st = LyXText::NEED_MORE_REFRESH;
428                         text->setCursor(bv, text->cursor.par(), text->cursor.pos());
429                         text->status(bv, st);
430                         text->getVisibleRow(bv, y + yo,
431                                             xo, row, y + text->first_y);
432                 }
433                 y += row->height();
434                 row = row->next();
435         }
436         force_clear_ = false;
437
438         // maybe we have to clear the screen at the bottom
439         if ((y < y2) && text->isTopLevel()) {
440                 workarea().getPainter().fillRectangle(0, y,
441                         workarea().workWidth(), y2 - y,
442                         LColor::bottomarea);
443         }
444 }
445
446
447 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
448         int y_text, int yo, int xo)
449 {
450         int const y = y_text - text->first_y + yo;
451
452         if (((y + row->height()) > 0) &&
453             ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
454                 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
455         }
456         force_clear_ = false;
457 }