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