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;
66 /// set default GUI configuration
70 cntl.buttonFontSize = FL_NORMAL_SIZE;
71 cntl.browserFontSize = FL_NORMAL_SIZE;
72 cntl.labelFontSize = FL_NORMAL_SIZE;
73 cntl.choiceFontSize = FL_NORMAL_SIZE;
74 cntl.inputFontSize = FL_NORMAL_SIZE;
75 cntl.menuFontSize = FL_NORMAL_SIZE;
76 cntl.borderWidth = -1;
77 cntl.vclass = FL_DefaultVisual;
78 fl_set_defaults(FL_PDVisual
80 | FL_PDBrowserFontSize
85 | FL_PDBorderWidth, &cntl);
91 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
92 // We don't abort on BadWindow
93 if (xeev->error_code == BadWindow) {
94 lyxerr << "BadWindow received !" << endl;
95 lyxerr << "If you're using xforms 1.0 or greater, "
96 << " please report this to lyx-devel@lists.lyx.org" << endl;
101 LyX::emergencyCleanup();
103 // Get the reason for the crash.
105 XGetErrorText(display, xeev->error_code, etxt, 512);
106 lyxerr << etxt << " id: " << xeev->resourceid << endl;
107 // By doing an abort we get a nice backtrace. (hopefully)
114 /// read in geometry specification
120 void lyx_gui::parse_init(int & argc, char * argv[])
124 FL_CMD_OPT cmdopt[] = {
125 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
128 FL_resource res[] = {
129 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
132 const int num_res = sizeof(res)/sizeof(FL_resource);
134 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
136 // It appears that, in xforms >=0.89.5, fl_initialize()
137 // calls setlocale() and ruins our LC_NUMERIC setting.
140 fl_get_app_resources(res, num_res);
142 Display * display = fl_get_display();
145 lyxerr << "LyX: unable to access X display, exiting" << endl;
146 os::warn("Unable to access X display, exiting");
150 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
152 XSetErrorHandler(LyX_XErrHandler);
154 lyxColorHandler.reset(new LyXColorHandler());
156 using namespace grfx;
158 #ifdef USE_XFORMS_IMAGE_LOADER
159 // connect the image loader based on the xforms library
160 Image::newImage = boost::bind(&xformsImage::newImage);
161 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
163 // connect the image loader based on the XPM library
164 Image::newImage = boost::bind(&ImageXPM::newImage);
165 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
170 void lyx_gui::parse_lyxrc()
175 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
177 if (lyxrc.popup_font_encoding.empty())
178 lyxrc.popup_font_encoding = lyxrc.font_norm;
179 // Set the font name for popups and menus
180 string boldfontname = lyxrc.popup_bold_font
181 + "-*-*-*-?-*-*-*-*-"
182 + lyxrc.popup_font_encoding;
183 // "?" means "scale that font"
184 string fontname = lyxrc.popup_normal_font
185 + "-*-*-*-?-*-*-*-*-"
186 + lyxrc.popup_font_encoding;
188 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
189 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
191 lyxerr << "Could not set menu font to "
192 << boldfontname << endl;
195 lyxerr << "Could not set popup font to "
198 if (bold < 0 && normal < 0) {
199 lyxerr << "Using 'helvetica' font for menus" << endl;
200 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
201 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
202 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
203 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
205 if (bold < 0 && normal < 0) {
206 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
207 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
212 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
214 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
216 fl_setpup_fontstyle(FL_NORMAL_STYLE);
217 fl_setpup_fontsize(FL_NORMAL_SIZE);
218 fl_setpup_color(FL_MCOL, FL_BLACK);
219 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
221 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
223 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
228 void lyx_gui::start(string const & batch, vector<string> files)
233 unsigned int width = 690;
234 unsigned int height = 510;
236 static const int geometryBitmask =
237 XParseGeometry(geometry,
238 &xpos, &ypos, &width, &height);
240 // if width is not set by geometry, check it against monitor width
241 if (!(geometryBitmask & 4)) {
242 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
243 if (WidthOfScreen(scr) - 8 < int(width))
244 width = WidthOfScreen(scr) - 8;
247 // if height is not set by geometry, check it against monitor height
248 if (!(geometryBitmask & 8)) {
249 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
250 if (HeightOfScreen(scr) - 24 < int(height))
251 height = HeightOfScreen(scr) - 24;
254 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
256 // recalculate xpos if it's not set
258 xpos = (WidthOfScreen(s) - width) / 2;
260 // recalculate ypos if it's not set
262 ypos = (HeightOfScreen(s) - height) / 2;
264 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
265 << "+" << xpos << "+" << ypos << endl;
267 XFormsView view(width, height);
268 view.show(xpos, ypos, "LyX");
273 // FIXME: some code below needs moving
275 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
277 vector<string>::const_iterator cit = files.begin();
278 vector<string>::const_iterator end = files.end();
279 for (; cit != end; ++cit) {
280 Buffer * b = bufferlist.loadLyXFile(*cit);
286 // switch to the last buffer successfully loaded
288 view.view()->buffer(last);
291 // handle the batch commands the user asked for
292 if (!batch.empty()) {
293 view.getLyXFunc()->dispatch(batch);
296 // enter the event loop
298 if (fl_check_forms() == FL_EVENT) {
301 lyxerr << "Received unhandled X11 event" << endl;
302 lyxerr << "Type: 0x" << hex << ev.xany.type <<
303 " Target: 0x" << hex << ev.xany.window << endl;
312 string const lyx_gui::hexname(LColor::color col)
314 string const name = lcolor.getX11Name(col);
315 Display * const display = fl_get_display();
316 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
319 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
320 lyxerr << "X can't find color \""
321 << lcolor.getLyXName(col)
328 // Note that X stores the RGB values in the range 0 - 65535
329 // whilst we require them in the range 0 - 255.
330 os << setbase(16) << setfill('0')
331 << setw(2) << (xcol.red / 256)
332 << setw(2) << (xcol.green / 256)
333 << setw(2) << (xcol.blue / 256);
335 return os.str().c_str();
339 void lyx_gui::update_color(LColor::color col)
341 lyxColorHandler->updateColor(col);
345 void lyx_gui::update_fonts()
351 bool lyx_gui::font_available(LyXFont const & font)
353 return fontloader.available(font);
359 void C_read_callback(int, void * data)
361 LyXComm * comm = static_cast<LyXComm *>(data);
367 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
369 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);