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