]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
Merge the unicode branch into trunk.
[lyx.git] / src / frontends / WorkArea.C
1 /**
2  * \file WorkArea.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  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  *
11  * Splash screen code added by Angus Leeming
12  */
13
14 #include <config.h>
15
16 #include "WorkArea.h"
17
18 #include "font_metrics.h"
19 #include "lyx_gui.h"
20 #include "Painter.h"
21
22 #include "BufferView.h"
23 #include "buffer.h"
24 #include "bufferparams.h"
25 #include "coordcache.h"
26 #include "cursor.h"
27 #include "debug.h"
28 #include "language.h"
29 #include "LColor.h"
30 #include "lyxfont.h"
31 #include "lyxrc.h"
32 #include "lyxrow.h"
33 #include "lyxtext.h"
34 #include "metricsinfo.h"
35 #include "paragraph.h"
36 #include "rowpainter.h"
37 #include "version.h"
38
39 #include "graphics/GraphicsImage.h"
40 #include "graphics/GraphicsLoader.h"
41
42 #include "support/filetools.h" // LibFileSearch
43 #include "support/forkedcontr.h"
44
45 #include <boost/utility.hpp>
46 #include <boost/bind.hpp>
47 #include <boost/current_function.hpp>
48 #include <boost/signals/trackable.hpp>
49
50 using lyx::support::libFileSearch;
51 using lyx::support::ForkedcallsController;
52
53 using std::endl;
54 using std::min;
55 using std::max;
56 using std::string;
57
58
59 namespace lyx {
60 namespace frontend {
61
62 // FIXME: The SplashScreen should be transfered to the
63 // LyXView and create a WorkArea only when a new buffer exists. This
64 // will allow to call WorkArea::redraw() in the constructor.
65 class SplashScreen : boost::noncopyable, boost::signals::trackable {
66 public:
67         /// This is a singleton class. Get the instance.
68         static SplashScreen const & get();
69         ///
70         lyx::graphics::Image const * image() const { return loader_.image(); }
71         ///
72         string const & text() const { return text_; }
73         ///
74         LyXFont const & font() const { return font_; }
75         ///
76         void connect(lyx::graphics::Loader::slot_type const & slot) const {
77                 loader_.connect(slot);
78         }
79         ///
80         void startLoading() const {
81                 if (loader_.status() == lyx::graphics::WaitingToLoad)
82                         loader_.startLoading();
83         }
84
85 private:
86         /** Make the c-tor private so we can control how many objects
87          *  are instantiated.
88          */
89         SplashScreen();
90
91         ///
92         lyx::graphics::Loader loader_;
93         /// The text to be written on top of the pixmap
94         string const text_;
95         /// in this font...
96         LyXFont font_;
97 };
98
99
100 SplashScreen const & SplashScreen::get()
101 {
102         static SplashScreen singleton;
103         return singleton;
104 }
105
106
107 SplashScreen::SplashScreen()
108         : text_(lyx_version ? lyx_version : "unknown")
109 {
110         if (!lyxrc.show_banner)
111                 return;
112
113         string const file = libFileSearch("images", "banner", "ppm");
114         if (file.empty())
115                 return;
116
117         // The font used to display the version info
118         font_.setFamily(LyXFont::SANS_FAMILY);
119         font_.setSeries(LyXFont::BOLD_SERIES);
120         font_.setSize(LyXFont::SIZE_NORMAL);
121         font_.setColor(LColor::yellow);
122
123         // Load up the graphics file
124         loader_.reset(file);
125 }
126
127 namespace {
128
129 // All the below connection objects are needed because of a bug in some
130 // versions of GCC (<=2.96 are on the suspects list.) By having and assigning
131 // to these connections we avoid a segfault upon startup, and also at exit.
132 // (Lgb)
133
134 boost::signals::connection timecon;
135
136 } // anon namespace
137
138 WorkArea::WorkArea(BufferView * buffer_view)
139         :  buffer_view_(buffer_view), greyed_out_(true),
140         cursor_visible_(false), cursor_timeout_(400)
141 {
142         // Start loading the pixmap as soon as possible
143         if (lyxrc.show_banner) {
144                 SplashScreen const & splash = SplashScreen::get();
145                 splash.connect(boost::bind(&WorkArea::checkAndGreyOut, this));
146                 splash.startLoading();
147         }
148
149         // Setup the signals
150         timecon = cursor_timeout_.timeout
151                 .connect(boost::bind(&WorkArea::toggleCursor, this));
152
153         cursor_timeout_.start();
154 }
155
156
157 void WorkArea::setBufferView(BufferView * buffer_view)
158 {
159         buffer_view_ = buffer_view;
160 }
161
162
163 BufferView & WorkArea::bufferView()
164 {
165         return *buffer_view_;
166 }
167
168
169 BufferView const & WorkArea::bufferView() const
170 {
171         return *buffer_view_;
172 }
173
174
175 void WorkArea::checkAndGreyOut()
176 {
177         if (greyed_out_)
178                 greyOut();
179 }
180
181
182 void WorkArea::redraw()
183 {
184         if (!buffer_view_)
185                 return;
186
187         if (!buffer_view_->buffer()) {
188                 greyOut();
189                 return;
190         }
191
192         buffer_view_->updateMetrics(false);
193         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
194         greyed_out_ = false;
195         getPainter().start();
196         paintText(*buffer_view_, vi, getPainter());
197         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
198         int const ymin = std::max(vi.y1, 0);
199         int const ymax =
200                 ( vi.p2 < vi.size - 1 ?  vi.y2 : height() );
201         expose(0, ymin, width(), ymax - ymin);
202         getPainter().end();
203
204         lyxerr[Debug::DEBUG]
205         << "  ymin = " << ymin << "  width() = " << width()
206                 << "  ymax-ymin = " << ymax-ymin << std::endl;
207 }
208
209
210 void WorkArea::processKeySym(LyXKeySymPtr key,
211                                                          key_modifier::state state)
212 {
213         hideCursor();
214         buffer_view_->workAreaKeyPress(key, state);
215
216         /* This is perhaps a bit of a hack. When we move
217          * around, or type, it's nice to be able to see
218          * the cursor immediately after the keypress. So
219          * we reset the toggle timeout and force the visibility
220          * of the cursor. Note we cannot do this inside
221          * dispatch() itself, because that's called recursively.
222          */
223 //      if (buffer_view_->available())
224         toggleCursor();
225
226         // uneeded "redraw()" call commented out for now.
227         // When/if the call to LyXView::redrawWorkArea() in "lyxfunc.C:1610"
228         // is not needed anymore, this line should be uncommented out
229         //redraw();
230 }
231
232
233 void WorkArea::dispatch(FuncRequest const & cmd0)
234 {
235         buffer_view_->workAreaDispatch(cmd0);
236         redraw();
237 }
238
239
240 void WorkArea::resizeBufferView()
241 {
242         buffer_view_->workAreaResize(width(), height());
243         redraw();
244 }
245
246
247 void WorkArea::greyOut()
248 {
249         greyed_out_ = true;
250         getPainter().start();
251
252         getPainter().fillRectangle(0, 0,
253                 width(),
254                 height(),
255                 LColor::bottomarea);
256
257         // Add a splash screen to the centre of the work area
258         SplashScreen const & splash = SplashScreen::get();
259         lyx::graphics::Image const * const splash_image = splash.image();
260         if (splash_image) {
261                 int const w = splash_image->getWidth();
262                 int const h = splash_image->getHeight();
263
264                 int x = (width() - w) / 2;
265                 int y = (height() - h) / 2;
266
267                 getPainter().image(x, y, w, h, *splash_image);
268
269                 x += 260;
270                 y += 265;
271
272                 string stext = splash.text();
273                 docstring dstext(stext.begin(), stext.end());
274                 getPainter().text(x, y, dstext, splash.font());
275         }
276         expose(0, 0, width(), height());
277         getPainter().end();
278 }
279
280
281 void WorkArea::showCursor()
282 {
283         if (cursor_visible_)
284                 return;
285
286         if (!buffer_view_->available())
287                 return;
288
289         CursorShape shape = BAR_SHAPE;
290
291         LyXText const & text = *buffer_view_->getLyXText();
292         LyXFont const & realfont = text.real_current_font;
293         BufferParams const & bp = buffer_view_->buffer()->params();
294         bool const samelang = realfont.language() == bp.language;
295         bool const isrtl = realfont.isVisibleRightToLeft();
296
297         if (!samelang || isrtl != bp.language->rightToLeft()) {
298                 shape = L_SHAPE;
299                 if (isrtl)
300                         shape = REVERSED_L_SHAPE;
301         }
302
303         // The ERT language hack needs fixing up
304         if (realfont.language() == latex_language)
305                 shape = BAR_SHAPE;
306
307         LyXFont const font = buffer_view_->cursor().getFont();
308         int const asc = font_metrics::maxAscent(font);
309         int const des = font_metrics::maxDescent(font);
310         int h = asc + des;
311         int x = 0;
312         int y = 0;
313         buffer_view_->cursor().getPos(x, y);
314         y -= asc;
315
316         // if it doesn't touch the screen, don't try to show it
317         if (y + h < 0 || y >= height())
318                 return;
319
320         cursor_visible_ = true;
321         showCursor(x, y, h, shape);
322 }
323
324
325 void WorkArea::hideCursor()
326 {
327         if (!cursor_visible_)
328                 return;
329
330         cursor_visible_ = false;
331         removeCursor();
332 }
333
334
335 void WorkArea::toggleCursor()
336 {
337         if (buffer_view_->buffer()) {
338
339                 if (cursor_visible_)
340                         hideCursor();
341                 else
342                         showCursor();
343
344                 // Use this opportunity to deal with any child processes that
345                 // have finished but are waiting to communicate this fact
346                 // to the rest of LyX.
347                 ForkedcallsController & fcc = ForkedcallsController::get();
348                 fcc.handleCompletedProcesses();
349         }
350
351         cursor_timeout_.restart();
352 }
353
354 } // namespace frontend
355 } // namespace lyx