]> git.lyx.org Git - lyx.git/blob - src/frontends/qt2/lyx_gui.C
Better DPI, Cleaner c-tor.
[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 "gettext.h"
24
25 #include <fcntl.h>
26 #include <boost/bind.hpp>
27
28 #include "lyx_gui.h"
29 #include "lyx_main.h"
30 #include "lyxrc.h"
31 #include "lyxfont.h"
32
33 // FIXME: move this stuff out again
34 #include "bufferlist.h"
35 #include "lyxfunc.h"
36 #include "lyxserver.h"
37 #include "BufferView.h"
38
39 // Dear Lord, deliver us from Evil,
40 // aka the Qt headers
41 #include <boost/shared_ptr.hpp>
42 #include <boost/function/function0.hpp>
43 #include <boost/signals/signal1.hpp>
44
45 #include "QtView.h"
46 #include "QLImage.h"
47 #include "qfont_loader.h"
48 #include "io_callback.h"
49
50 #include <qapplication.h>
51 #include <qwidget.h> 
52 #include <qpaintdevicemetrics.h> 
53
54 #ifndef CXX_GLOBAL_CSTD
55 using std::exit;
56 #endif
57
58 using std::vector;
59 using std::map;
60 using std::endl;
61
62 extern BufferList bufferlist;
63
64 namespace {
65
66 float getDPI()
67
68         QWidget w;
69         QPaintDeviceMetrics pdm(&w);
70         return 0.5 * (pdm.logicalDpiX() + pdm.logicalDpiY());
71 }
72
73 }
74
75 // FIXME: wrong place !
76 LyXServer * lyxserver;
77
78 void lyx_gui::parse_init(int & argc, char * argv[])
79 {
80         static QApplication a(argc, argv);
81
82         using namespace grfx;
83
84         Image::newImage = boost::bind(&QLImage::newImage);
85         Image::loadableFormats = boost::bind(&QLImage::loadableFormats);
86
87         // needs to be done before reading lyxrc
88         lyxrc.dpi = getDPI();
89 }
90
91
92 void lyx_gui::parse_lyxrc()
93 {
94 }
95
96
97 void lyx_gui::start(string const & batch, vector<string> files)
98 {
99         // initial geometry
100         int xpos = -1;
101         int ypos = -1;
102         unsigned int width = 690;
103         unsigned int height = 510;
104
105         QtView view(width, height);
106         view.show(xpos, ypos, "LyX");
107         view.init();
108
109         Buffer * last = 0;
110
111         // FIXME: some code below needs moving
112
113         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
114
115         vector<string>::const_iterator cit = files.begin();
116         vector<string>::const_iterator end = files.end();
117         for (; cit != end; ++cit) {
118                 Buffer * b = bufferlist.loadLyXFile(*cit);
119                 if (b) {
120                         last = b;
121                 }
122         }
123
124         // switch to the last buffer successfully loaded
125         if (last) {
126                 view.view()->buffer(last);
127         }
128
129         // handle the batch commands the user asked for
130         if (!batch.empty()) {
131                 view.getLyXFunc().dispatch(batch);
132         }
133
134         qApp->exec();
135
136         // FIXME
137         delete lyxserver;
138 }
139
140
141 void lyx_gui::exit()
142 {
143         qApp->exit(0);
144 }
145
146
147 string const lyx_gui::hexname(LColor::color col)
148 {
149         QColor color(lcolor.getX11Name(col).c_str());
150         return ltrim(color.name().latin1(), "#");
151 }
152
153
154 void lyx_gui::update_color(LColor::color)
155 {
156         // no need
157 }
158
159
160 void lyx_gui::update_fonts()
161 {
162         fontloader.update();
163 }
164
165
166 bool lyx_gui::font_available(LyXFont const & font)
167 {
168         return fontloader.available(font);
169 }
170
171
172 namespace {
173         map<int, io_callback *> io_callbacks;
174 }
175
176
177 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
178 {
179         io_callbacks[fd] = new io_callback(fd, comm);
180 }
181
182
183 void lyx_gui::remove_read_callback(int fd)
184 {
185         map<int, io_callback *>::iterator it = io_callbacks.find(fd);
186         if (it != io_callbacks.end()) {
187                 delete it->second;
188                 io_callbacks.erase(it);
189         }
190 }