2 * \file 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 "SocketCallback.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>
68 frontend::Application * createApplication(int & argc, char * argv[])
70 return new frontend::GuiApplication(argc, argv);
76 ////////////////////////////////////////////////////////////////////////
77 // Mac specific stuff goes here...
79 class MenuTranslator : public QTranslator
82 QString translate(const char * /*context*/,
83 const char * sourceText,
84 const char * /*comment*/ = 0)
86 string const s = sourceText;
87 if (s == N_("About %1") || s == N_("Preferences")
88 || s == N_("Reconfigure") || s == N_("Quit %1"))
96 ///////////////////////////////////////////////////////////////
97 // You can find more platform specific stuff
98 // at the end of this file...
99 ///////////////////////////////////////////////////////////////
102 using support::FileName;
104 GuiApplication * guiApp;
107 GuiApplication::GuiApplication(int & argc, char ** argv)
108 : QApplication(argc, argv), Application(argc, argv), menu_trans_(0)
110 // Qt bug? setQuitOnLastWindowClosed(true); does not work
111 setQuitOnLastWindowClosed(false);
114 // doubleClickInterval() is 400 ms on X11 which is just too long.
115 // On Windows and Mac OS X, the operating system's value is used.
116 // On Microsoft Windows, calling this function sets the double
117 // click interval for all applications. So we don't!
118 QApplication::setDoubleClickInterval(300);
121 // install translation file for Qt built-in dialogs
122 QString language_name = QString("qt_") + QLocale::system().name();
124 // language_name can be short (e.g. qt_zh) or long (e.g. qt_zh_CN).
125 // Short-named translator can be loaded from a long name, but not the
126 // opposite. Therefore, long name should be used without truncation.
127 // c.f. http://doc.trolltech.com/4.1/qtranslator.html#load
128 if (qt_trans_.load(language_name,
129 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
131 installTranslator(&qt_trans_);
132 // even if the language calls for RtL, don't do that
133 setLayoutDirection(Qt::LeftToRight);
135 << "Successfully installed Qt translations for locale "
136 << fromqstr(language_name) << std::endl;
139 << "Could not find Qt translations for locale "
140 << fromqstr(language_name) << std::endl;
143 // This allows to translate the strings that appear in the LyX menu.
147 using namespace lyx::graphics;
149 Image::newImage = boost::bind(&GuiImage::newImage);
150 Image::loadableFormats = boost::bind(&GuiImage::loadableFormats);
152 // needs to be done before reading lyxrc
154 lyxrc.dpi = (w.logicalDpiX() + w.logicalDpiY()) / 2;
156 LoaderQueue::setPriority(10,100);
160 // Set the cache to 5120 kilobytes which corresponds to screen size of
161 // 1280 by 1024 pixels with a color depth of 32 bits.
162 QPixmapCache::setCacheLimit(5120);
166 GuiApplication::~GuiApplication()
169 socket_callbacks_.clear();
173 Clipboard & GuiApplication::clipboard()
179 Selection & GuiApplication::selection()
185 int GuiApplication::exec()
187 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
188 return QApplication::exec();
192 void GuiApplication::exit(int status)
194 QApplication::exit(status);
198 void GuiApplication::execBatchCommands()
200 LyX::ref().execBatchCommands();
204 string const GuiApplication::romanFontName()
207 font.setKerning(false);
208 font.setStyleHint(QFont::Serif);
209 font.setFamily("serif");
211 return fromqstr(QFontInfo(font).family());
215 string const GuiApplication::sansFontName()
218 font.setKerning(false);
219 font.setStyleHint(QFont::SansSerif);
220 font.setFamily("sans");
222 return fromqstr(QFontInfo(font).family());
226 string const GuiApplication::typewriterFontName()
229 font.setKerning(false);
230 font.setStyleHint(QFont::TypeWriter);
231 font.setFamily("monospace");
233 return fromqstr(QFontInfo(font).family());
237 bool GuiApplication::event(QEvent * e)
240 case QEvent::FileOpen: {
241 // Open a file; this happens only on Mac OS X for now
242 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
244 if (!currentView() || !currentView()->view())
245 // The application is not properly initialized yet.
246 // So we acknowledge the event and delay the file opening
247 // until LyX is ready.
248 // FIXME UNICODE: FileName accept an utf8 encoded string.
249 LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
251 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
252 qstring_to_ucs4(foe->file())));
258 return QApplication::event(e);
263 bool GuiApplication::notify(QObject * receiver, QEvent * event)
267 return_value = QApplication::notify(receiver, event);
269 catch (std::exception const & e) {
270 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
271 LyX::cref().emergencyCleanup();
275 lyxerr << "Caught some really weird exception..." << endl;
276 LyX::cref().emergencyCleanup();
284 void GuiApplication::syncEvents()
286 // This is the ONLY place where processEvents may be called.
287 // During screen update/ redraw, this method is disabled to
288 // prevent keyboard events being handed to the LyX core, where
289 // they could cause re-entrant calls to screen update.
290 processEvents(QEventLoop::ExcludeUserInputEvents);
294 bool GuiApplication::getRgbColor(Color_color col,
297 QColor const & qcol = color_cache_.get(col);
298 if (!qcol.isValid()) {
304 rgbcol.r = qcol.red();
305 rgbcol.g = qcol.green();
306 rgbcol.b = qcol.blue();
311 string const GuiApplication::hexName(Color_color col)
313 return support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
317 void GuiApplication::updateColor(Color_color)
319 // FIXME: Bleh, can't we just clear them all at once ?
320 color_cache_.clear();
324 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
326 socket_callbacks_[fd] = new SocketCallback(this, fd, func);
330 void GuiApplication::unregisterSocketCallback(int fd)
332 socket_callbacks_.erase(fd);
336 void GuiApplication::commitData(QSessionManager & sm)
338 /// The implementation is required to avoid an application exit
339 /// when session state save is triggered by session manager.
340 /// The default implementation sends a close event to all
341 /// visible top level widgets when session managment allows
343 /// We are changing that to write all unsaved buffers...
344 if (sm.allowsInteraction() && !theBufferList().quitWriteAll())
349 ////////////////////////////////////////////////////////////////////////
350 // X11 specific stuff goes here...
352 bool GuiApplication::x11EventFilter(XEvent * xev)
358 case SelectionRequest: {
359 if (xev->xselectionrequest.selection != XA_PRIMARY)
361 LYXERR(Debug::GUI) << "X requested selection." << endl;
362 BufferView * bv = currentView()->view();
364 docstring const sel = bv->requestSelection();
370 case SelectionClear: {
371 if (xev->xselectionclear.selection != XA_PRIMARY)
373 LYXERR(Debug::GUI) << "Lost selection." << endl;
374 BufferView * bv = currentView()->view();
376 bv->clearSelection();
384 void GuiApplication::addMenuTranslator()
386 menu_trans_ = new MenuTranslator();
387 installTranslator(menu_trans_);
391 } // namespace frontend
394 #include "GuiApplication_moc.cpp"