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