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>
41 #include <QLibraryInfo>
44 #include <QTranslator>
51 #include <boost/bind.hpp>
57 extern void initEncodings();
59 ///////////////////////////////////////////////////////////////
60 // You can find other X11 and MACX specific stuff
61 // at the end of this file...
62 ///////////////////////////////////////////////////////////////
69 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
77 frontend::Application * createApplication(int & argc, char * argv[])
79 return new frontend::GuiApplication(argc, argv);
85 GuiApplication * guiApp;
88 GuiApplication::~GuiApplication()
90 socket_callbacks_.clear();
94 GuiApplication::GuiApplication(int & argc, char ** argv)
95 : QApplication(argc, argv), Application(argc, argv)
98 // doubleClickInterval() is 400 ms on X11 which is just too long.
99 // On Windows and Mac OS X, the operating system's value is used.
100 // On Microsoft Windows, calling this function sets the double
101 // click interval for all applications. So we don't!
102 QApplication::setDoubleClickInterval(300);
106 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
107 NewAEEventHandlerUPP(handleOpenDocuments),
111 // install translation file for Qt built-in dialogs
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);
159 Clipboard& GuiApplication::clipboard()
165 Selection& GuiApplication::selection()
171 int const GuiApplication::exec()
173 QTimer::singleShot(1, this, SLOT(execBatchCommands()));
174 return QApplication::exec();
178 void GuiApplication::exit(int status)
180 QApplication::exit(status);
184 void GuiApplication::execBatchCommands()
186 LyX::ref().execBatchCommands();
190 string const GuiApplication::romanFontName()
193 font.setKerning(false);
194 font.setStyleHint(QFont::Serif);
195 font.setFamily("serif");
197 return fromqstr(QFontInfo(font).family());
201 string const GuiApplication::sansFontName()
204 font.setKerning(false);
205 font.setStyleHint(QFont::SansSerif);
206 font.setFamily("sans");
208 return fromqstr(QFontInfo(font).family());
212 string const GuiApplication::typewriterFontName()
215 font.setKerning(false);
216 font.setStyleHint(QFont::TypeWriter);
217 font.setFamily("monospace");
219 return fromqstr(QFontInfo(font).family());
223 void GuiApplication::syncEvents()
225 // This is the ONLY place where processEvents may be called.
226 // During screen update/ redraw, this method is disabled to
227 // prevent keyboard events being handed to the LyX core, where
228 // they could cause re-entrant calls to screen update.
229 processEvents(QEventLoop::ExcludeUserInputEvents);
233 bool GuiApplication::getRgbColor(LColor_color col,
234 lyx::RGBColor & rgbcol)
236 QColor const & qcol = color_cache_.get(col);
237 if (!qcol.isValid()) {
243 rgbcol.r = qcol.red();
244 rgbcol.g = qcol.green();
245 rgbcol.b = qcol.blue();
250 string const GuiApplication::hexName(LColor_color col)
252 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
256 void GuiApplication::updateColor(LColor_color)
258 // FIXME: Bleh, can't we just clear them all at once ?
259 color_cache_.clear();
263 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
265 socket_callbacks_[fd] =
266 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
270 void GuiApplication::unregisterSocketCallback(int fd)
272 socket_callbacks_.erase(fd);
275 ////////////////////////////////////////////////////////////////////////
276 // X11 specific stuff goes here...
278 bool GuiApplication::x11EventFilter(XEvent * xev)
280 BufferView * bv = currentView().view();
283 case SelectionRequest:
284 lyxerr[Debug::GUI] << "X requested selection." << endl;
286 lyx::docstring const sel = bv->requestSelection();
292 lyxerr[Debug::GUI] << "Lost selection." << endl;
294 bv->clearSelection();
302 ////////////////////////////////////////////////////////////////////////
303 // Mac OSX specific stuff goes here...
308 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
310 DescType returnedType;
312 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
313 typeWildCard, &returnedType, nil, 0,
316 case errAEDescNotFound:
319 return errAEEventNotHandled;
327 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
328 AppleEvent* /*reply*/, long /*refCon*/)
331 AEDescList documentList;
332 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
337 err = checkAppleEventForMissingParams(*inEvent);
340 err = AECountItems(&documentList, &documentCount);
341 for (long documentIndex = 1;
342 err == noErr && documentIndex <= documentCount;
344 DescType returnedType;
349 err = AESizeOfNthItem(&documentList, documentIndex,
350 &returnedType, &actualSize);
352 err = AEGetNthPtr(&documentList, documentIndex,
354 &returnedType, (Ptr)&ref,
355 sizeof(FSRef), &actualSize);
357 FSRefMakePath(&ref, (UInt8*)qstr_buf,
359 s_arg=QString::fromUtf8(qstr_buf);
360 // bv->workAreaDispatch(
361 // FuncRequest(LFUN_FILE_OPEN,
362 // fromqstr(s_arg)));
368 AEDisposeDesc(&documentList);
373 bool GuiApplication::macEventFilter(EventRef event)
375 if (GetEventClass(event) == kEventClassAppleEvent) {
376 EventRecord eventrec;
377 ConvertEventRefToEventRecord(event, &eventrec);
378 AEProcessAppleEvent(&eventrec);
387 } // namespace frontend
390 #include "GuiApplication_moc.cpp"