]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/lyx_gui.C
This commit introduces frontends/Application.[Ch] and makes qt4/GuiApplication (renam...
[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 "support/lstrings.h"
32
33
34 #include "GuiView.h"
35 #include "ColorCache.h"
36 #include "FontLoader.h"
37 #include "QLImage.h"
38 #include "qt_helpers.h"
39 #include "socket_callback.h"
40 #include "GuiApplication.h"
41
42 #include <QApplication>
43 #include <QEventLoop>
44 #include <QTranslator>
45 #include <QTextCodec>
46 #include <QLocale>
47 #include <QLibraryInfo>
48
49 #include <boost/bind.hpp>
50 #include <boost/shared_ptr.hpp>
51
52
53 using lyx::support::ltrim;
54
55 using lyx::frontend::GuiImplementation;
56 using lyx::frontend::GuiView;
57 using lyx::frontend::GuiApplication;
58
59 using boost::shared_ptr;
60
61 #ifndef CXX_GLOBAL_CSTD
62 using std::exit;
63 #endif
64
65 using std::map;
66 using std::vector;
67 using std::string;
68
69 lyx::frontend::GuiApplication * guiApp;
70 lyx::frontend::Application * theApp;
71
72
73 namespace {
74
75 map<int, shared_ptr<socket_callback> > socket_callbacks;
76
77 } // namespace anon
78
79 namespace lyx_gui {
80
81 bool use_gui = true;
82
83 int exec(int & argc, char * argv[])
84 {
85         /*
86         FIXME : Abdel 29/05/2006 (younes.a@free.fr)
87         reorganize this code. In particular make sure that this
88         advice from Qt documentation is respected:
89
90                 Since the QApplication object does so much initialization, it
91                 must be created before any other objects related to the user
92                 interface are created.
93
94         Right now this is not the case, I suspect that a number of global variables
95         contains Qt object that are initialized before the passage through
96         parse_init(). This might also explain the message displayed by Qt
97         that caused the hanging:
98
99         QObject::killTimer: timers cannot be stopped from another thread
100
101         I hope that the problem will disappear automagically when we get rid of
102         lyx_gui entirely, thus using theApp directly throughout the code for LyXFunc,
103         Clipboard and Selection access.
104         */
105
106         // Force adding of font path _before_ QApplication is initialized
107         FontLoader::initFontPath();
108
109 #if defined(Q_WS_WIN) && !defined(Q_CYGWIN_WIN)
110         static GuiApplication app(argc, argv);
111 #else
112         GuiApplication app(argc, argv);
113 #endif
114
115         guiApp = &app;
116         theApp = guiApp;
117
118         return LyX::ref().exec2(argc, argv);
119 }
120
121
122 void parse_lyxrc()
123 {}
124
125
126 LyXView * create_view(unsigned int width, unsigned int height, int posx, int posy,
127           bool maximize)
128 {
129         return &guiApp->createView(width, height, posx, posy, maximize);
130 }
131
132
133 int start(LyXView * view, string const & batch)
134 {
135         return theApp->start(batch);
136 }
137
138
139 void sync_events()
140 {
141         // This is the ONLY place where processEvents may be called.
142         // During screen update/ redraw, this method is disabled to
143         // prevent keyboard events being handed to the LyX core, where
144         // they could cause re-entrant calls to screen update.
145         guiApp->processEvents(QEventLoop::ExcludeUserInputEvents);
146 }
147
148
149 void exit(int status)
150 {
151         guiApp->exit(status);
152 }
153
154
155 FuncStatus getStatus(FuncRequest const & ev)
156 {
157         FuncStatus flag;
158         switch (ev.action) {
159         case LFUN_TOOLTIPS_TOGGLE:
160                 flag.unknown(true);
161                 break;
162         default:
163                 break;
164         }
165
166         return flag;
167 }
168
169
170 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
171 {
172         QColor const & qcol = lcolorcache.get(col);
173         if (!qcol.isValid()) {
174                 rgbcol.r = 0;
175                 rgbcol.g = 0;
176                 rgbcol.b = 0;
177                 return false;
178         }
179         rgbcol.r = qcol.red();
180         rgbcol.g = qcol.green();
181         rgbcol.b = qcol.blue();
182         return true;
183 }
184
185
186 string const hexname(LColor_color col)
187 {
188         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
189 }
190
191
192 void update_color(LColor_color)
193 {
194         // FIXME: Bleh, can't we just clear them all at once ?
195         lcolorcache.clear();
196 }
197
198
199 void update_fonts()
200 {
201         guiApp->fontLoader().update();
202 }
203
204
205 bool font_available(LyXFont const & font)
206 {
207         return guiApp->fontLoader().available(font);
208 }
209
210
211 void register_socket_callback(int fd, boost::function<void()> func)
212 {
213         socket_callbacks[fd] = shared_ptr<socket_callback>(new socket_callback(fd, func));
214 }
215
216
217 void unregister_socket_callback(int fd)
218 {
219         socket_callbacks.erase(fd);
220 }
221
222
223 string const roman_font_name()
224 {
225         if (!use_gui)
226                 return "serif";
227
228         QFont font;
229         font.setStyleHint(QFont::Serif);
230         font.setFamily("serif");
231
232         return fromqstr(QFontInfo(font).family());
233 }
234
235
236 string const sans_font_name()
237 {
238         if (!use_gui)
239                 return "sans";
240
241         QFont font;
242         font.setStyleHint(QFont::SansSerif);
243         font.setFamily("sans");
244
245         return fromqstr(QFontInfo(font).family());
246 }
247
248
249 string const typewriter_font_name()
250 {
251         if (!use_gui)
252                 return "monospace";
253
254         QFont font;
255         font.setStyleHint(QFont::TypeWriter);
256         font.setFamily("monospace");
257
258         return fromqstr(QFontInfo(font).family());
259 }
260
261 }; // namespace lyx_gui