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 return new frontend::GuiApplication(argc, argv);
87 GuiApplication::GuiApplication(int & argc, char ** argv)
88 : QApplication(argc, argv), Application(argc, argv)
91 // doubleClickInterval() is 400 ms on X11 witch is just too long.
92 // On Windows and Mac OS X, the operating system's value is used.
93 // On Microsoft Windows, calling this function sets the double
94 // click interval for all applications. So we don't!
95 QApplication::setDoubleClickInterval(300);
99 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
100 NewAEEventHandlerUPP(handleOpenDocuments),
104 #if QT_VERSION >= 0x030200
105 // install translation file for Qt built-in dialogs
106 // These are only installed since Qt 3.2.x
107 QTranslator qt_trans(0);
108 if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
109 qInstallPathTranslations())) {
110 qApp->installTranslator(&qt_trans);
111 // even if the language calls for RtL, don't do that
112 qApp->setReverseLayout(false);
114 << "Successfully installed Qt translations for locale "
115 << QTextCodec::locale() << std::endl;
118 << "Could not find Qt translations for locale "
119 << QTextCodec::locale() << std::endl;
123 // These translations are meant to break Qt/Mac menu merging
124 // algorithm on some entries. It lists the menu names that
125 // should not be moved to the LyX menu
126 QTranslator aqua_trans(0);
127 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
129 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
131 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
133 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
136 qApp->installTranslator(&aqua_trans);
139 using namespace lyx::graphics;
141 Image::newImage = boost::bind(&QLImage::newImage);
142 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
144 // needs to be done before reading lyxrc
145 lyxrc.dpi = getDPI();
147 LoaderQueue::setPriority(10,100);
151 Clipboard& GuiApplication::clipboard()
157 Selection& GuiApplication::selection()
163 int const GuiApplication::exec()
165 return QApplication::exec();
169 void GuiApplication::exit(int status)
171 QApplication::exit(status);
175 string const GuiApplication::romanFontName()
178 font.setStyleHint(QFont::Serif);
179 font.setFamily("serif");
181 return fromqstr(QFontInfo(font).family());
185 string const GuiApplication::sansFontName()
188 font.setStyleHint(QFont::SansSerif);
189 font.setFamily("sans");
191 return fromqstr(QFontInfo(font).family());
195 string const GuiApplication::typewriterFontName()
198 font.setStyleHint(QFont::TypeWriter);
199 font.setFamily("monospace");
201 return fromqstr(QFontInfo(font).family());
205 void GuiApplication::syncEvents()
207 // This is the ONLY place where processEvents may be called.
208 // During screen update/ redraw, this method is disabled to
209 // prevent keyboard events being handed to the LyX core, where
210 // they could cause re-entrant calls to screen update.
211 #if QT_VERSION >= 0x030100
212 qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
217 bool GuiApplication::getRgbColor(LColor_color col,
218 lyx::RGBColor & rgbcol)
220 QColor const & qcol = lcolorcache.get(col);
221 if (!qcol.isValid()) {
227 rgbcol.r = qcol.red();
228 rgbcol.g = qcol.green();
229 rgbcol.b = qcol.blue();
234 string const GuiApplication::hexName(LColor_color col)
236 return lyx::support::ltrim(fromqstr(lcolorcache.get(col).name()), "#");
240 void GuiApplication::updateColor(LColor_color)
242 // FIXME: Bleh, can't we just clear them all at once ?
247 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
249 socket_callbacks[fd] = boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
253 void GuiApplication::unregisterSocketCallback(int fd)
255 socket_callbacks.erase(fd);
259 ////////////////////////////////////////////////////////////////////////
260 // X11 specific stuff goes here...
262 bool GuiApplication::x11EventFilter(XEvent * xev)
265 case SelectionRequest:
266 lyxerr[Debug::GUI] << "X requested selection." << endl;
268 lyx::docstring const sel = buffer_view_->requestSelection();
274 lyxerr[Debug::GUI] << "Lost selection." << endl;
276 buffer_view_->clearSelection();
284 ////////////////////////////////////////////////////////////////////////
285 // Mac OSX specific stuff goes here...
290 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
292 DescType returnedType;
294 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
295 typeWildCard, &returnedType, nil, 0,
298 case errAEDescNotFound:
301 return errAEEventNotHandled;
309 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
310 AppleEvent* /*reply*/, long /*refCon*/)
313 AEDescList documentList;
314 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
319 err = checkAppleEventForMissingParams(*inEvent);
322 err = AECountItems(&documentList, &documentCount);
323 for (long documentIndex = 1;
324 err == noErr && documentIndex <= documentCount;
326 DescType returnedType;
331 err = AESizeOfNthItem(&documentList, documentIndex,
332 &returnedType, &actualSize);
334 err = AEGetNthPtr(&documentList, documentIndex,
336 &returnedType, (Ptr)&ref,
337 sizeof(FSRef), &actualSize);
339 FSRefMakePath(&ref, (UInt8*)qstr_buf,
341 s_arg=QString::fromUtf8(qstr_buf);
342 // buffer_view_->workAreaDispatch(
343 // FuncRequest(LFUN_FILE_OPEN,
344 // fromqstr(s_arg)));
350 AEDisposeDesc(&documentList);
355 bool GuiApplication::macEventFilter(EventRef event)
357 if (GetEventClass(event) == kEventClassAppleEvent) {
358 EventRecord eventrec;
359 ConvertEventRefToEventRecord(event, &eventrec);
360 AEProcessAppleEvent(&eventrec);
369 } // namespace frontend