]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/lyx_gui.C
fix math fonts with LyX/Mac
[lyx.git] / src / frontends / qt2 / 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 "debug.h"
33
34 // Dear Lord, deliver us from Evil, aka the Qt headers
35 // Qt defines a macro 'signals' that clashes with a boost namespace.
36 // All is well if the namespace is visible first.
37 #include <boost/signal.hpp> // FIXME: Is this needed? (Lgb)
38 #include <boost/bind.hpp>
39 #include <boost/shared_ptr.hpp>
40
41 #include "QtView.h"
42 #include "lcolorcache.h"
43 #include "qfont_loader.h"
44 #include "QLImage.h"
45 #include "qt_helpers.h"
46 #include "socket_callback.h"
47
48 #ifdef Q_WS_MACX
49 #include <Carbon/Carbon.h>
50 #endif
51
52 #include <qapplication.h>
53 #include <qpaintdevicemetrics.h>
54 #include <qtranslator.h>
55 #include <qtextcodec.h>
56
57 using lyx::support::ltrim;
58
59 using lyx::frontend::QtView;
60
61 namespace os = lyx::support::os;
62
63 using boost::shared_ptr;
64
65 #ifndef CXX_GLOBAL_CSTD
66 using std::exit;
67 #endif
68
69 using std::map;
70 using std::vector;
71 using std::string;
72
73
74 extern BufferList bufferlist;
75
76 namespace {
77
78 float getDPI()
79 {
80         QWidget w;
81         QPaintDeviceMetrics pdm(&w);
82         return 0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY());
83 }
84
85 map<int, shared_ptr<socket_callback> > socket_callbacks;
86
87 } // namespace anon
88
89 // FIXME: wrong place !
90 LyXServer * lyxserver;
91 LyXServerSocket * lyxsocket;
92
93 // in QLyXKeySym.C
94 extern void initEncodings();
95
96 #ifdef Q_WS_X11
97 extern bool lyxX11EventFilter(XEvent * xev);
98 #endif
99
100 #ifdef Q_WS_MACX
101 extern bool macEventFilter(EventRef event);
102 extern pascal OSErr
103 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
104                     long /*refCon*/);
105 #endif
106
107 class LQApplication : public QApplication
108 {
109 public:
110         LQApplication(int & argc, char ** argv);
111         ~LQApplication();
112 #ifdef Q_WS_X11
113         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
114 #endif
115 #ifdef Q_WS_MACX
116         bool macEventFilter(EventRef event);
117 #endif
118 };
119
120
121 LQApplication::LQApplication(int & argc, char ** argv)
122         : QApplication(argc, argv)
123 {
124 #ifdef Q_WS_MACX
125         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
126                               NewAEEventHandlerUPP(handleOpenDocuments),
127                               0, false);
128 #endif
129 }
130
131
132 LQApplication::~LQApplication()
133 {}
134
135
136 #ifdef Q_WS_MACX
137 bool LQApplication::macEventFilter(EventRef event)
138 {
139         if (GetEventClass(event) == kEventClassAppleEvent) {
140                 EventRecord eventrec;
141                 ConvertEventRefToEventRecord(event, &eventrec);
142                 AEProcessAppleEvent(&eventrec);
143
144                 return false;
145         }
146         return false;
147 }
148 #endif
149
150
151 namespace lyx_gui {
152
153 bool use_gui = true;
154
155 void parse_init(int & argc, char * argv[])
156 {
157         // Force adding of font path _before_ QApplication is initialized
158         qfont_loader::initFontPath();
159         
160         static LQApplication app(argc, argv);
161
162 #if QT_VERSION >= 0x030200
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 #endif
179
180 #ifdef Q_WS_MACX
181         // These translations are meant to break Qt/Mac menu merging
182         // algorithm on some entries. It lists the menu names that
183         // should not be moved to the LyX menu
184         static QTranslator aqua_trans(0);
185         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
186                                              "do_not_merge_me"));
187         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
188                                              "do_not_merge_me"));
189         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
190                                              "do_not_merge_me"));
191         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
192                                              "do_not_merge_me"));
193
194         app.installTranslator(&aqua_trans);
195 #endif
196
197         using namespace lyx::graphics;
198
199         Image::newImage = boost::bind(&QLImage::newImage);
200         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
201
202         // needs to be done before reading lyxrc
203         lyxrc.dpi = getDPI();
204
205         initEncodings();
206
207         LoaderQueue::setPriority(10,100);
208 }
209
210
211 void parse_lyxrc()
212 {}
213
214
215 void start(string const & batch, vector<string> const & files)
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::slashify_path(os::getTmpDir() + "/lyxsocket"));
233
234         vector<string>::const_iterator cit = files.begin();
235         vector<string>::const_iterator end = files.end();
236         for (; cit != end; ++cit)
237                 view.view()->loadLyXFile(*cit, true);
238
239         // handle the batch commands the user asked for
240         if (!batch.empty()) {
241                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
242         }
243
244         qApp->exec();
245
246         // FIXME
247         delete lyxsocket;
248         delete lyxserver;
249         lyxserver = 0;
250 }
251
252
253 void sync_events()
254 {
255         qApp->processEvents();
256 }
257
258
259 void exit()
260 {
261         delete lyxsocket;
262         delete lyxserver;
263         lyxserver = 0;
264
265         // we cannot call qApp->exit(0) - that could return us
266         // into a static dialog return in the lyx code (for example,
267         // load autosave file QMessageBox. We have to just get the hell
268         // out.
269
270         ::exit(0);
271 }
272
273
274 FuncStatus getStatus(FuncRequest const & ev)
275 {
276         FuncStatus flag;
277         switch (ev.action) {
278         case LFUN_DIALOG_SHOW:
279                 if (ev.argument == "preamble")
280                         flag.unknown(true);
281                 break;
282         case LFUN_TOOLTIPS_TOGGLE:
283                 flag.unknown(true);
284                 break;
285         default:
286                 break;
287         }
288
289         return flag;
290 }
291
292
293 string const hexname(LColor_color col)
294 {
295         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
296 }
297
298
299 void update_color(LColor_color)
300 {
301         // FIXME: Bleh, can't we just clear them all at once ?
302         lcolorcache.clear();
303 }
304
305
306 void update_fonts()
307 {
308         fontloader.update();
309 }
310
311
312 bool font_available(LyXFont const & font)
313 {
314         return fontloader.available(font);
315 }
316
317
318 void register_socket_callback(int fd, boost::function<void()> func)
319 {
320         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
321 }
322
323
324 void unregister_socket_callback(int fd)
325 {
326         socket_callbacks.erase(fd);
327 }
328
329
330 string const roman_font_name()
331 {
332         if (!use_gui)
333                 return "serif";
334
335         QFont font;
336         font.setStyleHint(QFont::Serif);
337         font.setFamily("serif");
338
339         return fromqstr(QFontInfo(font).family());
340 }
341
342
343 string const sans_font_name()
344 {
345         if (!use_gui)
346                 return "sans";
347
348         QFont font;
349         font.setStyleHint(QFont::SansSerif);
350         font.setFamily("sans");
351
352         return fromqstr(QFontInfo(font).family());
353 }
354
355
356 string const typewriter_font_name()
357 {
358         if (!use_gui)
359                 return "monospace";
360
361         QFont font;
362         font.setStyleHint(QFont::TypeWriter);
363         font.setFamily("monospace");
364
365         return fromqstr(QFontInfo(font).family());
366 }
367
368 }; // namespace lyx_gui