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"
24 // FIXME: move this stuff out again
25 #include "bufferlist.h"
27 #include "lyxserver.h"
28 #include "BufferView.h"
29 #include "XFormsView.h"
31 #include FORMS_H_LOCATION
32 #include "ColorHandler.h"
33 #include "xforms_helpers.h"
34 #ifdef USE_XFORMS_IMAGE_LOADER
35 #include "xformsImage.h"
37 #include "graphics/GraphicsImageXPM.h"
43 #include <boost/bind.hpp>
45 #ifndef CXX_GLOBAL_CSTD
57 extern BufferList bufferlist;
59 // FIXME: wrong place !
60 LyXServer * lyxserver;
64 /// set default GUI configuration
68 cntl.buttonFontSize = FL_NORMAL_SIZE;
69 cntl.browserFontSize = FL_NORMAL_SIZE;
70 cntl.labelFontSize = FL_NORMAL_SIZE;
71 cntl.choiceFontSize = FL_NORMAL_SIZE;
72 cntl.inputFontSize = FL_NORMAL_SIZE;
73 cntl.menuFontSize = FL_NORMAL_SIZE;
74 cntl.borderWidth = -1;
75 cntl.vclass = FL_DefaultVisual;
76 fl_set_defaults(FL_PDVisual
78 | FL_PDBrowserFontSize
83 | FL_PDBorderWidth, &cntl);
89 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
90 // We don't abort on BadWindow
91 if (xeev->error_code == BadWindow) {
92 lyxerr << "BadWindow received !" << endl;
93 lyxerr << "If you're using xforms 1.0 or greater, "
94 << " please report this to lyx-devel@lists.lyx.org" << endl;
99 LyX::emergencyCleanup();
101 // Get the reason for the crash.
103 XGetErrorText(display, xeev->error_code, etxt, 512);
104 lyxerr << etxt << " id: " << xeev->resourceid << endl;
105 // By doing an abort we get a nice backtrace. (hopefully)
112 /// read in geometry specification
118 void lyx_gui::parse_init(int & argc, char * argv[])
122 FL_CMD_OPT cmdopt[] = {
123 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
126 FL_resource res[] = {
127 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
130 const int num_res = sizeof(res)/sizeof(FL_resource);
132 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
134 // It appears that, in xforms >=0.89.5, fl_initialize()
135 // calls setlocale() and ruins our LC_NUMERIC setting.
138 fl_get_app_resources(res, num_res);
140 Display * display = fl_get_display();
143 lyxerr << "LyX: unable to access X display, exiting" << endl;
144 os::warn("Unable to access X display, exiting");
148 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
150 XSetErrorHandler(LyX_XErrHandler);
152 lyxColorHandler.reset(new LyXColorHandler());
156 void lyx_gui::parse_lyxrc()
161 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
163 if (lyxrc.popup_font_encoding.empty())
164 lyxrc.popup_font_encoding = lyxrc.font_norm;
165 // Set the font name for popups and menus
166 string boldfontname = lyxrc.popup_bold_font
167 + "-*-*-*-?-*-*-*-*-"
168 + lyxrc.popup_font_encoding;
169 // "?" means "scale that font"
170 string fontname = lyxrc.popup_normal_font
171 + "-*-*-*-?-*-*-*-*-"
172 + lyxrc.popup_font_encoding;
174 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
175 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
177 lyxerr << "Could not set menu font to "
178 << boldfontname << endl;
181 lyxerr << "Could not set popup font to "
184 if (bold < 0 && normal < 0) {
185 lyxerr << "Using 'helvetica' font for menus" << endl;
186 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
187 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
188 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
189 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
191 if (bold < 0 && normal < 0) {
192 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
193 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
198 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
200 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
202 fl_setpup_fontstyle(FL_NORMAL_STYLE);
203 fl_setpup_fontsize(FL_NORMAL_SIZE);
204 fl_setpup_color(FL_MCOL, FL_BLACK);
205 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
207 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
209 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
214 void lyx_gui::start(string const & batch, vector<string> files)
219 unsigned int width = 690;
220 unsigned int height = 510;
222 static const int geometryBitmask =
223 XParseGeometry(geometry,
224 &xpos, &ypos, &width, &height);
226 // if width is not set by geometry, check it against monitor width
227 if (!(geometryBitmask & 4)) {
228 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
229 if (WidthOfScreen(scr) - 8 < int(width))
230 width = WidthOfScreen(scr) - 8;
233 // if height is not set by geometry, check it against monitor height
234 if (!(geometryBitmask & 8)) {
235 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
236 if (HeightOfScreen(scr) - 24 < int(height))
237 height = HeightOfScreen(scr) - 24;
240 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
242 // recalculate xpos if it's not set
244 xpos = (WidthOfScreen(s) - width) / 2;
246 // recalculate ypos if it's not set
248 ypos = (HeightOfScreen(s) - height) / 2;
250 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
251 << "+" << xpos << "+" << ypos << endl;
253 XFormsView view(width, height);
254 view.show(xpos, ypos, "LyX");
259 // FIXME: some code below needs moving
261 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
263 vector<string>::const_iterator cit = files.begin();
264 vector<string>::const_iterator end = files.end();
265 for (; cit != end; ++cit) {
266 Buffer * b = bufferlist.loadLyXFile(*cit);
272 // switch to the last buffer successfully loaded
274 view.view()->buffer(last);
277 // handle the batch commands the user asked for
278 if (!batch.empty()) {
279 view.getLyXFunc()->verboseDispatch(batch, false);
282 // enter the event loop
284 if (fl_check_forms() == FL_EVENT) {
287 lyxerr << "Received unhandled X11 event" << endl;
288 lyxerr << "Type: 0x" << hex << ev.xany.type <<
289 " Target: 0x" << hex << ev.xany.window << endl;
298 // Called by the graphics cache to connect the appropriate frontend
299 // image loading routines to the LyX kernel.
300 void lyx_gui::init_graphics()
302 using namespace grfx;
304 #ifdef USE_XFORMS_IMAGE_LOADER
305 // connect the image loader based on the xforms library
306 Image::newImage = boost::bind(&xformsImage::newImage);
307 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
309 // connect the image loader based on the XPM library
310 Image::newImage = boost::bind(&ImageXPM::newImage);
311 Image::loadableFormats = boost::bind(&ImageXPM::loadableFormats);
316 string const lyx_gui::hexname(LColor::color col)
318 string const name = lcolor.getX11Name(col);
319 Display * const display = fl_get_display();
320 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
323 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
324 lyxerr << "X can't find color \""
325 << lcolor.getLyXName(col)
332 // Note that X stores the RGB values in the range 0 - 65535
333 // whilst we require them in the range 0 - 255.
334 os << setbase(16) << setfill('0')
335 << setw(2) << (xcol.red / 256)
336 << setw(2) << (xcol.green / 256)
337 << setw(2) << (xcol.blue / 256);
339 return os.str().c_str();