]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/lyx_gui.C
get rid of broken_header.h and some unneeded tests
[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         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         qApp->processEvents();
254 }
255
256
257 void exit()
258 {
259         delete lyxsocket;
260         delete lyxserver;
261         lyxserver = 0;
262
263         // we cannot call qApp->exit(0) - that could return us
264         // into a static dialog return in the lyx code (for example,
265         // load autosave file QMessageBox. We have to just get the hell
266         // out.
267
268         ::exit(0);
269 }
270
271
272 FuncStatus getStatus(FuncRequest const & ev)
273 {
274         FuncStatus flag;
275         switch (ev.action) {
276         case LFUN_DIALOG_SHOW:
277                 if (ev.argument == "preamble")
278                         flag.unknown(true);
279                 break;
280         case LFUN_TOOLTIPS_TOGGLE:
281                 flag.unknown(true);
282                 break;
283         default:
284                 break;
285         }
286
287         return flag;
288 }
289
290
291 string const hexname(LColor_color col)
292 {
293         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
294 }
295
296
297 void update_color(LColor_color)
298 {
299         // FIXME: Bleh, can't we just clear them all at once ?
300         lcolorcache.clear();
301 }
302
303
304 void update_fonts()
305 {
306         fontloader.update();
307 }
308
309
310 bool font_available(LyXFont const & font)
311 {
312         return fontloader.available(font);
313 }
314
315
316 void register_socket_callback(int fd, boost::function<void()> func)
317 {
318         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
319 }
320
321
322 void unregister_socket_callback(int fd)
323 {
324         socket_callbacks.erase(fd);
325 }
326
327
328 string const roman_font_name()
329 {
330         if (!use_gui)
331                 return "serif";
332
333         QFont font;
334         font.setStyleHint(QFont::Serif);
335         font.setFamily("serif");
336
337         return fromqstr(QFontInfo(font).family());
338 }
339
340
341 string const sans_font_name()
342 {
343         if (!use_gui)
344                 return "sans";
345
346         QFont font;
347         font.setStyleHint(QFont::SansSerif);
348         font.setFamily("sans");
349
350         return fromqstr(QFontInfo(font).family());
351 }
352
353
354 string const typewriter_font_name()
355 {
356         if (!use_gui)
357                 return "monospace";
358
359         QFont font;
360         font.setStyleHint(QFont::TypeWriter);
361         font.setFamily("monospace");
362
363         return fromqstr(QFontInfo(font).family());
364 }
365
366 }; // namespace lyx_gui