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);
85 return new lyx::frontend::GuiApplication(argc, argv);
92 GuiApplication::GuiApplication(int & argc, char ** argv)
93 : QApplication(argc, argv), Application(argc, argv)
96 // doubleClickInterval() is 400 ms on X11 witch is just too long.
97 // On Windows and Mac OS X, the operating system's value is used.
98 // On Microsoft Windows, calling this function sets the double
99 // click interval for all applications. So we don't!
100 QApplication::setDoubleClickInterval(300);
104 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
105 NewAEEventHandlerUPP(handleOpenDocuments),
109 // install translation file for Qt built-in dialogs
110 // These are only installed since Qt 3.2.x
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);
159 Clipboard& GuiApplication::clipboard()
165 Selection& GuiApplication::selection()
171 int const GuiApplication::exec()
173 return QApplication::exec();
177 void GuiApplication::exit(int status)
179 QApplication::exit(status);
183 string const GuiApplication::romanFontName()
186 font.setStyleHint(QFont::Serif);
187 font.setFamily("serif");
189 return fromqstr(QFontInfo(font).family());
193 string const GuiApplication::sansFontName()
196 font.setStyleHint(QFont::SansSerif);
197 font.setFamily("sans");
199 return fromqstr(QFontInfo(font).family());
203 string const GuiApplication::typewriterFontName()
206 font.setStyleHint(QFont::TypeWriter);
207 font.setFamily("monospace");
209 return fromqstr(QFontInfo(font).family());
213 void GuiApplication::syncEvents()
215 // This is the ONLY place where processEvents may be called.
216 // During screen update/ redraw, this method is disabled to
217 // prevent keyboard events being handed to the LyX core, where
218 // they could cause re-entrant calls to screen update.
219 processEvents(QEventLoop::ExcludeUserInputEvents);
223 bool GuiApplication::getRgbColor(LColor_color col,
224 lyx::RGBColor & rgbcol)
226 QColor const & qcol = color_cache_.get(col);
227 if (!qcol.isValid()) {
233 rgbcol.r = qcol.red();
234 rgbcol.g = qcol.green();
235 rgbcol.b = qcol.blue();
240 string const GuiApplication::hexName(LColor_color col)
242 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
246 void GuiApplication::updateColor(LColor_color)
248 // FIXME: Bleh, can't we just clear them all at once ?
249 color_cache_.clear();
253 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
255 socket_callbacks_[fd] =
256 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
260 void GuiApplication::unregisterSocketCallback(int fd)
262 socket_callbacks_.erase(fd);
265 ////////////////////////////////////////////////////////////////////////
266 // X11 specific stuff goes here...
268 bool GuiApplication::x11EventFilter(XEvent * xev)
271 case SelectionRequest:
272 lyxerr[Debug::GUI] << "X requested selection." << endl;
274 lyx::docstring const sel = buffer_view_->requestSelection();
280 lyxerr[Debug::GUI] << "Lost selection." << endl;
282 buffer_view_->clearSelection();
290 ////////////////////////////////////////////////////////////////////////
291 // Mac OSX specific stuff goes here...
296 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
298 DescType returnedType;
300 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
301 typeWildCard, &returnedType, nil, 0,
304 case errAEDescNotFound:
307 return errAEEventNotHandled;
315 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
316 AppleEvent* /*reply*/, long /*refCon*/)
319 AEDescList documentList;
320 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
325 err = checkAppleEventForMissingParams(*inEvent);
328 err = AECountItems(&documentList, &documentCount);
329 for (long documentIndex = 1;
330 err == noErr && documentIndex <= documentCount;
332 DescType returnedType;
337 err = AESizeOfNthItem(&documentList, documentIndex,
338 &returnedType, &actualSize);
340 err = AEGetNthPtr(&documentList, documentIndex,
342 &returnedType, (Ptr)&ref,
343 sizeof(FSRef), &actualSize);
345 FSRefMakePath(&ref, (UInt8*)qstr_buf,
347 s_arg=QString::fromUtf8(qstr_buf);
348 // buffer_view_->workAreaDispatch(
349 // FuncRequest(LFUN_FILE_OPEN,
350 // fromqstr(s_arg)));
356 AEDisposeDesc(&documentList);
361 bool GuiApplication::macEventFilter(EventRef event)
363 if (GetEventClass(event) == kEventClassAppleEvent) {
364 EventRecord eventrec;
365 ConvertEventRefToEventRecord(event, &eventrec);
366 AEProcessAppleEvent(&eventrec);
375 } // namespace frontend