]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/lyx_gui.C
838bea46f13186fc1208a5b1676440cb2c4ad57d
[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 "Color.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
26
27 #include "support/lstrings.h"
28
29 #include "GuiView.h"
30 #include "QLImage.h"
31 #include "qt_helpers.h"
32 #include "socket_callback.h"
33 #include "GuiApplication.h"
34
35 #include <QApplication>
36 #include <QEventLoop>
37 #include <QTranslator>
38 #include <QTextCodec>
39 #include <QLocale>
40 #include <QLibraryInfo>
41
42 #include <boost/bind.hpp>
43 #include <boost/shared_ptr.hpp>
44
45
46 using lyx::support::ltrim;
47
48 using lyx::frontend::GuiImplementation;
49 using lyx::frontend::GuiView;
50 using lyx::frontend::GuiApplication;
51
52 using boost::shared_ptr;
53
54 #ifndef CXX_GLOBAL_CSTD
55 using std::exit;
56 #endif
57
58 using std::map;
59 using std::vector;
60 using std::string;
61
62 lyx::frontend::GuiApplication * guiApp;
63
64 namespace {
65
66 map<int, shared_ptr<socket_callback> > socket_callbacks;
67
68 } // namespace anon
69
70 namespace lyx_gui {
71
72 int exec(int & argc, char * argv[])
73 {
74         /*
75         FIXME : Abdel 29/05/2006 (younes.a@free.fr)
76         reorganize this code. In particular make sure that this
77         advice from Qt documentation is respected:
78
79                 Since the QApplication object does so much initialization, it
80                 must be created before any other objects related to the user
81                 interface are created.
82
83         Right now this is not the case, I suspect that a number of global variables
84         contains Qt object that are initialized before the passage through
85         parse_init(). This might also explain the message displayed by Qt
86         that caused the hanging:
87
88         QObject::killTimer: timers cannot be stopped from another thread
89
90         I hope that the problem will disappear automagically when we get rid of
91         lyx_gui entirely, thus using theApp directly throughout the code for LyXFunc,
92         Clipboard and Selection access.
93         */
94
95 #if defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN)
96         static GuiApplication app(argc, argv);
97 #else
98         GuiApplication app(argc, argv);
99 #endif
100
101         guiApp = &app;
102         theApp = guiApp;
103
104         return LyX::ref().exec2(argc, argv);
105 }
106
107
108 void sync_events()
109 {
110         // This is the ONLY place where processEvents may be called.
111         // During screen update/ redraw, this method is disabled to
112         // prevent keyboard events being handed to the LyX core, where
113         // they could cause re-entrant calls to screen update.
114         guiApp->processEvents(QEventLoop::ExcludeUserInputEvents);
115 }
116
117
118 void exit(int status)
119 {
120         guiApp->exit(status);
121 }
122
123
124 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
125 {
126         QColor const & qcol = guiApp->colorCache().get(col);
127         if (!qcol.isValid()) {
128                 rgbcol.r = 0;
129                 rgbcol.g = 0;
130                 rgbcol.b = 0;
131                 return false;
132         }
133         rgbcol.r = qcol.red();
134         rgbcol.g = qcol.green();
135         rgbcol.b = qcol.blue();
136         return true;
137 }
138
139
140 string const hexname(LColor_color col)
141 {
142         return ltrim(fromqstr(guiApp->colorCache().get(col).name()), "#");
143 }
144
145
146 void update_color(LColor_color)
147 {
148         // FIXME: Bleh, can't we just clear them all at once ?
149         guiApp->colorCache().clear();
150 }
151
152
153 void update_fonts()
154 {
155         guiApp->fontLoader().update();
156 }
157
158
159 bool font_available(LyXFont const & font)
160 {
161         return guiApp->fontLoader().available(font);
162 }
163
164
165 void register_socket_callback(int fd, boost::function<void()> func)
166 {
167         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
168 }
169
170
171 void unregister_socket_callback(int fd)
172 {
173         socket_callbacks.erase(fd);
174 }
175
176 }; // namespace lyx_gui