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