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 using lyx::frontend::fontloader;
53 using lyx::frontend::getRGBColor;
54 using lyx::frontend::lyxColorHandler;
55 using lyx::frontend::LyXColorHandler;
56 using lyx::frontend::XformsColor;
57 using lyx::frontend::XFormsView;
59 namespace os = lyx::support::os;
61 #ifndef CXX_GLOBAL_CSTD
71 using std::ostringstream;
76 extern BufferList bufferlist;
78 // FIXME: wrong place !
79 LyXServer * lyxserver;
80 LyXServerSocket * lyxsocket;
85 bool finished = false;
87 /// estimate DPI from X server
90 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
91 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
92 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
96 /// set default GUI configuration
100 cntl.buttonFontSize = FL_NORMAL_SIZE;
101 cntl.browserFontSize = FL_NORMAL_SIZE;
102 cntl.labelFontSize = FL_NORMAL_SIZE;
103 cntl.choiceFontSize = FL_NORMAL_SIZE;
104 cntl.inputFontSize = FL_NORMAL_SIZE;
105 cntl.menuFontSize = FL_NORMAL_SIZE;
106 cntl.borderWidth = -1;
107 cntl.vclass = FL_DefaultVisual;
108 fl_set_defaults(FL_PDVisual
109 | FL_PDButtonFontSize
110 | FL_PDBrowserFontSize
112 | FL_PDChoiceFontSize
115 | FL_PDBorderWidth, &cntl);
121 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
122 // We don't abort on BadWindow
123 if (xeev->error_code == BadWindow) {
124 lyxerr << "BadWindow received !" << endl;
125 lyxerr << "If you're using xforms 1.0 or greater, "
126 << " please report this to lyx-devel@lists.lyx.org" << endl;
131 LyX::cref().emergencyCleanup();
133 // Get the reason for the crash.
135 XGetErrorText(display, xeev->error_code, etxt, 512);
136 lyxerr << etxt << " id: " << xeev->resourceid << endl;
137 // By doing an abort we get a nice backtrace. (hopefully)
138 lyx::support::abort();
144 /// read in geometry specification
155 void parse_init(int & argc, char * argv[])
159 FL_CMD_OPT cmdopt[] = {
160 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
163 FL_resource res[] = {
164 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
167 const int num_res = sizeof(res)/sizeof(FL_resource);
169 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
171 // It appears that, in xforms >=0.89.5, fl_initialize()
172 // calls setlocale() and ruins our LC_NUMERIC setting.
175 fl_get_app_resources(res, num_res);
177 Display * display = fl_get_display();
180 lyxerr << "LyX: unable to access X display, exiting" << endl;
181 os::warn("Unable to access X display, exiting");
185 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
187 XSetErrorHandler(LyX_XErrHandler);
189 lyxColorHandler.reset(new LyXColorHandler);
191 using namespace lyx::graphics;
193 // connect the image loader based on the xforms library
194 Image::newImage = boost::bind(&xformsImage::newImage);
195 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
197 // must do this /before/ lyxrc gets read
198 lyxrc.dpi = getDPI();
200 LoaderQueue::setPriority(10,100);
206 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
208 if (lyxrc.popup_font_encoding.empty())
209 lyxrc.popup_font_encoding = lyxrc.font_norm;
210 // Set the font name for popups and menus
211 string boldfontname = lyxrc.popup_bold_font
212 + "-*-*-*-?-*-*-*-*-"
213 + lyxrc.popup_font_encoding;
214 // "?" means "scale that font"
215 string fontname = lyxrc.popup_normal_font
216 + "-*-*-*-?-*-*-*-*-"
217 + lyxrc.popup_font_encoding;
219 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
220 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
222 lyxerr << "Could not set menu font to "
223 << boldfontname << endl;
226 lyxerr << "Could not set popup font to "
229 if (bold < 0 && normal < 0) {
230 lyxerr << "Using 'helvetica' font for menus" << endl;
231 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
232 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
233 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
234 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
236 if (bold < 0 && normal < 0) {
237 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
238 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
243 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
245 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
247 fl_setpup_fontstyle(FL_NORMAL_STYLE);
248 fl_setpup_fontsize(FL_NORMAL_SIZE);
249 fl_setpup_color(FL_MCOL, FL_BLACK);
250 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
251 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
255 void start(string const & batch, vector<string> const & files)
260 unsigned int width = 690;
261 unsigned int height = 510;
263 int const geometryBitmask =
264 XParseGeometry(geometry,
265 &xpos, &ypos, &width, &height);
267 // if width is not set by geometry, check it against monitor width
268 if (!(geometryBitmask & WidthValue)) {
269 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
270 if (WidthOfScreen(scr) - 8 < int(width))
271 width = WidthOfScreen(scr) - 8;
274 // if height is not set by geometry, check it against monitor height
275 if (!(geometryBitmask & HeightValue)) {
276 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
277 if (HeightOfScreen(scr) - 24 < int(height))
278 height = HeightOfScreen(scr) - 24;
281 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
283 // recalculate xpos if it's not set
285 xpos = (WidthOfScreen(s) - width) / 2;
287 // recalculate ypos if it's not set
289 ypos = (HeightOfScreen(s) - height) / 2;
291 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
292 << '+' << xpos << '+' << ypos << endl;
294 boost::shared_ptr<XFormsView> view_ptr(new XFormsView(width, height));
295 LyX::ref().addLyXView(view_ptr);
297 XFormsView & view = *view_ptr.get();
298 view.show(xpos, ypos, "LyX");
301 // FIXME: some code below needs moving
303 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
304 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
305 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
307 vector<string>::const_iterator cit = files.begin();
308 vector<string>::const_iterator end = files.end();
309 for (; cit != end; ++cit)
310 view.view()->loadLyXFile(*cit, true);
312 // handle the batch commands the user asked for
314 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
316 // enter the event loop
318 if (fl_check_forms() == FL_EVENT) {
322 << "Received unhandled X11 event" << endl
323 << "Type: " << ev.xany.type
324 << " Target: 0x" << hex << ev.xany.window
329 // FIXME: breaks emergencyCleanup
347 FuncStatus getStatus(FuncRequest const & /*ev*/)
349 // Nothing interesting to do here
353 string const hexname(LColor_color col)
355 unsigned int r, g, b;
356 bool const success = getRGBColor(col, r, g, b);
358 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
365 os << setbase(16) << setfill('0')
374 void update_color(LColor_color col)
376 lyxColorHandler->getGCForeground(col);
377 lyxColorHandler->updateColor(col);
387 bool font_available(LyXFont const & font)
389 return fontloader.available(font);
395 void C_read_callback(int, void * data)
397 LyXComm * comm = static_cast<LyXComm *>(data);
402 void C_datasocket_callback(int, void * data)
404 LyXDataSocket * client = static_cast<LyXDataSocket *>(data);
405 client->server()->dataCallback(client);
409 void C_serversocket_callback(int, void * data)
411 LyXServerSocket * server = static_cast<LyXServerSocket *>(data);
412 server->serverCallback();
417 void set_read_callback(int fd, LyXComm * comm)
419 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
422 void remove_read_callback(int fd)
424 fl_remove_io_callback(fd, FL_READ, C_read_callback);
427 void set_datasocket_callback(LyXDataSocket * p)
429 fl_add_io_callback(p->fd(), FL_READ, C_datasocket_callback, p);
432 void remove_datasocket_callback(LyXDataSocket * p)
434 fl_remove_io_callback(p->fd(), FL_READ, C_datasocket_callback);
437 void set_serversocket_callback(LyXServerSocket * p)
439 fl_add_io_callback(p->fd(), FL_READ, C_serversocket_callback, p);
442 void remove_serversocket_callback(LyXServerSocket * p)
444 fl_remove_io_callback(p->fd(), FL_READ, C_serversocket_callback);
447 string const roman_font_name()
453 string const sans_font_name()
459 string const typewriter_font_name()
464 }; // namespace lyx_gui