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>
49 #include <X11/Xatom.h>
53 #include <boost/bind.hpp>
59 extern void initEncodings();
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 frontend::Application * createApplication(int & argc, char * argv[])
81 return new frontend::GuiApplication(argc, argv);
87 GuiApplication * guiApp;
90 GuiApplication::~GuiApplication()
92 socket_callbacks_.clear();
96 GuiApplication::GuiApplication(int & argc, char ** argv)
97 : QApplication(argc, argv), Application(argc, argv)
99 // Qt bug? setQuitOnLastWindowClosed(true); does not work
100 setQuitOnLastWindowClosed(false);
103 // doubleClickInterval() is 400 ms on X11 which is just too long.
104 // On Windows and Mac OS X, the operating system's value is used.
105 // On Microsoft Windows, calling this function sets the double
106 // click interval for all applications. So we don't!
107 QApplication::setDoubleClickInterval(300);
110 // install translation file for Qt built-in dialogs
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;
127 using namespace lyx::graphics;
129 Image::newImage = boost::bind(&QLImage::newImage);
130 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
132 // needs to be done before reading lyxrc
133 lyxrc.dpi = getDPI();
135 LoaderQueue::setPriority(10,100);
140 Clipboard& GuiApplication::clipboard()
146 Selection& GuiApplication::selection()
152 int const GuiApplication::exec()
154 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
155 return QApplication::exec();
159 void GuiApplication::exit(int status)
161 QApplication::exit(status);
165 void GuiApplication::execBatchCommands()
167 LyX::ref().execBatchCommands();
171 string const GuiApplication::romanFontName()
174 font.setKerning(false);
175 font.setStyleHint(QFont::Serif);
176 font.setFamily("serif");
178 return fromqstr(QFontInfo(font).family());
182 string const GuiApplication::sansFontName()
185 font.setKerning(false);
186 font.setStyleHint(QFont::SansSerif);
187 font.setFamily("sans");
189 return fromqstr(QFontInfo(font).family());
193 string const GuiApplication::typewriterFontName()
196 font.setKerning(false);
197 font.setStyleHint(QFont::TypeWriter);
198 font.setFamily("monospace");
200 return fromqstr(QFontInfo(font).family());
204 bool GuiApplication::event(QEvent * e)
207 case QEvent::FileOpen: {
208 // Open a file; this happens only on Mac OS X for now
209 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
210 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
211 fromqstr(foe->file())));
220 void GuiApplication::syncEvents()
222 // This is the ONLY place where processEvents may be called.
223 // During screen update/ redraw, this method is disabled to
224 // prevent keyboard events being handed to the LyX core, where
225 // they could cause re-entrant calls to screen update.
226 processEvents(QEventLoop::ExcludeUserInputEvents);
230 bool GuiApplication::getRgbColor(LColor_color col,
231 lyx::RGBColor & rgbcol)
233 QColor const & qcol = color_cache_.get(col);
234 if (!qcol.isValid()) {
240 rgbcol.r = qcol.red();
241 rgbcol.g = qcol.green();
242 rgbcol.b = qcol.blue();
247 string const GuiApplication::hexName(LColor_color col)
249 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
253 void GuiApplication::updateColor(LColor_color)
255 // FIXME: Bleh, can't we just clear them all at once ?
256 color_cache_.clear();
260 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
262 socket_callbacks_[fd] =
263 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
267 void GuiApplication::unregisterSocketCallback(int fd)
269 socket_callbacks_.erase(fd);
272 ////////////////////////////////////////////////////////////////////////
273 // X11 specific stuff goes here...
275 bool GuiApplication::x11EventFilter(XEvent * xev)
281 case SelectionRequest: {
282 if (xev->xselectionrequest.selection != XA_PRIMARY)
284 lyxerr[Debug::GUI] << "X requested selection." << endl;
285 BufferView * bv = currentView()->view();
287 docstring const sel = bv->requestSelection();
293 case SelectionClear: {
294 if (xev->xselectionclear.selection != XA_PRIMARY)
296 lyxerr[Debug::GUI] << "Lost selection." << endl;
297 BufferView * bv = currentView()->view();
299 bv->clearSelection();
308 } // namespace frontend
311 #include "GuiApplication_moc.cpp"