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"
18 #include "GuiWorkArea.h"
19 #include "qt_helpers.h"
22 #include "BufferView.h"
24 #include "graphics/LoaderQueue.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/package.h"
34 #include <QApplication>
38 #include <QLibraryInfo>
40 #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()));
75 GuiApplication::GuiApplication(int & argc, char ** argv)
76 : QApplication(argc, argv), Application(argc, argv)
79 // doubleClickInterval() is 400 ms on X11 witch is just too long.
80 // On Windows and Mac OS X, the operating system's value is used.
81 // On Microsoft Windows, calling this function sets the double
82 // click interval for all applications. So we don't!
83 QApplication::setDoubleClickInterval(300);
87 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
88 NewAEEventHandlerUPP(handleOpenDocuments),
92 // install translation file for Qt built-in dialogs
93 // These are only installed since Qt 3.2.x
95 QString language_name = QString("qt_") + QLocale::system().name();
96 language_name.truncate(5);
97 if (qt_trans.load(language_name,
98 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
100 qApp->installTranslator(&qt_trans);
101 // even if the language calls for RtL, don't do that
102 qApp->setLayoutDirection(Qt::LeftToRight);
104 << "Successfully installed Qt translations for locale "
105 << fromqstr(language_name) << std::endl;
108 << "Could not find Qt translations for locale "
109 << fromqstr(language_name) << std::endl;
112 // These translations are meant to break Qt/Mac menu merging
113 // algorithm on some entries. It lists the menu names that
114 // should not be moved to the LyX menu
115 QTranslator aqua_trans(0);
116 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
118 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
120 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
122 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
125 qApp->installTranslator(&aqua_trans);
128 using namespace lyx::graphics;
130 Image::newImage = boost::bind(&QLImage::newImage);
131 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
133 // needs to be done before reading lyxrc
134 lyxrc.dpi = getDPI();
136 LoaderQueue::setPriority(10,100);
140 int const GuiApplication::exec()
142 return QApplication::exec();
146 void GuiApplication::exit(int status)
148 QApplication::exit(status);
152 // FIXME: this whole method needs to be moved to Application.
153 LyXView & GuiApplication::createView(unsigned int width,
158 // this can't be done before because it needs the Languages object
161 int view_id = gui().newView(width, height);
162 GuiView & view = static_cast<GuiView &> (gui().view(view_id));
164 lyxfunc_.reset(new LyXFunc(&view));
166 // FIXME: for now we assume that there is only one LyXView with id = 0.
167 /*int workArea_id_ =*/ gui().newWorkArea(width, height, 0);
168 //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
170 LyX::ref().addLyXView(&view);
174 // FIXME: put this initialisation code in GuiView accessible via
175 // a pure virtual method in LyXView.
177 // only true when the -geometry option was NOT used
178 if (width != 0 && height != 0) {
179 if (posx != -1 && posy != -1) {
181 // FIXME: use only setGeoemtry when Trolltech has
182 // fixed the qt4/X11 bug
183 view.setGeometry(posx, posy,width, height);
185 view.resize(width, height);
186 view.move(posx, posy);
189 view.resize(width, height);
193 view.setWindowState(Qt::WindowMaximized);
202 ////////////////////////////////////////////////////////////////////////
203 // X11 specific stuff goes here...
205 bool GuiApplication::x11EventFilter(XEvent * xev)
208 case SelectionRequest:
209 lyxerr[Debug::GUI] << "X requested selection." << endl;
211 lyx::docstring const sel = buffer_view_->requestSelection();
213 gui_.selection().put(sel);
217 lyxerr[Debug::GUI] << "Lost selection." << endl;
219 buffer_view_->clearSelection();
227 ////////////////////////////////////////////////////////////////////////
228 // Mac OSX specific stuff goes here...
233 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
235 DescType returnedType;
237 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
238 typeWildCard, &returnedType, nil, 0,
241 case errAEDescNotFound:
244 return errAEEventNotHandled;
252 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
253 AppleEvent* /*reply*/, long /*refCon*/)
256 AEDescList documentList;
257 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
262 err = checkAppleEventForMissingParams(*inEvent);
265 err = AECountItems(&documentList, &documentCount);
266 for (long documentIndex = 1;
267 err == noErr && documentIndex <= documentCount;
269 DescType returnedType;
274 err = AESizeOfNthItem(&documentList, documentIndex,
275 &returnedType, &actualSize);
277 err = AEGetNthPtr(&documentList, documentIndex,
279 &returnedType, (Ptr)&ref,
280 sizeof(FSRef), &actualSize);
282 FSRefMakePath(&ref, (UInt8*)qstr_buf,
284 s_arg=QString::fromUtf8(qstr_buf);
285 // buffer_view_->workAreaDispatch(
286 // FuncRequest(LFUN_FILE_OPEN,
287 // fromqstr(s_arg)));
293 AEDisposeDesc(&documentList);
298 bool GuiApplication::macEventFilter(EventRef event)
300 if (GetEventClass(event) == kEventClassAppleEvent) {
301 EventRecord eventrec;
302 ConvertEventRefToEventRecord(event, &eventrec);
303 AEProcessAppleEvent(&eventrec);
312 } // namespace frontend