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