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"
30 #include "graphics/LoaderQueue.h"
32 // FIXME: move this stuff out again
33 #include "bufferlist.h"
34 #include "buffer_funcs.h"
36 #include "lyxserver.h"
37 #include "BufferView.h"
42 #include "xftFontLoader.h"
43 #include "GWorkArea.h"
45 #include "support/std_sstream.h"
48 #include <boost/bind.hpp>
51 #include "lyx_forms.h"
52 #include "xformsImage.h"
53 #include "xforms_helpers.h"
55 using std::ostringstream;
59 extern BufferList bufferlist;
61 // FIXME: wrong place !
62 LyXServer * lyxserver;
64 bool lyx_gui::use_gui = true;
69 bool finished = false;
72 /// estimate DPI from X server
75 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
76 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
77 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
81 /// set default GUI configuration
85 cntl.buttonFontSize = FL_NORMAL_SIZE;
86 cntl.browserFontSize = FL_NORMAL_SIZE;
87 cntl.labelFontSize = FL_NORMAL_SIZE;
88 cntl.choiceFontSize = FL_NORMAL_SIZE;
89 cntl.inputFontSize = FL_NORMAL_SIZE;
90 cntl.menuFontSize = FL_NORMAL_SIZE;
91 cntl.borderWidth = -1;
92 cntl.vclass = FL_DefaultVisual;
93 fl_set_defaults(FL_PDVisual
95 | FL_PDBrowserFontSize
100 | FL_PDBorderWidth, &cntl);
107 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
108 // We don't abort on BadWindow
109 if (xeev->error_code == BadWindow) {
110 lyxerr << "BadWindow received !" << std::endl;
111 lyxerr << "If you're using xforms 1.0 or greater, "
112 << " please report this to lyx-devel@lists.lyx.org"
118 LyX::emergencyCleanup();
120 // Get the reason for the crash.
122 XGetErrorText(display, xeev->error_code, etxt, 512);
123 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
124 // By doing an abort we get a nice backtrace. (hopefully)
125 lyx::support::abort();
131 /// read in geometry specification
137 void parse_init_xforms(int & argc, char * argv[])
141 FL_CMD_OPT cmdopt[] = {
142 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
145 FL_resource res[] = {
146 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
149 const int num_res = sizeof(res)/sizeof(FL_resource);
150 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
152 // It appears that, in xforms >=0.89.5, fl_initialize()
153 // calls setlocale() and ruins our LC_NUMERIC setting.
155 fl_get_app_resources(res, num_res);
157 Display * display = fl_get_display();
160 lyxerr << "LyX: unable to access X display, exiting"
162 lyx::support::os::warn("Unable to access X display, exiting");
166 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
168 XSetErrorHandler(LyX_XErrHandler);
170 using namespace lyx::graphics;
172 // connect the image loader based on the xforms library
173 Image::newImage = boost::bind(&xformsImage::newImage);
174 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
178 void lyx_gui::parse_init(int & argc, char * argv[])
180 new Gtk::Main(argc, argv);
182 parse_init_xforms(argc, argv);
186 // must do this /before/ lyxrc gets read
187 lyxrc.dpi = getDPI();
191 void parse_lyxrc_xforms()
193 XformsColor::read(lyx::support::AddName(
194 lyx::support::user_lyxdir(), "preferences.xform"));
196 if (lyxrc.popup_font_encoding.empty())
197 lyxrc.popup_font_encoding = lyxrc.font_norm;
198 // Set the font name for popups and menus
199 string boldfontname = lyxrc.popup_bold_font
200 + "-*-*-*-?-*-*-*-*-"
201 + lyxrc.popup_font_encoding;
202 // "?" means "scale that font"
203 string fontname = lyxrc.popup_normal_font
204 + "-*-*-*-?-*-*-*-*-"
205 + lyxrc.popup_font_encoding;
207 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
208 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
210 lyxerr << "Could not set menu font to "
211 << boldfontname << std::endl;
214 lyxerr << "Could not set popup font to "
215 << fontname << std::endl;
217 if (bold < 0 && normal < 0) {
218 lyxerr << "Using 'helvetica' font for menus" << std::endl;
219 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
220 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
221 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
222 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
224 if (bold < 0 && normal < 0) {
225 lyxerr << "Could not find helvetica font. Using 'fixed'."
227 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
232 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
234 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
236 fl_setpup_fontstyle(FL_NORMAL_STYLE);
237 fl_setpup_fontsize(FL_NORMAL_SIZE);
238 fl_setpup_color(FL_MCOL, FL_BLACK);
239 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
240 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
244 void lyx_gui::parse_lyxrc()
246 parse_lyxrc_xforms();
255 unsigned int width = 690;
256 unsigned int height = 510;
258 int const geometryBitmask =
259 XParseGeometry(geometry,
260 &xpos, &ypos, &width, &height);
262 // if width is not set by geometry, check it against monitor width
263 if (!(geometryBitmask & WidthValue)) {
264 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
265 if (WidthOfScreen(scr) - 8 < int(width))
266 width = WidthOfScreen(scr) - 8;
269 // if height is not set by geometry, check it against monitor height
270 if (!(geometryBitmask & HeightValue)) {
271 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
272 if (HeightOfScreen(scr) - 24 < int(height))
273 height = HeightOfScreen(scr) - 24;
276 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
278 // recalculate xpos if it's not set
280 xpos = (WidthOfScreen(s) - width) / 2;
282 // recalculate ypos if it's not set
284 ypos = (HeightOfScreen(s) - height) / 2;
286 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
287 << '+' << xpos << '+' << ypos << std::endl;
289 // XFormsView view(width, height);
290 // view.show(xpos, ypos, "LyX");
295 static void events_xforms()
297 if (fl_check_forms() == FL_EVENT) {
301 << "Received unhandled X11 event" << std::endl
302 << "Type: " << ev.xany.type
303 << " Target: 0x" << std::hex << ev.xany.window
304 << std::dec << std::endl;
309 void lyx_gui::start(string const & batch, std::vector<string> const & files)
313 XSynchronize(getDisplay(), true);
320 // FIXME: some code below needs moving
322 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
324 std::vector<string>::const_iterator cit = files.begin();
325 std::vector<string>::const_iterator end = files.end();
326 for (; cit != end; ++cit) {
327 Buffer * b = bufferlist.newBuffer(*cit);
328 if (loadLyXFile(b, *cit))
332 // switch to the last buffer successfully loaded
334 view.view()->buffer(last);
337 // handle the batch commands the user asked for
338 if (!batch.empty()) {
339 view.getLyXFunc().dispatch(batch);
342 // enter the event loop
344 while (Gtk::Main::events_pending())
345 Gtk::Main::iteration(false);
349 // FIXME: breaks emergencyCleanup
360 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
362 // Nothing interesting to do here
367 string const lyx_gui::hexname(LColor_color col)
370 Gdk::Color * gclr = colorCache.getColor(col);
373 gclr->parse(lcolor.getX11Name(col));
376 std::ostringstream os;
378 // Note that X stores the RGB values in the range 0 - 65535
379 // whilst we require them in the range 0 - 255.
380 os << std::setbase(16) << std::setfill('0')
381 << std::setw(2) << (gclr->get_red() / 256)
382 << std::setw(2) << (gclr->get_green() / 256)
383 << std::setw(2) << (gclr->get_blue() / 256);
389 void lyx_gui::update_color(LColor_color /*col*/)
395 void lyx_gui::update_fonts()
401 bool lyx_gui::font_available(LyXFont const & font)
403 return fontLoader.available(font);
410 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
417 std::map<int, SigC::Connection> gReadCallbackMap;
422 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
424 gReadCallbackMap[fd] = Glib::signal_io().connect(
425 SigC::bind(SigC::slot(readCallback), comm),
431 void lyx_gui::remove_read_callback(int fd)
433 gReadCallbackMap[fd].disconnect();
434 gReadCallbackMap.erase(fd);
438 void set_datasocket_callback(LyXDataSocket * /* p */)
442 void remove_datasocket_callback(LyXDataSocket * /* p */)
446 void set_serversocket_callback(LyXServerSocket * /* p */)
450 void remove_serversocket_callback(LyXServerSocket * /* p */)
454 string const lyx_gui::roman_font_name()
460 string const lyx_gui::sans_font_name()
466 string const lyx_gui::typewriter_font_name()
472 void lyx_gui::sync_events()