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.
15 // Too hard to make concept checks work with this file
16 #ifdef _GLIBCPP_CONCEPT_CHECKS
17 #undef _GLIBCPP_CONCEPT_CHECKS
23 #include "funcrequest.h"
27 #include "LyXAction.h"
31 #include "graphics/LoaderQueue.h"
33 #include "io_callback.h"
35 // FIXME: move this stuff out again
36 #include "bufferlist.h"
37 #include "buffer_funcs.h"
39 #include "lyxserver.h"
40 #include "lyxsocket.h"
41 #include "BufferView.h"
46 #include "xftFontLoader.h"
47 #include "GWorkArea.h"
50 #include "lyx_forms.h"
51 #include "xformsImage.h"
52 #include "xforms_helpers.h"
54 #include "support/lyxlib.h"
55 #include "support/os.h"
56 #include "support/filetools.h"
57 #include "support/path_defines.h"
61 #include <boost/bind.hpp>
62 #include <boost/function.hpp>
63 #include <boost/shared_ptr.hpp>
70 namespace os = lyx::support::os;
72 using std::ostringstream;
75 using lyx::frontend::colorCache;
76 using lyx::frontend::GView;
77 using lyx::frontend::XformsColor;
80 extern BufferList bufferlist;
82 // FIXME: wrong place !
83 LyXServer * lyxserver;
84 LyXServerSocket * lyxsocket;
86 bool lyx_gui::use_gui = true;
91 bool finished = false;
94 /// estimate DPI from X server
97 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
98 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
99 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
103 /// set default GUI configuration
107 cntl.buttonFontSize = FL_NORMAL_SIZE;
108 cntl.browserFontSize = FL_NORMAL_SIZE;
109 cntl.labelFontSize = FL_NORMAL_SIZE;
110 cntl.choiceFontSize = FL_NORMAL_SIZE;
111 cntl.inputFontSize = FL_NORMAL_SIZE;
112 cntl.menuFontSize = FL_NORMAL_SIZE;
113 cntl.borderWidth = -1;
114 cntl.vclass = FL_DefaultVisual;
115 fl_set_defaults(FL_PDVisual
116 | FL_PDButtonFontSize
117 | FL_PDBrowserFontSize
119 | FL_PDChoiceFontSize
122 | FL_PDBorderWidth, &cntl);
129 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
130 // We don't abort on BadWindow
131 if (xeev->error_code == BadWindow) {
132 lyxerr << "BadWindow received !" << std::endl;
133 lyxerr << "If you're using xforms 1.0 or greater, "
134 << " please report this to lyx-devel@lists.lyx.org"
140 LyX::cref().emergencyCleanup();
142 // Get the reason for the crash.
144 XGetErrorText(display, xeev->error_code, etxt, 512);
145 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
146 // By doing an abort we get a nice backtrace. (hopefully)
147 lyx::support::abort();
153 /// read in geometry specification
159 void parse_init_xforms(int & argc, char * argv[])
163 FL_CMD_OPT cmdopt[] = {
164 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
167 FL_resource res[] = {
168 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
171 const int num_res = sizeof(res)/sizeof(FL_resource);
172 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
174 // It appears that, in xforms >=0.89.5, fl_initialize()
175 // calls setlocale() and ruins our LC_NUMERIC setting.
177 fl_get_app_resources(res, num_res);
179 Display * display = fl_get_display();
182 lyxerr << "LyX: unable to access X display, exiting"
184 lyx::support::os::warn("Unable to access X display, exiting");
188 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
190 XSetErrorHandler(LyX_XErrHandler);
192 using namespace lyx::graphics;
194 // connect the image loader based on the xforms library
195 Image::newImage = boost::bind(&xformsImage::newImage);
196 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
200 void lyx_gui::parse_init(int & argc, char * argv[])
202 new Gtk::Main(argc, argv);
204 parse_init_xforms(argc, argv);
208 // must do this /before/ lyxrc gets read
209 lyxrc.dpi = getDPI();
213 void parse_lyxrc_xforms()
215 XformsColor::read(lyx::support::AddName(
216 lyx::support::user_lyxdir(), "preferences.xform"));
218 if (lyxrc.popup_font_encoding.empty())
219 lyxrc.popup_font_encoding = lyxrc.font_norm;
220 // Set the font name for popups and menus
221 string boldfontname = lyxrc.popup_bold_font
222 + "-*-*-*-?-*-*-*-*-"
223 + lyxrc.popup_font_encoding;
224 // "?" means "scale that font"
225 string fontname = lyxrc.popup_normal_font
226 + "-*-*-*-?-*-*-*-*-"
227 + lyxrc.popup_font_encoding;
229 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
230 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
232 lyxerr << "Could not set menu font to "
233 << boldfontname << std::endl;
236 lyxerr << "Could not set popup font to "
237 << fontname << std::endl;
239 if (bold < 0 && normal < 0) {
240 lyxerr << "Using 'helvetica' font for menus" << std::endl;
241 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
242 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
243 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
244 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
246 if (bold < 0 && normal < 0) {
247 lyxerr << "Could not find helvetica font. Using 'fixed'."
249 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
254 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
256 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
258 fl_setpup_fontstyle(FL_NORMAL_STYLE);
259 fl_setpup_fontsize(FL_NORMAL_SIZE);
260 fl_setpup_color(FL_MCOL, FL_BLACK);
261 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
262 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
266 void lyx_gui::parse_lyxrc()
268 parse_lyxrc_xforms();
277 unsigned int width = 690;
278 unsigned int height = 510;
280 int const geometryBitmask =
281 XParseGeometry(geometry,
282 &xpos, &ypos, &width, &height);
284 // if width is not set by geometry, check it against monitor width
285 if (!(geometryBitmask & WidthValue)) {
286 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
287 if (WidthOfScreen(scr) - 8 < int(width))
288 width = WidthOfScreen(scr) - 8;
291 // if height is not set by geometry, check it against monitor height
292 if (!(geometryBitmask & HeightValue)) {
293 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
294 if (HeightOfScreen(scr) - 24 < int(height))
295 height = HeightOfScreen(scr) - 24;
298 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
300 // recalculate xpos if it's not set
302 xpos = (WidthOfScreen(s) - width) / 2;
304 // recalculate ypos if it's not set
306 ypos = (HeightOfScreen(s) - height) / 2;
308 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
309 << '+' << xpos << '+' << ypos << std::endl;
311 // XFormsView view(width, height);
312 // view.show(xpos, ypos, "LyX");
317 static void events_xforms()
319 if (fl_check_forms() == FL_EVENT) {
323 << "Received unhandled X11 event" << std::endl
324 << "Type: " << ev.xany.type
325 << " Target: 0x" << std::hex << ev.xany.window
326 << std::dec << std::endl;
331 void lyx_gui::start(string const & batch, std::vector<string> const & files)
335 XSynchronize(getDisplay(), true);
337 boost::shared_ptr<GView> view_ptr(new GView);
338 LyX::ref().addLyXView(view_ptr);
340 GView & view = *view_ptr.get();
344 // FIXME: server code below needs moving
346 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
347 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
348 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
350 for_each(files.begin(), files.end(),
351 bind(&BufferView::loadLyXFile, view.view(), _1, true));
353 // handle the batch commands the user asked for
354 if (!batch.empty()) {
355 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
358 // enter the event loop
360 while (Gtk::Main::events_pending())
361 Gtk::Main::iteration(false);
365 // FIXME: breaks emergencyCleanup
377 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
381 // Add this back if the gtk doc prefs dialog includes preamble - jcs
382 /*case LFUN_DIALOG_SHOW:
383 if (ev.argument == "preamble")
386 case LFUN_TOOLTIPS_TOGGLE:
397 string const lyx_gui::hexname(LColor_color col)
400 Gdk::Color * gclr = colorCache.getColor(col);
403 gclr->parse(lcolor.getX11Name(col));
406 std::ostringstream os;
408 // Note that X stores the RGB values in the range 0 - 65535
409 // whilst we require them in the range 0 - 255.
410 os << std::setbase(16) << std::setfill('0')
411 << std::setw(2) << (gclr->get_red() / 256)
412 << std::setw(2) << (gclr->get_green() / 256)
413 << std::setw(2) << (gclr->get_blue() / 256);
419 void lyx_gui::update_color(LColor_color /*col*/)
425 void lyx_gui::update_fonts()
431 bool lyx_gui::font_available(LyXFont const & font)
433 return fontLoader.available(font);
439 std::map<int, boost::shared_ptr<io_callback> > callbacks;
444 void lyx_gui::register_socket_callback(int fd,
445 boost::function<void()> func)
447 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
451 void lyx_gui::unregister_socket_callback(int fd)
457 string const lyx_gui::roman_font_name()
463 string const lyx_gui::sans_font_name()
469 string const lyx_gui::typewriter_font_name()
475 void lyx_gui::sync_events()