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 "BufferList.h"
31 #include "BufferView.h"
34 #include "FuncRequest.h"
40 #include <QApplication>
43 #include <QFileOpenEvent>
45 #include <QLibraryInfo>
46 #include <QPixmapCache>
47 #include <QSessionManager>
50 #include <QTranslator>
54 #include <X11/Xatom.h>
58 #include <boost/bind.hpp>
65 ///////////////////////////////////////////////////////////////
66 // You can find other X11 specific stuff
67 // at the end of this file...
68 ///////////////////////////////////////////////////////////////
75 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
83 using support::FileName;
85 frontend::Application * createApplication(int & argc, char * argv[])
87 return new frontend::GuiApplication(argc, argv);
93 GuiApplication * guiApp;
96 GuiApplication::~GuiApplication()
98 socket_callbacks_.clear();
102 GuiApplication::GuiApplication(int & argc, char ** argv)
103 : QApplication(argc, argv), Application(argc, argv)
105 // Qt bug? setQuitOnLastWindowClosed(true); does not work
106 setQuitOnLastWindowClosed(false);
109 // doubleClickInterval() is 400 ms on X11 which is just too long.
110 // On Windows and Mac OS X, the operating system's value is used.
111 // On Microsoft Windows, calling this function sets the double
112 // click interval for all applications. So we don't!
113 QApplication::setDoubleClickInterval(300);
116 // install translation file for Qt built-in dialogs
117 QString language_name = QString("qt_") + QLocale::system().name();
119 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
120 // Short-named translator can be loaded from a long name, but not the
121 // opposite. Therefore, long name should be used without truncation.
122 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
123 if (qt_trans_.load(language_name,
124 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
126 installTranslator(&qt_trans_);
127 // even if the language calls for RtL, don't do that
128 setLayoutDirection(Qt::LeftToRight);
130 << "Successfully installed Qt translations for locale "
131 << fromqstr(language_name) << std::endl;
134 << "Could not find Qt translations for locale "
135 << fromqstr(language_name) << std::endl;
138 // This allows to translate the strings that appear in the LyX menu.
142 using namespace lyx::graphics;
144 Image::newImage = boost::bind(&QLImage::newImage);
145 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
147 // needs to be done before reading lyxrc
148 lyxrc.dpi = getDPI();
150 LoaderQueue::setPriority(10,100);
154 // Set the cache to 5120 kilobytes which corresponds to screen size of
155 // 1280 by 1024 pixels with a color depth of 32 bits.
156 QPixmapCache::setCacheLimit(5120);
160 Clipboard& GuiApplication::clipboard()
166 Selection& GuiApplication::selection()
172 int const GuiApplication::exec()
174 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
175 return QApplication::exec();
179 void GuiApplication::exit(int status)
181 QApplication::exit(status);
185 void GuiApplication::execBatchCommands()
187 LyX::ref().execBatchCommands();
191 string const GuiApplication::romanFontName()
194 font.setKerning(false);
195 font.setStyleHint(QFont::Serif);
196 font.setFamily("serif");
198 return fromqstr(QFontInfo(font).family());
202 string const GuiApplication::sansFontName()
205 font.setKerning(false);
206 font.setStyleHint(QFont::SansSerif);
207 font.setFamily("sans");
209 return fromqstr(QFontInfo(font).family());
213 string const GuiApplication::typewriterFontName()
216 font.setKerning(false);
217 font.setStyleHint(QFont::TypeWriter);
218 font.setFamily("monospace");
220 return fromqstr(QFontInfo(font).family());
224 bool GuiApplication::event(QEvent * e)
227 case QEvent::FileOpen: {
228 // Open a file; this happens only on Mac OS X for now
229 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
231 if (!currentView() || !currentView()->view())
232 // The application is not properly initialized yet.
233 // So we acknowledge the event and delay the file opening
234 // until LyX is ready.
235 // FIXME UNICODE: FileName accept an utf8 encoded string.
236 LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
238 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
239 qstring_to_ucs4(foe->file())));
245 return QApplication::event(e);
250 bool GuiApplication::notify(QObject * receiver, QEvent * event)
254 return_value = QApplication::notify(receiver, event);
256 catch (std::exception const & e) {
257 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
258 LyX::cref().emergencyCleanup();
262 lyxerr << "Caught some really weird exception..." << endl;
263 LyX::cref().emergencyCleanup();
271 void GuiApplication::syncEvents()
273 // This is the ONLY place where processEvents may be called.
274 // During screen update/ redraw, this method is disabled to
275 // prevent keyboard events being handed to the LyX core, where
276 // they could cause re-entrant calls to screen update.
277 processEvents(QEventLoop::ExcludeUserInputEvents);
281 bool GuiApplication::getRgbColor(Color_color col,
284 QColor const & qcol = color_cache_.get(col);
285 if (!qcol.isValid()) {
291 rgbcol.r = qcol.red();
292 rgbcol.g = qcol.green();
293 rgbcol.b = qcol.blue();
298 string const GuiApplication::hexName(Color_color col)
300 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
304 void GuiApplication::updateColor(Color_color)
306 // FIXME: Bleh, can't we just clear them all at once ?
307 color_cache_.clear();
311 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
313 socket_callbacks_[fd] =
314 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
318 void GuiApplication::unregisterSocketCallback(int fd)
320 socket_callbacks_.erase(fd);
324 void GuiApplication::commitData(QSessionManager & sm)
326 /// The implementation is required to avoid an application exit
327 /// when session state save is triggered by session manager.
328 /// The default implementation sends a close event to all
329 /// visible top level widgets when session managment allows
331 /// We are changing that to write all unsaved buffers...
332 if (sm.allowsInteraction() && !theBufferList().quitWriteAll())
337 ////////////////////////////////////////////////////////////////////////
338 // X11 specific stuff goes here...
340 bool GuiApplication::x11EventFilter(XEvent * xev)
346 case SelectionRequest: {
347 if (xev->xselectionrequest.selection != XA_PRIMARY)
349 LYXERR(Debug::GUI) << "X requested selection." << endl;
350 BufferView * bv = currentView()->view();
352 docstring const sel = bv->requestSelection();
358 case SelectionClear: {
359 if (xev->xselectionclear.selection != XA_PRIMARY)
361 LYXERR(Debug::GUI) << "Lost selection." << endl;
362 BufferView * bv = currentView()->view();
364 bv->clearSelection();
373 ////////////////////////////////////////////////////////////////////////
374 // Mac specific stuff goes here...
376 class MenuTranslator : public QTranslator {
378 virtual ~MenuTranslator() {};
379 virtual QString translate(const char * context,
380 const char * sourceText,
381 const char * comment = 0) const;
385 QString MenuTranslator::translate(const char * /*context*/,
386 const char * sourceText,
389 string const s = sourceText;
390 if (s == N_("About %1") || s == N_("Preferences")
391 || s == N_("Reconfigure") || s == N_("Quit %1"))
398 void GuiApplication::addMenuTranslator()
400 menu_trans_.reset(new MenuTranslator());
401 installTranslator(menu_trans_.get());
405 } // namespace frontend
408 #include "GuiApplication_moc.cpp"