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)
99 // doubleClickInterval() is 400 ms on X11 which is just too long.
100 // On Windows and Mac OS X, the operating system's value is used.
101 // On Microsoft Windows, calling this function sets the double
102 // click interval for all applications. So we don't!
103 QApplication::setDoubleClickInterval(300);
106 // install translation file for Qt built-in dialogs
107 QTranslator qt_trans;
108 QString language_name = QString("qt_") + QLocale::system().name();
109 language_name.truncate(5);
110 if (qt_trans.load(language_name,
111 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
113 qApp->installTranslator(&qt_trans);
114 // even if the language calls for RtL, don't do that
115 qApp->setLayoutDirection(Qt::LeftToRight);
117 << "Successfully installed Qt translations for locale "
118 << fromqstr(language_name) << std::endl;
121 << "Could not find Qt translations for locale "
122 << fromqstr(language_name) << std::endl;
125 // These translations are meant to break Qt/Mac menu merging
126 // algorithm on some entries. It lists the menu names that
127 // should not be moved to the LyX menu
128 QTranslator aqua_trans(0);
129 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
131 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
133 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
135 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
138 qApp->installTranslator(&aqua_trans);
141 using namespace lyx::graphics;
143 Image::newImage = boost::bind(&QLImage::newImage);
144 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
146 // needs to be done before reading lyxrc
147 lyxrc.dpi = getDPI();
149 LoaderQueue::setPriority(10,100);
154 Clipboard& GuiApplication::clipboard()
160 Selection& GuiApplication::selection()
166 int const GuiApplication::exec()
168 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
169 return QApplication::exec();
173 void GuiApplication::exit(int status)
175 QApplication::exit(status);
179 void GuiApplication::execBatchCommands()
181 LyX::ref().execBatchCommands();
185 string const GuiApplication::romanFontName()
188 font.setKerning(false);
189 font.setStyleHint(QFont::Serif);
190 font.setFamily("serif");
192 return fromqstr(QFontInfo(font).family());
196 string const GuiApplication::sansFontName()
199 font.setKerning(false);
200 font.setStyleHint(QFont::SansSerif);
201 font.setFamily("sans");
203 return fromqstr(QFontInfo(font).family());
207 string const GuiApplication::typewriterFontName()
210 font.setKerning(false);
211 font.setStyleHint(QFont::TypeWriter);
212 font.setFamily("monospace");
214 return fromqstr(QFontInfo(font).family());
218 bool GuiApplication::event(QEvent * e)
221 case QEvent::FileOpen: {
222 // Open a file; this happens only on Mac OS X for now
223 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
224 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
225 fromqstr(foe->file())));
234 void GuiApplication::syncEvents()
236 // This is the ONLY place where processEvents may be called.
237 // During screen update/ redraw, this method is disabled to
238 // prevent keyboard events being handed to the LyX core, where
239 // they could cause re-entrant calls to screen update.
240 processEvents(QEventLoop::ExcludeUserInputEvents);
244 bool GuiApplication::getRgbColor(LColor_color col,
245 lyx::RGBColor & rgbcol)
247 QColor const & qcol = color_cache_.get(col);
248 if (!qcol.isValid()) {
254 rgbcol.r = qcol.red();
255 rgbcol.g = qcol.green();
256 rgbcol.b = qcol.blue();
261 string const GuiApplication::hexName(LColor_color col)
263 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
267 void GuiApplication::updateColor(LColor_color)
269 // FIXME: Bleh, can't we just clear them all at once ?
270 color_cache_.clear();
274 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
276 socket_callbacks_[fd] =
277 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
281 void GuiApplication::unregisterSocketCallback(int fd)
283 socket_callbacks_.erase(fd);
286 ////////////////////////////////////////////////////////////////////////
287 // X11 specific stuff goes here...
289 bool GuiApplication::x11EventFilter(XEvent * xev)
291 BufferView * bv = currentView().view();
294 case SelectionRequest:
295 lyxerr[Debug::GUI] << "X requested selection." << endl;
297 lyx::docstring const sel = bv->requestSelection();
303 lyxerr[Debug::GUI] << "Lost selection." << endl;
305 bv->clearSelection();
313 } // namespace frontend
316 #include "GuiApplication_moc.cpp"