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 "xformsGImage.h"
37 #include "graphics/GraphicsImageXPM.h"
43 #include <boost/bind.hpp>
50 extern BufferList bufferlist;
52 // FIXME: wrong place !
53 LyXServer * lyxserver;
57 /// set default GUI configuration
61 cntl.buttonFontSize = FL_NORMAL_SIZE;
62 cntl.browserFontSize = FL_NORMAL_SIZE;
63 cntl.labelFontSize = FL_NORMAL_SIZE;
64 cntl.choiceFontSize = FL_NORMAL_SIZE;
65 cntl.inputFontSize = FL_NORMAL_SIZE;
66 cntl.menuFontSize = FL_NORMAL_SIZE;
67 cntl.borderWidth = -1;
68 cntl.vclass = FL_DefaultVisual;
69 fl_set_defaults(FL_PDVisual
71 | FL_PDBrowserFontSize
76 | FL_PDBorderWidth, &cntl);
82 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
83 // We don't abort on BadWindow
84 if (xeev->error_code == BadWindow) {
85 lyxerr << "BadWindow received !" << endl;
86 lyxerr << "If you're using xforms 1.0 or greater, "
87 << " please report this to lyx-devel@lists.lyx.org" << endl;
92 LyX::emergencyCleanup();
94 // Get the reason for the crash.
96 XGetErrorText(display, xeev->error_code, etxt, 512);
97 lyxerr << etxt << " id: " << xeev->resourceid << endl;
98 // By doing an abort we get a nice backtrace. (hopefully)
105 /// read in geometry specification
111 void lyx_gui::parse_init(int & argc, char * argv[])
115 FL_CMD_OPT cmdopt[] = {
116 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
119 FL_resource res[] = {
120 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
123 const int num_res = sizeof(res)/sizeof(FL_resource);
125 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
127 // It appears that, in xforms >=0.89.5, fl_initialize()
128 // calls setlocale() and ruins our LC_NUMERIC setting.
131 fl_get_app_resources(res, num_res);
133 Display * display = fl_get_display();
136 lyxerr << "LyX: unable to access X display, exiting" << endl;
137 os::warn("Unable to access X display, exiting");
141 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
143 XSetErrorHandler(LyX_XErrHandler);
145 lyxColorHandler.reset(new LyXColorHandler());
149 void lyx_gui::parse_lyxrc()
154 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
156 if (lyxrc.popup_font_encoding.empty())
157 lyxrc.popup_font_encoding = lyxrc.font_norm;
158 // Set the font name for popups and menus
159 string boldfontname = lyxrc.popup_bold_font
160 + "-*-*-*-?-*-*-*-*-"
161 + lyxrc.popup_font_encoding;
162 // "?" means "scale that font"
163 string fontname = lyxrc.popup_normal_font
164 + "-*-*-*-?-*-*-*-*-"
165 + lyxrc.popup_font_encoding;
167 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
168 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
170 lyxerr << "Could not set menu font to "
171 << boldfontname << endl;
174 lyxerr << "Could not set popup font to "
177 if (bold < 0 && normal < 0) {
178 lyxerr << "Using 'helvetica' font for menus" << endl;
179 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
180 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
181 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
182 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
184 if (bold < 0 && normal < 0) {
185 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
186 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
191 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
193 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
195 fl_setpup_fontstyle(FL_NORMAL_STYLE);
196 fl_setpup_fontsize(FL_NORMAL_SIZE);
197 fl_setpup_color(FL_MCOL, FL_BLACK);
198 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
200 fl_set_oneliner_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
202 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
207 void lyx_gui::start(string const & batch, vector<string> files)
212 unsigned int width = 690;
213 unsigned int height = 510;
215 static const int geometryBitmask =
216 XParseGeometry(geometry,
217 &xpos, &ypos, &width, &height);
219 // if width is not set by geometry, check it against monitor width
220 if (!(geometryBitmask & 4)) {
221 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
222 if (WidthOfScreen(scr) - 8 < int(width))
223 width = WidthOfScreen(scr) - 8;
226 // if height is not set by geometry, check it against monitor height
227 if (!(geometryBitmask & 8)) {
228 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
229 if (HeightOfScreen(scr) - 24 < int(height))
230 height = HeightOfScreen(scr) - 24;
233 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
235 // recalculate xpos if it's not set
237 xpos = (WidthOfScreen(s) - width) / 2;
239 // recalculate ypos if it's not set
241 ypos = (HeightOfScreen(s) - height) / 2;
243 lyxerr[Debug::GUI] << "Creating view: " << width << "x" << height
244 << "+" << xpos << "+" << ypos << endl;
246 XFormsView view(width, height);
247 view.show(xpos, ypos, "LyX");
252 // FIXME: some code below needs moving
254 lyxserver = new LyXServer(view.getLyXFunc(), lyxrc.lyxpipes);
256 vector<string>::const_iterator cit = files.begin();
257 vector<string>::const_iterator end = files.end();
258 for (; cit != end; ++cit) {
259 Buffer * b = bufferlist.loadLyXFile(*cit);
265 // switch to the last buffer successfully loaded
267 view.view()->buffer(last);
270 // handle the batch commands the user asked for
271 if (!batch.empty()) {
272 view.getLyXFunc()->verboseDispatch(batch, false);
275 // enter the event loop
277 if (fl_check_forms() == FL_EVENT) {
280 lyxerr << "Received unhandled X11 event" << endl;
281 lyxerr << "Type: 0x" << hex << ev.xany.type <<
282 " Target: 0x" << hex << ev.xany.window << endl;
291 // Called by the graphics cache to connect the appropriate frontend
292 // image loading routines to the LyX kernel.
293 void lyx_gui::init_graphics()
295 using namespace grfx;
297 #ifdef USE_XFORMS_IMAGE_LOADER
298 // connect the image loader based on the xforms library
299 GImage::newImage.connect(boost::bind(&xformsGImage::newImage));
300 GImage::loadableFormats.connect(boost::bind(&xformsGImage::loadableFormats));
302 // connect the image loader based on the XPM library
303 GImage::newImage.connect(boost::bind(&GImageXPM::newImage));
304 GImage::loadableFormats.connect(boost::bind(&GImageXPM::loadableFormats));