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 <boost/bind.hpp>
51 #include <boost/function.hpp>
52 #include <boost/shared_ptr.hpp>
60 #include "lyx_forms.h"
61 #include "xformsImage.h"
62 #include "xforms_helpers.h"
64 namespace os = lyx::support::os;
66 using std::ostringstream;
69 using lyx::frontend::colorCache;
70 using lyx::frontend::GView;
71 using lyx::frontend::XformsColor;
74 extern BufferList bufferlist;
76 // FIXME: wrong place !
77 LyXServer * lyxserver;
78 LyXServerSocket * lyxsocket;
80 bool lyx_gui::use_gui = true;
85 bool finished = false;
88 /// estimate DPI from X server
91 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
92 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
93 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
97 /// set default GUI configuration
101 cntl.buttonFontSize = FL_NORMAL_SIZE;
102 cntl.browserFontSize = FL_NORMAL_SIZE;
103 cntl.labelFontSize = FL_NORMAL_SIZE;
104 cntl.choiceFontSize = FL_NORMAL_SIZE;
105 cntl.inputFontSize = FL_NORMAL_SIZE;
106 cntl.menuFontSize = FL_NORMAL_SIZE;
107 cntl.borderWidth = -1;
108 cntl.vclass = FL_DefaultVisual;
109 fl_set_defaults(FL_PDVisual
110 | FL_PDButtonFontSize
111 | FL_PDBrowserFontSize
113 | FL_PDChoiceFontSize
116 | FL_PDBorderWidth, &cntl);
123 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
124 // We don't abort on BadWindow
125 if (xeev->error_code == BadWindow) {
126 lyxerr << "BadWindow received !" << std::endl;
127 lyxerr << "If you're using xforms 1.0 or greater, "
128 << " please report this to lyx-devel@lists.lyx.org"
134 LyX::cref().emergencyCleanup();
136 // Get the reason for the crash.
138 XGetErrorText(display, xeev->error_code, etxt, 512);
139 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
140 // By doing an abort we get a nice backtrace. (hopefully)
141 lyx::support::abort();
147 /// read in geometry specification
153 void parse_init_xforms(int & argc, char * argv[])
157 FL_CMD_OPT cmdopt[] = {
158 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
161 FL_resource res[] = {
162 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
165 const int num_res = sizeof(res)/sizeof(FL_resource);
166 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
168 // It appears that, in xforms >=0.89.5, fl_initialize()
169 // calls setlocale() and ruins our LC_NUMERIC setting.
171 fl_get_app_resources(res, num_res);
173 Display * display = fl_get_display();
176 lyxerr << "LyX: unable to access X display, exiting"
178 lyx::support::os::warn("Unable to access X display, exiting");
182 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
184 XSetErrorHandler(LyX_XErrHandler);
186 using namespace lyx::graphics;
188 // connect the image loader based on the xforms library
189 Image::newImage = boost::bind(&xformsImage::newImage);
190 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
194 void lyx_gui::parse_init(int & argc, char * argv[])
196 new Gtk::Main(argc, argv);
198 parse_init_xforms(argc, argv);
202 // must do this /before/ lyxrc gets read
203 lyxrc.dpi = getDPI();
207 void parse_lyxrc_xforms()
209 XformsColor::read(lyx::support::AddName(
210 lyx::support::user_lyxdir(), "preferences.xform"));
212 if (lyxrc.popup_font_encoding.empty())
213 lyxrc.popup_font_encoding = lyxrc.font_norm;
214 // Set the font name for popups and menus
215 string boldfontname = lyxrc.popup_bold_font
216 + "-*-*-*-?-*-*-*-*-"
217 + lyxrc.popup_font_encoding;
218 // "?" means "scale that font"
219 string fontname = lyxrc.popup_normal_font
220 + "-*-*-*-?-*-*-*-*-"
221 + lyxrc.popup_font_encoding;
223 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
224 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
226 lyxerr << "Could not set menu font to "
227 << boldfontname << std::endl;
230 lyxerr << "Could not set popup font to "
231 << fontname << std::endl;
233 if (bold < 0 && normal < 0) {
234 lyxerr << "Using 'helvetica' font for menus" << std::endl;
235 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
236 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
237 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
238 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
240 if (bold < 0 && normal < 0) {
241 lyxerr << "Could not find helvetica font. Using 'fixed'."
243 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
248 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
250 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
252 fl_setpup_fontstyle(FL_NORMAL_STYLE);
253 fl_setpup_fontsize(FL_NORMAL_SIZE);
254 fl_setpup_color(FL_MCOL, FL_BLACK);
255 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
256 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
260 void lyx_gui::parse_lyxrc()
262 parse_lyxrc_xforms();
271 unsigned int width = 690;
272 unsigned int height = 510;
274 int const geometryBitmask =
275 XParseGeometry(geometry,
276 &xpos, &ypos, &width, &height);
278 // if width is not set by geometry, check it against monitor width
279 if (!(geometryBitmask & WidthValue)) {
280 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
281 if (WidthOfScreen(scr) - 8 < int(width))
282 width = WidthOfScreen(scr) - 8;
285 // if height is not set by geometry, check it against monitor height
286 if (!(geometryBitmask & HeightValue)) {
287 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
288 if (HeightOfScreen(scr) - 24 < int(height))
289 height = HeightOfScreen(scr) - 24;
292 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
294 // recalculate xpos if it's not set
296 xpos = (WidthOfScreen(s) - width) / 2;
298 // recalculate ypos if it's not set
300 ypos = (HeightOfScreen(s) - height) / 2;
302 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
303 << '+' << xpos << '+' << ypos << std::endl;
305 // XFormsView view(width, height);
306 // view.show(xpos, ypos, "LyX");
311 static void events_xforms()
313 if (fl_check_forms() == FL_EVENT) {
317 << "Received unhandled X11 event" << std::endl
318 << "Type: " << ev.xany.type
319 << " Target: 0x" << std::hex << ev.xany.window
320 << std::dec << std::endl;
325 void lyx_gui::start(string const & batch, std::vector<string> const & files)
329 XSynchronize(getDisplay(), true);
331 boost::shared_ptr<GView> view_ptr(new GView);
332 LyX::ref().addLyXView(view_ptr);
334 GView & view = *view_ptr.get();
338 // FIXME: some code below needs moving
340 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
341 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
342 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
344 std::vector<string>::const_iterator cit = files.begin();
345 std::vector<string>::const_iterator end = files.end();
346 for (; cit != end; ++cit)
347 view.view()->loadLyXFile(*cit, true);
349 // handle the batch commands the user asked for
350 if (!batch.empty()) {
351 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
354 // enter the event loop
356 while (Gtk::Main::events_pending())
357 Gtk::Main::iteration(false);
361 // FIXME: breaks emergencyCleanup
373 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
375 // Nothing interesting to do here
380 string const lyx_gui::hexname(LColor_color col)
383 Gdk::Color * gclr = colorCache.getColor(col);
386 gclr->parse(lcolor.getX11Name(col));
389 std::ostringstream os;
391 // Note that X stores the RGB values in the range 0 - 65535
392 // whilst we require them in the range 0 - 255.
393 os << std::setbase(16) << std::setfill('0')
394 << std::setw(2) << (gclr->get_red() / 256)
395 << std::setw(2) << (gclr->get_green() / 256)
396 << std::setw(2) << (gclr->get_blue() / 256);
402 void lyx_gui::update_color(LColor_color /*col*/)
408 void lyx_gui::update_fonts()
414 bool lyx_gui::font_available(LyXFont const & font)
416 return fontLoader.available(font);
422 std::map<int, boost::shared_ptr<io_callback> > callbacks;
427 void lyx_gui::register_socket_callback(int fd,
428 boost::function<void()> func)
430 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
434 void lyx_gui::unregister_socket_callback(int fd)
440 string const lyx_gui::roman_font_name()
446 string const lyx_gui::sans_font_name()
452 string const lyx_gui::typewriter_font_name()
458 void lyx_gui::sync_events()