]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/lyx_gui.C
remove qPixmapFromMimeSource. This caused the inclusion of Qt3 support headers which...
[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  * \author Abdelrazak Younes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "lyx_gui.h"
16
17 // FIXME: move this stuff out again
18 #include "bufferlist.h"
19 #include "BufferView.h"
20 #include "Color.h"
21 #include "funcrequest.h"
22 #include "LColor.h"
23 #include "lyx_main.h"
24 #include "LyXAction.h"
25 #include "lyxfunc.h"
26 #include "lyxrc.h"
27 #include "lyxserver.h"
28 #include "lyxsocket.h"
29
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
39 #include "GuiView.h"
40 #include "ColorCache.h"
41 #include "FontLoader.h"
42 #include "QLImage.h"
43 #include "qt_helpers.h"
44 #include "socket_callback.h"
45 #include "Application.h"
46
47 #include <QApplication>
48 #include <QEventLoop>
49 #include <QTranslator>
50 #include <QTextCodec>
51 #include <QLocale>
52 #include <QLibraryInfo>
53
54 #include <boost/bind.hpp>
55 #include <boost/shared_ptr.hpp>
56
57
58 using lyx::support::ltrim;
59 using lyx::support::package;
60
61 using lyx::frontend::GuiImplementation;
62 using lyx::frontend::GuiView;
63 using lyx::frontend::Application;
64
65 namespace os = lyx::support::os;
66
67 using boost::shared_ptr;
68
69 #ifndef CXX_GLOBAL_CSTD
70 using std::exit;
71 #endif
72
73 using std::map;
74 using std::vector;
75 using std::string;
76
77 // FIXME: wrong place !
78 LyXServer * lyxserver;
79 LyXServerSocket * lyxsocket;
80
81 lyx::frontend::Application * theApp;
82
83 namespace {
84
85 int getDPI()
86 {
87         QWidget w;
88         return int(0.5 * (w.logicalDpiX() + w.logicalDpiY()));
89 }
90
91 map<int, shared_ptr<socket_callback> > socket_callbacks;
92
93 void cleanup()
94 {
95         delete lyxsocket;
96         lyxsocket = 0;
97         delete lyxserver;
98         lyxserver = 0;
99 }
100
101 } // namespace anon
102
103 // in QLyXKeySym.C
104 extern void initEncodings();
105
106 namespace lyx_gui {
107
108 bool use_gui = true;
109
110 int exec(int & argc, char * argv[])
111 {
112         /*
113         FIXME : Abdel 29/05/2006 (younes.a@free.fr)
114         reorganize this code. In particular make sure that this
115         advice from Qt documentation is respected:
116
117                 Since the QApplication object does so much initialization, it
118                 must be created before any other objects related to the user
119                 interface are created.
120
121         Right now this is not the case, I suspect that a number of global variables
122         contains Qt object that are initialized before the passage through
123         parse_init(). This might also explain the message displayed by Qt
124         that caused the hanging:
125
126         QObject::killTimer: timers cannot be stopped from another thread
127         */
128
129         // Force adding of font path _before_ QApplication is initialized
130         FontLoader::initFontPath();
131
132 #ifdef Q_WS_WIN
133         static Application app(argc, argv);
134 #else
135         Application app(argc, argv);
136 #endif
137
138         theApp = &app;
139
140
141         // install translation file for Qt built-in dialogs
142         // These are only installed since Qt 3.2.x
143         QTranslator qt_trans;
144         QString language_name = QString("qt_") + QLocale::system().name();
145         language_name.truncate(5);
146         if (qt_trans.load(language_name,
147                 QLibraryInfo::location(QLibraryInfo::TranslationsPath)))
148         {
149                 qApp->installTranslator(&qt_trans);
150                 // even if the language calls for RtL, don't do that
151                 qApp->setLayoutDirection(Qt::LeftToRight);
152                 lyxerr[Debug::GUI]
153                         << "Successfully installed Qt translations for locale "
154                         << fromqstr(language_name) << std::endl;
155         } else
156                 lyxerr[Debug::GUI]
157                         << "Could not find  Qt translations for locale "
158                         << fromqstr(language_name) << std::endl;
159
160 /*#ifdef Q_WS_MACX
161         // These translations are meant to break Qt/Mac menu merging
162         // algorithm on some entries. It lists the menu names that
163         // should not be moved to the LyX menu
164         QTranslator aqua_trans(0);
165         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setting", 0,
166                                              "do_not_merge_me"));
167         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Config", 0,
168                                              "do_not_merge_me"));
169         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Options", 0,
170                                              "do_not_merge_me"));
171         aqua_trans.insert(QTranslatorMessage("QMenuBar", "Setup", 0,
172                                              "do_not_merge_me"));
173
174         qApp->installTranslator(&aqua_trans);
175 #endif
176 */
177         using namespace lyx::graphics;
178
179         Image::newImage = boost::bind(&QLImage::newImage);
180         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
181
182         // needs to be done before reading lyxrc
183         lyxrc.dpi = getDPI();
184
185         LoaderQueue::setPriority(10,100);
186
187         return LyX::ref().exec2(argc, argv);
188 }
189
190
191 void parse_lyxrc()
192 {}
193
194
195 LyXView * create_view(unsigned int width, unsigned int height, int posx, int posy,
196           bool maximize)
197 {
198         // this can't be done before because it needs the Languages object
199         initEncodings();
200
201         int view_id = theApp->gui().newView(width, height);
202         GuiView & view = static_cast<GuiView &> (theApp->gui().view(view_id));
203
204         // FIXME: for now we assume that there is only one LyXView with id = 0.
205         /*int workArea_id_ =*/ theApp->gui().newWorkArea(width, height, 0);
206         //WorkArea * workArea_ = & theApp->gui().workArea(workArea_id_);
207
208         LyX::ref().addLyXView(&view);
209
210         view.init();
211
212         // only true when the -geometry option was NOT used
213         if (width != 0 && height != 0) {
214                 if (posx != -1 && posy != -1) {
215 #ifdef Q_OS_WIN32
216                         // FIXME: use only setGeoemtry when Trolltech has
217                         // fixed the qt4/X11 bug
218                         view.setGeometry(posx, posy,width, height);
219 #else
220                         view.resize(width, height);
221                         view.move(posx, posy);
222 #endif
223                 } else {
224                         view.resize(width, height);
225                 }
226
227                 if (maximize)
228                         view.setWindowState(Qt::WindowMaximized);
229         }
230
231         view.show();
232
233         return &view;
234 }
235
236
237 int start(LyXView * view, string const & batch)
238 {
239         // FIXME: some code below needs moving
240
241         lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
242         lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
243                           os::internal_path(package().temp_dir() + "/lyxsocket"));
244
245         // handle the batch commands the user asked for
246         if (!batch.empty()) {
247                 view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
248         }
249
250         int const status = qApp->exec();
251
252         // FIXME
253         cleanup();
254         return status;
255 }
256
257
258 void sync_events()
259 {
260         // This is the ONLY place where processEvents may be called.
261         // During screen update/ redraw, this method is disabled to
262         // prevent keyboard events being handed to the LyX core, where
263         // they could cause re-entrant calls to screen update.
264         qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
265 }
266
267
268 void exit(int status)
269 {
270         cleanup();
271         QApplication::exit(status);
272 }
273
274
275 FuncStatus getStatus(FuncRequest const & ev)
276 {
277         FuncStatus flag;
278         switch (ev.action) {
279         case LFUN_TOOLTIPS_TOGGLE:
280                 flag.unknown(true);
281                 break;
282         default:
283                 break;
284         }
285
286         return flag;
287 }
288
289
290 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
291 {
292         QColor const & qcol = lcolorcache.get(col);
293         if (!qcol.isValid()) {
294                 rgbcol.r = 0;
295                 rgbcol.g = 0;
296                 rgbcol.b = 0;
297                 return false;
298         }
299         rgbcol.r = qcol.red();
300         rgbcol.g = qcol.green();
301         rgbcol.b = qcol.blue();
302         return true;
303 }
304
305
306 string const hexname(LColor_color col)
307 {
308         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
309 }
310
311
312 void update_color(LColor_color)
313 {
314         // FIXME: Bleh, can't we just clear them all at once ?
315         lcolorcache.clear();
316 }
317
318
319 void update_fonts()
320 {
321         theApp->fontLoader().update();
322 }
323
324
325 bool font_available(LyXFont const & font)
326 {
327         return theApp->fontLoader().available(font);
328 }
329
330
331 void register_socket_callback(int fd, boost::function<void()> func)
332 {
333         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
334 }
335
336
337 void unregister_socket_callback(int fd)
338 {
339         socket_callbacks.erase(fd);
340 }
341
342
343 string const roman_font_name()
344 {
345         if (!use_gui)
346                 return "serif";
347
348         QFont font;
349         font.setStyleHint(QFont::Serif);
350         font.setFamily("serif");
351
352         return fromqstr(QFontInfo(font).family());
353 }
354
355
356 string const sans_font_name()
357 {
358         if (!use_gui)
359                 return "sans";
360
361         QFont font;
362         font.setStyleHint(QFont::SansSerif);
363         font.setFamily("sans");
364
365         return fromqstr(QFontInfo(font).family());
366 }
367
368
369 string const typewriter_font_name()
370 {
371         if (!use_gui)
372                 return "monospace";
373
374         QFont font;
375         font.setStyleHint(QFont::TypeWriter);
376         font.setFamily("monospace");
377
378         return fromqstr(QFontInfo(font).family());
379 }
380
381 }; // namespace lyx_gui