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