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::os;
48 using lyx::support::user_lyxdir;
50 #ifndef CXX_GLOBAL_CSTD
60 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)
125 lyx::support::abort();
131 /// read in geometry specification
142 void parse_init(int & argc, char * argv[])
146 FL_CMD_OPT cmdopt[] = {
147 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
150 FL_resource res[] = {
151 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
154 const int num_res = sizeof(res)/sizeof(FL_resource);
156 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
158 // It appears that, in xforms >=0.89.5, fl_initialize()
159 // calls setlocale() and ruins our LC_NUMERIC setting.
162 fl_get_app_resources(res, num_res);
164 Display * display = fl_get_display();
167 lyxerr << "LyX: unable to access X display, exiting" << endl;
168 os::warn("Unable to access X display, exiting");
172 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
174 XSetErrorHandler(LyX_XErrHandler);
176 lyxColorHandler.reset(new LyXColorHandler());
178 using namespace lyx::graphics;
180 // connect the image loader based on the xforms library
181 Image::newImage = boost::bind(&xformsImage::newImage);
182 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
184 // must do this /before/ lyxrc gets read
185 lyxrc.dpi = getDPI();
187 LoaderQueue::setPriority(10,100);
193 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
195 if (lyxrc.popup_font_encoding.empty())
196 lyxrc.popup_font_encoding = lyxrc.font_norm;
197 // Set the font name for popups and menus
198 string boldfontname = lyxrc.popup_bold_font
199 + "-*-*-*-?-*-*-*-*-"
200 + lyxrc.popup_font_encoding;
201 // "?" means "scale that font"
202 string fontname = lyxrc.popup_normal_font
203 + "-*-*-*-?-*-*-*-*-"
204 + lyxrc.popup_font_encoding;
206 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
207 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
209 lyxerr << "Could not set menu font to "
210 << boldfontname << endl;
213 lyxerr << "Could not set popup font to "
216 if (bold < 0 && normal < 0) {
217 lyxerr << "Using 'helvetica' font for menus" << endl;
218 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
219 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
220 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
221 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
223 if (bold < 0 && normal < 0) {
224 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
225 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
230 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
232 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
234 fl_setpup_fontstyle(FL_NORMAL_STYLE);
235 fl_setpup_fontsize(FL_NORMAL_SIZE);
236 fl_setpup_color(FL_MCOL, FL_BLACK);
237 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
238 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
242 void start(string const & batch, vector<string> const & files)
247 unsigned int width = 690;
248 unsigned int height = 510;
250 int const geometryBitmask =
251 XParseGeometry(geometry,
252 &xpos, &ypos, &width, &height);
254 // if width is not set by geometry, check it against monitor width
255 if (!(geometryBitmask & WidthValue)) {
256 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
257 if (WidthOfScreen(scr) - 8 < int(width))
258 width = WidthOfScreen(scr) - 8;
261 // if height is not set by geometry, check it against monitor height
262 if (!(geometryBitmask & HeightValue)) {
263 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
264 if (HeightOfScreen(scr) - 24 < int(height))
265 height = HeightOfScreen(scr) - 24;
268 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
270 // recalculate xpos if it's not set
272 xpos = (WidthOfScreen(s) - width) / 2;
274 // recalculate ypos if it's not set
276 ypos = (HeightOfScreen(s) - height) / 2;
278 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
279 << '+' << xpos << '+' << ypos << endl;
281 XFormsView view(width, height);
282 view.show(xpos, ypos, "LyX");
285 // FIXME: some code below needs moving
287 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
289 vector<string>::const_iterator cit = files.begin();
290 vector<string>::const_iterator end = files.end();
291 for (; cit != end; ++cit)
292 view.view()->loadLyXFile(*cit, true);
294 // handle the batch commands the user asked for
296 view.getLyXFunc().dispatch(batch);
298 // enter the event loop
300 if (fl_check_forms() == FL_EVENT) {
304 << "Received unhandled X11 event" << endl
305 << "Type: " << ev.xany.type
306 << " Target: 0x" << hex << ev.xany.window
311 // FIXME: breaks emergencyCleanup
328 FuncStatus getStatus(FuncRequest const & /*ev*/)
330 // Nothing interesting to do here
334 string const hexname(EnumLColor col)
336 unsigned int r, g, b;
337 bool const success = getRGBColor(col, r, g, b);
339 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
346 os << setbase(16) << setfill('0')
355 void update_color(EnumLColor col)
357 lyxColorHandler->updateColor(col);
367 bool font_available(LyXFont const & font)
369 return fontloader.available(font);
375 void C_read_callback(int, void * data)
377 LyXComm * comm = static_cast<LyXComm *>(data);
383 void set_read_callback(int fd, LyXComm * comm)
385 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
389 void remove_read_callback(int fd)
391 fl_remove_io_callback(fd, FL_READ, C_read_callback);
395 string const roman_font_name()
401 string const sans_font_name()
407 string const typewriter_font_name()
412 }; // namespace lyx_gui