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>
43 #include <QTranslator>
50 #include <boost/bind.hpp>
56 extern void initEncodings();
58 ///////////////////////////////////////////////////////////////
59 // You can find other X11 and MACX specific stuff
60 // at the end of this file...
61 ///////////////////////////////////////////////////////////////
68 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
76 frontend::Application * createApplication(int & argc, char * argv[])
78 return new frontend::GuiApplication(argc, argv);
84 GuiApplication * guiApp;
87 GuiApplication::~GuiApplication()
89 socket_callbacks_.clear();
93 GuiApplication::GuiApplication(int & argc, char ** argv)
94 : QApplication(argc, argv), Application(argc, argv)
97 // doubleClickInterval() is 400 ms on X11 which 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 QTranslator qt_trans;
112 QString language_name = QString("qt_") + QLocale::system().name();
113 language_name.truncate(5);
114 if (qt_trans.load(language_name,
115 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
117 qApp->installTranslator(&qt_trans);
118 // even if the language calls for RtL, don't do that
119 qApp->setLayoutDirection(Qt::LeftToRight);
121 << "Successfully installed Qt translations for locale "
122 << fromqstr(language_name) << std::endl;
125 << "Could not find Qt translations for locale "
126 << fromqstr(language_name) << std::endl;
129 // These translations are meant to break Qt/Mac menu merging
130 // algorithm on some entries. It lists the menu names that
131 // should not be moved to the LyX menu
132 QTranslator aqua_trans(0);
133 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
135 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
137 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
139 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
142 qApp->installTranslator(&aqua_trans);
145 using namespace lyx::graphics;
147 Image::newImage = boost::bind(&QLImage::newImage);
148 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
150 // needs to be done before reading lyxrc
151 lyxrc.dpi = getDPI();
153 LoaderQueue::setPriority(10,100);
155 setQuitOnLastWindowClosed(false);
156 QObject::connect(this, SIGNAL(lastWindowClosed()),
157 this, SLOT(quitLyX()));
163 void GuiApplication::quitLyX()
165 theLyXFunc().setLyXView(0);
167 // trigger LFUN_LYX_QUIT instead of QApplication::quit() directly
168 // since LFUN_LYX_QUIT may have more cleanup stuff
169 dispatch(FuncRequest(LFUN_LYX_QUIT, "force"));
173 Clipboard& GuiApplication::clipboard()
179 Selection& GuiApplication::selection()
185 int const GuiApplication::exec()
187 return QApplication::exec();
191 void GuiApplication::exit(int status)
193 QApplication::exit(status);
197 string const GuiApplication::romanFontName()
200 font.setKerning(false);
201 font.setStyleHint(QFont::Serif);
202 font.setFamily("serif");
204 return fromqstr(QFontInfo(font).family());
208 string const GuiApplication::sansFontName()
211 font.setKerning(false);
212 font.setStyleHint(QFont::SansSerif);
213 font.setFamily("sans");
215 return fromqstr(QFontInfo(font).family());
219 string const GuiApplication::typewriterFontName()
222 font.setKerning(false);
223 font.setStyleHint(QFont::TypeWriter);
224 font.setFamily("monospace");
226 return fromqstr(QFontInfo(font).family());
230 void GuiApplication::syncEvents()
232 // This is the ONLY place where processEvents may be called.
233 // During screen update/ redraw, this method is disabled to
234 // prevent keyboard events being handed to the LyX core, where
235 // they could cause re-entrant calls to screen update.
236 processEvents(QEventLoop::ExcludeUserInputEvents);
240 bool GuiApplication::getRgbColor(LColor_color col,
241 lyx::RGBColor & rgbcol)
243 QColor const & qcol = color_cache_.get(col);
244 if (!qcol.isValid()) {
250 rgbcol.r = qcol.red();
251 rgbcol.g = qcol.green();
252 rgbcol.b = qcol.blue();
257 string const GuiApplication::hexName(LColor_color col)
259 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
263 void GuiApplication::updateColor(LColor_color)
265 // FIXME: Bleh, can't we just clear them all at once ?
266 color_cache_.clear();
270 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
272 socket_callbacks_[fd] =
273 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
277 void GuiApplication::unregisterSocketCallback(int fd)
279 socket_callbacks_.erase(fd);
282 ////////////////////////////////////////////////////////////////////////
283 // X11 specific stuff goes here...
285 bool GuiApplication::x11EventFilter(XEvent * xev)
287 BufferView * bv = currentView().view();
290 case SelectionRequest:
291 lyxerr[Debug::GUI] << "X requested selection." << endl;
293 lyx::docstring const sel = bv->requestSelection();
299 lyxerr[Debug::GUI] << "Lost selection." << endl;
301 bv->clearSelection();
309 ////////////////////////////////////////////////////////////////////////
310 // Mac OSX specific stuff goes here...
315 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
317 DescType returnedType;
319 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
320 typeWildCard, &returnedType, nil, 0,
323 case errAEDescNotFound:
326 return errAEEventNotHandled;
334 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
335 AppleEvent* /*reply*/, long /*refCon*/)
338 AEDescList documentList;
339 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
344 err = checkAppleEventForMissingParams(*inEvent);
347 err = AECountItems(&documentList, &documentCount);
348 for (long documentIndex = 1;
349 err == noErr && documentIndex <= documentCount;
351 DescType returnedType;
356 err = AESizeOfNthItem(&documentList, documentIndex,
357 &returnedType, &actualSize);
359 err = AEGetNthPtr(&documentList, documentIndex,
361 &returnedType, (Ptr)&ref,
362 sizeof(FSRef), &actualSize);
364 FSRefMakePath(&ref, (UInt8*)qstr_buf,
366 s_arg=QString::fromUtf8(qstr_buf);
367 // bv->workAreaDispatch(
368 // FuncRequest(LFUN_FILE_OPEN,
369 // fromqstr(s_arg)));
375 AEDisposeDesc(&documentList);
380 bool GuiApplication::macEventFilter(EventRef event)
382 if (GetEventClass(event) == kEventClassAppleEvent) {
383 EventRecord eventrec;
384 ConvertEventRefToEventRecord(event, &eventrec);
385 AEProcessAppleEvent(&eventrec);
394 } // namespace frontend
397 #include "GuiApplication_moc.cpp"