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>
58 ///////////////////////////////////////////////////////////////
59 // You can find other X11 specific stuff
60 // at the end of this file...
61 ///////////////////////////////////////////////////////////////
68 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
76 frontend::Application * createApplication(int & argc, char * argv[])
78 return new frontend::GuiApplication(argc, argv);
84 GuiApplication * guiApp;
87 GuiApplication::~GuiApplication()
89 socket_callbacks_.clear();
93 GuiApplication::GuiApplication(int & argc, char ** argv)
94 : QApplication(argc, argv), Application(argc, argv)
96 // Qt bug? setQuitOnLastWindowClosed(true); does not work
97 setQuitOnLastWindowClosed(false);
100 // doubleClickInterval() is 400 ms on X11 which is just too long.
101 // On Windows and Mac OS X, the operating system's value is used.
102 // On Microsoft Windows, calling this function sets the double
103 // click interval for all applications. So we don't!
104 QApplication::setDoubleClickInterval(300);
107 // install translation file for Qt built-in dialogs
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;
124 using namespace lyx::graphics;
126 Image::newImage = boost::bind(&QLImage::newImage);
127 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
129 // needs to be done before reading lyxrc
130 lyxrc.dpi = getDPI();
132 LoaderQueue::setPriority(10,100);
137 Clipboard& GuiApplication::clipboard()
143 Selection& GuiApplication::selection()
149 int const GuiApplication::exec()
151 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
152 return QApplication::exec();
156 void GuiApplication::exit(int status)
158 QApplication::exit(status);
162 void GuiApplication::execBatchCommands()
164 LyX::ref().execBatchCommands();
168 string const GuiApplication::romanFontName()
171 font.setKerning(false);
172 font.setStyleHint(QFont::Serif);
173 font.setFamily("serif");
175 return fromqstr(QFontInfo(font).family());
179 string const GuiApplication::sansFontName()
182 font.setKerning(false);
183 font.setStyleHint(QFont::SansSerif);
184 font.setFamily("sans");
186 return fromqstr(QFontInfo(font).family());
190 string const GuiApplication::typewriterFontName()
193 font.setKerning(false);
194 font.setStyleHint(QFont::TypeWriter);
195 font.setFamily("monospace");
197 return fromqstr(QFontInfo(font).family());
201 bool GuiApplication::event(QEvent * e)
204 case QEvent::FileOpen: {
205 // Open a file; this happens only on Mac OS X for now
206 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
207 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
208 fromqstr(foe->file())));
212 return QApplication::event(e);
217 void GuiApplication::syncEvents()
219 // This is the ONLY place where processEvents may be called.
220 // During screen update/ redraw, this method is disabled to
221 // prevent keyboard events being handed to the LyX core, where
222 // they could cause re-entrant calls to screen update.
223 processEvents(QEventLoop::ExcludeUserInputEvents);
227 bool GuiApplication::getRgbColor(LColor_color col,
228 lyx::RGBColor & rgbcol)
230 QColor const & qcol = color_cache_.get(col);
231 if (!qcol.isValid()) {
237 rgbcol.r = qcol.red();
238 rgbcol.g = qcol.green();
239 rgbcol.b = qcol.blue();
244 string const GuiApplication::hexName(LColor_color col)
246 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
250 void GuiApplication::updateColor(LColor_color)
252 // FIXME: Bleh, can't we just clear them all at once ?
253 color_cache_.clear();
257 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
259 socket_callbacks_[fd] =
260 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
264 void GuiApplication::unregisterSocketCallback(int fd)
266 socket_callbacks_.erase(fd);
269 ////////////////////////////////////////////////////////////////////////
270 // X11 specific stuff goes here...
272 bool GuiApplication::x11EventFilter(XEvent * xev)
278 case SelectionRequest: {
279 if (xev->xselectionrequest.selection != XA_PRIMARY)
281 lyxerr[Debug::GUI] << "X requested selection." << endl;
282 BufferView * bv = currentView()->view();
284 docstring const sel = bv->requestSelection();
290 case SelectionClear: {
291 if (xev->xselectionclear.selection != XA_PRIMARY)
293 lyxerr[Debug::GUI] << "Lost selection." << endl;
294 BufferView * bv = currentView()->view();
296 bv->clearSelection();
305 } // namespace frontend
308 #include "GuiApplication_moc.cpp"