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 // FIXME: this is needed for now because LyXFunc is still constructed
26 #include "frontends/Application_pimpl.h"
28 #include "graphics/LoaderQueue.h"
30 #include "support/lstrings.h"
31 #include "support/os.h"
32 #include "support/package.h"
38 #include <QApplication>
42 #include <QLibraryInfo>
44 #include <QTranslator>
50 #include <boost/bind.hpp>
52 using lyx::support::subst;
58 extern void initEncodings();
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 and MACX specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
70 return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
79 GuiApplication::GuiApplication(int & argc, char ** argv)
80 : QApplication(argc, argv), Application(argc, argv)
83 // doubleClickInterval() is 400 ms on X11 witch is just too long.
84 // On Windows and Mac OS X, the operating system's value is used.
85 // On Microsoft Windows, calling this function sets the double
86 // click interval for all applications. So we don't!
87 QApplication::setDoubleClickInterval(300);
91 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
92 NewAEEventHandlerUPP(handleOpenDocuments),
96 // install translation file for Qt built-in dialogs
97 // These are only installed since Qt 3.2.x
99 QString language_name = QString("qt_") + QLocale::system().name();
100 language_name.truncate(5);
101 if (qt_trans.load(language_name,
102 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
104 qApp->installTranslator(&qt_trans);
105 // even if the language calls for RtL, don't do that
106 qApp->setLayoutDirection(Qt::LeftToRight);
108 << "Successfully installed Qt translations for locale "
109 << fromqstr(language_name) << std::endl;
112 << "Could not find Qt translations for locale "
113 << fromqstr(language_name) << std::endl;
116 // These translations are meant to break Qt/Mac menu merging
117 // algorithm on some entries. It lists the menu names that
118 // should not be moved to the LyX menu
119 QTranslator aqua_trans(0);
120 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
122 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
124 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
126 aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
129 qApp->installTranslator(&aqua_trans);
132 using namespace lyx::graphics;
134 Image::newImage = boost::bind(&QLImage::newImage);
135 Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
137 // needs to be done before reading lyxrc
138 lyxrc.dpi = getDPI();
140 LoaderQueue::setPriority(10,100);
144 Clipboard& GuiApplication::clipboard()
150 Selection& GuiApplication::selection()
156 int const GuiApplication::exec()
158 return QApplication::exec();
162 void GuiApplication::exit(int status)
164 QApplication::exit(status);
168 // FIXME: this whole method needs to be moved to Application.
169 LyXView & GuiApplication::createView(unsigned int width,
174 // this can't be done before because it needs the Languages object
177 int view_id = gui().newView(width, height);
178 GuiView & view = static_cast<GuiView &> (gui().view(view_id));
180 pimpl_->lyxfunc_.reset(new LyXFunc(&view));
182 // FIXME: for now we assume that there is only one LyXView with id = 0.
183 /*int workArea_id_ =*/ gui().newWorkArea(width, height, 0);
184 //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
186 LyX::ref().addLyXView(&view);
190 // FIXME: put this initialisation code in GuiView accessible via
191 // a pure virtual method in LyXView.
193 // only true when the -geometry option was NOT used
194 if (width != 0 && height != 0) {
195 if (posx != -1 && posy != -1) {
197 // FIXME: use only setGeoemtry when Trolltech has
198 // fixed the qt4/X11 bug
199 view.setGeometry(posx, posy,width, height);
201 view.resize(width, height);
202 view.move(posx, posy);
205 view.resize(width, height);
209 view.setWindowState(Qt::WindowMaximized);
218 ////////////////////////////////////////////////////////////////////////
219 // X11 specific stuff goes here...
221 bool GuiApplication::x11EventFilter(XEvent * xev)
224 case SelectionRequest:
225 lyxerr[Debug::GUI] << "X requested selection." << endl;
227 lyx::docstring const sel = buffer_view_->requestSelection();
233 lyxerr[Debug::GUI] << "Lost selection." << endl;
235 buffer_view_->clearSelection();
243 ////////////////////////////////////////////////////////////////////////
244 // Mac OSX specific stuff goes here...
249 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
251 DescType returnedType;
253 OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
254 typeWildCard, &returnedType, nil, 0,
257 case errAEDescNotFound:
260 return errAEEventNotHandled;
268 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
269 AppleEvent* /*reply*/, long /*refCon*/)
272 AEDescList documentList;
273 OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
278 err = checkAppleEventForMissingParams(*inEvent);
281 err = AECountItems(&documentList, &documentCount);
282 for (long documentIndex = 1;
283 err == noErr && documentIndex <= documentCount;
285 DescType returnedType;
290 err = AESizeOfNthItem(&documentList, documentIndex,
291 &returnedType, &actualSize);
293 err = AEGetNthPtr(&documentList, documentIndex,
295 &returnedType, (Ptr)&ref,
296 sizeof(FSRef), &actualSize);
298 FSRefMakePath(&ref, (UInt8*)qstr_buf,
300 s_arg=QString::fromUtf8(qstr_buf);
301 // buffer_view_->workAreaDispatch(
302 // FuncRequest(LFUN_FILE_OPEN,
303 // fromqstr(s_arg)));
309 AEDisposeDesc(&documentList);
314 bool GuiApplication::macEventFilter(EventRef event)
316 if (GetEventClass(event) == kEventClassAppleEvent) {
317 EventRecord eventrec;
318 ConvertEventRefToEventRecord(event, &eventrec);
319 AEProcessAppleEvent(&eventrec);
328 } // namespace frontend