]> git.lyx.org Git - lyx.git/blob - src/frontends/WorkArea.C
debf0005d97e8cbdd532675f62ee18ae70893d1e
[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
44 #include <boost/utility.hpp>
45 #include <boost/bind.hpp>
46 #include <boost/signals/trackable.hpp>
47
48 using lyx::support::libFileSearch;
49
50 using std::endl;
51 using std::min;
52 using std::max;
53 using std::string;
54
55
56 namespace lyx {
57 namespace frontend {
58
59 class SplashScreen : boost::noncopyable, boost::signals::trackable {
60 public:
61         /// This is a singleton class. Get the instance.
62         static SplashScreen const & get();
63         ///
64         lyx::graphics::Image const * image() const { return loader_.image(); }
65         ///
66         string const & text() const { return text_; }
67         ///
68         LyXFont const & font() const { return font_; }
69         ///
70         void connect(lyx::graphics::Loader::slot_type const & slot) const {
71                 loader_.connect(slot);
72         }
73         ///
74         void startLoading() const {
75                 if (loader_.status() == lyx::graphics::WaitingToLoad)
76                         loader_.startLoading();
77         }
78
79 private:
80         /** Make the c-tor private so we can control how many objects
81          *  are instantiated.
82          */
83         SplashScreen();
84
85         ///
86         lyx::graphics::Loader loader_;
87         /// The text to be written on top of the pixmap
88         string const text_;
89         /// in this font...
90         LyXFont font_;
91 };
92
93
94 SplashScreen const & SplashScreen::get()
95 {
96         static SplashScreen singleton;
97         return singleton;
98 }
99
100
101 SplashScreen::SplashScreen()
102         : text_(lyx_version ? lyx_version : "unknown")
103 {
104         if (!lyxrc.show_banner)
105                 return;
106
107         string const file = libFileSearch("images", "banner", "ppm");
108         if (file.empty())
109                 return;
110
111         // The font used to display the version info
112         font_.setFamily(LyXFont::SANS_FAMILY);
113         font_.setSeries(LyXFont::BOLD_SERIES);
114         font_.setSize(LyXFont::SIZE_NORMAL);
115         font_.setColor(LColor::yellow);
116
117         // Load up the graphics file
118         loader_.reset(file);
119 }
120
121 WorkArea::WorkArea(BufferView * buffer_view)
122         :  buffer_view_(buffer_view), greyed_out_(true)
123 {
124         // Start loading the pixmap as soon as possible
125         if (lyxrc.show_banner) {
126                 SplashScreen const & splash = SplashScreen::get();
127                 splash.connect(boost::bind(&WorkArea::checkAndGreyOut, this));
128                 splash.startLoading();
129         }
130 }
131
132
133 void WorkArea::setBufferView(BufferView * buffer_view)
134 {
135         buffer_view_ = buffer_view;
136 }
137
138
139 BufferView & WorkArea::bufferView()
140 {
141         return *buffer_view_;
142 }
143
144
145 BufferView const & WorkArea::bufferView() const
146 {
147         return *buffer_view_;
148 }
149
150
151 void WorkArea::checkAndGreyOut()
152 {
153         if (greyed_out_)
154                 greyOut();
155 }
156
157
158 void WorkArea::redraw()
159 {
160         BOOST_ASSERT(buffer_view_);
161
162         if (!buffer_view_->buffer()) {
163                 greyOut();
164                 return;
165         }
166
167         if (!buffer_view_->needsRedraw())
168                 return;
169
170         greyed_out_ = false;
171         ViewMetricsInfo const & vi = buffer_view_->viewMetricsInfo();
172         getPainter().start();
173         paintText(*buffer_view_, vi);
174         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
175         int const ymin = std::max(vi.y1, 0);
176         int const ymax =
177                 ( vi.p2 < vi.size - 1 ?  vi.y2 : height() );
178         expose(0, ymin, width(), ymax - ymin);
179         getPainter().end();
180         //theCoords.doneUpdating();
181         buffer_view_->needsRedraw(false);
182
183         if (lyxerr.debugging(Debug::DEBUG)) {
184                 lyxerr[Debug::DEBUG]
185                         << "  ymin = " << ymin << "  width() = " << width()
186                         << "  ymax-ymin = " << ymax-ymin << std::endl;
187         }
188 }
189
190
191 void WorkArea::greyOut()
192 {
193         greyed_out_ = true;
194         getPainter().start();
195
196         getPainter().fillRectangle(0, 0,
197                 width(),
198                 height(),
199                 LColor::bottomarea);
200
201         // Add a splash screen to the centre of the work area
202         SplashScreen const & splash = SplashScreen::get();
203         lyx::graphics::Image const * const splash_image = splash.image();
204         if (splash_image) {
205                 int const w = splash_image->getWidth();
206                 int const h = splash_image->getHeight();
207
208                 int x = (width() - w) / 2;
209                 int y = (height() - h) / 2;
210
211                 getPainter().image(x, y, w, h, *splash_image);
212
213                 x += 260;
214                 y += 265;
215
216                 getPainter().text(x, y, splash.text(), splash.font());
217         }
218         expose(0, 0, width(), height());
219         getPainter().end();
220 }
221
222 } // namespace frontend
223 } // namespace lyx