]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/lyx_gui.C
dont use pragma impementation and interface anymore
[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
28 // FIXME: move this stuff out again
29 #include "bufferlist.h"
30 #include "lyxfunc.h"
31 #include "lyxserver.h"
32 #include "BufferView.h"
33
34 // Dear Lord, deliver us from Evil,
35 // aka the Qt headers
36 #include <boost/shared_ptr.hpp>
37 #include <boost/function/function0.hpp>
38 #include <boost/signals/signal1.hpp>
39 #include <boost/bind.hpp>
40
41 #include "QtView.h"
42 #include "QLImage.h"
43 #include "qfont_loader.h"
44 #include "io_callback.h"
45
46 #include <qapplication.h>
47 #include <qwidget.h>
48 #include <qpaintdevicemetrics.h>
49
50 #include <fcntl.h>
51 #include <cstdlib>
52
53 #ifndef CXX_GLOBAL_CSTD
54 using std::exit;
55 #endif
56
57 using std::vector;
58 using std::map;
59 using std::endl;
60
61 extern BufferList bufferlist;
62
63 namespace {
64
65 float getDPI()
66 {
67         QWidget w;
68         QPaintDeviceMetrics pdm(&w);
69         return 0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY());
70 }
71
72 map<int, io_callback *> io_callbacks;
73
74 } // namespace anon
75
76
77 // FIXME: wrong place !
78 LyXServer * lyxserver;
79
80 // in QLyXKeySym.C
81 extern void initEncodings();
82
83 #ifdef Q_WS_X11
84 extern bool lyxX11EventFilter(XEvent * xev);
85 #endif
86
87
88 class LQApplication : public QApplication
89 {
90 public:
91         LQApplication(int & argc, char ** argv);
92         ~LQApplication();
93 #ifdef Q_WS_X11
94         bool x11EventFilter (XEvent * ev) { return lyxX11EventFilter(ev); }
95 #endif
96 };
97
98
99 LQApplication::LQApplication(int & argc, char ** argv)
100         : QApplication(argc, argv)
101 {}
102
103
104 LQApplication::~LQApplication()
105 {}
106
107
108 void lyx_gui::parse_init(int & argc, char * argv[])
109 {
110         static LQApplication a(argc, argv);
111
112         using namespace grfx;
113
114         Image::newImage = boost::bind(&QLImage::newImage);
115         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
116
117         // needs to be done before reading lyxrc
118         lyxrc.dpi = getDPI();
119
120         initEncodings();
121 }
122
123
124 void lyx_gui::parse_lyxrc()
125 {
126 }
127
128
129 void lyx_gui::start(string const & batch, vector<string> const & files)
130 {
131         // initial geometry
132         int xpos = -1;
133         int ypos = -1;
134         unsigned int width = 690;
135         unsigned int height = 510;
136
137         QtView view(width, height);
138         view.show();
139         view.init();
140
141         Buffer * last = 0;
142
143         // FIXME: some code below needs moving
144
145         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
146
147         vector<string>::const_iterator cit = files.begin();
148         vector<string>::const_iterator end = files.end();
149         for (; cit != end; ++cit) {
150                 Buffer * b = bufferlist.loadLyXFile(*cit);
151                 if (b) {
152                         last = b;
153                 }
154         }
155
156         // switch to the last buffer successfully loaded
157         if (last) {
158                 view.view()->buffer(last);
159         }
160
161         // handle the batch commands the user asked for
162         if (!batch.empty()) {
163                 view.getLyXFunc().dispatch(batch);
164         }
165
166         qApp->exec();
167
168         // FIXME
169         delete lyxserver;
170         lyxserver = 0;
171 }
172
173
174 void lyx_gui::exit()
175 {
176         delete lyxserver;
177         lyxserver = 0;
178
179         // we cannot call qApp->exit(0) - that could return us
180         // into a static dialog return in the lyx code (for example,
181         // load autosave file QMessageBox. We have to just get the hell
182         // out.
183         
184         ::exit(0);
185 }
186
187
188 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
189 {
190         FuncStatus flag;
191         switch (ev.action) {
192         case LFUN_LAYOUT_PREAMBLE:
193         case LFUN_TOOLTIPS_TOGGLE:
194         case LFUN_FORKS_SHOW:
195                 flag.unknown(true);
196                 break;
197         default:
198                 break;
199         }
200         return flag;
201 }
202
203
204 string const lyx_gui::hexname(LColor::color col)
205 {
206         QColor color(toqstr(lcolor.getX11Name(col)));
207         return ltrim(fromqstr(color.name()), "#");
208 }
209
210
211 void lyx_gui::update_color(LColor::color)
212 {
213         // no need
214 }
215
216
217 void lyx_gui::update_fonts()
218 {
219         fontloader.update();
220 }
221
222
223 bool lyx_gui::font_available(LyXFont const & font)
224 {
225         return fontloader.available(font);
226 }
227
228
229 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
230 {
231         io_callbacks[fd] = new io_callback(fd, comm);
232 }
233
234
235 void lyx_gui::remove_read_callback(int fd)
236 {
237         map<int, io_callback *>::iterator it = io_callbacks.find(fd);
238         if (it != io_callbacks.end()) {
239                 delete it->second;
240                 io_callbacks.erase(it);
241         }
242 }