]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
GUI API Cleanup step 3: merge with "younes" branch.
[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         if (!buffer_view_->needsRedraw())
193                 return;
194
195         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
196         greyed_out_ = false;
197         getPainter().start();
198         paintText(*buffer_view_, vi, getPainter());
199         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
200         int const ymin = std::max(vi.y1, 0);
201         int const ymax =
202                 ( vi.p2 < vi.size - 1 ?  vi.y2 : height() );
203         expose(0, ymin, width(), ymax - ymin);
204         getPainter().end();
205         buffer_view_->needsRedraw(false);
206
207         lyxerr[Debug::DEBUG]
208         << "  ymin = " << ymin << "  width() = " << width()
209                 << "  ymax-ymin = " << ymax-ymin << std::endl;
210 }
211
212
213 void WorkArea::processKeySym(LyXKeySymPtr key,
214                                                          key_modifier::state state)
215 {
216         hideCursor();
217         buffer_view_->workAreaKeyPress(key, state);
218
219         /* This is perhaps a bit of a hack. When we move
220          * around, or type, it's nice to be able to see
221          * the cursor immediately after the keypress. So
222          * we reset the toggle timeout and force the visibility
223          * of the cursor. Note we cannot do this inside
224          * dispatch() itself, because that's called recursively.
225          */
226 //      if (buffer_view_->available())
227         toggleCursor();
228         redraw();
229 }
230
231
232 void WorkArea::dispatch(FuncRequest const & cmd0)
233 {
234         buffer_view_->workAreaDispatch(cmd0);
235         redraw();
236 }
237
238
239 void WorkArea::resizeBufferView()
240 {
241         buffer_view_->workAreaResize(width(), height());
242         redraw();
243 }
244
245
246 void WorkArea::greyOut()
247 {
248         greyed_out_ = true;
249         getPainter().start();
250
251         getPainter().fillRectangle(0, 0,
252                 width(),
253                 height(),
254                 LColor::bottomarea);
255
256         // Add a splash screen to the centre of the work area
257         SplashScreen const & splash = SplashScreen::get();
258         lyx::graphics::Image const * const splash_image = splash.image();
259         if (splash_image) {
260                 int const w = splash_image->getWidth();
261                 int const h = splash_image->getHeight();
262
263                 int x = (width() - w) / 2;
264                 int y = (height() - h) / 2;
265
266                 getPainter().image(x, y, w, h, *splash_image);
267
268                 x += 260;
269                 y += 265;
270
271                 getPainter().text(x, y, splash.text(), splash.font());
272         }
273         expose(0, 0, width(), height());
274         getPainter().end();
275 }
276
277
278 void WorkArea::showCursor()
279 {
280         if (cursor_visible_)
281                 return;
282
283         if (!buffer_view_->available())
284                 return;
285
286         CursorShape shape = BAR_SHAPE;
287
288         LyXText const & text = *buffer_view_->getLyXText();
289         LyXFont const & realfont = text.real_current_font;
290         BufferParams const & bp = buffer_view_->buffer()->params();
291         bool const samelang = realfont.language() == bp.language;
292         bool const isrtl = realfont.isVisibleRightToLeft();
293
294         if (!samelang || isrtl != bp.language->rightToLeft()) {
295                 shape = L_SHAPE;
296                 if (isrtl)
297                         shape = REVERSED_L_SHAPE;
298         }
299
300         // The ERT language hack needs fixing up
301         if (realfont.language() == latex_language)
302                 shape = BAR_SHAPE;
303
304         LyXFont const font = buffer_view_->cursor().getFont();
305         int const asc = font_metrics::maxAscent(font);
306         int const des = font_metrics::maxDescent(font);
307         int h = asc + des;
308         int x = 0;
309         int y = 0;
310         buffer_view_->cursor().getPos(x, y);
311         y -= asc;
312
313         // if it doesn't touch the screen, don't try to show it
314         if (y + h < 0 || y >= height())
315                 return;
316
317         cursor_visible_ = true;
318         showCursor(x, y, h, shape);
319 }
320
321
322 void WorkArea::hideCursor()
323 {
324         if (!cursor_visible_)
325                 return;
326
327         cursor_visible_ = false;
328         removeCursor();
329 }
330
331
332 void WorkArea::toggleCursor()
333 {
334         if (buffer_view_->buffer()) {
335
336                 if (cursor_visible_)
337                         hideCursor();
338                 else
339                         showCursor();
340
341                 // Use this opportunity to deal with any child processes that
342                 // have finished but are waiting to communicate this fact
343                 // to the rest of LyX.
344                 ForkedcallsController & fcc = ForkedcallsController::get();
345                 fcc.handleCompletedProcesses();
346         }
347
348         cursor_timeout_.restart();
349 }
350
351 } // namespace frontend
352 } // namespace lyx