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"
41 #include <boost/bind.hpp>
47 #include "gnome_helpers.h"
48 #include <gtkmm/main.h>
49 #include <libglademm/xml.h>
51 #ifndef CXX_GLOBAL_CSTD
55 using std::ostringstream;
64 extern BufferList bufferlist;
66 // FIXME: wrong place !
67 LyXServer * lyxserver;
72 bool finished = false;
74 /// estimate DPI from X server
77 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
78 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
79 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
83 /// set default GUI configuration
87 cntl.buttonFontSize = FL_NORMAL_SIZE;
88 cntl.browserFontSize = FL_NORMAL_SIZE;
89 cntl.labelFontSize = FL_NORMAL_SIZE;
90 cntl.choiceFontSize = FL_NORMAL_SIZE;
91 cntl.inputFontSize = FL_NORMAL_SIZE;
92 cntl.menuFontSize = FL_NORMAL_SIZE;
93 cntl.borderWidth = -1;
94 cntl.vclass = FL_DefaultVisual;
95 fl_set_defaults(FL_PDVisual
97 | FL_PDBrowserFontSize
102 | FL_PDBorderWidth, &cntl);
108 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
109 // We don't abort on BadWindow
110 if (xeev->error_code == BadWindow) {
111 lyxerr << "BadWindow received !" << endl;
112 lyxerr << "If you're using xforms 1.0 or greater, "
113 << " please report this to lyx-devel@lists.lyx.org" << endl;
118 LyX::emergencyCleanup();
120 // Get the reason for the crash.
122 XGetErrorText(display, xeev->error_code, etxt, 512);
123 lyxerr << etxt << " id: " << xeev->resourceid << endl;
124 // By doing an abort we get a nice backtrace. (hopefully)
131 /// read in geometry specification
137 void lyx_gui::parse_init(int & argc, char * argv[])
141 FL_CMD_OPT cmdopt[] = {
142 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
145 FL_resource res[] = {
146 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
149 const int num_res = sizeof(res)/sizeof(FL_resource);
151 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
153 // It appears that, in xforms >=0.89.5, fl_initialize()
154 // calls setlocale() and ruins our LC_NUMERIC setting.
157 fl_get_app_resources(res, num_res);
159 Display * display = fl_get_display();
162 lyxerr << "LyX: unable to access X display, exiting" << endl;
163 os::warn("Unable to access X display, exiting");
167 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
169 XSetErrorHandler(LyX_XErrHandler);
171 lyxColorHandler.reset(new LyXColorHandler);
173 using namespace lyx::graphics;
175 // connect the image loader based on the xforms library
176 Image::newImage = boost::bind(&xformsImage::newImage);
177 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
179 // must do this /before/ lyxrc gets read
180 lyxrc.dpi = getDPI();
182 new Gtk::Main (argc, argv);
186 void lyx_gui::parse_lyxrc()
188 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
190 if (lyxrc.popup_font_encoding.empty())
191 lyxrc.popup_font_encoding = lyxrc.font_norm;
192 // Set the font name for popups and menus
193 string boldfontname = lyxrc.popup_bold_font
194 + "-*-*-*-?-*-*-*-*-"
195 + lyxrc.popup_font_encoding;
196 // "?" means "scale that font"
197 string fontname = lyxrc.popup_normal_font
198 + "-*-*-*-?-*-*-*-*-"
199 + lyxrc.popup_font_encoding;
201 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
202 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
204 lyxerr << "Could not set menu font to "
205 << boldfontname << endl;
208 lyxerr << "Could not set popup font to "
211 if (bold < 0 && normal < 0) {
212 lyxerr << "Using 'helvetica' font for menus" << endl;
213 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
214 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
215 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
216 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
218 if (bold < 0 && normal < 0) {
219 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
220 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
225 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
227 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
229 fl_setpup_fontstyle(FL_NORMAL_STYLE);
230 fl_setpup_fontsize(FL_NORMAL_SIZE);
231 fl_setpup_color(FL_MCOL, FL_BLACK);
232 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
233 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
236 void lyx_gui::start(string const & batch, vector<string> const & files)
241 unsigned int width = 690;
242 unsigned int height = 510;
244 int const geometryBitmask =
245 XParseGeometry(geometry,
246 &xpos, &ypos, &width, &height);
248 // if width is not set by geometry, check it against monitor width
249 if (!(geometryBitmask & WidthValue)) {
250 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
251 if (WidthOfScreen(scr) - 8 < int(width))
252 width = WidthOfScreen(scr) - 8;
255 // if height is not set by geometry, check it against monitor height
256 if (!(geometryBitmask & HeightValue)) {
257 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
258 if (HeightOfScreen(scr) - 24 < int(height))
259 height = HeightOfScreen(scr) - 24;
262 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
264 // recalculate xpos if it's not set
266 xpos = (WidthOfScreen(s) - width) / 2;
268 // recalculate ypos if it's not set
270 ypos = (HeightOfScreen(s) - height) / 2;
272 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
273 << '+' << xpos << '+' << ypos << endl;
275 XFormsView view(width, height);
276 view.show(xpos, ypos, "LyX");
281 // FIXME: some code below needs moving
283 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
285 vector<string>::const_iterator cit = files.begin();
286 vector<string>::const_iterator end = files.end();
287 for (; cit != end; ++cit) {
288 Buffer * b = bufferlist.loadLyXFile(*cit);
294 // switch to the last buffer successfully loaded
296 view.view()->buffer(last);
299 // handle the batch commands the user asked for
300 if (!batch.empty()) {
301 view.getLyXFunc().dispatch(batch);
304 // enter the event loop
306 while (Gtk::Main::events_pending())
307 Gtk::Main::iteration(false);
308 if (fl_check_forms() == FL_EVENT) {
312 << "Received unhandled X11 event" << endl
313 << "Type: " << ev.xany.type
314 << " Target: 0x" << hex << ev.xany.window
320 // FIXME: breaks emergencyCleanup
330 FuncStatus lyx_gui::getStatus(FuncRequest const & /*ev*/)
332 // Nothing interesting to do here
336 string const lyx_gui::hexname(LColor::color col)
338 string const name = lcolor.getX11Name(col);
339 Display * const display = fl_get_display();
340 Colormap const cmap = fl_state[fl_get_vclass()].colormap;
343 if (XLookupColor(display, cmap, name.c_str(), &xcol, &ccol) == 0) {
344 lyxerr << "X can't find color \""
345 << lcolor.getLyXName(col)
352 // Note that X stores the RGB values in the range 0 - 65535
353 // whilst we require them in the range 0 - 255.
354 os << setbase(16) << setfill('0')
355 << setw(2) << (xcol.red / 256)
356 << setw(2) << (xcol.green / 256)
357 << setw(2) << (xcol.blue / 256);
359 return STRCONV(os.str());
363 void lyx_gui::update_color(LColor::color col)
365 lyxColorHandler->updateColor(col);
369 void lyx_gui::update_fonts()
375 bool lyx_gui::font_available(LyXFont const & font)
377 return fontloader.available(font);
383 void C_read_callback(int, void * data)
385 LyXComm * comm = static_cast<LyXComm *>(data);
391 void lyx_gui::set_read_callback(int fd, LyXComm * comm)
393 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
397 void lyx_gui::remove_read_callback(int fd)
399 fl_remove_io_callback(fd, FL_READ, C_read_callback);