]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/lyx_gui.C
some tabular fixes for the problems reported by Helge
[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 "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.h>
54 #include <qpaintdevicemetrics.h>
55 #include <qtranslator.h>
56 #include <qtextcodec.h>
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         QPaintDeviceMetrics pdm(&w);
84         return int(0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY()));
85 }
86
87 map<int, shared_ptr<socket_callback> > socket_callbacks;
88
89 } // namespace anon
90
91 // FIXME: wrong place !
92 LyXServer * lyxserver;
93 LyXServerSocket * lyxsocket;
94
95 // in QLyXKeySym.C
96 extern void initEncodings();
97
98 #ifdef Q_WS_X11
99 extern bool lyxX11EventFilter(XEvent * xev);
100 #endif
101
102 #ifdef Q_WS_MACX
103 extern bool macEventFilter(EventRef event);
104 extern pascal OSErr
105 handleOpenDocuments(const AppleEvent* inEvent, AppleEvent* /*reply*/,
106                     long /*refCon*/);
107 #endif
108
109 class LQApplication : public QApplication
110 {
111 public:
112         LQApplication(int & argc, char ** argv);
113         ~LQApplication();
114 #ifdef Q_WS_X11
115         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
116 #endif
117 #ifdef Q_WS_MACX
118         bool macEventFilter(EventRef event);
119 #endif
120 };
121
122
123 LQApplication::LQApplication(int & argc, char ** argv)
124         : QApplication(argc, argv)
125 {
126 #ifdef Q_WS_MACX
127         AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
128                               NewAEEventHandlerUPP(handleOpenDocuments),
129                               0, false);
130 #endif
131 }
132
133
134 LQApplication::~LQApplication()
135 {}
136
137
138 #ifdef Q_WS_MACX
139 bool LQApplication::macEventFilter(EventRef event)
140 {
141         if (GetEventClass(event) == kEventClassAppleEvent) {
142                 EventRecord eventrec;
143                 ConvertEventRefToEventRecord(event, &eventrec);
144                 AEProcessAppleEvent(&eventrec);
145
146                 return false;
147         }
148         return false;
149 }
150 #endif
151
152
153 namespace lyx_gui {
154
155 bool use_gui = true;
156
157 void parse_init(int & argc, char * argv[])
158 {
159         // Force adding of font path _before_ QApplication is initialized
160         qfont_loader::initFontPath();
161
162         static LQApplication app(argc, argv);
163
164 #if QT_VERSION >= 0x030200
165         // install translation file for Qt built-in dialogs
166         // These are only installed since Qt 3.2.x
167         static QTranslator qt_trans(0);
168         if (qt_trans.load(QString("qt_") + QTextCodec::locale(),
169                           qInstallPathTranslations())) {
170                 app.installTranslator(&qt_trans);
171                 // even if the language calls for RtL, don't do that
172                 app.setReverseLayout(false);
173                 lyxerr[Debug::GUI]
174                         << "Successfully installed Qt translations for locale "
175                         << QTextCodec::locale() << std::endl;
176         } else
177                 lyxerr[Debug::GUI]
178                         << "Could not find  Qt translations for locale "
179                         << QTextCodec::locale() << std::endl;
180 #endif
181
182 #ifdef Q_WS_MACX
183         // These translations are meant to break Qt/Mac menu merging
184         // algorithm on some entries. It lists the menu names that
185         // should not be moved to the LyX menu
186         static QTranslator aqua_trans(0);
187         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
188                                              "do_not_merge_me"));
189         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
190                                              "do_not_merge_me"));
191         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
192                                              "do_not_merge_me"));
193         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
194                                              "do_not_merge_me"));
195
196         app.installTranslator(&aqua_trans);
197 #endif
198
199         using namespace lyx::graphics;
200
201         Image::newImage = boost::bind(&QLImage::newImage);
202         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
203
204         // needs to be done before reading lyxrc
205         lyxrc.dpi = getDPI();
206
207         initEncodings();
208
209         LoaderQueue::setPriority(10,100);
210 }
211
212
213 void parse_lyxrc()
214 {}
215
216
217 void start(string const & batch, vector<string> const & files)
218 {
219         // initial geometry
220         unsigned int width = 690;
221         unsigned int height = 510;
222
223         boost::shared_ptr<QtView> view_ptr(new QtView(width, height));
224         LyX::ref().addLyXView(view_ptr);
225
226         QtView & view = *view_ptr.get();
227         view.show();
228         view.init();
229
230         // FIXME: some code below needs moving
231
232         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
233         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
234                           os::internal_path(package().temp_dir() + "/lyxsocket"));
235
236         for_each(files.begin(), files.end(),
237                  bind(&BufferView::loadLyXFile, view.view(), _1, 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