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