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