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