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());
158 void lyx_gui::parse_lyxrc()
163 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
165 if (lyxrc.popup_font_encoding.empty())
166 lyxrc.popup_font_encoding = lyxrc.font_norm;
167 // Set the font name for popups and menus
168 string boldfontname = lyxrc.popup_bold_font
169 + "-*-*-*-?-*-*-*-*-"
170 + lyxrc.popup_font_encoding;
171 // "?" means "scale that font"
172 string fontname = lyxrc.popup_normal_font
173 + "-*-*-*-?-*-*-*-*-"
174 + lyxrc.popup_font_encoding;
176 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
177 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
179 lyxerr << "Could not set menu font to "
180 << boldfontname << endl;
183 lyxerr << "Could not set popup font to "
186 if (bold < 0 && normal < 0) {
187 lyxerr << "Using 'helvetica' font for menus" << endl;
188 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
189 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
190 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
191 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
193 if (bold < 0 && normal < 0) {
194 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
195 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
200 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
202 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
204 fl_setpup_fontstyle(FL_NORMAL_STYLE);
205 fl_setpup_fontsize(FL_NORMAL_SIZE);
206 fl_setpup_color(FL_MCOL, FL_BLACK);
207 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
209 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
211 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
216 void lyx_gui::start(string const & batch, vector<string> files)
221 unsigned int width = 690;
222 unsigned int height = 510;
224 static const int geometryBitmask =
225 XParseGeometry(geometry,
226 &xpos, &ypos, &width, &height);
228 // if width is not set by geometry, check it against monitor width
229 if (!(geometryBitmask & 4)) {
230 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
231 if (WidthOfScreen(scr) - 8 < int(width))
232 width = WidthOfScreen(scr) - 8;
235 // if height is not set by geometry, check it against monitor height
236 if (!(geometryBitmask & 8)) {
237 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
238 if (HeightOfScreen(scr) - 24 < int(height))
239 height = HeightOfScreen(scr) - 24;
242 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
244 // recalculate xpos if it's not set
246 xpos = (WidthOfScreen(s) - width) / 2;
248 // recalculate ypos if it's not set
250 ypos = (HeightOfScreen(s) - height) / 2;
252 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
253 << "+" << xpos << "+" << ypos << endl;
255 XFormsView view(width, height);
256 view.show(xpos, ypos, "LyX");
261 // FIXME: some code below needs moving
263 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
265 vector<string>::const_iterator cit = files.begin();
266 vector<string>::const_iterator end = files.end();
267 for (; cit != end; ++cit) {
268 Buffer * b = bufferlist.loadLyXFile(*cit);
274 // switch to the last buffer successfully loaded
276 view.view()->buffer(last);
279 // handle the batch commands the user asked for
280 if (!batch.empty()) {
281 view.getLyXFunc()->verboseDispatch(batch, false);
284 // enter the event loop
286 if (fl_check_forms() == FL_EVENT) {
289 lyxerr << "Received unhandled X11 event" << endl;
290 lyxerr << "Type: 0x" << hex << ev.xany.type <<
291 " Target: 0x" << hex << ev.xany.window << endl;
300 // Called by the graphics cache to connect the appropriate frontend
301 // image loading routines to the LyX kernel.
302 void lyx_gui::init_graphics()
304 using namespace grfx;
306 #ifdef USE_XFORMS_IMAGE_LOADER
307 // connect the image loader based on the xforms library
308 Image::newImage = boost::bind(&xformsImage::newImage);
309 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
311 // connect the image loader based on the XPM library
312 Image::newImage = boost::bind(&ImageXPM::newImage);
313 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
318 string const lyx_gui::hexname(LColor::color col)
320 string const name = lcolor.getX11Name(col);
321 Display * const display = fl_get_display();
322 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
325 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
326 lyxerr << "X can't find color \""
327 << lcolor.getLyXName(col)
334 // Note that X stores the RGB values in the range 0 - 65535
335 // whilst we require them in the range 0 - 255.
336 os << setbase(16) << setfill('0')
337 << setw(2) << (xcol.red / 256)
338 << setw(2) << (xcol.green / 256)
339 << setw(2) << (xcol.blue / 256);
341 return os.str().c_str();
345 void lyx_gui::update_color(LColor::color col)
347 lyxColorHandler->updateColor(col);
351 void lyx_gui::update_fonts()
357 bool lyx_gui::font_available(LyXFont const & font)
359 return fontloader.available(font);