]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
Flying high and free...
[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
16 #include "support/lyxlib.h"
17 #include "support/os.h"
18 #include "support/filetools.h"
19 #include "support/path_defines.h"
20
21 #include "debug.h"
22 #include "gettext.h"
23
24 #include "lyx_main.h"
25 #include "lyxrc.h"
26 #include "lyxfont.h"
27 #include "graphics/LoaderQueue.h"
28
29 // FIXME: move this stuff out again
30 #include "bufferlist.h"
31 #include "buffer_funcs.h"
32 #include "lyxfunc.h"
33 #include "lyxserver.h"
34 #include "BufferView.h"
35 #include "XFormsView.h"
36
37 #include "lyx_forms.h"
38 #include "ColorHandler.h"
39 #include "xforms_helpers.h"
40 #include "xfont_loader.h"
41 #include "xformsImage.h"
42
43 #include "Lsstream.h"
44 #include <iomanip>
45 #include <fcntl.h>
46 #include <boost/bind.hpp>
47
48 using namespace lyx::support;
49
50 #ifndef CXX_GLOBAL_CSTD
51 using std::exit;
52 #endif
53
54 using std::vector;
55 using std::hex;
56 using std::dec;
57 using std::endl;
58 using std::setbase;
59 using std::setfill;
60 using std::setw;
61
62 extern BufferList bufferlist;
63
64 // FIXME: wrong place !
65 LyXServer * lyxserver;
66
67 namespace {
68
69 /// quit lyx
70 bool finished = false;
71
72 /// estimate DPI from X server
73 float getDPI()
74 {
75         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
76         return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
77                 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
78 }
79
80
81 /// set default GUI configuration
82 void setDefaults()
83 {
84         FL_IOPT cntl;
85         cntl.buttonFontSize = FL_NORMAL_SIZE;
86         cntl.browserFontSize = FL_NORMAL_SIZE;
87         cntl.labelFontSize = FL_NORMAL_SIZE;
88         cntl.choiceFontSize = FL_NORMAL_SIZE;
89         cntl.inputFontSize = FL_NORMAL_SIZE;
90         cntl.menuFontSize  = FL_NORMAL_SIZE;
91         cntl.borderWidth = -1;
92         cntl.vclass = FL_DefaultVisual;
93         fl_set_defaults(FL_PDVisual
94                         | FL_PDButtonFontSize
95                         | FL_PDBrowserFontSize
96                         | FL_PDLabelFontSize
97                         | FL_PDChoiceFontSize
98                         | FL_PDInputFontSize
99                         | FL_PDMenuFontSize
100                         | FL_PDBorderWidth, &cntl);
101 }
102
103
104 extern "C" {
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 !" << endl;
110                 lyxerr << "If you're using xforms 1.0 or greater, "
111                         << " please report this to lyx-devel@lists.lyx.org" << 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 << 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 namespace lyx_gui {
136
137 bool use_gui = true;
138
139
140 void parse_init(int & argc, char * argv[])
141 {
142         setDefaults();
143
144         FL_CMD_OPT cmdopt[] = {
145                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
146         };
147
148         FL_resource res[] = {
149                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
150         };
151
152         const int num_res = sizeof(res)/sizeof(FL_resource);
153
154         fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
155
156         // It appears that, in xforms >=0.89.5, fl_initialize()
157         // calls setlocale() and ruins our LC_NUMERIC setting.
158         locale_init();
159
160         fl_get_app_resources(res, num_res);
161
162         Display * display = fl_get_display();
163
164         if (!display) {
165                 lyxerr << "LyX: unable to access X display, exiting" << endl;
166                 os::warn("Unable to access X display, exiting");
167                 ::exit(1);
168         }
169
170         fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
171
172         XSetErrorHandler(LyX_XErrHandler);
173
174         lyxColorHandler.reset(new LyXColorHandler());
175
176         using namespace lyx::graphics;
177
178         // connect the image loader based on the xforms library
179         Image::newImage = boost::bind(&xformsImage::newImage);
180         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
181
182         // must do this /before/ lyxrc gets read
183         lyxrc.dpi = getDPI();
184
185         LoaderQueue::setPriority(10,100);
186 }
187
188
189 void parse_lyxrc()
190 {
191         XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
192
193         if (lyxrc.popup_font_encoding.empty())
194                 lyxrc.popup_font_encoding = lyxrc.font_norm;
195         // Set the font name for popups and menus
196         string boldfontname = lyxrc.popup_bold_font
197                                + "-*-*-*-?-*-*-*-*-"
198                                + lyxrc.popup_font_encoding;
199                 // "?" means "scale that font"
200         string fontname = lyxrc.popup_normal_font
201                                + "-*-*-*-?-*-*-*-*-"
202                                + lyxrc.popup_font_encoding;
203
204         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
205         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
206         if (bold < 0)
207                 lyxerr << "Could not set menu font to "
208                        << boldfontname << endl;
209
210         if (normal < 0)
211                 lyxerr << "Could not set popup font to "
212                        << fontname << endl;
213
214         if (bold < 0 && normal < 0) {
215                 lyxerr << "Using 'helvetica' font for menus" << endl;
216                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
217                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
218                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
219                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
220
221                 if (bold < 0 && normal < 0) {
222                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
223                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
224                         normal = bold = 0;
225                 }
226         }
227         if (bold < 0)
228                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
229         else if (normal < 0)
230                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
231
232         fl_setpup_fontstyle(FL_NORMAL_STYLE);
233         fl_setpup_fontsize(FL_NORMAL_SIZE);
234         fl_setpup_color(FL_MCOL, FL_BLACK);
235         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
236         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
237 }
238
239
240 void start(string const & batch, vector<string> const & files)
241 {
242         // initial geometry
243         int xpos = -1;
244         int ypos = -1;
245         unsigned int width = 690;
246         unsigned int height = 510;
247
248         int const geometryBitmask =
249                 XParseGeometry(geometry,
250                                &xpos, &ypos, &width, &height);
251
252         // if width is not set by geometry, check it against monitor width
253         if (!(geometryBitmask & WidthValue)) {
254                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
255                 if (WidthOfScreen(scr) - 8 < int(width))
256                         width = WidthOfScreen(scr) - 8;
257         }
258
259         // if height is not set by geometry, check it against monitor height
260         if (!(geometryBitmask & HeightValue)) {
261                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
262                 if (HeightOfScreen(scr) - 24 < int(height))
263                         height = HeightOfScreen(scr) - 24;
264         }
265
266         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
267
268         // recalculate xpos if it's not set
269         if (xpos == -1)
270                 xpos = (WidthOfScreen(s) - width) / 2;
271
272         // recalculate ypos if it's not set
273         if (ypos == -1)
274                 ypos = (HeightOfScreen(s) - height) / 2;
275
276         lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
277                            << '+' << xpos << '+' << ypos << endl;
278
279         XFormsView view(width, height);
280         view.show(xpos, ypos, "LyX");
281         view.init();
282
283         // FIXME: some code below needs moving
284
285         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
286
287         vector<string>::const_iterator cit = files.begin();
288         vector<string>::const_iterator end = files.end();
289         for (; cit != end; ++cit)
290                 view.view()->loadLyXFile(*cit, true);
291
292         // handle the batch commands the user asked for
293         if (!batch.empty()) {
294                 view.getLyXFunc().dispatch(batch);
295         }
296
297         // enter the event loop
298         while (!finished) {
299                 if (fl_check_forms() == FL_EVENT) {
300                         XEvent ev;
301                         fl_XNextEvent(&ev);
302                         lyxerr[Debug::GUI]
303                                 << "Received unhandled X11 event" << endl
304                                 << "Type: " << ev.xany.type
305                                 << " Target: 0x" << hex << ev.xany.window
306                                 << dec << endl;
307                 }
308         }
309
310         // FIXME: breaks emergencyCleanup
311         delete lyxserver;
312 }
313
314
315 void exit()
316 {
317         finished = true;
318 }
319
320
321 void sync_events()
322 {
323         // FIXME
324 }
325
326
327 FuncStatus getStatus(FuncRequest const & /*ev*/)
328 {
329         // Nothing interesting to do here
330         return FuncStatus();
331 }
332
333 string const hexname(LColor::color col)
334 {
335         unsigned int r, g, b;
336         bool const success = getRGBColor(col, r, g, b);
337         if (!success) {
338                 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
339                        << '"' << endl;
340                 return string();
341         }
342
343         ostringstream os;
344
345         os << setbase(16) << setfill('0')
346            << setw(2) << r
347            << setw(2) << g
348            << setw(2) << b;
349
350         return STRCONV(os.str());
351 }
352
353
354 void update_color(LColor::color col)
355 {
356         lyxColorHandler->updateColor(col);
357 }
358
359
360 void update_fonts()
361 {
362         fontloader.update();
363 }
364
365
366 bool font_available(LyXFont const & font)
367 {
368         return fontloader.available(font);
369 }
370
371 namespace {
372
373 extern "C"
374 void C_read_callback(int, void * data)
375 {
376         LyXComm * comm = static_cast<LyXComm *>(data);
377         comm->read_ready();
378 }
379
380 }
381
382 void set_read_callback(int fd, LyXComm * comm)
383 {
384         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
385 }
386
387
388 void remove_read_callback(int fd)
389 {
390         fl_remove_io_callback(fd, FL_READ, C_read_callback);
391 }
392
393
394 string const roman_font_name()
395 {
396         return "times";
397 }
398
399
400 string const sans_font_name()
401 {
402         return "helvetica";
403 }
404
405
406 string const typewriter_font_name()
407 {
408         return "courier";
409 }
410
411 }; // namespace lyx_gui