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