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"
33 #include <QApplication>
37 #include <QLibraryInfo>
39 #include <QTranslator>
46 #include <boost/bind.hpp>
48 using lyx::support::subst;
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()));
72 lyx::frontend::GuiApplication * guiApp;
76 lyx::frontend::Application * createApplication(int & argc, char * argv[])
78 return new lyx::frontend::GuiApplication(argc, argv);
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 witch 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 // These are only installed since Qt 3.2.x
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);
158 Clipboard& GuiApplication::clipboard()
164 Selection& GuiApplication::selection()
170 int const GuiApplication::exec()
172 return QApplication::exec();
176 void GuiApplication::exit(int status)
178 QApplication::exit(status);
182 string const GuiApplication::romanFontName()
185 font.setStyleHint(QFont::Serif);
186 font.setFamily("serif");
188 return fromqstr(QFontInfo(font).family());
192 string const GuiApplication::sansFontName()
195 font.setStyleHint(QFont::SansSerif);
196 font.setFamily("sans");
198 return fromqstr(QFontInfo(font).family());
202 string const GuiApplication::typewriterFontName()
205 font.setStyleHint(QFont::TypeWriter);
206 font.setFamily("monospace");
208 return fromqstr(QFontInfo(font).family());
212 void GuiApplication::syncEvents()
214 // This is the ONLY place where processEvents may be called.
215 // During screen update/ redraw, this method is disabled to
216 // prevent keyboard events being handed to the LyX core, where
217 // they could cause re-entrant calls to screen update.
218 processEvents(QEventLoop::ExcludeUserInputEvents);
222 bool GuiApplication::getRgbColor(LColor_color col,
223 lyx::RGBColor & rgbcol)
225 QColor const & qcol = color_cache_.get(col);
226 if (!qcol.isValid()) {
232 rgbcol.r = qcol.red();
233 rgbcol.g = qcol.green();
234 rgbcol.b = qcol.blue();
239 string const GuiApplication::hexName(LColor_color col)
241 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
245 void GuiApplication::updateColor(LColor_color)
247 // FIXME: Bleh, can't we just clear them all at once ?
248 color_cache_.clear();
252 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
254 socket_callbacks_[fd] =
255 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
259 void GuiApplication::unregisterSocketCallback(int fd)
261 socket_callbacks_.erase(fd);
264 ////////////////////////////////////////////////////////////////////////
265 // X11 specific stuff goes here...
267 bool GuiApplication::x11EventFilter(XEvent * xev)
270 case SelectionRequest:
271 lyxerr[Debug::GUI] << "X requested selection." << endl;
273 lyx::docstring const sel = buffer_view_->requestSelection();
279 lyxerr[Debug::GUI] << "Lost selection." << endl;
281 buffer_view_->clearSelection();
289 ////////////////////////////////////////////////////////////////////////
290 // Mac OSX specific stuff goes here...
295 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
297 DescType returnedType;
299 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
300 typeWildCard, &returnedType, nil, 0,
303 case errAEDescNotFound:
306 return errAEEventNotHandled;
314 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
315 AppleEvent* /*reply*/, long /*refCon*/)
318 AEDescList documentList;
319 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
324 err = checkAppleEventForMissingParams(*inEvent);
327 err = AECountItems(&documentList, &documentCount);
328 for (long documentIndex = 1;
329 err == noErr && documentIndex <= documentCount;
331 DescType returnedType;
336 err = AESizeOfNthItem(&documentList, documentIndex,
337 &returnedType, &actualSize);
339 err = AEGetNthPtr(&documentList, documentIndex,
341 &returnedType, (Ptr)&ref,
342 sizeof(FSRef), &actualSize);
344 FSRefMakePath(&ref, (UInt8*)qstr_buf,
346 s_arg=QString::fromUtf8(qstr_buf);
347 // buffer_view_->workAreaDispatch(
348 // FuncRequest(LFUN_FILE_OPEN,
349 // fromqstr(s_arg)));
355 AEDisposeDesc(&documentList);
360 bool GuiApplication::macEventFilter(EventRef event)
362 if (GetEventClass(event) == kEventClassAppleEvent) {
363 EventRecord eventrec;
364 ConvertEventRefToEventRecord(event, &eventrec);
365 AEProcessAppleEvent(&eventrec);
374 } // namespace frontend