]> git.lyx.org Git - features.git/blob - src/frontends/qt2/lyx_gui.C
Jo�o Assirati's lyxsocket patch.
[features.git] / src / frontends / qt2 / lyx_gui.C
1 /**
2  * \file qt2/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  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14
15 #include "support/lstrings.h"
16 #include "qt_helpers.h"
17
18 #include "lyx_gui.h"
19 #include "funcrequest.h"
20 #include "graphics/LoaderQueue.h"
21
22 // FIXME: move this stuff out again
23 #include "bufferlist.h"
24 #include "LyXAction.h"
25 #include "lyxfunc.h"
26 #include "lyxrc.h"
27 #include "lyxserver.h"
28 #include "BufferView.h"
29 #include "LColor.h"
30
31 // Dear Lord, deliver us from Evil,
32 // aka the Qt headers
33 #include <boost/signals/signal1.hpp>
34 #include <boost/bind.hpp>
35
36 #include "QtView.h"
37 #include "QLImage.h"
38 #include "qfont_loader.h"
39 #include "io_callback.h"
40 #include "lcolorcache.h"
41
42 #include <qapplication.h>
43 #include <qpaintdevicemetrics.h>
44
45 using lyx::support::ltrim;
46
47 #ifndef CXX_GLOBAL_CSTD
48 using std::exit;
49 #endif
50
51 using std::map;
52 using std::vector;
53 using std::string;
54
55
56 extern BufferList bufferlist;
57
58 namespace {
59
60 float getDPI()
61 {
62         QWidget w;
63         QPaintDeviceMetrics pdm(&w);
64         return 0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY());
65 }
66
67 map<int, io_callback *> io_callbacks;
68
69 } // namespace anon
70
71 // FIXME: wrong place !
72 LyXServer * lyxserver;
73
74 // in QLyXKeySym.C
75 extern void initEncodings();
76
77 #ifdef Q_WS_X11
78 extern bool lyxX11EventFilter(XEvent * xev);
79 #endif
80
81
82 class LQApplication : public QApplication
83 {
84 public:
85         LQApplication(int & argc, char ** argv);
86         ~LQApplication();
87 #ifdef Q_WS_X11
88         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
89 #endif
90 };
91
92
93 LQApplication::LQApplication(int & argc, char ** argv)
94         : QApplication(argc, argv)
95 {}
96
97
98 LQApplication::~LQApplication()
99 {}
100
101 namespace lyx_gui {
102
103 bool use_gui = true;
104
105
106 void parse_init(int & argc, char * argv[])
107 {
108         static LQApplication a(argc, argv);
109
110         using namespace lyx::graphics;
111
112         Image::newImage = boost::bind(&QLImage::newImage);
113         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
114
115         // needs to be done before reading lyxrc
116         lyxrc.dpi = getDPI();
117
118         initEncodings();
119
120         LoaderQueue::setPriority(10,100);
121 }
122
123
124 void parse_lyxrc()
125 {}
126
127
128 void start(string const & batch, vector<string> const & files)
129 {
130         // initial geometry
131         unsigned int width = 690;
132         unsigned int height = 510;
133
134         QtView view(width, height);
135         view.show();
136         view.init();
137
138         // FIXME: some code below needs moving
139
140         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
141
142         vector<string>::const_iterator cit = files.begin();
143         vector<string>::const_iterator end = files.end();
144         for (; cit != end; ++cit)
145                 view.view()->loadLyXFile(*cit, true);
146
147         // handle the batch commands the user asked for
148         if (!batch.empty()) {
149                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
150         }
151
152         qApp->exec();
153
154         // FIXME
155         delete lyxserver;
156         lyxserver = 0;
157 }
158
159
160 void sync_events()
161 {
162         qApp->processEvents();
163 }
164
165
166 void exit()
167 {
168         delete lyxserver;
169         lyxserver = 0;
170
171         // we cannot call qApp->exit(0) - that could return us
172         // into a static dialog return in the lyx code (for example,
173         // load autosave file QMessageBox. We have to just get the hell
174         // out.
175
176         ::exit(0);
177 }
178
179
180 FuncStatus getStatus(FuncRequest const & ev)
181 {
182         FuncStatus flag;
183         switch (ev.action) {
184         case LFUN_DIALOG_SHOW:
185                 if (ev.argument == "preamble" || ev.argument == "forks")
186                         flag.unknown(true);
187                 break;
188         case LFUN_TOOLTIPS_TOGGLE:
189                 flag.unknown(true);
190                 break;
191         default:
192                 break;
193         }
194         return flag;
195 }
196
197
198 string const hexname(LColor_color col)
199 {
200         return ltrim(fromqstr(lcolorcache.get(col).name()), "#");
201 }
202
203
204 void update_color(LColor_color)
205 {
206         // FIXME: Bleh, can't we just clear them all at once ?
207         lcolorcache.clear();
208 }
209
210
211 void update_fonts()
212 {
213         fontloader.update();
214 }
215
216
217 bool font_available(LyXFont const & font)
218 {
219         return fontloader.available(font);
220 }
221
222
223 void set_read_callback(int fd, LyXComm * comm)
224 {
225         io_callbacks[fd] = new io_callback(fd, comm);
226 }
227
228
229 void remove_read_callback(int fd)
230 {
231         map<int, io_callback *>::iterator it = io_callbacks.find(fd);
232         if (it != io_callbacks.end()) {
233                 delete it->second;
234                 io_callbacks.erase(it);
235         }
236 }
237
238
239 void set_datasocket_callback(LyXDataSocket * /* p */)
240 {}
241
242
243 void remove_datasocket_callback(LyXDataSocket * /* p */)
244 {}
245
246
247 void set_serversocket_callback(LyXServerSocket * /* p */)
248 {}
249
250
251 void remove_serversocket_callback(LyXServerSocket * /* p */)
252 {}
253
254
255 string const roman_font_name()
256 {
257         if (!use_gui)
258                 return "serif";
259
260         QFont font;
261         font.setStyleHint(QFont::Serif);
262         font.setFamily("serif");
263         return QFontInfo(font).family().latin1();
264 }
265
266
267 string const sans_font_name()
268 {
269         if (!use_gui)
270                 return "sans";
271
272         QFont font;
273         font.setStyleHint(QFont::SansSerif);
274         font.setFamily("sans");
275         return QFontInfo(font).family().latin1();
276 }
277
278
279 string const typewriter_font_name()
280 {
281         if (!use_gui)
282                 return "monospace";
283
284         QFont font;
285         font.setStyleHint(QFont::TypeWriter);
286         font.setFamily("monospace");
287         return QFontInfo(font).family().latin1();
288 }
289
290 }; // namespace lyx_gui