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"
30 #include "funcrequest.h"
35 #include <QApplication>
39 #include <QLibraryInfo>
41 #include <QTranslator>
48 #include <boost/bind.hpp>
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()));
74 frontend::Application * createApplication(int & argc, char * argv[])
76 return new frontend::GuiApplication(argc, argv);
82 GuiApplication * guiApp;
85 GuiApplication::~GuiApplication()
87 socket_callbacks_.clear();
91 GuiApplication::GuiApplication(int & argc, char ** argv)
92 : QApplication(argc, argv), Application(argc, argv)
95 // doubleClickInterval() is 400 ms on X11 which is just too long.
96 // On Windows and Mac OS X, the operating system's value is used.
97 // On Microsoft Windows, calling this function sets the double
98 // click interval for all applications. So we don't!
99 QApplication::setDoubleClickInterval(300);
103 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
104 NewAEEventHandlerUPP(handleOpenDocuments),
108 // install translation file for Qt built-in dialogs
109 // These are only installed since Qt 3.2.x
110 QTranslator qt_trans;
111 QString language_name = QString("qt_") + QLocale::system().name();
112 language_name.truncate(5);
113 if (qt_trans.load(language_name,
114 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
116 qApp->installTranslator(&qt_trans);
117 // even if the language calls for RtL, don't do that
118 qApp->setLayoutDirection(Qt::LeftToRight);
120 << "Successfully installed Qt translations for locale "
121 << fromqstr(language_name) << std::endl;
124 << "Could not find Qt translations for locale "
125 << fromqstr(language_name) << std::endl;
128 // These translations are meant to break Qt/Mac menu merging
129 // algorithm on some entries. It lists the menu names that
130 // should not be moved to the LyX menu
131 QTranslator aqua_trans(0);
132 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
134 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
136 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
138 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
141 qApp->installTranslator(&aqua_trans);
144 using namespace lyx::graphics;
146 Image::newImage = boost::bind(&QLImage::newImage);
147 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
149 // needs to be done before reading lyxrc
150 lyxrc.dpi = getDPI();
152 LoaderQueue::setPriority(10,100);
154 setQuitOnLastWindowClosed(false);
155 QObject::connect(this, SIGNAL(lastWindowClosed()),
156 this, SLOT(quitLyX()));
162 void GuiApplication::quitLyX()
164 theLyXFunc().setLyXView(0);
166 // trigger LFUN_LYX_QUIT instead of QApplication::quit() directly
167 // since LFUN_LYX_QUIT may have more cleanup stuff
168 dispatch(FuncRequest(LFUN_LYX_QUIT, "force"));
172 Clipboard& GuiApplication::clipboard()
178 Selection& GuiApplication::selection()
184 int const GuiApplication::exec()
186 return QApplication::exec();
190 void GuiApplication::exit(int status)
192 QApplication::exit(status);
196 string const GuiApplication::romanFontName()
199 font.setKerning(false);
200 font.setStyleHint(QFont::Serif);
201 font.setFamily("serif");
203 return fromqstr(QFontInfo(font).family());
207 string const GuiApplication::sansFontName()
210 font.setKerning(false);
211 font.setStyleHint(QFont::SansSerif);
212 font.setFamily("sans");
214 return fromqstr(QFontInfo(font).family());
218 string const GuiApplication::typewriterFontName()
221 font.setKerning(false);
222 font.setStyleHint(QFont::TypeWriter);
223 font.setFamily("monospace");
225 return fromqstr(QFontInfo(font).family());
229 void GuiApplication::syncEvents()
231 // This is the ONLY place where processEvents may be called.
232 // During screen update/ redraw, this method is disabled to
233 // prevent keyboard events being handed to the LyX core, where
234 // they could cause re-entrant calls to screen update.
235 processEvents(QEventLoop::ExcludeUserInputEvents);
239 bool GuiApplication::getRgbColor(LColor_color col,
240 lyx::RGBColor & rgbcol)
242 QColor const & qcol = color_cache_.get(col);
243 if (!qcol.isValid()) {
249 rgbcol.r = qcol.red();
250 rgbcol.g = qcol.green();
251 rgbcol.b = qcol.blue();
256 string const GuiApplication::hexName(LColor_color col)
258 return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
262 void GuiApplication::updateColor(LColor_color)
264 // FIXME: Bleh, can't we just clear them all at once ?
265 color_cache_.clear();
269 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
271 socket_callbacks_[fd] =
272 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
276 void GuiApplication::unregisterSocketCallback(int fd)
278 socket_callbacks_.erase(fd);
281 ////////////////////////////////////////////////////////////////////////
282 // X11 specific stuff goes here...
284 bool GuiApplication::x11EventFilter(XEvent * xev)
287 case SelectionRequest:
288 lyxerr[Debug::GUI] << "X requested selection." << endl;
290 lyx::docstring const sel = buffer_view_->requestSelection();
296 lyxerr[Debug::GUI] << "Lost selection." << endl;
298 buffer_view_->clearSelection();
306 ////////////////////////////////////////////////////////////////////////
307 // Mac OSX specific stuff goes here...
312 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
314 DescType returnedType;
316 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
317 typeWildCard, &returnedType, nil, 0,
320 case errAEDescNotFound:
323 return errAEEventNotHandled;
331 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
332 AppleEvent* /*reply*/, long /*refCon*/)
335 AEDescList documentList;
336 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
341 err = checkAppleEventForMissingParams(*inEvent);
344 err = AECountItems(&documentList, &documentCount);
345 for (long documentIndex = 1;
346 err == noErr && documentIndex <= documentCount;
348 DescType returnedType;
353 err = AESizeOfNthItem(&documentList, documentIndex,
354 &returnedType, &actualSize);
356 err = AEGetNthPtr(&documentList, documentIndex,
358 &returnedType, (Ptr)&ref,
359 sizeof(FSRef), &actualSize);
361 FSRefMakePath(&ref, (UInt8*)qstr_buf,
363 s_arg=QString::fromUtf8(qstr_buf);
364 // buffer_view_->workAreaDispatch(
365 // FuncRequest(LFUN_FILE_OPEN,
366 // fromqstr(s_arg)));
372 AEDisposeDesc(&documentList);
377 bool GuiApplication::macEventFilter(EventRef event)
379 if (GetEventClass(event) == kEventClassAppleEvent) {
380 EventRecord eventrec;
381 ConvertEventRefToEventRecord(event, &eventrec);
382 AEProcessAppleEvent(&eventrec);
391 } // namespace frontend
394 #include "GuiApplication_moc.cpp"