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
54 extern BufferList bufferlist;
56 // FIXME: wrong place !
57 LyXServer * lyxserver;
61 /// set default GUI configuration
65 cntl.buttonFontSize = FL_NORMAL_SIZE;
66 cntl.browserFontSize = FL_NORMAL_SIZE;
67 cntl.labelFontSize = FL_NORMAL_SIZE;
68 cntl.choiceFontSize = FL_NORMAL_SIZE;
69 cntl.inputFontSize = FL_NORMAL_SIZE;
70 cntl.menuFontSize = FL_NORMAL_SIZE;
71 cntl.borderWidth = -1;
72 cntl.vclass = FL_DefaultVisual;
73 fl_set_defaults(FL_PDVisual
75 | FL_PDBrowserFontSize
80 | FL_PDBorderWidth, &cntl);
86 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
87 // We don't abort on BadWindow
88 if (xeev->error_code == BadWindow) {
89 lyxerr << "BadWindow received !" << endl;
90 lyxerr << "If you're using xforms 1.0 or greater, "
91 << " please report this to lyx-devel@lists.lyx.org" << endl;
96 LyX::emergencyCleanup();
98 // Get the reason for the crash.
100 XGetErrorText(display, xeev->error_code, etxt, 512);
101 lyxerr << etxt << " id: " << xeev->resourceid << endl;
102 // By doing an abort we get a nice backtrace. (hopefully)
109 /// read in geometry specification
115 void lyx_gui::parse_init(int & argc, char * argv[])
119 FL_CMD_OPT cmdopt[] = {
120 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
123 FL_resource res[] = {
124 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
127 const int num_res = sizeof(res)/sizeof(FL_resource);
129 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
131 // It appears that, in xforms >=0.89.5, fl_initialize()
132 // calls setlocale() and ruins our LC_NUMERIC setting.
135 fl_get_app_resources(res, num_res);
137 Display * display = fl_get_display();
140 lyxerr << "LyX: unable to access X display, exiting" << endl;
141 os::warn("Unable to access X display, exiting");
145 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
147 XSetErrorHandler(LyX_XErrHandler);
149 lyxColorHandler.reset(new LyXColorHandler());
153 void lyx_gui::parse_lyxrc()
158 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
160 if (lyxrc.popup_font_encoding.empty())
161 lyxrc.popup_font_encoding = lyxrc.font_norm;
162 // Set the font name for popups and menus
163 string boldfontname = lyxrc.popup_bold_font
164 + "-*-*-*-?-*-*-*-*-"
165 + lyxrc.popup_font_encoding;
166 // "?" means "scale that font"
167 string fontname = lyxrc.popup_normal_font
168 + "-*-*-*-?-*-*-*-*-"
169 + lyxrc.popup_font_encoding;
171 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
172 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
174 lyxerr << "Could not set menu font to "
175 << boldfontname << endl;
178 lyxerr << "Could not set popup font to "
181 if (bold < 0 && normal < 0) {
182 lyxerr << "Using 'helvetica' font for menus" << endl;
183 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
184 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
185 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
186 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
188 if (bold < 0 && normal < 0) {
189 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
190 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
195 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
197 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
199 fl_setpup_fontstyle(FL_NORMAL_STYLE);
200 fl_setpup_fontsize(FL_NORMAL_SIZE);
201 fl_setpup_color(FL_MCOL, FL_BLACK);
202 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
204 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
206 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
211 void lyx_gui::start(string const & batch, vector<string> files)
216 unsigned int width = 690;
217 unsigned int height = 510;
219 static const int geometryBitmask =
220 XParseGeometry(geometry,
221 &xpos, &ypos, &width, &height);
223 // if width is not set by geometry, check it against monitor width
224 if (!(geometryBitmask & 4)) {
225 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
226 if (WidthOfScreen(scr) - 8 < int(width))
227 width = WidthOfScreen(scr) - 8;
230 // if height is not set by geometry, check it against monitor height
231 if (!(geometryBitmask & 8)) {
232 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
233 if (HeightOfScreen(scr) - 24 < int(height))
234 height = HeightOfScreen(scr) - 24;
237 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
239 // recalculate xpos if it's not set
241 xpos = (WidthOfScreen(s) - width) / 2;
243 // recalculate ypos if it's not set
245 ypos = (HeightOfScreen(s) - height) / 2;
247 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
248 << "+" << xpos << "+" << ypos << endl;
250 XFormsView view(width, height);
251 view.show(xpos, ypos, "LyX");
256 // FIXME: some code below needs moving
258 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
260 vector<string>::const_iterator cit = files.begin();
261 vector<string>::const_iterator end = files.end();
262 for (; cit != end; ++cit) {
263 Buffer * b = bufferlist.loadLyXFile(*cit);
269 // switch to the last buffer successfully loaded
271 view.view()->buffer(last);
274 // handle the batch commands the user asked for
275 if (!batch.empty()) {
276 view.getLyXFunc()->verboseDispatch(batch, false);
279 // enter the event loop
281 if (fl_check_forms() == FL_EVENT) {
284 lyxerr << "Received unhandled X11 event" << endl;
285 lyxerr << "Type: 0x" << hex << ev.xany.type <<
286 " Target: 0x" << hex << ev.xany.window << endl;
295 // Called by the graphics cache to connect the appropriate frontend
296 // image loading routines to the LyX kernel.
297 void lyx_gui::init_graphics()
299 using namespace grfx;
301 #ifdef USE_XFORMS_IMAGE_LOADER
302 // connect the image loader based on the xforms library
303 Image::newImage.connect(boost::bind(&xformsImage::newImage));
304 Image::loadableFormats.connect(boost::bind(&xformsImage::loadableFormats));
306 // connect the image loader based on the XPM library
307 Image::newImage.connect(boost::bind(&ImageXPM::newImage));
308 Image::loadableFormats.connect(boost::bind(&ImageXPM::loadableFormats));