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"
19 #include "support/path_defines.h"
27 #include "graphics/LoaderQueue.h"
29 // FIXME: move this stuff out again
30 #include "bufferlist.h"
31 #include "buffer_funcs.h"
33 #include "lyxserver.h"
34 #include "BufferView.h"
35 #include "XFormsView.h"
37 #include "lyx_forms.h"
38 #include "ColorHandler.h"
39 #include "xforms_helpers.h"
40 #include "xfont_loader.h"
41 #include "xformsImage.h"
46 #include <boost/bind.hpp>
48 using namespace lyx::support;
50 #ifndef CXX_GLOBAL_CSTD
62 extern BufferList bufferlist;
64 // FIXME: wrong place !
65 LyXServer * lyxserver;
70 bool finished = false;
72 /// estimate DPI from X server
75 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
76 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
77 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
81 /// set default GUI configuration
85 cntl.buttonFontSize = FL_NORMAL_SIZE;
86 cntl.browserFontSize = FL_NORMAL_SIZE;
87 cntl.labelFontSize = FL_NORMAL_SIZE;
88 cntl.choiceFontSize = FL_NORMAL_SIZE;
89 cntl.inputFontSize = FL_NORMAL_SIZE;
90 cntl.menuFontSize = FL_NORMAL_SIZE;
91 cntl.borderWidth = -1;
92 cntl.vclass = FL_DefaultVisual;
93 fl_set_defaults(FL_PDVisual
95 | FL_PDBrowserFontSize
100 | FL_PDBorderWidth, &cntl);
106 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
107 // We don't abort on BadWindow
108 if (xeev->error_code == BadWindow) {
109 lyxerr << "BadWindow received !" << endl;
110 lyxerr << "If you're using xforms 1.0 or greater, "
111 << " please report this to lyx-devel@lists.lyx.org" << endl;
116 LyX::emergencyCleanup();
118 // Get the reason for the crash.
120 XGetErrorText(display, xeev->error_code, etxt, 512);
121 lyxerr << etxt << " id: " << xeev->resourceid << endl;
122 // By doing an abort we get a nice backtrace. (hopefully)
123 lyx::support::abort();
129 /// read in geometry specification
140 void parse_init(int & argc, char * argv[])
144 FL_CMD_OPT cmdopt[] = {
145 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
148 FL_resource res[] = {
149 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
152 const int num_res = sizeof(res)/sizeof(FL_resource);
154 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
156 // It appears that, in xforms >=0.89.5, fl_initialize()
157 // calls setlocale() and ruins our LC_NUMERIC setting.
160 fl_get_app_resources(res, num_res);
162 Display * display = fl_get_display();
165 lyxerr << "LyX: unable to access X display, exiting" << endl;
166 os::warn("Unable to access X display, exiting");
170 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
172 XSetErrorHandler(LyX_XErrHandler);
174 lyxColorHandler.reset(new LyXColorHandler());
176 using namespace lyx::graphics;
178 // connect the image loader based on the xforms library
179 Image::newImage = boost::bind(&xformsImage::newImage);
180 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
182 // must do this /before/ lyxrc gets read
183 lyxrc.dpi = getDPI();
185 LoaderQueue::setPriority(10,100);
191 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
193 if (lyxrc.popup_font_encoding.empty())
194 lyxrc.popup_font_encoding = lyxrc.font_norm;
195 // Set the font name for popups and menus
196 string boldfontname = lyxrc.popup_bold_font
197 + "-*-*-*-?-*-*-*-*-"
198 + lyxrc.popup_font_encoding;
199 // "?" means "scale that font"
200 string fontname = lyxrc.popup_normal_font
201 + "-*-*-*-?-*-*-*-*-"
202 + lyxrc.popup_font_encoding;
204 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
205 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
207 lyxerr << "Could not set menu font to "
208 << boldfontname << endl;
211 lyxerr << "Could not set popup font to "
214 if (bold < 0 && normal < 0) {
215 lyxerr << "Using 'helvetica' font for menus" << endl;
216 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
217 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
218 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
219 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
221 if (bold < 0 && normal < 0) {
222 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
223 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
228 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
230 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
232 fl_setpup_fontstyle(FL_NORMAL_STYLE);
233 fl_setpup_fontsize(FL_NORMAL_SIZE);
234 fl_setpup_color(FL_MCOL, FL_BLACK);
235 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
236 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240 void start(string const & batch, vector<string> const & files)
245 unsigned int width = 690;
246 unsigned int height = 510;
248 int const geometryBitmask =
249 XParseGeometry(geometry,
250 &xpos, &ypos, &width, &height);
252 // if width is not set by geometry, check it against monitor width
253 if (!(geometryBitmask & WidthValue)) {
254 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
255 if (WidthOfScreen(scr) - 8 < int(width))
256 width = WidthOfScreen(scr) - 8;
259 // if height is not set by geometry, check it against monitor height
260 if (!(geometryBitmask & HeightValue)) {
261 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
262 if (HeightOfScreen(scr) - 24 < int(height))
263 height = HeightOfScreen(scr) - 24;
266 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
268 // recalculate xpos if it's not set
270 xpos = (WidthOfScreen(s) - width) / 2;
272 // recalculate ypos if it's not set
274 ypos = (HeightOfScreen(s) - height) / 2;
276 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
277 << '+' << xpos << '+' << ypos << endl;
279 XFormsView view(width, height);
280 view.show(xpos, ypos, "LyX");
283 // FIXME: some code below needs moving
285 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
287 vector<string>::const_iterator cit = files.begin();
288 vector<string>::const_iterator end = files.end();
289 for (; cit != end; ++cit)
290 view.view()->loadLyXFile(*cit, true);
292 // handle the batch commands the user asked for
294 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
326 FuncStatus getStatus(FuncRequest const & /*ev*/)
328 // Nothing interesting to do here
332 string const hexname(LColor::color col)
334 unsigned int r, g, b;
335 bool const success = getRGBColor(col, r, g, b);
337 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
344 os << setbase(16) << setfill('0')
349 return STRCONV(os.str());
353 void update_color(LColor::color col)
355 lyxColorHandler->updateColor(col);
365 bool font_available(LyXFont const & font)
367 return fontloader.available(font);
373 void C_read_callback(int, void * data)
375 LyXComm * comm = static_cast<LyXComm *>(data);
381 void set_read_callback(int fd, LyXComm * comm)
383 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
387 void remove_read_callback(int fd)
389 fl_remove_io_callback(fd, FL_READ, C_read_callback);
393 string const roman_font_name()
399 string const sans_font_name()
405 string const typewriter_font_name()
410 }; // namespace lyx_gui