]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
834b96a5e32ab006c6a3fda4be8e28879806ec56
[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 #include <config.h>
14
15 #include "screen.h"
16 #include "font_metrics.h"
17 #include "lyx_gui.h"
18 #include "Painter.h"
19 #include "WorkArea.h"
20
21 #include "BufferView.h"
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "coordcache.h"
25 #include "cursor.h"
26 #include "debug.h"
27 #include "language.h"
28 #include "LColor.h"
29 #include "lyxfont.h"
30 #include "lyxrc.h"
31 #include "lyxrow.h"
32 #include "lyxtext.h"
33 #include "metricsinfo.h"
34 #include "paragraph.h"
35 #include "rowpainter.h"
36 #include "version.h"
37
38 #include "insets/updatableinset.h"
39
40 #include "graphics/GraphicsImage.h"
41 #include "graphics/GraphicsLoader.h"
42
43 #include "support/filetools.h" // LibFileSearch
44
45 #include <boost/utility.hpp>
46 #include <boost/bind.hpp>
47 #include <boost/signals/trackable.hpp>
48
49 using lyx::support::LibFileSearch;
50
51 using std::endl;
52 using std::min;
53 using std::max;
54 using std::string;
55
56
57 namespace {
58
59 class SplashScreen : boost::noncopyable, boost::signals::trackable {
60 public:
61         /// This is a singleton class. Get the instance.
62         static SplashScreen const & get();
63         ///
64         lyx::graphics::Image const * image() const { return loader_.image(); }
65         ///
66         string const & text() const { return text_; }
67         ///
68         LyXFont const & font() const { return font_; }
69         ///
70         void connect(lyx::graphics::Loader::slot_type const & slot) const {
71                 loader_.connect(slot);
72         }
73         ///
74         void startLoading() const {
75                 if (loader_.status() == lyx::graphics::WaitingToLoad)
76                         loader_.startLoading();
77         }
78
79 private:
80         /** Make the c-tor private so we can control how many objects
81          *  are instantiated.
82          */
83         SplashScreen();
84
85         ///
86         lyx::graphics::Loader loader_;
87         /// The text to be written on top of the pixmap
88         string const text_;
89         /// in this font...
90         LyXFont font_;
91 };
92
93
94 SplashScreen const & SplashScreen::get()
95 {
96         static SplashScreen singleton;
97         return singleton;
98 }
99
100
101 SplashScreen::SplashScreen()
102         : text_(lyx_version ? lyx_version : "unknown")
103 {
104         if (!lyxrc.show_banner)
105                 return;
106
107         string const file = LibFileSearch("images", "banner", "ppm");
108         if (file.empty())
109                 return;
110
111         // The font used to display the version info
112         font_.setFamily(LyXFont::SANS_FAMILY);
113         font_.setSeries(LyXFont::BOLD_SERIES);
114         font_.setSize(LyXFont::SIZE_NORMAL);
115         font_.setColor(LColor::yellow);
116
117         // Load up the graphics file
118         loader_.reset(file);
119 }
120
121 } // namespace anon
122
123
124 LyXScreen::LyXScreen()
125         : greyed_out_(true), cursor_visible_(false)
126 {
127         // Start loading the pixmap as soon as possible
128         if (lyxrc.show_banner) {
129                 SplashScreen const & splash = SplashScreen::get();
130                 splash.connect(boost::bind(&LyXScreen::checkAndGreyOut, this));
131                 splash.startLoading();
132         }
133 }
134
135
136 LyXScreen::~LyXScreen()
137 {
138 }
139
140
141 void LyXScreen::checkAndGreyOut()
142 {
143         if (greyed_out_)
144                 greyOut();
145 }
146
147
148 void LyXScreen::showCursor(BufferView & bv)
149 {
150         // this is needed to make sure we copy back the right
151         // pixmap on the hide for the Qt frontend
152         lyx_gui::sync_events();
153
154         if (cursor_visible_)
155                 return;
156
157         if (!bv.available())
158                 return;
159
160         Cursor_Shape shape = BAR_SHAPE;
161
162         LyXText const & text = *bv.getLyXText();
163         LyXFont const & realfont = text.real_current_font;
164         BufferParams const & bp = bv.buffer()->params();
165         bool const samelang = realfont.language() == bp.language;
166         bool const isrtl = realfont.isVisibleRightToLeft();
167
168         if (!samelang || isrtl != bp.language->RightToLeft()) {
169                 shape = L_SHAPE;
170                 if (isrtl)
171                         shape = REVERSED_L_SHAPE;
172         }
173
174         // The ERT language hack needs fixing up
175         if (realfont.language() == latex_language)
176                 shape = BAR_SHAPE;
177
178         LyXFont const font = bv.cursor().getFont();
179         int const asc = font_metrics::maxAscent(font);
180         int const des = font_metrics::maxDescent(font);
181         int h = asc + des;
182         int x = 0;
183         int y = 0;
184         bv.cursor().getPos(x, y);
185         y -= asc;
186         //lyxerr << "LyXScreen::showCursor x: " << x << " y: " << y << endl;
187
188         // if it doesn't touch the screen, don't try to show it
189         if (y + h < 0 || y >= workarea().workHeight())
190                 return;
191
192         cursor_visible_ = true;
193         showCursor(x, y, h, shape);
194 }
195
196
197 void LyXScreen::hideCursor()
198 {
199         if (!cursor_visible_)
200                 return;
201
202         cursor_visible_ = false;
203         removeCursor();
204 }
205
206
207 void LyXScreen::toggleCursor(BufferView & bv)
208 {
209         if (cursor_visible_)
210                 hideCursor();
211         else
212                 showCursor(bv);
213 }
214
215
216 void LyXScreen::redraw(BufferView & bv, ViewMetricsInfo const & vi)
217 {
218         greyed_out_ = false;
219         workarea().getPainter().start();
220         hideCursor();
221         paintText(bv, vi);
222         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
223         expose(0, 0, workarea().workWidth(), workarea().workHeight());
224         workarea().getPainter().end();
225         theCoords.doneUpdating();
226         showCursor(bv);
227 }
228
229
230 void LyXScreen::greyOut()
231 {
232         greyed_out_ = true;
233         workarea().getPainter().start();
234
235         workarea().getPainter().fillRectangle(0, 0,
236                 workarea().workWidth(),
237                 workarea().workHeight(),
238                 LColor::bottomarea);
239
240         // Add a splash screen to the centre of the work area
241         SplashScreen const & splash = SplashScreen::get();
242         lyx::graphics::Image const * const splash_image = splash.image();
243         if (splash_image) {
244                 int const w = splash_image->getWidth();
245                 int const h = splash_image->getHeight();
246
247                 int x = (workarea().workWidth() - w) / 2;
248                 int y = (workarea().workHeight() - h) / 2;
249
250                 workarea().getPainter().image(x, y, w, h, *splash_image);
251
252                 x += 260;
253                 y += 265;
254
255                 workarea().getPainter().text(x, y, splash.text(), splash.font());
256         }
257         expose(0, 0, workarea().workWidth(), workarea().workHeight());
258         workarea().getPainter().end();
259 }