]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
remove LFUN_TOOLBAR_PUSH
[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::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         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() - top_y >= vheight) {
193                 if (row->height() < vheight
194                     && row->height() > vheight / 4) {
195                         newtop = cursor.y()
196                                 + row->height()
197                                 - row->baseline() - vheight;
198                 } else {
199                         // scroll down
200                         newtop = cursor.y()
201                                 - vheight / 2;   /* the scroll region must be so big!! */
202                 }
203
204         } else if (static_cast<int>((cursor.y()) - row->baseline()) <
205                    top_y && top_y > 0) {
206                 if (row->height() < vheight
207                     && row->height() > vheight / 4) {
208                         newtop = cursor.y() - row->baseline();
209                 } else {
210                         // scroll up
211                         newtop = cursor.y() - vheight / 2;
212                         newtop = min(newtop, top_y);
213                 }
214         }
215
216         newtop = max(newtop, 0);
217
218         return newtop;
219 }
220
221
222 bool LyXScreen::fitCursor(LyXText * text, BufferView * bv)
223 {
224         // Is a change necessary?
225         int const newtop = topCursorVisible(text->cursor, text->first_y);
226         bool const result = (newtop != text->first_y);
227         if (result)
228                 draw(text, bv, newtop);
229         return result;
230 }
231
232
233 void LyXScreen::update(LyXText * text, BufferView * bv,
234         int yo, int xo)
235 {
236         int const vwidth = workarea().workWidth();
237         int const vheight = workarea().workHeight();
238
239         workarea().getPainter().start();
240
241         switch (text->status()) {
242         case LyXText::NEED_MORE_REFRESH:
243         {
244                 int const y = max(int(text->refresh_y - text->first_y), 0);
245                 drawFromTo(text, bv, y, vheight, yo, xo);
246                 text->refresh_y = 0;
247                 // otherwise this is called ONLY from BufferView_pimpl(update)
248                 // or we should see to set this flag accordingly
249                 if (text != bv->text)
250                         text->status(bv, LyXText::UNCHANGED);
251                 expose(0, y, vwidth, vheight - y);
252         }
253         break;
254         case LyXText::NEED_VERY_LITTLE_REFRESH:
255         {
256                 // ok I will update the current cursor row
257                 drawOneRow(text, bv, text->refresh_row, text->refresh_y,
258                            yo, xo);
259                 // this because if we had a major update the refresh_row could
260                 // have been set to 0!
261                 if (text->refresh_row) {
262                         // otherwise this is called ONLY from BufferView_pimpl(update)
263                         // or we should see to set this flag accordingly
264                         if (text != bv->text)
265                                 text->status(bv, LyXText::UNCHANGED);
266                         expose(0, text->refresh_y - text->first_y + yo,
267                                    vwidth, text->refresh_row->height());
268                 }
269         }
270         break;
271         case LyXText::CHANGED_IN_DRAW: // just to remove the warning
272         case LyXText::UNCHANGED:
273                 // Nothing needs done
274                 break;
275         }
276
277         workarea().getPainter().end();
278 }
279
280
281 void LyXScreen::toggleSelection(LyXText * text, BufferView * bv,
282                                 bool kill_selection,
283                                 int yo, int xo)
284 {
285         // only if there is a selection
286         if (!text->selection.set()) return;
287
288         int const bottom = min(
289                 max(static_cast<int>(text->selection.end.y()
290                                      - text->selection.end.row()->baseline()
291                                      + text->selection.end.row()->height()),
292                     text->first_y),
293                 static_cast<int>(text->first_y + workarea().workHeight()));
294         int const top = min(
295                 max(static_cast<int>(text->selection.start.y() -
296                                      text->selection.start.row()->baseline()),
297                     text->first_y),
298                 static_cast<int>(text->first_y + workarea().workHeight()));
299
300         if (kill_selection)
301                 text->selection.set(false);
302
303         workarea().getPainter().start();
304
305         drawFromTo(text, bv, top - text->first_y, bottom - text->first_y,
306                    yo, xo);
307         expose(0, top - text->first_y,
308                workarea().workWidth(),
309                bottom - text->first_y - (top - text->first_y));
310
311         workarea().getPainter().end();
312 }
313
314
315 void LyXScreen::toggleToggle(LyXText * text, BufferView * bv,
316                              int yo, int xo)
317 {
318         if (text->toggle_cursor.par() == text->toggle_end_cursor.par()
319             && text->toggle_cursor.pos() == text->toggle_end_cursor.pos())
320                 return;
321
322         int const top_tmp = text->toggle_cursor.y()
323                 - text->toggle_cursor.row()->baseline();
324         int const bottom_tmp = text->toggle_end_cursor.y()
325                 - text->toggle_end_cursor.row()->baseline()
326                 + text->toggle_end_cursor.row()->height();
327
328         int const offset = yo < 0 ? yo : 0;
329         int const bottom = min(max(bottom_tmp, text->first_y),
330                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
331         int const top = min(max(top_tmp, text->first_y),
332                 static_cast<int>(text->first_y + workarea().workHeight())) - offset;
333
334         workarea().getPainter().start();
335
336         drawFromTo(text, bv, top - text->first_y,
337                    bottom - text->first_y, yo,
338                    xo);
339         expose(0, top - text->first_y, workarea().workWidth(),
340                bottom - text->first_y - (top - text->first_y));
341
342         workarea().getPainter().end();
343 }
344
345
346 void LyXScreen::redraw(LyXText * text, BufferView * bv)
347 {
348         workarea().getPainter().start();
349
350         if (!text) {
351                 greyOut();
352                 expose(0, 0, workarea().workWidth(), workarea().workHeight());
353                 workarea().getPainter().end();
354                 return;
355         }
356
357         drawFromTo(text, bv, 0, workarea().workHeight(), 0, 0, text == bv->text);
358         expose(0, 0, workarea().workWidth(), workarea().workHeight());
359
360         workarea().getPainter().end();
361
362         if (cursor_visible_) {
363                 cursor_visible_ = false;
364                 bv->showCursor();
365         }
366 }
367
368
369 void LyXScreen::greyOut()
370 {
371         workarea().getPainter().fillRectangle(0, 0,
372                 workarea().workWidth(),
373                 workarea().workHeight(),
374                 LColor::bottomarea);
375
376         // Add a splash screen to the centre of the work area
377         SplashScreen const & splash = SplashScreen::get();
378         grfx::Image const * const splash_image = splash.image();
379         if (splash_image) {
380                 int const w = splash_image->getWidth();
381                 int const h = splash_image->getHeight();
382
383                 int x = (workarea().workWidth() - w) / 2;
384                 int y = (workarea().workHeight() - h) / 2;
385
386                 workarea().getPainter().image(x, y, w, h, *splash_image);
387
388                 string const & splash_text  = splash.text();
389                 LyXFont const & splash_font = splash.font();
390
391                 x += 260;
392                 y += 265;
393
394                 workarea().getPainter().text(x, y, splash_text, splash_font);
395         }
396 }
397
398
399 void LyXScreen::drawFromTo(LyXText * text, BufferView * bv,
400         int y1, int y2, int yo, int xo,
401         bool internal)
402 {
403         lyxerr[Debug::GUI] << "screen: drawFromTo " << y1 << "-" << y2 << endl;
404
405         int y_text = text->first_y + y1;
406
407         // get the first needed row
408         Row * row = text->getRowNearY(y_text);
409         // y_text is now the real beginning of the row
410
411         int y = y_text - text->first_y;
412         // y1 is now the real beginning of row on the screen
413
414         while (row != 0 && y < y2) {
415                 LyXText::text_status st = text->status();
416                 text->getVisibleRow(bv, y + yo,
417                                     xo, row, y + text->first_y);
418                 internal = internal && (st != LyXText::CHANGED_IN_DRAW);
419                 while (internal && text->status() == LyXText::CHANGED_IN_DRAW) {
420                         text->fullRebreak(bv);
421                         st = LyXText::NEED_MORE_REFRESH;
422                         text->setCursor(bv, text->cursor.par(), text->cursor.pos());
423                         text->status(bv, st);
424                         text->getVisibleRow(bv, y + yo,
425                                             xo, row, y + text->first_y);
426                 }
427                 y += row->height();
428                 row = row->next();
429         }
430         force_clear_ = false;
431
432         // maybe we have to clear the screen at the bottom
433         if ((y < y2) && text->bv_owner) {
434                 workarea().getPainter().fillRectangle(0, y,
435                         workarea().workWidth(), y2 - y,
436                         LColor::bottomarea);
437         }
438 }
439
440
441 void LyXScreen::drawOneRow(LyXText * text, BufferView * bv, Row * row,
442         int y_text, int yo, int xo)
443 {
444         int const y = y_text - text->first_y + yo;
445
446         if (((y + row->height()) > 0) &&
447             ((y - row->height()) <= static_cast<int>(workarea().workHeight()))) {
448                 text->getVisibleRow(bv, y, xo, row, y + text->first_y);
449         }
450         force_clear_ = false;
451 }