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.
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);
139 Clipboard& GuiApplication::clipboard()
145 Selection& GuiApplication::selection()
151 int const GuiApplication::exec()
153 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
154 return QApplication::exec();
158 void GuiApplication::exit(int status)
160 QApplication::exit(status);
164 void GuiApplication::execBatchCommands()
166 LyX::ref().execBatchCommands();
170 string const GuiApplication::romanFontName()
173 font.setKerning(false);
174 font.setStyleHint(QFont::Serif);
175 font.setFamily("serif");
177 return fromqstr(QFontInfo(font).family());
181 string const GuiApplication::sansFontName()
184 font.setKerning(false);
185 font.setStyleHint(QFont::SansSerif);
186 font.setFamily("sans");
188 return fromqstr(QFontInfo(font).family());
192 string const GuiApplication::typewriterFontName()
195 font.setKerning(false);
196 font.setStyleHint(QFont::TypeWriter);
197 font.setFamily("monospace");
199 return fromqstr(QFontInfo(font).family());
203 bool GuiApplication::event(QEvent * e)
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())));
214 return QApplication::event(e);
219 bool GuiApplication::notify(QObject * receiver, QEvent * event)
223 return_value = QApplication::notify(receiver, event);
225 catch (std::exception const & e) {
226 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
227 LyX::cref().emergencyCleanup();
231 lyxerr << "Caught some really weird exception..." << endl;
232 LyX::cref().emergencyCleanup();
240 void GuiApplication::syncEvents()
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);
250 bool GuiApplication::getRgbColor(LColor_color col,
251 lyx::RGBColor & rgbcol)
253 QColor const & qcol = color_cache_.get(col);
254 if (!qcol.isValid()) {
260 rgbcol.r = qcol.red();
261 rgbcol.g = qcol.green();
262 rgbcol.b = qcol.blue();
267 string const GuiApplication::hexName(LColor_color col)
269 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
273 void GuiApplication::updateColor(LColor_color)
275 // FIXME: Bleh, can't we just clear them all at once ?
276 color_cache_.clear();
280 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
282 socket_callbacks_[fd] =
283 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
287 void GuiApplication::unregisterSocketCallback(int fd)
289 socket_callbacks_.erase(fd);
292 ////////////////////////////////////////////////////////////////////////
293 // X11 specific stuff goes here...
295 bool GuiApplication::x11EventFilter(XEvent * xev)
301 case SelectionRequest: {
302 if (xev->xselectionrequest.selection != XA_PRIMARY)
304 LYXERR(Debug::GUI) << "X requested selection." << endl;
305 BufferView * bv = currentView()->view();
307 docstring const sel = bv->requestSelection();
313 case SelectionClear: {
314 if (xev->xselectionclear.selection != XA_PRIMARY)
316 LYXERR(Debug::GUI) << "Lost selection." << endl;
317 BufferView * bv = currentView()->view();
319 bv->clearSelection();
328 } // namespace frontend
331 #include "GuiApplication_moc.cpp"