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