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 // FIXME: it would be great if we could just do:
79 //return new lyx::frontend::GuiApplication(argc, argv);
81 #if defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN)
82 static lyx::frontend::GuiApplication app(argc, argv);
84 lyx::frontend::GuiApplication app(argc, argv);
93 GuiApplication::GuiApplication(int & argc, char ** argv)
94 : QApplication(argc, argv), Application(argc, argv)
97 // doubleClickInterval() is 400 ms on X11 witch is just too long.
98 // On Windows and Mac OS X, the operating system's value is used.
99 // On Microsoft Windows, calling this function sets the double
100 // click interval for all applications. So we don't!
101 QApplication::setDoubleClickInterval(300);
105 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
106 NewAEEventHandlerUPP(handleOpenDocuments),
110 // install translation file for Qt built-in dialogs
111 // These are only installed since Qt 3.2.x
112 QTranslator qt_trans;
113 QString language_name = QString("qt_") + QLocale::system().name();
114 language_name.truncate(5);
115 if (qt_trans.load(language_name,
116 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
118 qApp->installTranslator(&qt_trans);
119 // even if the language calls for RtL, don't do that
120 qApp->setLayoutDirection(Qt::LeftToRight);
122 << "Successfully installed Qt translations for locale "
123 << fromqstr(language_name) << std::endl;
126 << "Could not find Qt translations for locale "
127 << fromqstr(language_name) << std::endl;
130 // These translations are meant to break Qt/Mac menu merging
131 // algorithm on some entries. It lists the menu names that
132 // should not be moved to the LyX menu
133 QTranslator aqua_trans(0);
134 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
136 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
138 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
140 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
143 qApp->installTranslator(&aqua_trans);
146 using namespace lyx::graphics;
148 Image::newImage = boost::bind(&QLImage::newImage);
149 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
151 // needs to be done before reading lyxrc
152 lyxrc.dpi = getDPI();
154 LoaderQueue::setPriority(10,100);
160 Clipboard& GuiApplication::clipboard()
166 Selection& GuiApplication::selection()
172 int const GuiApplication::exec()
174 return QApplication::exec();
178 void GuiApplication::exit(int status)
180 QApplication::exit(status);
184 string const GuiApplication::romanFontName()
187 font.setStyleHint(QFont::Serif);
188 font.setFamily("serif");
190 return fromqstr(QFontInfo(font).family());
194 string const GuiApplication::sansFontName()
197 font.setStyleHint(QFont::SansSerif);
198 font.setFamily("sans");
200 return fromqstr(QFontInfo(font).family());
204 string const GuiApplication::typewriterFontName()
207 font.setStyleHint(QFont::TypeWriter);
208 font.setFamily("monospace");
210 return fromqstr(QFontInfo(font).family());
214 void GuiApplication::syncEvents()
216 // This is the ONLY place where processEvents may be called.
217 // During screen update/ redraw, this method is disabled to
218 // prevent keyboard events being handed to the LyX core, where
219 // they could cause re-entrant calls to screen update.
220 processEvents(QEventLoop::ExcludeUserInputEvents);
224 bool GuiApplication::getRgbColor(LColor_color col,
225 lyx::RGBColor & rgbcol)
227 QColor const & qcol = color_cache_.get(col);
228 if (!qcol.isValid()) {
234 rgbcol.r = qcol.red();
235 rgbcol.g = qcol.green();
236 rgbcol.b = qcol.blue();
241 string const GuiApplication::hexName(LColor_color col)
243 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
247 void GuiApplication::updateColor(LColor_color)
249 // FIXME: Bleh, can't we just clear them all at once ?
250 color_cache_.clear();
254 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
256 socket_callbacks_[fd] =
257 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
261 void GuiApplication::unregisterSocketCallback(int fd)
263 socket_callbacks_.erase(fd);
266 ////////////////////////////////////////////////////////////////////////
267 // X11 specific stuff goes here...
269 bool GuiApplication::x11EventFilter(XEvent * xev)
272 case SelectionRequest:
273 lyxerr[Debug::GUI] << "X requested selection." << endl;
275 lyx::docstring const sel = buffer_view_->requestSelection();
281 lyxerr[Debug::GUI] << "Lost selection." << endl;
283 buffer_view_->clearSelection();
291 ////////////////////////////////////////////////////////////////////////
292 // Mac OSX specific stuff goes here...
297 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
299 DescType returnedType;
301 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
302 typeWildCard, &returnedType, nil, 0,
305 case errAEDescNotFound:
308 return errAEEventNotHandled;
316 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
317 AppleEvent* /*reply*/, long /*refCon*/)
320 AEDescList documentList;
321 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
326 err = checkAppleEventForMissingParams(*inEvent);
329 err = AECountItems(&documentList, &documentCount);
330 for (long documentIndex = 1;
331 err == noErr && documentIndex <= documentCount;
333 DescType returnedType;
338 err = AESizeOfNthItem(&documentList, documentIndex,
339 &returnedType, &actualSize);
341 err = AEGetNthPtr(&documentList, documentIndex,
343 &returnedType, (Ptr)&ref,
344 sizeof(FSRef), &actualSize);
346 FSRefMakePath(&ref, (UInt8*)qstr_buf,
348 s_arg=QString::fromUtf8(qstr_buf);
349 // buffer_view_->workAreaDispatch(
350 // FuncRequest(LFUN_FILE_OPEN,
351 // fromqstr(s_arg)));
357 AEDisposeDesc(&documentList);
362 bool GuiApplication::macEventFilter(EventRef event)
364 if (GetEventClass(event) == kEventClassAppleEvent) {
365 EventRecord eventrec;
366 ConvertEventRefToEventRecord(event, &eventrec);
367 AEProcessAppleEvent(&eventrec);
376 } // namespace frontend