]> git.lyx.org Git - features.git/blob - src/frontends/gtk/lyx_gui.C
the lyxsocket changes
[features.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 #include <gtkmm.h>
15
16 #include "lyx_gui.h"
17
18 #include "support/lyxlib.h"
19 #include "support/os.h"
20 #include "support/filetools.h"
21 #include "support/path_defines.h"
22
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26
27 #include "LColor.h"
28 #include "LyXAction.h"
29 #include "lyx_main.h"
30 #include "lyxrc.h"
31 #include "lyxfont.h"
32 #include "graphics/LoaderQueue.h"
33
34 // FIXME: move this stuff out again
35 #include "bufferlist.h"
36 #include "buffer_funcs.h"
37 #include "lyxfunc.h"
38 #include "lyxserver.h"
39 #include "lyxsocket.h"
40 #include "BufferView.h"
41
42 #include "GView.h"
43 #include "GtkmmX.h"
44
45 #include "xftFontLoader.h"
46 #include "GWorkArea.h"
47
48 #include "support/std_sstream.h"
49 #include <iomanip>
50 #include <fcntl.h>
51 #include <boost/bind.hpp>
52 #include <boost/function.hpp>
53
54 //just for xforms
55 #include "lyx_forms.h"
56 #include "xformsImage.h"
57 #include "xforms_helpers.h"
58
59 namespace os = lyx::support::os;
60
61 using std::ostringstream;
62 using std::string;
63
64 using lyx::frontend::colorCache;
65 using lyx::frontend::GView;
66 using lyx::frontend::XformsColor;
67
68
69 extern BufferList bufferlist;
70
71 // FIXME: wrong place !
72 LyXServer * lyxserver;
73 LyXServerSocket * lyxsocket;
74
75 bool lyx_gui::use_gui = true;
76
77 namespace {
78
79 /// quit lyx
80 bool finished = false;
81
82
83 /// estimate DPI from X server
84 float getDPI()
85 {
86         Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
87         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
88                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
89 }
90
91
92 /// set default GUI configuration
93 void setDefaults()
94 {
95         FL_IOPT cntl;
96         cntl.buttonFontSize = FL_NORMAL_SIZE;
97         cntl.browserFontSize = FL_NORMAL_SIZE;
98         cntl.labelFontSize = FL_NORMAL_SIZE;
99         cntl.choiceFontSize = FL_NORMAL_SIZE;
100         cntl.inputFontSize = FL_NORMAL_SIZE;
101         cntl.menuFontSize  = FL_NORMAL_SIZE;
102         cntl.borderWidth = -1;
103         cntl.vclass = FL_DefaultVisual;
104         fl_set_defaults(FL_PDVisual
105                         | FL_PDButtonFontSize
106                         | FL_PDBrowserFontSize
107                         | FL_PDLabelFontSize
108                         | FL_PDChoiceFontSize
109                         | FL_PDInputFontSize
110                         | FL_PDMenuFontSize
111                         | FL_PDBorderWidth, &cntl);
112 }
113
114
115 extern "C" {
116
117
118 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
119         // We don't abort on BadWindow
120         if (xeev->error_code == BadWindow) {
121                 lyxerr << "BadWindow received !" << std::endl;
122                 lyxerr << "If you're using xforms 1.0 or greater, "
123                        << " please report this to lyx-devel@lists.lyx.org"
124                        << std::endl;
125                 return 0;
126         }
127
128         // emergency cleanup
129         LyX::cref().emergencyCleanup();
130
131         // Get the reason for the crash.
132         char etxt[513];
133         XGetErrorText(display, xeev->error_code, etxt, 512);
134         lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
135         // By doing an abort we get a nice backtrace. (hopefully)
136         lyx::support::abort();
137         return 0;
138 }
139
140 }
141
142 /// read in geometry specification
143 char geometry[40];
144
145 } // namespace anon
146
147
148 void parse_init_xforms(int & argc, char * argv[])
149 {
150         setDefaults();
151
152         FL_CMD_OPT cmdopt[] = {
153                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
154         };
155
156         FL_resource res[] = {
157                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
158         };
159
160         const int num_res = sizeof(res)/sizeof(FL_resource);
161         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
162
163         // It appears that, in xforms >=0.89.5, fl_initialize()
164         // calls setlocale() and ruins our LC_NUMERIC setting.
165
166         fl_get_app_resources(res, num_res);
167
168         Display * display = fl_get_display();
169
170         if (!display) {
171                 lyxerr << "LyX: unable to access X display, exiting"
172                        << std::endl;
173                 lyx::support::os::warn("Unable to access X display, exiting");
174                 ::exit(1);
175         }
176
177         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
178
179         XSetErrorHandler(LyX_XErrHandler);
180
181         using namespace lyx::graphics;
182
183         // connect the image loader based on the xforms library
184         Image::newImage = boost::bind(&xformsImage::newImage);
185         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
186 }
187
188
189 void lyx_gui::parse_init(int & argc, char * argv[])
190 {
191         new Gtk::Main(argc, argv);
192
193         parse_init_xforms(argc, argv);
194
195         locale_init();
196
197         // must do this /before/ lyxrc gets read
198         lyxrc.dpi = getDPI();
199 }
200
201
202 void parse_lyxrc_xforms()
203 {
204         XformsColor::read(lyx::support::AddName(
205                                   lyx::support::user_lyxdir(), "preferences.xform"));
206
207         if (lyxrc.popup_font_encoding.empty())
208                 lyxrc.popup_font_encoding = lyxrc.font_norm;
209         // Set the font name for popups and menus
210         string boldfontname = lyxrc.popup_bold_font
211                                + "-*-*-*-?-*-*-*-*-"
212                                + lyxrc.popup_font_encoding;
213                 // "?" means "scale that font"
214         string fontname = lyxrc.popup_normal_font
215                                + "-*-*-*-?-*-*-*-*-"
216                                + lyxrc.popup_font_encoding;
217
218         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
219         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
220         if (bold < 0)
221                 lyxerr << "Could not set menu font to "
222                        << boldfontname << std::endl;
223
224         if (normal < 0)
225                 lyxerr << "Could not set popup font to "
226                        << fontname << std::endl;
227
228         if (bold < 0 && normal < 0) {
229                 lyxerr << "Using 'helvetica' font for menus" << std::endl;
230                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
231                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
232                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
233                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
234
235                 if (bold < 0 && normal < 0) {
236                         lyxerr << "Could not find helvetica font. Using 'fixed'."
237                                << std::endl;
238                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
239                         normal = bold = 0;
240                 }
241         }
242         if (bold < 0)
243                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
244         else if (normal < 0)
245                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
246
247         fl_setpup_fontstyle(FL_NORMAL_STYLE);
248         fl_setpup_fontsize(FL_NORMAL_SIZE);
249         fl_setpup_color(FL_MCOL, FL_BLACK);
250         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
251         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
252 }
253
254
255 void lyx_gui::parse_lyxrc()
256 {
257         parse_lyxrc_xforms();
258 }
259
260
261 void start_xforms()
262 {
263         // initial geometry
264         int xpos = -1;
265         int ypos = -1;
266         unsigned int width = 690;
267         unsigned int height = 510;
268
269         int const geometryBitmask =
270                 XParseGeometry(geometry,
271                                &xpos, &ypos, &width, &height);
272
273         // if width is not set by geometry, check it against monitor width
274         if (!(geometryBitmask & WidthValue)) {
275                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
276                 if (WidthOfScreen(scr) - 8 < int(width))
277                         width = WidthOfScreen(scr) - 8;
278         }
279
280         // if height is not set by geometry, check it against monitor height
281         if (!(geometryBitmask & HeightValue)) {
282                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
283                 if (HeightOfScreen(scr) - 24 < int(height))
284                         height = HeightOfScreen(scr) - 24;
285         }
286
287         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
288
289         // recalculate xpos if it's not set
290         if (xpos == -1)
291                 xpos = (WidthOfScreen(s) - width) / 2;
292
293         // recalculate ypos if it's not set
294         if (ypos == -1)
295                 ypos = (HeightOfScreen(s) - height) / 2;
296
297         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
298                            << '+' << xpos << '+' << ypos << std::endl;
299
300 //      XFormsView view(width, height);
301 //      view.show(xpos, ypos, "LyX");
302 //      view.init();
303 }
304
305
306 static void events_xforms()
307 {
308         if (fl_check_forms() == FL_EVENT) {
309                 XEvent ev;
310                 fl_XNextEvent(&ev);
311                 lyxerr[Debug::GUI]
312                         << "Received unhandled X11 event" << std::endl
313                         << "Type: " << ev.xany.type
314                         << " Target: 0x" << std::hex << ev.xany.window
315                         << std::dec << std::endl;
316         }
317 }
318
319
320 void lyx_gui::start(string const & batch, std::vector<string> const & files)
321 {
322         start_xforms();
323         // just for debug
324         XSynchronize(getDisplay(), true);
325
326         boost::shared_ptr<GView> view_ptr(new GView);
327         LyX::ref().addLyXView(view_ptr);
328
329         GView & view = *view_ptr.get();
330         view.show();
331         view.init();
332
333         // FIXME: some code below needs moving
334
335         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
336         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
337                           os::slashify_path(os::getTmpDir() + "/lyxsocket"));
338
339         std::vector<string>::const_iterator cit = files.begin();
340         std::vector<string>::const_iterator end = files.end();
341         for (; cit != end; ++cit)
342                 view.view()->loadLyXFile(*cit, true);
343
344         // handle the batch commands the user asked for
345         if (!batch.empty()) {
346                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
347         }
348
349         // enter the event loop
350         while (!finished) {
351                 while (Gtk::Main::events_pending())
352                         Gtk::Main::iteration(false);
353                 events_xforms();
354         }
355
356         // FIXME: breaks emergencyCleanup
357         delete lyxsocket;
358         delete lyxserver;
359 }
360
361
362 void lyx_gui::exit()
363 {
364         finished = true;
365 }
366
367
368 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
369 {
370         // Nothing interesting to do here
371         return FuncStatus();
372 }
373
374
375 string const lyx_gui::hexname(LColor_color col)
376 {
377         Gdk::Color gdkColor;
378         Gdk::Color * gclr = colorCache.getColor(col);
379         if (!gclr) {
380                 gclr = &gdkColor;
381                 gclr->parse(lcolor.getX11Name(col));
382         }
383
384         std::ostringstream os;
385
386         // Note that X stores the RGB values in the range 0 - 65535
387         // whilst we require them in the range 0 - 255.
388         os << std::setbase(16) << std::setfill('0')
389            << std::setw(2) << (gclr->get_red() / 256)
390            << std::setw(2) << (gclr->get_green() / 256)
391            << std::setw(2) << (gclr->get_blue() / 256);
392
393         return os.str();
394 }
395
396
397 void lyx_gui::update_color(LColor_color /*col*/)
398 {
399         colorCache.clear();
400 }
401
402
403 void lyx_gui::update_fonts()
404 {
405         fontLoader.update();
406 }
407
408
409 bool lyx_gui::font_available(LyXFont const & font)
410 {
411         return fontLoader.available(font);
412 }
413
414
415 namespace {
416
417
418 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
419 {
420         comm->read_ready();
421         return true;
422 }
423
424
425 std::map<int, SigC::Connection> gReadCallbackMap;
426
427 }
428
429
430 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
431 {
432         gReadCallbackMap[fd] = Glib::signal_io().connect(
433                 SigC::bind(SigC::slot(readCallback), comm),
434                 fd,
435                 Glib::IO_IN);
436 }
437
438
439 void lyx_gui::remove_read_callback(int fd)
440 {
441         gReadCallbackMap[fd].disconnect();
442         gReadCallbackMap.erase(fd);
443 }
444
445
446 void lyx_gui::register_socket_callback(int /*fd*/,
447                                        boost::function<void()> /*func*/)
448 {}
449
450
451 void lyx_gui::unregister_socket_callback(int /*fd*/)
452 {}
453
454
455 string const lyx_gui::roman_font_name()
456 {
457         return "times";
458 }
459
460
461 string const lyx_gui::sans_font_name()
462 {
463         return "helvetica";
464 }
465
466
467 string const lyx_gui::typewriter_font_name()
468 {
469         return "courier";
470 }
471
472
473 void lyx_gui::sync_events()
474 {
475         // FIXME
476 }