]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/GuiApplication.C
* LyXView::updateInset(): schedule a redraw instead of redraw immediately.
[lyx.git] / src / frontends / qt4 / GuiApplication.C
1 /**
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.
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/lstrings.h"
26 #include "support/os.h"
27 #include "support/package.h"
28
29 #include "BufferView.h"
30 #include "Color.h"
31 #include "debug.h"
32 #include "funcrequest.h"
33 #include "lyx_main.h"
34 #include "lyxfunc.h"
35 #include "lyxrc.h"
36
37 #include <QApplication>
38 #include <QClipboard>
39 #include <QEventLoop>
40 #include <QFileOpenEvent>
41 #include <QLocale>
42 #include <QLibraryInfo>
43 #include <QTextCodec>
44 #include <QTimer>
45 #include <QTranslator>
46 #include <QWidget>
47
48 #ifdef Q_WS_X11
49 #include <X11/Xatom.h>
50 #include <X11/Xlib.h>
51 #endif
52
53 #include <boost/bind.hpp>
54
55 using std::string;
56 using std::endl;
57
58 // in QLyXKeySym.C
59 extern void initEncodings();
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 frontend::Application * createApplication(int & argc, char * argv[])
80 {
81         return new frontend::GuiApplication(argc, argv);
82 }
83
84
85 namespace frontend {
86
87 GuiApplication * guiApp;
88
89
90 GuiApplication::~GuiApplication()
91 {
92         socket_callbacks_.clear();
93 }
94
95
96 GuiApplication::GuiApplication(int & argc, char ** argv)
97         : QApplication(argc, argv), Application(argc, argv)
98 {
99         // Qt bug? setQuitOnLastWindowClosed(true); does not work
100         setQuitOnLastWindowClosed(false);
101
102 #ifdef Q_WS_X11
103         // doubleClickInterval() is 400 ms on X11 which is just too long.
104         // On Windows and Mac OS X, the operating system's value is used.
105         // On Microsoft Windows, calling this function sets the double
106         // click interval for all applications. So we don't!
107         QApplication::setDoubleClickInterval(300);
108 #endif
109
110         // install translation file for Qt built-in dialogs
111         QString language_name = QString("qt_") + QLocale::system().name();
112         language_name.truncate(5);
113         if (qt_trans_.load(language_name,
114                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
115         {
116                 qApp->installTranslator(&qt_trans_);
117                 // even if the language calls for RtL, don't do that
118                 qApp->setLayoutDirection(Qt::LeftToRight);
119                 lyxerr[Debug::GUI]
120                         << "Successfully installed Qt translations for locale "
121                         << fromqstr(language_name) << std::endl;
122         } else
123                 lyxerr[Debug::GUI]
124                         << "Could not find  Qt translations for locale "
125                         << fromqstr(language_name) << std::endl;
126
127         using namespace lyx::graphics;
128
129         Image::newImage = boost::bind(&QLImage::newImage);
130         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
131
132         // needs to be done before reading lyxrc
133         lyxrc.dpi = getDPI();
134
135         LoaderQueue::setPriority(10,100);
136
137         guiApp = this;
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         QTimer::singleShot(1, this, SLOT(execBatchCommands()));
155         return QApplication::exec();
156 }
157
158
159 void GuiApplication::exit(int status)
160 {
161         QApplication::exit(status);
162 }
163
164
165 void GuiApplication::execBatchCommands()
166 {
167         LyX::ref().execBatchCommands();
168 }
169
170
171 string const GuiApplication::romanFontName()
172 {
173         QFont font;
174         font.setKerning(false);
175         font.setStyleHint(QFont::Serif);
176         font.setFamily("serif");
177
178         return fromqstr(QFontInfo(font).family());
179 }
180
181
182 string const GuiApplication::sansFontName()
183 {
184         QFont font;
185         font.setKerning(false);
186         font.setStyleHint(QFont::SansSerif);
187         font.setFamily("sans");
188
189         return fromqstr(QFontInfo(font).family());
190 }
191
192
193 string const GuiApplication::typewriterFontName()
194 {
195         QFont font;
196         font.setKerning(false);
197         font.setStyleHint(QFont::TypeWriter);
198         font.setFamily("monospace");
199
200         return fromqstr(QFontInfo(font).family());
201 }
202
203
204 bool GuiApplication::event(QEvent * e)
205 {
206         switch(e->type()) {
207         case QEvent::FileOpen: {
208                 // Open a file; this happens only on Mac OS X for now
209                 QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
210                 lyx::dispatch(FuncRequest(LFUN_FILE_OPEN,
211                                           fromqstr(foe->file())));
212                 return true;
213         }
214         default:
215                 return false;
216         }
217 }
218
219
220 void GuiApplication::syncEvents()
221 {
222         // This is the ONLY place where processEvents may be called.
223         // During screen update/ redraw, this method is disabled to
224         // prevent keyboard events being handed to the LyX core, where
225         // they could cause re-entrant calls to screen update.
226         processEvents(QEventLoop::ExcludeUserInputEvents);
227 }
228
229
230 bool GuiApplication::getRgbColor(LColor_color col,
231         lyx::RGBColor & rgbcol)
232 {
233         QColor const & qcol = color_cache_.get(col);
234         if (!qcol.isValid()) {
235                 rgbcol.r = 0;
236                 rgbcol.g = 0;
237                 rgbcol.b = 0;
238                 return false;
239         }
240         rgbcol.r = qcol.red();
241         rgbcol.g = qcol.green();
242         rgbcol.b = qcol.blue();
243         return true;
244 }
245
246
247 string const GuiApplication::hexName(LColor_color col)
248 {
249         return lyx::support::ltrim(fromqstr(color_cache_.get(col).name()), "#");
250 }
251
252
253 void GuiApplication::updateColor(LColor_color)
254 {
255         // FIXME: Bleh, can't we just clear them all at once ?
256         color_cache_.clear();
257 }
258
259
260 void GuiApplication::registerSocketCallback(int fd, boost::function<void()> func)
261 {
262         socket_callbacks_[fd] =
263                 boost::shared_ptr<socket_callback>(new socket_callback(fd, func));
264 }
265
266
267 void GuiApplication::unregisterSocketCallback(int fd)
268 {
269         socket_callbacks_.erase(fd);
270 }
271
272 ////////////////////////////////////////////////////////////////////////
273 // X11 specific stuff goes here...
274 #ifdef Q_WS_X11
275 bool GuiApplication::x11EventFilter(XEvent * xev)
276 {
277         if (!currentView())
278                 return false;
279
280         switch (xev->type) {
281         case SelectionRequest: {
282                 if (xev->xselectionrequest.selection != XA_PRIMARY)
283                         break;
284                 lyxerr[Debug::GUI] << "X requested selection." << endl;
285                 BufferView * bv = currentView()->view();
286                 if (bv) {
287                         docstring const sel = bv->requestSelection();
288                         if (!sel.empty())
289                                 selection_.put(sel);
290                 }
291                 break;
292         }
293         case SelectionClear: {
294                 if (xev->xselectionclear.selection != XA_PRIMARY)
295                         break;
296                 lyxerr[Debug::GUI] << "Lost selection." << endl;
297                 BufferView * bv = currentView()->view();
298                 if (bv)
299                         bv->clearSelection();
300                 break;
301         }
302         }
303         return false;
304 }
305 #endif
306
307
308 } // namespace frontend
309 } // namespace lyx
310
311 #include "GuiApplication_moc.cpp"