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