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 /// estimate DPI from X server
72 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
73 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
74 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
78 /// set default GUI configuration
82 cntl.buttonFontSize = FL_NORMAL_SIZE;
83 cntl.browserFontSize = FL_NORMAL_SIZE;
84 cntl.labelFontSize = FL_NORMAL_SIZE;
85 cntl.choiceFontSize = FL_NORMAL_SIZE;
86 cntl.inputFontSize = FL_NORMAL_SIZE;
87 cntl.menuFontSize = FL_NORMAL_SIZE;
88 cntl.borderWidth = -1;
89 cntl.vclass = FL_DefaultVisual;
90 fl_set_defaults(FL_PDVisual
92 | FL_PDBrowserFontSize
97 | FL_PDBorderWidth, &cntl);
103 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
104 // We don't abort on BadWindow
105 if (xeev->error_code == BadWindow) {
106 lyxerr << "BadWindow received !" << endl;
107 lyxerr << "If you're using xforms 1.0 or greater, "
108 << " please report this to lyx-devel@lists.lyx.org" << endl;
113 LyX::emergencyCleanup();
115 // Get the reason for the crash.
117 XGetErrorText(display, xeev->error_code, etxt, 512);
118 lyxerr << etxt << " id: " << xeev->resourceid << endl;
119 // By doing an abort we get a nice backtrace. (hopefully)
126 /// read in geometry specification
132 void lyx_gui::parse_init(int & argc, char * argv[])
136 FL_CMD_OPT cmdopt[] = {
137 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
140 FL_resource res[] = {
141 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
144 const int num_res = sizeof(res)/sizeof(FL_resource);
146 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
148 // It appears that, in xforms >=0.89.5, fl_initialize()
149 // calls setlocale() and ruins our LC_NUMERIC setting.
152 fl_get_app_resources(res, num_res);
154 Display * display = fl_get_display();
157 lyxerr << "LyX: unable to access X display, exiting" << endl;
158 os::warn("Unable to access X display, exiting");
162 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
164 XSetErrorHandler(LyX_XErrHandler);
166 lyxColorHandler.reset(new LyXColorHandler());
168 using namespace grfx;
170 #ifdef USE_XFORMS_IMAGE_LOADER
171 // connect the image loader based on the xforms library
172 Image::newImage = boost::bind(&xformsImage::newImage);
173 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
175 // connect the image loader based on the XPM library
176 Image::newImage = boost::bind(&ImageXPM::newImage);
177 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
180 // must do this /before/ lyxrc gets read
181 lyxrc.dpi = getDPI();
185 void lyx_gui::parse_lyxrc()
187 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
189 if (lyxrc.popup_font_encoding.empty())
190 lyxrc.popup_font_encoding = lyxrc.font_norm;
191 // Set the font name for popups and menus
192 string boldfontname = lyxrc.popup_bold_font
193 + "-*-*-*-?-*-*-*-*-"
194 + lyxrc.popup_font_encoding;
195 // "?" means "scale that font"
196 string fontname = lyxrc.popup_normal_font
197 + "-*-*-*-?-*-*-*-*-"
198 + lyxrc.popup_font_encoding;
200 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
201 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
203 lyxerr << "Could not set menu font to "
204 << boldfontname << endl;
207 lyxerr << "Could not set popup font to "
210 if (bold < 0 && normal < 0) {
211 lyxerr << "Using 'helvetica' font for menus" << endl;
212 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
213 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
214 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
215 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
217 if (bold < 0 && normal < 0) {
218 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
219 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
224 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
226 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
228 fl_setpup_fontstyle(FL_NORMAL_STYLE);
229 fl_setpup_fontsize(FL_NORMAL_SIZE);
230 fl_setpup_color(FL_MCOL, FL_BLACK);
231 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
235 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240 void lyx_gui::start(string const & batch, vector<string> files)
245 unsigned int width = 690;
246 unsigned int height = 510;
248 static const int geometryBitmask =
249 XParseGeometry(geometry,
250 &xpos, &ypos, &width, &height);
252 // if width is not set by geometry, check it against monitor width
253 if (!(geometryBitmask & 4)) {
254 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
255 if (WidthOfScreen(scr) - 8 < int(width))
256 width = WidthOfScreen(scr) - 8;
259 // if height is not set by geometry, check it against monitor height
260 if (!(geometryBitmask & 8)) {
261 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
262 if (HeightOfScreen(scr) - 24 < int(height))
263 height = HeightOfScreen(scr) - 24;
266 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
268 // recalculate xpos if it's not set
270 xpos = (WidthOfScreen(s) - width) / 2;
272 // recalculate ypos if it's not set
274 ypos = (HeightOfScreen(s) - height) / 2;
276 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
277 << "+" << xpos << "+" << ypos << endl;
279 XFormsView view(width, height);
280 view.show(xpos, ypos, "LyX");
285 // FIXME: some code below needs moving
287 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
289 vector<string>::const_iterator cit = files.begin();
290 vector<string>::const_iterator end = files.end();
291 for (; cit != end; ++cit) {
292 Buffer * b = bufferlist.loadLyXFile(*cit);
298 // switch to the last buffer successfully loaded
300 view.view()->buffer(last);
303 // handle the batch commands the user asked for
304 if (!batch.empty()) {
305 view.getLyXFunc().dispatch(batch);
308 // enter the event loop
310 if (fl_check_forms() == FL_EVENT) {
313 lyxerr << "Received unhandled X11 event" << endl;
314 lyxerr << "Type: 0x" << hex << ev.xany.type <<
315 " Target: 0x" << hex << ev.xany.window << dec << endl;
319 // FIXME: breaks emergencyCleanup
330 string const lyx_gui::hexname(LColor::color col)
332 string const name = lcolor.getX11Name(col);
333 Display * const display = fl_get_display();
334 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
337 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
338 lyxerr << "X can't find color \""
339 << lcolor.getLyXName(col)
346 // Note that X stores the RGB values in the range 0 - 65535
347 // whilst we require them in the range 0 - 255.
348 os << setbase(16) << setfill('0')
349 << setw(2) << (xcol.red / 256)
350 << setw(2) << (xcol.green / 256)
351 << setw(2) << (xcol.blue / 256);
353 return os.str().c_str();
357 void lyx_gui::update_color(LColor::color col)
359 lyxColorHandler->updateColor(col);
363 void lyx_gui::update_fonts()
369 bool lyx_gui::font_available(LyXFont const & font)
371 return fontloader.available(font);
377 void C_read_callback(int, void * data)
379 LyXComm * comm = static_cast<LyXComm *>(data);
385 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
387 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
391 void lyx_gui::remove_read_callback(int fd)
393 fl_remove_io_callback(fd, FL_READ, C_read_callback);