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