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.
6 * \author Lars Gullik Bjønnes
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"
30 #include "buffer_funcs.h"
32 #include "lyxserver.h"
33 #include "BufferView.h"
34 #include "XFormsView.h"
36 #include "lyx_forms.h"
37 #include "ColorHandler.h"
38 #include "xforms_helpers.h"
39 #include "xfont_loader.h"
40 #include "xformsImage.h"
45 #include <boost/bind.hpp>
47 #ifndef CXX_GLOBAL_CSTD
59 extern BufferList bufferlist;
61 // FIXME: wrong place !
62 LyXServer * lyxserver;
67 bool finished = false;
69 /// estimate DPI from X server
72 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
73 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
74 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
78 /// set default GUI configuration
82 cntl.buttonFontSize = FL_NORMAL_SIZE;
83 cntl.browserFontSize = FL_NORMAL_SIZE;
84 cntl.labelFontSize = FL_NORMAL_SIZE;
85 cntl.choiceFontSize = FL_NORMAL_SIZE;
86 cntl.inputFontSize = FL_NORMAL_SIZE;
87 cntl.menuFontSize = FL_NORMAL_SIZE;
88 cntl.borderWidth = -1;
89 cntl.vclass = FL_DefaultVisual;
90 fl_set_defaults(FL_PDVisual
92 | FL_PDBrowserFontSize
97 | FL_PDBorderWidth, &cntl);
103 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
104 // We don't abort on BadWindow
105 if (xeev->error_code == BadWindow) {
106 lyxerr << "BadWindow received !" << endl;
107 lyxerr << "If you're using xforms 1.0 or greater, "
108 << " please report this to lyx-devel@lists.lyx.org" << endl;
113 LyX::emergencyCleanup();
115 // Get the reason for the crash.
117 XGetErrorText(display, xeev->error_code, etxt, 512);
118 lyxerr << etxt << " id: " << xeev->resourceid << endl;
119 // By doing an abort we get a nice backtrace. (hopefully)
126 /// read in geometry specification
137 void parse_init(int & argc, char * argv[])
141 FL_CMD_OPT cmdopt[] = {
142 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
145 FL_resource res[] = {
146 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
149 const int num_res = sizeof(res)/sizeof(FL_resource);
151 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
153 // It appears that, in xforms >=0.89.5, fl_initialize()
154 // calls setlocale() and ruins our LC_NUMERIC setting.
157 fl_get_app_resources(res, num_res);
159 Display * display = fl_get_display();
162 lyxerr << "LyX: unable to access X display, exiting" << endl;
163 os::warn("Unable to access X display, exiting");
167 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
169 XSetErrorHandler(LyX_XErrHandler);
171 lyxColorHandler.reset(new LyXColorHandler());
173 using namespace grfx;
175 // connect the image loader based on the xforms library
176 Image::newImage = boost::bind(&xformsImage::newImage);
177 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
179 // must do this /before/ lyxrc gets read
180 lyxrc.dpi = getDPI();
182 LoaderQueue::setPriority(10,100);
188 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
190 if (lyxrc.popup_font_encoding.empty())
191 lyxrc.popup_font_encoding = lyxrc.font_norm;
192 // Set the font name for popups and menus
193 string boldfontname = lyxrc.popup_bold_font
194 + "-*-*-*-?-*-*-*-*-"
195 + lyxrc.popup_font_encoding;
196 // "?" means "scale that font"
197 string fontname = lyxrc.popup_normal_font
198 + "-*-*-*-?-*-*-*-*-"
199 + lyxrc.popup_font_encoding;
201 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
202 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
204 lyxerr << "Could not set menu font to "
205 << boldfontname << endl;
208 lyxerr << "Could not set popup font to "
211 if (bold < 0 && normal < 0) {
212 lyxerr << "Using 'helvetica' font for menus" << endl;
213 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
214 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
215 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
216 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
218 if (bold < 0 && normal < 0) {
219 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
220 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
225 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
227 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
229 fl_setpup_fontstyle(FL_NORMAL_STYLE);
230 fl_setpup_fontsize(FL_NORMAL_SIZE);
231 fl_setpup_color(FL_MCOL, FL_BLACK);
232 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
237 void start(string const & batch, vector<string> const & files)
242 unsigned int width = 690;
243 unsigned int height = 510;
245 int const geometryBitmask =
246 XParseGeometry(geometry,
247 &xpos, &ypos, &width, &height);
249 // if width is not set by geometry, check it against monitor width
250 if (!(geometryBitmask & WidthValue)) {
251 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
252 if (WidthOfScreen(scr) - 8 < int(width))
253 width = WidthOfScreen(scr) - 8;
256 // if height is not set by geometry, check it against monitor height
257 if (!(geometryBitmask & HeightValue)) {
258 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
259 if (HeightOfScreen(scr) - 24 < int(height))
260 height = HeightOfScreen(scr) - 24;
263 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
265 // recalculate xpos if it's not set
267 xpos = (WidthOfScreen(s) - width) / 2;
269 // recalculate ypos if it's not set
271 ypos = (HeightOfScreen(s) - height) / 2;
273 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
274 << '+' << xpos << '+' << ypos << endl;
276 XFormsView view(width, height);
277 view.show(xpos, ypos, "LyX");
282 // FIXME: some code below needs moving
284 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
286 vector<string>::const_iterator cit = files.begin();
287 vector<string>::const_iterator end = files.end();
288 for (; cit != end; ++cit) {
289 Buffer * b = bufferlist.newBuffer(*cit);
290 if (loadLyXFile(b, *cit))
294 // switch to the last buffer successfully loaded
296 view.view()->buffer(last);
299 // handle the batch commands the user asked for
300 if (!batch.empty()) {
301 view.getLyXFunc().dispatch(batch);
304 // enter the event loop
306 if (fl_check_forms() == FL_EVENT) {
310 << "Received unhandled X11 event" << endl
311 << "Type: " << ev.xany.type
312 << " Target: 0x" << hex << ev.xany.window
317 // FIXME: breaks emergencyCleanup
334 FuncStatus getStatus(FuncRequest const & /*ev*/)
336 // Nothing interesting to do here
340 string const hexname(LColor::color col)
342 unsigned int r, g, b;
343 bool const success = getRGBColor(col, r, g, b);
345 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
352 os << setbase(16) << setfill('0')
357 return STRCONV(os.str());
361 void update_color(LColor::color col)
363 lyxColorHandler->updateColor(col);
373 bool font_available(LyXFont const & font)
375 return fontloader.available(font);
381 void C_read_callback(int, void * data)
383 LyXComm * comm = static_cast<LyXComm *>(data);
389 void set_read_callback(int fd, LyXComm * comm)
391 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
395 void remove_read_callback(int fd)
397 fl_remove_io_callback(fd, FL_READ, C_read_callback);
401 string const roman_font_name()
407 string const sans_font_name()
413 string const typewriter_font_name()
418 }; // namespace lyx_gui