]> git.lyx.org Git - features.git/blob - src/frontends/qt4/GuiApplication.C
remove some old cruft (unused OS X specific code)
[features.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/Xlib.h>
50 #endif
51
52 #include <boost/bind.hpp>
53
54 using std::string;
55 using std::endl;
56
57 // in QLyXKeySym.C
58 extern void initEncodings();
59
60 ///////////////////////////////////////////////////////////////
61 // You can find other X11 specific stuff
62 // at the end of this file...
63 ///////////////////////////////////////////////////////////////
64
65 namespace {
66
67 int getDPI()
68 {
69         QWidget w;
70         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
71 }
72
73 } // namespace anon
74
75
76 namespace lyx {
77
78 frontend::Application * createApplication(int & argc, char * argv[])
79 {
80         return new frontend::GuiApplication(argc, argv);
81 }
82
83
84 namespace frontend {
85
86 GuiApplication * guiApp;
87
88
89 GuiApplication::~GuiApplication()
90 {
91         socket_callbacks_.clear();
92 }
93
94
95 GuiApplication::GuiApplication(int & argc, char ** argv)
96         : QApplication(argc, argv), Application(argc, argv)
97 {
98         // Qt bug? setQuitOnLastWindowClosed(true); does not work
99         setQuitOnLastWindowClosed(false);
100
101 #ifdef Q_WS_X11
102         // doubleClickInterval() is 400 ms on X11 which is just too long.
103         // On Windows and Mac OS X, the operating system's value is used.
104         // On Microsoft Windows, calling this function sets the double
105         // click interval for all applications. So we don't!
106         QApplication::setDoubleClickInterval(300);
107 #endif
108
109         // install translation file for Qt built-in dialogs
110         QTranslator qt_trans;
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         BufferView * bv = currentView()->view();
281
282         switch (xev->type) {
283         case SelectionRequest:
284                 lyxerr[Debug::GUI] << "X requested selection." << endl;
285                 if (bv) {
286                         lyx::docstring const sel = bv->requestSelection();
287                         if (!sel.empty())
288                                 selection_.put(sel);
289                 }
290                 break;
291         case SelectionClear:
292                 lyxerr[Debug::GUI] << "Lost selection." << endl;
293                 if (bv)
294                         bv->clearSelection();
295                 break;
296         }
297         return false;
298 }
299 #endif
300
301
302 } // namespace frontend
303 } // namespace lyx
304
305 #include "GuiApplication_moc.cpp"