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