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"
30 #include "graphics/LoaderQueue.h"
32 // FIXME: move this stuff out again
33 #include "bufferlist.h"
34 #include "buffer_funcs.h"
36 #include "lyxserver.h"
37 #include "BufferView.h"
42 #include "xftFontLoader.h"
43 #include "GWorkArea.h"
45 #include "support/std_sstream.h"
48 #include <boost/bind.hpp>
51 #include "lyx_forms.h"
52 #include "xformsImage.h"
53 #include "xforms_helpers.h"
55 using std::ostringstream;
58 extern BufferList bufferlist;
60 // FIXME: wrong place !
61 LyXServer * lyxserver;
63 bool lyx_gui::use_gui = true;
68 bool finished = false;
71 /// estimate DPI from X server
74 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
75 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
76 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
80 /// set default GUI configuration
84 cntl.buttonFontSize = FL_NORMAL_SIZE;
85 cntl.browserFontSize = FL_NORMAL_SIZE;
86 cntl.labelFontSize = FL_NORMAL_SIZE;
87 cntl.choiceFontSize = FL_NORMAL_SIZE;
88 cntl.inputFontSize = FL_NORMAL_SIZE;
89 cntl.menuFontSize = FL_NORMAL_SIZE;
90 cntl.borderWidth = -1;
91 cntl.vclass = FL_DefaultVisual;
92 fl_set_defaults(FL_PDVisual
94 | FL_PDBrowserFontSize
99 | FL_PDBorderWidth, &cntl);
106 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
107 // We don't abort on BadWindow
108 if (xeev->error_code == BadWindow) {
109 lyxerr << "BadWindow received !" << std::endl;
110 lyxerr << "If you're using xforms 1.0 or greater, "
111 << " please report this to lyx-devel@lists.lyx.org"
117 LyX::emergencyCleanup();
119 // Get the reason for the crash.
121 XGetErrorText(display, xeev->error_code, etxt, 512);
122 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
123 // By doing an abort we get a nice backtrace. (hopefully)
124 lyx::support::abort();
130 /// read in geometry specification
136 void parse_init_xforms(int & argc, char * argv[])
140 FL_CMD_OPT cmdopt[] = {
141 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
144 FL_resource res[] = {
145 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
148 const int num_res = sizeof(res)/sizeof(FL_resource);
149 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
151 // It appears that, in xforms >=0.89.5, fl_initialize()
152 // calls setlocale() and ruins our LC_NUMERIC setting.
154 fl_get_app_resources(res, num_res);
156 Display * display = fl_get_display();
159 lyxerr << "LyX: unable to access X display, exiting"
161 lyx::support::os::warn("Unable to access X display, exiting");
165 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
167 XSetErrorHandler(LyX_XErrHandler);
169 using namespace lyx::graphics;
171 // connect the image loader based on the xforms library
172 Image::newImage = boost::bind(&xformsImage::newImage);
173 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
177 void lyx_gui::parse_init(int & argc, char * argv[])
179 new Gtk::Main(argc, argv);
181 parse_init_xforms(argc, argv);
185 // must do this /before/ lyxrc gets read
186 lyxrc.dpi = getDPI();
190 void parse_lyxrc_xforms()
192 XformsColor::read(lyx::support::AddName(
193 lyx::support::user_lyxdir(), "preferences.xform"));
195 if (lyxrc.popup_font_encoding.empty())
196 lyxrc.popup_font_encoding = lyxrc.font_norm;
197 // Set the font name for popups and menus
198 string boldfontname = lyxrc.popup_bold_font
199 + "-*-*-*-?-*-*-*-*-"
200 + lyxrc.popup_font_encoding;
201 // "?" means "scale that font"
202 string fontname = lyxrc.popup_normal_font
203 + "-*-*-*-?-*-*-*-*-"
204 + lyxrc.popup_font_encoding;
206 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
207 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
209 lyxerr << "Could not set menu font to "
210 << boldfontname << std::endl;
213 lyxerr << "Could not set popup font to "
214 << fontname << std::endl;
216 if (bold < 0 && normal < 0) {
217 lyxerr << "Using 'helvetica' font for menus" << std::endl;
218 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
219 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
220 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
221 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
223 if (bold < 0 && normal < 0) {
224 lyxerr << "Could not find helvetica font. Using 'fixed'."
226 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
231 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
233 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
235 fl_setpup_fontstyle(FL_NORMAL_STYLE);
236 fl_setpup_fontsize(FL_NORMAL_SIZE);
237 fl_setpup_color(FL_MCOL, FL_BLACK);
238 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
239 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
243 void lyx_gui::parse_lyxrc()
245 parse_lyxrc_xforms();
254 unsigned int width = 690;
255 unsigned int height = 510;
257 int const geometryBitmask =
258 XParseGeometry(geometry,
259 &xpos, &ypos, &width, &height);
261 // if width is not set by geometry, check it against monitor width
262 if (!(geometryBitmask & WidthValue)) {
263 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
264 if (WidthOfScreen(scr) - 8 < int(width))
265 width = WidthOfScreen(scr) - 8;
268 // if height is not set by geometry, check it against monitor height
269 if (!(geometryBitmask & HeightValue)) {
270 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
271 if (HeightOfScreen(scr) - 24 < int(height))
272 height = HeightOfScreen(scr) - 24;
275 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
277 // recalculate xpos if it's not set
279 xpos = (WidthOfScreen(s) - width) / 2;
281 // recalculate ypos if it's not set
283 ypos = (HeightOfScreen(s) - height) / 2;
285 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
286 << '+' << xpos << '+' << ypos << std::endl;
288 // XFormsView view(width, height);
289 // view.show(xpos, ypos, "LyX");
294 static void events_xforms()
296 if (fl_check_forms() == FL_EVENT) {
300 << "Received unhandled X11 event" << std::endl
301 << "Type: " << ev.xany.type
302 << " Target: 0x" << std::hex << ev.xany.window
303 << std::dec << std::endl;
308 void lyx_gui::start(string const & batch, std::vector<string> const & files)
312 XSynchronize(getDisplay(), true);
319 // FIXME: some code below needs moving
321 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
323 std::vector<string>::const_iterator cit = files.begin();
324 std::vector<string>::const_iterator end = files.end();
325 for (; cit != end; ++cit) {
326 Buffer * b = bufferlist.newBuffer(*cit);
327 if (loadLyXFile(b, *cit))
331 // switch to the last buffer successfully loaded
333 view.view()->buffer(last);
336 // handle the batch commands the user asked for
337 if (!batch.empty()) {
338 view.getLyXFunc().dispatch(batch);
341 // enter the event loop
343 while (Gtk::Main::events_pending())
344 Gtk::Main::iteration(false);
348 // FIXME: breaks emergencyCleanup
359 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
361 // Nothing interesting to do here
366 string const lyx_gui::hexname(EnumLColor col)
369 Gdk::Color * gclr = colorCache.getColor(col);
372 gclr->parse(lcolor.getX11Name(col));
375 std::ostringstream os;
377 // Note that X stores the RGB values in the range 0 - 65535
378 // whilst we require them in the range 0 - 255.
379 os << std::setbase(16) << std::setfill('0')
380 << std::setw(2) << (gclr->get_red() / 256)
381 << std::setw(2) << (gclr->get_green() / 256)
382 << std::setw(2) << (gclr->get_blue() / 256);
388 void lyx_gui::update_color(EnumLColor /*col*/)
394 void lyx_gui::update_fonts()
400 bool lyx_gui::font_available(LyXFont const & font)
402 return fontLoader.available(font);
409 bool readCallback(Glib::IOCondition /*condition*/, LyXComm * comm)
416 std::map<int, SigC::Connection> gReadCallbackMap;
421 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
423 gReadCallbackMap[fd] = Glib::signal_io().connect(
424 SigC::bind(SigC::slot(readCallback), comm),
430 void lyx_gui::remove_read_callback(int fd)
432 gReadCallbackMap[fd].disconnect();
433 gReadCallbackMap.erase(fd);
437 string const lyx_gui::roman_font_name()
443 string const lyx_gui::sans_font_name()
449 string const lyx_gui::typewriter_font_name()
455 void lyx_gui::sync_events()