]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/lyx_gui.C
fix crash with "save as"
[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 unknown
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
20 #include "debug.h"
21 #include "gettext.h"
22
23 #include "lyx_main.h"
24 #include "lyxrc.h"
25 #include "lyxfont.h"
26
27 // FIXME: move this stuff out again
28 #include "bufferlist.h"
29 #include "lyxfunc.h"
30 #include "lyxserver.h"
31 #include "BufferView.h"
32 #include "XFormsView.h"
33
34 #include FORMS_H_LOCATION
35 #include "ColorHandler.h"
36 #include "xforms_helpers.h"
37 #include "xfont_loader.h"
38 #ifdef USE_XFORMS_IMAGE_LOADER
39 #include "xformsImage.h"
40 #else
41 #include "graphics/GraphicsImageXPM.h"
42 #endif
43
44 #include "Lsstream.h"
45 #include <iomanip>
46 #include <fcntl.h>
47 #include <boost/bind.hpp>
48
49 #ifndef CXX_GLOBAL_CSTD
50 using std::exit;
51 #endif
52
53 using std::vector;
54 using std::hex;
55 using std::dec;
56 using std::endl;
57 using std::setbase;
58 using std::setfill;
59 using std::setw;
60
61 extern BufferList bufferlist;
62
63 // FIXME: wrong place !
64 LyXServer * lyxserver;
65
66 namespace {
67
68 /// quit lyx
69 bool finished = false;
70
71 /// estimate DPI from X server
72 float getDPI()
73 {
74         Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
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 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
106         // We don't abort on BadWindow
107         if (xeev->error_code == BadWindow) {
108                 lyxerr << "BadWindow received !" << endl;
109                 lyxerr << "If you're using xforms 1.0 or greater, "
110                         << " please report this to lyx-devel@lists.lyx.org" << endl;
111                 return 0;
112         }
113
114         // emergency cleanup
115         LyX::emergencyCleanup();
116
117         // Get the reason for the crash.
118         char etxt[513];
119         XGetErrorText(display, xeev->error_code, etxt, 512);
120         lyxerr << etxt << " id: " << xeev->resourceid << endl;
121         // By doing an abort we get a nice backtrace. (hopefully)
122         lyx::abort();
123         return 0;
124 }
125
126 }
127
128 /// read in geometry specification
129 char geometry[40];
130
131 } // namespace anon
132
133
134 void lyx_gui::parse_init(int & argc, char * argv[])
135 {
136         setDefaults();
137
138         FL_CMD_OPT cmdopt[] = {
139                 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
140         };
141
142         FL_resource res[] = {
143                 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
144         };
145
146         const int num_res = sizeof(res)/sizeof(FL_resource);
147
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         locale_init();
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" << endl;
160                 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         lyxColorHandler.reset(new LyXColorHandler());
169
170         using namespace grfx;
171
172 #ifdef USE_XFORMS_IMAGE_LOADER
173         // connect the image loader based on the xforms library
174         Image::newImage = boost::bind(&xformsImage::newImage);
175         Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
176 #else
177         // connect the image loader based on the XPM library
178         Image::newImage = boost::bind(&ImageXPM::newImage);
179         Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
180 #endif
181
182         // must do this /before/ lyxrc gets read
183         lyxrc.dpi = getDPI();
184 }
185
186
187 void lyx_gui::parse_lyxrc()
188 {
189         XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
190
191         if (lyxrc.popup_font_encoding.empty())
192                 lyxrc.popup_font_encoding = lyxrc.font_norm;
193         // Set the font name for popups and menus
194         string boldfontname = lyxrc.popup_bold_font
195                                + "-*-*-*-?-*-*-*-*-"
196                                + lyxrc.popup_font_encoding;
197                 // "?" means "scale that font"
198         string fontname = lyxrc.popup_normal_font
199                                + "-*-*-*-?-*-*-*-*-"
200                                + lyxrc.popup_font_encoding;
201
202         int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
203         int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
204         if (bold < 0)
205                 lyxerr << "Could not set menu font to "
206                        << boldfontname << endl;
207
208         if (normal < 0)
209                 lyxerr << "Could not set popup font to "
210                        << fontname << endl;
211
212         if (bold < 0 && normal < 0) {
213                 lyxerr << "Using 'helvetica' font for menus" << endl;
214                 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
215                 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
216                 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
217                 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
218
219                 if (bold < 0 && normal < 0) {
220                         lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
221                         fl_set_font_name(FL_NORMAL_STYLE, "fixed");
222                         normal = bold = 0;
223                 }
224         }
225         if (bold < 0)
226                 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
227         else if (normal < 0)
228                 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
229
230         fl_setpup_fontstyle(FL_NORMAL_STYLE);
231         fl_setpup_fontsize(FL_NORMAL_SIZE);
232         fl_setpup_color(FL_MCOL, FL_BLACK);
233         fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
234 #if FL_REVISION < 89
235         fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
236 #else
237         fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
238 #endif
239 }
240
241
242 void lyx_gui::start(string const & batch, vector<string> const & files)
243 {
244         // initial geometry
245         int xpos = -1;
246         int ypos = -1;
247         unsigned int width = 690;
248         unsigned int height = 510;
249
250         int const geometryBitmask =
251                 XParseGeometry(geometry,
252                                &xpos, &ypos, &width, &height);
253
254         // if width is not set by geometry, check it against monitor width
255         if (!(geometryBitmask & WidthValue)) {
256                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
257                 if (WidthOfScreen(scr) - 8 < int(width))
258                         width = WidthOfScreen(scr) - 8;
259         }
260
261         // if height is not set by geometry, check it against monitor height
262         if (!(geometryBitmask & HeightValue)) {
263                 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
264                 if (HeightOfScreen(scr) - 24 < int(height))
265                         height = HeightOfScreen(scr) - 24;
266         }
267
268         Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
269
270         // recalculate xpos if it's not set
271         if (xpos == -1)
272                 xpos = (WidthOfScreen(s) - width) / 2;
273
274         // recalculate ypos if it's not set
275         if (ypos == -1)
276                 ypos = (HeightOfScreen(s) - height) / 2;
277
278         lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
279                 << "+" << xpos << "+" << ypos << endl;
280
281         XFormsView view(width, height);
282         view.show(xpos, ypos, "LyX");
283         view.init();
284
285         Buffer * last = 0;
286
287         // FIXME: some code below needs moving
288
289         lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
290
291         vector<string>::const_iterator cit = files.begin();
292         vector<string>::const_iterator end = files.end();
293         for (; cit != end; ++cit) {
294                 Buffer * b = bufferlist.loadLyXFile(*cit);
295                 if (b) {
296                         last = b;
297                 }
298         }
299
300         // switch to the last buffer successfully loaded
301         if (last) {
302                 view.view()->buffer(last);
303         }
304
305         // handle the batch commands the user asked for
306         if (!batch.empty()) {
307                 view.getLyXFunc().dispatch(batch);
308         }
309
310         // enter the event loop
311         while (!finished) {
312                 if (fl_check_forms() == FL_EVENT) {
313                         XEvent ev;
314                         fl_XNextEvent(&ev);
315                         lyxerr << "Received unhandled X11 event" << endl;
316                         lyxerr << "Type: " << ev.xany.type
317                                << " Target: 0x" << hex << ev.xany.window
318                                << dec << endl;
319                 }
320         }
321
322         // FIXME: breaks emergencyCleanup
323         delete lyxserver;
324 }
325
326
327 void lyx_gui::exit()
328 {
329         finished = true;
330 }
331
332
333 string const lyx_gui::hexname(LColor::color col)
334 {
335         string const name = lcolor.getX11Name(col);
336         Display * const display = fl_get_display();
337         Colormap const cmap = fl_state[fl_get_vclass()].colormap;
338         XColor xcol, ccol;
339
340         if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
341                         lyxerr << "X can't find color \""
342                                << lcolor.getLyXName(col)
343                                << "\"" << endl;
344                         return string();
345         }
346
347         ostringstream os;
348
349         // Note that X stores the RGB values in the range 0 - 65535
350         // whilst we require them in the range 0 - 255.
351         os << setbase(16) << setfill('0')
352            << setw(2) << (xcol.red   / 256)
353            << setw(2) << (xcol.green / 256)
354            << setw(2) << (xcol.blue  / 256);
355
356         return STRCONV(os.str());
357 }
358
359
360 void lyx_gui::update_color(LColor::color col)
361 {
362         lyxColorHandler->updateColor(col);
363 }
364
365
366 void lyx_gui::update_fonts()
367 {
368         fontloader.update();
369 }
370
371
372 bool lyx_gui::font_available(LyXFont const & font)
373 {
374         return fontloader.available(font);
375 }
376
377 namespace {
378
379 extern "C"
380 void C_read_callback(int, void * data)
381 {
382         LyXComm * comm = static_cast<LyXComm *>(data);
383         comm->read_ready();
384 }
385
386 }
387
388 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
389 {
390         fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
391 }
392
393
394 void lyx_gui::remove_read_callback(int fd)
395 {
396         fl_remove_io_callback(fd, FL_READ, C_read_callback);
397 }