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"
24 #include "funcrequest.h"
28 #include "LyXAction.h"
31 #include "lyxserver.h"
33 #include "graphics/LoaderQueue.h"
35 #include "support/lyxlib.h"
36 #include "support/os.h"
37 #include "support/filetools.h"
38 #include "support/path_defines.h"
40 #include "lyx_forms.h"
42 #include <boost/bind.hpp>
44 #include "support/std_sstream.h"
48 using lyx::support::AddName;
49 using lyx::support::user_lyxdir;
51 namespace os = lyx::support::os;
53 #ifndef CXX_GLOBAL_CSTD
63 using std::ostringstream;
68 extern BufferList bufferlist;
70 // FIXME: wrong place !
71 LyXServer * lyxserver;
76 bool finished = false;
78 /// estimate DPI from X server
81 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
82 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
83 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
87 /// set default GUI configuration
91 cntl.buttonFontSize = FL_NORMAL_SIZE;
92 cntl.browserFontSize = FL_NORMAL_SIZE;
93 cntl.labelFontSize = FL_NORMAL_SIZE;
94 cntl.choiceFontSize = FL_NORMAL_SIZE;
95 cntl.inputFontSize = FL_NORMAL_SIZE;
96 cntl.menuFontSize = FL_NORMAL_SIZE;
97 cntl.borderWidth = -1;
98 cntl.vclass = FL_DefaultVisual;
99 fl_set_defaults(FL_PDVisual
100 | FL_PDButtonFontSize
101 | FL_PDBrowserFontSize
103 | FL_PDChoiceFontSize
106 | FL_PDBorderWidth, &cntl);
112 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
113 // We don't abort on BadWindow
114 if (xeev->error_code == BadWindow) {
115 lyxerr << "BadWindow received !" << endl;
116 lyxerr << "If you're using xforms 1.0 or greater, "
117 << " please report this to lyx-devel@lists.lyx.org" << endl;
122 LyX::emergencyCleanup();
124 // Get the reason for the crash.
126 XGetErrorText(display, xeev->error_code, etxt, 512);
127 lyxerr << etxt << " id: " << xeev->resourceid << endl;
128 // By doing an abort we get a nice backtrace. (hopefully)
129 lyx::support::abort();
135 /// read in geometry specification
146 void parse_init(int & argc, char * argv[])
150 FL_CMD_OPT cmdopt[] = {
151 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
154 FL_resource res[] = {
155 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
158 const int num_res = sizeof(res)/sizeof(FL_resource);
160 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
162 // It appears that, in xforms >=0.89.5, fl_initialize()
163 // calls setlocale() and ruins our LC_NUMERIC setting.
166 fl_get_app_resources(res, num_res);
168 Display * display = fl_get_display();
171 lyxerr << "LyX: unable to access X display, exiting" << endl;
172 os::warn("Unable to access X display, exiting");
176 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
178 XSetErrorHandler(LyX_XErrHandler);
180 lyxColorHandler.reset(new LyXColorHandler);
182 using namespace lyx::graphics;
184 // connect the image loader based on the xforms library
185 Image::newImage = boost::bind(&xformsImage::newImage);
186 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
188 // must do this /before/ lyxrc gets read
189 lyxrc.dpi = getDPI();
191 LoaderQueue::setPriority(10,100);
197 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
199 if (lyxrc.popup_font_encoding.empty())
200 lyxrc.popup_font_encoding = lyxrc.font_norm;
201 // Set the font name for popups and menus
202 string boldfontname = lyxrc.popup_bold_font
203 + "-*-*-*-?-*-*-*-*-"
204 + lyxrc.popup_font_encoding;
205 // "?" means "scale that font"
206 string fontname = lyxrc.popup_normal_font
207 + "-*-*-*-?-*-*-*-*-"
208 + lyxrc.popup_font_encoding;
210 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
211 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
213 lyxerr << "Could not set menu font to "
214 << boldfontname << endl;
217 lyxerr << "Could not set popup font to "
220 if (bold < 0 && normal < 0) {
221 lyxerr << "Using 'helvetica' font for menus" << endl;
222 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
223 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
224 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
225 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
227 if (bold < 0 && normal < 0) {
228 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
229 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
234 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
236 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
238 fl_setpup_fontstyle(FL_NORMAL_STYLE);
239 fl_setpup_fontsize(FL_NORMAL_SIZE);
240 fl_setpup_color(FL_MCOL, FL_BLACK);
241 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
246 void start(string const & batch, vector<string> const & files)
251 unsigned int width = 690;
252 unsigned int height = 510;
254 int const geometryBitmask =
255 XParseGeometry(geometry,
256 &xpos, &ypos, &width, &height);
258 // if width is not set by geometry, check it against monitor width
259 if (!(geometryBitmask & WidthValue)) {
260 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
261 if (WidthOfScreen(scr) - 8 < int(width))
262 width = WidthOfScreen(scr) - 8;
265 // if height is not set by geometry, check it against monitor height
266 if (!(geometryBitmask & HeightValue)) {
267 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
268 if (HeightOfScreen(scr) - 24 < int(height))
269 height = HeightOfScreen(scr) - 24;
272 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
274 // recalculate xpos if it's not set
276 xpos = (WidthOfScreen(s) - width) / 2;
278 // recalculate ypos if it's not set
280 ypos = (HeightOfScreen(s) - height) / 2;
282 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
283 << '+' << xpos << '+' << ypos << endl;
285 XFormsView view(width, height);
286 view.show(xpos, ypos, "LyX");
289 // FIXME: some code below needs moving
291 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
293 vector<string>::const_iterator cit = files.begin();
294 vector<string>::const_iterator end = files.end();
295 for (; cit != end; ++cit)
296 view.view()->loadLyXFile(*cit, true);
298 // handle the batch commands the user asked for
300 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
302 // enter the event loop
304 if (fl_check_forms() == FL_EVENT) {
308 << "Received unhandled X11 event" << endl
309 << "Type: " << ev.xany.type
310 << " Target: 0x" << hex << ev.xany.window
315 // FIXME: breaks emergencyCleanup
332 FuncStatus getStatus(FuncRequest const & /*ev*/)
334 // Nothing interesting to do here
338 string const hexname(LColor_color col)
340 unsigned int r, g, b;
341 bool const success = getRGBColor(col, r, g, b);
343 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
350 os << setbase(16) << setfill('0')
359 void update_color(LColor_color col)
361 lyxColorHandler->updateColor(col);
371 bool font_available(LyXFont const & font)
373 return fontloader.available(font);
379 void C_read_callback(int, void * data)
381 LyXComm * comm = static_cast<LyXComm *>(data);
387 void set_read_callback(int fd, LyXComm * comm)
389 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
393 void remove_read_callback(int fd)
395 fl_remove_io_callback(fd, FL_READ, C_read_callback);
399 string const roman_font_name()
405 string const sans_font_name()
411 string const typewriter_font_name()
416 }; // namespace lyx_gui