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.
15 #include "ColorHandler.h"
16 #include "xfont_loader.h"
17 #include "xforms_helpers.h"
18 #include "xformsImage.h"
19 #include "XFormsView.h"
21 #include "bufferlist.h"
22 #include "BufferView.h"
29 #include "lyxserver.h"
31 #include "graphics/LoaderQueue.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
38 #include "lyx_forms.h"
40 #include <boost/bind.hpp>
42 #include "support/std_sstream.h"
46 using lyx::support::AddName;
47 using lyx::support::user_lyxdir;
49 namespace os = lyx::support::os;
51 #ifndef CXX_GLOBAL_CSTD
61 using std::ostringstream;
66 extern BufferList bufferlist;
68 // FIXME: wrong place !
69 LyXServer * lyxserver;
74 bool finished = false;
76 /// estimate DPI from X server
79 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
80 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
81 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
85 /// set default GUI configuration
89 cntl.buttonFontSize = FL_NORMAL_SIZE;
90 cntl.browserFontSize = FL_NORMAL_SIZE;
91 cntl.labelFontSize = FL_NORMAL_SIZE;
92 cntl.choiceFontSize = FL_NORMAL_SIZE;
93 cntl.inputFontSize = FL_NORMAL_SIZE;
94 cntl.menuFontSize = FL_NORMAL_SIZE;
95 cntl.borderWidth = -1;
96 cntl.vclass = FL_DefaultVisual;
97 fl_set_defaults(FL_PDVisual
99 | FL_PDBrowserFontSize
101 | FL_PDChoiceFontSize
104 | FL_PDBorderWidth, &cntl);
110 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
111 // We don't abort on BadWindow
112 if (xeev->error_code == BadWindow) {
113 lyxerr << "BadWindow received !" << endl;
114 lyxerr << "If you're using xforms 1.0 or greater, "
115 << " please report this to lyx-devel@lists.lyx.org" << endl;
120 LyX::emergencyCleanup();
122 // Get the reason for the crash.
124 XGetErrorText(display, xeev->error_code, etxt, 512);
125 lyxerr << etxt << " id: " << xeev->resourceid << endl;
126 // By doing an abort we get a nice backtrace. (hopefully)
127 lyx::support::abort();
133 /// read in geometry specification
144 void parse_init(int & argc, char * argv[])
148 FL_CMD_OPT cmdopt[] = {
149 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
152 FL_resource res[] = {
153 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
156 const int num_res = sizeof(res)/sizeof(FL_resource);
158 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
160 // It appears that, in xforms >=0.89.5, fl_initialize()
161 // calls setlocale() and ruins our LC_NUMERIC setting.
164 fl_get_app_resources(res, num_res);
166 Display * display = fl_get_display();
169 lyxerr << "LyX: unable to access X display, exiting" << endl;
170 os::warn("Unable to access X display, exiting");
174 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
176 XSetErrorHandler(LyX_XErrHandler);
178 lyxColorHandler.reset(new LyXColorHandler);
180 using namespace lyx::graphics;
182 // connect the image loader based on the xforms library
183 Image::newImage = boost::bind(&xformsImage::newImage);
184 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
186 // must do this /before/ lyxrc gets read
187 lyxrc.dpi = getDPI();
189 LoaderQueue::setPriority(10,100);
195 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
197 if (lyxrc.popup_font_encoding.empty())
198 lyxrc.popup_font_encoding = lyxrc.font_norm;
199 // Set the font name for popups and menus
200 string boldfontname = lyxrc.popup_bold_font
201 + "-*-*-*-?-*-*-*-*-"
202 + lyxrc.popup_font_encoding;
203 // "?" means "scale that font"
204 string fontname = lyxrc.popup_normal_font
205 + "-*-*-*-?-*-*-*-*-"
206 + lyxrc.popup_font_encoding;
208 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
209 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
211 lyxerr << "Could not set menu font to "
212 << boldfontname << endl;
215 lyxerr << "Could not set popup font to "
218 if (bold < 0 && normal < 0) {
219 lyxerr << "Using 'helvetica' font for menus" << endl;
220 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
221 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
222 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
223 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
225 if (bold < 0 && normal < 0) {
226 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
227 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
232 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
234 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
236 fl_setpup_fontstyle(FL_NORMAL_STYLE);
237 fl_setpup_fontsize(FL_NORMAL_SIZE);
238 fl_setpup_color(FL_MCOL, FL_BLACK);
239 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244 void start(string const & batch, vector<string> const & files)
249 unsigned int width = 690;
250 unsigned int height = 510;
252 int const geometryBitmask =
253 XParseGeometry(geometry,
254 &xpos, &ypos, &width, &height);
256 // if width is not set by geometry, check it against monitor width
257 if (!(geometryBitmask & WidthValue)) {
258 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
259 if (WidthOfScreen(scr) - 8 < int(width))
260 width = WidthOfScreen(scr) - 8;
263 // if height is not set by geometry, check it against monitor height
264 if (!(geometryBitmask & HeightValue)) {
265 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
266 if (HeightOfScreen(scr) - 24 < int(height))
267 height = HeightOfScreen(scr) - 24;
270 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
272 // recalculate xpos if it's not set
274 xpos = (WidthOfScreen(s) - width) / 2;
276 // recalculate ypos if it's not set
278 ypos = (HeightOfScreen(s) - height) / 2;
280 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
281 << '+' << xpos << '+' << ypos << endl;
283 XFormsView view(width, height);
284 view.show(xpos, ypos, "LyX");
287 // FIXME: some code below needs moving
289 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
291 vector<string>::const_iterator cit = files.begin();
292 vector<string>::const_iterator end = files.end();
293 for (; cit != end; ++cit)
294 view.view()->loadLyXFile(*cit, true);
296 // handle the batch commands the user asked for
298 view.getLyXFunc().dispatch(batch);
300 // enter the event loop
302 if (fl_check_forms() == FL_EVENT) {
306 << "Received unhandled X11 event" << endl
307 << "Type: " << ev.xany.type
308 << " Target: 0x" << hex << ev.xany.window
313 // FIXME: breaks emergencyCleanup
330 FuncStatus getStatus(FuncRequest const & /*ev*/)
332 // Nothing interesting to do here
336 string const hexname(LColor_color col)
338 unsigned int r, g, b;
339 bool const success = getRGBColor(col, r, g, b);
341 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
348 os << setbase(16) << setfill('0')
357 void update_color(LColor_color col)
359 lyxColorHandler->updateColor(col);
369 bool font_available(LyXFont const & font)
371 return fontloader.available(font);
377 void C_read_callback(int, void * data)
379 LyXComm * comm = static_cast<LyXComm *>(data);
385 void set_read_callback(int fd, LyXComm * comm)
387 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
391 void remove_read_callback(int fd)
393 fl_remove_io_callback(fd, FL_READ, C_read_callback);
397 string const roman_font_name()
403 string const sans_font_name()
409 string const typewriter_font_name()
414 }; // namespace lyx_gui