2 * \file qt4/GuiApplication.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * \author Abdelrazak Younes
10 * Full author contact details are available in file CREDITS.
15 #include "GuiApplication.h"
17 #include "qt_helpers.h"
19 #include "socket_callback.h"
21 #include "frontends/LyXView.h"
23 #include "graphics/LoaderQueue.h"
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/Package.h"
29 #include "BufferView.h"
32 #include "FuncRequest.h"
37 #include <QApplication>
40 #include <QFileOpenEvent>
42 #include <QLibraryInfo>
45 #include <QTranslator>
49 #include <X11/Xatom.h>
53 #include <boost/bind.hpp>
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
70 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
78 frontend::Application * createApplication(int & argc, char * argv[])
80 return new frontend::GuiApplication(argc, argv);
86 GuiApplication * guiApp;
89 GuiApplication::~GuiApplication()
91 socket_callbacks_.clear();
95 GuiApplication::GuiApplication(int & argc, char ** argv)
96 : QApplication(argc, argv), Application(argc, argv)
98 // Qt bug? setQuitOnLastWindowClosed(true); does not work
99 setQuitOnLastWindowClosed(false);
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);
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)))
115 qApp->installTranslator(&qt_trans_);
116 // even if the language calls for RtL, don't do that
117 qApp->setLayoutDirection(Qt::LeftToRight);
119 << "Successfully installed Qt translations for locale "
120 << fromqstr(language_name) << std::endl;
123 << "Could not find Qt translations for locale "
124 << fromqstr(language_name) << std::endl;
126 using namespace lyx::graphics;
128 Image::newImage = boost::bind(&QLImage::newImage);
129 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
131 // needs to be done before reading lyxrc
132 lyxrc.dpi = getDPI();
134 LoaderQueue::setPriority(10,100);
140 Clipboard& GuiApplication::clipboard()
146 Selection& GuiApplication::selection()
152 int const GuiApplication::exec()
154 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
155 return QApplication::exec();
159 void GuiApplication::exit(int status)
161 QApplication::exit(status);
165 void GuiApplication::execBatchCommands()
167 LyX::ref().execBatchCommands();
171 string const GuiApplication::romanFontName()
174 font.setKerning(false);
175 font.setStyleHint(QFont::Serif);
176 font.setFamily("serif");
178 return fromqstr(QFontInfo(font).family());
182 string const GuiApplication::sansFontName()
185 font.setKerning(false);
186 font.setStyleHint(QFont::SansSerif);
187 font.setFamily("sans");
189 return fromqstr(QFontInfo(font).family());
193 string const GuiApplication::typewriterFontName()
196 font.setKerning(false);
197 font.setStyleHint(QFont::TypeWriter);
198 font.setFamily("monospace");
200 return fromqstr(QFontInfo(font).family());
204 bool GuiApplication::event(QEvent * e)
207 case QEvent::FileOpen: {
208 // Open a file; this happens only on Mac OS X for now
209 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
210 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
211 fromqstr(foe->file())));
215 return QApplication::event(e);
220 bool GuiApplication::notify(QObject * receiver, QEvent * event)
224 return_value = QApplication::notify(receiver, event);
226 catch (std::exception const & e) {
227 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
228 LyX::cref().emergencyCleanup();
232 lyxerr << "Caught some really weird exception..." << endl;
233 LyX::cref().emergencyCleanup();
241 void GuiApplication::syncEvents()
243 // This is the ONLY place where processEvents may be called.
244 // During screen update/ redraw, this method is disabled to
245 // prevent keyboard events being handed to the LyX core, where
246 // they could cause re-entrant calls to screen update.
247 processEvents(QEventLoop::ExcludeUserInputEvents);
251 bool GuiApplication::getRgbColor(Color_color col,
254 QColor const & qcol = color_cache_.get(col);
255 if (!qcol.isValid()) {
261 rgbcol.r = qcol.red();
262 rgbcol.g = qcol.green();
263 rgbcol.b = qcol.blue();
268 string const GuiApplication::hexName(Color_color col)
270 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
274 void GuiApplication::updateColor(Color_color)
276 // FIXME: Bleh, can't we just clear them all at once ?
277 color_cache_.clear();
281 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
283 socket_callbacks_[fd] =
284 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
288 void GuiApplication::unregisterSocketCallback(int fd)
290 socket_callbacks_.erase(fd);
293 ////////////////////////////////////////////////////////////////////////
294 // X11 specific stuff goes here...
296 bool GuiApplication::x11EventFilter(XEvent * xev)
302 case SelectionRequest: {
303 if (xev->xselectionrequest.selection != XA_PRIMARY)
305 LYXERR(Debug::GUI) << "X requested selection." << endl;
306 BufferView * bv = currentView()->view();
308 docstring const sel = bv->requestSelection();
314 case SelectionClear: {
315 if (xev->xselectionclear.selection != XA_PRIMARY)
317 LYXERR(Debug::GUI) << "Lost selection." << endl;
318 BufferView * bv = currentView()->view();
320 bv->clearSelection();
329 } // namespace frontend
332 #include "GuiApplication_moc.cpp"