3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Lars Gullik Bjnes
10 * Full author contact details are available in file CREDITS.
15 // Too hard to make concept checks work with this file
16 #ifdef _GLIBCPP_CONCEPT_CHECKS
17 #undef _GLIBCPP_CONCEPT_CHECKS
23 #include "funcrequest.h"
27 #include "LyXAction.h"
31 #include "graphics/LoaderQueue.h"
33 #include "io_callback.h"
35 // FIXME: move this stuff out again
36 #include "bufferlist.h"
37 #include "buffer_funcs.h"
39 #include "lyxserver.h"
40 #include "lyxsocket.h"
41 #include "BufferView.h"
46 #include "xftFontLoader.h"
47 #include "GWorkArea.h"
50 #include "lyx_forms.h"
51 #include "xformsImage.h"
52 #include "xforms_helpers.h"
54 #include "support/lyxlib.h"
55 #include "support/os.h"
56 #include "support/filetools.h"
57 #include "support/package.h"
61 #include <boost/bind.hpp>
62 #include <boost/function.hpp>
63 #include <boost/shared_ptr.hpp>
70 namespace os = lyx::support::os;
72 using std::ostringstream;
75 using lyx::support::package;
77 using lyx::frontend::colorCache;
78 using lyx::frontend::GView;
79 using lyx::frontend::XformsColor;
82 extern BufferList bufferlist;
84 // FIXME: wrong place !
85 LyXServer * lyxserver;
86 LyXServerSocket * lyxsocket;
88 bool lyx_gui::use_gui = true;
93 bool finished = false;
96 /// estimate DPI from X server
99 Screen * scr = ScreenOfDisplay(getDisplay(), getScreen());
100 return int(((HeightOfScreen(scr) * 25.4 / HeightMMOfScreen(scr)) +
101 (WidthOfScreen(scr) * 25.4 / WidthMMOfScreen(scr))) / 2);
105 /// set default GUI configuration
109 cntl.buttonFontSize = FL_NORMAL_SIZE;
110 cntl.browserFontSize = FL_NORMAL_SIZE;
111 cntl.labelFontSize = FL_NORMAL_SIZE;
112 cntl.choiceFontSize = FL_NORMAL_SIZE;
113 cntl.inputFontSize = FL_NORMAL_SIZE;
114 cntl.menuFontSize = FL_NORMAL_SIZE;
115 cntl.borderWidth = -1;
116 cntl.vclass = FL_DefaultVisual;
117 fl_set_defaults(FL_PDVisual
118 | FL_PDButtonFontSize
119 | FL_PDBrowserFontSize
121 | FL_PDChoiceFontSize
124 | FL_PDBorderWidth, &cntl);
131 int LyX_XErrHandler(Display * display, XErrorEvent * xeev) {
132 // We don't abort on BadWindow
133 if (xeev->error_code == BadWindow) {
134 lyxerr << "BadWindow received !" << std::endl;
135 lyxerr << "If you're using xforms 1.0 or greater, "
136 << " please report this to lyx-devel@lists.lyx.org"
142 LyX::cref().emergencyCleanup();
144 // Get the reason for the crash.
146 XGetErrorText(display, xeev->error_code, etxt, 512);
147 lyxerr << etxt << " id: " << xeev->resourceid << std::endl;
148 // By doing an abort we get a nice backtrace. (hopefully)
149 lyx::support::abort();
155 /// read in geometry specification
161 void parse_init_xforms(int & argc, char * argv[])
165 FL_CMD_OPT cmdopt[] = {
166 {"-geometry", "*.geometry", XrmoptionSepArg, "690x510"}
169 FL_resource res[] = {
170 {"geometry", "geometryClass", FL_STRING, geometry, "", 40}
173 const int num_res = sizeof(res)/sizeof(FL_resource);
174 fl_initialize(&argc, argv, "LyX", cmdopt, num_res);
176 // It appears that, in xforms >=0.89.5, fl_initialize()
177 // calls setlocale() and ruins our LC_NUMERIC setting.
179 fl_get_app_resources(res, num_res);
181 Display * display = fl_get_display();
184 lyxerr << "LyX: unable to access X display, exiting"
189 fcntl(ConnectionNumber(display), F_SETFD, FD_CLOEXEC);
191 XSetErrorHandler(LyX_XErrHandler);
193 using namespace lyx::graphics;
195 // connect the image loader based on the xforms library
196 Image::newImage = boost::bind(&xformsImage::newImage);
197 Image::loadableFormats = boost::bind(&xformsImage::loadableFormats);
201 void lyx_gui::parse_init(int & argc, char * argv[])
203 new Gtk::Main(argc, argv);
205 parse_init_xforms(argc, argv);
209 // must do this /before/ lyxrc gets read
210 lyxrc.dpi = getDPI();
214 void parse_lyxrc_xforms()
216 XformsColor::read(lyx::support::AddName(package().user_support(),
217 "preferences.xform"));
219 if (lyxrc.popup_font_encoding.empty())
220 lyxrc.popup_font_encoding = lyxrc.font_norm;
221 // Set the font name for popups and menus
222 string boldfontname = lyxrc.popup_bold_font
223 + "-*-*-*-?-*-*-*-*-"
224 + lyxrc.popup_font_encoding;
225 // "?" means "scale that font"
226 string fontname = lyxrc.popup_normal_font
227 + "-*-*-*-?-*-*-*-*-"
228 + lyxrc.popup_font_encoding;
230 int bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
231 int normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
233 lyxerr << "Could not set menu font to "
234 << boldfontname << std::endl;
237 lyxerr << "Could not set popup font to "
238 << fontname << std::endl;
240 if (bold < 0 && normal < 0) {
241 lyxerr << "Using 'helvetica' font for menus" << std::endl;
242 boldfontname = "-*-helvetica-bold-r-*-*-*-?-*-*-*-*-iso8859-1";
243 fontname = "-*-helvetica-medium-r-*-*-*-?-*-*-*-*-iso8859-1";
244 bold = fl_set_font_name(FL_BOLD_STYLE, boldfontname.c_str());
245 normal = fl_set_font_name(FL_NORMAL_STYLE, fontname.c_str());
247 if (bold < 0 && normal < 0) {
248 lyxerr << "Could not find helvetica font. Using 'fixed'."
250 fl_set_font_name(FL_NORMAL_STYLE, "fixed");
255 fl_set_font_name(FL_BOLD_STYLE, fontname.c_str());
257 fl_set_font_name(FL_NORMAL_STYLE, boldfontname.c_str());
259 fl_setpup_fontstyle(FL_NORMAL_STYLE);
260 fl_setpup_fontsize(FL_NORMAL_SIZE);
261 fl_setpup_color(FL_MCOL, FL_BLACK);
262 fl_set_goodies_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
263 fl_set_tooltip_font(FL_NORMAL_STYLE, FL_NORMAL_SIZE);
267 void lyx_gui::parse_lyxrc()
269 parse_lyxrc_xforms();
278 unsigned int width = 690;
279 unsigned int height = 510;
281 int const geometryBitmask =
282 XParseGeometry(geometry,
283 &xpos, &ypos, &width, &height);
285 // if width is not set by geometry, check it against monitor width
286 if (!(geometryBitmask & WidthValue)) {
287 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
288 if (WidthOfScreen(scr) - 8 < int(width))
289 width = WidthOfScreen(scr) - 8;
292 // if height is not set by geometry, check it against monitor height
293 if (!(geometryBitmask & HeightValue)) {
294 Screen * scr = ScreenOfDisplay(fl_get_display(), fl_screen);
295 if (HeightOfScreen(scr) - 24 < int(height))
296 height = HeightOfScreen(scr) - 24;
299 Screen * s = ScreenOfDisplay(fl_get_display(), fl_screen);
301 // recalculate xpos if it's not set
303 xpos = (WidthOfScreen(s) - width) / 2;
305 // recalculate ypos if it's not set
307 ypos = (HeightOfScreen(s) - height) / 2;
309 lyxerr[Debug::GUI] << "Creating view: " << width << 'x' << height
310 << '+' << xpos << '+' << ypos << std::endl;
312 // XFormsView view(width, height);
313 // view.show(xpos, ypos, "LyX");
318 static void events_xforms()
320 if (fl_check_forms() == FL_EVENT) {
324 << "Received unhandled X11 event" << std::endl
325 << "Type: " << ev.xany.type
326 << " Target: 0x" << std::hex << ev.xany.window
327 << std::dec << std::endl;
332 void lyx_gui::start(string const & batch, std::vector<string> const & files)
336 XSynchronize(getDisplay(), true);
338 boost::shared_ptr<GView> view_ptr(new GView);
339 LyX::ref().addLyXView(view_ptr);
341 GView & view = *view_ptr.get();
345 // FIXME: server code below needs moving
347 lyxserver = new LyXServer(&view.getLyXFunc(), lyxrc.lyxpipes);
348 lyxsocket = new LyXServerSocket(&view.getLyXFunc(),
349 os::internal_path(package().temp_dir() + "/lyxsocket"));
351 for_each(files.begin(), files.end(),
352 bind(&BufferView::loadLyXFile, view.view(), _1, true));
354 // handle the batch commands the user asked for
355 if (!batch.empty()) {
356 view.getLyXFunc().dispatch(lyxaction.lookupFunc(batch));
359 // enter the event loop
361 while (Gtk::Main::events_pending())
362 Gtk::Main::iteration(false);
366 // FIXME: breaks emergencyCleanup
378 FuncStatus lyx_gui::getStatus(FuncRequest const & ev)
382 // Add this back if the gtk doc prefs dialog includes preamble - jcs
383 /*case LFUN_DIALOG_SHOW:
384 if (ev.argument == "preamble")
387 case LFUN_TOOLTIPS_TOGGLE:
398 string const lyx_gui::hexname(LColor_color col)
401 Gdk::Color * gclr = colorCache.getColor(col);
404 gclr->parse(lcolor.getX11Name(col));
407 std::ostringstream os;
409 // Note that X stores the RGB values in the range 0 - 65535
410 // whilst we require them in the range 0 - 255.
411 os << std::setbase(16) << std::setfill('0')
412 << std::setw(2) << (gclr->get_red() / 256)
413 << std::setw(2) << (gclr->get_green() / 256)
414 << std::setw(2) << (gclr->get_blue() / 256);
420 void lyx_gui::update_color(LColor_color /*col*/)
426 void lyx_gui::update_fonts()
432 bool lyx_gui::font_available(LyXFont const & font)
434 return fontLoader.available(font);
440 std::map<int, boost::shared_ptr<io_callback> > callbacks;
445 void lyx_gui::register_socket_callback(int fd,
446 boost::function<void()> func)
448 callbacks[fd] = boost::shared_ptr<io_callback>(new io_callback(fd, func));
452 void lyx_gui::unregister_socket_callback(int fd)
458 string const lyx_gui::roman_font_name()
464 string const lyx_gui::sans_font_name()
470 string const lyx_gui::typewriter_font_name()
476 void lyx_gui::sync_events()