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