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"
24 #include "funcrequest.h"
28 #include "LyXAction.h"
31 #include "lyxserver.h"
32 #include "lyxsocket.h"
34 #include "graphics/LoaderQueue.h"
36 #include "support/filetools.h"
37 #include "support/lyxlib.h"
38 #include "support/os.h"
39 #include "support/path_defines.h"
41 #include "lyx_forms.h"
43 #include <boost/bind.hpp>
45 #include "support/std_sstream.h"
49 using lyx::support::AddName;
50 using lyx::support::user_lyxdir;
52 namespace os = lyx::support::os;
54 #ifndef CXX_GLOBAL_CSTD
64 using std::ostringstream;
69 extern BufferList bufferlist;
71 // FIXME: wrong place !
72 LyXServer * lyxserver;
73 LyXServerSocket * lyxsocket;
78 bool finished = false;
80 /// estimate DPI from X server
83 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
84 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
85 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
89 /// set default GUI configuration
93 cntl.buttonFontSize = FL_NORMAL_SIZE;
94 cntl.browserFontSize = FL_NORMAL_SIZE;
95 cntl.labelFontSize = FL_NORMAL_SIZE;
96 cntl.choiceFontSize = FL_NORMAL_SIZE;
97 cntl.inputFontSize = FL_NORMAL_SIZE;
98 cntl.menuFontSize = FL_NORMAL_SIZE;
99 cntl.borderWidth = -1;
100 cntl.vclass = FL_DefaultVisual;
101 fl_set_defaults(FL_PDVisual
102 | FL_PDButtonFontSize
103 | FL_PDBrowserFontSize
105 | FL_PDChoiceFontSize
108 | FL_PDBorderWidth, &cntl);
114 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
115 // We don't abort on BadWindow
116 if (xeev->error_code == BadWindow) {
117 lyxerr << "BadWindow received !" << endl;
118 lyxerr << "If you're using xforms 1.0 or greater, "
119 << " please report this to lyx-devel@lists.lyx.org" << endl;
124 LyX::cref().emergencyCleanup();
126 // Get the reason for the crash.
128 XGetErrorText(display, xeev->error_code, etxt, 512);
129 lyxerr << etxt << " id: " << xeev->resourceid << endl;
130 // By doing an abort we get a nice backtrace. (hopefully)
131 lyx::support::abort();
137 /// read in geometry specification
148 void parse_init(int & argc, char * argv[])
152 FL_CMD_OPT cmdopt[] = {
153 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
156 FL_resource res[] = {
157 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
160 const int num_res = sizeof(res)/sizeof(FL_resource);
162 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
164 // It appears that, in xforms >=0.89.5, fl_initialize()
165 // calls setlocale() and ruins our LC_NUMERIC setting.
168 fl_get_app_resources(res, num_res);
170 Display * display = fl_get_display();
173 lyxerr << "LyX: unable to access X display, exiting" << endl;
174 os::warn("Unable to access X display, exiting");
178 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
180 XSetErrorHandler(LyX_XErrHandler);
182 lyxColorHandler.reset(new LyXColorHandler);
184 using namespace lyx::graphics;
186 // connect the image loader based on the xforms library
187 Image::newImage = boost::bind(&xformsImage::newImage);
188 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
190 // must do this /before/ lyxrc gets read
191 lyxrc.dpi = getDPI();
193 LoaderQueue::setPriority(10,100);
199 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
201 if (lyxrc.popup_font_encoding.empty())
202 lyxrc.popup_font_encoding = lyxrc.font_norm;
203 // Set the font name for popups and menus
204 string boldfontname = lyxrc.popup_bold_font
205 + "-*-*-*-?-*-*-*-*-"
206 + lyxrc.popup_font_encoding;
207 // "?" means "scale that font"
208 string fontname = lyxrc.popup_normal_font
209 + "-*-*-*-?-*-*-*-*-"
210 + lyxrc.popup_font_encoding;
212 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
213 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
215 lyxerr << "Could not set menu font to "
216 << boldfontname << endl;
219 lyxerr << "Could not set popup font to "
222 if (bold < 0 && normal < 0) {
223 lyxerr << "Using 'helvetica' font for menus" << endl;
224 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
225 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
226 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
227 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
229 if (bold < 0 && normal < 0) {
230 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
231 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
236 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
238 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
240 fl_setpup_fontstyle(FL_NORMAL_STYLE);
241 fl_setpup_fontsize(FL_NORMAL_SIZE);
242 fl_setpup_color(FL_MCOL, FL_BLACK);
243 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
248 void start(string const & batch, vector<string> const & files)
253 unsigned int width = 690;
254 unsigned int height = 510;
256 int const geometryBitmask =
257 XParseGeometry(geometry,
258 &xpos, &ypos, &width, &height);
260 // if width is not set by geometry, check it against monitor width
261 if (!(geometryBitmask & WidthValue)) {
262 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
263 if (WidthOfScreen(scr) - 8 < int(width))
264 width = WidthOfScreen(scr) - 8;
267 // if height is not set by geometry, check it against monitor height
268 if (!(geometryBitmask & HeightValue)) {
269 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
270 if (HeightOfScreen(scr) - 24 < int(height))
271 height = HeightOfScreen(scr) - 24;
274 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
276 // recalculate xpos if it's not set
278 xpos = (WidthOfScreen(s) - width) / 2;
280 // recalculate ypos if it's not set
282 ypos = (HeightOfScreen(s) - height) / 2;
284 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
285 << '+' << xpos << '+' << ypos << endl;
287 boost::shared_ptr<XFormsView> view_ptr(new XFormsView(width, height));
288 LyX::ref().addLyXView(view_ptr);
290 XFormsView & view = *view_ptr.get();
291 view.show(xpos, ypos, "LyX");
294 // FIXME: some code below needs moving
296 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
297 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
298 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
300 vector<string>::const_iterator cit = files.begin();
301 vector<string>::const_iterator end = files.end();
302 for (; cit != end; ++cit)
303 view.view()->loadLyXFile(*cit, true);
305 // handle the batch commands the user asked for
307 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
309 // enter the event loop
311 if (fl_check_forms() == FL_EVENT) {
315 << "Received unhandled X11 event" << endl
316 << "Type: " << ev.xany.type
317 << " Target: 0x" << hex << ev.xany.window
322 // FIXME: breaks emergencyCleanup
340 FuncStatus getStatus(FuncRequest const & /*ev*/)
342 // Nothing interesting to do here
346 string const hexname(LColor_color col)
348 unsigned int r, g, b;
349 bool const success = getRGBColor(col, r, g, b);
351 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
358 os << setbase(16) << setfill('0')
367 void update_color(LColor_color col)
369 lyxColorHandler->getGCForeground(col);
370 lyxColorHandler->updateColor(col);
380 bool font_available(LyXFont const & font)
382 return fontloader.available(font);
388 void C_read_callback(int, void * data)
390 LyXComm * comm = static_cast<LyXComm *>(data);
395 void C_datasocket_callback(int, void * data)
397 LyXDataSocket * client = static_cast<LyXDataSocket *>(data);
398 client->server()->dataCallback(client);
402 void C_serversocket_callback(int, void * data)
404 LyXServerSocket * server = static_cast<LyXServerSocket *>(data);
405 server->serverCallback();
410 void set_read_callback(int fd, LyXComm * comm)
412 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
415 void remove_read_callback(int fd)
417 fl_remove_io_callback(fd, FL_READ, C_read_callback);
420 void set_datasocket_callback(LyXDataSocket * p)
422 fl_add_io_callback(p->fd(), FL_READ, C_datasocket_callback, p);
425 void remove_datasocket_callback(LyXDataSocket * p)
427 fl_remove_io_callback(p->fd(), FL_READ, C_datasocket_callback);
430 void set_serversocket_callback(LyXServerSocket * p)
432 fl_add_io_callback(p->fd(), FL_READ, C_serversocket_callback, p);
435 void remove_serversocket_callback(LyXServerSocket * p)
437 fl_remove_io_callback(p->fd(), FL_READ, C_serversocket_callback);
440 string const roman_font_name()
446 string const sans_font_name()
452 string const typewriter_font_name()
457 }; // namespace lyx_gui