]> git.lyx.org Git - lyx.git/blob - src/frontends/screen.C
start fixing cursor size in math
[lyx.git] / src / frontends / screen.C
1 /**
2  * \file screen.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  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Splash screen code added by Angus Leeming
11  */
12
13 #include <config.h>
14
15 #include "screen.h"
16 #include "font_metrics.h"
17 #include "lyx_gui.h"
18 #include "Painter.h"
19 #include "WorkArea.h"
20
21 #include "BufferView.h"
22 #include "buffer.h"
23 #include "bufferparams.h"
24 #include "cursor.h"
25 #include "debug.h"
26 #include "language.h"
27 #include "LColor.h"
28 #include "lyxfont.h"
29 #include "lyxrc.h"
30 #include "lyxrow.h"
31 #include "lyxtext.h"
32 #include "metricsinfo.h"
33 #include "paragraph.h"
34 #include "rowpainter.h"
35 #include "version.h"
36
37 #include "insets/updatableinset.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 {
57
58 class SplashScreen : boost::noncopyable, boost::signals::trackable {
59 public:
60         /// This is a singleton class. Get the instance.
61         static SplashScreen const & get();
62         ///
63         lyx::graphics::Image const * image() const { return loader_.image(); }
64         ///
65         string const & text() const { return text_; }
66         ///
67         LyXFont const & font() const { return font_; }
68         ///
69         void connect(lyx::graphics::Loader::slot_type const & slot) const {
70                 loader_.connect(slot);
71         }
72         ///
73         void startLoading() const {
74                 if (loader_.status() == lyx::graphics::WaitingToLoad)
75                         loader_.startLoading();
76         }
77
78 private:
79         /** Make the c-tor private so we can control how many objects
80          *  are instantiated.
81          */
82         SplashScreen();
83
84         ///
85         lyx::graphics::Loader loader_;
86         /// The text to be written on top of the pixmap
87         string const text_;
88         /// in this font...
89         LyXFont font_;
90 };
91
92
93 SplashScreen const & SplashScreen::get()
94 {
95         static SplashScreen singleton;
96         return singleton;
97 }
98
99
100 SplashScreen::SplashScreen()
101         : text_(lyx_version ? lyx_version : "unknown")
102 {
103         if (!lyxrc.show_banner)
104                 return;
105
106         string const file = LibFileSearch("images", "banner", "ppm");
107         if (file.empty())
108                 return;
109
110         // The font used to display the version info
111         font_.setFamily(LyXFont::SANS_FAMILY);
112         font_.setSeries(LyXFont::BOLD_SERIES);
113         font_.setSize(LyXFont::SIZE_NORMAL);
114         font_.setColor(LColor::yellow);
115
116         // Load up the graphics file
117         loader_.reset(file);
118 }
119
120 } // namespace anon
121
122
123 LyXScreen::LyXScreen()
124         : greyed_out_(true), cursor_visible_(false)
125 {
126         // Start loading the pixmap as soon as possible
127         if (lyxrc.show_banner) {
128                 SplashScreen const & splash = SplashScreen::get();
129                 splash.connect(boost::bind(&LyXScreen::checkAndGreyOut, this));
130                 splash.startLoading();
131         }
132 }
133
134
135 LyXScreen::~LyXScreen()
136 {
137 }
138
139
140 void LyXScreen::checkAndGreyOut()
141 {
142         if (greyed_out_)
143                 greyOut();
144 }
145
146
147 void LyXScreen::showCursor(BufferView & bv)
148 {
149         // this is needed to make sure we copy back the right
150         // pixmap on the hide for the Qt frontend
151         lyx_gui::sync_events();
152
153         if (cursor_visible_)
154                 return;
155
156         if (!bv.available())
157                 return;
158
159         Cursor_Shape shape = BAR_SHAPE;
160
161         LyXText const & text = *bv.getLyXText();
162         LyXFont const & realfont = text.real_current_font;
163         BufferParams const & bp = bv.buffer()->params();
164         bool const samelang = realfont.language() == bp.language;
165         bool const isrtl = realfont.isVisibleRightToLeft();
166
167         if (!samelang || isrtl != bp.language->RightToLeft()) {
168                 shape = L_SHAPE;
169                 if (isrtl)
170                         shape = REVERSED_L_SHAPE;
171         }
172
173         // The ERT language hack needs fixing up
174         if (realfont.language() == latex_language)
175                 shape = BAR_SHAPE;
176
177         LyXFont const font = bv.cursor().getFont();
178         int const asc = font_metrics::maxAscent(font);
179         int const des = font_metrics::maxDescent(font);
180         int h = asc + des;
181         int x = 0;
182         int y = 0;
183         bv.cursor().getPos(x, y);
184         y -= asc;
185         //lyxerr << "LyXScreen::showCursor x: " << x << " y: " << y << endl;
186
187         // if it doesn't touch the screen, don't try to show it
188         if (y + h < 0 || y >= workarea().workHeight())
189                 return;
190
191         cursor_visible_ = true;
192         showCursor(x, y, h, shape);
193 }
194
195
196 void LyXScreen::hideCursor()
197 {
198         if (!cursor_visible_)
199                 return;
200
201         cursor_visible_ = false;
202         removeCursor();
203 }
204
205
206 void LyXScreen::toggleCursor(BufferView & bv)
207 {
208         if (cursor_visible_)
209                 hideCursor();
210         else
211                 showCursor(bv);
212 }
213
214
215 void LyXScreen::redraw(BufferView & bv, ViewMetricsInfo const & vi)
216 {
217         greyed_out_ = false;
218         workarea().getPainter().start();
219         hideCursor();
220         paintText(bv, vi);
221         lyxerr[Debug::DEBUG] << "Redraw screen" << endl;
222         expose(0, 0, workarea().workWidth(), workarea().workHeight());
223         workarea().getPainter().end();
224         showCursor(bv);
225 }
226
227
228 void LyXScreen::greyOut()
229 {
230         greyed_out_ = true;
231         workarea().getPainter().start();
232
233         workarea().getPainter().fillRectangle(0, 0,
234                 workarea().workWidth(),
235                 workarea().workHeight(),
236                 LColor::bottomarea);
237
238         // Add a splash screen to the centre of the work area
239         SplashScreen const & splash = SplashScreen::get();
240         lyx::graphics::Image const * const splash_image = splash.image();
241         if (splash_image) {
242                 int const w = splash_image->getWidth();
243                 int const h = splash_image->getHeight();
244
245                 int x = (workarea().workWidth() - w) / 2;
246                 int y = (workarea().workHeight() - h) / 2;
247
248                 workarea().getPainter().image(x, y, w, h, *splash_image);
249
250                 x += 260;
251                 y += 265;
252
253                 workarea().getPainter().text(x, y, splash.text(), splash.font());
254         }
255         expose(0, 0, workarea().workWidth(), workarea().workHeight());
256         workarea().getPainter().end();
257 }