3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjnes
10 * Full author contact details are available in file CREDITS.
18 #include "support/lyxlib.h"
19 #include "support/os.h"
20 #include "support/filetools.h"
21 #include "support/path_defines.h"
24 #include "funcrequest.h"
28 #include "LyXAction.h"
32 #include "graphics/LoaderQueue.h"
34 #include "io_callback.h"
36 // FIXME: move this stuff out again
37 #include "bufferlist.h"
38 #include "buffer_funcs.h"
40 #include "lyxserver.h"
41 #include "lyxsocket.h"
42 #include "BufferView.h"
47 #include "xftFontLoader.h"
48 #include "GWorkArea.h"
50 #include "support/std_sstream.h"
53 #include <boost/bind.hpp>
54 #include <boost/function.hpp>
55 #include <boost/shared_ptr.hpp>
58 #include "lyx_forms.h"
59 #include "xformsImage.h"
60 #include "xforms_helpers.h"
62 namespace os = lyx::support::os;
64 using std::ostringstream;
67 using lyx::frontend::colorCache;
68 using lyx::frontend::GView;
69 using lyx::frontend::XformsColor;
72 extern BufferList bufferlist;
74 // FIXME: wrong place !
75 LyXServer * lyxserver;
76 LyXServerSocket * lyxsocket;
78 bool lyx_gui::use_gui = true;
83 bool finished = false;
86 /// estimate DPI from X server
89 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
90 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
91 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
95 /// set default GUI configuration
99 cntl.buttonFontSize = FL_NORMAL_SIZE;
100 cntl.browserFontSize = FL_NORMAL_SIZE;
101 cntl.labelFontSize = FL_NORMAL_SIZE;
102 cntl.choiceFontSize = FL_NORMAL_SIZE;
103 cntl.inputFontSize = FL_NORMAL_SIZE;
104 cntl.menuFontSize = FL_NORMAL_SIZE;
105 cntl.borderWidth = -1;
106 cntl.vclass = FL_DefaultVisual;
107 fl_set_defaults(FL_PDVisual
108 | FL_PDButtonFontSize
109 | FL_PDBrowserFontSize
111 | FL_PDChoiceFontSize
114 | 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 !" << std::endl;
125 lyxerr << "If you're using xforms 1.0 or greater, "
126 << " please report this to lyx-devel@lists.lyx.org"
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 << std::endl;
138 // By doing an abort we get a nice backtrace. (hopefully)
139 lyx::support::abort();
145 /// read in geometry specification
151 void parse_init_xforms(int & argc, char * argv[])
155 FL_CMD_OPT cmdopt[] = {
156 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
159 FL_resource res[] = {
160 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
163 const int num_res = sizeof(res)/sizeof(FL_resource);
164 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
166 // It appears that, in xforms >=0.89.5, fl_initialize()
167 // calls setlocale() and ruins our LC_NUMERIC setting.
169 fl_get_app_resources(res, num_res);
171 Display * display = fl_get_display();
174 lyxerr << "LyX: unable to access X display, exiting"
176 lyx::support::os::warn("Unable to access X display, exiting");
180 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
182 XSetErrorHandler(LyX_XErrHandler);
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);
192 void lyx_gui::parse_init(int & argc, char * argv[])
194 new Gtk::Main(argc, argv);
196 parse_init_xforms(argc, argv);
200 // must do this /before/ lyxrc gets read
201 lyxrc.dpi = getDPI();
205 void parse_lyxrc_xforms()
207 XformsColor::read(lyx::support::AddName(
208 lyx::support::user_lyxdir(), "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 << std::endl;
228 lyxerr << "Could not set popup font to "
229 << fontname << std::endl;
231 if (bold < 0 && normal < 0) {
232 lyxerr << "Using 'helvetica' font for menus" << std::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'."
241 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
246 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
248 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
250 fl_setpup_fontstyle(FL_NORMAL_STYLE);
251 fl_setpup_fontsize(FL_NORMAL_SIZE);
252 fl_setpup_color(FL_MCOL, FL_BLACK);
253 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
254 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
258 void lyx_gui::parse_lyxrc()
260 parse_lyxrc_xforms();
269 unsigned int width = 690;
270 unsigned int height = 510;
272 int const geometryBitmask =
273 XParseGeometry(geometry,
274 &xpos, &ypos, &width, &height);
276 // if width is not set by geometry, check it against monitor width
277 if (!(geometryBitmask & WidthValue)) {
278 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
279 if (WidthOfScreen(scr) - 8 < int(width))
280 width = WidthOfScreen(scr) - 8;
283 // if height is not set by geometry, check it against monitor height
284 if (!(geometryBitmask & HeightValue)) {
285 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
286 if (HeightOfScreen(scr) - 24 < int(height))
287 height = HeightOfScreen(scr) - 24;
290 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
292 // recalculate xpos if it's not set
294 xpos = (WidthOfScreen(s) - width) / 2;
296 // recalculate ypos if it's not set
298 ypos = (HeightOfScreen(s) - height) / 2;
300 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
301 << '+' << xpos << '+' << ypos << std::endl;
303 // XFormsView view(width, height);
304 // view.show(xpos, ypos, "LyX");
309 static void events_xforms()
311 if (fl_check_forms() == FL_EVENT) {
315 << "Received unhandled X11 event" << std::endl
316 << "Type: " << ev.xany.type
317 << " Target: 0x" << std::hex << ev.xany.window
318 << std::dec << std::endl;
323 void lyx_gui::start(string const & batch, std::vector<string> const & files)
327 XSynchronize(getDisplay(), true);
329 boost::shared_ptr<GView> view_ptr(new GView);
330 LyX::ref().addLyXView(view_ptr);
332 GView & view = *view_ptr.get();
336 // FIXME: some code below needs moving
338 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
339 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
340 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
342 std::vector<string>::const_iterator cit = files.begin();
343 std::vector<string>::const_iterator end = files.end();
344 for (; cit != end; ++cit)
345 view.view()->loadLyXFile(*cit, true);
347 // handle the batch commands the user asked for
348 if (!batch.empty()) {
349 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
352 // enter the event loop
354 while (Gtk::Main::events_pending())
355 Gtk::Main::iteration(false);
359 // FIXME: breaks emergencyCleanup
371 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
373 // Nothing interesting to do here
378 string const lyx_gui::hexname(LColor_color col)
381 Gdk::Color * gclr = colorCache.getColor(col);
384 gclr->parse(lcolor.getX11Name(col));
387 std::ostringstream os;
389 // Note that X stores the RGB values in the range 0 - 65535
390 // whilst we require them in the range 0 - 255.
391 os << std::setbase(16) << std::setfill('0')
392 << std::setw(2) << (gclr->get_red() / 256)
393 << std::setw(2) << (gclr->get_green() / 256)
394 << std::setw(2) << (gclr->get_blue() / 256);
400 void lyx_gui::update_color(LColor_color /*col*/)
406 void lyx_gui::update_fonts()
412 bool lyx_gui::font_available(LyXFont const & font)
414 return fontLoader.available(font);
420 std::map<int, boost::shared_ptr<io_callback> > callbacks;
425 void lyx_gui::register_socket_callback(int fd,
426 boost::function<void()> func)
428 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
432 void lyx_gui::unregister_socket_callback(int fd)
438 string const lyx_gui::roman_font_name()
444 string const lyx_gui::sans_font_name()
450 string const lyx_gui::typewriter_font_name()
456 void lyx_gui::sync_events()