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