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();
115 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
116 // Short-named translator can be loaded from a long name, but not the
117 // opposite. Therefore, long name should be used without truncation.
118 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
119 if (qt_trans_.load(language_name,
120 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
122 qApp->installTranslator(&qt_trans_);
123 // even if the language calls for RtL, don't do that
124 qApp->setLayoutDirection(Qt::LeftToRight);
126 << "Successfully installed Qt translations for locale "
127 << fromqstr(language_name) << std::endl;
130 << "Could not find Qt translations for locale "
131 << fromqstr(language_name) << std::endl;
133 using namespace lyx::graphics;
135 Image::newImage = boost::bind(&QLImage::newImage);
136 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
138 // needs to be done before reading lyxrc
139 lyxrc.dpi = getDPI();
141 LoaderQueue::setPriority(10,100);
147 Clipboard& GuiApplication::clipboard()
153 Selection& GuiApplication::selection()
159 int const GuiApplication::exec()
161 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
162 return QApplication::exec();
166 void GuiApplication::exit(int status)
168 QApplication::exit(status);
172 void GuiApplication::execBatchCommands()
174 LyX::ref().execBatchCommands();
178 string const GuiApplication::romanFontName()
181 font.setKerning(false);
182 font.setStyleHint(QFont::Serif);
183 font.setFamily("serif");
185 return fromqstr(QFontInfo(font).family());
189 string const GuiApplication::sansFontName()
192 font.setKerning(false);
193 font.setStyleHint(QFont::SansSerif);
194 font.setFamily("sans");
196 return fromqstr(QFontInfo(font).family());
200 string const GuiApplication::typewriterFontName()
203 font.setKerning(false);
204 font.setStyleHint(QFont::TypeWriter);
205 font.setFamily("monospace");
207 return fromqstr(QFontInfo(font).family());
211 bool GuiApplication::event(QEvent * e)
214 case QEvent::FileOpen: {
215 // Open a file; this happens only on Mac OS X for now
216 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
218 if (!currentView() || !currentView()->view())
219 // The application is not properly initialized yet.
220 // So we acknowledge the event and delay the file opening
221 // until LyX is ready.
222 // FIXME UNICODE: FileName accept an utf8 encoded string.
223 LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
225 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
226 qstring_to_ucs4(foe->file())));
232 return QApplication::event(e);
237 bool GuiApplication::notify(QObject * receiver, QEvent * event)
241 return_value = QApplication::notify(receiver, event);
243 catch (std::exception const & e) {
244 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
245 LyX::cref().emergencyCleanup();
249 lyxerr << "Caught some really weird exception..." << endl;
250 LyX::cref().emergencyCleanup();
258 void GuiApplication::syncEvents()
260 // This is the ONLY place where processEvents may be called.
261 // During screen update/ redraw, this method is disabled to
262 // prevent keyboard events being handed to the LyX core, where
263 // they could cause re-entrant calls to screen update.
264 processEvents(QEventLoop::ExcludeUserInputEvents);
268 bool GuiApplication::getRgbColor(Color_color col,
271 QColor const & qcol = color_cache_.get(col);
272 if (!qcol.isValid()) {
278 rgbcol.r = qcol.red();
279 rgbcol.g = qcol.green();
280 rgbcol.b = qcol.blue();
285 string const GuiApplication::hexName(Color_color col)
287 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
291 void GuiApplication::updateColor(Color_color)
293 // FIXME: Bleh, can't we just clear them all at once ?
294 color_cache_.clear();
298 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
300 socket_callbacks_[fd] =
301 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
305 void GuiApplication::unregisterSocketCallback(int fd)
307 socket_callbacks_.erase(fd);
310 ////////////////////////////////////////////////////////////////////////
311 // X11 specific stuff goes here...
313 bool GuiApplication::x11EventFilter(XEvent * xev)
319 case SelectionRequest: {
320 if (xev->xselectionrequest.selection != XA_PRIMARY)
322 LYXERR(Debug::GUI) << "X requested selection." << endl;
323 BufferView * bv = currentView()->view();
325 docstring const sel = bv->requestSelection();
331 case SelectionClear: {
332 if (xev->xselectionclear.selection != XA_PRIMARY)
334 LYXERR(Debug::GUI) << "Lost selection." << endl;
335 BufferView * bv = currentView()->view();
337 bv->clearSelection();
346 } // namespace frontend
349 #include "GuiApplication_moc.cpp"