]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/lyx_gui.C
Abdelrazak Younes: Compile fixes for the Mac (ChangeLog follows)
[lyx.git] / src / frontends / qt4 / lyx_gui.C
1 /**
2  * \file qt2/lyx_gui.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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "lyx_gui.h"
15
16 // FIXME: move this stuff out again
17 #include "bufferlist.h"
18 #include "BufferView.h"
19 #include "funcrequest.h"
20 #include "LColor.h"
21 #include "lyx_main.h"
22 #include "LyXAction.h"
23 #include "lyxfunc.h"
24 #include "lyxrc.h"
25 #include "lyxserver.h"
26 #include "lyxsocket.h"
27
28 #include "graphics/LoaderQueue.h"
29
30 #include "support/lstrings.h"
31 #include "support/os.h"
32 #include "support/package.h"
33 #include "debug.h"
34
35 // Dear Lord, deliver us from Evil, aka the Qt headers
36 // Qt defines a macro 'signals' that clashes with a boost namespace.
37 // All is well if the namespace is visible first.
38 #include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
39 #include <boost/bind.hpp>
40 #include <boost/shared_ptr.hpp>
41
42 #include "QtView.h"
43 #include "lcolorcache.h"
44 #include "qfont_loader.h"
45 #include "QLImage.h"
46 #include "qt_helpers.h"
47 #include "socket_callback.h"
48
49 #ifdef Q_WS_MACX
50 #include <Carbon/Carbon.h>
51 #endif
52
53 #include <QApplication>
54 #include <QEventLoop>
55 #include <QTranslator>
56 #include <QTextCodec>
57
58 using lyx::support::ltrim;
59 using lyx::support::package;
60
61 using lyx::frontend::QtView;
62
63 namespace os = lyx::support::os;
64
65 using boost::shared_ptr;
66
67 #ifndef CXX_GLOBAL_CSTD
68 using std::exit;
69 #endif
70
71 using std::map;
72 using std::vector;
73 using std::string;
74
75
76 extern BufferList bufferlist;
77
78 namespace {
79
80 int getDPI()
81 {
82         QWidget w;
83         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
84 }
85
86 map<int, shared_ptr<socket_callback> > socket_callbacks;
87
88 } // namespace anon
89
90 // FIXME: wrong place !
91 LyXServer * lyxserver;
92 LyXServerSocket * lyxsocket;
93
94 // in QLyXKeySym.C
95 extern void initEncodings();
96
97 #ifdef Q_WS_X11
98 extern bool lyxX11EventFilter(XEvent * xev);
99 #endif
100
101 #ifdef Q_WS_MACX
102 extern bool macEventFilter(EventRef event);
103 extern pascal OSErr
104 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
105                     long /*refCon*/);
106 #endif
107
108 class LQApplication : public QApplication
109 {
110 public:
111         LQApplication(int & argc, char ** argv);
112         ~LQApplication();
113 #ifdef Q_WS_X11
114         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
115 #endif
116 #ifdef Q_WS_MACX
117         bool macEventFilter(EventRef event);
118 #endif
119 };
120
121
122 LQApplication::LQApplication(int & argc, char ** argv)
123         : QApplication(argc, argv)
124 {
125 #ifdef Q_WS_MACX
126         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
127                               NewAEEventHandlerUPP(handleOpenDocuments),
128                               0, false);
129 #endif
130 }
131
132
133 LQApplication::~LQApplication()
134 {}
135
136
137 #ifdef Q_WS_MACX
138 bool LQApplication::macEventFilter(EventRef event)
139 {
140         if (GetEventClass(event) == kEventClassAppleEvent) {
141                 EventRecord eventrec;
142                 ConvertEventRefToEventRecord(event, &eventrec);
143                 AEProcessAppleEvent(&eventrec);
144
145                 return false;
146         }
147         return false;
148 }
149 #endif
150
151
152 namespace lyx_gui {
153
154 bool use_gui = true;
155
156 void parse_init(int & argc, char * argv[])
157 {
158         // Force adding of font path _before_ QApplication is initialized
159         FontLoader::initFontPath();
160
161         static LQApplication app(argc, argv);
162
163         // install translation file for Qt built-in dialogs
164         // These are only installed since Qt 3.2.x
165         static QTranslator qt_trans(0);
166         if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
167                           qInstallPathTranslations())) {
168                 app.installTranslator(&qt_trans);
169                 // even if the language calls for RtL, don't do that
170                 app.setReverseLayout(false);
171                 lyxerr[Debug::GUI]
172                         << "Successfully installed Qt translations for locale "
173                         << QTextCodec::locale() << std::endl;
174         } else
175                 lyxerr[Debug::GUI]
176                         << "Could not find  Qt translations for locale "
177                         << QTextCodec::locale() << std::endl;
178
179 /*#ifdef Q_WS_MACX
180         // These translations are meant to break Qt/Mac menu merging
181         // algorithm on some entries. It lists the menu names that
182         // should not be moved to the LyX menu
183         static QTranslator aqua_trans(0);
184         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
185                                              "do_not_merge_me"));
186         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
187                                              "do_not_merge_me"));
188         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
189                                              "do_not_merge_me"));
190         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
191                                              "do_not_merge_me"));
192
193         app.installTranslator(&aqua_trans);
194 #endif
195 */
196         using namespace lyx::graphics;
197
198         Image::newImage = boost::bind(&QLImage::newImage);
199         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
200
201         // needs to be done before reading lyxrc
202         lyxrc.dpi = getDPI();
203
204         LoaderQueue::setPriority(10,100);
205 }
206
207
208 void parse_lyxrc()
209 {}
210
211
212 void start(string const & batch, vector<string> const & files)
213 {
214         // this can't be done before because it needs the Languages object
215         initEncodings();
216
217         // initial geometry
218         unsigned int width = 690;
219         unsigned int height = 510;
220
221         boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
222         LyX::ref().addLyXView(view_ptr);
223
224         QtView & view = *view_ptr.get();
225         view.show();
226         view.init();
227
228         // FIXME: some code below needs moving
229
230         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
231         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
232                           os::internal_path(package().temp_dir() + "/lyxsocket"));
233
234         for_each(files.begin(), files.end(),
235                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
236
237         // handle the batch commands the user asked for
238         if (!batch.empty()) {
239                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
240         }
241
242         qApp->exec();
243
244         // FIXME
245         delete lyxsocket;
246         delete lyxserver;
247         lyxserver = 0;
248 }
249
250
251 void sync_events()
252 {
253         // This is the ONLY place where processEvents may be called.
254         // During screen update/ redraw, this method is disabled to
255         // prevent keyboard events being handed to the LyX core, where
256         // they could cause re-entrant calls to screen update.
257 #if QT_VERSION >= 0x030100
258         qApp->processEvents(QEventLoop::ExcludeUserInput);
259 #endif
260 }
261
262
263 void exit()
264 {
265         delete lyxsocket;
266         delete lyxserver;
267         lyxserver = 0;
268
269         // we cannot call qApp->exit(0) - that could return us
270         // into a static dialog return in the lyx code (for example,
271         // load autosave file QMessageBox. We have to just get the hell
272         // out.
273
274         ::exit(0);
275 }
276
277
278 FuncStatus getStatus(FuncRequest const & ev)
279 {
280         FuncStatus flag;
281         switch (ev.action) {
282         case LFUN_DIALOG_SHOW:
283                 if (ev.argument == "preamble")
284                         flag.unknown(true);
285                 break;
286         case LFUN_TOOLTIPS_TOGGLE:
287                 flag.unknown(true);
288                 break;
289         default:
290                 break;
291         }
292
293         return flag;
294 }
295
296
297 string const hexname(LColor_color col)
298 {
299         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
300 }
301
302
303 void update_color(LColor_color)
304 {
305         // FIXME: Bleh, can't we just clear them all at once ?
306         lcolorcache.clear();
307 }
308
309
310 void update_fonts()
311 {
312         fontloader.update();
313 }
314
315
316 bool font_available(LyXFont const & font)
317 {
318         return fontloader.available(font);
319 }
320
321
322 void register_socket_callback(int fd, boost::function<void()> func)
323 {
324         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
325 }
326
327
328 void unregister_socket_callback(int fd)
329 {
330         socket_callbacks.erase(fd);
331 }
332
333
334 string const roman_font_name()
335 {
336         if (!use_gui)
337                 return "serif";
338
339         QFont font;
340         font.setStyleHint(QFont::Serif);
341         font.setFamily("serif");
342
343         return fromqstr(QFontInfo(font).family());
344 }
345
346
347 string const sans_font_name()
348 {
349         if (!use_gui)
350                 return "sans";
351
352         QFont font;
353         font.setStyleHint(QFont::SansSerif);
354         font.setFamily("sans");
355
356         return fromqstr(QFontInfo(font).family());
357 }
358
359
360 string const typewriter_font_name()
361 {
362         if (!use_gui)
363                 return "monospace";
364
365         QFont font;
366         font.setStyleHint(QFont::TypeWriter);
367         font.setFamily("monospace");
368
369         return fromqstr(QFontInfo(font).family());
370 }
371
372 }; // namespace lyx_gui