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