]> git.lyx.org Git - lyx.git/blob - src/frontends/qt3/GuiApplication.C
d167f9fdb83577e9639bba0bb5f9308ace1c8a4f
[lyx.git] / src / frontends / qt3 / GuiApplication.C
1 /**
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.
5  *
6  * \author unknown
7  * \author John Levon
8  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "frontends/LyXView.h"
16 #include "frontends/WorkArea.h"
17
18 #include "GuiApplication.h"
19
20 #include "qt_helpers.h"
21 #include "QLImage.h"
22 #include "socket_callback.h"
23
24 #include "graphics/LoaderQueue.h"
25
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/package.h"
29
30 #include "BufferView.h"
31 #include "lyx_main.h"
32 #include "lyxrc.h"
33 #include "debug.h"
34
35 #include <qapplication.h>
36 #include <qclipboard.h>
37 #include <qeventloop.h>
38 #include <qlocale.h>
39 #include <qpaintdevicemetrics.h>
40 #include <qtextcodec.h>
41 #include <qtranslator.h>
42
43 #ifdef Q_WS_X11
44 #include <X11/Xlib.h>
45 #endif
46
47 #include <boost/bind.hpp>
48
49 using lyx::support::subst;
50
51 using std::string;
52 using std::endl;
53
54 // in QLyXKeySym.C
55 extern void initEncodings();
56
57 ///////////////////////////////////////////////////////////////
58 // You can find other X11 and MACX specific stuff
59 // at the end of this file...
60 ///////////////////////////////////////////////////////////////
61
62 namespace {
63
64 map<int, shared_ptr<socket_callback> > socket_callbacks;
65
66 int getDPI()
67 {
68         QWidget w;
69         QPaintDeviceMetrics pdm(&w);
70         return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
71 }
72
73 } // namespace anon
74
75
76 namespace lyx {
77
78 lyx::frontend::Application * createApplication(int & argc, char * argv[])
79 {
80         GuiApplication app(argc, argv);
81
82         return &app;
83 }
84
85 namespace frontend {
86
87 GuiApplication::GuiApplication(int & argc, char ** argv)
88         : QApplication(argc, argv), Application(argc, argv)
89 {
90 #ifdef Q_WS_X11
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);
96 #endif
97
98 #ifdef Q_WS_MACX
99         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
100                               NewAEEventHandlerUPP(handleOpenDocuments),
101                               0, false);
102 #endif
103
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);
113                 lyxerr[Debug::GUI]
114                         << "Successfully installed Qt translations for locale "
115                         << QTextCodec::locale() << std::endl;
116         } else
117                 lyxerr[Debug::GUI]
118                         << "Could not find  Qt translations for locale "
119                         << QTextCodec::locale() << std::endl;
120 #endif
121
122 #ifdef Q_WS_MACX
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,
128                                              "do_not_merge_me"));
129         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
130                                              "do_not_merge_me"));
131         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
132                                              "do_not_merge_me"));
133         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
134                                              "do_not_merge_me"));
135
136         qApp->installTranslator(&aqua_trans);
137 #endif
138
139         using namespace lyx::graphics;
140
141         Image::newImage = boost::bind(&QLImage::newImage);
142         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
143
144         // needs to be done before reading lyxrc
145         lyxrc.dpi = getDPI();
146
147         LoaderQueue::setPriority(10,100);
148 }
149
150
151 Clipboard& GuiApplication::clipboard()
152 {
153         return clipboard_;
154 }
155
156
157 Selection& GuiApplication::selection()
158 {
159         return selection_;
160 }
161
162
163 int const GuiApplication::exec()
164 {
165         return QApplication::exec();
166 }
167
168
169 void GuiApplication::exit(int status)
170 {
171         QApplication::exit(status);
172 }
173
174
175 string const GuiApplication::romanFontName()
176 {
177         QFont font;
178         font.setStyleHint(QFont::Serif);
179         font.setFamily("serif");
180
181         return fromqstr(QFontInfo(font).family());
182 }
183
184
185 string const GuiApplication::sansFontName()
186 {
187         QFont font;
188         font.setStyleHint(QFont::SansSerif);
189         font.setFamily("sans");
190
191         return fromqstr(QFontInfo(font).family());
192 }
193
194
195 string const GuiApplication::typewriterFontName()
196 {
197         QFont font;
198         font.setStyleHint(QFont::TypeWriter);
199         font.setFamily("monospace");
200
201         return fromqstr(QFontInfo(font).family());
202 }
203
204
205 void GuiApplication::syncEvents()
206 {
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);
213 #endif
214 }
215
216
217 bool GuiApplication::getRgbColor(LColor_color col,
218                                                                  lyx::RGBColor & rgbcol)
219 {
220         QColor const & qcol = lcolorcache.get(col);
221         if (!qcol.isValid()) {
222                 rgbcol.r = 0;
223                 rgbcol.g = 0;
224                 rgbcol.b = 0;
225                 return false;
226         }
227         rgbcol.r = qcol.red();
228         rgbcol.g = qcol.green();
229         rgbcol.b = qcol.blue();
230         return true;
231 }
232
233
234 string const GuiApplication::hexName(LColor_color col)
235 {
236         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
237 }
238
239
240 void GuiApplication::updateColor(LColor_color)
241 {
242         // FIXME: Bleh, can't we just clear them all at once ?
243         lcolorcache.clear();
244 }
245
246
247 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
248 {
249         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
250 }
251
252
253 void GuiApplication::unregisterSocketCallback(int fd)
254 {
255         socket_callbacks.erase(fd);
256 }
257
258
259 ////////////////////////////////////////////////////////////////////////
260 // X11 specific stuff goes here...
261 #ifdef Q_WS_X11
262 bool GuiApplication::x11EventFilter(XEvent * xev)
263 {
264         switch (xev->type) {
265         case SelectionRequest:
266                 lyxerr[Debug::GUI] << "X requested selection." << endl;
267                 if (buffer_view_) {
268                         lyx::docstring const sel = buffer_view_->requestSelection();
269                         if (!sel.empty())
270                                 selection_.put(sel);
271                 }
272                 break;
273         case SelectionClear:
274                 lyxerr[Debug::GUI] << "Lost selection." << endl;
275                 if (buffer_view_)
276                         buffer_view_->clearSelection();
277                 break;
278         }
279         return false;
280 }
281 #endif
282
283
284 ////////////////////////////////////////////////////////////////////////
285 // Mac OSX specific stuff goes here...
286
287 #ifdef Q_WS_MACX
288 namespace{
289
290 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
291  {
292         DescType returnedType;
293         Size actualSize;
294         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
295                                       typeWildCard, &returnedType, nil, 0,
296                                       &actualSize);
297         switch (err) {
298         case errAEDescNotFound:
299                 return noErr;
300         case noErr:
301                 return errAEEventNotHandled;
302         default:
303                 return err;
304         }
305  }
306
307 } // namespace
308
309 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
310                                        AppleEvent* /*reply*/, long /*refCon*/)
311 {
312         QString s_arg;
313         AEDescList documentList;
314         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
315                                    &documentList);
316         if (err != noErr)
317                 return err;
318
319         err = checkAppleEventForMissingParams(*inEvent);
320         if (err == noErr) {
321                 long documentCount;
322                 err = AECountItems(&documentList, &documentCount);
323                 for (long documentIndex = 1;
324                      err == noErr && documentIndex <= documentCount;
325                      documentIndex++) {
326                         DescType returnedType;
327                         Size actualSize;
328                         AEKeyword keyword;
329                         FSRef ref;
330                         char qstr_buf[1024];
331                         err = AESizeOfNthItem(&documentList, documentIndex,
332                                               &returnedType, &actualSize);
333                         if (err == noErr) {
334                                 err = AEGetNthPtr(&documentList, documentIndex,
335                                                   typeFSRef, &keyword,
336                                                   &returnedType, (Ptr)&ref,
337                                                   sizeof(FSRef), &actualSize);
338                                 if (err == noErr) {
339                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
340                                                       1024);
341                                         s_arg=QString::fromUtf8(qstr_buf);
342 //                                      buffer_view_->workAreaDispatch(
343 //                                              FuncRequest(LFUN_FILE_OPEN,
344 //                                                          fromqstr(s_arg)));
345                                         break;
346                                 }
347                         }
348                 } // for ...
349         }
350         AEDisposeDesc(&documentList);
351
352         return err;
353 }
354
355 bool GuiApplication::macEventFilter(EventRef event)
356 {
357         if (GetEventClass(event) == kEventClassAppleEvent) {
358                 EventRecord eventrec;
359                 ConvertEventRefToEventRecord(event, &eventrec);
360                 AEProcessAppleEvent(&eventrec);
361
362                 return false;
363         }
364         return false;
365 }
366
367 #endif  // Q_WS_MACX
368
369 } // namespace frontend
370 } // namespace lyx