2 * \file qt3/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 "frontends/LyXView.h"
16 #include "frontends/WorkArea.h"
18 #include "GuiApplication.h"
20 #include "lcolorcache.h"
21 #include "qt_helpers.h"
23 #include "socket_callback.h"
25 #include "graphics/LoaderQueue.h"
27 #include "support/lstrings.h"
28 #include "support/os.h"
29 #include "support/package.h"
31 #include "BufferView.h"
37 #include <qapplication.h>
38 #include <qclipboard.h>
39 #include <qeventloop.h>
41 #include <qpaintdevicemetrics.h>
42 #include <qtextcodec.h>
43 #include <qtranslator.h>
49 #include <boost/bind.hpp>
51 using lyx::support::subst;
57 extern void initEncodings();
59 ///////////////////////////////////////////////////////////////
60 // You can find other X11 and MACX specific stuff
61 // at the end of this file...
62 ///////////////////////////////////////////////////////////////
66 std::map<int, boost::shared_ptr<socket_callback> > socket_callbacks;
71 QPaintDeviceMetrics pdm(&w);
72 return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
80 frontend::Application * createApplication(int & argc, char * argv[])
82 frontend::GuiApplication app(argc, argv);
89 GuiApplication::GuiApplication(int & argc, char ** argv)
90 : QApplication(argc, argv), Application(argc, argv)
93 // doubleClickInterval() is 400 ms on X11 witch is just too long.
94 // On Windows and Mac OS X, the operating system's value is used.
95 // On Microsoft Windows, calling this function sets the double
96 // click interval for all applications. So we don't!
97 QApplication::setDoubleClickInterval(300);
101 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
102 NewAEEventHandlerUPP(handleOpenDocuments),
106 #if QT_VERSION >= 0x030200
107 // install translation file for Qt built-in dialogs
108 // These are only installed since Qt 3.2.x
109 QTranslator qt_trans(0);
110 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
111 qInstallPathTranslations())) {
112 qApp->installTranslator(&qt_trans);
113 // even if the language calls for RtL, don't do that
114 qApp->setReverseLayout(false);
116 << "Successfully installed Qt translations for locale "
117 << QTextCodec::locale() << std::endl;
120 << "Could not find Qt translations for locale "
121 << QTextCodec::locale() << std::endl;
125 // These translations are meant to break Qt/Mac menu merging
126 // algorithm on some entries. It lists the menu names that
127 // should not be moved to the LyX menu
128 QTranslator aqua_trans(0);
129 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
131 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
133 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
135 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
138 qApp->installTranslator(&aqua_trans);
141 using namespace lyx::graphics;
143 Image::newImage = boost::bind(&QLImage::newImage);
144 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
146 // needs to be done before reading lyxrc
147 lyxrc.dpi = getDPI();
149 LoaderQueue::setPriority(10,100);
153 Clipboard& GuiApplication::clipboard()
159 Selection& GuiApplication::selection()
165 int const GuiApplication::exec()
167 return QApplication::exec();
171 void GuiApplication::exit(int status)
173 QApplication::exit(status);
177 string const GuiApplication::romanFontName()
180 font.setStyleHint(QFont::Serif);
181 font.setFamily("serif");
183 return fromqstr(QFontInfo(font).family());
187 string const GuiApplication::sansFontName()
190 font.setStyleHint(QFont::SansSerif);
191 font.setFamily("sans");
193 return fromqstr(QFontInfo(font).family());
197 string const GuiApplication::typewriterFontName()
200 font.setStyleHint(QFont::TypeWriter);
201 font.setFamily("monospace");
203 return fromqstr(QFontInfo(font).family());
207 void GuiApplication::syncEvents()
209 // This is the ONLY place where processEvents may be called.
210 // During screen update/ redraw, this method is disabled to
211 // prevent keyboard events being handed to the LyX core, where
212 // they could cause re-entrant calls to screen update.
213 #if QT_VERSION >= 0x030100
214 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
219 bool GuiApplication::getRgbColor(LColor_color col,
220 lyx::RGBColor & rgbcol)
222 QColor const & qcol = lcolorcache.get(col);
223 if (!qcol.isValid()) {
229 rgbcol.r = qcol.red();
230 rgbcol.g = qcol.green();
231 rgbcol.b = qcol.blue();
236 string const GuiApplication::hexName(LColor_color col)
238 return lyx::support::ltrim(fromqstr(lcolorcache.get(col).name()), "#");
242 void GuiApplication::updateColor(LColor_color)
244 // FIXME: Bleh, can't we just clear them all at once ?
249 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
251 socket_callbacks[fd] = boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
255 void GuiApplication::unregisterSocketCallback(int fd)
257 socket_callbacks.erase(fd);
261 ////////////////////////////////////////////////////////////////////////
262 // X11 specific stuff goes here...
264 bool GuiApplication::x11EventFilter(XEvent * xev)
267 case SelectionRequest:
268 lyxerr[Debug::GUI] << "X requested selection." << endl;
270 lyx::docstring const sel = buffer_view_->requestSelection();
276 lyxerr[Debug::GUI] << "Lost selection." << endl;
278 buffer_view_->clearSelection();
286 ////////////////////////////////////////////////////////////////////////
287 // Mac OSX specific stuff goes here...
292 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
294 DescType returnedType;
296 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
297 typeWildCard, &returnedType, nil, 0,
300 case errAEDescNotFound:
303 return errAEEventNotHandled;
311 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
312 AppleEvent* /*reply*/, long /*refCon*/)
315 AEDescList documentList;
316 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
321 err = checkAppleEventForMissingParams(*inEvent);
324 err = AECountItems(&documentList, &documentCount);
325 for (long documentIndex = 1;
326 err == noErr && documentIndex <= documentCount;
328 DescType returnedType;
333 err = AESizeOfNthItem(&documentList, documentIndex,
334 &returnedType, &actualSize);
336 err = AEGetNthPtr(&documentList, documentIndex,
338 &returnedType, (Ptr)&ref,
339 sizeof(FSRef), &actualSize);
341 FSRefMakePath(&ref, (UInt8*)qstr_buf,
343 s_arg=QString::fromUtf8(qstr_buf);
344 // buffer_view_->workAreaDispatch(
345 // FuncRequest(LFUN_FILE_OPEN,
346 // fromqstr(s_arg)));
352 AEDisposeDesc(&documentList);
357 bool GuiApplication::macEventFilter(EventRef event)
359 if (GetEventClass(event) == kEventClassAppleEvent) {
360 EventRecord eventrec;
361 ConvertEventRefToEventRecord(event, &eventrec);
362 AEProcessAppleEvent(&eventrec);
371 } // namespace frontend