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.
6 * \author Lars Gullik Bjønnes
9 * Full author contact details are available in file CREDITS.
15 #include "ColorHandler.h"
16 #include "xfont_loader.h"
17 #include "xforms_helpers.h"
18 #include "xformsImage.h"
19 #include "XFormsView.h"
21 #include "bufferlist.h"
22 #include "BufferView.h"
29 #include "lyxserver.h"
31 #include "graphics/LoaderQueue.h"
33 #include "support/lyxlib.h"
34 #include "support/os.h"
35 #include "support/filetools.h"
36 #include "support/path_defines.h"
38 #include "lyx_forms.h"
40 #include <boost/bind.hpp>
42 #include "support/std_sstream.h"
46 using lyx::support::AddName;
47 using lyx::support::user_lyxdir;
49 namespace os = lyx::support::os;
51 #ifndef CXX_GLOBAL_CSTD
61 using std::ostringstream;
65 extern BufferList bufferlist;
67 // FIXME: wrong place !
68 LyXServer * lyxserver;
73 bool finished = false;
75 /// estimate DPI from X server
78 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
79 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
80 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
84 /// set default GUI configuration
88 cntl.buttonFontSize = FL_NORMAL_SIZE;
89 cntl.browserFontSize = FL_NORMAL_SIZE;
90 cntl.labelFontSize = FL_NORMAL_SIZE;
91 cntl.choiceFontSize = FL_NORMAL_SIZE;
92 cntl.inputFontSize = FL_NORMAL_SIZE;
93 cntl.menuFontSize = FL_NORMAL_SIZE;
94 cntl.borderWidth = -1;
95 cntl.vclass = FL_DefaultVisual;
96 fl_set_defaults(FL_PDVisual
98 | FL_PDBrowserFontSize
100 | FL_PDChoiceFontSize
103 | FL_PDBorderWidth, &cntl);
109 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
110 // We don't abort on BadWindow
111 if (xeev->error_code == BadWindow) {
112 lyxerr << "BadWindow received !" << endl;
113 lyxerr << "If you're using xforms 1.0 or greater, "
114 << " please report this to lyx-devel@lists.lyx.org" << endl;
119 LyX::emergencyCleanup();
121 // Get the reason for the crash.
123 XGetErrorText(display, xeev->error_code, etxt, 512);
124 lyxerr << etxt << " id: " << xeev->resourceid << endl;
125 // By doing an abort we get a nice backtrace. (hopefully)
126 lyx::support::abort();
132 /// read in geometry specification
143 void parse_init(int & argc, char * argv[])
147 FL_CMD_OPT cmdopt[] = {
148 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
151 FL_resource res[] = {
152 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
155 const int num_res = sizeof(res)/sizeof(FL_resource);
157 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
159 // It appears that, in xforms >=0.89.5, fl_initialize()
160 // calls setlocale() and ruins our LC_NUMERIC setting.
163 fl_get_app_resources(res, num_res);
165 Display * display = fl_get_display();
168 lyxerr << "LyX: unable to access X display, exiting" << endl;
169 os::warn("Unable to access X display, exiting");
173 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
175 XSetErrorHandler(LyX_XErrHandler);
177 lyxColorHandler.reset(new LyXColorHandler);
179 using namespace lyx::graphics;
181 // connect the image loader based on the xforms library
182 Image::newImage = boost::bind(&xformsImage::newImage);
183 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
185 // must do this /before/ lyxrc gets read
186 lyxrc.dpi = getDPI();
188 LoaderQueue::setPriority(10,100);
194 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
196 if (lyxrc.popup_font_encoding.empty())
197 lyxrc.popup_font_encoding = lyxrc.font_norm;
198 // Set the font name for popups and menus
199 string boldfontname = lyxrc.popup_bold_font
200 + "-*-*-*-?-*-*-*-*-"
201 + lyxrc.popup_font_encoding;
202 // "?" means "scale that font"
203 string fontname = lyxrc.popup_normal_font
204 + "-*-*-*-?-*-*-*-*-"
205 + lyxrc.popup_font_encoding;
207 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
208 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
210 lyxerr << "Could not set menu font to "
211 << boldfontname << endl;
214 lyxerr << "Could not set popup font to "
217 if (bold < 0 && normal < 0) {
218 lyxerr << "Using 'helvetica' font for menus" << endl;
219 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
220 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
221 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
222 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
224 if (bold < 0 && normal < 0) {
225 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
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 start(string const & batch, vector<string> const & files)
248 unsigned int width = 690;
249 unsigned int height = 510;
251 int const geometryBitmask =
252 XParseGeometry(geometry,
253 &xpos, &ypos, &width, &height);
255 // if width is not set by geometry, check it against monitor width
256 if (!(geometryBitmask & WidthValue)) {
257 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
258 if (WidthOfScreen(scr) - 8 < int(width))
259 width = WidthOfScreen(scr) - 8;
262 // if height is not set by geometry, check it against monitor height
263 if (!(geometryBitmask & HeightValue)) {
264 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
265 if (HeightOfScreen(scr) - 24 < int(height))
266 height = HeightOfScreen(scr) - 24;
269 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
271 // recalculate xpos if it's not set
273 xpos = (WidthOfScreen(s) - width) / 2;
275 // recalculate ypos if it's not set
277 ypos = (HeightOfScreen(s) - height) / 2;
279 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
280 << '+' << xpos << '+' << ypos << endl;
282 XFormsView view(width, height);
283 view.show(xpos, ypos, "LyX");
286 // FIXME: some code below needs moving
288 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
290 vector<string>::const_iterator cit = files.begin();
291 vector<string>::const_iterator end = files.end();
292 for (; cit != end; ++cit)
293 view.view()->loadLyXFile(*cit, true);
295 // handle the batch commands the user asked for
297 view.getLyXFunc().dispatch(batch);
299 // enter the event loop
301 if (fl_check_forms() == FL_EVENT) {
305 << "Received unhandled X11 event" << endl
306 << "Type: " << ev.xany.type
307 << " Target: 0x" << hex << ev.xany.window
312 // FIXME: breaks emergencyCleanup
329 FuncStatus getStatus(FuncRequest const & /*ev*/)
331 // Nothing interesting to do here
335 string const hexname(LColor_color col)
337 unsigned int r, g, b;
338 bool const success = getRGBColor(col, r, g, b);
340 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
347 os << setbase(16) << setfill('0')
356 void update_color(LColor_color col)
358 lyxColorHandler->updateColor(col);
368 bool font_available(LyXFont const & font)
370 return fontloader.available(font);
376 void C_read_callback(int, void * data)
378 LyXComm * comm = static_cast<LyXComm *>(data);
384 void set_read_callback(int fd, LyXComm * comm)
386 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
390 void remove_read_callback(int fd)
392 fl_remove_io_callback(fd, FL_READ, C_read_callback);
396 string const roman_font_name()
402 string const sans_font_name()
408 string const typewriter_font_name()
413 }; // namespace lyx_gui