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
58 extern BufferList bufferlist;
60 // FIXME: wrong place !
61 LyXServer * lyxserver;
66 bool finished = false;
68 /// set default GUI configuration
72 cntl.buttonFontSize = FL_NORMAL_SIZE;
73 cntl.browserFontSize = FL_NORMAL_SIZE;
74 cntl.labelFontSize = FL_NORMAL_SIZE;
75 cntl.choiceFontSize = FL_NORMAL_SIZE;
76 cntl.inputFontSize = FL_NORMAL_SIZE;
77 cntl.menuFontSize = FL_NORMAL_SIZE;
78 cntl.borderWidth = -1;
79 cntl.vclass = FL_DefaultVisual;
80 fl_set_defaults(FL_PDVisual
82 | FL_PDBrowserFontSize
87 | FL_PDBorderWidth, &cntl);
93 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
94 // We don't abort on BadWindow
95 if (xeev->error_code == BadWindow) {
96 lyxerr << "BadWindow received !" << endl;
97 lyxerr << "If you're using xforms 1.0 or greater, "
98 << " please report this to lyx-devel@lists.lyx.org" << endl;
103 LyX::emergencyCleanup();
105 // Get the reason for the crash.
107 XGetErrorText(display, xeev->error_code, etxt, 512);
108 lyxerr << etxt << " id: " << xeev->resourceid << endl;
109 // By doing an abort we get a nice backtrace. (hopefully)
116 /// read in geometry specification
122 void lyx_gui::parse_init(int & argc, char * argv[])
126 FL_CMD_OPT cmdopt[] = {
127 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
130 FL_resource res[] = {
131 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
134 const int num_res = sizeof(res)/sizeof(FL_resource);
136 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
138 // It appears that, in xforms >=0.89.5, fl_initialize()
139 // calls setlocale() and ruins our LC_NUMERIC setting.
142 fl_get_app_resources(res, num_res);
144 Display * display = fl_get_display();
147 lyxerr << "LyX: unable to access X display, exiting" << endl;
148 os::warn("Unable to access X display, exiting");
152 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
154 XSetErrorHandler(LyX_XErrHandler);
156 lyxColorHandler.reset(new LyXColorHandler());
158 using namespace grfx;
160 #ifdef USE_XFORMS_IMAGE_LOADER
161 // connect the image loader based on the xforms library
162 Image::newImage = boost::bind(&xformsImage::newImage);
163 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
165 // connect the image loader based on the XPM library
166 Image::newImage = boost::bind(&ImageXPM::newImage);
167 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
172 void lyx_gui::parse_lyxrc()
177 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
179 if (lyxrc.popup_font_encoding.empty())
180 lyxrc.popup_font_encoding = lyxrc.font_norm;
181 // Set the font name for popups and menus
182 string boldfontname = lyxrc.popup_bold_font
183 + "-*-*-*-?-*-*-*-*-"
184 + lyxrc.popup_font_encoding;
185 // "?" means "scale that font"
186 string fontname = lyxrc.popup_normal_font
187 + "-*-*-*-?-*-*-*-*-"
188 + lyxrc.popup_font_encoding;
190 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
191 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
193 lyxerr << "Could not set menu font to "
194 << boldfontname << endl;
197 lyxerr << "Could not set popup font to "
200 if (bold < 0 && normal < 0) {
201 lyxerr << "Using 'helvetica' font for menus" << endl;
202 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
203 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
204 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
205 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
207 if (bold < 0 && normal < 0) {
208 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
209 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
214 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
216 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
218 fl_setpup_fontstyle(FL_NORMAL_STYLE);
219 fl_setpup_fontsize(FL_NORMAL_SIZE);
220 fl_setpup_color(FL_MCOL, FL_BLACK);
221 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
223 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
225 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
230 void lyx_gui::start(string const & batch, vector<string> files)
235 unsigned int width = 690;
236 unsigned int height = 510;
238 static const int geometryBitmask =
239 XParseGeometry(geometry,
240 &xpos, &ypos, &width, &height);
242 // if width is not set by geometry, check it against monitor width
243 if (!(geometryBitmask & 4)) {
244 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
245 if (WidthOfScreen(scr) - 8 < int(width))
246 width = WidthOfScreen(scr) - 8;
249 // if height is not set by geometry, check it against monitor height
250 if (!(geometryBitmask & 8)) {
251 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
252 if (HeightOfScreen(scr) - 24 < int(height))
253 height = HeightOfScreen(scr) - 24;
256 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
258 // recalculate xpos if it's not set
260 xpos = (WidthOfScreen(s) - width) / 2;
262 // recalculate ypos if it's not set
264 ypos = (HeightOfScreen(s) - height) / 2;
266 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
267 << "+" << xpos << "+" << ypos << endl;
269 XFormsView view(width, height);
270 view.show(xpos, ypos, "LyX");
275 // FIXME: some code below needs moving
277 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
279 vector<string>::const_iterator cit = files.begin();
280 vector<string>::const_iterator end = files.end();
281 for (; cit != end; ++cit) {
282 Buffer * b = bufferlist.loadLyXFile(*cit);
288 // switch to the last buffer successfully loaded
290 view.view()->buffer(last);
293 // handle the batch commands the user asked for
294 if (!batch.empty()) {
295 view.getLyXFunc()->dispatch(batch);
298 // enter the event loop
300 if (fl_check_forms() == FL_EVENT) {
303 lyxerr << "Received unhandled X11 event" << endl;
304 lyxerr << "Type: 0x" << hex << ev.xany.type <<
305 " Target: 0x" << hex << ev.xany.window << dec << endl;
309 // FIXME: breaks emergencyCleanup
320 string const lyx_gui::hexname(LColor::color col)
322 string const name = lcolor.getX11Name(col);
323 Display * const display = fl_get_display();
324 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
327 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
328 lyxerr << "X can't find color \""
329 << lcolor.getLyXName(col)
336 // Note that X stores the RGB values in the range 0 - 65535
337 // whilst we require them in the range 0 - 255.
338 os << setbase(16) << setfill('0')
339 << setw(2) << (xcol.red / 256)
340 << setw(2) << (xcol.green / 256)
341 << setw(2) << (xcol.blue / 256);
343 return os.str().c_str();
347 void lyx_gui::update_color(LColor::color col)
349 lyxColorHandler->updateColor(col);
353 void lyx_gui::update_fonts()
359 bool lyx_gui::font_available(LyXFont const & font)
361 return fontloader.available(font);
367 void C_read_callback(int, void * data)
369 LyXComm * comm = static_cast<LyXComm *>(data);
375 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
377 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);