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