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