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 using namespace lyx::support;
49 #ifndef CXX_GLOBAL_CSTD
61 extern BufferList bufferlist;
63 // FIXME: wrong place !
64 LyXServer * lyxserver;
69 bool finished = false;
71 /// estimate DPI from X server
74 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
75 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
76 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
80 /// set default GUI configuration
84 cntl.buttonFontSize = FL_NORMAL_SIZE;
85 cntl.browserFontSize = FL_NORMAL_SIZE;
86 cntl.labelFontSize = FL_NORMAL_SIZE;
87 cntl.choiceFontSize = FL_NORMAL_SIZE;
88 cntl.inputFontSize = FL_NORMAL_SIZE;
89 cntl.menuFontSize = FL_NORMAL_SIZE;
90 cntl.borderWidth = -1;
91 cntl.vclass = FL_DefaultVisual;
92 fl_set_defaults(FL_PDVisual
94 | FL_PDBrowserFontSize
99 | FL_PDBorderWidth, &cntl);
105 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
106 // We don't abort on BadWindow
107 if (xeev->error_code == BadWindow) {
108 lyxerr << "BadWindow received !" << endl;
109 lyxerr << "If you're using xforms 1.0 or greater, "
110 << " please report this to lyx-devel@lists.lyx.org" << endl;
115 LyX::emergencyCleanup();
117 // Get the reason for the crash.
119 XGetErrorText(display, xeev->error_code, etxt, 512);
120 lyxerr << etxt << " id: " << xeev->resourceid << endl;
121 // By doing an abort we get a nice backtrace. (hopefully)
122 lyx::support::abort();
128 /// read in geometry specification
139 void parse_init(int & argc, char * argv[])
143 FL_CMD_OPT cmdopt[] = {
144 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
147 FL_resource res[] = {
148 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
151 const int num_res = sizeof(res)/sizeof(FL_resource);
153 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
155 // It appears that, in xforms >=0.89.5, fl_initialize()
156 // calls setlocale() and ruins our LC_NUMERIC setting.
159 fl_get_app_resources(res, num_res);
161 Display * display = fl_get_display();
164 lyxerr << "LyX: unable to access X display, exiting" << endl;
165 os::warn("Unable to access X display, exiting");
169 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
171 XSetErrorHandler(LyX_XErrHandler);
173 lyxColorHandler.reset(new LyXColorHandler());
175 using namespace grfx;
177 // connect the image loader based on the xforms library
178 Image::newImage = boost::bind(&xformsImage::newImage);
179 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
181 // must do this /before/ lyxrc gets read
182 lyxrc.dpi = getDPI();
184 LoaderQueue::setPriority(10,100);
190 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
192 if (lyxrc.popup_font_encoding.empty())
193 lyxrc.popup_font_encoding = lyxrc.font_norm;
194 // Set the font name for popups and menus
195 string boldfontname = lyxrc.popup_bold_font
196 + "-*-*-*-?-*-*-*-*-"
197 + lyxrc.popup_font_encoding;
198 // "?" means "scale that font"
199 string fontname = lyxrc.popup_normal_font
200 + "-*-*-*-?-*-*-*-*-"
201 + lyxrc.popup_font_encoding;
203 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
204 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
206 lyxerr << "Could not set menu font to "
207 << boldfontname << endl;
210 lyxerr << "Could not set popup font to "
213 if (bold < 0 && normal < 0) {
214 lyxerr << "Using 'helvetica' font for menus" << endl;
215 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
216 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
217 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
218 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
220 if (bold < 0 && normal < 0) {
221 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
222 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
227 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
229 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
231 fl_setpup_fontstyle(FL_NORMAL_STYLE);
232 fl_setpup_fontsize(FL_NORMAL_SIZE);
233 fl_setpup_color(FL_MCOL, FL_BLACK);
234 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
235 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
239 void start(string const & batch, vector<string> const & files)
244 unsigned int width = 690;
245 unsigned int height = 510;
247 int const geometryBitmask =
248 XParseGeometry(geometry,
249 &xpos, &ypos, &width, &height);
251 // if width is not set by geometry, check it against monitor width
252 if (!(geometryBitmask & WidthValue)) {
253 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
254 if (WidthOfScreen(scr) - 8 < int(width))
255 width = WidthOfScreen(scr) - 8;
258 // if height is not set by geometry, check it against monitor height
259 if (!(geometryBitmask & HeightValue)) {
260 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
261 if (HeightOfScreen(scr) - 24 < int(height))
262 height = HeightOfScreen(scr) - 24;
265 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
267 // recalculate xpos if it's not set
269 xpos = (WidthOfScreen(s) - width) / 2;
271 // recalculate ypos if it's not set
273 ypos = (HeightOfScreen(s) - height) / 2;
275 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
276 << '+' << xpos << '+' << ypos << endl;
278 XFormsView view(width, height);
279 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 view.view()->loadLyXFile(*cit, true);
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
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