]> git.lyx.org Git - lyx.git/blob - src/frontends/gtk/lyx_gui.C
make it compile again
[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 "GuiImplementation.h"
47 #include "GView.h"
48 #include "GtkmmX.h"
49
50 #include "xftFontLoader.h"
51 #include "GWorkArea.h"
52
53 #include "support/lyxlib.h"
54 #include "support/os.h"
55 #include "support/filetools.h"
56 #include "support/package.h"
57
58 #include <gtkmm.h>
59
60 #include "LyXGdkImage.h"
61
62 #include <boost/bind.hpp>
63 #include <boost/function.hpp>
64 #include <boost/shared_ptr.hpp>
65
66 #include <fcntl.h>
67
68 #include <sstream>
69 #include <iomanip>
70
71 namespace os = lyx::support::os;
72
73 using std::ostringstream;
74 using std::string;
75
76 using lyx::support::package;
77
78 using lyx::frontend::colorCache;
79 using lyx::frontend::Gui;
80 using lyx::frontend::GuiImplementation;
81 using lyx::frontend::GView;
82
83
84 extern BufferList bufferlist;
85
86 // FIXME: wrong place !
87 LyXServer * lyxserver;
88 LyXServerSocket * lyxsocket;
89
90 bool lyx_gui::use_gui = true;
91
92 namespace {
93
94 /// estimate DPI from X server
95 int getDPI()
96 {
97         //TODO use GDK instead
98         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
99         return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
100                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
101 }
102
103 } // namespace anon
104
105 class Application: public Gtk::Main
106 {
107 public:
108         ///
109         Application(int & argc, char * argv[]): Gtk::Main(argc, argv)
110         {}
111         ///
112         Gui & gui() { return gui_; }
113
114 private:
115         ///
116         GuiImplementation gui_;
117 };
118
119 Application * theApp;
120
121 int lyx_gui::exec(int & argc, char * argv[])
122 {
123         theApp = new Application(argc, argv);
124
125         using namespace lyx::graphics;
126         Image::newImage = boost::bind(&LyXGdkImage::newImage);
127         Image::loadableFormats = boost::bind(&LyXGdkImage::loadableFormats);
128
129         locale_init();
130
131         // must do this /before/ lyxrc gets read
132         lyxrc.dpi = getDPI();
133
134         return LyX::ref().exec2(argc, argv);
135 }
136
137
138 void lyx_gui::parse_lyxrc()
139 {
140 }
141
142
143 LyXView * lyx_gui::create_view(unsigned int width, unsigned int height,
144                    int /*posx*/, int /*posy*/, bool)
145 {
146         int view_id = theApp->gui().newView(width, height);
147         GView & view = static_cast<GView &> (theApp->gui().view(view_id));
148         theApp->gui().newWorkArea(width, height, 0);
149
150         LyX::ref().addLyXView(&view);
151
152         view.show();
153         view.init();
154
155         return &view;
156 }
157
158
159 int lyx_gui::start(LyXView * view, string const & batch)
160 {
161         // FIXME: server code below needs moving
162
163         lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
164         lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
165                           os::internal_path(package().temp_dir() + "/lyxsocket"));
166
167         // handle the batch commands the user asked for
168         if (!batch.empty()) {
169                 view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
170         }
171
172         theApp->run();
173
174         // FIXME: breaks emergencyCleanup
175         delete lyxsocket;
176         delete lyxserver;
177         return EXIT_SUCCESS;
178 }
179
180
181 void lyx_gui::exit(int /*status*/)
182 {
183         // FIXME: Don't ignore status
184         theApp->quit();
185 }
186
187
188 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
189 {
190         FuncStatus flag;
191         switch (ev.action) {
192         case LFUN_TOOLTIPS_TOGGLE:
193                 flag.unknown(true);
194                 break;
195         default:
196                 break;
197         }
198
199         return flag;
200 }
201
202
203 bool lyx_gui::getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
204 {
205         Gdk::Color gdkColor;
206         Gdk::Color * gclr = colorCache.getColor(col);
207         if (!gclr) {
208                 gclr = &gdkColor;
209                 if(!gclr->parse(lcolor.getX11Name(col))) {
210                         rgbcol.r = 0;
211                         rgbcol.g = 0;
212                         rgbcol.b = 0;
213                         return false;
214                 }
215         }
216
217         // Note that X stores the RGB values in the range 0 - 65535
218         // whilst we require them in the range 0 - 255.
219         rgbcol.r = gclr->get_red() / 256;
220         rgbcol.g = gclr->get_green() / 256;
221         rgbcol.b = gclr->get_blue() / 256;
222         return true;
223 }
224
225
226 string const lyx_gui::hexname(LColor_color col)
227 {
228         lyx::RGBColor rgbcol;
229         if (!getRGBColor(col, rgbcol)) {
230                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
231                        << '"' << std::endl;
232                 return string();
233         }
234
235         std::ostringstream os;
236
237         os << std::setbase(16) << std::setfill('0')
238            << std::setw(2) << rgbcol.r
239            << std::setw(2) << rgbcol.g
240            << std::setw(2) << rgbcol.b;
241
242         return os.str();
243 }
244
245
246 void lyx_gui::update_color(LColor_color /*col*/)
247 {
248         colorCache.clear();
249 }
250
251
252 void lyx_gui::update_fonts()
253 {
254         fontLoader.update();
255 }
256
257
258 bool lyx_gui::font_available(LyXFont const & font)
259 {
260         return fontLoader.available(font);
261 }
262
263
264 namespace {
265
266 std::map<int, boost::shared_ptr<io_callback> > callbacks;
267
268 } // NS anon
269
270
271 void lyx_gui::register_socket_callback(int fd,
272                                        boost::function<void()> func)
273 {
274         callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
275 }
276
277
278 void lyx_gui::unregister_socket_callback(int fd)
279 {
280         callbacks.erase(fd);
281 }
282
283
284 string const lyx_gui::roman_font_name()
285 {
286         return "times";
287 }
288
289
290 string const lyx_gui::sans_font_name()
291 {
292         return "helvetica";
293 }
294
295
296 string const lyx_gui::typewriter_font_name()
297 {
298         return "courier";
299 }
300
301
302 void lyx_gui::sync_events()
303 {
304         // FIXME
305 }