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