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