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);
84 GuiApplication::GuiApplication(int & argc, char ** argv)
85 : QApplication(argc, argv), Application(argc, argv)
88 // doubleClickInterval() is 400 ms on X11 witch is just too long.
89 // On Windows and Mac OS X, the operating system's value is used.
90 // On Microsoft Windows, calling this function sets the double
91 // click interval for all applications. So we don't!
92 QApplication::setDoubleClickInterval(300);
96 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
97 NewAEEventHandlerUPP(handleOpenDocuments),
101 // install translation file for Qt built-in dialogs
102 // These are only installed since Qt 3.2.x
103 QTranslator qt_trans;
104 QString language_name = QString("qt_") + QLocale::system().name();
105 language_name.truncate(5);
106 if (qt_trans.load(language_name,
107 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
109 qApp->installTranslator(&qt_trans);
110 // even if the language calls for RtL, don't do that
111 qApp->setLayoutDirection(Qt::LeftToRight);
113 << "Successfully installed Qt translations for locale "
114 << fromqstr(language_name) << std::endl;
117 << "Could not find Qt translations for locale "
118 << fromqstr(language_name) << std::endl;
121 // These translations are meant to break Qt/Mac menu merging
122 // algorithm on some entries. It lists the menu names that
123 // should not be moved to the LyX menu
124 QTranslator aqua_trans(0);
125 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
127 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
129 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
131 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
134 qApp->installTranslator(&aqua_trans);
137 using namespace lyx::graphics;
139 Image::newImage = boost::bind(&QLImage::newImage);
140 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
142 // needs to be done before reading lyxrc
143 lyxrc.dpi = getDPI();
145 LoaderQueue::setPriority(10,100);
151 Clipboard& GuiApplication::clipboard()
157 Selection& GuiApplication::selection()
163 int const GuiApplication::exec()
165 return QApplication::exec();
169 void GuiApplication::exit(int status)
171 QApplication::exit(status);
175 string const GuiApplication::romanFontName()
178 font.setStyleHint(QFont::Serif);
179 font.setFamily("serif");
181 return fromqstr(QFontInfo(font).family());
185 string const GuiApplication::sansFontName()
188 font.setStyleHint(QFont::SansSerif);
189 font.setFamily("sans");
191 return fromqstr(QFontInfo(font).family());
195 string const GuiApplication::typewriterFontName()
198 font.setStyleHint(QFont::TypeWriter);
199 font.setFamily("monospace");
201 return fromqstr(QFontInfo(font).family());
205 void GuiApplication::syncEvents()
207 // This is the ONLY place where processEvents may be called.
208 // During screen update/ redraw, this method is disabled to
209 // prevent keyboard events being handed to the LyX core, where
210 // they could cause re-entrant calls to screen update.
211 processEvents(QEventLoop::ExcludeUserInputEvents);
215 bool GuiApplication::getRgbColor(LColor_color col,
216 lyx::RGBColor & rgbcol)
218 QColor const & qcol = color_cache_.get(col);
219 if (!qcol.isValid()) {
225 rgbcol.r = qcol.red();
226 rgbcol.g = qcol.green();
227 rgbcol.b = qcol.blue();
232 string const GuiApplication::hexName(LColor_color col)
234 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
238 void GuiApplication::updateColor(LColor_color)
240 // FIXME: Bleh, can't we just clear them all at once ?
241 color_cache_.clear();
245 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
247 socket_callbacks_[fd] =
248 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
252 void GuiApplication::unregisterSocketCallback(int fd)
254 socket_callbacks_.erase(fd);
257 ////////////////////////////////////////////////////////////////////////
258 // X11 specific stuff goes here...
260 bool GuiApplication::x11EventFilter(XEvent * xev)
263 case SelectionRequest:
264 lyxerr[Debug::GUI] << "X requested selection." << endl;
266 lyx::docstring const sel = buffer_view_->requestSelection();
272 lyxerr[Debug::GUI] << "Lost selection." << endl;
274 buffer_view_->clearSelection();
282 ////////////////////////////////////////////////////////////////////////
283 // Mac OSX specific stuff goes here...
288 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
290 DescType returnedType;
292 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
293 typeWildCard, &returnedType, nil, 0,
296 case errAEDescNotFound:
299 return errAEEventNotHandled;
307 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
308 AppleEvent* /*reply*/, long /*refCon*/)
311 AEDescList documentList;
312 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
317 err = checkAppleEventForMissingParams(*inEvent);
320 err = AECountItems(&documentList, &documentCount);
321 for (long documentIndex = 1;
322 err == noErr && documentIndex <= documentCount;
324 DescType returnedType;
329 err = AESizeOfNthItem(&documentList, documentIndex,
330 &returnedType, &actualSize);
332 err = AEGetNthPtr(&documentList, documentIndex,
334 &returnedType, (Ptr)&ref,
335 sizeof(FSRef), &actualSize);
337 FSRefMakePath(&ref, (UInt8*)qstr_buf,
339 s_arg=QString::fromUtf8(qstr_buf);
340 // buffer_view_->workAreaDispatch(
341 // FuncRequest(LFUN_FILE_OPEN,
342 // fromqstr(s_arg)));
348 AEDisposeDesc(&documentList);
353 bool GuiApplication::macEventFilter(EventRef event)
355 if (GetEventClass(event) == kEventClassAppleEvent) {
356 EventRecord eventrec;
357 ConvertEventRefToEventRecord(event, &eventrec);
358 AEProcessAppleEvent(&eventrec);
367 } // namespace frontend