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>
52 #include <boost/bind.hpp>
58 extern void initEncodings();
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 and MACX 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 QTranslator qt_trans;
111 QString language_name = QString("qt_") + QLocale::system().name();
112 language_name.truncate(5);
113 if (qt_trans.load(language_name,
114 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
116 qApp->installTranslator(&qt_trans);
117 // even if the language calls for RtL, don't do that
118 qApp->setLayoutDirection(Qt::LeftToRight);
120 << "Successfully installed Qt translations for locale "
121 << fromqstr(language_name) << std::endl;
124 << "Could not find Qt translations for locale "
125 << fromqstr(language_name) << std::endl;
128 // These translations are meant to break Qt/Mac menu merging
129 // algorithm on some entries. It lists the menu names that
130 // should not be moved to the LyX menu
131 QTranslator aqua_trans(0);
132 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
134 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
136 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
138 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
141 qApp->installTranslator(&aqua_trans);
144 using namespace lyx::graphics;
146 Image::newImage = boost::bind(&QLImage::newImage);
147 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
149 // needs to be done before reading lyxrc
150 lyxrc.dpi = getDPI();
152 LoaderQueue::setPriority(10,100);
157 Clipboard& GuiApplication::clipboard()
163 Selection& GuiApplication::selection()
169 int const GuiApplication::exec()
171 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
172 return QApplication::exec();
176 void GuiApplication::exit(int status)
178 QApplication::exit(status);
182 void GuiApplication::execBatchCommands()
184 LyX::ref().execBatchCommands();
188 string const GuiApplication::romanFontName()
191 font.setKerning(false);
192 font.setStyleHint(QFont::Serif);
193 font.setFamily("serif");
195 return fromqstr(QFontInfo(font).family());
199 string const GuiApplication::sansFontName()
202 font.setKerning(false);
203 font.setStyleHint(QFont::SansSerif);
204 font.setFamily("sans");
206 return fromqstr(QFontInfo(font).family());
210 string const GuiApplication::typewriterFontName()
213 font.setKerning(false);
214 font.setStyleHint(QFont::TypeWriter);
215 font.setFamily("monospace");
217 return fromqstr(QFontInfo(font).family());
221 bool GuiApplication::event(QEvent * e)
224 case QEvent::FileOpen: {
225 // Open a file; this happens only on Mac OS X for now
226 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
227 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
228 fromqstr(foe->file())));
237 void GuiApplication::syncEvents()
239 // This is the ONLY place where processEvents may be called.
240 // During screen update/ redraw, this method is disabled to
241 // prevent keyboard events being handed to the LyX core, where
242 // they could cause re-entrant calls to screen update.
243 processEvents(QEventLoop::ExcludeUserInputEvents);
247 bool GuiApplication::getRgbColor(LColor_color col,
248 lyx::RGBColor & rgbcol)
250 QColor const & qcol = color_cache_.get(col);
251 if (!qcol.isValid()) {
257 rgbcol.r = qcol.red();
258 rgbcol.g = qcol.green();
259 rgbcol.b = qcol.blue();
264 string const GuiApplication::hexName(LColor_color col)
266 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
270 void GuiApplication::updateColor(LColor_color)
272 // FIXME: Bleh, can't we just clear them all at once ?
273 color_cache_.clear();
277 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
279 socket_callbacks_[fd] =
280 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
284 void GuiApplication::unregisterSocketCallback(int fd)
286 socket_callbacks_.erase(fd);
289 ////////////////////////////////////////////////////////////////////////
290 // X11 specific stuff goes here...
292 bool GuiApplication::x11EventFilter(XEvent * xev)
297 BufferView * bv = currentView()->view();
300 case SelectionRequest:
301 lyxerr[Debug::GUI] << "X requested selection." << endl;
303 lyx::docstring const sel = bv->requestSelection();
309 lyxerr[Debug::GUI] << "Lost selection." << endl;
311 bv->clearSelection();
319 } // namespace frontend
322 #include "GuiApplication_moc.cpp"