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"
29 #include "graphics/LoaderQueue.h"
31 // FIXME: move this stuff out again
32 #include "bufferlist.h"
33 #include "buffer_funcs.h"
35 #include "lyxserver.h"
36 #include "BufferView.h"
41 #include "xftFontLoader.h"
42 #include "GWorkArea.h"
44 #include "support/std_sstream.h"
47 #include <boost/bind.hpp>
50 #include "lyx_forms.h"
51 #include "xformsImage.h"
52 #include "xforms_helpers.h"
54 using std::ostringstream;
57 extern BufferList bufferlist;
59 // FIXME: wrong place !
60 LyXServer * lyxserver;
62 bool lyx_gui::use_gui = true;
67 bool finished = false;
70 /// estimate DPI from X server
73 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
74 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
75 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
79 /// set default GUI configuration
83 cntl.buttonFontSize = FL_NORMAL_SIZE;
84 cntl.browserFontSize = FL_NORMAL_SIZE;
85 cntl.labelFontSize = FL_NORMAL_SIZE;
86 cntl.choiceFontSize = FL_NORMAL_SIZE;
87 cntl.inputFontSize = FL_NORMAL_SIZE;
88 cntl.menuFontSize = FL_NORMAL_SIZE;
89 cntl.borderWidth = -1;
90 cntl.vclass = FL_DefaultVisual;
91 fl_set_defaults(FL_PDVisual
93 | FL_PDBrowserFontSize
98 | FL_PDBorderWidth, &cntl);
105 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
106 // We don't abort on BadWindow
107 if (xeev->error_code == BadWindow) {
108 lyxerr << "BadWindow received !" << std::endl;
109 lyxerr << "If you're using xforms 1.0 or greater, "
110 << " please report this to lyx-devel@lists.lyx.org"
116 LyX::emergencyCleanup();
118 // Get the reason for the crash.
120 XGetErrorText(display, xeev->error_code, etxt, 512);
121 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
122 // By doing an abort we get a nice backtrace. (hopefully)
123 lyx::support::abort();
129 /// read in geometry specification
135 void parse_init_xforms(int & argc, char * argv[])
139 FL_CMD_OPT cmdopt[] = {
140 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
143 FL_resource res[] = {
144 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
147 const int num_res = sizeof(res)/sizeof(FL_resource);
148 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
150 // It appears that, in xforms >=0.89.5, fl_initialize()
151 // calls setlocale() and ruins our LC_NUMERIC setting.
153 fl_get_app_resources(res, num_res);
155 Display * display = fl_get_display();
158 lyxerr << "LyX: unable to access X display, exiting"
160 lyx::support::os::warn("Unable to access X display, exiting");
164 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
166 XSetErrorHandler(LyX_XErrHandler);
168 using namespace lyx::graphics;
170 // connect the image loader based on the xforms library
171 Image::newImage = boost::bind(&xformsImage::newImage);
172 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
176 void lyx_gui::parse_init(int & argc, char * argv[])
178 new Gtk::Main(argc, argv);
180 parse_init_xforms(argc, argv);
184 // must do this /before/ lyxrc gets read
185 lyxrc.dpi = getDPI();
189 void parse_lyxrc_xforms()
191 XformsColor::read(lyx::support::AddName(
192 lyx::support::user_lyxdir(), "preferences.xform"));
194 if (lyxrc.popup_font_encoding.empty())
195 lyxrc.popup_font_encoding = lyxrc.font_norm;
196 // Set the font name for popups and menus
197 string boldfontname = lyxrc.popup_bold_font
198 + "-*-*-*-?-*-*-*-*-"
199 + lyxrc.popup_font_encoding;
200 // "?" means "scale that font"
201 string fontname = lyxrc.popup_normal_font
202 + "-*-*-*-?-*-*-*-*-"
203 + lyxrc.popup_font_encoding;
205 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
206 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
208 lyxerr << "Could not set menu font to "
209 << boldfontname << std::endl;
212 lyxerr << "Could not set popup font to "
213 << fontname << std::endl;
215 if (bold < 0 && normal < 0) {
216 lyxerr << "Using 'helvetica' font for menus" << std::endl;
217 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
218 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
219 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
220 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
222 if (bold < 0 && normal < 0) {
223 lyxerr << "Could not find helvetica font. Using 'fixed'."
225 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
230 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
232 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
234 fl_setpup_fontstyle(FL_NORMAL_STYLE);
235 fl_setpup_fontsize(FL_NORMAL_SIZE);
236 fl_setpup_color(FL_MCOL, FL_BLACK);
237 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
238 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 void lyx_gui::parse_lyxrc()
244 parse_lyxrc_xforms();
253 unsigned int width = 690;
254 unsigned int height = 510;
256 int const geometryBitmask =
257 XParseGeometry(geometry,
258 &xpos, &ypos, &width, &height);
260 // if width is not set by geometry, check it against monitor width
261 if (!(geometryBitmask & WidthValue)) {
262 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
263 if (WidthOfScreen(scr) - 8 < int(width))
264 width = WidthOfScreen(scr) - 8;
267 // if height is not set by geometry, check it against monitor height
268 if (!(geometryBitmask & HeightValue)) {
269 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
270 if (HeightOfScreen(scr) - 24 < int(height))
271 height = HeightOfScreen(scr) - 24;
274 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
276 // recalculate xpos if it's not set
278 xpos = (WidthOfScreen(s) - width) / 2;
280 // recalculate ypos if it's not set
282 ypos = (HeightOfScreen(s) - height) / 2;
284 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
285 << '+' << xpos << '+' << ypos << std::endl;
287 // XFormsView view(width, height);
288 // view.show(xpos, ypos, "LyX");
293 static void events_xforms()
295 if (fl_check_forms() == FL_EVENT) {
299 << "Received unhandled X11 event" << std::endl
300 << "Type: " << ev.xany.type
301 << " Target: 0x" << std::hex << ev.xany.window
302 << std::dec << std::endl;
307 void lyx_gui::start(string const & batch, std::vector<string> const & files)
311 XSynchronize(getDisplay(), true);
318 // FIXME: some code below needs moving
320 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
322 std::vector<string>::const_iterator cit = files.begin();
323 std::vector<string>::const_iterator end = files.end();
324 for (; cit != end; ++cit) {
325 Buffer * b = bufferlist.newBuffer(*cit);
326 if (loadLyXFile(b, *cit))
330 // switch to the last buffer successfully loaded
332 view.view()->buffer(last);
335 // handle the batch commands the user asked for
336 if (!batch.empty()) {
337 view.getLyXFunc().dispatch(batch);
340 // enter the event loop
342 while (Gtk::Main::events_pending())
343 Gtk::Main::iteration(false);
347 // FIXME: breaks emergencyCleanup
358 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
360 // Nothing interesting to do here
365 string const lyx_gui::hexname(LColor::color col)
368 Gdk::Color * gclr = colorCache.getColor(col);
371 gclr->parse(lcolor.getX11Name(col));
374 std::ostringstream os;
376 // Note that X stores the RGB values in the range 0 - 65535
377 // whilst we require them in the range 0 - 255.
378 os << std::setbase(16) << std::setfill('0')
379 << std::setw(2) << (gclr->get_red() / 256)
380 << std::setw(2) << (gclr->get_green() / 256)
381 << std::setw(2) << (gclr->get_blue() / 256);
387 void lyx_gui::update_color(LColor::color /*col*/)
393 void lyx_gui::update_fonts()
399 bool lyx_gui::font_available(LyXFont const & font)
401 return fontLoader.available(font);
408 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
415 std::map<int, SigC::Connection> gReadCallbackMap;
420 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
422 gReadCallbackMap[fd] = Glib::signal_io().connect(
423 SigC::bind(SigC::slot(readCallback), comm),
429 void lyx_gui::remove_read_callback(int fd)
431 gReadCallbackMap[fd].disconnect();
432 gReadCallbackMap.erase(fd);
436 string const lyx_gui::roman_font_name()
442 string const lyx_gui::sans_font_name()
448 string const lyx_gui::typewriter_font_name()
454 void lyx_gui::sync_events()