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 // FIXME: move this stuff out again
35 #include "bufferlist.h"
36 #include "buffer_funcs.h"
38 #include "lyxserver.h"
39 #include "BufferView.h"
44 #include "xftFontLoader.h"
45 #include "GWorkArea.h"
47 #include "support/std_sstream.h"
50 #include <boost/bind.hpp>
53 #include "lyx_forms.h"
54 #include "xformsImage.h"
55 #include "xforms_helpers.h"
57 using std::ostringstream;
61 extern BufferList bufferlist;
63 // FIXME: wrong place !
64 LyXServer * lyxserver;
66 bool lyx_gui::use_gui = true;
71 bool finished = false;
74 /// estimate DPI from X server
77 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
78 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
79 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
83 /// set default GUI configuration
87 cntl.buttonFontSize = FL_NORMAL_SIZE;
88 cntl.browserFontSize = FL_NORMAL_SIZE;
89 cntl.labelFontSize = FL_NORMAL_SIZE;
90 cntl.choiceFontSize = FL_NORMAL_SIZE;
91 cntl.inputFontSize = FL_NORMAL_SIZE;
92 cntl.menuFontSize = FL_NORMAL_SIZE;
93 cntl.borderWidth = -1;
94 cntl.vclass = FL_DefaultVisual;
95 fl_set_defaults(FL_PDVisual
97 | FL_PDBrowserFontSize
102 | FL_PDBorderWidth, &cntl);
109 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
110 // We don't abort on BadWindow
111 if (xeev->error_code == BadWindow) {
112 lyxerr << "BadWindow received !" << std::endl;
113 lyxerr << "If you're using xforms 1.0 or greater, "
114 << " please report this to lyx-devel@lists.lyx.org"
120 LyX::emergencyCleanup();
122 // Get the reason for the crash.
124 XGetErrorText(display, xeev->error_code, etxt, 512);
125 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
126 // By doing an abort we get a nice backtrace. (hopefully)
127 lyx::support::abort();
133 /// read in geometry specification
139 void parse_init_xforms(int & argc, char * argv[])
143 FL_CMD_OPT cmdopt[] = {
144 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
147 FL_resource res[] = {
148 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
151 const int num_res = sizeof(res)/sizeof(FL_resource);
152 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
154 // It appears that, in xforms >=0.89.5, fl_initialize()
155 // calls setlocale() and ruins our LC_NUMERIC setting.
157 fl_get_app_resources(res, num_res);
159 Display * display = fl_get_display();
162 lyxerr << "LyX: unable to access X display, exiting"
164 lyx::support::os::warn("Unable to access X display, exiting");
168 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
170 XSetErrorHandler(LyX_XErrHandler);
172 using namespace lyx::graphics;
174 // connect the image loader based on the xforms library
175 Image::newImage = boost::bind(&xformsImage::newImage);
176 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
180 void lyx_gui::parse_init(int & argc, char * argv[])
182 new Gtk::Main(argc, argv);
184 parse_init_xforms(argc, argv);
188 // must do this /before/ lyxrc gets read
189 lyxrc.dpi = getDPI();
193 void parse_lyxrc_xforms()
195 XformsColor::read(lyx::support::AddName(
196 lyx::support::user_lyxdir(), "preferences.xform"));
198 if (lyxrc.popup_font_encoding.empty())
199 lyxrc.popup_font_encoding = lyxrc.font_norm;
200 // Set the font name for popups and menus
201 string boldfontname = lyxrc.popup_bold_font
202 + "-*-*-*-?-*-*-*-*-"
203 + lyxrc.popup_font_encoding;
204 // "?" means "scale that font"
205 string fontname = lyxrc.popup_normal_font
206 + "-*-*-*-?-*-*-*-*-"
207 + lyxrc.popup_font_encoding;
209 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
210 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
212 lyxerr << "Could not set menu font to "
213 << boldfontname << std::endl;
216 lyxerr << "Could not set popup font to "
217 << fontname << std::endl;
219 if (bold < 0 && normal < 0) {
220 lyxerr << "Using 'helvetica' font for menus" << std::endl;
221 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
222 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
223 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
224 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
226 if (bold < 0 && normal < 0) {
227 lyxerr << "Could not find helvetica font. Using 'fixed'."
229 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
234 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
236 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
238 fl_setpup_fontstyle(FL_NORMAL_STYLE);
239 fl_setpup_fontsize(FL_NORMAL_SIZE);
240 fl_setpup_color(FL_MCOL, FL_BLACK);
241 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
246 void lyx_gui::parse_lyxrc()
248 parse_lyxrc_xforms();
257 unsigned int width = 690;
258 unsigned int height = 510;
260 int const geometryBitmask =
261 XParseGeometry(geometry,
262 &xpos, &ypos, &width, &height);
264 // if width is not set by geometry, check it against monitor width
265 if (!(geometryBitmask & WidthValue)) {
266 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
267 if (WidthOfScreen(scr) - 8 < int(width))
268 width = WidthOfScreen(scr) - 8;
271 // if height is not set by geometry, check it against monitor height
272 if (!(geometryBitmask & HeightValue)) {
273 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
274 if (HeightOfScreen(scr) - 24 < int(height))
275 height = HeightOfScreen(scr) - 24;
278 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
280 // recalculate xpos if it's not set
282 xpos = (WidthOfScreen(s) - width) / 2;
284 // recalculate ypos if it's not set
286 ypos = (HeightOfScreen(s) - height) / 2;
288 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
289 << '+' << xpos << '+' << ypos << std::endl;
291 // XFormsView view(width, height);
292 // view.show(xpos, ypos, "LyX");
297 static void events_xforms()
299 if (fl_check_forms() == FL_EVENT) {
303 << "Received unhandled X11 event" << std::endl
304 << "Type: " << ev.xany.type
305 << " Target: 0x" << std::hex << ev.xany.window
306 << std::dec << std::endl;
311 void lyx_gui::start(string const & batch, std::vector<string> const & files)
315 XSynchronize(getDisplay(), true);
317 boost::shared_ptr<GView> view_ptr(new GView);
318 LyX::ref().addLyXView(view_ptr);
320 GView & view = *view_ptr.get();
326 // FIXME: some code below needs moving
328 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
330 std::vector<string>::const_iterator cit = files.begin();
331 std::vector<string>::const_iterator end = files.end();
332 for (; cit != end; ++cit) {
333 Buffer * b = bufferlist.newBuffer(*cit);
334 if (loadLyXFile(b, *cit))
338 // switch to the last buffer successfully loaded
340 view.view()->buffer(last);
343 // handle the batch commands the user asked for
344 if (!batch.empty()) {
345 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
348 // enter the event loop
350 while (Gtk::Main::events_pending())
351 Gtk::Main::iteration(false);
355 // FIXME: breaks emergencyCleanup
366 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
368 // Nothing interesting to do here
373 string const lyx_gui::hexname(LColor_color col)
376 Gdk::Color * gclr = colorCache.getColor(col);
379 gclr->parse(lcolor.getX11Name(col));
382 std::ostringstream os;
384 // Note that X stores the RGB values in the range 0 - 65535
385 // whilst we require them in the range 0 - 255.
386 os << std::setbase(16) << std::setfill('0')
387 << std::setw(2) << (gclr->get_red() / 256)
388 << std::setw(2) << (gclr->get_green() / 256)
389 << std::setw(2) << (gclr->get_blue() / 256);
395 void lyx_gui::update_color(LColor_color /*col*/)
401 void lyx_gui::update_fonts()
407 bool lyx_gui::font_available(LyXFont const & font)
409 return fontLoader.available(font);
416 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
423 std::map<int, SigC::Connection> gReadCallbackMap;
428 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
430 gReadCallbackMap[fd] = Glib::signal_io().connect(
431 SigC::bind(SigC::slot(readCallback), comm),
437 void lyx_gui::remove_read_callback(int fd)
439 gReadCallbackMap[fd].disconnect();
440 gReadCallbackMap.erase(fd);
444 void lyx_gui::set_datasocket_callback(LyXDataSocket * /* p */)
448 void lyx_gui::remove_datasocket_callback(LyXDataSocket * /* p */)
452 void lyx_gui::set_serversocket_callback(LyXServerSocket * /* p */)
456 void lyx_gui::remove_serversocket_callback(LyXServerSocket * /* p */)
460 string const lyx_gui::roman_font_name()
466 string const lyx_gui::sans_font_name()
472 string const lyx_gui::typewriter_font_name()
478 void lyx_gui::sync_events()