2 * \file gnome/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"
19 #include "support/path_defines.h"
28 // FIXME: move this stuff out again
29 #include "bufferlist.h"
31 #include "lyxserver.h"
32 #include "BufferView.h"
33 #include "XFormsView.h"
35 #include FORMS_H_LOCATION
36 #include "ColorHandler.h"
37 #include "xforms_helpers.h"
38 #include "xfont_loader.h"
39 #include "xformsImage.h"
44 #include <boost/bind.hpp>
46 #include "gnome_helpers.h"
47 #include <gtkmm/main.h>
48 #include <libglademm/xml.h>
50 #ifndef CXX_GLOBAL_CSTD
62 extern BufferList bufferlist;
64 // FIXME: wrong place !
65 LyXServer * lyxserver;
70 bool finished = false;
72 /// estimate DPI from X server
75 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
76 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
77 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
81 /// set default GUI configuration
85 cntl.buttonFontSize = FL_NORMAL_SIZE;
86 cntl.browserFontSize = FL_NORMAL_SIZE;
87 cntl.labelFontSize = FL_NORMAL_SIZE;
88 cntl.choiceFontSize = FL_NORMAL_SIZE;
89 cntl.inputFontSize = FL_NORMAL_SIZE;
90 cntl.menuFontSize = FL_NORMAL_SIZE;
91 cntl.borderWidth = -1;
92 cntl.vclass = FL_DefaultVisual;
93 fl_set_defaults(FL_PDVisual
95 | FL_PDBrowserFontSize
100 | 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 !" << endl;
110 lyxerr << "If you're using xforms 1.0 or greater, "
111 << " please report this to lyx-devel@lists.lyx.org" << endl;
116 LyX::emergencyCleanup();
118 // Get the reason for the crash.
120 XGetErrorText(display, xeev->error_code, etxt, 512);
121 lyxerr << etxt << " id: " << xeev->resourceid << endl;
122 // By doing an abort we get a nice backtrace. (hopefully)
129 /// read in geometry specification
135 void lyx_gui::parse_init(int & argc, char * argv[])
139 FL_CMD_OPT cmdopt[] = {
140 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
143 FL_resource res[] = {
144 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
147 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.
155 fl_get_app_resources(res, num_res);
157 Display * display = fl_get_display();
160 lyxerr << "LyX: unable to access X display, exiting" << endl;
161 os::warn("Unable to access X display, exiting");
165 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
167 XSetErrorHandler(LyX_XErrHandler);
169 lyxColorHandler.reset(new LyXColorHandler());
171 using namespace lyx::graphics;
173 // connect the image loader based on the xforms library
174 Image::newImage = boost::bind(&xformsImage::newImage);
175 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
177 // must do this /before/ lyxrc gets read
178 lyxrc.dpi = getDPI();
180 new Gtk::Main (argc, argv);
184 void lyx_gui::parse_lyxrc()
186 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
188 if (lyxrc.popup_font_encoding.empty())
189 lyxrc.popup_font_encoding = lyxrc.font_norm;
190 // Set the font name for popups and menus
191 string boldfontname = lyxrc.popup_bold_font
192 + "-*-*-*-?-*-*-*-*-"
193 + lyxrc.popup_font_encoding;
194 // "?" means "scale that font"
195 string fontname = lyxrc.popup_normal_font
196 + "-*-*-*-?-*-*-*-*-"
197 + lyxrc.popup_font_encoding;
199 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
200 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
202 lyxerr << "Could not set menu font to "
203 << boldfontname << endl;
206 lyxerr << "Could not set popup font to "
209 if (bold < 0 && normal < 0) {
210 lyxerr << "Using 'helvetica' font for menus" << endl;
211 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
212 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
213 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
214 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
216 if (bold < 0 && normal < 0) {
217 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
218 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
223 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
225 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
227 fl_setpup_fontstyle(FL_NORMAL_STYLE);
228 fl_setpup_fontsize(FL_NORMAL_SIZE);
229 fl_setpup_color(FL_MCOL, FL_BLACK);
230 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
231 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
234 void lyx_gui::start(string const & batch, vector<string> const & files)
239 unsigned int width = 690;
240 unsigned int height = 510;
242 int const geometryBitmask =
243 XParseGeometry(geometry,
244 &xpos, &ypos, &width, &height);
246 // if width is not set by geometry, check it against monitor width
247 if (!(geometryBitmask & WidthValue)) {
248 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
249 if (WidthOfScreen(scr) - 8 < int(width))
250 width = WidthOfScreen(scr) - 8;
253 // if height is not set by geometry, check it against monitor height
254 if (!(geometryBitmask & HeightValue)) {
255 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
256 if (HeightOfScreen(scr) - 24 < int(height))
257 height = HeightOfScreen(scr) - 24;
260 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
262 // recalculate xpos if it's not set
264 xpos = (WidthOfScreen(s) - width) / 2;
266 // recalculate ypos if it's not set
268 ypos = (HeightOfScreen(s) - height) / 2;
270 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
271 << '+' << xpos << '+' << ypos << endl;
273 XFormsView view(width, height);
274 view.show(xpos, ypos, "LyX");
279 // FIXME: some code below needs moving
281 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
283 vector<string>::const_iterator cit = files.begin();
284 vector<string>::const_iterator end = files.end();
285 for (; cit != end; ++cit) {
286 Buffer * b = bufferlist.loadLyXFile(*cit);
292 // switch to the last buffer successfully loaded
294 view.view()->buffer(last);
297 // handle the batch commands the user asked for
298 if (!batch.empty()) {
299 view.getLyXFunc().dispatch(batch);
302 // enter the event loop
304 while (Gtk::Main::events_pending())
305 Gtk::Main::iteration(false);
306 if (fl_check_forms() == FL_EVENT) {
310 << "Received unhandled X11 event" << endl
311 << "Type: " << ev.xany.type
312 << " Target: 0x" << hex << ev.xany.window
318 // FIXME: breaks emergencyCleanup
328 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
330 // Nothing interesting to do here
334 string const lyx_gui::hexname(LColor::color col)
336 string const name = lcolor.getX11Name(col);
337 Display * const display = fl_get_display();
338 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
341 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
342 lyxerr << "X can't find color \""
343 << lcolor.getLyXName(col)
350 // Note that X stores the RGB values in the range 0 - 65535
351 // whilst we require them in the range 0 - 255.
352 os << setbase(16) << setfill('0')
353 << setw(2) << (xcol.red / 256)
354 << setw(2) << (xcol.green / 256)
355 << setw(2) << (xcol.blue / 256);
357 return STRCONV(os.str());
361 void lyx_gui::update_color(LColor::color col)
363 lyxColorHandler->updateColor(col);
367 void lyx_gui::update_fonts()
373 bool lyx_gui::font_available(LyXFont const & font)
375 return fontloader.available(font);
381 void C_read_callback(int, void * data)
383 LyXComm * comm = static_cast<LyXComm *>(data);
389 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
391 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
395 void lyx_gui::remove_read_callback(int fd)
397 fl_remove_io_callback(fd, FL_READ, C_read_callback);