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"
32 #include "lyxserver.h"
33 #include "lyxsocket.h"
35 #include "graphics/LoaderQueue.h"
37 #include "support/filetools.h"
38 #include "support/lyxlib.h"
39 #include "support/os.h"
40 #include "support/package.h"
41 #include "support/convert.h"
43 #include "lyx_forms.h"
45 #include <boost/bind.hpp>
52 using lyx::support::addName;
53 using lyx::support::package;
55 using lyx::frontend::fontloader;
56 using lyx::frontend::lyxColorHandler;
57 using lyx::frontend::LyXColorHandler;
58 using lyx::frontend::XformsColor;
59 using lyx::frontend::XFormsView;
61 namespace os = lyx::support::os;
63 #ifndef CXX_GLOBAL_CSTD
73 using std::ostringstream;
78 extern BufferList bufferlist;
80 // FIXME: wrong place !
81 LyXServer * lyxserver;
82 LyXServerSocket * lyxsocket;
87 bool finished = false;
89 /// estimate DPI from X server
92 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
93 return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
94 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
98 /// set default GUI configuration
102 cntl.buttonFontSize = FL_NORMAL_SIZE;
103 cntl.browserFontSize = FL_NORMAL_SIZE;
104 cntl.labelFontSize = FL_NORMAL_SIZE;
105 cntl.choiceFontSize = FL_NORMAL_SIZE;
106 cntl.inputFontSize = FL_NORMAL_SIZE;
107 cntl.menuFontSize = FL_NORMAL_SIZE;
108 cntl.borderWidth = -1;
109 cntl.vclass = FL_DefaultVisual;
110 fl_set_defaults(FL_PDVisual
111 | FL_PDButtonFontSize
112 | FL_PDBrowserFontSize
114 | FL_PDChoiceFontSize
117 | FL_PDBorderWidth, &cntl);
123 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
124 // We don't abort on BadWindow
125 if (xeev->error_code == BadWindow) {
126 lyxerr << "BadWindow received !" << endl;
127 lyxerr << "If you're using xforms 1.0 or greater, "
128 << " please report this to lyx-devel@lists.lyx.org" << endl;
133 LyX::cref().emergencyCleanup();
135 // Get the reason for the crash.
137 XGetErrorText(display, xeev->error_code, etxt, 512);
138 lyxerr << etxt << " id: " << xeev->resourceid << endl;
139 // By doing an abort we get a nice backtrace. (hopefully)
140 lyx::support::abort();
146 /// read in geometry specification
157 void parse_init(int & argc, char * argv[])
161 FL_CMD_OPT cmdopt[] = {
162 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
165 FL_resource res[] = {
166 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
169 const int num_res = sizeof(res)/sizeof(FL_resource);
171 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
173 // It appears that, in xforms >=0.89.5, fl_initialize()
174 // calls setlocale() and ruins our LC_NUMERIC setting.
177 fl_get_app_resources(res, num_res);
179 Display * display = fl_get_display();
182 lyxerr << "LyX: unable to access X display, exiting" << endl;
186 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
188 XSetErrorHandler(LyX_XErrHandler);
190 lyxColorHandler.reset(new LyXColorHandler);
192 using namespace lyx::graphics;
194 // connect the image loader based on the xforms library
195 Image::newImage = boost::bind(&xformsImage::newImage);
196 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
198 // must do this /before/ lyxrc gets read
199 lyxrc.dpi = getDPI();
201 LoaderQueue::setPriority(10,100);
207 XformsColor::read(addName(package().user_support(),
208 "preferences.xform"));
210 if (lyxrc.popup_font_encoding.empty())
211 lyxrc.popup_font_encoding = lyxrc.font_norm;
212 // Set the font name for popups and menus
213 string boldfontname = lyxrc.popup_bold_font
214 + "-*-*-*-?-*-*-*-*-"
215 + lyxrc.popup_font_encoding;
216 // "?" means "scale that font"
217 string fontname = lyxrc.popup_normal_font
218 + "-*-*-*-?-*-*-*-*-"
219 + lyxrc.popup_font_encoding;
221 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
222 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
224 lyxerr << "Could not set menu font to "
225 << boldfontname << endl;
228 lyxerr << "Could not set popup font to "
231 if (bold < 0 && normal < 0) {
232 lyxerr << "Using 'helvetica' font for menus" << endl;
233 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
234 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
235 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
236 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
238 if (bold < 0 && normal < 0) {
239 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
240 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
245 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
247 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
249 fl_setpup_fontstyle(FL_NORMAL_STYLE);
250 fl_setpup_fontsize(FL_NORMAL_SIZE);
251 fl_setpup_color(FL_MCOL, FL_BLACK);
252 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
253 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
257 void start(string const & batch, vector<string> const & files)
262 unsigned int width = 690;
263 unsigned int height = 510;
265 if (lyxrc.geometry_width != 0 && lyxrc.geometry_height != 0 ) {
266 width = lyxrc.geometry_width;
267 height = lyxrc.geometry_height;
269 // if lyxrc returns (0,0), then use session info
271 string val = LyX::ref().session().loadSessionInfo("WindowWidth");
273 width = convert<unsigned int>(val);
274 val = LyX::ref().session().loadSessionInfo("WindowHeight");
276 height = convert<unsigned int>(val);
279 int const geometryBitmask =
280 XParseGeometry(geometry,
281 &xpos, &ypos, &width, &height);
283 // if width is not set by geometry, check it against monitor width
284 if (!(geometryBitmask & WidthValue)) {
285 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
286 if (WidthOfScreen(scr) - 8 < int(width))
287 width = WidthOfScreen(scr) - 8;
290 // if height is not set by geometry, check it against monitor height
291 if (!(geometryBitmask & HeightValue)) {
292 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
293 if (HeightOfScreen(scr) - 24 < int(height))
294 height = HeightOfScreen(scr) - 24;
297 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
299 // recalculate xpos if it's not set
301 xpos = (WidthOfScreen(s) - width) / 2;
303 // recalculate ypos if it's not set
305 ypos = (HeightOfScreen(s) - height) / 2;
307 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
308 << '+' << xpos << '+' << ypos << endl;
310 boost::shared_ptr<XFormsView> view(new XFormsView(width, height));
311 LyX::ref().addLyXView(view);
313 view->show(xpos, ypos, "LyX");
316 // FIXME: some code below needs moving
318 lyxserver = new LyXServer(&view->getLyXFunc(), lyxrc.lyxpipes);
319 lyxsocket = new LyXServerSocket(&view->getLyXFunc(),
320 os::internal_path(package().temp_dir() + "/lyxsocket"));
322 for_each(files.begin(), files.end(),
323 bind(&BufferView::loadLyXFile, view->view(), _1, true));
325 // handle the batch commands the user asked for
327 view->getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
329 // enter the event loop
331 if (fl_check_forms() == FL_EVENT) {
335 << "Received unhandled X11 event" << endl
336 << "Type: " << ev.xany.type
337 << " Target: 0x" << hex << ev.xany.window
342 // FIXME: breaks emergencyCleanup
360 FuncStatus getStatus(FuncRequest const & /*ev*/)
362 // Nothing interesting to do here
367 bool getRGBColor(LColor_color col, lyx::RGBColor & rgbcol)
369 string const name = lcolor.getX11Name(col);
370 Display * const display = fl_get_display();
371 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
374 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
381 // Note that X stores the RGB values in the range 0 - 65535
382 // whilst we require them in the range 0 - 255.
383 rgbcol.r = xcol.red / 256;
384 rgbcol.g = xcol.green / 256;
385 rgbcol.b = xcol.blue / 256;
390 string const hexname(LColor_color col)
392 lyx::RGBColor rgbcol;
393 if (!getRGBColor(col, rgbcol)) {
394 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
401 os << setbase(16) << setfill('0')
402 << setw(2) << rgbcol.r
403 << setw(2) << rgbcol.g
404 << setw(2) << rgbcol.b;
410 void update_color(LColor_color col)
412 lyxColorHandler->getGCForeground(col);
413 lyxColorHandler->updateColor(col);
423 bool font_available(LyXFont const & font)
425 return fontloader.available(font);
431 std::map<int, boost::function<void()> > socket_callbacks;
434 void C_socket_callback(int fd, void *)
436 socket_callbacks[fd]();
443 void register_socket_callback(int fd, boost::function<void()> func)
445 socket_callbacks[fd] = func;
446 fl_add_io_callback(fd, FL_READ, C_socket_callback, 0);
450 void unregister_socket_callback(int fd)
452 fl_remove_io_callback(fd, FL_READ, C_socket_callback);
453 socket_callbacks.erase(fd);
457 string const roman_font_name()
463 string const sans_font_name()
469 string const typewriter_font_name()
474 }; // namespace lyx_gui