2 * \file xforms/lyx_gui.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
16 #include "support/lyxlib.h"
17 #include "support/os.h"
18 #include "support/filetools.h"
26 #include "graphics/LoaderQueue.h"
28 // FIXME: move this stuff out again
29 #include "bufferlist.h"
31 #include "lyxserver.h"
32 #include "BufferView.h"
33 #include "XFormsView.h"
35 #include FORMS_H_LOCATION
36 #include "ColorHandler.h"
37 #include "xforms_helpers.h"
38 #include "xfont_loader.h"
39 #include "xformsImage.h"
44 #include <boost/bind.hpp>
46 #ifndef CXX_GLOBAL_CSTD
58 extern BufferList bufferlist;
60 // FIXME: wrong place !
61 LyXServer * lyxserver;
66 bool finished = false;
68 /// estimate DPI from X server
71 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
72 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
73 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
77 /// set default GUI configuration
81 cntl.buttonFontSize = FL_NORMAL_SIZE;
82 cntl.browserFontSize = FL_NORMAL_SIZE;
83 cntl.labelFontSize = FL_NORMAL_SIZE;
84 cntl.choiceFontSize = FL_NORMAL_SIZE;
85 cntl.inputFontSize = FL_NORMAL_SIZE;
86 cntl.menuFontSize = FL_NORMAL_SIZE;
87 cntl.borderWidth = -1;
88 cntl.vclass = FL_DefaultVisual;
89 fl_set_defaults(FL_PDVisual
91 | FL_PDBrowserFontSize
96 | FL_PDBorderWidth, &cntl);
102 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
103 // We don't abort on BadWindow
104 if (xeev->error_code == BadWindow) {
105 lyxerr << "BadWindow received !" << endl;
106 lyxerr << "If you're using xforms 1.0 or greater, "
107 << " please report this to lyx-devel@lists.lyx.org" << endl;
112 LyX::emergencyCleanup();
114 // Get the reason for the crash.
116 XGetErrorText(display, xeev->error_code, etxt, 512);
117 lyxerr << etxt << " id: " << xeev->resourceid << endl;
118 // By doing an abort we get a nice backtrace. (hopefully)
125 /// read in geometry specification
131 void lyx_gui::parse_init(int & argc, char * argv[])
135 FL_CMD_OPT cmdopt[] = {
136 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
139 FL_resource res[] = {
140 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
143 const int num_res = sizeof(res)/sizeof(FL_resource);
145 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
147 // It appears that, in xforms >=0.89.5, fl_initialize()
148 // calls setlocale() and ruins our LC_NUMERIC setting.
151 fl_get_app_resources(res, num_res);
153 Display * display = fl_get_display();
156 lyxerr << "LyX: unable to access X display, exiting" << endl;
157 os::warn("Unable to access X display, exiting");
161 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
163 XSetErrorHandler(LyX_XErrHandler);
165 lyxColorHandler.reset(new LyXColorHandler());
167 using namespace grfx;
169 // connect the image loader based on the xforms library
170 Image::newImage = boost::bind(&xformsImage::newImage);
171 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
173 // must do this /before/ lyxrc gets read
174 lyxrc.dpi = getDPI();
176 LoaderQueue::setPriority(10,100);
180 void lyx_gui::parse_lyxrc()
182 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
184 if (lyxrc.popup_font_encoding.empty())
185 lyxrc.popup_font_encoding = lyxrc.font_norm;
186 // Set the font name for popups and menus
187 string boldfontname = lyxrc.popup_bold_font
188 + "-*-*-*-?-*-*-*-*-"
189 + lyxrc.popup_font_encoding;
190 // "?" means "scale that font"
191 string fontname = lyxrc.popup_normal_font
192 + "-*-*-*-?-*-*-*-*-"
193 + lyxrc.popup_font_encoding;
195 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
196 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
198 lyxerr << "Could not set menu font to "
199 << boldfontname << endl;
202 lyxerr << "Could not set popup font to "
205 if (bold < 0 && normal < 0) {
206 lyxerr << "Using 'helvetica' font for menus" << endl;
207 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
208 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
209 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
210 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
212 if (bold < 0 && normal < 0) {
213 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
214 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
219 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
221 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
223 fl_setpup_fontstyle(FL_NORMAL_STYLE);
224 fl_setpup_fontsize(FL_NORMAL_SIZE);
225 fl_setpup_color(FL_MCOL, FL_BLACK);
226 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
227 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
231 void lyx_gui::start(string const & batch, vector<string> const & files)
236 unsigned int width = 690;
237 unsigned int height = 510;
239 int const geometryBitmask =
240 XParseGeometry(geometry,
241 &xpos, &ypos, &width, &height);
243 // if width is not set by geometry, check it against monitor width
244 if (!(geometryBitmask & WidthValue)) {
245 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
246 if (WidthOfScreen(scr) - 8 < int(width))
247 width = WidthOfScreen(scr) - 8;
250 // if height is not set by geometry, check it against monitor height
251 if (!(geometryBitmask & HeightValue)) {
252 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
253 if (HeightOfScreen(scr) - 24 < int(height))
254 height = HeightOfScreen(scr) - 24;
257 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
259 // recalculate xpos if it's not set
261 xpos = (WidthOfScreen(s) - width) / 2;
263 // recalculate ypos if it's not set
265 ypos = (HeightOfScreen(s) - height) / 2;
267 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
268 << '+' << xpos << '+' << ypos << endl;
270 XFormsView view(width, height);
271 view.show(xpos, ypos, "LyX");
276 // FIXME: some code below needs moving
278 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
280 vector<string>::const_iterator cit = files.begin();
281 vector<string>::const_iterator end = files.end();
282 for (; cit != end; ++cit) {
283 Buffer * b = bufferlist.loadLyXFile(*cit);
289 // switch to the last buffer successfully loaded
291 view.view()->buffer(last);
294 // handle the batch commands the user asked for
295 if (!batch.empty()) {
296 view.getLyXFunc().dispatch(batch);
299 // enter the event loop
301 if (fl_check_forms() == FL_EVENT) {
305 << "Received unhandled X11 event" << endl
306 << "Type: " << ev.xany.type
307 << " Target: 0x" << hex << ev.xany.window
312 // FIXME: breaks emergencyCleanup
323 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
325 // Nothing interesting to do here
329 string const lyx_gui::hexname(LColor::color col)
331 unsigned int r, g, b;
332 bool const success = getRGBColor(col, r, g, b);
334 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
341 os << setbase(16) << setfill('0')
346 return STRCONV(os.str());
350 void lyx_gui::update_color(LColor::color col)
352 lyxColorHandler->updateColor(col);
356 void lyx_gui::update_fonts()
362 bool lyx_gui::font_available(LyXFont const & font)
364 return fontloader.available(font);
370 void C_read_callback(int, void * data)
372 LyXComm * comm = static_cast<LyXComm *>(data);
378 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
380 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
384 void lyx_gui::remove_read_callback(int fd)
386 fl_remove_io_callback(fd, FL_READ, C_read_callback);