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