]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Introduce LFUN_PRINT.
[lyx.git] / src / frontends / xforms / lyx_gui.C
1 /**
2  * \file xforms/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 Bjønnes
7  * \author John Levon
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "lyx_gui.h"
15 #include "ColorHandler.h"
16 #include "xfont_loader.h"
17 #include "xforms_helpers.h"
18 #include "xformsImage.h"
19 #include "XFormsView.h"
20
21 #include "bufferlist.h"
22 #include "BufferView.h"
23 #include "debug.h"
24 #include "funcrequest.h"
25 #include "gettext.h"
26 #include "LColor.h"
27 #include "lyx_main.h"
28 #include "LyXAction.h"
29 #include "lyxfunc.h"
30 #include "lyxrc.h"
31 #include "lyxserver.h"
32 #include "lyxsocket.h"
33
34 #include "graphics/LoaderQueue.h"
35
36 #include "support/filetools.h"
37 #include "support/lyxlib.h"
38 #include "support/os.h"
39 #include "support/path_defines.h"
40
41 #include "lyx_forms.h"
42
43 #include <boost/bind.hpp>
44
45 #include "support/std_sstream.h"
46 #include <iomanip>
47 #include <fcntl.h>
48
49 using lyx::support::AddName;
50 using lyx::support::user_lyxdir;
51
52 namespace os = lyx::support::os;
53
54 #ifndef CXX_GLOBAL_CSTD
55 using std::exit;
56 #endif
57
58 using std::dec;
59 using std::endl;
60 using std::hex;
61 using std::setbase;
62 using std::setfill;
63 using std::setw;
64 using std::ostringstream;
65 using std::vector;
66 using std::string;
67
68
69 extern BufferList bufferlist;
70
71 // FIXME: wrong place !
72 LyXServer * lyxserver;
73 LyXServerSocket * lyxsocket;
74
75 namespace {
76
77 /// quit lyx
78 bool finished = false;
79
80 /// estimate DPI from X server
81 float getDPI()
82 {
83         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
84         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
85                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
86 }
87
88
89 /// set default GUI configuration
90 void setDefaults()
91 {
92         FL_IOPT cntl;
93         cntl.buttonFontSize = FL_NORMAL_SIZE;
94         cntl.browserFontSize = FL_NORMAL_SIZE;
95         cntl.labelFontSize = FL_NORMAL_SIZE;
96         cntl.choiceFontSize = FL_NORMAL_SIZE;
97         cntl.inputFontSize = FL_NORMAL_SIZE;
98         cntl.menuFontSize  = FL_NORMAL_SIZE;
99         cntl.borderWidth = -1;
100         cntl.vclass = FL_DefaultVisual;
101         fl_set_defaults(FL_PDVisual
102                         | FL_PDButtonFontSize
103                         | FL_PDBrowserFontSize
104                         | FL_PDLabelFontSize
105                         | FL_PDChoiceFontSize
106                         | FL_PDInputFontSize
107                         | FL_PDMenuFontSize
108                         | FL_PDBorderWidth, &cntl);
109 }
110
111
112 extern "C" {
113
114 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
115         // We don't abort on BadWindow
116         if (xeev->error_code == BadWindow) {
117                 lyxerr << "BadWindow received !" << endl;
118                 lyxerr << "If you're using xforms 1.0 or greater, "
119                         << " please report this to lyx-devel@lists.lyx.org" << endl;
120                 return 0;
121         }
122
123         // emergency cleanup
124         LyX::cref().emergencyCleanup();
125
126         // Get the reason for the crash.
127         char etxt[513];
128         XGetErrorText(display, xeev->error_code, etxt, 512);
129         lyxerr << etxt << " id: " << xeev->resourceid << endl;
130         // By doing an abort we get a nice backtrace. (hopefully)
131         lyx::support::abort();
132         return 0;
133 }
134
135 }
136
137 /// read in geometry specification
138 char geometry[40];
139
140 } // namespace anon
141
142
143 namespace lyx_gui {
144
145 bool use_gui = true;
146
147
148 void parse_init(int & argc, char * argv[])
149 {
150         setDefaults();
151
152         FL_CMD_OPT cmdopt[] = {
153                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
154         };
155
156         FL_resource res[] = {
157                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
158         };
159
160         const int num_res = sizeof(res)/sizeof(FL_resource);
161
162         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
163
164         // It appears that, in xforms >=0.89.5, fl_initialize()
165         // calls setlocale() and ruins our LC_NUMERIC setting.
166         locale_init();
167
168         fl_get_app_resources(res, num_res);
169
170         Display * display = fl_get_display();
171
172         if (!display) {
173                 lyxerr << "LyX: unable to access X display, exiting" << endl;
174                 os::warn("Unable to access X display, exiting");
175                 ::exit(1);
176         }
177
178         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
179
180         XSetErrorHandler(LyX_XErrHandler);
181
182         lyxColorHandler.reset(new LyXColorHandler);
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         // must do this /before/ lyxrc gets read
191         lyxrc.dpi = getDPI();
192
193         LoaderQueue::setPriority(10,100);
194 }
195
196
197 void parse_lyxrc()
198 {
199         XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
200
201         if (lyxrc.popup_font_encoding.empty())
202                 lyxrc.popup_font_encoding = lyxrc.font_norm;
203         // Set the font name for popups and menus
204         string boldfontname = lyxrc.popup_bold_font
205                                + "-*-*-*-?-*-*-*-*-"
206                                + lyxrc.popup_font_encoding;
207                 // "?" means "scale that font"
208         string fontname = lyxrc.popup_normal_font
209                                + "-*-*-*-?-*-*-*-*-"
210                                + lyxrc.popup_font_encoding;
211
212         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
213         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
214         if (bold < 0)
215                 lyxerr << "Could not set menu font to "
216                        << boldfontname << endl;
217
218         if (normal < 0)
219                 lyxerr << "Could not set popup font to "
220                        << fontname << endl;
221
222         if (bold < 0 && normal < 0) {
223                 lyxerr << "Using 'helvetica' font for menus" << endl;
224                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
225                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
226                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
227                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
228
229                 if (bold < 0 && normal < 0) {
230                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
231                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
232                         normal = bold = 0;
233                 }
234         }
235         if (bold < 0)
236                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
237         else if (normal < 0)
238                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
239
240         fl_setpup_fontstyle(FL_NORMAL_STYLE);
241         fl_setpup_fontsize(FL_NORMAL_SIZE);
242         fl_setpup_color(FL_MCOL, FL_BLACK);
243         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
245 }
246
247
248 void start(string const & batch, vector<string> const & files)
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 << endl;
286
287         boost::shared_ptr<XFormsView> view_ptr(new XFormsView(width, height));
288         LyX::ref().addLyXView(view_ptr);
289
290         XFormsView & view = *view_ptr.get();
291         view.show(xpos, ypos, "LyX");
292         view.init();
293
294         // FIXME: some code below needs moving
295
296         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
297         lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
298                           os::slashify_path(os::getTmpDir() + "/lyxsocket"));
299
300         vector<string>::const_iterator cit = files.begin();
301         vector<string>::const_iterator end = files.end();
302         for (; cit != end; ++cit)
303                 view.view()->loadLyXFile(*cit, true);
304
305         // handle the batch commands the user asked for
306         if (!batch.empty())
307                 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
308
309         // enter the event loop
310         while (!finished) {
311                 if (fl_check_forms() == FL_EVENT) {
312                         XEvent ev;
313                         fl_XNextEvent(&ev);
314                         lyxerr[Debug::GUI]
315                                 << "Received unhandled X11 event" << endl
316                                 << "Type: " << ev.xany.type
317                                 << " Target: 0x" << hex << ev.xany.window
318                                 << dec << endl;
319                 }
320         }
321
322         // FIXME: breaks emergencyCleanup
323         delete lyxsocket;
324         delete lyxserver;
325 }
326
327
328 void exit()
329 {
330         finished = true;
331 }
332
333
334 void sync_events()
335 {
336         // FIXME
337 }
338
339
340 FuncStatus getStatus(FuncRequest const & /*ev*/)
341 {
342         // Nothing interesting to do here
343         return FuncStatus();
344 }
345
346 string const hexname(LColor_color col)
347 {
348         unsigned int r, g, b;
349         bool const success = getRGBColor(col, r, g, b);
350         if (!success) {
351                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
352                        << '"' << endl;
353                 return string();
354         }
355
356         ostringstream os;
357
358         os << setbase(16) << setfill('0')
359            << setw(2) << r
360            << setw(2) << g
361            << setw(2) << b;
362
363         return os.str();
364 }
365
366
367 void update_color(LColor_color col)
368 {
369         lyxColorHandler->getGCForeground(col);
370         lyxColorHandler->updateColor(col);
371 }
372
373
374 void update_fonts()
375 {
376         fontloader.update();
377 }
378
379
380 bool font_available(LyXFont const & font)
381 {
382         return fontloader.available(font);
383 }
384
385 namespace {
386
387 extern "C"
388 void C_read_callback(int, void * data)
389 {
390         LyXComm * comm = static_cast<LyXComm *>(data);
391         comm->read_ready();
392 }
393
394 extern "C"
395 void C_datasocket_callback(int, void * data)
396 {
397         LyXDataSocket * client = static_cast<LyXDataSocket *>(data);
398         client->server()->dataCallback(client);
399 }
400
401 extern "C"
402 void C_serversocket_callback(int, void * data)
403 {
404         LyXServerSocket * server = static_cast<LyXServerSocket *>(data);
405         server->serverCallback();
406 }
407
408 }
409
410 void set_read_callback(int fd, LyXComm * comm)
411 {
412         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
413 }
414
415 void remove_read_callback(int fd)
416 {
417         fl_remove_io_callback(fd, FL_READ, C_read_callback);
418 }
419
420 void set_datasocket_callback(LyXDataSocket * p)
421 {
422         fl_add_io_callback(p->fd(), FL_READ, C_datasocket_callback, p);
423 }
424
425 void remove_datasocket_callback(LyXDataSocket * p)
426 {
427         fl_remove_io_callback(p->fd(), FL_READ, C_datasocket_callback);
428 }
429
430 void set_serversocket_callback(LyXServerSocket * p)
431 {
432         fl_add_io_callback(p->fd(), FL_READ, C_serversocket_callback, p);
433 }
434
435 void remove_serversocket_callback(LyXServerSocket * p)
436 {
437         fl_remove_io_callback(p->fd(), FL_READ, C_serversocket_callback);
438 }
439
440 string const roman_font_name()
441 {
442         return "times";
443 }
444
445
446 string const sans_font_name()
447 {
448         return "helvetica";
449 }
450
451
452 string const typewriter_font_name()
453 {
454         return "courier";
455 }
456
457 }; // namespace lyx_gui