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>
50 using lyx::support::AddName;
51 using lyx::support::user_lyxdir;
53 using lyx::frontend::fontloader;
54 using lyx::frontend::getRGBColor;
55 using lyx::frontend::lyxColorHandler;
56 using lyx::frontend::LyXColorHandler;
57 using lyx::frontend::XformsColor;
58 using lyx::frontend::XFormsView;
60 namespace os = lyx::support::os;
62 #ifndef CXX_GLOBAL_CSTD
72 using std::ostringstream;
77 extern BufferList bufferlist;
79 // FIXME: wrong place !
80 LyXServer * lyxserver;
81 LyXServerSocket * lyxsocket;
86 bool finished = false;
88 /// estimate DPI from X server
91 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
92 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
93 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
97 /// set default GUI configuration
101 cntl.buttonFontSize = FL_NORMAL_SIZE;
102 cntl.browserFontSize = FL_NORMAL_SIZE;
103 cntl.labelFontSize = FL_NORMAL_SIZE;
104 cntl.choiceFontSize = FL_NORMAL_SIZE;
105 cntl.inputFontSize = FL_NORMAL_SIZE;
106 cntl.menuFontSize = FL_NORMAL_SIZE;
107 cntl.borderWidth = -1;
108 cntl.vclass = FL_DefaultVisual;
109 fl_set_defaults(FL_PDVisual
110 | FL_PDButtonFontSize
111 | FL_PDBrowserFontSize
113 | FL_PDChoiceFontSize
116 | FL_PDBorderWidth, &cntl);
122 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
123 // We don't abort on BadWindow
124 if (xeev->error_code == BadWindow) {
125 lyxerr << "BadWindow received !" << endl;
126 lyxerr << "If you're using xforms 1.0 or greater, "
127 << " please report this to lyx-devel@lists.lyx.org" << endl;
132 LyX::cref().emergencyCleanup();
134 // Get the reason for the crash.
136 XGetErrorText(display, xeev->error_code, etxt, 512);
137 lyxerr << etxt << " id: " << xeev->resourceid << endl;
138 // By doing an abort we get a nice backtrace. (hopefully)
139 lyx::support::abort();
145 /// read in geometry specification
156 void parse_init(int & argc, char * argv[])
160 FL_CMD_OPT cmdopt[] = {
161 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
164 FL_resource res[] = {
165 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
168 const int num_res = sizeof(res)/sizeof(FL_resource);
170 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
172 // It appears that, in xforms >=0.89.5, fl_initialize()
173 // calls setlocale() and ruins our LC_NUMERIC setting.
176 fl_get_app_resources(res, num_res);
178 Display * display = fl_get_display();
181 lyxerr << "LyX: unable to access X display, exiting" << endl;
182 os::warn("Unable to access X display, exiting");
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(user_lyxdir(), "preferences.xform"));
209 if (lyxrc.popup_font_encoding.empty())
210 lyxrc.popup_font_encoding = lyxrc.font_norm;
211 // Set the font name for popups and menus
212 string boldfontname = lyxrc.popup_bold_font
213 + "-*-*-*-?-*-*-*-*-"
214 + lyxrc.popup_font_encoding;
215 // "?" means "scale that font"
216 string fontname = lyxrc.popup_normal_font
217 + "-*-*-*-?-*-*-*-*-"
218 + lyxrc.popup_font_encoding;
220 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
221 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
223 lyxerr << "Could not set menu font to "
224 << boldfontname << endl;
227 lyxerr << "Could not set popup font to "
230 if (bold < 0 && normal < 0) {
231 lyxerr << "Using 'helvetica' font for menus" << endl;
232 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
233 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
234 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
235 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
237 if (bold < 0 && normal < 0) {
238 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
239 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
244 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
246 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
248 fl_setpup_fontstyle(FL_NORMAL_STYLE);
249 fl_setpup_fontsize(FL_NORMAL_SIZE);
250 fl_setpup_color(FL_MCOL, FL_BLACK);
251 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
252 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
256 void start(string const & batch, vector<string> const & files)
261 unsigned int width = 690;
262 unsigned int height = 510;
264 int const geometryBitmask =
265 XParseGeometry(geometry,
266 &xpos, &ypos, &width, &height);
268 // if width is not set by geometry, check it against monitor width
269 if (!(geometryBitmask & WidthValue)) {
270 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
271 if (WidthOfScreen(scr) - 8 < int(width))
272 width = WidthOfScreen(scr) - 8;
275 // if height is not set by geometry, check it against monitor height
276 if (!(geometryBitmask & HeightValue)) {
277 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
278 if (HeightOfScreen(scr) - 24 < int(height))
279 height = HeightOfScreen(scr) - 24;
282 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
284 // recalculate xpos if it's not set
286 xpos = (WidthOfScreen(s) - width) / 2;
288 // recalculate ypos if it's not set
290 ypos = (HeightOfScreen(s) - height) / 2;
292 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
293 << '+' << xpos << '+' << ypos << endl;
295 boost::shared_ptr<XFormsView> view(new XFormsView(width, height));
296 LyX::ref().addLyXView(view);
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 std::map<int, boost::function<void()> > socket_callbacks;
398 void C_socket_callback(int fd, void *)
400 socket_callbacks[fd]();
407 void register_socket_callback(int fd, boost::function<void()> func)
409 socket_callbacks[fd] = func;
410 fl_add_io_callback(fd, FL_READ, C_socket_callback, 0);
414 void unregister_socket_callback(int fd)
416 fl_remove_io_callback(fd, FL_READ, C_socket_callback);
417 socket_callbacks.erase(fd);
421 string const roman_font_name()
427 string const sans_font_name()
433 string const typewriter_font_name()
438 }; // namespace lyx_gui