2 * \file xforms/lyx_gui.C
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
9 * Full author contact details are available in file CREDITS
16 #include "support/lyxlib.h"
17 #include "support/os.h"
18 #include "support/filetools.h"
27 // FIXME: move this stuff out again
28 #include "bufferlist.h"
30 #include "lyxserver.h"
31 #include "BufferView.h"
32 #include "XFormsView.h"
34 #include FORMS_H_LOCATION
35 #include "ColorHandler.h"
36 #include "xforms_helpers.h"
37 #include "xfont_loader.h"
38 #include "xformsImage.h"
43 #include <boost/bind.hpp>
45 #include "gnome_helpers.h"
46 #include <gtkmm/main.h>
47 #include <libglademm/xml.h>
49 #ifndef CXX_GLOBAL_CSTD
61 extern BufferList bufferlist;
63 // FIXME: wrong place !
64 LyXServer * lyxserver;
69 bool finished = false;
71 /// estimate DPI from X server
74 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
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);
105 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
106 // We don't abort on BadWindow
107 if (xeev->error_code == BadWindow) {
108 lyxerr << "BadWindow received !" << endl;
109 lyxerr << "If you're using xforms 1.0 or greater, "
110 << " please report this to lyx-devel@lists.lyx.org" << endl;
115 LyX::emergencyCleanup();
117 // Get the reason for the crash.
119 XGetErrorText(display, xeev->error_code, etxt, 512);
120 lyxerr << etxt << " id: " << xeev->resourceid << endl;
121 // By doing an abort we get a nice backtrace. (hopefully)
128 /// read in geometry specification
134 void lyx_gui::parse_init(int & argc, char * argv[])
138 FL_CMD_OPT cmdopt[] = {
139 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
142 FL_resource res[] = {
143 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
146 const int num_res = sizeof(res)/sizeof(FL_resource);
148 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
150 // It appears that, in xforms >=0.89.5, fl_initialize()
151 // 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" << endl;
160 os::warn("Unable to access X display, exiting");
164 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
166 XSetErrorHandler(LyX_XErrHandler);
168 lyxColorHandler.reset(new LyXColorHandler());
170 using namespace grfx;
172 // connect the image loader based on the xforms library
173 Image::newImage = boost::bind(&xformsImage::newImage);
174 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
176 // must do this /before/ lyxrc gets read
177 lyxrc.dpi = getDPI();
179 new Gtk::Main (argc, argv);
183 void lyx_gui::parse_lyxrc()
185 XformsColor::read(AddName(user_lyxdir, "preferences.xform"));
187 if (lyxrc.popup_font_encoding.empty())
188 lyxrc.popup_font_encoding = lyxrc.font_norm;
189 // Set the font name for popups and menus
190 string boldfontname = lyxrc.popup_bold_font
191 + "-*-*-*-?-*-*-*-*-"
192 + lyxrc.popup_font_encoding;
193 // "?" means "scale that font"
194 string fontname = lyxrc.popup_normal_font
195 + "-*-*-*-?-*-*-*-*-"
196 + lyxrc.popup_font_encoding;
198 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
199 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
201 lyxerr << "Could not set menu font to "
202 << boldfontname << endl;
205 lyxerr << "Could not set popup font to "
208 if (bold < 0 && normal < 0) {
209 lyxerr << "Using 'helvetica' font for menus" << endl;
210 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
211 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
212 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
213 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
215 if (bold < 0 && normal < 0) {
216 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
217 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
222 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
224 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
226 fl_setpup_fontstyle(FL_NORMAL_STYLE);
227 fl_setpup_fontsize(FL_NORMAL_SIZE);
228 fl_setpup_color(FL_MCOL, FL_BLACK);
229 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
230 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 void lyx_gui::start(string const & batch, vector<string> const & files)
238 unsigned int width = 690;
239 unsigned int height = 510;
241 int const geometryBitmask =
242 XParseGeometry(geometry,
243 &xpos, &ypos, &width, &height);
245 // if width is not set by geometry, check it against monitor width
246 if (!(geometryBitmask & WidthValue)) {
247 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
248 if (WidthOfScreen(scr) - 8 < int(width))
249 width = WidthOfScreen(scr) - 8;
252 // if height is not set by geometry, check it against monitor height
253 if (!(geometryBitmask & HeightValue)) {
254 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
255 if (HeightOfScreen(scr) - 24 < int(height))
256 height = HeightOfScreen(scr) - 24;
259 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
261 // recalculate xpos if it's not set
263 xpos = (WidthOfScreen(s) - width) / 2;
265 // recalculate ypos if it's not set
267 ypos = (HeightOfScreen(s) - height) / 2;
269 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
270 << '+' << xpos << '+' << ypos << endl;
272 XFormsView view(width, height);
273 view.show(xpos, ypos, "LyX");
278 // FIXME: some code below needs moving
280 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
282 vector<string>::const_iterator cit = files.begin();
283 vector<string>::const_iterator end = files.end();
284 for (; cit != end; ++cit) {
285 Buffer * b = bufferlist.loadLyXFile(*cit);
291 // switch to the last buffer successfully loaded
293 view.view()->buffer(last);
296 // handle the batch commands the user asked for
297 if (!batch.empty()) {
298 view.getLyXFunc().dispatch(batch);
301 // enter the event loop
303 while (Gtk::Main::events_pending())
304 Gtk::Main::iteration(false);
305 if (fl_check_forms() == FL_EVENT) {
309 << "Received unhandled X11 event" << endl
310 << "Type: " << ev.xany.type
311 << " Target: 0x" << hex << ev.xany.window
317 // FIXME: breaks emergencyCleanup
327 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
329 // Nothing interesting to do here
333 string const lyx_gui::hexname(LColor::color col)
335 string const name = lcolor.getX11Name(col);
336 Display * const display = fl_get_display();
337 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
340 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
341 lyxerr << "X can't find color \""
342 << lcolor.getLyXName(col)
349 // Note that X stores the RGB values in the range 0 - 65535
350 // whilst we require them in the range 0 - 255.
351 os << setbase(16) << setfill('0')
352 << setw(2) << (xcol.red / 256)
353 << setw(2) << (xcol.green / 256)
354 << setw(2) << (xcol.blue / 256);
356 return STRCONV(os.str());
360 void lyx_gui::update_color(LColor::color col)
362 lyxColorHandler->updateColor(col);
366 void lyx_gui::update_fonts()
372 bool lyx_gui::font_available(LyXFont const & font)
374 return fontloader.available(font);
380 void C_read_callback(int, void * data)
382 LyXComm * comm = static_cast<LyXComm *>(data);
388 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
390 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
394 void lyx_gui::remove_read_callback(int fd)
396 fl_remove_io_callback(fd, FL_READ, C_read_callback);