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 "graphics/LoaderQueue.h"
23 #include "support/lstrings.h"
24 #include "support/os.h"
25 #include "support/package.h"
27 #include "BufferView.h"
30 #include "funcrequest.h"
35 #include <QApplication>
39 #include <QLibraryInfo>
41 #include <QTranslator>
48 #include <boost/bind.hpp>
54 extern void initEncodings();
56 ///////////////////////////////////////////////////////////////
57 // You can find other X11 and MACX specific stuff
58 // at the end of this file...
59 ///////////////////////////////////////////////////////////////
66 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
74 frontend::Application * createApplication(int & argc, char * argv[])
76 return new frontend::GuiApplication(argc, argv);
82 GuiApplication * guiApp;
85 GuiApplication::~GuiApplication()
87 socket_callbacks_.clear();
91 GuiApplication::GuiApplication(int & argc, char ** argv)
92 : QApplication(argc, argv), Application(argc, argv)
95 // doubleClickInterval() is 400 ms on X11 which is just too long.
96 // On Windows and Mac OS X, the operating system's value is used.
97 // On Microsoft Windows, calling this function sets the double
98 // click interval for all applications. So we don't!
99 QApplication::setDoubleClickInterval(300);
103 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
104 NewAEEventHandlerUPP(handleOpenDocuments),
108 // install translation file for Qt built-in dialogs
109 QTranslator qt_trans;
110 QString language_name = QString("qt_") + QLocale::system().name();
111 language_name.truncate(5);
112 if (qt_trans.load(language_name,
113 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
115 qApp->installTranslator(&qt_trans);
116 // even if the language calls for RtL, don't do that
117 qApp->setLayoutDirection(Qt::LeftToRight);
119 << "Successfully installed Qt translations for locale "
120 << fromqstr(language_name) << std::endl;
123 << "Could not find Qt translations for locale "
124 << fromqstr(language_name) << std::endl;
127 // These translations are meant to break Qt/Mac menu merging
128 // algorithm on some entries. It lists the menu names that
129 // should not be moved to the LyX menu
130 QTranslator aqua_trans(0);
131 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
133 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
135 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
137 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
140 qApp->installTranslator(&aqua_trans);
143 using namespace lyx::graphics;
145 Image::newImage = boost::bind(&QLImage::newImage);
146 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
148 // needs to be done before reading lyxrc
149 lyxrc.dpi = getDPI();
151 LoaderQueue::setPriority(10,100);
153 setQuitOnLastWindowClosed(false);
154 QObject::connect(this, SIGNAL(lastWindowClosed()),
155 this, SLOT(quitLyX()));
161 void GuiApplication::quitLyX()
163 theLyXFunc().setLyXView(0);
165 // trigger LFUN_LYX_QUIT instead of QApplication::quit() directly
166 // since LFUN_LYX_QUIT may have more cleanup stuff
167 dispatch(FuncRequest(LFUN_LYX_QUIT, "force"));
171 Clipboard& GuiApplication::clipboard()
177 Selection& GuiApplication::selection()
183 int const GuiApplication::exec()
185 return QApplication::exec();
189 void GuiApplication::exit(int status)
191 QApplication::exit(status);
195 string const GuiApplication::romanFontName()
198 font.setKerning(false);
199 font.setStyleHint(QFont::Serif);
200 font.setFamily("serif");
202 return fromqstr(QFontInfo(font).family());
206 string const GuiApplication::sansFontName()
209 font.setKerning(false);
210 font.setStyleHint(QFont::SansSerif);
211 font.setFamily("sans");
213 return fromqstr(QFontInfo(font).family());
217 string const GuiApplication::typewriterFontName()
220 font.setKerning(false);
221 font.setStyleHint(QFont::TypeWriter);
222 font.setFamily("monospace");
224 return fromqstr(QFontInfo(font).family());
228 void GuiApplication::syncEvents()
230 // This is the ONLY place where processEvents may be called.
231 // During screen update/ redraw, this method is disabled to
232 // prevent keyboard events being handed to the LyX core, where
233 // they could cause re-entrant calls to screen update.
234 processEvents(QEventLoop::ExcludeUserInputEvents);
238 bool GuiApplication::getRgbColor(LColor_color col,
239 lyx::RGBColor & rgbcol)
241 QColor const & qcol = color_cache_.get(col);
242 if (!qcol.isValid()) {
248 rgbcol.r = qcol.red();
249 rgbcol.g = qcol.green();
250 rgbcol.b = qcol.blue();
255 string const GuiApplication::hexName(LColor_color col)
257 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
261 void GuiApplication::updateColor(LColor_color)
263 // FIXME: Bleh, can't we just clear them all at once ?
264 color_cache_.clear();
268 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
270 socket_callbacks_[fd] =
271 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
275 void GuiApplication::unregisterSocketCallback(int fd)
277 socket_callbacks_.erase(fd);
280 ////////////////////////////////////////////////////////////////////////
281 // X11 specific stuff goes here...
283 bool GuiApplication::x11EventFilter(XEvent * xev)
286 case SelectionRequest:
287 lyxerr[Debug::GUI] << "X requested selection." << endl;
289 lyx::docstring const sel = buffer_view_->requestSelection();
295 lyxerr[Debug::GUI] << "Lost selection." << endl;
297 buffer_view_->clearSelection();
305 ////////////////////////////////////////////////////////////////////////
306 // Mac OSX specific stuff goes here...
311 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
313 DescType returnedType;
315 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
316 typeWildCard, &returnedType, nil, 0,
319 case errAEDescNotFound:
322 return errAEEventNotHandled;
330 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
331 AppleEvent* /*reply*/, long /*refCon*/)
334 AEDescList documentList;
335 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
340 err = checkAppleEventForMissingParams(*inEvent);
343 err = AECountItems(&documentList, &documentCount);
344 for (long documentIndex = 1;
345 err == noErr && documentIndex <= documentCount;
347 DescType returnedType;
352 err = AESizeOfNthItem(&documentList, documentIndex,
353 &returnedType, &actualSize);
355 err = AEGetNthPtr(&documentList, documentIndex,
357 &returnedType, (Ptr)&ref,
358 sizeof(FSRef), &actualSize);
360 FSRefMakePath(&ref, (UInt8*)qstr_buf,
362 s_arg=QString::fromUtf8(qstr_buf);
363 // buffer_view_->workAreaDispatch(
364 // FuncRequest(LFUN_FILE_OPEN,
365 // fromqstr(s_arg)));
371 AEDisposeDesc(&documentList);
376 bool GuiApplication::macEventFilter(EventRef event)
378 if (GetEventClass(event) == kEventClassAppleEvent) {
379 EventRecord eventrec;
380 ConvertEventRefToEventRecord(event, &eventrec);
381 AEProcessAppleEvent(&eventrec);
390 } // namespace frontend
393 #include "GuiApplication_moc.cpp"