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 "funcrequest.h"
22 #include "LyXAction.h"
26 #include "graphics/LoaderQueue.h"
28 #include "io_callback.h"
30 // FIXME: move this stuff out again
31 #include "bufferlist.h"
32 #include "buffer_funcs.h"
34 #include "lyxserver.h"
35 #include "lyxsocket.h"
36 #include "BufferView.h"
41 #include "xftFontLoader.h"
42 #include "GWorkArea.h"
45 #include "lyx_forms.h"
46 #include "xformsImage.h"
47 #include "xforms_helpers.h"
49 #include "support/lyxlib.h"
50 #include "support/os.h"
51 #include "support/filetools.h"
52 #include "support/path_defines.h"
56 #include <boost/bind.hpp>
57 #include <boost/function.hpp>
58 #include <boost/shared_ptr.hpp>
65 namespace os = lyx::support::os;
67 using std::ostringstream;
70 using lyx::frontend::colorCache;
71 using lyx::frontend::GView;
72 using lyx::frontend::XformsColor;
75 extern BufferList bufferlist;
77 // FIXME: wrong place !
78 LyXServer * lyxserver;
79 LyXServerSocket * lyxsocket;
81 bool lyx_gui::use_gui = true;
86 bool finished = false;
89 /// estimate DPI from X server
92 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
93 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
94 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
98 /// set default GUI configuration
102 cntl.buttonFontSize = FL_NORMAL_SIZE;
103 cntl.browserFontSize = FL_NORMAL_SIZE;
104 cntl.labelFontSize = FL_NORMAL_SIZE;
105 cntl.choiceFontSize = FL_NORMAL_SIZE;
106 cntl.inputFontSize = FL_NORMAL_SIZE;
107 cntl.menuFontSize = FL_NORMAL_SIZE;
108 cntl.borderWidth = -1;
109 cntl.vclass = FL_DefaultVisual;
110 fl_set_defaults(FL_PDVisual
111 | FL_PDButtonFontSize
112 | FL_PDBrowserFontSize
114 | FL_PDChoiceFontSize
117 | FL_PDBorderWidth, &cntl);
124 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
125 // We don't abort on BadWindow
126 if (xeev->error_code == BadWindow) {
127 lyxerr << "BadWindow received !" << std::endl;
128 lyxerr << "If you're using xforms 1.0 or greater, "
129 << " please report this to lyx-devel@lists.lyx.org"
135 LyX::cref().emergencyCleanup();
137 // Get the reason for the crash.
139 XGetErrorText(display, xeev->error_code, etxt, 512);
140 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
141 // By doing an abort we get a nice backtrace. (hopefully)
142 lyx::support::abort();
148 /// read in geometry specification
154 void parse_init_xforms(int & argc, char * argv[])
158 FL_CMD_OPT cmdopt[] = {
159 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
162 FL_resource res[] = {
163 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
166 const int num_res = sizeof(res)/sizeof(FL_resource);
167 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
169 // It appears that, in xforms >=0.89.5, fl_initialize()
170 // calls setlocale() and ruins our LC_NUMERIC setting.
172 fl_get_app_resources(res, num_res);
174 Display * display = fl_get_display();
177 lyxerr << "LyX: unable to access X display, exiting"
179 lyx::support::os::warn("Unable to access X display, exiting");
183 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
185 XSetErrorHandler(LyX_XErrHandler);
187 using namespace lyx::graphics;
189 // connect the image loader based on the xforms library
190 Image::newImage = boost::bind(&xformsImage::newImage);
191 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
195 void lyx_gui::parse_init(int & argc, char * argv[])
197 new Gtk::Main(argc, argv);
199 parse_init_xforms(argc, argv);
203 // must do this /before/ lyxrc gets read
204 lyxrc.dpi = getDPI();
208 void parse_lyxrc_xforms()
210 XformsColor::read(lyx::support::AddName(
211 lyx::support::user_lyxdir(), "preferences.xform"));
213 if (lyxrc.popup_font_encoding.empty())
214 lyxrc.popup_font_encoding = lyxrc.font_norm;
215 // Set the font name for popups and menus
216 string boldfontname = lyxrc.popup_bold_font
217 + "-*-*-*-?-*-*-*-*-"
218 + lyxrc.popup_font_encoding;
219 // "?" means "scale that font"
220 string fontname = lyxrc.popup_normal_font
221 + "-*-*-*-?-*-*-*-*-"
222 + lyxrc.popup_font_encoding;
224 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
225 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
227 lyxerr << "Could not set menu font to "
228 << boldfontname << std::endl;
231 lyxerr << "Could not set popup font to "
232 << fontname << std::endl;
234 if (bold < 0 && normal < 0) {
235 lyxerr << "Using 'helvetica' font for menus" << std::endl;
236 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
237 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
238 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
239 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
241 if (bold < 0 && normal < 0) {
242 lyxerr << "Could not find helvetica font. Using 'fixed'."
244 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
249 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
251 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
253 fl_setpup_fontstyle(FL_NORMAL_STYLE);
254 fl_setpup_fontsize(FL_NORMAL_SIZE);
255 fl_setpup_color(FL_MCOL, FL_BLACK);
256 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
257 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
261 void lyx_gui::parse_lyxrc()
263 parse_lyxrc_xforms();
272 unsigned int width = 690;
273 unsigned int height = 510;
275 int const geometryBitmask =
276 XParseGeometry(geometry,
277 &xpos, &ypos, &width, &height);
279 // if width is not set by geometry, check it against monitor width
280 if (!(geometryBitmask & WidthValue)) {
281 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
282 if (WidthOfScreen(scr) - 8 < int(width))
283 width = WidthOfScreen(scr) - 8;
286 // if height is not set by geometry, check it against monitor height
287 if (!(geometryBitmask & HeightValue)) {
288 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
289 if (HeightOfScreen(scr) - 24 < int(height))
290 height = HeightOfScreen(scr) - 24;
293 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
295 // recalculate xpos if it's not set
297 xpos = (WidthOfScreen(s) - width) / 2;
299 // recalculate ypos if it's not set
301 ypos = (HeightOfScreen(s) - height) / 2;
303 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
304 << '+' << xpos << '+' << ypos << std::endl;
306 // XFormsView view(width, height);
307 // view.show(xpos, ypos, "LyX");
312 static void events_xforms()
314 if (fl_check_forms() == FL_EVENT) {
318 << "Received unhandled X11 event" << std::endl
319 << "Type: " << ev.xany.type
320 << " Target: 0x" << std::hex << ev.xany.window
321 << std::dec << std::endl;
326 void lyx_gui::start(string const & batch, std::vector<string> const & files)
330 XSynchronize(getDisplay(), true);
332 boost::shared_ptr<GView> view_ptr(new GView);
333 LyX::ref().addLyXView(view_ptr);
335 GView & view = *view_ptr.get();
339 // FIXME: server code below needs moving
341 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
342 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
343 os::slashify_path(os::getTmpDir() + "/lyxsocket"));
345 for_each(files.begin(), files.end(),
346 bind(&BufferView::loadLyXFile, view.view(), _1, true));
348 // handle the batch commands the user asked for
349 if (!batch.empty()) {
350 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
353 // enter the event loop
355 while (Gtk::Main::events_pending())
356 Gtk::Main::iteration(false);
360 // FIXME: breaks emergencyCleanup
372 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
374 // Nothing interesting to do here
379 string const lyx_gui::hexname(LColor_color col)
382 Gdk::Color * gclr = colorCache.getColor(col);
385 gclr->parse(lcolor.getX11Name(col));
388 std::ostringstream os;
390 // Note that X stores the RGB values in the range 0 - 65535
391 // whilst we require them in the range 0 - 255.
392 os << std::setbase(16) << std::setfill('0')
393 << std::setw(2) << (gclr->get_red() / 256)
394 << std::setw(2) << (gclr->get_green() / 256)
395 << std::setw(2) << (gclr->get_blue() / 256);
401 void lyx_gui::update_color(LColor_color /*col*/)
407 void lyx_gui::update_fonts()
413 bool lyx_gui::font_available(LyXFont const & font)
415 return fontLoader.available(font);
421 std::map<int, boost::shared_ptr<io_callback> > callbacks;
426 void lyx_gui::register_socket_callback(int fd,
427 boost::function<void()> func)
429 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
433 void lyx_gui::unregister_socket_callback(int fd)
439 string const lyx_gui::roman_font_name()
445 string const lyx_gui::sans_font_name()
451 string const lyx_gui::typewriter_font_name()
457 void lyx_gui::sync_events()