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"
28 #include "lyxserver.h"
30 #include "graphics/LoaderQueue.h"
32 #include "support/lyxlib.h"
33 #include "support/os.h"
34 #include "support/filetools.h"
35 #include "support/path_defines.h"
37 #include "lyx_forms.h"
39 #include <boost/bind.hpp>
45 using namespace lyx::support;
47 #ifndef CXX_GLOBAL_CSTD
60 extern BufferList bufferlist;
62 // FIXME: wrong place !
63 LyXServer * lyxserver;
68 bool finished = false;
70 /// estimate DPI from X server
73 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
74 return ((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
75 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2;
79 /// set default GUI configuration
83 cntl.buttonFontSize = FL_NORMAL_SIZE;
84 cntl.browserFontSize = FL_NORMAL_SIZE;
85 cntl.labelFontSize = FL_NORMAL_SIZE;
86 cntl.choiceFontSize = FL_NORMAL_SIZE;
87 cntl.inputFontSize = FL_NORMAL_SIZE;
88 cntl.menuFontSize = FL_NORMAL_SIZE;
89 cntl.borderWidth = -1;
90 cntl.vclass = FL_DefaultVisual;
91 fl_set_defaults(FL_PDVisual
93 | FL_PDBrowserFontSize
98 | FL_PDBorderWidth, &cntl);
104 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
105 // We don't abort on BadWindow
106 if (xeev->error_code == BadWindow) {
107 lyxerr << "BadWindow received !" << endl;
108 lyxerr << "If you're using xforms 1.0 or greater, "
109 << " please report this to lyx-devel@lists.lyx.org" << endl;
114 LyX::emergencyCleanup();
116 // Get the reason for the crash.
118 XGetErrorText(display, xeev->error_code, etxt, 512);
119 lyxerr << etxt << " id: " << xeev->resourceid << endl;
120 // By doing an abort we get a nice backtrace. (hopefully)
121 lyx::support::abort();
127 /// read in geometry specification
138 void parse_init(int & argc, char * argv[])
142 FL_CMD_OPT cmdopt[] = {
143 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
146 FL_resource res[] = {
147 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
150 const int num_res = sizeof(res)/sizeof(FL_resource);
152 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
154 // It appears that, in xforms >=0.89.5, fl_initialize()
155 // calls setlocale() and ruins our LC_NUMERIC setting.
158 fl_get_app_resources(res, num_res);
160 Display * display = fl_get_display();
163 lyxerr << "LyX: unable to access X display, exiting" << endl;
164 os::warn("Unable to access X display, exiting");
168 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
170 XSetErrorHandler(LyX_XErrHandler);
172 lyxColorHandler.reset(new LyXColorHandler());
174 using namespace lyx::graphics;
176 // connect the image loader based on the xforms library
177 Image::newImage = boost::bind(&xformsImage::newImage);
178 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
180 // must do this /before/ lyxrc gets read
181 lyxrc.dpi = getDPI();
183 LoaderQueue::setPriority(10,100);
189 XformsColor::read(AddName(user_lyxdir(), "preferences.xform"));
191 if (lyxrc.popup_font_encoding.empty())
192 lyxrc.popup_font_encoding = lyxrc.font_norm;
193 // Set the font name for popups and menus
194 string boldfontname = lyxrc.popup_bold_font
195 + "-*-*-*-?-*-*-*-*-"
196 + lyxrc.popup_font_encoding;
197 // "?" means "scale that font"
198 string fontname = lyxrc.popup_normal_font
199 + "-*-*-*-?-*-*-*-*-"
200 + lyxrc.popup_font_encoding;
202 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
203 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
205 lyxerr << "Could not set menu font to "
206 << boldfontname << endl;
209 lyxerr << "Could not set popup font to "
212 if (bold < 0 && normal < 0) {
213 lyxerr << "Using 'helvetica' font for menus" << endl;
214 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
215 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
216 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
217 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
219 if (bold < 0 && normal < 0) {
220 lyxerr << "Could not find helvetica font. Using 'fixed'." << endl;
221 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
226 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
228 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
230 fl_setpup_fontstyle(FL_NORMAL_STYLE);
231 fl_setpup_fontsize(FL_NORMAL_SIZE);
232 fl_setpup_color(FL_MCOL, FL_BLACK);
233 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
234 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
238 void start(string const & batch, vector<string> const & files)
243 unsigned int width = 690;
244 unsigned int height = 510;
246 int const geometryBitmask =
247 XParseGeometry(geometry,
248 &xpos, &ypos, &width, &height);
250 // if width is not set by geometry, check it against monitor width
251 if (!(geometryBitmask & WidthValue)) {
252 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
253 if (WidthOfScreen(scr) - 8 < int(width))
254 width = WidthOfScreen(scr) - 8;
257 // if height is not set by geometry, check it against monitor height
258 if (!(geometryBitmask & HeightValue)) {
259 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
260 if (HeightOfScreen(scr) - 24 < int(height))
261 height = HeightOfScreen(scr) - 24;
264 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
266 // recalculate xpos if it's not set
268 xpos = (WidthOfScreen(s) - width) / 2;
270 // recalculate ypos if it's not set
272 ypos = (HeightOfScreen(s) - height) / 2;
274 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
275 << '+' << xpos << '+' << ypos << endl;
277 XFormsView view(width, height);
278 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 view.view()->loadLyXFile(*cit, true);
290 // handle the batch commands the user asked for
292 view.getLyXFunc().dispatch(batch);
294 // enter the event loop
296 if (fl_check_forms() == FL_EVENT) {
300 << "Received unhandled X11 event" << endl
301 << "Type: " << ev.xany.type
302 << " Target: 0x" << hex << ev.xany.window
307 // FIXME: breaks emergencyCleanup
324 FuncStatus getStatus(FuncRequest const & /*ev*/)
326 // Nothing interesting to do here
330 string const hexname(LColor::color col)
332 unsigned int r, g, b;
333 bool const success = getRGBColor(col, r, g, b);
335 lyxerr << "X can't find color for \"" << lcolor.getLyXName(col)
342 os << setbase(16) << setfill('0')
347 return STRCONV(os.str());
351 void update_color(LColor::color col)
353 lyxColorHandler->updateColor(col);
363 bool font_available(LyXFont const & font)
365 return fontloader.available(font);
371 void C_read_callback(int, void * data)
373 LyXComm * comm = static_cast<LyXComm *>(data);
379 void set_read_callback(int fd, LyXComm * comm)
381 fl_add_io_callback(fd, FL_READ, C_read_callback, comm);
385 void remove_read_callback(int fd)
387 fl_remove_io_callback(fd, FL_READ, C_read_callback);
391 string const roman_font_name()
397 string const sans_font_name()
403 string const typewriter_font_name()
408 }; // namespace lyx_gui