]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
Add vertical spacer.
[lyx.git] / src / frontends / qt4 / GuiApplication.C
1 /**
2  * \file qt4/GuiApplication.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author unknown
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "GuiApplication.h"
16
17 #include "qt_helpers.h"
18 #include "QLImage.h"
19 #include "socket_callback.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "debug.h"
32 #include "funcrequest.h"
33 #include "lyx_main.h"
34 #include "lyxfunc.h"
35 #include "lyxrc.h"
36
37 #include <QApplication>
38 #include <QClipboard>
39 #include <QEventLoop>
40 #include <QFileOpenEvent>
41 #include <QLocale>
42 #include <QLibraryInfo>
43 #include <QTextCodec>
44 #include <QTimer>
45 #include <QTranslator>
46 #include <QWidget>
47
48 #ifdef Q_WS_X11
49 #include <X11/Xatom.h>
50 #include <X11/Xlib.h>
51 #endif
52
53 #include <boost/bind.hpp>
54
55 #include <exception>
56
57 using std::string;
58 using std::endl;
59
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
64
65 namespace {
66
67 int getDPI()
68 {
69         QWidget w;
70         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
71 }
72
73 } // namespace anon
74
75
76 namespace lyx {
77
78 frontend::Application * createApplication(int & argc, char * argv[])
79 {
80         return new frontend::GuiApplication(argc, argv);
81 }
82
83
84 namespace frontend {
85
86 GuiApplication * guiApp;
87
88
89 GuiApplication::~GuiApplication()
90 {
91         socket_callbacks_.clear();
92 }
93
94
95 GuiApplication::GuiApplication(int & argc, char ** argv)
96         : QApplication(argc, argv), Application(argc, argv)
97 {
98         // Qt bug? setQuitOnLastWindowClosed(true); does not work
99         setQuitOnLastWindowClosed(false);
100
101 #ifdef Q_WS_X11
102         // doubleClickInterval() is 400 ms on X11 which is just too long.
103         // On Windows and Mac OS X, the operating system's value is used.
104         // On Microsoft Windows, calling this function sets the double
105         // click interval for all applications. So we don't!
106         QApplication::setDoubleClickInterval(300);
107 #endif
108
109         // install translation file for Qt built-in dialogs
110         QString language_name = QString("qt_") + QLocale::system().name();
111         language_name.truncate(5);
112         if (qt_trans_.load(language_name,
113                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
114         {
115                 qApp->installTranslator(&qt_trans_);
116                 // even if the language calls for RtL, don't do that
117                 qApp->setLayoutDirection(Qt::LeftToRight);
118                 LYXERR(Debug::GUI)
119                         << "Successfully installed Qt translations for locale "
120                         << fromqstr(language_name) << std::endl;
121         } else
122                 LYXERR(Debug::GUI)
123                         << "Could not find  Qt translations for locale "
124                         << fromqstr(language_name) << std::endl;
125
126         using namespace lyx::graphics;
127
128         Image::newImage = boost::bind(&QLImage::newImage);
129         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
130
131         // needs to be done before reading lyxrc
132         lyxrc.dpi = getDPI();
133
134         LoaderQueue::setPriority(10,100);
135
136         guiApp = this;
137 }
138
139 Clipboard& GuiApplication::clipboard()
140 {
141         return clipboard_;
142 }
143
144
145 Selection& GuiApplication::selection()
146 {
147         return selection_;
148 }
149
150
151 int const GuiApplication::exec()
152 {
153         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
154         return QApplication::exec();
155 }
156
157
158 void GuiApplication::exit(int status)
159 {
160         QApplication::exit(status);
161 }
162
163
164 void GuiApplication::execBatchCommands()
165 {
166         LyX::ref().execBatchCommands();
167 }
168
169
170 string const GuiApplication::romanFontName()
171 {
172         QFont font;
173         font.setKerning(false);
174         font.setStyleHint(QFont::Serif);
175         font.setFamily("serif");
176
177         return fromqstr(QFontInfo(font).family());
178 }
179
180
181 string const GuiApplication::sansFontName()
182 {
183         QFont font;
184         font.setKerning(false);
185         font.setStyleHint(QFont::SansSerif);
186         font.setFamily("sans");
187
188         return fromqstr(QFontInfo(font).family());
189 }
190
191
192 string const GuiApplication::typewriterFontName()
193 {
194         QFont font;
195         font.setKerning(false);
196         font.setStyleHint(QFont::TypeWriter);
197         font.setFamily("monospace");
198
199         return fromqstr(QFontInfo(font).family());
200 }
201
202
203 bool GuiApplication::event(QEvent * e)
204 {
205         switch(e->type()) {
206         case QEvent::FileOpen: {
207                 // Open a file; this happens only on Mac OS X for now
208                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
209                 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
210                                           fromqstr(foe->file())));
211                 return true;
212         }
213         default:
214                 return QApplication::event(e);
215         }
216 }
217
218
219 bool GuiApplication::notify(QObject * receiver, QEvent * event)
220 {
221         bool return_value;
222         try {
223                 return_value = QApplication::notify(receiver, event);
224         }
225         catch (std::exception  const & e) {
226                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
227                 LyX::cref().emergencyCleanup();
228                 abort();
229         }
230         catch (...) {
231                 lyxerr << "Caught some really weird exception..." << endl;
232                 LyX::cref().emergencyCleanup();
233                 abort();
234         }
235
236         return return_value;
237 }
238
239
240 void GuiApplication::syncEvents()
241 {
242         // This is the ONLY place where processEvents may be called.
243         // During screen update/ redraw, this method is disabled to
244         // prevent keyboard events being handed to the LyX core, where
245         // they could cause re-entrant calls to screen update.
246         processEvents(QEventLoop::ExcludeUserInputEvents);
247 }
248
249
250 bool GuiApplication::getRgbColor(LColor_color col,
251         lyx::RGBColor & rgbcol)
252 {
253         QColor const & qcol = color_cache_.get(col);
254         if (!qcol.isValid()) {
255                 rgbcol.r = 0;
256                 rgbcol.g = 0;
257                 rgbcol.b = 0;
258                 return false;
259         }
260         rgbcol.r = qcol.red();
261         rgbcol.g = qcol.green();
262         rgbcol.b = qcol.blue();
263         return true;
264 }
265
266
267 string const GuiApplication::hexName(LColor_color col)
268 {
269         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
270 }
271
272
273 void GuiApplication::updateColor(LColor_color)
274 {
275         // FIXME: Bleh, can't we just clear them all at once ?
276         color_cache_.clear();
277 }
278
279
280 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
281 {
282         socket_callbacks_[fd] =
283                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
284 }
285
286
287 void GuiApplication::unregisterSocketCallback(int fd)
288 {
289         socket_callbacks_.erase(fd);
290 }
291
292 ////////////////////////////////////////////////////////////////////////
293 // X11 specific stuff goes here...
294 #ifdef Q_WS_X11
295 bool GuiApplication::x11EventFilter(XEvent * xev)
296 {
297         if (!currentView())
298                 return false;
299
300         switch (xev->type) {
301         case SelectionRequest: {
302                 if (xev->xselectionrequest.selection != XA_PRIMARY)
303                         break;
304                 LYXERR(Debug::GUI) << "X requested selection." << endl;
305                 BufferView * bv = currentView()->view();
306                 if (bv) {
307                         docstring const sel = bv->requestSelection();
308                         if (!sel.empty())
309                                 selection_.put(sel);
310                 }
311                 break;
312         }
313         case SelectionClear: {
314                 if (xev->xselectionclear.selection != XA_PRIMARY)
315                         break;
316                 LYXERR(Debug::GUI) << "Lost selection." << endl;
317                 BufferView * bv = currentView()->view();
318                 if (bv)
319                         bv->clearSelection();
320                 break;
321         }
322         }
323         return false;
324 }
325 #endif
326
327
328 } // namespace frontend
329 } // namespace lyx
330
331 #include "GuiApplication_moc.cpp"