]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.cpp
* LyX::addFileToLoad(): new method for double-clicked file icons on MacOS.
[lyx.git] / src / frontends / qt4 / GuiApplication.cpp
1 /**
2  * \file qt4/GuiApplication.cpp
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 "GuiApplication.h"
16
17 #include "qt_helpers.h"
18 #include "QLImage.h"
19 #include "socket_callback.h"
20
21 #include "frontends/LyXView.h"
22
23 #include "graphics/LoaderQueue.h"
24
25 #include "support/FileName.h"
26 #include "support/lstrings.h"
27 #include "support/os.h"
28 #include "support/Package.h"
29
30 #include "BufferView.h"
31 #include "Color.h"
32 #include "debug.h"
33 #include "FuncRequest.h"
34 #include "LyX.h"
35 #include "LyXFunc.h"
36 #include "LyXRC.h"
37
38 #include <QApplication>
39 #include <QClipboard>
40 #include <QEventLoop>
41 #include <QFileOpenEvent>
42 #include <QLocale>
43 #include <QLibraryInfo>
44 #include <QTextCodec>
45 #include <QTimer>
46 #include <QTranslator>
47 #include <QWidget>
48
49 #ifdef Q_WS_X11
50 #include <X11/Xatom.h>
51 #include <X11/Xlib.h>
52 #endif
53
54 #include <boost/bind.hpp>
55
56 #include <exception>
57
58 using std::string;
59 using std::endl;
60
61 ///////////////////////////////////////////////////////////////
62 // You can find other X11 specific stuff
63 // at the end of this file...
64 ///////////////////////////////////////////////////////////////
65
66 namespace {
67
68 int getDPI()
69 {
70         QWidget w;
71         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
72 }
73
74 } // namespace anon
75
76
77 namespace lyx {
78
79 using support::FileName;
80
81 frontend::Application * createApplication(int & argc, char * argv[])
82 {
83         return new frontend::GuiApplication(argc, argv);
84 }
85
86
87 namespace frontend {
88
89 GuiApplication * guiApp;
90
91
92 GuiApplication::~GuiApplication()
93 {
94         socket_callbacks_.clear();
95 }
96
97
98 GuiApplication::GuiApplication(int & argc, char ** argv)
99         : QApplication(argc, argv), Application(argc, argv)
100 {
101         // Qt bug? setQuitOnLastWindowClosed(true); does not work
102         setQuitOnLastWindowClosed(false);
103
104 #ifdef Q_WS_X11
105         // doubleClickInterval() is 400 ms on X11 which is just too long.
106         // On Windows and Mac OS X, the operating system's value is used.
107         // On Microsoft Windows, calling this function sets the double
108         // click interval for all applications. So we don't!
109         QApplication::setDoubleClickInterval(300);
110 #endif
111
112         // install translation file for Qt built-in dialogs
113         QString language_name = QString("qt_") + QLocale::system().name();
114         language_name.truncate(5);
115         if (qt_trans_.load(language_name,
116                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
117         {
118                 qApp->installTranslator(&qt_trans_);
119                 // even if the language calls for RtL, don't do that
120                 qApp->setLayoutDirection(Qt::LeftToRight);
121                 LYXERR(Debug::GUI)
122                         << "Successfully installed Qt translations for locale "
123                         << fromqstr(language_name) << std::endl;
124         } else
125                 LYXERR(Debug::GUI)
126                         << "Could not find  Qt translations for locale "
127                         << fromqstr(language_name) << std::endl;
128
129         using namespace lyx::graphics;
130
131         Image::newImage = boost::bind(&QLImage::newImage);
132         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
133
134         // needs to be done before reading lyxrc
135         lyxrc.dpi = getDPI();
136
137         LoaderQueue::setPriority(10,100);
138
139         guiApp = this;
140 }
141
142
143 Clipboard& GuiApplication::clipboard()
144 {
145         return clipboard_;
146 }
147
148
149 Selection& GuiApplication::selection()
150 {
151         return selection_;
152 }
153
154
155 int const GuiApplication::exec()
156 {
157         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
158         return QApplication::exec();
159 }
160
161
162 void GuiApplication::exit(int status)
163 {
164         QApplication::exit(status);
165 }
166
167
168 void GuiApplication::execBatchCommands()
169 {
170         LyX::ref().execBatchCommands();
171 }
172
173
174 string const GuiApplication::romanFontName()
175 {
176         QFont font;
177         font.setKerning(false);
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.setKerning(false);
189         font.setStyleHint(QFont::SansSerif);
190         font.setFamily("sans");
191
192         return fromqstr(QFontInfo(font).family());
193 }
194
195
196 string const GuiApplication::typewriterFontName()
197 {
198         QFont font;
199         font.setKerning(false);
200         font.setStyleHint(QFont::TypeWriter);
201         font.setFamily("monospace");
202
203         return fromqstr(QFontInfo(font).family());
204 }
205
206
207 bool GuiApplication::event(QEvent * e)
208 {
209         switch(e->type()) {
210         case QEvent::FileOpen: {
211                 // Open a file; this happens only on Mac OS X for now
212                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
213
214                 if (!currentView() || !currentView()->view())
215                         // The application is not properly initialized yet.
216                         // So we acknowledge the event and delay the file opening
217                         // until LyX is ready.
218                         // FIXME UNICODE: FileName accept an utf8 encoded string.
219                         LyX::ref().addFileToLoad(FileName(fromqstr(foe->file())));
220                 else
221                         lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
222                                 qstring_to_ucs4(foe->file())));
223
224                 e->accept();
225                 return true;
226         }
227         default:
228                 return QApplication::event(e);
229         }
230 }
231
232
233 bool GuiApplication::notify(QObject * receiver, QEvent * event)
234 {
235         bool return_value;
236         try {
237                 return_value = QApplication::notify(receiver, event);
238         }
239         catch (std::exception  const & e) {
240                 lyxerr << "Caught \"normal\" exception: " << e.what() << endl;
241                 LyX::cref().emergencyCleanup();
242                 abort();
243         }
244         catch (...) {
245                 lyxerr << "Caught some really weird exception..." << endl;
246                 LyX::cref().emergencyCleanup();
247                 abort();
248         }
249
250         return return_value;
251 }
252
253
254 void GuiApplication::syncEvents()
255 {
256         // This is the ONLY place where processEvents may be called.
257         // During screen update/ redraw, this method is disabled to
258         // prevent keyboard events being handed to the LyX core, where
259         // they could cause re-entrant calls to screen update.
260         processEvents(QEventLoop::ExcludeUserInputEvents);
261 }
262
263
264 bool GuiApplication::getRgbColor(Color_color col,
265         RGBColor & rgbcol)
266 {
267         QColor const & qcol = color_cache_.get(col);
268         if (!qcol.isValid()) {
269                 rgbcol.r = 0;
270                 rgbcol.g = 0;
271                 rgbcol.b = 0;
272                 return false;
273         }
274         rgbcol.r = qcol.red();
275         rgbcol.g = qcol.green();
276         rgbcol.b = qcol.blue();
277         return true;
278 }
279
280
281 string const GuiApplication::hexName(Color_color col)
282 {
283         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
284 }
285
286
287 void GuiApplication::updateColor(Color_color)
288 {
289         // FIXME: Bleh, can't we just clear them all at once ?
290         color_cache_.clear();
291 }
292
293
294 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
295 {
296         socket_callbacks_[fd] =
297                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
298 }
299
300
301 void GuiApplication::unregisterSocketCallback(int fd)
302 {
303         socket_callbacks_.erase(fd);
304 }
305
306 ////////////////////////////////////////////////////////////////////////
307 // X11 specific stuff goes here...
308 #ifdef Q_WS_X11
309 bool GuiApplication::x11EventFilter(XEvent * xev)
310 {
311         if (!currentView())
312                 return false;
313
314         switch (xev->type) {
315         case SelectionRequest: {
316                 if (xev->xselectionrequest.selection != XA_PRIMARY)
317                         break;
318                 LYXERR(Debug::GUI) << "X requested selection." << endl;
319                 BufferView * bv = currentView()->view();
320                 if (bv) {
321                         docstring const sel = bv->requestSelection();
322                         if (!sel.empty())
323                                 selection_.put(sel);
324                 }
325                 break;
326         }
327         case SelectionClear: {
328                 if (xev->xselectionclear.selection != XA_PRIMARY)
329                         break;
330                 LYXERR(Debug::GUI) << "Lost selection." << endl;
331                 BufferView * bv = currentView()->view();
332                 if (bv)
333                         bv->clearSelection();
334                 break;
335         }
336         }
337         return false;
338 }
339 #endif
340
341
342 } // namespace frontend
343 } // namespace lyx
344
345 #include "GuiApplication_moc.cpp"