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>
52 extern void initEncodings();
54 ///////////////////////////////////////////////////////////////
55 // You can find other X11 and MACX specific stuff
56 // at the end of this file...
57 ///////////////////////////////////////////////////////////////
64 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
72 frontend::Application * createApplication(int & argc, char * argv[])
74 return new frontend::GuiApplication(argc, argv);
80 GuiApplication * guiApp;
83 GuiApplication::~GuiApplication()
85 socket_callbacks_.clear();
89 GuiApplication::GuiApplication(int & argc, char ** argv)
90 : QApplication(argc, argv), Application(argc, argv)
93 // doubleClickInterval() is 400 ms on X11 which 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 // install translation file for Qt built-in dialogs
107 // These are only installed since Qt 3.2.x
108 QTranslator qt_trans;
109 QString language_name = QString("qt_") + QLocale::system().name();
110 language_name.truncate(5);
111 if (qt_trans.load(language_name,
112 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
114 qApp->installTranslator(&qt_trans);
115 // even if the language calls for RtL, don't do that
116 qApp->setLayoutDirection(Qt::LeftToRight);
118 << "Successfully installed Qt translations for locale "
119 << fromqstr(language_name) << std::endl;
122 << "Could not find Qt translations for locale "
123 << fromqstr(language_name) << std::endl;
126 // These translations are meant to break Qt/Mac menu merging
127 // algorithm on some entries. It lists the menu names that
128 // should not be moved to the LyX menu
129 QTranslator aqua_trans(0);
130 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
132 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
134 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
136 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
139 qApp->installTranslator(&aqua_trans);
142 using namespace lyx::graphics;
144 Image::newImage = boost::bind(&QLImage::newImage);
145 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
147 // needs to be done before reading lyxrc
148 lyxrc.dpi = getDPI();
150 LoaderQueue::setPriority(10,100);
156 Clipboard& GuiApplication::clipboard()
162 Selection& GuiApplication::selection()
168 int const GuiApplication::exec()
170 return QApplication::exec();
174 void GuiApplication::exit(int status)
176 QApplication::exit(status);
180 string const GuiApplication::romanFontName()
183 font.setKerning(false);
184 font.setStyleHint(QFont::Serif);
185 font.setFamily("serif");
187 return fromqstr(QFontInfo(font).family());
191 string const GuiApplication::sansFontName()
194 font.setKerning(false);
195 font.setStyleHint(QFont::SansSerif);
196 font.setFamily("sans");
198 return fromqstr(QFontInfo(font).family());
202 string const GuiApplication::typewriterFontName()
205 font.setKerning(false);
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