]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
Qt3/Qt4:
[lyx.git] / src / frontends / gtk / lyx_gui.C
1 /**
2  * \file gtk/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 Lars Gullik Bjnes
7  * \author John Levon
8  * \author Huang Ying
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 // Too hard to make concept checks work with this file
16 #ifdef _GLIBCXX_CONCEPT_CHECKS
17 #undef _GLIBCXX_CONCEPT_CHECKS
18 #endif
19 #ifdef _GLIBCPP_CONCEPT_CHECKS
20 #undef _GLIBCPP_CONCEPT_CHECKS
21 #endif
22
23 #include "lyx_gui.h"
24
25 #include "debug.h"
26 #include "funcrequest.h"
27 #include "gettext.h"
28
29 #include "Color.h"
30 #include "LColor.h"
31 #include "LyXAction.h"
32 #include "lyx_main.h"
33 #include "lyxrc.h"
34 #include "lyxfont.h"
35 #include "graphics/LoaderQueue.h"
36
37 #include "io_callback.h"
38
39 // FIXME: move this stuff out again
40 #include "bufferlist.h"
41 #include "lyxfunc.h"
42 #include "lyxserver.h"
43 #include "lyxsocket.h"
44 #include "BufferView.h"
45
46 #include "GView.h"
47 #include "GtkmmX.h"
48
49 #include "xftFontLoader.h"
50 #include "GWorkArea.h"
51
52 #include "support/lyxlib.h"
53 #include "support/os.h"
54 #include "support/filetools.h"
55 #include "support/package.h"
56
57 #include <gtkmm.h>
58
59 #include "LyXGdkImage.h"
60
61 #include <boost/bind.hpp>
62 #include <boost/function.hpp>
63 #include <boost/shared_ptr.hpp>
64
65 #include <fcntl.h>
66
67 #include <sstream>
68 #include <iomanip>
69
70 namespace os = lyx::support::os;
71
72 using std::ostringstream;
73 using std::string;
74
75 using lyx::support::package;
76
77 using lyx::frontend::colorCache;
78 using lyx::frontend::GView;
79
80
81 extern BufferList bufferlist;
82
83 // FIXME: wrong place !
84 LyXServer * lyxserver;
85 LyXServerSocket * lyxsocket;
86
87 bool lyx_gui::use_gui = true;
88
89 namespace {
90
91 /// estimate DPI from X server
92 int getDPI()
93 {
94         //TODO use GDK instead
95         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
96         return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
97                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
98 }
99
100 } // namespace anon
101
102
103 void lyx_gui::exec(int & argc, char * argv[])
104 {
105         new Gtk::Main(argc, argv);
106
107         using namespace lyx::graphics;
108         Image::newImage = boost::bind(&LyXGdkImage::newImage);
109         Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
110
111         locale_init();
112
113         // must do this /before/ lyxrc gets read
114         lyxrc.dpi = getDPI();
115
116         LyX::ref().exec2(argc, argv);
117 }
118
119
120 void lyx_gui::parse_lyxrc()
121 {
122 }
123
124
125 void lyx_gui::start(string const & batch, std::vector<string> const & files,
126                     unsigned int width, unsigned int height, int posx, int posy, bool)
127 {
128         boost::shared_ptr<GView> view_ptr(new GView);
129         LyX::ref().addLyXView(view_ptr);
130
131         GView & view = *view_ptr.get();
132         view.show();
133         view.init();
134
135         // FIXME: server code below needs moving
136
137         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
138         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
139                           os::internal_path(package().temp_dir() + "/lyxsocket"));
140
141         for_each(files.begin(), files.end(),
142                  bind(&BufferView::loadLyXFile, view.view(), _1, true));
143
144         // handle the batch commands the user asked for
145         if (!batch.empty()) {
146                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
147         }
148
149         Gtk::Main::run();
150
151         // FIXME: breaks emergencyCleanup
152         delete lyxsocket;
153         delete lyxserver;
154 }
155
156
157 void lyx_gui::exit(int /*status*/)
158 {
159         // FIXME: Don't ignore status
160         Gtk::Main::quit();
161 }
162
163
164 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
165 {
166         FuncStatus flag;
167         switch (ev.action) {
168         case LFUN_DIALOG_SHOW:
169                 if (ev.argument == "preamble")
170                         flag.unknown(true);
171                 break;
172         case LFUN_TOOLTIPS_TOGGLE:
173                 flag.unknown(true);
174                 break;
175         default:
176                 break;
177         }
178
179         return flag;
180 }
181
182
183 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
184 {
185         Gdk::Color gdkColor;
186         Gdk::Color * gclr = colorCache.getColor(col);
187         if (!gclr) {
188                 gclr = &gdkColor;
189                 if(!gclr->parse(lcolor.getX11Name(col))) {
190                         rgbcol.r = 0;
191                         rgbcol.g = 0;
192                         rgbcol.b = 0;
193                         return false;
194                 }
195         }
196
197         // Note that X stores the RGB values in the range 0 - 65535
198         // whilst we require them in the range 0 - 255.
199         rgbcol.r = gclr->get_red() / 256;
200         rgbcol.g = gclr->get_green() / 256;
201         rgbcol.b = gclr->get_blue() / 256;
202         return true;
203 }
204
205
206 string const lyx_gui::hexname(LColor_color col)
207 {
208         lyx::RGBColor rgbcol;
209         if (!getRGBColor(col, rgbcol)) {
210                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
211                        << '"' << std::endl;
212                 return string();
213         }
214
215         std::ostringstream os;
216
217         os << std::setbase(16) << std::setfill('0')
218            << std::setw(2) << rgbcol.r
219            << std::setw(2) << rgbcol.g
220            << std::setw(2) << rgbcol.b;
221
222         return os.str();
223 }
224
225
226 void lyx_gui::update_color(LColor_color /*col*/)
227 {
228         colorCache.clear();
229 }
230
231
232 void lyx_gui::update_fonts()
233 {
234         fontLoader.update();
235 }
236
237
238 bool lyx_gui::font_available(LyXFont const & font)
239 {
240         return fontLoader.available(font);
241 }
242
243
244 namespace {
245
246 std::map<int, boost::shared_ptr<io_callback> > callbacks;
247
248 } // NS anon
249
250
251 void lyx_gui::register_socket_callback(int fd,
252                                        boost::function<void()> func)
253 {
254         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
255 }
256
257
258 void lyx_gui::unregister_socket_callback(int fd)
259 {
260         callbacks.erase(fd);
261 }
262
263
264 string const lyx_gui::roman_font_name()
265 {
266         return "times";
267 }
268
269
270 string const lyx_gui::sans_font_name()
271 {
272         return "helvetica";
273 }
274
275
276 string const lyx_gui::typewriter_font_name()
277 {
278         return "courier";
279 }
280
281
282 void lyx_gui::sync_events()
283 {
284         // FIXME
285 }