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