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"
47 #include <boost/bind.hpp>
50 #include "lyx_forms.h"
51 #include "xformsImage.h"
52 #include "xforms_helpers.h"
54 extern BufferList bufferlist;
56 // FIXME: wrong place !
57 LyXServer * lyxserver;
59 bool lyx_gui::use_gui = true;
64 bool finished = false;
67 /// estimate DPI from X server
70 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
71 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
72 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
76 /// set default GUI configuration
80 cntl.buttonFontSize = FL_NORMAL_SIZE;
81 cntl.browserFontSize = FL_NORMAL_SIZE;
82 cntl.labelFontSize = FL_NORMAL_SIZE;
83 cntl.choiceFontSize = FL_NORMAL_SIZE;
84 cntl.inputFontSize = FL_NORMAL_SIZE;
85 cntl.menuFontSize = FL_NORMAL_SIZE;
86 cntl.borderWidth = -1;
87 cntl.vclass = FL_DefaultVisual;
88 fl_set_defaults(FL_PDVisual
90 | FL_PDBrowserFontSize
95 | FL_PDBorderWidth, &cntl);
102 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
103 // We don't abort on BadWindow
104 if (xeev->error_code == BadWindow) {
105 lyxerr << "BadWindow received !" << std::endl;
106 lyxerr << "If you're using xforms 1.0 or greater, "
107 << " please report this to lyx-devel@lists.lyx.org"
113 LyX::emergencyCleanup();
115 // Get the reason for the crash.
117 XGetErrorText(display, xeev->error_code, etxt, 512);
118 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
119 // By doing an abort we get a nice backtrace. (hopefully)
120 lyx::support::abort();
126 /// read in geometry specification
132 void parse_init_xforms(int & argc, char * argv[])
136 FL_CMD_OPT cmdopt[] = {
137 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
140 FL_resource res[] = {
141 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
144 const int num_res = sizeof(res)/sizeof(FL_resource);
145 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
147 // It appears that, in xforms >=0.89.5, fl_initialize()
148 // calls setlocale() and ruins our LC_NUMERIC setting.
150 fl_get_app_resources(res, num_res);
152 Display * display = fl_get_display();
155 lyxerr << "LyX: unable to access X display, exiting"
157 lyx::support::os::warn("Unable to access X display, exiting");
161 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
163 XSetErrorHandler(LyX_XErrHandler);
165 using namespace lyx::graphics;
167 // connect the image loader based on the xforms library
168 Image::newImage = boost::bind(&xformsImage::newImage);
169 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
173 void lyx_gui::parse_init(int & argc, char * argv[])
175 new Gtk::Main(argc, argv);
177 parse_init_xforms(argc, argv);
181 // must do this /before/ lyxrc gets read
182 lyxrc.dpi = getDPI();
186 void parse_lyxrc_xforms()
188 XformsColor::read(lyx::support::AddName(
189 lyx::support::user_lyxdir(), "preferences.xform"));
191 if (lyxrc.popup_font_encoding.empty())
192 lyxrc.popup_font_encoding = lyxrc.font_norm;
193 // Set the font name for popups and menus
194 string boldfontname = lyxrc.popup_bold_font
195 + "-*-*-*-?-*-*-*-*-"
196 + lyxrc.popup_font_encoding;
197 // "?" means "scale that font"
198 string fontname = lyxrc.popup_normal_font
199 + "-*-*-*-?-*-*-*-*-"
200 + lyxrc.popup_font_encoding;
202 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
203 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
205 lyxerr << "Could not set menu font to "
206 << boldfontname << std::endl;
209 lyxerr << "Could not set popup font to "
210 << fontname << std::endl;
212 if (bold < 0 && normal < 0) {
213 lyxerr << "Using 'helvetica' font for menus" << std::endl;
214 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
215 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
216 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
217 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
219 if (bold < 0 && normal < 0) {
220 lyxerr << "Could not find helvetica font. Using 'fixed'."
222 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
227 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
229 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
231 fl_setpup_fontstyle(FL_NORMAL_STYLE);
232 fl_setpup_fontsize(FL_NORMAL_SIZE);
233 fl_setpup_color(FL_MCOL, FL_BLACK);
234 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
235 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
239 void lyx_gui::parse_lyxrc()
241 parse_lyxrc_xforms();
250 unsigned int width = 690;
251 unsigned int height = 510;
253 int const geometryBitmask =
254 XParseGeometry(geometry,
255 &xpos, &ypos, &width, &height);
257 // if width is not set by geometry, check it against monitor width
258 if (!(geometryBitmask & WidthValue)) {
259 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
260 if (WidthOfScreen(scr) - 8 < int(width))
261 width = WidthOfScreen(scr) - 8;
264 // if height is not set by geometry, check it against monitor height
265 if (!(geometryBitmask & HeightValue)) {
266 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
267 if (HeightOfScreen(scr) - 24 < int(height))
268 height = HeightOfScreen(scr) - 24;
271 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
273 // recalculate xpos if it's not set
275 xpos = (WidthOfScreen(s) - width) / 2;
277 // recalculate ypos if it's not set
279 ypos = (HeightOfScreen(s) - height) / 2;
281 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
282 << '+' << xpos << '+' << ypos << std::endl;
284 // XFormsView view(width, height);
285 // view.show(xpos, ypos, "LyX");
290 static void events_xforms()
292 if (fl_check_forms() == FL_EVENT) {
296 << "Received unhandled X11 event" << std::endl
297 << "Type: " << ev.xany.type
298 << " Target: 0x" << std::hex << ev.xany.window
299 << std::dec << std::endl;
304 void lyx_gui::start(string const & batch, std::vector<string> const & files)
308 XSynchronize(getDisplay(), true);
315 // FIXME: some code below needs moving
317 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
319 std::vector<string>::const_iterator cit = files.begin();
320 std::vector<string>::const_iterator end = files.end();
321 for (; cit != end; ++cit) {
322 Buffer * b = bufferlist.newBuffer(*cit);
323 if (loadLyXFile(b, *cit))
327 // switch to the last buffer successfully loaded
329 view.view()->buffer(last);
332 // handle the batch commands the user asked for
333 if (!batch.empty()) {
334 view.getLyXFunc().dispatch(batch);
337 // enter the event loop
339 while (Gtk::Main::events_pending())
340 Gtk::Main::iteration(false);
344 // FIXME: breaks emergencyCleanup
355 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
357 // Nothing interesting to do here
362 string const lyx_gui::hexname(LColor::color col)
365 Gdk::Color * gclr = colorCache.getColor(col);
368 gclr->parse(lcolor.getX11Name(col));
371 std::ostringstream os;
373 // Note that X stores the RGB values in the range 0 - 65535
374 // whilst we require them in the range 0 - 255.
375 os << std::setbase(16) << std::setfill('0')
376 << std::setw(2) << (gclr->get_red() / 256)
377 << std::setw(2) << (gclr->get_green() / 256)
378 << std::setw(2) << (gclr->get_blue() / 256);
384 void lyx_gui::update_color(LColor::color /*col*/)
390 void lyx_gui::update_fonts()
396 bool lyx_gui::font_available(LyXFont const & font)
398 return fontLoader.available(font);
405 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
412 std::map<int, SigC::Connection> gReadCallbackMap;
417 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
419 gReadCallbackMap[fd] = Glib::signal_io().connect(
420 SigC::bind(SigC::slot(readCallback), comm),
426 void lyx_gui::remove_read_callback(int fd)
428 gReadCallbackMap[fd].disconnect();
429 gReadCallbackMap.erase(fd);
433 string const lyx_gui::roman_font_name()
439 string const lyx_gui::sans_font_name()
445 string const lyx_gui::typewriter_font_name()
451 void lyx_gui::sync_events()