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/FileName.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/Package.h"
30 #include "BufferView.h"
33 #include "FuncRequest.h"
38 #include <QApplication>
41 #include <QFileOpenEvent>
43 #include <QLibraryInfo>
46 #include <QTranslator>
50 #include <X11/Xatom.h>
54 #include <boost/bind.hpp>
61 ///////////////////////////////////////////////////////////////
62 // You can find other X11 specific stuff
63 // at the end of this file...
64 ///////////////////////////////////////////////////////////////
71 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
79 using support::FileName;
81 frontend::Application * createApplication(int & argc, char * argv[])
83 return new frontend::GuiApplication(argc, argv);
89 GuiApplication * guiApp;
92 GuiApplication::~GuiApplication()
94 socket_callbacks_.clear();
98 GuiApplication::GuiApplication(int & argc, char ** argv)
99 : QApplication(argc, argv), Application(argc, argv)
101 // Qt bug? setQuitOnLastWindowClosed(true); does not work
102 setQuitOnLastWindowClosed(false);
105 // doubleClickInterval() is 400 ms on X11 which is just too long.
106 // On Windows and Mac OS X, the operating system's value is used.
107 // On Microsoft Windows, calling this function sets the double
108 // click interval for all applications. So we don't!
109 QApplication::setDoubleClickInterval(300);
112 // install translation file for Qt built-in dialogs
113 QString language_name = QString("qt_") + QLocale::system().name();
114 language_name.truncate(5);
115 if (qt_trans_.load(language_name,
116 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
118 qApp->installTranslator(&qt_trans_);
119 // even if the language calls for RtL, don't do that
120 qApp->setLayoutDirection(Qt::LeftToRight);
122 << "Successfully installed Qt translations for locale "
123 << fromqstr(language_name) << std::endl;
126 << "Could not find Qt translations for locale "
127 << fromqstr(language_name) << std::endl;
129 using namespace lyx::graphics;
131 Image::newImage = boost::bind(&QLImage::newImage);
132 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
134 // needs to be done before reading lyxrc
135 lyxrc.dpi = getDPI();
137 LoaderQueue::setPriority(10,100);
143 Clipboard& GuiApplication::clipboard()
149 Selection& GuiApplication::selection()
155 int const GuiApplication::exec()
157 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
158 return QApplication::exec();
162 void GuiApplication::exit(int status)
164 QApplication::exit(status);
168 void GuiApplication::execBatchCommands()
170 LyX::ref().execBatchCommands();
174 string const GuiApplication::romanFontName()
177 font.setKerning(false);
178 font.setStyleHint(QFont::Serif);
179 font.setFamily("serif");
181 return fromqstr(QFontInfo(font).family());
185 string const GuiApplication::sansFontName()
188 font.setKerning(false);
189 font.setStyleHint(QFont::SansSerif);
190 font.setFamily("sans");
192 return fromqstr(QFontInfo(font).family());
196 string const GuiApplication::typewriterFontName()
199 font.setKerning(false);
200 font.setStyleHint(QFont::TypeWriter);
201 font.setFamily("monospace");
203 return fromqstr(QFontInfo(font).family());
207 bool GuiApplication::event(QEvent * e)
210 case QEvent::FileOpen: {
211 // Open a file; this happens only on Mac OS X for now
212 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
214 if (!currentView() || !currentView()->view())
215 // The application is not properly initialized yet.
216 // So we acknowledge the event and delay the file opening
217 // until LyX is ready.
218 // FIXME UNICODE: FileName accept an utf8 encoded string.
219 LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
221 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
222 qstring_to_ucs4(foe->file())));
228 return QApplication::event(e);
233 bool GuiApplication::notify(QObject * receiver, QEvent * event)
237 return_value = QApplication::notify(receiver, event);
239 catch (std::exception const & e) {
240 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
241 LyX::cref().emergencyCleanup();
245 lyxerr << "Caught some really weird exception..." << endl;
246 LyX::cref().emergencyCleanup();
254 void GuiApplication::syncEvents()
256 // This is the ONLY place where processEvents may be called.
257 // During screen update/ redraw, this method is disabled to
258 // prevent keyboard events being handed to the LyX core, where
259 // they could cause re-entrant calls to screen update.
260 processEvents(QEventLoop::ExcludeUserInputEvents);
264 bool GuiApplication::getRgbColor(Color_color col,
267 QColor const & qcol = color_cache_.get(col);
268 if (!qcol.isValid()) {
274 rgbcol.r = qcol.red();
275 rgbcol.g = qcol.green();
276 rgbcol.b = qcol.blue();
281 string const GuiApplication::hexName(Color_color col)
283 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
287 void GuiApplication::updateColor(Color_color)
289 // FIXME: Bleh, can't we just clear them all at once ?
290 color_cache_.clear();
294 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
296 socket_callbacks_[fd] =
297 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
301 void GuiApplication::unregisterSocketCallback(int fd)
303 socket_callbacks_.erase(fd);
306 ////////////////////////////////////////////////////////////////////////
307 // X11 specific stuff goes here...
309 bool GuiApplication::x11EventFilter(XEvent * xev)
315 case SelectionRequest: {
316 if (xev->xselectionrequest.selection != XA_PRIMARY)
318 LYXERR(Debug::GUI) << "X requested selection." << endl;
319 BufferView * bv = currentView()->view();
321 docstring const sel = bv->requestSelection();
327 case SelectionClear: {
328 if (xev->xselectionclear.selection != XA_PRIMARY)
330 LYXERR(Debug::GUI) << "Lost selection." << endl;
331 BufferView * bv = currentView()->view();
333 bv->clearSelection();
342 } // namespace frontend
345 #include "GuiApplication_moc.cpp"