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"
27 // FIXME: move this stuff out again
28 #include "bufferlist.h"
30 #include "lyxserver.h"
31 #include "BufferView.h"
32 #include "XFormsView.h"
34 #include FORMS_H_LOCATION
35 #include "ColorHandler.h"
36 #include "xforms_helpers.h"
37 #include "xfont_loader.h"
38 #include "xformsImage.h"
43 #include <boost/bind.hpp>
45 #ifndef CXX_GLOBAL_CSTD
57 extern BufferList bufferlist;
59 // FIXME: wrong place !
60 LyXServer * lyxserver;
65 bool finished = false;
67 /// estimate DPI from X server
70 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
71 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
72 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
76 /// set default GUI configuration
80 cntl.buttonFontSize = FL_NORMAL_SIZE;
81 cntl.browserFontSize = FL_NORMAL_SIZE;
82 cntl.labelFontSize = FL_NORMAL_SIZE;
83 cntl.choiceFontSize = FL_NORMAL_SIZE;
84 cntl.inputFontSize = FL_NORMAL_SIZE;
85 cntl.menuFontSize = FL_NORMAL_SIZE;
86 cntl.borderWidth = -1;
87 cntl.vclass = FL_DefaultVisual;
88 fl_set_defaults(FL_PDVisual
90 | FL_PDBrowserFontSize
95 | FL_PDBorderWidth, &cntl);
101 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
102 // We don't abort on BadWindow
103 if (xeev->error_code == BadWindow) {
104 lyxerr << "BadWindow received !" << endl;
105 lyxerr << "If you're using xforms 1.0 or greater, "
106 << " please report this to lyx-devel@lists.lyx.org" << endl;
111 LyX::emergencyCleanup();
113 // Get the reason for the crash.
115 XGetErrorText(display, xeev->error_code, etxt, 512);
116 lyxerr << etxt << " id: " << xeev->resourceid << endl;
117 // By doing an abort we get a nice backtrace. (hopefully)
124 /// read in geometry specification
130 void lyx_gui::parse_init(int & argc, char * argv[])
134 FL_CMD_OPT cmdopt[] = {
135 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
138 FL_resource res[] = {
139 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
142 const int num_res = sizeof(res)/sizeof(FL_resource);
144 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
146 // It appears that, in xforms >=0.89.5, fl_initialize()
147 // calls setlocale() and ruins our LC_NUMERIC setting.
150 fl_get_app_resources(res, num_res);
152 Display * display = fl_get_display();
155 lyxerr << "LyX: unable to access X display, exiting" << endl;
156 os::warn("Unable to access X display, exiting");
160 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
162 XSetErrorHandler(LyX_XErrHandler);
164 lyxColorHandler.reset(new LyXColorHandler());
166 using namespace grfx;
168 // connect the image loader based on the xforms library
169 Image::newImage = boost::bind(&xformsImage::newImage);
170 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
172 // must do this /before/ lyxrc gets read
173 lyxrc.dpi = getDPI();
177 void lyx_gui::parse_lyxrc()
179 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
181 if (lyxrc.popup_font_encoding.empty())
182 lyxrc.popup_font_encoding = lyxrc.font_norm;
183 // Set the font name for popups and menus
184 string boldfontname = lyxrc.popup_bold_font
185 + "-*-*-*-?-*-*-*-*-"
186 + lyxrc.popup_font_encoding;
187 // "?" means "scale that font"
188 string fontname = lyxrc.popup_normal_font
189 + "-*-*-*-?-*-*-*-*-"
190 + lyxrc.popup_font_encoding;
192 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
193 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
195 lyxerr << "Could not set menu font to "
196 << boldfontname << endl;
199 lyxerr << "Could not set popup font to "
202 if (bold < 0 && normal < 0) {
203 lyxerr << "Using 'helvetica' font for menus" << endl;
204 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
205 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
206 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
207 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
209 if (bold < 0 && normal < 0) {
210 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
211 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
216 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
218 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
220 fl_setpup_fontstyle(FL_NORMAL_STYLE);
221 fl_setpup_fontsize(FL_NORMAL_SIZE);
222 fl_setpup_color(FL_MCOL, FL_BLACK);
223 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
224 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
228 void lyx_gui::start(string const & batch, vector<string> const & files)
233 unsigned int width = 690;
234 unsigned int height = 510;
236 int const geometryBitmask =
237 XParseGeometry(geometry,
238 &xpos, &ypos, &width, &height);
240 // if width is not set by geometry, check it against monitor width
241 if (!(geometryBitmask & WidthValue)) {
242 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
243 if (WidthOfScreen(scr) - 8 < int(width))
244 width = WidthOfScreen(scr) - 8;
247 // if height is not set by geometry, check it against monitor height
248 if (!(geometryBitmask & HeightValue)) {
249 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
250 if (HeightOfScreen(scr) - 24 < int(height))
251 height = HeightOfScreen(scr) - 24;
254 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
256 // recalculate xpos if it's not set
258 xpos = (WidthOfScreen(s) - width) / 2;
260 // recalculate ypos if it's not set
262 ypos = (HeightOfScreen(s) - height) / 2;
264 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
265 << '+' << xpos << '+' << ypos << endl;
267 XFormsView view(width, height);
268 view.show(xpos, ypos, "LyX");
273 // FIXME: some code below needs moving
275 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
277 vector<string>::const_iterator cit = files.begin();
278 vector<string>::const_iterator end = files.end();
279 for (; cit != end; ++cit) {
280 Buffer * b = bufferlist.loadLyXFile(*cit);
286 // switch to the last buffer successfully loaded
288 view.view()->buffer(last);
291 // handle the batch commands the user asked for
292 if (!batch.empty()) {
293 view.getLyXFunc().dispatch(batch);
296 // enter the event loop
298 if (fl_check_forms() == FL_EVENT) {
302 << "Received unhandled X11 event" << endl
303 << "Type: " << ev.xany.type
304 << " Target: 0x" << hex << ev.xany.window
309 // FIXME: breaks emergencyCleanup
320 string const lyx_gui::hexname(LColor::color col)
322 string const name = lcolor.getX11Name(col);
323 Display * const display = fl_get_display();
324 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
327 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
328 lyxerr << "X can't find color \""
329 << lcolor.getLyXName(col)
336 // Note that X stores the RGB values in the range 0 - 65535
337 // whilst we require them in the range 0 - 255.
338 os << setbase(16) << setfill('0')
339 << setw(2) << (xcol.red / 256)
340 << setw(2) << (xcol.green / 256)
341 << setw(2) << (xcol.blue / 256);
343 return STRCONV(os.str());
347 void lyx_gui::update_color(LColor::color col)
349 lyxColorHandler->updateColor(col);
353 void lyx_gui::update_fonts()
359 bool lyx_gui::font_available(LyXFont const & font)
361 return fontloader.available(font);
367 void C_read_callback(int, void * data)
369 LyXComm * comm = static_cast<LyXComm *>(data);
375 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
377 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
381 void lyx_gui::remove_read_callback(int fd)
383 fl_remove_io_callback(fd, FL_READ, C_read_callback);