]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
"Inter-word Space"
[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 #include "rowpainter.h"
28 #include "insets/updatableinset.h"
29 #include "mathed/formulabase.h"
30
31 // Splash screen-specific stuff
32 #include "lyxfont.h"
33 #include "version.h"
34
35 #include "graphics/GraphicsLoader.h"
36 #include "graphics/GraphicsImage.h"
37
38 #include "support/filetools.h" // LibFileSearch
39
40 #include <boost/utility.hpp>
41 #include <boost/bind.hpp>
42 #include <boost/signals/trackable.hpp>
43
44 using std::min;
45 using std::max;
46 using std::endl;
47
48 namespace {
49
50 class SplashScreen : boost::noncopyable, boost::signals::trackable {
51 public:
52         /// This is a singleton class. Get the instance.
53         static SplashScreen const & get();
54         ///
55         grfx::Image const * image() const { return loader_.image(); }
56         ///
57         string const & text() const { return text_; }
58         ///
59         LyXFont const & font() const { return font_; }
60         ///
61         void connect(grfx::Loader::slot_type const & slot) const {
62                 loader_.connect(slot);
63         }
64         ///
65         void startLoading() const {
66                 if (loader_.status() == grfx::WaitingToLoad)
67                         loader_.startLoading();
68         }
69
70 private:
71         /** Make the c-tor private so we can control how many objects
72          *  are instantiated.
73          */
74         SplashScreen();
75
76         ///
77         grfx::Loader loader_;
78         /// The text to be written on top of the pixmap
79         string const text_;
80         /// in this font...
81         LyXFont font_;
82 };
83
84
85 SplashScreen const & SplashScreen::get()
86 {
87         static SplashScreen singleton;
88         return singleton;
89 }
90
91
92 SplashScreen::SplashScreen()
93         : text_(lyx_version ? lyx_version : "unknown")
94 {
95         if (!lyxrc.show_banner)
96                 return;
97
98         string const file = LibFileSearch("images", "banner", "ppm");
99         if (file.empty())
100                 return;
101
102         // The font used to display the version info
103         font_.setFamily(LyXFont::SANS_FAMILY);
104         font_.setSeries(LyXFont::BOLD_SERIES);
105         font_.setSize(LyXFont::SIZE_NORMAL);
106         font_.setColor(LColor::yellow);
107
108         // Load up the graphics file
109         loader_.reset(file);
110 }
111
112 } // namespace anon
113
114
115 LyXScreen::LyXScreen()
116         : cursor_visible_(false), greyed_out_(true)
117 {
118         // Start loading the pixmap as soon as possible
119         if (lyxrc.show_banner) {
120                 SplashScreen const & splash = SplashScreen::get();
121                 splash.connect(boost::bind(&LyXScreen::greyOut, this));
122                 splash.startLoading();
123         }
124 }
125
126
127 LyXScreen::~LyXScreen()
128 {
129 }
130
131
132 void LyXScreen::showCursor(BufferView & bv)
133 {
134         if (cursor_visible_)
135                 return;
136
137         if (!bv.available())
138                 return;
139
140         Cursor_Shape shape = BAR_SHAPE;
141
142         LyXText const & text = *bv.getLyXText();
143         LyXFont const & realfont(text.real_current_font);
144         BufferParams const & bp(bv.buffer()->params);
145         bool const samelang = realfont.language() == bp.language;
146         bool const isrtl = realfont.isVisibleRightToLeft();
147
148         if (!samelang || isrtl != bp.language->RightToLeft()) {
149                 shape = L_SHAPE;
150                 if (isrtl)
151                         shape = REVERSED_L_SHAPE;
152         }
153
154         int ascent = font_metrics::maxAscent(realfont);
155         int descent = font_metrics::maxDescent(realfont);
156         int h = ascent + descent;
157         int x = 0;
158         int y = 0;
159         int const top_y = bv.text->top_y();
160
161         if (bv.theLockingInset()) {
162                 // Would be nice to clean this up to make some understandable sense...
163                 UpdatableInset * inset = bv.theLockingInset();
164                 inset->getCursor(bv, x, y);
165
166                 // Non-obvious. The reason we have to have these
167                 // extra checks is that the ->getCursor() calls rely
168                 // on the inset's own knowledge of its screen position.
169                 // If we scroll up or down in a big enough increment, the
170                 // inset->draw() is not called: this doesn't update
171                 // inset.top_baseline, so getCursor() returns an old value.
172                 // Ugly as you like.
173                 int bx, by;
174                 inset->getCursorPos(&bv, bx, by);
175                 by += inset->insetInInsetY() + bv.text->cursor.iy();
176                 if (by < top_y)
177                         return;
178                 if (by > top_y + workarea().workHeight())
179                         return;
180         } else {
181                 x = bv.text->cursor.x();
182                 y = bv.text->cursor.y();
183                 y -= top_y;
184         }
185
186         y -= ascent;
187
188         // if it doesn't fit entirely on the screen, don't try to show it
189         if (y < 0 || y + h > workarea().workHeight())
190                 return;
191
192         showCursor(x, y, h, shape);
193
194         cursor_visible_ = true;
195 }
196
197
198 void LyXScreen::hideCursor()
199 {
200         if (!cursor_visible_)
201                 return;
202
203         removeCursor();
204         cursor_visible_ = false;
205 }
206
207
208 void LyXScreen::toggleCursor(BufferView & bv)
209 {
210         if (cursor_visible_)
211                 hideCursor();
212         else
213                 showCursor(bv);
214 }
215
216
217 bool LyXScreen::fitManualCursor(BufferView * bv, LyXText * text,
218         int /*x*/, int y, int asc, int desc)
219 {
220         int const vheight = workarea().workHeight();
221         int newtop = text->top_y();
222
223         if (y + desc - text->top_y() >= vheight)
224                 newtop = y - 3 * vheight / 4;  // the scroll region must be so big!!
225         else if (y - asc < text->top_y()
226                 && text->top_y() > 0) {
227                 newtop = y - vheight / 4;
228         }
229
230         newtop = max(newtop, 0); // can newtop ever be < 0? (Lgb)
231
232         if (newtop != text->top_y()) {
233                 draw(text, bv, newtop);
234                 text->top_y(newtop);
235                 return true;
236         }
237
238         return false;
239 }
240
241
242 unsigned int LyXScreen::topCursorVisible(LyXCursor const & cursor, int top_y)
243 {
244         int const vheight = workarea().workHeight();
245         int newtop = top_y;
246
247         RowList::iterator row = cursor.row();
248
249 #warning SUPER HACK DISABLED (Lgb)
250 #if 0
251         // Is this a hack? Yes, probably... (Lgb)
252         if (!row)
253                 return max(newtop, 0);
254 #endif
255         if (cursor.y() - row->baseline() + row->height() - top_y >= vheight) {
256                 if (row->height() < vheight
257                     && row->height() > vheight / 4) {
258                         newtop = cursor.y()
259                                 + row->height()
260                                 - row->baseline() - vheight;
261                 } else {
262                         // scroll down
263                         newtop = cursor.y()
264                                 - vheight / 2;   /* the scroll region must be so big!! */
265                 }
266
267         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
268                    top_y && top_y > 0) {
269                 if (row->height() < vheight
270                     && row->height() > vheight / 4) {
271                         newtop = cursor.y() - row->baseline();
272                 } else {
273                         // scroll up
274                         newtop = cursor.y() - vheight / 2;
275                         newtop = min(newtop, top_y);
276                 }
277         }
278
279         newtop = max(newtop, 0);
280
281         return newtop;
282 }
283
284
285 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
286 {
287         // Is a change necessary?
288         int const newtop = topCursorVisible(text->cursor, text->top_y());
289         bool const result = (newtop != text->top_y());
290         if (result) {
291                 draw(text, bv, newtop);
292         }
293
294         return result;
295 }
296
297
298 void LyXScreen::update(BufferView & bv, int yo, int xo)
299 {
300         int const vwidth = workarea().workWidth();
301         int const vheight = workarea().workHeight();
302         LyXText * text = bv.text;
303
304         workarea().getPainter().start();
305
306         switch (text->refreshStatus()) {
307         case LyXText::REFRESH_AREA:
308         {
309                 text->updateRowPositions();
310                 int const y = max(int(text->refresh_y - text->top_y()), 0);
311                 drawFromTo(text, &bv, y, vheight, yo, xo);
312                 expose(0, y, vwidth, vheight - y);
313         }
314         break;
315         case LyXText::REFRESH_ROW:
316         {
317                 text->updateRowPositions();
318                 // ok I will update the current cursor row
319                 drawOneRow(text, &bv, text->refresh_row, text->refresh_y,
320                            yo, xo);
321                 // this because if we had a major update the refresh_row could
322                 // have been set to 0!
323                 if (text->refresh_row != text->rows().end()) {
324                         expose(0, text->refresh_y - text->top_y() + yo,
325                                    vwidth, text->refresh_row->height());
326                 }
327         }
328         break;
329         case LyXText::REFRESH_NONE:
330                 // Nothing needs done
331                 break;
332         }
333
334         workarea().getPainter().end();
335 }
336
337
338 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
339                                 bool kill_selection,
340                                 int yo, int xo)
341 {
342         // only if there is a selection
343         if (!text->selection.set()) return;
344
345         int const bottom = min(
346                 max(static_cast<int>(text->selection.end.y()
347                                      - text->selection.end.row()->baseline()
348                                      + text->selection.end.row()->height()),
349                     text->top_y()),
350                 static_cast<int>(text->top_y() + workarea().workHeight()));
351         int const top = min(
352                 max(static_cast<int>(text->selection.start.y() -
353                                      text->selection.start.row()->baseline()),
354                     text->top_y()),
355                 static_cast<int>(text->top_y() + workarea().workHeight()));
356
357         if (kill_selection)
358                 text->selection.set(false);
359
360         workarea().getPainter().start();
361
362         drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(),
363                    yo, xo);
364         expose(0, top - text->top_y(),
365                workarea().workWidth(),
366                bottom - text->top_y() - (top - text->top_y()));
367
368         workarea().getPainter().end();
369 }
370
371
372 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
373                              int yo, int xo)
374 {
375         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
376             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
377                 return;
378
379         int const top_tmp = text->toggle_cursor.y()
380                 - text->toggle_cursor.row()->baseline();
381         int const bottom_tmp = text->toggle_end_cursor.y()
382                 - text->toggle_end_cursor.row()->baseline()
383                 + text->toggle_end_cursor.row()->height();
384
385         int const offset = yo < 0 ? yo : 0;
386         int const bottom = min(max(bottom_tmp, text->top_y()),
387                 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
388         int const top = min(max(top_tmp, text->top_y()),
389                 static_cast<int>(text->top_y() + workarea().workHeight())) - offset;
390
391         workarea().getPainter().start();
392
393         drawFromTo(text, bv, top - text->top_y(), bottom - text->top_y(), yo, xo);
394         expose(0, top - text->top_y(), workarea().workWidth(),
395                bottom - text->top_y() - (top - text->top_y()));
396
397         workarea().getPainter().end();
398 }
399
400
401 void LyXScreen::redraw(LyXText * text, BufferView * bv)
402 {
403         greyed_out_ = !text;
404
405         if (greyed_out_) {
406                 greyOut();
407                 return;
408         }
409
410         workarea().getPainter().start();
411
412         text->updateRowPositions();
413         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0);
414         expose(0, 0, workarea().workWidth(), workarea().workHeight());
415
416         workarea().getPainter().end();
417 }
418
419
420 void LyXScreen::greyOut()
421 {
422         if (!greyed_out_)
423                 return;
424
425         workarea().getPainter().start();
426
427         workarea().getPainter().fillRectangle(0, 0,
428                 workarea().workWidth(),
429                 workarea().workHeight(),
430                 LColor::bottomarea);
431
432         // Add a splash screen to the centre of the work area
433         SplashScreen const & splash = SplashScreen::get();
434         grfx::Image const * const splash_image = splash.image();
435         if (splash_image) {
436                 int const w = splash_image->getWidth();
437                 int const h = splash_image->getHeight();
438
439                 int x = (workarea().workWidth() - w) / 2;
440                 int y = (workarea().workHeight() - h) / 2;
441
442                 workarea().getPainter().image(x, y, w, h, *splash_image);
443
444                 string const & splash_text  = splash.text();
445                 LyXFont const & splash_font = splash.font();
446
447                 x += 260;
448                 y += 265;
449
450                 workarea().getPainter().text(x, y, splash_text, splash_font);
451         }
452         expose(0, 0, workarea().workWidth(), workarea().workHeight());
453         workarea().getPainter().end();
454 }
455
456
457 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
458         int y1, int y2, int yo, int xo)
459 {
460         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << '-' << y2 << endl;
461
462         int const topy = text->top_y();
463         int y_text = topy + y1;
464         RowList::iterator rit = text->getRowNearY(y_text);
465         int y = y_text - topy;
466         // y1 is now the real beginning of row on the screen
467
468         hideCursor();
469
470         RowList::iterator const rend = text->rows().end();
471         while (rit != rend && y < y2) {
472                 RowPainter rp(*bv, *text, rit);
473                 rp.paint(y + yo, xo, y + topy);
474                 y += rit->height();
475                 ++rit;
476         }
477
478         // maybe we have to clear the screen at the bottom
479         if ((y < y2) && !text->isInInset()) {
480                 workarea().getPainter().fillRectangle(0, y,
481                         workarea().workWidth(), y2 - y,
482                         LColor::bottomarea);
483         }
484 }
485
486
487 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv,
488                            RowList::iterator row,
489         int y_text, int yo, int xo)
490 {
491         int const y = y_text - text->top_y() + yo;
492
493         if (y + row->height() <= 0)
494                 return;
495
496         if (y - row->height() > workarea().workHeight())
497                 return;
498
499         hideCursor();
500
501         RowPainter rp(*bv, *text, row);
502         rp.paint(y, xo, y + text->top_y());
503 }