3 * Copyright 2002 the LyX Team
4 * Read the file COPYING
7 * \author John Levon <moz@compsoc.man.ac.uk>
14 #include "support/lyxlib.h"
15 #include "support/os.h"
16 #include "support/filetools.h"
25 // FIXME: move this stuff out again
26 #include "bufferlist.h"
28 #include "lyxserver.h"
29 #include "BufferView.h"
30 #include "XFormsView.h"
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"
39 #include "graphics/GraphicsImageXPM.h"
45 #include <boost/bind.hpp>
47 #ifndef CXX_GLOBAL_CSTD
59 extern BufferList bufferlist;
61 // FIXME: wrong place !
62 LyXServer * lyxserver;
67 bool finished = false;
69 /// set default GUI configuration
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
83 | FL_PDBrowserFontSize
88 | FL_PDBorderWidth, &cntl);
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;
104 LyX::emergencyCleanup();
106 // Get the reason for the crash.
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)
117 /// read in geometry specification
123 void lyx_gui::parse_init(int & argc, char * argv[])
127 FL_CMD_OPT cmdopt[] = {
128 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
131 FL_resource res[] = {
132 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
135 const int num_res = sizeof(res)/sizeof(FL_resource);
137 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
139 // It appears that, in xforms >=0.89.5, fl_initialize()
140 // calls setlocale() and ruins our LC_NUMERIC setting.
143 fl_get_app_resources(res, num_res);
145 Display * display = fl_get_display();
148 lyxerr << "LyX: unable to access X display, exiting" << endl;
149 os::warn("Unable to access X display, exiting");
153 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
155 XSetErrorHandler(LyX_XErrHandler);
157 lyxColorHandler.reset(new LyXColorHandler());
159 using namespace grfx;
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);
166 // connect the image loader based on the XPM library
167 Image::newImage = boost::bind(&ImageXPM::newImage);
168 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
173 void lyx_gui::parse_lyxrc()
178 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
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;
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());
194 lyxerr << "Could not set menu font to "
195 << boldfontname << endl;
198 lyxerr << "Could not set popup font to "
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());
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");
215 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
217 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
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);
224 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
226 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
231 void lyx_gui::start(string const & batch, vector<string> files)
236 unsigned int width = 690;
237 unsigned int height = 510;
239 static const int geometryBitmask =
240 XParseGeometry(geometry,
241 &xpos, &ypos, &width, &height);
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;
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;
257 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
259 // recalculate xpos if it's not set
261 xpos = (WidthOfScreen(s) - width) / 2;
263 // recalculate ypos if it's not set
265 ypos = (HeightOfScreen(s) - height) / 2;
267 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
268 << "+" << xpos << "+" << ypos << endl;
270 XFormsView view(width, height);
271 view.show(xpos, ypos, "LyX");
276 // FIXME: some code below needs moving
278 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
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);
289 // switch to the last buffer successfully loaded
291 view.view()->buffer(last);
294 // handle the batch commands the user asked for
295 if (!batch.empty()) {
296 view.getLyXFunc()->dispatch(batch);
299 // enter the event loop
301 if (fl_check_forms() == FL_EVENT) {
304 lyxerr << "Received unhandled X11 event" << endl;
305 lyxerr << "Type: 0x" << hex << ev.xany.type <<
306 " Target: 0x" << hex << ev.xany.window << dec << endl;
310 // FIXME: breaks emergencyCleanup
321 string const lyx_gui::hexname(LColor::color col)
323 string const name = lcolor.getX11Name(col);
324 Display * const display = fl_get_display();
325 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
328 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
329 lyxerr << "X can't find color \""
330 << lcolor.getLyXName(col)
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);
344 return os.str().c_str();
348 void lyx_gui::update_color(LColor::color col)
350 lyxColorHandler->updateColor(col);
354 void lyx_gui::update_fonts()
360 bool lyx_gui::font_available(LyXFont const & font)
362 return fontloader.available(font);
368 void C_read_callback(int, void * data)
370 LyXComm * comm = static_cast<LyXComm *>(data);
376 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
378 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
382 void lyx_gui::remove_read_callback(int fd)
384 fl_remove_io_callback(fd, FL_READ, C_read_callback);