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