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