]> git.lyx.org Git - features.git/blob - src/frontends/qt3/GuiApplication.C
This commit creates a new "frontends/lyx_gui.C" that contains all functions that...
[features.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
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "lyx_main.h"
31 #include "lyxrc.h"
32 #include "debug.h"
33
34 #include <qapplication.h>
35 #include <qclipboard.h>
36 #include <qeventloop.h>
37 #include <qlocale.h>
38 #include <qpaintdevicemetrics.h>
39 #include <qtextcodec.h>
40 #include <qtranslator.h>
41
42 #ifdef Q_WS_X11
43 #include <X11/Xlib.h>
44 #endif
45
46 #include <boost/bind.hpp>
47
48 using lyx::support::subst;
49
50 using std::string;
51 using std::endl;
52
53 // in QLyXKeySym.C
54 extern void initEncodings();
55
56 ///////////////////////////////////////////////////////////////
57 // You can find other X11 and MACX specific stuff
58 // at the end of this file...
59 ///////////////////////////////////////////////////////////////
60
61 namespace {
62
63 int getDPI()
64 {
65         QWidget w;
66         QPaintDeviceMetrics pdm(&w);
67         return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
68 }
69
70 } // namespace anon
71
72
73 namespace lyx {
74 namespace frontend {
75
76 GuiApplication::GuiApplication(int & argc, char ** argv)
77         : QApplication(argc, argv), Application(argc, argv)
78 {
79 #ifdef Q_WS_X11
80         // doubleClickInterval() is 400 ms on X11 witch is just too long.
81         // On Windows and Mac OS X, the operating system's value is used.
82         // On Microsoft Windows, calling this function sets the double
83         // click interval for all applications. So we don't!
84         QApplication::setDoubleClickInterval(300);
85 #endif
86
87 #ifdef Q_WS_MACX
88         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
89                               NewAEEventHandlerUPP(handleOpenDocuments),
90                               0, false);
91 #endif
92
93 #if QT_VERSION >= 0x030200
94         // install translation file for Qt built-in dialogs
95         // These are only installed since Qt 3.2.x
96         QTranslator qt_trans(0);
97         if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
98                                 qInstallPathTranslations())) {
99                 qApp->installTranslator(&qt_trans);
100                 // even if the language calls for RtL, don't do that
101                 qApp->setReverseLayout(false);
102                 lyxerr[Debug::GUI]
103                         << "Successfully installed Qt translations for locale "
104                         << QTextCodec::locale() << std::endl;
105         } else
106                 lyxerr[Debug::GUI]
107                         << "Could not find  Qt translations for locale "
108                         << QTextCodec::locale() << std::endl;
109 #endif
110
111 #ifdef Q_WS_MACX
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,
117                                              "do_not_merge_me"));
118         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
119                                              "do_not_merge_me"));
120         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
121                                              "do_not_merge_me"));
122         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
123                                              "do_not_merge_me"));
124
125         qApp->installTranslator(&aqua_trans);
126 #endif
127
128         using namespace lyx::graphics;
129
130         Image::newImage = boost::bind(&QLImage::newImage);
131         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
132
133         // needs to be done before reading lyxrc
134         lyxrc.dpi = getDPI();
135
136         LoaderQueue::setPriority(10,100);
137 }
138
139
140 Clipboard& GuiApplication::clipboard()
141 {
142         return clipboard_;
143 }
144
145
146 Selection& GuiApplication::selection()
147 {
148         return selection_;
149 }
150
151
152 int const GuiApplication::exec()
153 {
154         return QApplication::exec();
155 }
156
157
158 void GuiApplication::exit(int status)
159 {
160         QApplication::exit(status);
161 }
162
163
164 string const GuiApplication::romanFontName()
165 {
166         QFont font;
167         font.setStyleHint(QFont::Serif);
168         font.setFamily("serif");
169
170         return fromqstr(QFontInfo(font).family());
171 }
172
173
174 string const GuiApplication::sansFontName()
175 {
176         QFont font;
177         font.setStyleHint(QFont::SansSerif);
178         font.setFamily("sans");
179
180         return fromqstr(QFontInfo(font).family());
181 }
182
183
184 string const GuiApplication::typewriterFontName()
185 {
186         QFont font;
187         font.setStyleHint(QFont::TypeWriter);
188         font.setFamily("monospace");
189
190         return fromqstr(QFontInfo(font).family());
191 }
192
193
194 ////////////////////////////////////////////////////////////////////////
195 // X11 specific stuff goes here...
196 #ifdef Q_WS_X11
197 bool GuiApplication::x11EventFilter(XEvent * xev)
198 {
199         switch (xev->type) {
200         case SelectionRequest:
201                 lyxerr[Debug::GUI] << "X requested selection." << endl;
202                 if (buffer_view_) {
203                         lyx::docstring const sel = buffer_view_->requestSelection();
204                         if (!sel.empty())
205                                 selection_.put(sel);
206                 }
207                 break;
208         case SelectionClear:
209                 lyxerr[Debug::GUI] << "Lost selection." << endl;
210                 if (buffer_view_)
211                         buffer_view_->clearSelection();
212                 break;
213         }
214         return false;
215 }
216 #endif
217
218
219 ////////////////////////////////////////////////////////////////////////
220 // Mac OSX specific stuff goes here...
221
222 #ifdef Q_WS_MACX
223 namespace{
224
225 OSErr checkAppleEventForMissingParams(const AppleEvent& theAppleEvent)
226  {
227         DescType returnedType;
228         Size actualSize;
229         OSErr err = AEGetAttributePtr(&theAppleEvent, keyMissedKeywordAttr,
230                                       typeWildCard, &returnedType, nil, 0,
231                                       &actualSize);
232         switch (err) {
233         case errAEDescNotFound:
234                 return noErr;
235         case noErr:
236                 return errAEEventNotHandled;
237         default:
238                 return err;
239         }
240  }
241
242 } // namespace
243
244 OSErr GuiApplication::handleOpenDocuments(const AppleEvent* inEvent,
245                                        AppleEvent* /*reply*/, long /*refCon*/)
246 {
247         QString s_arg;
248         AEDescList documentList;
249         OSErr err = AEGetParamDesc(inEvent, keyDirectObject, typeAEList,
250                                    &documentList);
251         if (err != noErr)
252                 return err;
253
254         err = checkAppleEventForMissingParams(*inEvent);
255         if (err == noErr) {
256                 long documentCount;
257                 err = AECountItems(&documentList, &documentCount);
258                 for (long documentIndex = 1;
259                      err == noErr && documentIndex <= documentCount;
260                      documentIndex++) {
261                         DescType returnedType;
262                         Size actualSize;
263                         AEKeyword keyword;
264                         FSRef ref;
265                         char qstr_buf[1024];
266                         err = AESizeOfNthItem(&documentList, documentIndex,
267                                               &returnedType, &actualSize);
268                         if (err == noErr) {
269                                 err = AEGetNthPtr(&documentList, documentIndex,
270                                                   typeFSRef, &keyword,
271                                                   &returnedType, (Ptr)&ref,
272                                                   sizeof(FSRef), &actualSize);
273                                 if (err == noErr) {
274                                         FSRefMakePath(&ref, (UInt8*)qstr_buf,
275                                                       1024);
276                                         s_arg=QString::fromUtf8(qstr_buf);
277 //                                      buffer_view_->workAreaDispatch(
278 //                                              FuncRequest(LFUN_FILE_OPEN,
279 //                                                          fromqstr(s_arg)));
280                                         break;
281                                 }
282                         }
283                 } // for ...
284         }
285         AEDisposeDesc(&documentList);
286
287         return err;
288 }
289
290 bool GuiApplication::macEventFilter(EventRef event)
291 {
292         if (GetEventClass(event) == kEventClassAppleEvent) {
293                 EventRecord eventrec;
294                 ConvertEventRefToEventRecord(event, &eventrec);
295                 AEProcessAppleEvent(&eventrec);
296
297                 return false;
298         }
299         return false;
300 }
301
302 #endif  // Q_WS_MACX
303
304 } // namespace frontend
305 } // namespace lyx