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